gpt4 book ai didi

node.js - 隐藏mainWindow时,调用mainWindow.on ('close')多次

转载 作者:行者123 更新时间:2023-12-03 12:34:50 25 4
gpt4 key购买 nike

我做了一个基本的 Electron 托盘,当单击其中一个选项时,它会打开一个窗口。我检查是否用bool打开了BrowserWindow并创建或显示/隐藏了窗口。

const contextMenu = Menu.buildFromTemplate([
{ label: 'Open configuration menu', click:() => {
console.log("called createwin");
createwin();
}

createwin是:
function createwin(){
if (windowshown == false) {
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
webPreferences: {
nodeIntegration: true
}
})
console.log("Window has been created")
windowshown = true;
mainWindow.loadFile('configuration.html')
}
else {
mainWindow.show();
console.log("Window has been shown");
}
mainWindow.on('close', (event) => {
event.preventDefault();
console.log("Window has been hidden");
mainWindow.hide();
})
}


当我将其创建的窗口隐藏/显示为4次时,控制台如下所示:

called createwin
Window has been created
window has been hidden
called createwin
Window has been shown
window has been hidden
window has been hidden
called createwin
Window has been shown
window has been hidden
window has been hidden
window has been hidden
called createwin
window has been hidden
window has been hidden
window has been hidden
window has been hidden

第四次之后,它只是停止响应。我什至都做不到
      mainWindow.removeAllListeners('close');
mainWindow.close()
mainWindow = null
app.quit();

我的问题是我的应用程序第四次隐藏窗口后完全变得无响应。

更新:如果我从 npm start而不是Visual Studio Code的调试器开始,则在隐藏窗口后,该应用程序将变得完全无响应。

我在这里想念什么?

最佳答案

每当您调用close时,就会向createwin事件添加一个新的事件监听器。然后,在关闭窗口时运行每个事件处理程序,从而导致重复window has been hidden

创建新窗口时,只需添加一次事件处理程序:

function createwin(){
if (windowshown == false) {
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
webPreferences: {
nodeIntegration: true
}
})
console.log("Window has been created")
windowshown = true;
mainWindow.loadFile('configuration.html')

mainWindow.on('close', (event) => {
event.preventDefault();
console.log("Window has been hidden");
mainWindow.hide();
})
}
else {
mainWindow.show();
console.log("Window has been shown");
}
}

关于node.js - 隐藏mainWindow时,调用mainWindow.on ('close')多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com