From 8f6a7a2bd8ac5d69ab9f6d030e71c33c843dba59 Mon Sep 17 00:00:00 2001 From: vb Date: Thu, 8 Jul 2021 16:17:30 +0800 Subject: [PATCH 1/2] fix(setSize): reset position when setSize --- src/Menubar.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Menubar.ts b/src/Menubar.ts index 361c0fe..c751ab7 100644 --- a/src/Menubar.ts +++ b/src/Menubar.ts @@ -139,6 +139,30 @@ export class Menubar extends EventEmitter { throw new Error('Window has been initialized just above. qed.'); } + this.emit('show'); + + this.setPosition(trayPos) + + this._browserWindow.show(); + this._isVisible = true; + this.emit('after-show'); + return; + } + + private async setPosition(trayPos?: Electron.Rectangle): Promise { + if (!this.tray) { + throw new Error('Tray should have been instantiated by now'); + } + + if (!this._browserWindow) { + await this.createWindow(); + } + + // Use guard for TypeScript, to avoid ! everywhere + if (!this._browserWindow) { + throw new Error('Window has been initialized just above. qed.'); + } + // 'Windows' taskbar: sync windows position each time before showing // https://github.com/maxogden/menubar/issues/232 if (['win32', 'linux'].includes(process.platform)) { @@ -146,8 +170,6 @@ export class Menubar extends EventEmitter { this._options.windowPosition = getWindowPosition(this.tray); } - this.emit('show'); - if (trayPos && trayPos.x !== 0) { // Cache the bounds this._cachedBounds = trayPos; @@ -188,9 +210,6 @@ export class Menubar extends EventEmitter { // `.setPosition` crashed on non-integers // https://github.com/maxogden/menubar/issues/233 this._browserWindow.setPosition(Math.round(x), Math.round(y)); - this._browserWindow.show(); - this._isVisible = true; - this.emit('after-show'); return; } @@ -310,6 +329,8 @@ export class Menubar extends EventEmitter { this._browserWindow.on('close', this.windowClear.bind(this)); + this._browserWindow.on('resize', this.setPosition.bind(this)); + // If the user explicity set options.index to false, we don't loadURL // https://github.com/maxogden/menubar/issues/255 if (this._options.index !== false) { From a75344411c775d02b3df602b6f33826febb838d4 Mon Sep 17 00:00:00 2001 From: vb Date: Thu, 8 Jul 2021 17:33:48 +0800 Subject: [PATCH 2/2] fix(position): add default argument --- src/Menubar.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Menubar.ts b/src/Menubar.ts index c751ab7..d17896d 100644 --- a/src/Menubar.ts +++ b/src/Menubar.ts @@ -329,7 +329,7 @@ export class Menubar extends EventEmitter { this._browserWindow.on('close', this.windowClear.bind(this)); - this._browserWindow.on('resize', this.setPosition.bind(this)); + this._browserWindow.on('resize', this.setPosition.bind(this, undefined)); // If the user explicity set options.index to false, we don't loadURL // https://github.com/maxogden/menubar/issues/255