From d5308a3bf512a53adf2a8db70b53d5a26755edcf Mon Sep 17 00:00:00 2001 From: h44z Date: Mon, 3 Dec 2018 21:55:43 +0100 Subject: [PATCH] Close to tray (#21) * Close to Tray implemented * Enable Tray Icon on Linux * Remove unnecessary function * Revert 26a3a98e384cc62a94f2b213af3a6543874b3d95 --- src/electron/baseMenu.ts | 2 +- src/electron/electronConstants.ts | 1 + src/electron/tray.main.ts | 12 ++++++++++++ src/electron/window.main.ts | 7 +++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/electron/baseMenu.ts b/src/electron/baseMenu.ts index 3e7e3d3c59..cf5676c35b 100644 --- a/src/electron/baseMenu.ts +++ b/src/electron/baseMenu.ts @@ -93,7 +93,7 @@ export class BaseMenu { }, { label: this.i18nService.t('close'), - role: 'close', + role: 'quit', }, ], }; diff --git a/src/electron/electronConstants.ts b/src/electron/electronConstants.ts index c22e2b5640..8887e6142e 100644 --- a/src/electron/electronConstants.ts +++ b/src/electron/electronConstants.ts @@ -1,4 +1,5 @@ export class ElectronConstants { static readonly enableMinimizeToTrayKey: string = 'enableMinimizeToTray'; + static readonly enableCloseToTrayKey: string = 'enableCloseToTray'; static readonly enableTrayKey: string = 'enableTray'; } diff --git a/src/electron/tray.main.ts b/src/electron/tray.main.ts index 5a8ec4b3fc..4ae77343a1 100644 --- a/src/electron/tray.main.ts +++ b/src/electron/tray.main.ts @@ -68,6 +68,17 @@ export class TrayMain { }); } + if (process.platform === 'win32') { + this.windowMain.win.on('close', async (e: Event) => { + if (await this.storageService.get(ElectronConstants.enableCloseToTrayKey)) { + if(!this.windowMain.isQuitting){ + e.preventDefault(); + this.hideToTray(); + } + } + }); + } + this.windowMain.win.on('show', async (e: Event) => { const enableTray = await this.storageService.get(ElectronConstants.enableTrayKey); if (!enableTray) { @@ -124,6 +135,7 @@ export class TrayMain { } private closeWindow() { + this.windowMain.isQuitting = true; if (this.windowMain.win != null) { this.windowMain.win.close(); } diff --git a/src/electron/window.main.ts b/src/electron/window.main.ts index d5f9302424..33f7baddd8 100644 --- a/src/electron/window.main.ts +++ b/src/electron/window.main.ts @@ -13,6 +13,7 @@ const Keys = { export class WindowMain { win: BrowserWindow; + isQuitting: boolean = false; private windowStateChangeTimer: NodeJS.Timer; private windowStates: { [key: string]: any; } = {}; @@ -39,6 +40,12 @@ export class WindowMain { } } + // This method will be called when Electron is shutting + // down the application. + app.on('before-quit', () => { + this.isQuitting = true; + }); + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs.