From f9989c52c559acfcbc0ff5a4670772f33f79937c Mon Sep 17 00:00:00 2001 From: Brad Decker Date: Sun, 19 Apr 2020 17:00:17 -0500 Subject: [PATCH] Position notification relative to last focused window (#8356) --- app/scripts/lib/notification-manager.js | 23 ++++++++++++++++++----- app/scripts/platforms/extension.js | 12 ++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/scripts/lib/notification-manager.js b/app/scripts/lib/notification-manager.js index 666c1f8c8..218dd290e 100644 --- a/app/scripts/lib/notification-manager.js +++ b/app/scripts/lib/notification-manager.js @@ -29,17 +29,30 @@ class NotificationManager { // bring focus to existing chrome popup await this.platform.focusWindow(popup.id) } else { - const { screenX, screenY, outerWidth, outerHeight } = window - const notificationTop = Math.round(screenY + (outerHeight / 2) - (NOTIFICATION_HEIGHT / 2)) - const notificationLeft = Math.round(screenX + (outerWidth / 2) - (NOTIFICATION_WIDTH / 2)) + let left = 0 + let top = 0 + try { + const lastFocused = await this.platform.getLastFocusedWindow() + // Position window in top right corner of lastFocused window. + top = lastFocused.top + left = lastFocused.left + (lastFocused.width - NOTIFICATION_WIDTH) + } catch (_) { + // The following properties are more than likely 0, due to being + // opened from the background chrome process for the extension that + // has no physical dimensions + const { screenX, screenY, outerWidth } = window + top = Math.max(screenY, 0) + left = Math.max(screenX + (outerWidth - NOTIFICATION_WIDTH), 0) + } + // create new notification popup const popupWindow = await this.platform.openWindow({ url: 'notification.html', type: 'popup', width: NOTIFICATION_WIDTH, height: NOTIFICATION_HEIGHT, - top: Math.max(notificationTop, 0), - left: Math.max(notificationLeft, 0), + left, + top, }) this._popupId = popupWindow.id } diff --git a/app/scripts/platforms/extension.js b/app/scripts/platforms/extension.js index 5f0e308d8..831b158c9 100644 --- a/app/scripts/platforms/extension.js +++ b/app/scripts/platforms/extension.js @@ -60,6 +60,18 @@ class ExtensionPlatform { }) } + getLastFocusedWindow () { + return new Promise((resolve, reject) => { + extension.windows.getLastFocused((windowObject) => { + const error = checkForError() + if (error) { + return reject(error) + } + return resolve(windowObject) + }) + }) + } + closeCurrentWindow () { return extension.windows.getCurrent((windowDetails) => { return extension.windows.remove(windowDetails.id)