Skip to content

Commit

Permalink
fix: update prevent flicker on Windows (fixes #274) (#276)
Browse files Browse the repository at this point in the history
* update prevent flicker on Windows

* Update src/Menubar.ts

Co-authored-by: Amaury Martiny <[email protected]>
  • Loading branch information
beoss and amaury1093 authored Apr 27, 2020
1 parent e665f61 commit 9592f34
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/Menubar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { getWindowPosition } from './util/getWindowPosition';
export class Menubar extends EventEmitter {
private _app: Electron.App;
private _browserWindow?: BrowserWindow;
private _blurTimeout: NodeJS.Timeout | null = null; // track blur events with timeout
private _isVisible: boolean; // track visibility
private _cachedBounds?: Electron.Rectangle; // _cachedBounds are needed for double-clicked event
private _options: Options;
// TODO https://github.com/jenslind/electron-positioner/issues/15
Expand All @@ -27,6 +29,7 @@ export class Menubar extends EventEmitter {
super();
this._app = app;
this._options = cleanOptions(options);
this._isVisible = false;

if (app.isReady()) {
// See https://github.com/maxogden/menubar/pull/151
Expand Down Expand Up @@ -97,13 +100,17 @@ export class Menubar extends EventEmitter {
* Hide the menubar window.
*/
hideWindow(): void {
if (!this._browserWindow || !this._browserWindow.isVisible()) {
if (!this._browserWindow || !this._isVisible) {
return;
}

this.emit('hide');
this._browserWindow.hide();
this.emit('after-hide');
this._isVisible = false;
if (this._blurTimeout) {
clearTimeout(this._blurTimeout);
this._blurTimeout = null;
}
}

/**
Expand Down Expand Up @@ -134,7 +141,6 @@ export class Menubar extends EventEmitter {
if (!this._browserWindow) {
throw new Error('Window has been initialized just above. qed.');
}

this.emit('show');

if (trayPos && trayPos.x !== 0) {
Expand Down Expand Up @@ -251,11 +257,18 @@ export class Menubar extends EventEmitter {
if (event && (event.shiftKey || event.ctrlKey || event.metaKey)) {
return this.hideWindow();
}
if (this._browserWindow && this._browserWindow.isVisible()) {

// if blur was invoked clear timeout
if (this._blurTimeout) {
clearInterval(this._blurTimeout);
}

if (this._browserWindow && this._isVisible) {
return this.hideWindow();
}

this._cachedBounds = bounds || this._cachedBounds;
this._isVisible = true;
await this.showWindow(this._cachedBounds);
}

Expand All @@ -281,9 +294,12 @@ export class Menubar extends EventEmitter {
return;
}

// hack to close if icon clicked when open
this._browserWindow.isAlwaysOnTop()
? this.emit('focus-lost')
: this.hideWindow();
: (this._blurTimeout = setTimeout(() => {
this.hideWindow();
}, 100));
});

if (this._options.showOnAllWorkspaces !== false) {
Expand Down

0 comments on commit 9592f34

Please sign in to comment.