fix(popups): close the history and command popups on an outside click or after 15s idle

The popups are non-focusable, so nothing closed them except their own toggle,
Esc or a selection, while they held Enter, arrows, Esc and 0-9 as global
shortcuts. Typing "3" or Enter in another app inserted a history entry or
switched the active command. A click outside the popup (converted from
physical pixels to DIP) or 15s without a navigation key now dismisses it and
releases the keys.
This commit is contained in:
Yun Chan 2026-10-21 11:32:17 +09:00
parent 67cc920e06
commit 06365651fc
3 changed files with 126 additions and 2 deletions

View file

@ -1,7 +1,7 @@
// src/main/bootstrap.ts — 초기화 시퀀스
import { app, dialog, globalShortcut, ipcMain as ipcMainRef, nativeTheme } from 'electron'
import { uIOhook, type UiohookKeyboardEvent } from 'uiohook-napi'
import { app, dialog, globalShortcut, ipcMain as ipcMainRef, nativeTheme, screen, type BrowserWindow } from 'electron'
import { uIOhook, type UiohookKeyboardEvent, type UiohookMouseEvent } from 'uiohook-napi'
import { VK } from '@d3ro/core/keybinding'
import { initLoggerService, getLogger } from './services/LoggerService'
import { initConfigService, configGet, configSet, onConfigChanged } from './services/ConfigService'
@ -43,6 +43,8 @@ import {
hideCommandPopup,
sendKeyToCommandPopup,
isCommandPopupVisible,
getHistoryPopupWindow,
getCommandPopupWindow,
reapplyThemeToAllPopups,
} from './windows/WindowManager'
import { createTray } from './windows/TrayManager'
@ -380,6 +382,15 @@ async function initPopupWindows(): Promise<void> {
const popups = createPopupNavigationController()
getShutdownRegistry().register({ name: 'popup-navigation', dispose: () => popups.dispose() })
// 팝업은 포커스가 없어 blur 가 없다 — 다른 곳을 클릭하면 닫아 전역 Enter·숫자키를 놓게 한다.
const releasePopupClickHook = acquireGlobalInputHook()
getShutdownRegistry().register({ name: 'popup-click-hook', dispose: () => releasePopupClickHook() })
uIOhook.on('mousedown', (event: UiohookMouseEvent) => {
if (!popups.owner) return
// uiohook 은 물리 픽셀, 창 bounds 는 DIP 다(배율 125%·150% 에서 어긋난다).
const physical = { x: event.x, y: event.y }
popups.handlePointerDown(process.platform === 'win32' ? screen.screenToDipPoint(physical) : physical)
})
// 팝업 토글도 재바인딩 가능한 액션이다 — 하드코딩 accelerator 대신 KeyBindingService를 구독한다.
getKeyBindingService().on('triggered', (payload) => {
@ -394,6 +405,11 @@ async function initPopupWindows(): Promise<void> {
setupPopupIPC(popups)
}
function windowContains(win: BrowserWindow, point: { x: number; y: number }): boolean {
const b = win.getBounds()
return point.x >= b.x && point.x < b.x + b.width && point.y >= b.y && point.y < b.y + b.height
}
/** 팝업 컨트롤러를 Electron 창·전역 단축키·서비스 어댑터로 조립한다. */
function createPopupNavigationController(): PopupNavigationController {
return new PopupNavigationController({
@ -406,6 +422,7 @@ function createPopupNavigationController(): PopupNavigationController {
hide: () => hideHistoryPopup(),
isVisible: () => isHistoryPopupVisible(),
sendKey: (key) => sendKeyToHistoryPopup(key),
containsPoint: (point) => windowContains(getHistoryPopupWindow(), point),
},
command: {
show: () => {
@ -416,6 +433,7 @@ function createPopupNavigationController(): PopupNavigationController {
hide: () => hideCommandPopup(),
isVisible: () => isCommandPopupVisible(),
sendKey: (key) => sendKeyToCommandPopup(key),
containsPoint: (point) => windowContains(getCommandPopupWindow(), point),
},
},
shortcuts: {

View file

@ -16,6 +16,8 @@ export interface PopupWindowPort {
hide(): void
isVisible(): boolean
sendKey(key: string): void
/** 화면 좌표(DIP) 점이 팝업 안인지. 없으면 바깥 클릭으로 닫지 않는다. */
containsPoint?(point: { x: number; y: number }): boolean
}
export interface ShortcutPort {
@ -33,6 +35,8 @@ export interface PopupNavigationDeps {
applyCommandSelection(selection: { id: string; name: string }): void
/** 지연 실행 — 기본 setTimeout */
schedule?(fn: () => void, ms: number): void
/** 취소 가능한 타이머 — 기본 setTimeout/clearTimeout */
startTimer?(fn: () => void, ms: number): () => void
warn?(message: string): void
}
@ -49,12 +53,19 @@ export const POPUP_NAV_KEYS: ReadonlyArray<{ accel: string; key: string }> = [
{ accel: '0', key: '0' },
]
/**
* 키 입력 없이 이만큼 지나면 팝업을 닫는다. 팝업은 포커스가 없어 blur 로 닫히지 않는데, 떠 있는 동안
* Enter·방향키·숫자를 전역으로 붙잡아 다른 앱에서 친 "3"·Enter 가 기록 삽입·명령 변경이 됐다.
*/
export const POPUP_IDLE_DISMISS_MS = 15_000
/** 히스토리 팝업이 사라진 뒤 삽입까지 기다리는 시간(포커스 복귀 대기) */
export const HISTORY_INSERT_DELAY_MS = 200
export class PopupNavigationController {
private _owner: PopupKind | null = null
private _keysRegistered = false
private _cancelIdle: (() => void) | null = null
constructor(private readonly _deps: PopupNavigationDeps) {}
@ -80,6 +91,16 @@ export class PopupNavigationController {
this._deps.windows[kind].show()
this._owner = kind
this._registerKeys()
this._restartIdle()
}
/** 다른 곳을 클릭했으면(포커스 없는 팝업이라 blur 가 없다) 떠 있는 팝업을 닫는다. */
handlePointerDown(point: { x: number; y: number }): void {
const owner = this._owner
if (!owner) return
const win = this._deps.windows[owner]
if (!win.containsPoint || win.containsPoint(point)) return
this.dismiss(owner)
}
/** 팝업을 닫는다. 키는 그 팝업이 소유자일 때만 해제한다(다른 팝업의 키를 뺏지 않게). */
@ -88,6 +109,7 @@ export class PopupNavigationController {
if (this._owner === kind) {
this._owner = null
this._unregisterKeys()
this._stopIdle()
}
}
@ -111,6 +133,26 @@ export class PopupNavigationController {
dispose(): void {
this._owner = null
this._unregisterKeys()
this._stopIdle()
}
private _restartIdle(): void {
this._stopIdle()
const owner = this._owner
if (!owner) return
const start = this._deps.startTimer ?? ((fn: () => void, ms: number) => {
const handle = setTimeout(fn, ms)
return () => clearTimeout(handle)
})
this._cancelIdle = start(() => {
this._cancelIdle = null
if (this._owner === owner) this.dismiss(owner)
}, POPUP_IDLE_DISMISS_MS)
}
private _stopIdle(): void {
this._cancelIdle?.()
this._cancelIdle = null
}
private _registerKeys(): void {
@ -146,6 +188,7 @@ export class PopupNavigationController {
const win = this._deps.windows[owner]
if (win.isVisible()) {
win.sendKey(key)
this._restartIdle()
return
}
// 팝업이 다른 경로로 사라졌다 — 전역 키(Enter/방향키/숫자)를 계속 붙잡지 않게 놓는다

View file

@ -6,6 +6,7 @@ import {
PopupNavigationController,
POPUP_NAV_KEYS,
HISTORY_INSERT_DELAY_MS,
POPUP_IDLE_DISMISS_MS,
type PopupKind,
type PopupWindowPort,
} from '../../../src/main/windows/popup-navigation'
@ -160,3 +161,65 @@ describe('PopupNavigationController', () => {
expect(warn).toHaveBeenCalled()
})
})
describe('PopupNavigationController — 저절로 닫기', () => {
function withTimers(): {
controller: PopupNavigationController
windows: Record<PopupKind, FakeWindow>
shortcuts: ReturnType<typeof fakeShortcuts>
timers: Array<{ fn: () => void; ms: number; active: boolean }>
} {
const windows = { history: fakeWindow(), command: fakeWindow() }
const shortcuts = fakeShortcuts()
const timers: Array<{ fn: () => void; ms: number; active: boolean }> = []
const controller = new PopupNavigationController({
windows,
shortcuts,
insertText: async () => undefined,
applyCommandSelection: () => undefined,
startTimer: (fn, ms) => {
const timer = { fn, ms, active: true }
timers.push(timer)
return () => {
timer.active = false
}
},
})
return { controller, windows, shortcuts, timers }
}
it('키 입력 없이 시간이 지나면 팝업을 닫고 전역 키를 놓는다', () => {
const { controller, windows, shortcuts, timers } = withTimers()
controller.show('history')
const idle = timers.filter((t) => t.active)
expect(idle).toHaveLength(1)
expect(idle[0]?.ms).toBe(POPUP_IDLE_DISMISS_MS)
idle[0]?.fn()
expect(windows.history.visible).toBe(false)
expect(shortcuts.registered.size).toBe(0)
})
it('키를 누르면 유휴 시간을 다시 잰다', () => {
const { controller, shortcuts, timers } = withTimers()
controller.show('history')
shortcuts.press('Down')
expect(timers.filter((t) => t.active)).toHaveLength(1)
expect(timers[0]?.active).toBe(false)
})
it('팝업 바깥을 클릭하면 닫고, 안쪽 클릭은 그대로 둔다', () => {
const { controller, windows, shortcuts } = withTimers()
windows.history.containsPoint = (p) => p.x >= 100 && p.x <= 300 && p.y >= 100 && p.y <= 300
controller.show('history')
controller.handlePointerDown({ x: 150, y: 150 })
expect(windows.history.visible).toBe(true)
controller.handlePointerDown({ x: 900, y: 900 })
expect(windows.history.visible).toBe(false)
expect(shortcuts.registered.size).toBe(0)
})
})