diff --git a/app/windowManager.js b/app/windowManager.js index 5e7b1d8..60cc9af 100644 --- a/app/windowManager.js +++ b/app/windowManager.js @@ -87,7 +87,7 @@ async function createMainWindow() { * cancels the destruction and hides the window instead. */ mainWindow.on('close', (event) => { - if (!app.isQuitting) { + if (!app.isQuitting && config.systemTray ) { event.preventDefault(); mainWindow.hide(); return false; diff --git a/main.js b/main.js index 964bf21..4ef9eed 100644 --- a/main.js +++ b/main.js @@ -82,8 +82,20 @@ const { createSplashWindow, closeSplashWindow, setSplashProgress } = require("./ const { userThemesPath, initUserThemes, isWindows, validateBinaries, defaultDownloadFolder } = require("./server/helpers/path.helpers"); const {createSystemTray, destroyTray} = require("./app/tray"); +/** + * Global flag indicating if the application is intentionally shutting down. + * Used across the app to bypass the "minimize to tray on close" behavior. + * @type {boolean} + */ app.isQuitting = false; +/** + * State flag to track the asynchronous cleanup process during shutdown. + * Prevents the application from exiting before services (RPC, Tray, Logs) are properly closed. + * @type {boolean} + */ +let isCleanedUp = false; + /** * If another instance want to run */ @@ -145,7 +157,7 @@ app.whenReady().then(async () => { }); app.on("window-all-closed", () => { - if (app.isQuitting) { + if (app.isQuitting || !configFeatures.systemTray) { logger.info("Shutting down..."); app.quit(); } else if (process.platform !== "darwin") { @@ -153,10 +165,25 @@ app.on("window-all-closed", () => { } }); -app.on("before-quit", async () => { +app.on("before-quit", (event) => { app.isQuitting = true; - destroyTray(); - await stopRPC(); - logger.info("All services stopped. Have a nice day!"); - logSessionEnd(); + + if (!isCleanedUp) { + event.preventDefault(); + + (async () => { + try { + destroyTray(); + await stopRPC(); + } catch (err) { + logger.error("Error during cleanup:", err); + } finally { + logger.info("All services stopped. Have a nice day!"); + logSessionEnd(); + + isCleanedUp = true; + app.quit(); + } + })(); + } }); \ No newline at end of file