gpt4 book ai didi

javascript - Electron 托盘图标重复问题(Windows 10)

转载 作者:行者123 更新时间:2023-12-03 12:33:10 27 4
gpt4 key购买 nike

我正在建立我的第一个 Electron 项目,但遇到一个托盘图标重复的问题。这里向您展示图片中发生了什么:
[Picture]
我想指出,我正在测试该应用程序。频繁启动和停止它。最终,这些图标确实减少为一个(Windows 10垃圾收集?)。但是,我仍然坚信这是异常行为。
该应用程序本身允许用户打开新窗口来监视事物。由于我不喜欢将“附加”窗口最小化到 Electron 中的位置,因此我将它们最小化时将其设置为“隐藏”。这样的想法是,当用户想要再次显示该窗口时,可以在列表上右键单击系统托盘中的应用程序图标并选择所需的窗口名称,然后从列表中选择它。
我相信这个问题可能与我在更新它时创建和销毁任务栏图标的方式有关。我要做的是销毁托盘,然后在将新窗口名称附加到模板数组之后再次构建它(如下面的代码所示)。
这样做是一个好主意吗? -我还没有看到很多如何做到这一点的例子,所以我自己动手做。
如果您需要更多信息,请随时在下面评论。谢谢!
相关代码:(在 main.js 中)

const iconPath = path.join(__dirname, '/images/testIcon.png')
let tray = null;

function ShowWindow(windowNameFromTray)
{
singleWindow.webContents.send('open-window-from-other-process', windowNameFromTray);
}

ipcMain.on('open-currently-open-window', function(e, windowName)
{
ShowWindow(windowName)
})

let template =
[
{
label: 'windows',
submenu:[]
}
]

ipcMain.on('retrieved-windowId', function(e, windowName)
{
tray.destroy()
tray = new Tray(iconPath)
tray.setToolTip('Window App')

var element =
{
label: windowName,
click()
{
ShowWindow(windowName)
}
}

template[0].submenu.push(element)
let contextMenu = Menu.buildFromTemplate(template)
tray.setContextMenu(contextMenu)
});

...

最佳答案

您好,我也不知道这对您也有同样的问题,但是我发现这与托盘的销毁有关,首先我们必须在main.js文件中创建一个Tray对象,然后我们必须使用该对象全局范围中的纸盘对象。现在我们做错的是我们正在创建任务栏图标,我们没有在破坏其他东西,而是再次创建新东西,现在我的解决方案是在每个地方都共享一个Tray对象,这样我们就可以破坏同一个Tray对象,所以它不会当我们创建一个新文件时创建一个新文件这是我的代码,我正在另一个文件中创建我的纸盘
Main.js

    const electron = require("electron");
let app = null;
let tray = null;
const { app, BrowserWindow, ipcMain,Tray, Menu,dialog} = electron;
const nativeImage = electron.nativeImage
global.share = { app, BrowserWindow, ipcMain,Tray,tray,BackGroudProecess, Menu,mainwin,nativeImage};
const Mytray= require('./tray.js');
Mytray.Create(languagerPack);//you don't need to pass the language pack in here this code according to my developement (obivously you get that ;-) )
tray.js
module.export ={
CreateTray(items){

let p = new Promise((res,rej)=>{
const iconPath = path.join(__dirname+"../../../img/icon.ico");
global.share.tray = new Tray(global.share.nativeImage.createFromPath(iconPath))
res(global.share.tray);//this is the important palce we have pass global tray
})
p.then((tray)=>{
const trayMenuTemplate = [
{


label:items.not_signed,
enabled: false
},

{
label:items.about,// 'About',
click: function () {
global.share.mainwin.webContents.send("OPEN:ABOUT",{state:true});
}
},

{
label:items.help,// 'Help',
click: function () {
console.log("Clicked on Help")
}
},
]

let trayMenu = global.share.Menu.buildFromTemplate(trayMenuTemplate)

if(tray != null){
tray.setContextMenu(trayMenu)
}
}).catch((rej)=>{console.log(rej)})
},

}

}
如何销毁?
   global.share.tray.destroy() // this way it destroys the correct tray since there is only one tray object in the whole program 
而当您破坏和重新装上新托盘时,必须确保旧托盘是否已损坏,这就是您的处理方式
if(global.share.tray.isDestroyed){
console.log("Is Destroyed");
}

关于javascript - Electron 托盘图标重复问题(Windows 10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62935881/

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