feat(keybinding): several shortcuts per action, mouse buttons, searchable picker

Shortcuts were defined in four places that drifted apart: per-action IPC channel
pairs, a hand-written VK table in the service, a second one in the renderer, and
three copies of the keycap styling. Adding an action meant editing all of them,
so two shortcuts stayed hardcoded in bootstrap and one had no settings entry at
all.

packages/core/src/keybinding.ts is now the single source for the binding type,
the selectable key catalog, the action catalog, normalization, validation,
conflict detection, display labels, search and deserialization. Main, preload
and renderer all read from it; nothing redefines keys or rules locally.

- Each action holds a list of bindings instead of one. AppConfig's four
  *Shortcut fields collapse into a single keyBindings map, migrated on launch.
- Mouse buttons can be bound. Left click is refused, right/middle need a
  modifier, side buttons are free. uiohook cannot swallow events, so the
  original click still fires and the UI says so.
- Keys can be picked from a grouped dropdown with a search box, not only by
  recording a keypress.
- HOTKEY's 14 channels become KEYBINDING's 9, taking the action as a parameter,
  so actions no longer multiply channels. The history and command popups moved
  out of bootstrap into ordinary actions.
- displayLabel is gone; labels derive from the binding and follow the app
  language and platform.

Fixes found on the way:
- Double-press hands-free was unreachable: lookup returned only the first
  matching action, and dictation shares its default binding.
- Reserved-combination checks compared joined key names, so a different modifier
  order let Ctrl+C through.
- Disabling shortcuts released every global registration in the process,
  including the popup ones, and never restored them.
- Enabling shortcuts after starting disabled left nothing registered.
- The dashboard stored the caption event payload instead of the state in it.
This commit is contained in:
Yun Chan 2026-09-21 13:41:47 +09:00
parent 0ca9e242fa
commit 4ad1ae6ed4
49 changed files with 5901 additions and 1792 deletions

View file

@ -200,7 +200,7 @@ test.describe.serial('Extreme Red Team - Cycle 3: Modals, Deep Configuration & S
expect(uncaughtExceptions).toEqual([]);
});
test('RT-11: HotkeyRecordModal - Interactive Hotkey Recording & Conflict Protection', async () => {
test('RT-11: KeyBindingPicker - Key Recording, Reserved-Combo Protection & Cancel Safety', async () => {
// 1. Re-open SettingsModal to General Tab
const settingsTrigger = window.getByText('설정', { exact: true });
await settingsTrigger.click();
@ -211,34 +211,114 @@ test.describe.serial('Extreme Red Team - Cycle 3: Modals, Deep Configuration & S
await generalTab.click();
await window.waitForTimeout(400);
// 2. Click pencil icon on Dictation shortcut to open HotkeyRecordModal
const editHotkeyBtn = window.locator('button:has(svg.lucide-pencil)').first();
await expect(editHotkeyBtn).toBeVisible();
await editHotkeyBtn.click();
await window.waitForTimeout(500);
// 2. The dictation row is rendered by KeyBindingField off KEYBINDING_ACTIONS
const dictationField = window.getByTestId('keybinding-field-dictation');
await expect(dictationField).toBeVisible({ timeout: 5000 });
// Verify HotkeyRecordModal is open
await expect(window.getByText(/단축키 설정|단축키 녹화|키 조합/i).first()).toBeVisible({ timeout: 5000 });
// Wait for the binding map to arrive, then snapshot how many bindings this action
// has so we can prove afterwards that cancelling did not corrupt them.
const bindingChips = dictationField.getByTestId('keybinding-binding');
await expect(bindingChips.first()).toBeVisible({ timeout: 5000 });
const bindingsBefore = await bindingChips.count();
// 3. Test pressing a key (F9)
// 3. Open the picker with the "+ Add" button (replaces the old pencil icon)
await dictationField.getByTestId('keybinding-add-dictation').click();
const picker = window.getByTestId('keybinding-picker');
await expect(picker).toBeVisible({ timeout: 5000 });
// 4. Record tab: pressing F9 captures it onto a keycap
const recordArea = picker.getByTestId('keybinding-record-area');
await expect(recordArea).toBeVisible();
await window.keyboard.press('F9');
await window.waitForTimeout(400);
await expect(recordArea.getByText('F9', { exact: true })).toBeVisible();
// Verify chip shows F9
await expect(window.getByText('F9', { exact: true })).toBeVisible();
// Take screenshot while modal is open with captured key
// Take screenshot while the picker holds the captured key
await window.screenshot({
path: path.join(SCREENSHOT_DIR, 'rt11_hotkey_modal_verified.png'),
path: path.join(SCREENSHOT_DIR, 'rt11_keybinding_picker_recorded.png'),
});
// 4. Click Cancel button to close HotkeyRecordModal without corrupting bindings
const cancelBtn = window.getByRole('button', { name: /취소|Cancel/i });
await expect(cancelBtn).toBeVisible();
await cancelBtn.click();
await window.waitForTimeout(500);
// 5. Reserved-combo protection: Ctrl+C must be rejected and Save must stay disabled
await picker.getByTestId('keybinding-record-again').click();
await window.waitForTimeout(300);
await window.keyboard.press('Control+c');
await window.waitForTimeout(400);
await expect(picker.getByTestId('keybinding-rejection').first()).toBeVisible();
await expect(picker.getByTestId('keybinding-confirm')).toBeDisabled();
// 6. Cancel closes the picker without touching the stored bindings
await picker.getByTestId('keybinding-cancel').click();
await window.waitForTimeout(500);
await expect(picker).toBeHidden();
await expect(bindingChips).toHaveCount(bindingsBefore);
// Close SettingsModal (its close button sits in DialogTitle, ahead of any field markup)
const closeSettingsBtn = window.locator('div[role="dialog"] button:has(svg.lucide-x)').first();
if (await closeSettingsBtn.isVisible()) {
await closeSettingsBtn.click();
await window.waitForTimeout(500);
}
expect(uncaughtExceptions).toEqual([]);
});
test('RT-11b: KeyBindingPicker - Searchable Key Dropdown & Multi-Binding Add/Remove', async () => {
// 1. Re-open SettingsModal to General Tab
const settingsTrigger = window.getByText('설정', { exact: true });
await settingsTrigger.click();
await window.waitForTimeout(600);
const generalTab = window.getByRole('tab', { name: /일반|General/i });
await generalTab.click();
await window.waitForTimeout(400);
const dictationField = window.getByTestId('keybinding-field-dictation');
const bindingChips = dictationField.getByTestId('keybinding-binding');
await expect(bindingChips.first()).toBeVisible({ timeout: 5000 });
const bindingsBefore = await bindingChips.count();
// 2. Open the picker and switch to the list tab
await dictationField.getByTestId('keybinding-add-dictation').click();
const picker = window.getByTestId('keybinding-picker');
await expect(picker).toBeVisible({ timeout: 5000 });
await picker
.getByTestId('keybinding-picker-tabs')
.getByText(/목록에서 선택|Choose from List/i)
.click();
await window.waitForTimeout(300);
// 3. The dropdown carries its own search field and narrows the key catalog as you type.
// Its listbox is portalled to <body>, so options are queried from the page root.
const searchInput = picker.getByTestId('keybinding-search').locator('input');
await expect(searchInput).toBeVisible();
const keyOptions = window.getByTestId('keybinding-key-option');
expect(await keyOptions.count()).toBeGreaterThan(1);
await searchInput.fill('f8');
await window.waitForTimeout(400);
await expect(keyOptions).toHaveCount(1);
await keyOptions.first().click();
await window.waitForTimeout(300);
// Picked key is reflected back into the picker's selection preview
await expect(picker.getByText('F8', { exact: true }).first()).toBeVisible();
// 4. Saving adds a SECOND binding to the same action (multi-binding)
const confirmBtn = picker.getByTestId('keybinding-confirm');
await expect(confirmBtn).toBeEnabled({ timeout: 5000 });
await confirmBtn.click();
await expect(picker).toBeHidden();
await expect(bindingChips).toHaveCount(bindingsBefore + 1);
await window.screenshot({
path: path.join(SCREENSHOT_DIR, 'rt11b_keybinding_multi_binding.png'),
});
// 5. Remove it again so the stored config is left exactly as we found it
await bindingChips.last().getByTestId('keybinding-remove').click();
await expect(bindingChips).toHaveCount(bindingsBefore);
// Close SettingsModal
const closeSettingsBtn = window.locator('div[role="dialog"] button:has(svg.lucide-x)').first();
if (await closeSettingsBtn.isVisible()) {
await closeSettingsBtn.click();

View file

@ -51,13 +51,13 @@ vi.mock('../../../src/main/services/AudioCaptureService', () => ({
getAudioCaptureService: () => mockAudio
}))
const mockHotkey = {
const mockKeyBinding = {
on: vi.fn(),
off: vi.fn()
}
vi.mock('../../../src/main/services/HotkeyService', () => ({
getHotkeyService: () => mockHotkey
vi.mock('../../../src/main/services/KeyBindingService', () => ({
getKeyBindingService: () => mockKeyBinding
}))
vi.mock('../../../src/main/services/ConfigService', () => ({

View file

@ -1,8 +1,18 @@
import { describe, it, expect, vi } from 'vitest'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ErrorCode } from '@d3ro/core/errors'
import {
KEYBINDING_ACTIONS,
createDefaultBindingMap,
findActionSpec,
} from '@d3ro/core/keybinding'
import type {
KeyBinding,
KeyBindingMap,
KeyBindingValidationResult,
} from '@d3ro/core/types'
import { configGet } from '../../src/main/services/ConfigService'
import { registerHotkeyHandlers } from '../../src/main/ipc/hotkey-handlers'
import { registerKeyBindingHandlers } from '../../src/main/ipc/keybinding-handlers'
import { registerCaptionHandlers } from '../../src/main/ipc/caption-handlers'
import { registerRAGHandlers } from '../../src/main/ipc/rag-handlers'
import { registerVoiceConversationHandlers } from '../../src/main/ipc/voice-conversation-handlers'
@ -13,73 +23,171 @@ import { registerTemplateHandlers } from '../../src/main/ipc/template-handlers'
import { registerMeetingDocTemplateHandlers } from '../../src/main/ipc/meeting-doc-template-handlers'
import { invokeIpc, useRedHarness } from './harness'
vi.mock('../../src/main/services/HotkeyService', () => ({
getHotkeyService: () => ({
const { keyBindingService } = vi.hoisted(() => ({
keyBindingService: {
loadFromConfig: vi.fn(),
start: vi.fn(),
stop: vi.fn(),
on: vi.fn(),
off: vi.fn(),
}),
},
}))
vi.mock('../../src/main/services/KeyBindingService', () => ({
getKeyBindingService: () => keyBindingService,
}))
useRedHarness()
const SAMPLE_BINDING = {
keyCode: 65,
/** Ctrl + A — 시스템 예약 조합이라 검증에서 거부되어야 한다 */
const RESERVED_BINDING: KeyBinding = {
device: 'keyboard',
code: 0x41,
ctrl: true,
alt: false,
shift: false,
meta: false,
displayLabel: 'Ctrl+A',
}
describe('유스케이스: 핫키 / 캡션 / RAG / 대화 / 액션 / 파일전사 / 회의 IPC', () => {
it('핫키 받아쓰기 단축키를 읽고 쓴다', async () => {
registerHotkeyHandlers()
const set = await invokeIpc(IPC_CHANNELS.HOTKEY.SET_DICTATION_SHORTCUT, { binding: SAMPLE_BINDING })
/** Ctrl + Shift + F9 — 예약되지 않은 유효한 조합 */
const FREE_BINDING: KeyBinding = {
device: 'keyboard',
code: 0x78,
ctrl: true,
alt: false,
shift: true,
meta: false,
}
describe('유스케이스: 키바인딩 / 캡션 / RAG / 대화 / 액션 / 파일전사 / 회의 IPC', () => {
it('키바인딩 맵을 읽으면 모든 액션이 들어 있다', async () => {
registerKeyBindingHandlers()
const get = await invokeIpc<KeyBindingMap>(IPC_CHANNELS.KEYBINDING.GET_MAP)
expect(get.success).toBe(true)
if (get.success) {
for (const action of KEYBINDING_ACTIONS) {
expect(get.data[action.id]).toEqual(action.defaultBindings)
}
}
})
it('액션 바인딩을 교체하면 맵에 반영되고 서비스가 다시 읽는다', async () => {
registerKeyBindingHandlers()
const set = await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_BINDINGS, {
actionId: 'dictation',
bindings: [FREE_BINDING],
})
expect(set.success).toBe(true)
const get = await invokeIpc(IPC_CHANNELS.HOTKEY.GET_DICTATION_SHORTCUT)
expect(get.success).toBe(true)
if (get.success) expect(get.data.displayLabel).toBe('Ctrl+A')
expect(keyBindingService.loadFromConfig).toHaveBeenCalled()
const get = await invokeIpc<KeyBindingMap>(IPC_CHANNELS.KEYBINDING.GET_MAP)
if (get.success) expect(get.data.dictation).toEqual([FREE_BINDING])
})
it('핫키 핸즈프리 단축키를 읽고 쓴다', async () => {
registerHotkeyHandlers()
await invokeIpc(IPC_CHANNELS.HOTKEY.SET_HANDS_FREE_SHORTCUT, { binding: SAMPLE_BINDING })
const get = await invokeIpc(IPC_CHANNELS.HOTKEY.GET_HANDS_FREE_SHORTCUT)
expect(get.success).toBe(true)
it('시스템 예약 조합은 HotkeySystemReserved로 거부된다', async () => {
registerKeyBindingHandlers()
const set = await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_BINDINGS, {
actionId: 'dictation',
bindings: [RESERVED_BINDING],
})
expect(set.success).toBe(false)
if (!set.success) expect(set.error.code).toBe(ErrorCode.HotkeySystemReserved)
})
it('핫키 명령 단축키를 읽고 쓴다', async () => {
registerHotkeyHandlers()
await invokeIpc(IPC_CHANNELS.HOTKEY.SET_COMMAND_SHORTCUT, { binding: SAMPLE_BINDING })
const get = await invokeIpc(IPC_CHANNELS.HOTKEY.GET_COMMAND_SHORTCUT)
expect(get.success).toBe(true)
it('다른 액션이 쓰는 바인딩은 HotkeyConflict로 거부된다', async () => {
registerKeyBindingHandlers()
const ok = await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_BINDINGS, {
actionId: 'caption',
bindings: [FREE_BINDING],
})
expect(ok.success).toBe(true)
const conflict = await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_BINDINGS, {
actionId: 'command',
bindings: [FREE_BINDING],
})
expect(conflict.success).toBe(false)
if (!conflict.success) expect(conflict.error.code).toBe(ErrorCode.HotkeyConflict)
})
it('핫키 자막 단축키를 읽고 쓴다', async () => {
registerHotkeyHandlers()
await invokeIpc(IPC_CHANNELS.HOTKEY.SET_CAPTION_SHORTCUT, { binding: SAMPLE_BINDING })
const get = await invokeIpc(IPC_CHANNELS.HOTKEY.GET_CAPTION_SHORTCUT)
expect(get.success).toBe(true)
it('바인딩 사전 검사는 유효성과 충돌을 함께 돌려준다', async () => {
registerKeyBindingHandlers()
const reserved = await invokeIpc<KeyBindingValidationResult>(
IPC_CHANNELS.KEYBINDING.VALIDATE,
{ actionId: 'dictation', binding: RESERVED_BINDING },
)
expect(reserved.success).toBe(true)
if (reserved.success) {
expect(reserved.data.validation.valid).toBe(false)
expect(reserved.data.validation.reason).toBe('system-reserved')
}
const free = await invokeIpc<KeyBindingValidationResult>(
IPC_CHANNELS.KEYBINDING.VALIDATE,
{ actionId: 'dictation', binding: FREE_BINDING },
)
if (free.success) {
expect(free.data.validation.valid).toBe(true)
expect(free.data.conflicts).toEqual([])
}
})
it('핫키 활성 토글을 끈다', async () => {
registerHotkeyHandlers()
const set = await invokeIpc(IPC_CHANNELS.HOTKEY.SET_ENABLED, { enabled: false })
it('액션 하나를 기본값으로 되돌린다', async () => {
registerKeyBindingHandlers()
await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_BINDINGS, {
actionId: 'dictation',
bindings: [FREE_BINDING],
})
const reset = await invokeIpc(IPC_CHANNELS.KEYBINDING.RESET_ACTION, { actionId: 'dictation' })
expect(reset.success).toBe(true)
const get = await invokeIpc<KeyBindingMap>(IPC_CHANNELS.KEYBINDING.GET_MAP)
if (get.success) {
expect(get.data.dictation).toEqual(findActionSpec('dictation')?.defaultBindings)
}
})
it('전체를 기본값으로 되돌린다', async () => {
registerKeyBindingHandlers()
await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_BINDINGS, {
actionId: 'caption',
bindings: [FREE_BINDING],
})
const reset = await invokeIpc(IPC_CHANNELS.KEYBINDING.RESET_ALL)
expect(reset.success).toBe(true)
const get = await invokeIpc<KeyBindingMap>(IPC_CHANNELS.KEYBINDING.GET_MAP)
if (get.success) expect(get.data).toEqual(createDefaultBindingMap())
})
it('알 수 없는 액션 id는 거부된다', async () => {
registerKeyBindingHandlers()
const res = await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_BINDINGS, {
actionId: 'nope',
bindings: [FREE_BINDING],
})
expect(res.success).toBe(false)
})
it('키바인딩 활성 토글을 끈다', async () => {
registerKeyBindingHandlers()
const set = await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_ENABLED, { enabled: false })
expect(set.success).toBe(true)
const get = await invokeIpc(IPC_CHANNELS.HOTKEY.IS_ENABLED)
const get = await invokeIpc(IPC_CHANNELS.KEYBINDING.IS_ENABLED)
expect(get.success).toBe(true)
if (get.success) expect(get.data).toBe(false)
expect(configGet('hotkeyEnabled')).toBe(false)
expect(keyBindingService.stop).toHaveBeenCalled()
})
it('핫키 활성 토글을 켠다', async () => {
registerHotkeyHandlers()
await invokeIpc(IPC_CHANNELS.HOTKEY.SET_ENABLED, { enabled: true })
const get = await invokeIpc(IPC_CHANNELS.HOTKEY.IS_ENABLED)
it('키바인딩을 다시 켜면 등록을 다시 읽고 후킹을 시작한다', async () => {
registerKeyBindingHandlers()
keyBindingService.loadFromConfig.mockClear()
await invokeIpc(IPC_CHANNELS.KEYBINDING.SET_ENABLED, { enabled: true })
const get = await invokeIpc(IPC_CHANNELS.KEYBINDING.IS_ENABLED)
if (get.success) expect(get.data).toBe(true)
expect(keyBindingService.loadFromConfig).toHaveBeenCalled()
expect(keyBindingService.start).toHaveBeenCalled()
})
it('캡션 초기 상태를 읽는다', async () => {

View file

@ -104,8 +104,8 @@ vi.mock('../../src/main/services/CaptionService', () => ({
resetCaptionServiceForTests: () => undefined,
}))
vi.mock('../../src/main/services/HotkeyService', () => ({
getHotkeyService: () => ({
vi.mock('../../src/main/services/KeyBindingService', () => ({
getKeyBindingService: () => ({
on: vi.fn(),
off: vi.fn(),
}),