From 172a0bacc0f2c74ffc29c9705e4773de8bbd8267 Mon Sep 17 00:00:00 2001 From: AdamuAbba Date: Tue, 5 Mar 2024 05:57:56 +0100 Subject: [PATCH] fix(windowManager): fixed dark/light mode icon swap bug --- electron/windowManager.ts | 67 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/electron/windowManager.ts b/electron/windowManager.ts index c5737fd01..970022488 100644 --- a/electron/windowManager.ts +++ b/electron/windowManager.ts @@ -1,4 +1,4 @@ -import { app, BrowserWindow, ipcMain, Menu } from 'electron'; +import { app, BrowserWindow, ipcMain, Menu, nativeImage, Tray } from 'electron'; import { warn } from 'electron-log'; import windowState from 'electron-window-state'; import { join } from 'path'; @@ -10,6 +10,7 @@ import { initTapdProxy } from './tapd/tapdProxyServer'; class WindowManager { mainWindow: BrowserWindow | null = null; + tray: Tray | null = null; start() { app.on('ready', async () => { @@ -45,6 +46,12 @@ class WindowManager { enableRemoteModule: true, }, }); + + // create App system tray icon with context menus + if (!this.tray) { + this.createAppTray(); + } + this.mainWindow.setMenuBarVisibility(false); if (IS_DEV) { @@ -92,6 +99,64 @@ class WindowManager { this.createMainWindow(); } } + + /** + * Creates App tray icon with a menu of options + * to `Hide/Show` the app window + * and also `quite` the running app instance + * @returns void + */ + createAppTray() { + const trayIcon = join(APP_ROOT, 'assets', 'icon.png'); + this.tray = new Tray(nativeImage.createFromPath(trayIcon)); + this.tray.setIgnoreDoubleClickEvents(true); + + /** + * `hides` polar windows + */ + const handleOnHideClick = () => { + if (process.platform !== 'darwin') { + app.dock?.hide(); + } + this.mainWindow?.setSkipTaskbar(true); + this.mainWindow?.hide(); + }; + + /** + * `shows` polar window + */ + const handleOnShowClick = () => { + if (process.platform !== 'darwin') { + app.dock?.show(); + } + this.mainWindow?.setSkipTaskbar(false); + this.mainWindow?.show(); + }; + + /** + * closes all windows and quits the app + */ + const handleQuitClick = () => { + app.quit(); + }; + + const contextMenu: Menu = Menu.buildFromTemplate([ + { + label: 'Minimize to tray', + click: handleOnHideClick, + }, + { + label: 'Show window', + click: handleOnShowClick, + }, + { + label: 'Quit Polar', + click: handleQuitClick, + }, + ]); + this.tray.setToolTip('Polar'); + this.tray.setContextMenu(contextMenu); + } } export default WindowManager;