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
This commit is contained in:
Oscar Hinton 2020-12-04 18:21:34 +01:00 committed by GitHub
parent 0565d6f667
commit c9df039fa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 23 deletions

View File

@ -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';
}

View File

@ -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<boolean>(ElectronConstants.enableTrayKey)) {
this.showTray();
}
if (process.platform === 'win32') {
this.windowMain.win.on('minimize', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey)) {
this.windowMain.win.on('minimize', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey)) {
e.preventDefault();
this.hideToTray();
}
});
this.windowMain.win.on('close', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableCloseToTrayKey)) {
if (!this.windowMain.isQuitting) {
e.preventDefault();
this.hideToTray();
}
});
this.windowMain.win.on('close', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableCloseToTrayKey)) {
if (!this.windowMain.isQuitting) {
e.preventDefault();
this.hideToTray();
}
}
});
}
}
});
this.windowMain.win.on('show', async (e: Event) => {
const enableTray = await this.storageService.get<boolean>(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<boolean>(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<boolean>(ElectronConstants.alwaysShowDock)) {
this.hideDock();
}
} else {
this.windowMain.win.show();
if (this.isDarwin()) {
this.showDock();
}
}
}

View File

@ -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();
}
});