Fix dock icon not working when minimized to menu bar, fix window listeners not working after closing the main window (#223)

This commit is contained in:
Oscar Hinton 2020-12-11 15:46:20 +01:00 committed by GitHub
parent ecf1edfb3e
commit 63fe38b3f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import { import {
app, app,
BrowserWindow,
Menu, Menu,
MenuItem, MenuItem,
MenuItemConstructorOptions, MenuItemConstructorOptions,
@ -57,15 +58,17 @@ export class TrayMain {
if (await this.storageService.get<boolean>(ElectronConstants.enableTrayKey)) { if (await this.storageService.get<boolean>(ElectronConstants.enableTrayKey)) {
this.showTray(); this.showTray();
} }
}
this.windowMain.win.on('minimize', async (e: Event) => { setupWindowListeners(win: BrowserWindow) {
win.on('minimize', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey)) { if (await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey)) {
e.preventDefault(); e.preventDefault();
this.hideToTray(); this.hideToTray();
} }
}); });
this.windowMain.win.on('close', async (e: Event) => { win.on('close', async (e: Event) => {
if (await this.storageService.get<boolean>(ElectronConstants.enableCloseToTrayKey)) { if (await this.storageService.get<boolean>(ElectronConstants.enableCloseToTrayKey)) {
if (!this.windowMain.isQuitting) { if (!this.windowMain.isQuitting) {
e.preventDefault(); e.preventDefault();
@ -74,7 +77,7 @@ export class TrayMain {
} }
}); });
this.windowMain.win.on('show', async (e: Event) => { win.on('show', async (e: Event) => {
const enableTray = await this.storageService.get<boolean>(ElectronConstants.enableTrayKey); const enableTray = await this.storageService.get<boolean>(ElectronConstants.enableTrayKey);
if (!enableTray) { if (!enableTray) {
setTimeout(() => this.removeTray(false), 100); setTimeout(() => this.removeTray(false), 100);

View File

@ -23,7 +23,8 @@ export class WindowMain {
constructor(private storageService: StorageService, private hideTitleBar = false, constructor(private storageService: StorageService, private hideTitleBar = false,
private defaultWidth = 950, private defaultHeight = 600, private defaultWidth = 950, private defaultHeight = 600,
private argvCallback: (argv: string[]) => void = null) { } private argvCallback: (argv: string[]) => void = null,
private createWindowCallback: (win: BrowserWindow) => void) { }
init(): Promise<any> { init(): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -82,6 +83,9 @@ export class WindowMain {
// dock icon is clicked and there are no other windows open. // dock icon is clicked and there are no other windows open.
if (this.win === null) { if (this.win === null) {
await this.createWindow(); await this.createWindow();
} else {
// Show the window when clicking on Dock icon
this.win.show();
} }
}); });
@ -169,6 +173,9 @@ export class WindowMain {
this.windowStateChangeHandler(Keys.mainWindowSize, this.win); this.windowStateChangeHandler(Keys.mainWindowSize, this.win);
}); });
if (this.createWindowCallback) {
this.createWindowCallback(this.win);
}
} }
async toggleAlwaysOnTop() { async toggleAlwaysOnTop() {