From 917a5125afa0f1e36e39df71b425d3032915ccfc Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 25 Jul 2025 14:37:01 +0800 Subject: [PATCH] :bug: The tray is generated repeatedly after the new window is locked and then entered https://github.com/siyuan-note/siyuan/issues/15357 --- app/electron/main.js | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index b019add3b..afac87281 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -678,6 +678,10 @@ for (let i = argStart; i < process.argv.length; i++) { app.whenReady().then(() => { const resetTrayMenu = (tray, lang, mainWindow) => { + if (!mainWindow || mainWindow.isDestroyed()) { + return; + } + const trayMenuTemplate = [{ label: mainWindow.isVisible() ? lang.hideWindow : lang.showWindow, click: () => { showHideWindow(tray, lang, mainWindow); @@ -726,6 +730,10 @@ app.whenReady().then(() => { } }; const showHideWindow = (tray, lang, mainWindow) => { + if (!mainWindow || mainWindow.isDestroyed()) { + return; + } + if (!mainWindow.isVisible()) { if (mainWindow.isMinimized()) { mainWindow.restore(); @@ -1113,23 +1121,24 @@ app.whenReady().then(() => { if (exitWS) { return; } - let tray; - if ("win32" === process.platform || "linux" === process.platform) { - // 系统托盘 - tray = new Tray(path.join(appDir, "stage", "icon-large.png")); - tray.setToolTip(`${path.basename(data.workspaceDir)} - SiYuan v${appVer}`); - const mainWindow = getWindowByContentId(event.sender.id); - if (!mainWindow) { - return; - } - resetTrayMenu(tray, data.languages, mainWindow); - tray.on("click", () => { - showHideWindow(tray, data.languages, mainWindow); - }); - } + workspaces.find(item => { if (!item.workspaceDir) { item.workspaceDir = data.workspaceDir; + let tray; + if ("win32" === process.platform || "linux" === process.platform) { + // 系统托盘 + tray = new Tray(path.join(appDir, "stage", "icon-large.png")); + tray.setToolTip(`${path.basename(data.workspaceDir)} - SiYuan v${appVer}`); + const mainWindow = getWindowByContentId(event.sender.id); + if (!mainWindow || mainWindow.isDestroyed()) { + return; + } + resetTrayMenu(tray, data.languages, mainWindow); + tray.on("click", () => { + showHideWindow(tray, data.languages, mainWindow); + }); + } item.tray = tray; return true; }