From 4c063251e0d5b48b5f968a249bc410444effef2c Mon Sep 17 00:00:00 2001 From: SiriusXT <1160925501@qq.com> Date: Sun, 30 Mar 2025 17:39:28 +0800 Subject: [PATCH] Fix program crash during initial setup and the tray not showing. --- src/routes/setup.ts | 7 ++++++- src/services/tray.ts | 6 ++++-- src/services/window.ts | 7 +++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/routes/setup.ts b/src/routes/setup.ts index 9ccb4cf33..5bb0f56c9 100644 --- a/src/routes/setup.ts +++ b/src/routes/setup.ts @@ -37,8 +37,13 @@ function setupPage(req: Request, res: Response) { async function handleElectronRedirect() { const windowService = (await import("../services/window.js")).default; const { app } = await import("electron"); - windowService.createMainWindow(app); + + // Wait for the main window to be created before closing the setup window to prevent triggering `window-all-closed`. + await windowService.createMainWindow(app); windowService.closeSetupWindow(); + + const tray = (await import("../services/tray.js")).default; + tray.createTray(); } export default { diff --git a/src/services/tray.ts b/src/services/tray.ts index 2933e49fc..82137174d 100644 --- a/src/services/tray.ts +++ b/src/services/tray.ts @@ -95,6 +95,9 @@ function updateWindowVisibilityMap(allWindows: BrowserWindow[]) { function updateTrayMenu() { + if (!tray) { + return; + } const lastFocusedWindow = windowService.getLastFocusedWindow(); const allWindows = windowService.getAllWindows(); updateWindowVisibilityMap(allWindows); @@ -299,6 +302,5 @@ function createTray() { } export default { - createTray, - updateTrayMenu + createTray }; diff --git a/src/services/window.ts b/src/services/window.ts index 12c125b6d..a575d27d6 100644 --- a/src/services/window.ts +++ b/src/services/window.ts @@ -11,7 +11,6 @@ import remoteMain from "@electron/remote/main/index.js"; import { BrowserWindow, shell, type App, type BrowserWindowConstructorOptions, type WebContents } from "electron"; import { dialog, ipcMain } from "electron"; import { formatDownloadTitle, isDev, isMac, isWindows } from "./utils.js"; -import tray from "./tray.js"; import { fileURLToPath } from "url"; import { dirname } from "path"; @@ -29,14 +28,14 @@ function trackWindowFocus(win: BrowserWindow) { allWindows = allWindows.filter(w => !w.isDestroyed() && w !== win); allWindows.push(win); if (!optionService.getOptionBool("disableTray")) { - tray.updateTrayMenu(); + ipcMain.emit("reload-tray"); } }); win.on("closed", () => { allWindows = allWindows.filter(w => !w.isDestroyed()); if (!optionService.getOptionBool("disableTray")) { - tray.updateTrayMenu(); + ipcMain.emit("reload-tray"); } }); } @@ -326,7 +325,7 @@ function getLastFocusedWindow() { return allWindows.length > 0 ? allWindows[allWindows.length - 1] : null; } -function getAllWindows(){ +function getAllWindows() { return allWindows; }