mirror of
https://github.com/MasterAcnolo/Freedom-Loader.git
synced 2026-09-28 12:31:14 +02:00
feat: system tray. Working on Windows, should be tested on Linux (GNOME) env. More features to systemTray coming soon
This commit is contained in:
49
app/tray.js
49
app/tray.js
@@ -13,39 +13,46 @@ let tray = null;
|
|||||||
* Creates and configures the cross-platform System Tray.
|
* Creates and configures the cross-platform System Tray.
|
||||||
*
|
*
|
||||||
* Responsibilities:
|
* Responsibilities:
|
||||||
* - Load the appropriate icon for the OS.
|
* - Loads the appropriate icon format based on the operating system.
|
||||||
* - Build the right-click context menu.
|
* - Builds the right-click context menu.
|
||||||
* - Handle left-click behavior (toggle window visibility).
|
* - Handles left-click behavior to toggle main window visibility.
|
||||||
*
|
*
|
||||||
* @param {import('electron').BrowserWindow} mainWindow - The main application window.
|
* @param {import('electron').BrowserWindow} mainWindow - The main application window.
|
||||||
* @param devMode - is application packaged
|
* @param {boolean} devMode - Indicates whether the application is running in development mode.
|
||||||
* @returns {Tray} The created Tray instance.
|
* @returns {Tray} The created Tray instance.
|
||||||
*/
|
*/
|
||||||
function createSystemTray(mainWindow, devMode) {
|
function createSystemTray(mainWindow, devMode) {
|
||||||
// Prevent creating multiple instances
|
// Prevent creating multiple tray instances
|
||||||
if (tray) return tray;
|
if (tray) return tray;
|
||||||
|
|
||||||
/**
|
const isWin = process.platform === "win32";
|
||||||
* Resolve the icon path.
|
const iconName = isWin ? "app-icon.ico" : "app-icon-64x64.png";
|
||||||
* Tip: Use a .png for Linux/macOS and a .ico for Windows for best results.
|
|
||||||
* Here we use a generic PNG assuming it exists in your resources folder.
|
// Attempt to load the icon from the local build directory first
|
||||||
*/
|
let iconPath = path.join(__dirname, "..", "build", iconName);
|
||||||
const iconPath = devMode ?
|
|
||||||
path.join(__dirname, "..", "build", "app-icon-64x64.png") :
|
// Fallback to the extracted resources path if running in production
|
||||||
path.join(process.resourcesPath, "build", "app-icon-64x64.png");
|
if (!devMode && !fs.existsSync(iconPath)) {
|
||||||
|
iconPath = path.join(process.resourcesPath, iconName);
|
||||||
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(iconPath)) {
|
if (!fs.existsSync(iconPath)) {
|
||||||
logger.error(`❌ ERREUR : L'icône du Tray est introuvable à ce chemin : ${iconPath}`);
|
logger.error(`System Tray icon not found at resolved path: ${iconPath}`);
|
||||||
} else {
|
} else {
|
||||||
logger.info(`✅ Icône trouvée pour le Tray !`);
|
logger.info("System Tray icon resolved successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const icon = nativeImage.createFromPath(iconPath);
|
const icon = nativeImage.createFromPath(iconPath);
|
||||||
|
|
||||||
|
if (icon.isEmpty()) {
|
||||||
|
logger.error("System Tray icon file was found but could not be decoded by Electron (image is empty).");
|
||||||
|
}
|
||||||
|
|
||||||
tray = new Tray(icon);
|
tray = new Tray(icon);
|
||||||
tray.setToolTip("Freedom Loader");
|
tray.setToolTip("Freedom Loader");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The context menu (Right-click).
|
* System Tray Context Menu (Right-click action).
|
||||||
*/
|
*/
|
||||||
const contextMenu = Menu.buildFromTemplate([
|
const contextMenu = Menu.buildFromTemplate([
|
||||||
{
|
{
|
||||||
@@ -54,13 +61,13 @@ function createSystemTray(mainWindow, devMode) {
|
|||||||
mainWindow.show();
|
mainWindow.show();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{type: "separator"},
|
{ type: "separator" },
|
||||||
{
|
{
|
||||||
label: "Quit",
|
label: "Quit",
|
||||||
click: () => {
|
click: () => {
|
||||||
/**
|
/**
|
||||||
* Tell the app it's a deliberate exit,
|
* Flags the application for a deliberate exit,
|
||||||
* bypassing the "minimize to tray" behavior.
|
* bypassing the default "minimize to tray" behavior.
|
||||||
*/
|
*/
|
||||||
app.isQuitting = true;
|
app.isQuitting = true;
|
||||||
app.quit();
|
app.quit();
|
||||||
@@ -71,8 +78,8 @@ function createSystemTray(mainWindow, devMode) {
|
|||||||
tray.setContextMenu(contextMenu);
|
tray.setContextMenu(contextMenu);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Left-click behavior: Toggle window visibility.
|
* Left-click behavior: Toggles main window visibility.
|
||||||
* Note: macOS doesn't usually use left-click on tray, but Windows/Linux do.
|
* but it remains standard practice for Windows and Linux environments.
|
||||||
*/
|
*/
|
||||||
tray.on("click", () => {
|
tray.on("click", () => {
|
||||||
if (mainWindow.isVisible()) {
|
if (mainWindow.isVisible()) {
|
||||||
|
|||||||
@@ -84,6 +84,14 @@
|
|||||||
"from": "theme",
|
"from": "theme",
|
||||||
"to": "theme"
|
"to": "theme"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"from": "build/app-icon.ico",
|
||||||
|
"to": "app-icon.ico"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"from": "build/app-icon-64x64.png",
|
||||||
|
"to": "app-icon-64x64.png"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"from": "build/confirm-icon.png",
|
"from": "build/confirm-icon.png",
|
||||||
"to": "confirm-icon.png"
|
"to": "confirm-icon.png"
|
||||||
|
|||||||
Reference in New Issue
Block a user