From 4cc562c228623778d8d221804753c9f2fc28e909 Mon Sep 17 00:00:00 2001 From: Jordan Aasen <166539328+jaasen-livefront@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:53:15 -0800 Subject: [PATCH] [PM-12971] - close safari default extension on pop out (#11391) * fix safari extension opening * Update apps/browser/src/platform/browser/browser-api.ts Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * remove whitespace * remove check for id --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> --- .../src/platform/browser/browser-api.ts | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/apps/browser/src/platform/browser/browser-api.ts b/apps/browser/src/platform/browser/browser-api.ts index 33f18ce572..072ef74004 100644 --- a/apps/browser/src/platform/browser/browser-api.ts +++ b/apps/browser/src/platform/browser/browser-api.ts @@ -58,11 +58,33 @@ export class BrowserApi { } static async createWindow(options: chrome.windows.CreateData): Promise { - return new Promise((resolve) => - chrome.windows.create(options, (window) => { - resolve(window); - }), - ); + return new Promise((resolve) => { + chrome.windows.create(options, async (newWindow) => { + if (!BrowserApi.isSafariApi) { + return resolve(newWindow); + } + // Safari doesn't close the default extension popup when a new window is created so we need to + // manually trigger the close by focusing the main window after the new window is created + const allWindows = await new Promise((resolve) => { + chrome.windows.getAll({ windowTypes: ["normal"] }, (windows) => resolve(windows)); + }); + + const mainWindow = allWindows.find((window) => window.id !== newWindow.id); + + // No main window found, resolve the new window + if (mainWindow == null || !mainWindow.id) { + return resolve(newWindow); + } + + // Focus the main window to close the extension popup + chrome.windows.update(mainWindow.id, { focused: true }, () => { + // Refocus the newly created window + chrome.windows.update(newWindow.id, { focused: true }, () => { + resolve(newWindow); + }); + }); + }); + }); } /**