Skip to content

Commit

Permalink
feat: improve windows managemet when switching to popup
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonmaslowski committed Feb 19, 2025
1 parent 42c71a2 commit b29e275
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { closeAllLaceWindows } from '@lib/scripts/background/util';
import { closeAllLaceOrNamiTabs } from '@lib/scripts/background/util';
import { MessageSender, NamiMessages } from '../shared/types';
import { logger } from '@lace/common';

Expand All @@ -8,7 +8,7 @@ export const createLaceMigrationOpenListener =
logger.debug('[NAMI MIGRATION] createLaceMigrationOpenListener', message, sender);
if (message === NamiMessages.open && sender.id === namiExtensionId) {
// First close all open lace tabs
await closeAllLaceWindows();
await closeAllLaceOrNamiTabs();
createTab({ url: `chrome-extension://${laceExtensionId}/app.html` });
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-magic-numbers */
import { runtime, tabs, storage as webStorage } from 'webextension-polyfill';
import { runtime, tabs, storage as webStorage, windows, action, Tabs, Windows } from 'webextension-polyfill';
import {
BackgroundService,
BaseChannels,
Expand All @@ -22,12 +22,13 @@ import { backgroundServiceProperties } from '../config';
import { exposeApi } from '@cardano-sdk/web-extension';
import { Cardano } from '@cardano-sdk/core';
import { config } from '@src/config';
import { getADAPriceFromBackgroundStorage, closeAllLaceWindows } from '../util';
import { getADAPriceFromBackgroundStorage, closeAllLaceOrNamiTabs } from '../util';
import { currencies as currenciesMap, currencyCode } from '@providers/currency/constants';
import { clearBackgroundStorage, getBackgroundStorage, setBackgroundStorage } from '../storage';
import { laceFeaturesApiProperties, LACE_FEATURES_CHANNEL } from '../injectUtil';
import { getErrorMessage } from '@src/utils/get-error-message';
import { logger } from '@lace/common';
import { POPUP_WINDOW_NAMI_TITLE } from '@utils/constants';

export const requestMessage$ = new Subject<Message>();
export const backendFailures$ = new BehaviorSubject(0);
Expand Down Expand Up @@ -109,18 +110,38 @@ const handleOpenNamiBrowser = async (data: OpenNamiBrowserData) => {
await tabs.create({ url: `popup.html#${data.path}` }).catch((error) => logger.error(error));
};

const isItLaceOrNamiTab = (tab: Tabs.Tab) => ['Lace', POPUP_WINDOW_NAMI_TITLE].includes(tab.title);
const doesWindowHaveOtherTabs = (browserWindow: Windows.Window) =>
browserWindow.tabs.some((t) => !isItLaceOrNamiTab(t));
const ensureTabsAvailable = (browserWindows: Windows.Window[]) => {
const promises = browserWindows.map(async (w) => ({
...w,
tabs: w.tabs || (await tabs.query({ windowId: w.id }))
}));
return Promise.all(promises);
};

const handleOpenPopup = async () => {
if (typeof chrome.action.openPopup !== 'function') return;
try {
const [currentWindow] = await tabs.query({ currentWindow: true, title: 'Lace' });
await closeAllLaceWindows();
// behaves inconsistently if executed without setTimeout
setTimeout(async () => {
if (currentWindow?.windowId) {
await chrome.windows.update(currentWindow.windowId, { focused: true });
await chrome.action.openPopup();
}
}, 30);
const allWindows = await windows.getAll();
if (allWindows.length === 0) return;

const allWindowsWithTheirTabs = await ensureTabsAvailable(allWindows);
const windowsWithTabsOtherThanLaceOrNami = allWindowsWithTheirTabs.filter((w) => doesWindowHaveOtherTabs(w));
const windowsOrderedWithActiveFirst = windowsWithTabsOtherThanLaceOrNami.sort(
(w1, w2) => Number(w2.focused) - Number(w1.focused)
);

let windowToBeUsed = windowsOrderedWithActiveFirst[0];
const allWindowsHaveOnlyLaceOrNamiTabs = !windowToBeUsed;
if (allWindowsHaveOnlyLaceOrNamiTabs) {
windowToBeUsed = allWindowsWithTheirTabs[0];
await tabs.create({ active: true, windowId: windowToBeUsed.id });
}

await windows.update(windowToBeUsed.id, { focused: true });
await closeAllLaceOrNamiTabs();
await action.openPopup();
} catch (error) {
// unable to programatically open the popup again
logger.error(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const getActiveWallet = async ({
return { wallet, account };
};

export const closeAllLaceWindows = async (shouldRemoveTab?: (url: string) => boolean): Promise<void> => {
export const closeAllLaceOrNamiTabs = async (shouldRemoveTab?: (url: string) => boolean): Promise<void> => {
const openTabs = await tabs.query({ title: 'Lace' });
const namiTabs = await tabs.query({ title: POPUP_WINDOW_NAMI_TITLE });
openTabs.push(...namiTabs);
Expand All @@ -135,7 +135,7 @@ export const closeAllLaceWindows = async (shouldRemoveTab?: (url: string) => boo
};

export const ensureUiIsOpenAndLoaded = async (url?: string): Promise<Tabs.Tab> => {
await closeAllLaceWindows((tabUrl) => DAPP_CONNECTOR_REGEX.test(tabUrl));
await closeAllLaceOrNamiTabs((tabUrl) => DAPP_CONNECTOR_REGEX.test(tabUrl));

const tab = await launchCip30Popup(url);

Expand Down

0 comments on commit b29e275

Please sign in to comment.