Files
Reims-Go/frontend/src/lib/fullscreenHotkey.ts
T

256 lines
7.1 KiB
TypeScript

export const DEFAULT_FULLSCREEN_HOTKEY = 'Ctrl+Shift+F'
export interface HotkeyModifiers {
ctrl: boolean
shift: boolean
alt: boolean
win: boolean
}
export interface HotkeyKeyEventLike {
code?: string
key?: string
ctrlKey?: boolean
shiftKey?: boolean
altKey?: boolean
metaKey?: boolean
repeat?: boolean
}
export type FullscreenHotkeyErrorReason = 'unsupported' | 'needs_modifier' | 'reserved'
export type FullscreenHotkeyCaptureResult =
| { kind: 'cancel' }
| { kind: 'ignore' }
| { kind: 'ok'; hotkey: string }
| { kind: 'error'; reason: FullscreenHotkeyErrorReason; hotkey?: string }
export type FullscreenHotkeyParseResult =
| { ok: true; hotkey: string }
| { ok: false; reason: FullscreenHotkeyErrorReason | 'invalid'; hotkey?: string }
const MODIFIER_CODES = new Set([
'ControlLeft',
'ControlRight',
'ShiftLeft',
'ShiftRight',
'AltLeft',
'AltRight',
'MetaLeft',
'MetaRight',
'OSLeft',
'OSRight',
])
const MODIFIER_KEYS = new Set([
'Control',
'Shift',
'Alt',
'AltGraph',
'Meta',
'OS',
])
const BACKEND_NAMED_KEYS: Record<string, string> = {
ENTER: 'Enter',
RETURN: 'Enter',
SPACE: 'Space',
TAB: 'Tab',
ESCAPE: 'Escape',
ESC: 'Escape',
BACKSPACE: 'Backspace',
INSERT: 'Insert',
DELETE: 'Delete',
HOME: 'Home',
END: 'End',
PAGEUP: 'PageUp',
PAGEDOWN: 'PageDown',
UP: 'Up',
DOWN: 'Down',
LEFT: 'Left',
RIGHT: 'Right',
}
const EVENT_KEY_ALIASES: Record<string, string> = {
...BACKEND_NAMED_KEYS,
SPACEBAR: 'Space',
DEL: 'Delete',
ARROWUP: 'Up',
ARROWDOWN: 'Down',
ARROWLEFT: 'Left',
ARROWRIGHT: 'Right',
}
const CODE_TO_KEY: Record<string, string> = {
Enter: 'Enter',
NumpadEnter: 'Enter',
Space: 'Space',
Tab: 'Tab',
Escape: 'Escape',
Backspace: 'Backspace',
Insert: 'Insert',
Delete: 'Delete',
Home: 'Home',
End: 'End',
PageUp: 'PageUp',
PageDown: 'PageDown',
ArrowUp: 'Up',
ArrowDown: 'Down',
ArrowLeft: 'Left',
ArrowRight: 'Right',
}
export function isFunctionKey(key: string): boolean {
return /^F(?:[1-9]|1[0-2])$/.test(key)
}
export function isReservedFullscreenHotkey(mods: HotkeyModifiers, key: string): boolean {
return (
(mods.ctrl && mods.alt && key === 'Delete') ||
(mods.alt && (key === 'F4' || key === 'Tab')) ||
(mods.win && key === 'L')
)
}
function formatCanonical(mods: HotkeyModifiers, key: string): string {
const parts: string[] = []
if (mods.ctrl) parts.push('Ctrl')
if (mods.shift) parts.push('Shift')
if (mods.alt) parts.push('Alt')
if (mods.win) parts.push('Win')
parts.push(key)
return parts.join('+')
}
// Matches Go's strings.TrimSpace (Unicode White_Space, notably excluding U+FEFF).
function trimGoSpace(s: string): string {
return s.replace(/^[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+|[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+$/g, '')
}
function normalizePrimaryToken(token: string, aliases: Record<string, string> = BACKEND_NAMED_KEYS): string | null {
const upper = trimGoSpace(token).toUpperCase()
if (!upper) return null
if (/^[A-Z0-9]$/.test(upper)) return upper
if (/^F(?:[1-9]|1[0-2])$/.test(upper)) return upper
return aliases[upper] ?? null
}
function mapEventPrimaryKey(e: HotkeyKeyEventLike): string | null {
const code = (e.code ?? '').trim()
const key = e.key ?? ''
// Numpad digits/operators have distinct Windows VK codes from top-row digits,
// so avoid falling back to e.key for unmapped Numpad keys.
if (code.startsWith('Numpad') && code !== 'NumpadEnter') return null
// Prefer layout-aware ASCII letters from e.key so non-US layouts (e.g. QWERTZ)
// record the same virtual key letter that Windows WH_KEYBOARD_LL receives.
if (/^[a-zA-Z]$/.test(key)) return key.toUpperCase()
if (code) {
const letter = /^Key([A-Z])$/.exec(code)
if (letter) return letter[1]
const digit = /^Digit([0-9])$/.exec(code)
if (digit) return digit[1]
if (/^F(?:[1-9]|1[0-2])$/.test(code)) return code
if (code in CODE_TO_KEY) return CODE_TO_KEY[code]
}
if (key === ' ') return 'Space'
return normalizePrimaryToken(key, EVENT_KEY_ALIASES)
}
export function parseFullscreenHotkey(raw: string): FullscreenHotkeyParseResult {
const input = trimGoSpace(raw) || DEFAULT_FULLSCREEN_HOTKEY
const mods: HotkeyModifiers = { ctrl: false, shift: false, alt: false, win: false }
let primary = ''
for (const part of input.split('+')) {
const token = trimGoSpace(part).toUpperCase()
if (!token) return { ok: false, reason: 'invalid' }
if (token === 'CTRL' || token === 'CONTROL') {
if (mods.ctrl) return { ok: false, reason: 'invalid' }
mods.ctrl = true
continue
}
if (token === 'SHIFT') {
if (mods.shift) return { ok: false, reason: 'invalid' }
mods.shift = true
continue
}
if (token === 'ALT' || token === 'OPTION') {
if (mods.alt) return { ok: false, reason: 'invalid' }
mods.alt = true
continue
}
if (token === 'WIN' || token === 'META' || token === 'SUPER') {
if (mods.win) return { ok: false, reason: 'invalid' }
mods.win = true
continue
}
if (primary) return { ok: false, reason: 'invalid' }
const mapped = normalizePrimaryToken(token)
if (!mapped) return { ok: false, reason: 'unsupported' }
primary = mapped
}
if (!primary) return { ok: false, reason: 'invalid' }
const hasModifier = mods.ctrl || mods.shift || mods.alt || mods.win
if (!hasModifier && !isFunctionKey(primary)) {
return { ok: false, reason: 'needs_modifier', hotkey: primary }
}
const canonical = formatCanonical(mods, primary)
if (isReservedFullscreenHotkey(mods, primary)) {
return { ok: false, reason: 'reserved', hotkey: canonical }
}
return { ok: true, hotkey: canonical }
}
export function resolveFullscreenHotkey(raw?: string, fallback = DEFAULT_FULLSCREEN_HOTKEY): string {
const parsed = parseFullscreenHotkey(raw ?? '')
return parsed.ok ? parsed.hotkey : fallback
}
export function splitFullscreenHotkey(hotkey: string): string[] {
return hotkey
.split('+')
.map((part) => part.trim())
.filter(Boolean)
}
export function captureFullscreenHotkeyEvent(e: HotkeyKeyEventLike): FullscreenHotkeyCaptureResult {
if (e.repeat) return { kind: 'ignore' }
const code = (e.code ?? '').trim()
const key = e.key ?? ''
if (MODIFIER_CODES.has(code) || MODIFIER_KEYS.has(key)) {
return { kind: 'ignore' }
}
const mods: HotkeyModifiers = {
ctrl: !!e.ctrlKey,
shift: !!e.shiftKey,
alt: !!e.altKey,
win: !!e.metaKey,
}
const hasModifier = mods.ctrl || mods.shift || mods.alt || mods.win
if (!hasModifier && (code === 'Escape' || key === 'Escape' || key === 'Esc')) {
return { kind: 'cancel' }
}
const primary = mapEventPrimaryKey(e)
if (!primary) {
return { kind: 'error', reason: 'unsupported' }
}
if (!hasModifier && !isFunctionKey(primary)) {
return { kind: 'error', reason: 'needs_modifier', hotkey: primary }
}
const canonical = formatCanonical(mods, primary)
if (isReservedFullscreenHotkey(mods, primary)) {
return { kind: 'error', reason: 'reserved', hotkey: canonical }
}
return { kind: 'ok', hotkey: canonical }
}