diff --git a/app/ipcHandlers.js b/app/ipcHandlers.js index 2a0c0ac..cb7b537 100644 --- a/app/ipcHandlers.js +++ b/app/ipcHandlers.js @@ -13,7 +13,7 @@ const { ipcMain, dialog, shell } = require("electron"); const fs = require("fs"); const { logger, logDir } = require("../server/logger"); -const { configFeatures, featuresPath } = require("../config"); +const {configFeatures, featuresPath, devMode} = require("../config"); const { getThemes, reloadThemes } = require("./themeManager"); const config = require("../config"); const { validateDownloadPath, getDefaultDownloadPath } = require("./pathValidator"); @@ -114,7 +114,16 @@ function registerIpcHandlers(getMainWindow) { /** * Window minimize request from renderer. */ - ipcMain.on("window-minimize", () => getMainWindow()?.minimize()); + ipcMain.on("window-minimize", () => { + // Minimize to tray + if (configFeatures.systemTray) { + getMainWindow()?.hide() + } else { + // Native minimize + getMainWindow()?.minimize() + } + } + ); /** * Toggles maximize/unmaximize state of main window. @@ -223,7 +232,7 @@ function registerIpcHandlers(getMainWindow) { if (key === "systemTray") { if (value === true) { logger.info("System Tray enabled dynamically."); - createSystemTray(getMainWindow(), config.devMode); + createSystemTray(devMode); } else { logger.info("System Tray disabled dynamically."); destroyTray(); diff --git a/app/tray.js b/app/tray.js index 0664d69..56a9b0b 100644 --- a/app/tray.js +++ b/app/tray.js @@ -2,6 +2,7 @@ const {app, Menu, Tray, nativeImage} = require("electron"); const path = require("path"); const {logger} = require("../server/logger"); // Ajuste le chemin si besoin const fs = require("fs"); +const {getMainWindow} = require("./windowManager"); /** * Global reference to the Tray instance to prevent garbage collection. @@ -17,11 +18,10 @@ let tray = null; * - Builds the right-click context menu. * - Handles left-click behavior to toggle main window visibility. * - * @param {import('electron').BrowserWindow} mainWindow - The main application window. * @param {boolean} devMode - Indicates whether the application is running in development mode. * @returns {Tray} The created Tray instance. */ -function createSystemTray(mainWindow, devMode) { +function createSystemTray(devMode) { // Prevent creating multiple tray instances if (tray) return tray; @@ -58,7 +58,11 @@ function createSystemTray(mainWindow, devMode) { { label: "Show Freedom Loader", click: () => { - mainWindow.show(); + const window = getMainWindow(); + if (window && !window.isDestroyed()) { + window.show(); + window.focus(); + } }, }, { type: "separator" }, @@ -82,11 +86,14 @@ function createSystemTray(mainWindow, devMode) { * but it remains standard practice for Windows and Linux environments. */ tray.on("click", () => { - if (mainWindow.isVisible()) { - mainWindow.hide(); + const window = getMainWindow(); + if (!window || window.isDestroyed()) return; + + if (window.isVisible()) { + window.hide(); } else { - mainWindow.show(); - mainWindow.focus(); + window.show(); + window.focus(); } }); diff --git a/app/windowManager.js b/app/windowManager.js index 60cc9af..015b4d8 100644 --- a/app/windowManager.js +++ b/app/windowManager.js @@ -87,7 +87,8 @@ async function createMainWindow() { * cancels the destruction and hides the window instead. */ mainWindow.on('close', (event) => { - if (!app.isQuitting && config.systemTray ) { + + if (!app.isQuitting && config.configFeatures.systemTray) { event.preventDefault(); mainWindow.hide(); return false; diff --git a/main.js b/main.js index 037ab4a..46f56b4 100644 --- a/main.js +++ b/main.js @@ -161,7 +161,7 @@ app.whenReady().then(async () => { getMainWindow().show(); if (configFeatures.systemTray) { - createSystemTray(getMainWindow(), configFeatures.devMode); + createSystemTray(devMode); } if (configFeatures.discordRPC) startRPC();