Skip to content

Commit

Permalink
KEEP-1731: Fix notification popup opening
Browse files Browse the repository at this point in the history
  • Loading branch information
domnikov-timofei committed Jul 5, 2023
1 parent 5e0b22c commit d0fc054
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async function setupBackgroundService() {
});

// Notification window management
const windowManager = new WindowManager({ extensionStorage });
const windowManager = new WindowManager();
backgroundService.on(
'Show notification',
windowManager.showWindow.bind(windowManager)
Expand Down
3 changes: 2 additions & 1 deletion src/copied/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"clipboardWrite",
"idle",
"storage",
"unlimitedStorage"
"unlimitedStorage",
"tabs"
]
}
103 changes: 43 additions & 60 deletions src/lib/windowManager.ts
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);
}
}

0 comments on commit d0fc054

Please sign in to comment.