From ddd734fd30ad390b51464c00727fa858352aeb15 Mon Sep 17 00:00:00 2001 From: Yun Chan Date: Wed, 21 Oct 2026 11:37:36 +0900 Subject: [PATCH] fix(window): don't steal focus when the app starts at login The login item now passes --autostart (also used when querying it, which Windows needs to match the registration). On such a launch the main window is shown inactive instead of focused, flashed and pinned on top for a second, so typing that already started in another app is not interrupted. Existing registrations migrate on the next startup sync. --- .../src/main/services/AutoLaunchService.ts | 18 ++++++++-- .../desktop/src/main/windows/WindowManager.ts | 34 ++++++++++++------- .../main-window-close-redteam-r1-0.test.ts | 30 ++++++++++++++++ 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/apps/desktop/src/main/services/AutoLaunchService.ts b/apps/desktop/src/main/services/AutoLaunchService.ts index aeae9e0..8fe8fea 100644 --- a/apps/desktop/src/main/services/AutoLaunchService.ts +++ b/apps/desktop/src/main/services/AutoLaunchService.ts @@ -7,13 +7,26 @@ import { configGet, configSet } from './ConfigService' const logger = getLogger('AutoLaunchService') +/** 로그인 자동 실행으로 켜졌다는 표시 — 이때는 창을 띄우되 사용자가 쓰던 창의 포커스를 뺏지 않는다. */ +export const AUTOSTART_ARG = '--autostart' + +export function launchedAtLogin(argv: readonly string[] = process.argv): boolean { + return argv.includes(AUTOSTART_ARG) +} + +/** Windows 는 같은 args 로 조회해야 등록 여부를 맞게 돌려준다 — 등록·조회가 같은 값을 쓴다. */ +function loginItemArgs(): string[] { + // 개발 모드에서는 electron.exe 경로가 등록되므로 앱 경로를 함께 넘긴다 + return app.isPackaged ? [AUTOSTART_ARG] : [app.getAppPath(), AUTOSTART_ARG] +} + class AutoLaunchService { /** * 현재 자동 실행 설정 상태를 조회한다. */ isEnabled(): boolean { // Electron API로 실제 OS 설정 확인 - const settings = app.getLoginItemSettings() + const settings = app.getLoginItemSettings({ args: loginItemArgs() }) return settings.openAtLogin } @@ -25,8 +38,7 @@ class AutoLaunchService { app.setLoginItemSettings({ openAtLogin: enabled, // Windows: 시작 프로그램에 등록 - // 개발 모드에서는 electron.exe 경로가 등록되므로 주의 - args: app.isPackaged ? [] : [app.getAppPath()] + args: loginItemArgs() }) configSet('autoLaunch', enabled) diff --git a/apps/desktop/src/main/windows/WindowManager.ts b/apps/desktop/src/main/windows/WindowManager.ts index e6af338..232234a 100644 --- a/apps/desktop/src/main/windows/WindowManager.ts +++ b/apps/desktop/src/main/windows/WindowManager.ts @@ -38,6 +38,7 @@ import { } from './popup-window-factory' import { createElectronScreenGeometry } from './screen-geometry' import { installApplicationMenu } from './app-menu' +import { launchedAtLogin } from '../services/AutoLaunchService' import { SUGGESTION_OVERLAY_MAX_HEIGHT, SUGGESTION_OVERLAY_WIDTH, @@ -247,6 +248,7 @@ export function createMainWindow(): BrowserWindow { const initWidth = Math.min(WINDOW_SIZE.MAIN.width, screenWidth) const initHeight = Math.min(WINDOW_SIZE.MAIN.height, screenHeight) + const quietLaunch = launchedAtLogin() mainWindow = new BrowserWindow({ title: 'D3RO Voice', width: initWidth, @@ -255,7 +257,8 @@ export function createMainWindow(): BrowserWindow { y: Math.max(0, Math.round((screenHeight - initHeight) / 2)), minWidth: 800, minHeight: 600, - show: true, + // 로그인 자동 실행이면 아래에서 포커스 없이 띄운다 — 막 입력을 시작한 사용자의 창을 뺏지 않게. + show: !quietLaunch, autoHideMenuBar: true, // 보더리스: 렌더러의 커스텀 TitleBar(AppLayout 최상단)가 크롬을 대체한다. // macOS는 hiddenInset으로 네이티브 교통신호(traffic lights)를 콘텐츠 위에 @@ -282,6 +285,11 @@ export function createMainWindow(): BrowserWindow { installApplicationMenu(Menu, process.platform) mainWindow.on('ready-to-show', () => { + if (quietLaunch) { + mainWindow?.showInactive() + logger.info('Main window shown without focus (launched at login)') + return + } mainWindow?.show() mainWindow?.restore() mainWindow?.focus() @@ -325,17 +333,19 @@ export function createMainWindow(): BrowserWindow { } mainWindow.center() - mainWindow.restore() - mainWindow.show() - mainWindow.focus() - mainWindow.setAlwaysOnTop(true) - setTimeout(() => { - try { - mainWindow?.setAlwaysOnTop(false) - } catch { - // ignore - } - }, 1000) + if (!quietLaunch) { + mainWindow.restore() + mainWindow.show() + mainWindow.focus() + mainWindow.setAlwaysOnTop(true) + setTimeout(() => { + try { + mainWindow?.setAlwaysOnTop(false) + } catch { + // ignore + } + }, 1000) + } logger.info('Main window created') return mainWindow diff --git a/apps/desktop/tests/main/windows/main-window-close-redteam-r1-0.test.ts b/apps/desktop/tests/main/windows/main-window-close-redteam-r1-0.test.ts index a4ec426..42b1988 100644 --- a/apps/desktop/tests/main/windows/main-window-close-redteam-r1-0.test.ts +++ b/apps/desktop/tests/main/windows/main-window-close-redteam-r1-0.test.ts @@ -41,6 +41,7 @@ vi.mock('electron', () => { center = vi.fn() restore = vi.fn() show = vi.fn() + showInactive = vi.fn() focus = vi.fn() hide = vi.fn() setAlwaysOnTop = vi.fn() @@ -96,6 +97,35 @@ beforeEach(async () => { lifecycle.setIsQuitting(false) }) +describe('main window launch', () => { + it('로그인 자동 실행(--autostart)이면 포커스를 뺏지 않고 띄운다', async () => { + vi.resetModules() + const argv = process.argv + process.argv = [...argv, '--autostart'] + try { + const { createMainWindow } = await import('../../../src/main/windows/WindowManager') + createMainWindow() + const win = state.windows[0] as unknown as Record> & { + listeners: Map + } + win.listeners.get('ready-to-show')?.() + expect(win.showInactive).toHaveBeenCalled() + expect(win.focus).not.toHaveBeenCalled() + expect(win.setAlwaysOnTop).not.toHaveBeenCalled() + } finally { + process.argv = argv + } + }) + + it('직접 실행하면 앞으로 띄우고 포커스를 준다', async () => { + vi.resetModules() + const { createMainWindow } = await import('../../../src/main/windows/WindowManager') + createMainWindow() + const win = state.windows[0] as unknown as Record> + expect(win.focus).toHaveBeenCalled() + }) +}) + describe('main window close', () => { it('closeToTray=false 이면 창 닫기가 앱 종료로 이어진다', async () => { config.closeToTray = false