From c9df039fa9f392462130b733b4a724a024672866 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Fri, 4 Dec 2020 18:21:34 +0100 Subject: [PATCH] Desktop fit & finish (#212) * Add context menu on right click to mac * Add hide dock setting * Change "hide dock" to "always show dock" * Add support on mac for minimize to menu bar on close, minimize or start * Add "openAtLogin" to ElectronConstants * Add "restoreFromTray" to TrayMainService --- src/electron/electronConstants.ts | 2 + src/electron/tray.main.ts | 70 +++++++++++++++++++++---------- src/electron/window.main.ts | 2 +- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/electron/electronConstants.ts b/src/electron/electronConstants.ts index 849cd7722eb..47510bb68b6 100644 --- a/src/electron/electronConstants.ts +++ b/src/electron/electronConstants.ts @@ -7,4 +7,6 @@ export class ElectronConstants { static readonly minimizeOnCopyToClipboardKey: string = 'minimizeOnCopyToClipboardKey'; static readonly enableBiometric: string = 'enabledBiometric'; static readonly enableBrowserIntegration: string = 'enableBrowserIntegration'; + static readonly alwaysShowDock: string = 'alwaysShowDock'; + static readonly openAtLogin: string = 'openAtLogin'; } diff --git a/src/electron/tray.main.ts b/src/electron/tray.main.ts index 7e31b632fd6..a782d95e12b 100644 --- a/src/electron/tray.main.ts +++ b/src/electron/tray.main.ts @@ -1,4 +1,5 @@ import { + app, Menu, MenuItem, MenuItemConstructorOptions, @@ -44,7 +45,7 @@ export class TrayMain { }, { type: 'separator' }, { - label: process.platform === 'darwin' ? this.i18nService.t('close') : this.i18nService.t('exit'), + label: this.i18nService.t('exit'), click: () => this.closeWindow(), }]; @@ -52,30 +53,26 @@ export class TrayMain { menuItemOptions.splice(1, 0, ...additionalMenuItems); } - if (process.platform !== 'darwin') { - this.contextMenu = Menu.buildFromTemplate(menuItemOptions); - } + this.contextMenu = Menu.buildFromTemplate(menuItemOptions); if (await this.storageService.get(ElectronConstants.enableTrayKey)) { this.showTray(); } - if (process.platform === 'win32') { - this.windowMain.win.on('minimize', async (e: Event) => { - if (await this.storageService.get(ElectronConstants.enableMinimizeToTrayKey)) { + this.windowMain.win.on('minimize', async (e: Event) => { + if (await this.storageService.get(ElectronConstants.enableMinimizeToTrayKey)) { + e.preventDefault(); + this.hideToTray(); + } + }); + + 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('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); @@ -96,11 +93,20 @@ export class TrayMain { } } - hideToTray() { + async hideToTray() { this.showTray(); if (this.windowMain.win != null) { this.windowMain.win.hide(); } + if (this.isDarwin() && !await this.storageService.get(ElectronConstants.alwaysShowDock)) { + this.hideDock(); + } + } + + restoreFromTray() { + if (this.windowMain.win == null || !this.windowMain.win.isVisible()) { + this.toggleWindow(); + } } showTray() { @@ -111,29 +117,49 @@ export class TrayMain { this.tray = new Tray(this.icon); this.tray.setToolTip(this.appName); this.tray.on('click', () => this.toggleWindow()); + this.tray.on('right-click', () => this.tray.popUpContextMenu(this.contextMenu)); if (this.pressedIcon != null) { this.tray.setPressedImage(this.pressedIcon); } - if (this.contextMenu != null) { + if (this.contextMenu != null && !this.isDarwin()) { this.tray.setContextMenu(this.contextMenu); } } - private toggleWindow() { + private hideDock() { + app.dock.hide(); + } + + private showDock() { + app.dock.show(); + } + + private isDarwin() { + return process.platform === 'darwin'; + } + + private async toggleWindow() { if (this.windowMain.win == null) { - if (process.platform === 'darwin') { + if (this.isDarwin()) { // On MacOS, closing the window via the red button destroys the BrowserWindow instance. this.windowMain.createWindow().then(() => { this.windowMain.win.show(); + this.showDock(); }); } return; } if (this.windowMain.win.isVisible()) { this.windowMain.win.hide(); + if (this.isDarwin() && !await this.storageService.get(ElectronConstants.alwaysShowDock)) { + this.hideDock(); + } } else { this.windowMain.win.show(); + if (this.isDarwin()) { + this.showDock(); + } } } diff --git a/src/electron/window.main.ts b/src/electron/window.main.ts index e650ad38d6a..e1a1498da8e 100644 --- a/src/electron/window.main.ts +++ b/src/electron/window.main.ts @@ -72,7 +72,7 @@ export class WindowMain { app.on('window-all-closed', () => { // On OS X it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q - if (process.platform !== 'darwin' || isMacAppStore()) { + if (process.platform !== 'darwin' || this.isQuitting || isMacAppStore()) { app.quit(); } });