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.
This commit is contained in:
parent
e7937c877f
commit
ddd734fd30
3 changed files with 67 additions and 15 deletions
|
|
@ -7,13 +7,26 @@ import { configGet, configSet } from './ConfigService'
|
||||||
|
|
||||||
const logger = getLogger('AutoLaunchService')
|
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 {
|
class AutoLaunchService {
|
||||||
/**
|
/**
|
||||||
* 현재 자동 실행 설정 상태를 조회한다.
|
* 현재 자동 실행 설정 상태를 조회한다.
|
||||||
*/
|
*/
|
||||||
isEnabled(): boolean {
|
isEnabled(): boolean {
|
||||||
// Electron API로 실제 OS 설정 확인
|
// Electron API로 실제 OS 설정 확인
|
||||||
const settings = app.getLoginItemSettings()
|
const settings = app.getLoginItemSettings({ args: loginItemArgs() })
|
||||||
return settings.openAtLogin
|
return settings.openAtLogin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,8 +38,7 @@ class AutoLaunchService {
|
||||||
app.setLoginItemSettings({
|
app.setLoginItemSettings({
|
||||||
openAtLogin: enabled,
|
openAtLogin: enabled,
|
||||||
// Windows: 시작 프로그램에 등록
|
// Windows: 시작 프로그램에 등록
|
||||||
// 개발 모드에서는 electron.exe 경로가 등록되므로 주의
|
args: loginItemArgs()
|
||||||
args: app.isPackaged ? [] : [app.getAppPath()]
|
|
||||||
})
|
})
|
||||||
|
|
||||||
configSet('autoLaunch', enabled)
|
configSet('autoLaunch', enabled)
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ import {
|
||||||
} from './popup-window-factory'
|
} from './popup-window-factory'
|
||||||
import { createElectronScreenGeometry } from './screen-geometry'
|
import { createElectronScreenGeometry } from './screen-geometry'
|
||||||
import { installApplicationMenu } from './app-menu'
|
import { installApplicationMenu } from './app-menu'
|
||||||
|
import { launchedAtLogin } from '../services/AutoLaunchService'
|
||||||
import {
|
import {
|
||||||
SUGGESTION_OVERLAY_MAX_HEIGHT,
|
SUGGESTION_OVERLAY_MAX_HEIGHT,
|
||||||
SUGGESTION_OVERLAY_WIDTH,
|
SUGGESTION_OVERLAY_WIDTH,
|
||||||
|
|
@ -247,6 +248,7 @@ export function createMainWindow(): BrowserWindow {
|
||||||
const initWidth = Math.min(WINDOW_SIZE.MAIN.width, screenWidth)
|
const initWidth = Math.min(WINDOW_SIZE.MAIN.width, screenWidth)
|
||||||
const initHeight = Math.min(WINDOW_SIZE.MAIN.height, screenHeight)
|
const initHeight = Math.min(WINDOW_SIZE.MAIN.height, screenHeight)
|
||||||
|
|
||||||
|
const quietLaunch = launchedAtLogin()
|
||||||
mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow({
|
||||||
title: 'D3RO Voice',
|
title: 'D3RO Voice',
|
||||||
width: initWidth,
|
width: initWidth,
|
||||||
|
|
@ -255,7 +257,8 @@ export function createMainWindow(): BrowserWindow {
|
||||||
y: Math.max(0, Math.round((screenHeight - initHeight) / 2)),
|
y: Math.max(0, Math.round((screenHeight - initHeight) / 2)),
|
||||||
minWidth: 800,
|
minWidth: 800,
|
||||||
minHeight: 600,
|
minHeight: 600,
|
||||||
show: true,
|
// 로그인 자동 실행이면 아래에서 포커스 없이 띄운다 — 막 입력을 시작한 사용자의 창을 뺏지 않게.
|
||||||
|
show: !quietLaunch,
|
||||||
autoHideMenuBar: true,
|
autoHideMenuBar: true,
|
||||||
// 보더리스: 렌더러의 커스텀 TitleBar(AppLayout 최상단)가 크롬을 대체한다.
|
// 보더리스: 렌더러의 커스텀 TitleBar(AppLayout 최상단)가 크롬을 대체한다.
|
||||||
// macOS는 hiddenInset으로 네이티브 교통신호(traffic lights)를 콘텐츠 위에
|
// macOS는 hiddenInset으로 네이티브 교통신호(traffic lights)를 콘텐츠 위에
|
||||||
|
|
@ -282,6 +285,11 @@ export function createMainWindow(): BrowserWindow {
|
||||||
installApplicationMenu(Menu, process.platform)
|
installApplicationMenu(Menu, process.platform)
|
||||||
|
|
||||||
mainWindow.on('ready-to-show', () => {
|
mainWindow.on('ready-to-show', () => {
|
||||||
|
if (quietLaunch) {
|
||||||
|
mainWindow?.showInactive()
|
||||||
|
logger.info('Main window shown without focus (launched at login)')
|
||||||
|
return
|
||||||
|
}
|
||||||
mainWindow?.show()
|
mainWindow?.show()
|
||||||
mainWindow?.restore()
|
mainWindow?.restore()
|
||||||
mainWindow?.focus()
|
mainWindow?.focus()
|
||||||
|
|
@ -325,17 +333,19 @@ export function createMainWindow(): BrowserWindow {
|
||||||
}
|
}
|
||||||
|
|
||||||
mainWindow.center()
|
mainWindow.center()
|
||||||
mainWindow.restore()
|
if (!quietLaunch) {
|
||||||
mainWindow.show()
|
mainWindow.restore()
|
||||||
mainWindow.focus()
|
mainWindow.show()
|
||||||
mainWindow.setAlwaysOnTop(true)
|
mainWindow.focus()
|
||||||
setTimeout(() => {
|
mainWindow.setAlwaysOnTop(true)
|
||||||
try {
|
setTimeout(() => {
|
||||||
mainWindow?.setAlwaysOnTop(false)
|
try {
|
||||||
} catch {
|
mainWindow?.setAlwaysOnTop(false)
|
||||||
// ignore
|
} catch {
|
||||||
}
|
// ignore
|
||||||
}, 1000)
|
}
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
logger.info('Main window created')
|
logger.info('Main window created')
|
||||||
return mainWindow
|
return mainWindow
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ vi.mock('electron', () => {
|
||||||
center = vi.fn()
|
center = vi.fn()
|
||||||
restore = vi.fn()
|
restore = vi.fn()
|
||||||
show = vi.fn()
|
show = vi.fn()
|
||||||
|
showInactive = vi.fn()
|
||||||
focus = vi.fn()
|
focus = vi.fn()
|
||||||
hide = vi.fn()
|
hide = vi.fn()
|
||||||
setAlwaysOnTop = vi.fn()
|
setAlwaysOnTop = vi.fn()
|
||||||
|
|
@ -96,6 +97,35 @@ beforeEach(async () => {
|
||||||
lifecycle.setIsQuitting(false)
|
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<string, ReturnType<typeof vi.fn>> & {
|
||||||
|
listeners: Map<string, Listener>
|
||||||
|
}
|
||||||
|
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<string, ReturnType<typeof vi.fn>>
|
||||||
|
expect(win.focus).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('main window close', () => {
|
describe('main window close', () => {
|
||||||
it('closeToTray=false 이면 창 닫기가 앱 종료로 이어진다', async () => {
|
it('closeToTray=false 이면 창 닫기가 앱 종료로 이어진다', async () => {
|
||||||
config.closeToTray = false
|
config.closeToTray = false
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue