From 486d345dc4147de51f54ecbf65b027ccf5484b8a Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Fri, 12 Jul 2019 21:25:48 +0530 Subject: [PATCH] fix: ELECTRON-1382 (Fix notification z-index when alwaysOnTop is enabled) (#727) * ELECTRON-1382 - Fix notification z-index when alwaysOnTop is enabled --- spec/dialogHandler.spec.ts | 6 ++++++ src/app/window-actions.ts | 12 +++++++++++- src/app/window-handler.ts | 2 +- src/renderer/notification.ts | 20 ++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/spec/dialogHandler.spec.ts b/spec/dialogHandler.spec.ts index ff2594c8b..aede23f1b 100644 --- a/spec/dialogHandler.spec.ts +++ b/spec/dialogHandler.spec.ts @@ -10,6 +10,12 @@ jest.mock('../src/app/window-handler', () => { }; }); +jest.mock('../src/renderer/notification', () => { + return { + setupNotificationPosition: jest.fn(), + }; +}); + jest.mock('electron-log'); describe('dialog handler', () => { diff --git a/src/app/window-actions.ts b/src/app/window-actions.ts index c10ebdfb0..e8a93802e 100644 --- a/src/app/window-actions.ts +++ b/src/app/window-actions.ts @@ -5,6 +5,7 @@ import { isMac, isWindowsOS } from '../common/env'; import { i18n } from '../common/i18n'; import { logger } from '../common/logger'; import { throttle } from '../common/utils'; +import { notification } from '../renderer/notification'; import { config } from './config-handler'; import { ICustomBrowserWindow, windowHandler } from './window-handler'; import { showPopupMenu, windowExists } from './window-utils'; @@ -56,6 +57,11 @@ const windowMaximized = async (): Promise => { const throttledWindowChanges = throttle(async () => { await saveWindowSettings(); await windowMaximized(); + notification.moveNotificationToTop(); +}, 1000); + +const throttledWindowRestore = throttle(async () => { + notification.moveNotificationToTop(); }, 1000); /** @@ -109,7 +115,7 @@ export const updateAlwaysOnTop = (shouldSetAlwaysOnTop: boolean, shouldActivateM const browserWins: ICustomBrowserWindow[] = BrowserWindow.getAllWindows() as ICustomBrowserWindow[]; if (browserWins.length > 0) { browserWins - .filter((browser) => typeof browser.notificationObj !== 'object') + .filter((browser) => typeof browser.notificationData !== 'object') .forEach((browser) => browser.setAlwaysOnTop(shouldSetAlwaysOnTop)); // An issue where changing the alwaysOnTop property @@ -175,6 +181,10 @@ export const monitorWindowActions = (window: BrowserWindow): void => { window.on('leave-full-screen', throttledWindowChanges); window.on('unmaximize', throttledWindowChanges); + + if ((window as ICustomBrowserWindow).winName === apiName.mainWindowName) { + window.on('restore', throttledWindowRestore); + } }; /** diff --git a/src/app/window-handler.ts b/src/app/window-handler.ts index 0b2579202..d84f65aa0 100644 --- a/src/app/window-handler.ts +++ b/src/app/window-handler.ts @@ -35,7 +35,7 @@ interface ICustomBrowserWindowConstructorOpts extends Electron.BrowserWindowCons export interface ICustomBrowserWindow extends Electron.BrowserWindow { winName: string; - notificationObj?: object; + notificationData?: object; origin?: string; } diff --git a/src/renderer/notification.ts b/src/renderer/notification.ts index c73203253..1c01e6f79 100644 --- a/src/renderer/notification.ts +++ b/src/renderer/notification.ts @@ -300,6 +300,21 @@ class Notification extends NotificationHandler { this.inactiveWindows = []; } + /** + * Brings all the notification to the top + * issue: ELECTRON-1382 + */ + public moveNotificationToTop(): void { + const notificationWindows = this.activeNotifications as ICustomBrowserWindow[]; + notificationWindows + .filter((browserWindow) => typeof browserWindow.notificationData === 'object' && browserWindow.isVisible()) + .forEach((browserWindow) => { + if (browserWindow && windowExists(browserWindow) && browserWindow.isVisible()) { + browserWindow.moveTop(); + } + }); + } + /** * Waits for window to load and resolves * @@ -368,6 +383,11 @@ class Notification extends NotificationHandler { if (!notificationWindow || !windowExists(notificationWindow)) { return; } + + if (notificationWindow.notificationData && notificationWindow.notificationData.sticky) { + return; + } + const displayTime = (notificationWindow.notificationData && notificationWindow.notificationData.displayTime) ? notificationWindow.notificationData.displayTime : notificationSettings.displayTime;