[英]Multiple Windows in Electron
I've been looking for hours, and I cant find a way to build a working setup to use multiple windows in my Electron app.我一直在寻找几个小时,但我无法找到一种方法来构建工作设置以在我的 Electron 应用程序中使用多个 windows。
I'm using electron-forge
with plain vanilla JS.我正在使用带有普通 JS
electron-forge
。 No react or vue.没有反应或Vue。
What I'm trying to achieve is popups that I can trigger from inside the main application Dashboard using functions.我想要实现的是可以使用函数从主应用程序仪表板内部触发的弹出窗口。
Here's what I have for HTML:这是我为 HTML 提供的:
<i onclick="newBot()" class="fas fa-plus new-bot-button bar-icon"></i>
<i onclick="selectBot()" class="fas fa-box-open select-bot-button bar-icon"></i>
<i onclick="startBot()" class="fas fa-play start-bot-button bar-icon"></i>
<i onclick="stopBot()" class="fas fa-stop stop-bot-button bar-icon"></i>
<i onclick="about()" class="fas fa-info about-button bar-icon"></i>
I'm completely clueless on how to implement this in JavaScript, so help would be greatly appreciated.我完全不知道如何在 JavaScript 中实现这一点,因此将不胜感激。
Note, I have a preload script mainPreload.js
and the main script is main.js
.注意,我有一个预加载脚本
mainPreload.js
,主脚本是main.js
。 The window is also frameless as well, and have a custom title bar that I coded. window 也是无框的,并且有一个我编写的自定义标题栏。 It looks like this.
它看起来像这样。 The icons referenced above are the set on the far right.
上面引用的图标是最右边的集合。
Here's my main.js
:这是我的
main.js
:
const { ipcMain, app, BrowserWindow } = require('electron')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
resizable: false,
webPreferences: {
preload: path.join(__dirname, 'mainPreload.js'),
nodeIntegration: true,
contextIsolation: false
},
frame: false,
icon: './assets/toggle-01.png'
})
win.loadFile('index.html');
ipcMain.on('minimize', () => {
win.minimize()
});
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
Here's my mainPreload.js
:这是我的
mainPreload.js
:
function scrollDown() {
document.getElementById('console-text').scrollTop = document.getElementById('console-text').scrollHeight
}
window.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('console-text');
console.log = function(message) {
const p = container.appendChild(document.createElement('p'));
p.textContent = message;
p.className = 'console-output';
scrollDown();
};
window.addEventListener('error', (error) => {
const p = container.appendChild(document.createElement('p'));
p.textContent = error.message;
p.className = 'console-output';
scrollDown();
});
console.log('Ready')
})
Thanks!谢谢!
To show new windows you just need to create new BrowserWindow instances.要显示新的 windows,您只需创建新的 BrowserWindow 实例。 You can refer to ElectronJS samples on GitHub
您可以参考GitHub 上的 ElectronJS 示例
Below is a code based on the Create Window demo.下面是基于 Create Window 演示的代码。
Add an ID to your button (not the best idea to use <i>
tag for this btw):为您的按钮添加一个 ID(使用
<i>
标签不是最好的办法):
<i id="id-about" class="fas fa-info about-button bar-icon"></i>
Then create a click event handler.然后创建一个单击事件处理程序。 Put this code into your renderer process (in your case a JS running in your
index.html
).将此代码放入您的渲染器进程(在您的情况下,JS 在您的
index.html
中运行)。
const {BrowserWindow} = require('electron').remote
const path = require('path')
const newWindowBtn = document.getElementById('id-about')
newWindowBtn.addEventListener('click', (event) => {
const modalPath = path.join('file://', __dirname, '../../sections/windows/about.html')
let win = new BrowserWindow({ width: 400, height: 320 })
win.on('close', () => { win = null })
win.loadURL(modalPath)
win.show()
})
Hope this helps.希望这可以帮助。 You can find more info in the ElectronJS Docs ( BrowserWindow in particular)
您可以在ElectronJS 文档(特别是BrowserWindow )中找到更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.