From e29537f83865e073747fc8ec46348f8a3c2379b0 Mon Sep 17 00:00:00 2001 From: viktor44 Date: Sat, 30 Nov 2024 01:21:07 +0100 Subject: [PATCH] Minimize to tray (bug fix) --- .../services/electron-app/interface.ts | 12 +++++++++ .../services/services/electron-app/main.ts | 4 +++ .../services/services/electron-app/worker.ts | 27 +++++++++++++++++++ .../src/windows/utils/MainWindowManager.ts | 8 ++++++ 4 files changed, 51 insertions(+) create mode 100644 packages/app/src/services/services/electron-app/worker.ts diff --git a/packages/app/src/services/services/electron-app/interface.ts b/packages/app/src/services/services/electron-app/interface.ts index c71755dc..d6c83d0a 100644 --- a/packages/app/src/services/services/electron-app/interface.ts +++ b/packages/app/src/services/services/electron-app/interface.ts @@ -26,6 +26,8 @@ export class ElectronAppService extends ServiceBase implements RPC.Interface {} + // @ts-ignore + trayIconVisible(): Promise {} // @ts-ignore addObserver(obs: RPC.ObserverNode): Promise {} } @@ -41,3 +43,13 @@ export interface AppMetadata { name: string, version: string, } + +@service('browser-window') +export class ElectronAppServiceProviderService extends ServiceBase implements RPC.Interface { + // @ts-ignore + showTrayIcon(): Promise {} + // @ts-ignore + hideTrayIcon(): Promise {} + // @ts-ignore + trayIconVisible(): Promise {} +} diff --git a/packages/app/src/services/services/electron-app/main.ts b/packages/app/src/services/services/electron-app/main.ts index 3953a62d..792e994f 100644 --- a/packages/app/src/services/services/electron-app/main.ts +++ b/packages/app/src/services/services/electron-app/main.ts @@ -128,6 +128,10 @@ export class ElectronAppServiceImpl extends ElectronAppService implements RPC.In return new ServiceSubscription(subscriptions, obs); } + async trayIconVisible() { + return await this.provider?.trayIconVisible() || false; + } + private initPrepareQuit() { app.on('before-quit', (event) => { if (!this.appCanQuit) { diff --git a/packages/app/src/services/services/electron-app/worker.ts b/packages/app/src/services/services/electron-app/worker.ts new file mode 100644 index 00000000..00660a4a --- /dev/null +++ b/packages/app/src/services/services/electron-app/worker.ts @@ -0,0 +1,27 @@ +// @ts-ignore: no declaration file +import { setMinimizeToTray } from '../../../app/duck'; +import { getAppMinimizeToTrayStatus } from '../../../app/selectors'; +import { StationStoreWorker } from '../../../types'; +import { RPC } from '../../lib/types'; +import { ElectronAppServiceProviderService } from './interface'; + +export class ElectronAppServiceProviderServiceImpl extends ElectronAppServiceProviderService implements RPC.Interface { + store: StationStoreWorker; + + constructor(store: StationStoreWorker, uuid?: string) { + super(uuid); + this.store = store; + } + + async showTrayIcon() { + this.store.dispatch(setMinimizeToTray(true)); + } + + async hideTrayIcon() { + this.store.dispatch(setMinimizeToTray(false)); + } + + async trayIconVisible() { + return getAppMinimizeToTrayStatus(this.store.getState()) || false; + } +} diff --git a/packages/app/src/windows/utils/MainWindowManager.ts b/packages/app/src/windows/utils/MainWindowManager.ts index 2518bba2..9807b300 100644 --- a/packages/app/src/windows/utils/MainWindowManager.ts +++ b/packages/app/src/windows/utils/MainWindowManager.ts @@ -1,6 +1,7 @@ import { getUrlToLoad } from '../../utils/dev'; import { isDarwin } from '../../utils/process'; import { getResourceIconPath } from '../../utils/resources'; +// @ts-ignore: no declaration file import { windowCreated } from '../duck'; import GenericWindowManager from './GenericWindowManager'; @@ -25,6 +26,13 @@ export default class MainWindowManager extends GenericWindowManager { savePosition: 'main-window', }); + this.on('minimize', async () => { + const trayIconVisible = await services.electronApp.trayIconVisible() + if (trayIconVisible) { + services.browserWindow.hideAllWindows(); + } + }); + await super.load(); return this.window!;