Skip to content

Commit

Permalink
fix popout size in windows (#3249)
Browse files Browse the repository at this point in the history
  • Loading branch information
wentokay authored Mar 9, 2023
1 parent 5debbe2 commit e0c66c0
Showing 1 changed file with 21 additions and 83 deletions.
104 changes: 21 additions & 83 deletions packages/common/src/browser/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,56 +54,9 @@ export class BrowserRuntimeExtension {
// that those promises can properly resolve with the right state,
// i.e. user denied the request.
//
UiActionRequestManager.cancelAllRequests();

return new Promise(async (resolve, reject) => {
// try to reuse existing popup window
try {
const popupWindowId: number | undefined =
await BrowserRuntimeCommon.getLocalStorage("popupWindowId");
if (popupWindowId) {
const popupWindow = await chrome?.windows.get(popupWindowId);
if (popupWindow) {
const tabs = await chrome.tabs.query({ windowId: popupWindowId });
if (tabs.length === 1) {
const tab = tabs[0];
const url: string = Array.isArray(options.url)
? options.url[0]!
: options.url!;
const updatedTab = await chrome.tabs.update(tab.id!, { url });
if (updatedTab) {
const popupWindow = await chrome?.windows.update(
updatedTab.windowId,
{ focused: true }
);
return resolve(popupWindow);
} else {
const error = BrowserRuntimeCommon.checkForError();
return reject(error);
}
}
}
}
} catch (e) {
// fall through to create new window
}
// if nothign to reuse create new window.
try {
const newPopupWindow = await chrome?.windows.create(options);
if (newPopupWindow) {
BrowserRuntimeCommon.setLocalStorage(
"popupWindowId",
newPopupWindow.id
);
resolve(newPopupWindow);
}
const error = BrowserRuntimeCommon.checkForError();
return reject(error);
} catch (e) {
const error = BrowserRuntimeCommon.checkForError();
return reject(error);
}
});
await UiActionRequestManager.cancelAllRequests();
const newPopupWindow = await chrome?.windows.create(options);
return newPopupWindow;
}

public static async getLastFocusedWindow(): Promise<chrome.windows.Window>;
Expand Down Expand Up @@ -216,40 +169,25 @@ export async function openApproveMessagePopupWindow(
export async function openPopupWindow(
url: string
): Promise<chrome.windows.Window> {
const MACOS_TOOLBAR_HEIGHT = 28;
const WINDOWS_TOOLBAR_HEIGHT = 28; // TODO: confirm this.
function getOs() {
const os = ["Windows", "Linux", "Mac"];
return os.find((v) => navigator.appVersion.indexOf(v) >= 0);
}
function isMacOs(): boolean {
return getOs() === "Mac";
}
function isWindows(): boolean {
return getOs() === "Windows";
}

return new Promise((resolve) => {
BrowserRuntimeExtension.getLastFocusedWindow().then((window: any) => {
BrowserRuntimeExtension._openWindow({
url: `${url}`,
type: "popup",
width: EXTENSION_WIDTH,
height:
EXTENSION_HEIGHT +
(isMacOs()
? MACOS_TOOLBAR_HEIGHT
: isWindows()
? WINDOWS_TOOLBAR_HEIGHT
: 0),
top: window.top,
left: window.left + (window.width - EXTENSION_WIDTH),
focused: true,
}).then((window: any) => {
resolve(window);
});
});
const _window = await BrowserRuntimeExtension.getLastFocusedWindow();

const [EXTRA_HEIGHT, EXTRA_WIDTH] =
(navigator as any).userAgentData.platform === "Windows"
? [36, 12]
: [28, 0];

await BrowserRuntimeExtension._openWindow({
url: `${url}`,
type: "popup",
width: EXTENSION_WIDTH + EXTRA_WIDTH,
height: EXTENSION_HEIGHT + EXTRA_HEIGHT,
top: _window.top,
left:
(_window.left ?? 0) +
((_window.width ?? 0) - EXTENSION_WIDTH - EXTRA_WIDTH),
focused: true,
});
return _window;
}

export function openOnboarding() {
Expand Down

1 comment on commit e0c66c0

@vercel
Copy link

@vercel vercel bot commented on e0c66c0 Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.