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:
parent
0ca9e242fa
commit
4ad1ae6ed4
49 changed files with 5901 additions and 1792 deletions
|
|
@ -1,13 +1,12 @@
|
|||
// apps/desktop/src/renderer/utils/format-hotkey.ts
|
||||
// HotkeyBinding → 플랫폼별 표시 라벨 (segments + 결합 문자열).
|
||||
// macOS: ⌘/⌥/⌃/⇧ + key, Windows/Linux: Ctrl/Alt/Win/Shift + key.
|
||||
// 렌더러가 실행 중인 플랫폼을 알아내는 얇은 어댑터.
|
||||
//
|
||||
// 기존 binding.displayLabel은 키 녹화 시 만들어진 정적 문자열이라
|
||||
// 플랫폼이 다르면 잘못 보임 — 이 헬퍼가 단일 진입점.
|
||||
// 키 이름 · 수정자 표기 · 결합 규칙의 정본은 @d3ro/core/keybinding 이다
|
||||
// (formatBindingSegments / joinBindingSegments). 매핑 테이블을 여기에 두지 않는다.
|
||||
|
||||
import type { HotkeyBinding } from '@d3ro/core/types'
|
||||
import type { BindingPlatform } from '@d3ro/core/keybinding'
|
||||
|
||||
export type Platform = 'darwin' | 'win32' | 'linux'
|
||||
export type Platform = BindingPlatform
|
||||
|
||||
export function getPlatform(): Platform {
|
||||
const p = (window as { electronAPI?: { platform?: string } }).electronAPI?.platform
|
||||
|
|
@ -16,104 +15,3 @@ export function getPlatform(): Platform {
|
|||
if (typeof navigator !== 'undefined' && /Mac/i.test(navigator.platform)) return 'darwin'
|
||||
return 'win32'
|
||||
}
|
||||
|
||||
// keyCode → 키명. Windows VK_* 기준 + 일부 macOS 분기.
|
||||
export function keyCodeToName(keyCode: number, platform: Platform = getPlatform()): string {
|
||||
// Modifier 표기 — 플랫폼별
|
||||
if (platform === 'darwin') {
|
||||
if (keyCode === 16 || keyCode === 160 || keyCode === 161) return '⇧'
|
||||
if (keyCode === 17 || keyCode === 162 || keyCode === 163) return '⌃'
|
||||
if (keyCode === 18 || keyCode === 164 || keyCode === 165) return '⌥'
|
||||
if (keyCode === 91 || keyCode === 92) return '⌘'
|
||||
} else {
|
||||
if (keyCode === 16 || keyCode === 160 || keyCode === 161) return 'Shift'
|
||||
if (keyCode === 17 || keyCode === 162 || keyCode === 163) return 'Ctrl'
|
||||
if (keyCode === 18 || keyCode === 164 || keyCode === 165) return 'Alt'
|
||||
if (keyCode === 91 || keyCode === 92) return 'Win'
|
||||
}
|
||||
|
||||
// 공통 키
|
||||
switch (keyCode) {
|
||||
case 8: return 'Backspace'
|
||||
case 9: return 'Tab'
|
||||
case 13: return platform === 'darwin' ? '↩' : 'Enter'
|
||||
case 19: return 'Pause'
|
||||
case 20: return 'CapsLock'
|
||||
case 27: return 'Esc'
|
||||
case 32: return 'Space'
|
||||
case 33: return 'PgUp'
|
||||
case 34: return 'PgDn'
|
||||
case 35: return 'End'
|
||||
case 36: return 'Home'
|
||||
case 37: return '←'
|
||||
case 38: return '↑'
|
||||
case 39: return '→'
|
||||
case 40: return '↓'
|
||||
case 45: return 'Insert'
|
||||
case 46: return platform === 'darwin' ? '⌫' : 'Delete'
|
||||
case 186: return ';'
|
||||
case 187: return '='
|
||||
case 188: return ','
|
||||
case 189: return '-'
|
||||
case 190: return '.'
|
||||
case 191: return '/'
|
||||
case 192: return '`'
|
||||
case 219: return '['
|
||||
case 220: return '\\'
|
||||
case 221: return ']'
|
||||
case 222: return "'"
|
||||
default: break
|
||||
}
|
||||
|
||||
// F1~F24
|
||||
if (keyCode >= 112 && keyCode <= 135) return `F${keyCode - 111}`
|
||||
// Numpad
|
||||
if (keyCode >= 96 && keyCode <= 105) return `Num${keyCode - 96}`
|
||||
// 알파벳/숫자
|
||||
if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90)) {
|
||||
return String.fromCharCode(keyCode)
|
||||
}
|
||||
|
||||
return `Key${keyCode}`
|
||||
}
|
||||
|
||||
/**
|
||||
* binding을 플랫폼별 segment 배열로 변환.
|
||||
* 예) Mac: ['⌘', '⇧', '1'], Win: ['Win', 'Shift', '1']
|
||||
*
|
||||
* 순서:
|
||||
* - macOS: ⌃ ⌥ ⇧ ⌘ + key (Apple 표준)
|
||||
* - Win/Linux: Ctrl Win Alt Shift + key
|
||||
*/
|
||||
export function formatHotkeySegments(binding: HotkeyBinding | null): string[] {
|
||||
if (!binding) return []
|
||||
const platform = getPlatform()
|
||||
const segments: string[] = []
|
||||
|
||||
if (platform === 'darwin') {
|
||||
if (binding.ctrl) segments.push('⌃')
|
||||
if (binding.alt) segments.push('⌥')
|
||||
if (binding.shift) segments.push('⇧')
|
||||
if (binding.meta) segments.push('⌘')
|
||||
} else {
|
||||
if (binding.ctrl) segments.push('Ctrl')
|
||||
if (binding.meta) segments.push('Win')
|
||||
if (binding.alt) segments.push('Alt')
|
||||
if (binding.shift) segments.push('Shift')
|
||||
}
|
||||
|
||||
segments.push(keyCodeToName(binding.keyCode, platform))
|
||||
return segments
|
||||
}
|
||||
|
||||
/**
|
||||
* 사람이 읽기 위한 단일 문자열 형태.
|
||||
* macOS: '⌘⇧1' (구분자 없음 — Apple 표준)
|
||||
* Win/Linux: 'Ctrl + Shift + 1'
|
||||
*/
|
||||
export function formatHotkeyLabel(binding: HotkeyBinding | null): string {
|
||||
const segments = formatHotkeySegments(binding)
|
||||
if (segments.length === 0) return ''
|
||||
const platform = getPlatform()
|
||||
return platform === 'darwin' ? segments.join('') : segments.join(' + ')
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue