From 47d3fb0a29cecbcb263c46dd6a96de3c4bb989ab Mon Sep 17 00:00:00 2001 From: David Kaya Date: Thu, 16 Apr 2026 14:00:30 +0200 Subject: [PATCH] fix: tray icon click opens main window reliably after quick prompt use MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit showAndFocusWindow() used BrowserWindow.getAllWindows()[0] to find the main window, which breaks once the quick prompt window is created — the array order is not guaranteed and windows[0] may resolve to the quick prompt instead, causing the show/focus calls to target a hidden, frameless window. Accept the main window reference as an explicit parameter and guard against destroyed windows with isDestroyed(). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/main/index.ts | 8 ++++---- src/main/services/systemTray.ts | 8 +++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index c71d1ac..86f5543 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -63,9 +63,9 @@ async function bootstrap(): Promise { applyTitleBarTheme(mainWindow, workspace.settings.theme); systemTray = new SystemTray({ - onShowWindow: showAndFocusWindow, + onShowWindow: () => showAndFocusWindow(mainWindow!), onCreateScratchpad: () => { - showAndFocusWindow(); + showAndFocusWindow(mainWindow!); mainWindow?.webContents.send('tray:create-scratchpad'); }, onQuit: () => app.quit(), @@ -127,8 +127,8 @@ app.on('window-all-closed', () => { app.on('activate', async () => { if (BrowserWindow.getAllWindows().length === 0) { await bootstrap(); - } else { - showAndFocusWindow(); + } else if (mainWindow && !mainWindow.isDestroyed()) { + showAndFocusWindow(mainWindow); } }); diff --git a/src/main/services/systemTray.ts b/src/main/services/systemTray.ts index e0db7c5..3695ba5 100644 --- a/src/main/services/systemTray.ts +++ b/src/main/services/systemTray.ts @@ -3,7 +3,7 @@ import { join } from 'node:path'; import type { WorkspaceState } from '@shared/domain/workspace'; -const { app, Menu, Tray, nativeImage, BrowserWindow } = electron; +const { app, Menu, Tray, nativeImage } = electron; type TrayType = InstanceType; type NativeImageType = ReturnType; @@ -123,10 +123,8 @@ export function setupCloseToTray( /** * Show and focus the main window, restoring from tray if hidden. */ -export function showAndFocusWindow(): void { - const windows = BrowserWindow.getAllWindows(); - const mainWindow = windows[0]; - if (!mainWindow) return; +export function showAndFocusWindow(mainWindow: Electron.BrowserWindow): void { + if (mainWindow.isDestroyed()) return; // On macOS, show the dock icon again if (process.platform === 'darwin') {