-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEEP-1731: Fix notification popup opening
- Loading branch information
1 parent
5e0b22c
commit d0fc054
Showing
3 changed files
with
46 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
"clipboardWrite", | ||
"idle", | ||
"storage", | ||
"unlimitedStorage" | ||
"unlimitedStorage", | ||
"tabs" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,63 @@ | ||
import ObservableStore from 'obs-store'; | ||
import invariant from 'tiny-invariant'; | ||
import Browser from 'webextension-polyfill'; | ||
|
||
import { type ExtensionStorage } from '../storage/storage'; | ||
|
||
const height = 622; | ||
const width = 357; | ||
const NOTIFICATION_WINDOW_URL = Browser.runtime.getURL('/notification.html'); | ||
|
||
export class WindowManager { | ||
#store; | ||
#lastWindowPromise: Promise<Browser.Windows.Window | void> = | ||
Promise.resolve(); | ||
|
||
async #getNotificationWindowId() { | ||
const windows = await Browser.windows.getAll({ | ||
populate: true, | ||
windowTypes: ['popup'], | ||
}); | ||
|
||
constructor({ extensionStorage }: { extensionStorage: ExtensionStorage }) { | ||
this.#store = new ObservableStore( | ||
extensionStorage.getInitState({ | ||
notificationWindowId: undefined, | ||
inShowMode: undefined, | ||
}) | ||
const win = windows.find(w => | ||
w.tabs?.some(tab => tab.url?.startsWith(NOTIFICATION_WINDOW_URL)) | ||
); | ||
|
||
extensionStorage.subscribe(this.#store); | ||
return win?.id; | ||
} | ||
|
||
async showWindow() { | ||
const { inShowMode } = this.#store.getState(); | ||
|
||
if (inShowMode) { | ||
return null; | ||
} | ||
|
||
this.#store.updateState({ inShowMode: true }); | ||
|
||
const notificationWindow = await this.#getNotificationWindow(); | ||
|
||
if (notificationWindow?.id) { | ||
await Browser.windows.update(notificationWindow.id, { focused: true }); | ||
} else { | ||
const { id: notificationWindowId } = await Browser.windows.create({ | ||
url: 'notification.html', | ||
type: 'popup', | ||
focused: true, | ||
width, | ||
height, | ||
showWindow() { | ||
this.#lastWindowPromise = this.#lastWindowPromise | ||
.catch(() => undefined) | ||
.then(async win => { | ||
const notificationWindowId = | ||
win?.id ?? (await this.#getNotificationWindowId()); | ||
|
||
try { | ||
invariant(notificationWindowId); | ||
return await Browser.windows.update(notificationWindowId, { | ||
focused: true, | ||
}); | ||
} catch (e) { | ||
return Browser.windows.create({ | ||
url: NOTIFICATION_WINDOW_URL, | ||
type: 'popup', | ||
focused: true, | ||
width: 357, | ||
height: 622, | ||
}); | ||
} | ||
}); | ||
|
||
this.#store.updateState({ notificationWindowId }); | ||
} | ||
|
||
this.#store.updateState({ inShowMode: false }); | ||
} | ||
|
||
async #getNotificationWindow() { | ||
const windows = | ||
(await Browser.windows.getAll({ windowTypes: ['popup'] })) ?? []; | ||
|
||
const { notificationWindowId } = this.#store.getState(); | ||
|
||
return windows.find(window => window.id === notificationWindowId); | ||
} | ||
|
||
async resizeWindow(newWidth: number, newHeight: number) { | ||
const notificationWindow = await this.#getNotificationWindow(); | ||
const notificationWindowId = await this.#getNotificationWindowId(); | ||
if (notificationWindowId == null) return; | ||
|
||
if (notificationWindow?.id) { | ||
await Browser.windows.update(notificationWindow.id, { | ||
width: newWidth, | ||
height: newHeight, | ||
}); | ||
} | ||
await Browser.windows.update(notificationWindowId, { | ||
width: newWidth, | ||
height: newHeight, | ||
}); | ||
} | ||
|
||
async closeWindow() { | ||
const notificationWindow = await this.#getNotificationWindow(); | ||
|
||
if (notificationWindow?.id) { | ||
await Browser.windows.remove(notificationWindow.id); | ||
} | ||
const notificationWindowId = await this.#getNotificationWindowId(); | ||
if (notificationWindowId == null) return; | ||
|
||
this.#store.updateState({ notificationWindowId: undefined }); | ||
await Browser.windows.remove(notificationWindowId); | ||
} | ||
} |