From 7a7354147a8e3f3f88197040029d4cbe16539c6b Mon Sep 17 00:00:00 2001 From: Bradley Axen Date: Mon, 27 Jan 2025 10:16:02 -0800 Subject: [PATCH] fix: open new window for url links (#810) Co-authored-by: Alex Hancock --- ui/desktop/src/main.ts | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/ui/desktop/src/main.ts b/ui/desktop/src/main.ts index e7beab6ee7..5c8a909fd2 100644 --- a/ui/desktop/src/main.ts +++ b/ui/desktop/src/main.ts @@ -31,31 +31,20 @@ const { exec } = require('child_process'); // Handle creating/removing shortcuts on Windows when installing/uninstalling. if (started) app.quit(); -// Track the last focused window -let lastFocusedWindow: BrowserWindow | null = null; - -// Update last focused window when any window gains focus -app.on('browser-window-focus', (_, window) => { - lastFocusedWindow = window; -}); - // Triggered when the user opens "goose://..." links app.on('open-url', async (event, url) => { event.preventDefault(); console.log('open-url:', url); - // Get the last focused window, or the first window if none was focused - const targetWindow = lastFocusedWindow || BrowserWindow.getAllWindows()[0]; + const recentDirs = loadRecentDirs(); + const openDir = recentDirs.length > 0 ? recentDirs[0] : null; - if (targetWindow) { - // Ensure window is visible - if (!targetWindow.isFocused()) { - targetWindow.show(); - targetWindow.focus(); - } - console.log('sending add-extension to frontend:', url); - targetWindow.webContents.send('add-extension', url); - } + // Create the new Chat window + const newWindow = await createChat(app, undefined, openDir); + + newWindow.webContents.once('did-finish-load', () => { + newWindow.webContents.send('add-extension', url); + }); }); declare var MAIN_WINDOW_VITE_DEV_SERVER_URL: string; @@ -220,7 +209,6 @@ const createChat = async (app, query?: string, dir?: string, version?: string) = // Register shortcut when window is focused mainWindow.on('focus', () => { registerDevToolsShortcut(mainWindow); - lastFocusedWindow = mainWindow; }); // Unregister shortcut when window loses focus @@ -231,12 +219,10 @@ const createChat = async (app, query?: string, dir?: string, version?: string) = windowMap.set(windowId, mainWindow); mainWindow.on('closed', () => { windowMap.delete(windowId); - if (lastFocusedWindow === mainWindow) { - lastFocusedWindow = null; - } unregisterDevToolsShortcut(); goosedProcess.kill(); }); + return mainWindow; }; const createTray = () => {