Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(settings): shortcut recorder with hold-chord and non-primary-modifier support

enes32f2cccd055c
diff --git a/src/renderer/components/settings/ShortcutRecorderButton.tsx b/src/renderer/components/settings/ShortcutRecorderButton.tsx
new file mode 100644
index 000000000..ac8a644b4
--- /dev/null
+++ b/src/renderer/components/settings/ShortcutRecorderButton.tsx
@@ -0,0 +1,85 @@
+/**
+ * Generalized shortcut recorder: click to arm, press a chord. Keyed chords commit on keydown;
+ * for allowHoldChord commands a modifier-only chord commits on full release (hold-to-talk).
+ * While armed it sets data-shortcut-recording (the window dispatcher bails on it via
+ * defaultPrevented — every key here is preventDefault'ed). The main-process recording bit
+ * (so ⌘W/⌘M/⌘0 interception cannot swallow the capture) is Task 5's: `window.nodeTerminal
+ * .shortcuts` does not exist on `NodeTerminalApi` yet, and that type lives in
+ * `src/shared/types.ts` — a file this task does not own. An optional-chained call does NOT
+ * typecheck against a member the interface never declares, so the two calls are marked below
+ * rather than shipped broken.
+ */
+import { useRef, useState } from 'react'
+import { COMMANDS_BY_ID, normalizeBindingForCommand, type CommandId } from '@shared/keybindings'
+import { isMacPlatform } from '@shared/platform-utils'
+import { recordingKeydown, recordingKeyup, type RecordingState } from './shortcutRecording'
+
+const isMac = isMacPlatform()
+
+export function ShortcutRecorderButton({
+ commandId,
+ idleLabel,
+ onCommit
+}: {
+ commandId: CommandId
+ idleLabel: string
+ onCommit: (combo: string) => void
+}): React.JSX.Element {
+ const [capturing, setCapturing] = useState(false)
+ const [hint, setHint] = useState('')
+ const stateRef = useRef<RecordingState>({ mods: null })
+ const def = COMMANDS_BY_ID.get(commandId)!
+ const opts = { isMac, allowHold: def.allowHoldChord === true }
+
+ const stop = (): void => {
+ setCapturing(false)
+ setHint('')
+ stateRef.current = { mods: null }
+ // Task 5 wires the main-process recording bit here: setRecording(false)
+ }
+ const start = (): void => {
+ stateRef.current = { mods: null }
+ setCapturing(true)
+ setHint('')
+ // Task 5 wires the main-process recording bit here: setRecording(true)
+ }
+ const commit = (combo: string): void => {
+ const r = normalizeBindingForCommand(def, combo, isMac)
+ if (!r.ok) {
+ setHint(r.error)
+ return
+ }
+ onCommit(r.value)
+ stop()
+ }
+ const onKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>): void => {
+ if (!capturing) return
+ e.preventDefault()
+ e.stopPropagation()
+ const action = recordingKeydown(stateRef.current, e, opts)
+ if (action.kind === 'cancel') stop()
+ else if (action.kind === 'commit') commit(action.combo)
+ else if (action.kind === 'pending') {
+ stateRef.current = action.state
+ setHint(action.hint)
+ }
+ }
+ const onKeyUp = (e: React.KeyboardEvent<HTMLButtonElement>): void => {
+ if (!capturing) return
+ const action = recordingKeyup(stateRef.current, e, opts)
+ if (action.kind === 'commit') commit(action.combo)
+ }
+ return (
+ <button
+ type="button"
+ data-shortcut-recording={capturing || undefined}
+ className="min-w-[120px] cursor-pointer rounded-md border border-border bg-panel-header px-3 py-1.5 text-[13px] font-medium text-text outline-none hover:bg-[rgba(255,255,255,0.06)]"
+ onClick={start}
+ onKeyDown={onKeyDown}
+ onKeyUp={onKeyUp}
+ onBlur={stop}
+ >
+ {capturing ? hint || 'Press keys…' : idleLabel}
+ </button>
+ )
+}
diff --git a/src/renderer/components/settings/shortcutRecording.test.ts b/src/renderer/components/settings/shortcutRecording.test.ts
new file mode 100644
index 000000000..86c1acbd6
--- /dev/null
+++ b/src/renderer/components/settings/shortcutRecording.test.ts
@@ -0,0 +1,78 @@
+import { describe, it, expect } from 'vitest'
+import { recordingKeydown, recordingKeyup } from './shortcutRecording'
+
+const e = (
+ over: Partial<{ metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean; key: string }>
+) => ({
+ metaKey: false,
+ ctrlKey: false,
+ shiftKey: false,
+ altKey: false,
+ key: '',
+ ...over
+})
+const HOLD = { isMac: true, allowHold: true }
+const KEYED = { isMac: true, allowHold: false }
+
+describe('recordingKeydown', () => {
+ it('a keyed chord commits immediately', () => {
+ expect(recordingKeydown({ mods: null }, e({ metaKey: true, key: 'd' }), KEYED)).toEqual({
+ kind: 'commit',
+ combo: 'Cmd+D'
+ })
+ })
+ it('mac Ctrl rides along in a keyed capture (the PR1 gap, closed)', () => {
+ expect(
+ recordingKeydown({ mods: null }, e({ metaKey: true, ctrlKey: true, key: 'd' }), KEYED)
+ ).toEqual({ kind: 'commit', combo: 'Cmd+Ctrl+D' })
+ })
+ it('Escape cancels', () => {
+ expect(recordingKeydown({ mods: null }, e({ key: 'Escape' }), KEYED)).toEqual({ kind: 'cancel' })
+ })
+ it('modifier-only keydown is pending: remembered for a hold commit when allowed', () => {
+ const r = recordingKeydown({ mods: null }, e({ metaKey: true, altKey: true, key: 'Alt' }), HOLD)
+ expect(r.kind).toBe('pending')
+ if (r.kind === 'pending') expect(r.state.mods).toEqual({ cmd: true, ctrl: false, alt: true, shift: false })
+ })
+ it('mac Ctrl is collected into a hold chord (the PR1 gap, closed)', () => {
+ const r = recordingKeydown({ mods: null }, e({ metaKey: true, ctrlKey: true, key: 'Control' }), HOLD)
+ if (r.kind === 'pending') expect(r.state.mods?.ctrl).toBe(true)
+ expect(r.kind).toBe('pending')
+ })
+ it('non-mac held Super is refused with a hint', () => {
+ const r = recordingKeydown({ mods: null }, e({ metaKey: true, ctrlKey: true, key: 'Control' }), {
+ isMac: false,
+ allowHold: true
+ })
+ expect(r.kind).toBe('pending')
+ if (r.kind === 'pending') {
+ expect(r.state.mods).toBeNull()
+ expect(r.hint).toContain('Super')
+ }
+ })
+ it('modifier-only keydown without allowHold just previews the requirement', () => {
+ const r = recordingKeydown({ mods: null }, e({ metaKey: true, key: 'Meta' }), KEYED)
+ expect(r.kind).toBe('pending')
+ if (r.kind === 'pending') expect(r.state.mods).toBeNull()
+ })
+})
+
+describe('recordingKeyup', () => {
+ it('full release commits the remembered hold chord', () => {
+ const armed = { mods: { cmd: true, ctrl: false, alt: true, shift: false } }
+ expect(recordingKeyup(armed, e({}), HOLD)).toEqual({ kind: 'commit', combo: 'Cmd+Alt' })
+ })
+ it('partial release keeps waiting; no remembered mods ignores', () => {
+ const armed = { mods: { cmd: true, ctrl: false, alt: true, shift: false } }
+ expect(recordingKeyup(armed, e({ metaKey: true }), HOLD)).toEqual({ kind: 'ignored' })
+ expect(recordingKeyup({ mods: null }, e({}), HOLD)).toEqual({ kind: 'ignored' })
+ })
+ // WATCH-2: the legacy SpeechSection `anyModDown` omitted ctrlKey, so a mac hold chord recorded
+ // with Ctrl held committed while Ctrl was still down — mis-capturing the gesture. The full
+ // release condition must read ALL FOUR modifier flags.
+ it('a keyup with only Ctrl still held is ignored (the anyModDown gap, closed)', () => {
+ const armed = { mods: { cmd: true, ctrl: true, alt: false, shift: false } }
+ expect(recordingKeyup(armed, e({ ctrlKey: true }), HOLD)).toEqual({ kind: 'ignored' })
+ expect(recordingKeyup(armed, e({}), HOLD)).toEqual({ kind: 'commit', combo: 'Cmd+Ctrl' })
+ })
+})
diff --git a/src/renderer/components/settings/shortcutRecording.ts b/src/renderer/components/settings/shortcutRecording.ts
new file mode 100644
index 000000000..bc5c0aa67
--- /dev/null
+++ b/src/renderer/components/settings/shortcutRecording.ts
@@ -0,0 +1,131 @@
+/**
+ * The pure capture state machine behind the Settings shortcut recorder. Generalizes the legacy
+ * SpeechSection `ShortcutCaptureField` (one hard-wired dictation chord) into a decision function
+ * any command row can drive, and closes the two gaps that field shipped with.
+ *
+ * Two commit shapes, exactly as the v3 chord grammar defines them:
+ * - A KEYED chord (primary modifier + a non-modifier key) commits IMMEDIATELY on keydown.
+ * - A HOLD chord (modifier keys only — permitted only when `allowHold`, i.e. the command's
+ * `allowHoldChord`) commits on KEYUP once every key is released. The keyup event no longer
+ * carries the modifier state that was held, so each modifier keydown along the way remembers
+ * it in `RecordingState.mods` (see `buildModifierChord`'s doc in @shared/shortcut).
+ * Escape cancels. The caller cancels on blur.
+ *
+ * The gaps this closes, both instances of "a capture must describe the WHOLE gesture" (matching is
+ * exact on all four modifier flags, so a chord that omits a modifier the user was holding can
+ * never fire again):
+ * 1. **mac Ctrl is collected**, in BOTH paths — the keyed path via `captureToShortcut`, the hold
+ * path via `ChordModifiers.ctrl` here. The legacy field built `{cmd, alt, shift}` and dropped
+ * a held ⌃ on the floor.
+ * 2. **Full release reads ALL FOUR flags.** The legacy `anyModDown` was
+ * `(isMac ? metaKey : ctrlKey) || altKey || shiftKey` — no `ctrlKey`, so a mac chord recorded
+ * with Ctrl held committed while Ctrl was still down, recording a chord the user never
+ * finished making.
+ * A non-mac held Meta (Super/Win) is REFUSED rather than recorded as a bare `Cmd+…`: that gesture
+ * has no spelling in this grammar (same rule `captureToShortcut` already applies to keyed
+ * captures, here extended to the hold path).
+ *
+ * Pure: no DOM, no React, no clock. The component owns `RecordingState` in a ref and the caller
+ * validates the committed combo through `normalizeBindingForCommand` — this module decides only
+ * what the gesture MEANT, never whether the command accepts it.
+ */
+import {
+ buildModifierChord,
+ captureToShortcut,
+ formatShortcut,
+ isModifierEventKey,
+ type ChordModifiers,
+ type ShortcutKeyEvent
+} from '@shared/shortcut'
+
+/** What the recorder remembers between events: the strongest modifier-only state seen so far,
+ * or `null` while no hold chord is armed. */
+export interface RecordingState {
+ mods: ChordModifiers | null
+}
+
+export type RecordingAction =
+ /** A complete gesture. The combo is canonical but NOT yet validated for the command. */
+ | { kind: 'commit'; combo: string }
+ /** Escape — stop recording, change nothing. */
+ | { kind: 'cancel' }
+ /** Still recording: adopt `state` and show `hint`. */
+ | { kind: 'pending'; state: RecordingState; hint: string }
+ /** Nothing to do (and nothing to show) — leave the recorder exactly as it was. */
+ | { kind: 'ignored' }
+
+export interface RecordingOptions {
+ isMac: boolean
+ /** The command's `allowHoldChord`. When false, a modifier-only gesture can never commit — it
+ * only previews the requirement, and the user must go on to press a real key. */
+ allowHold: boolean
+}
+
+/** Non-mac Meta (Super/Win): unspellable in this grammar, in both the keyed and hold paths. */
+const SUPER_UNSUPPORTED = 'The Super key is not supported.'
+
+const holdPrimaryHint = (isMac: boolean): string => (isMac ? 'Hold ⌘…' : 'Hold Ctrl…')
+const keyedPrimaryHint = (isMac: boolean): string =>
+ isMac ? 'Hold ⌘ and press a key' : 'Hold Ctrl and press a key'
+
+/** Decide what a keydown during recording means. `state` is the recorder's current state; the
+ * returned `pending` state replaces it (a `commit`/`cancel`/`ignored` leaves it to the caller,
+ * which stops recording on the first two). */
+export function recordingKeydown(
+ state: RecordingState,
+ e: ShortcutKeyEvent & { key: string },
+ opts: RecordingOptions
+): RecordingAction {
+ const { isMac, allowHold } = opts
+ if (e.key === 'Escape') return { kind: 'cancel' }
+
+ if (isModifierEventKey(e.key)) {
+ // Only modifier keys are down so far. When the command permits a hold chord this is a
+ // candidate commit-on-release; otherwise it is purely a preview of what is still missing.
+ if (!allowHold) return { kind: 'pending', state: { mods: null }, hint: keyedPrimaryHint(isMac) }
+ if (!isMac && e.metaKey) return { kind: 'pending', state: { mods: null }, hint: SUPER_UNSUPPORTED }
+ const primaryPressed = isMac ? e.metaKey : e.ctrlKey
+ if (!primaryPressed) {
+ return { kind: 'pending', state: { mods: null }, hint: holdPrimaryHint(isMac) }
+ }
+ // A literal ⌃ beside the primary is part of the gesture on mac; off-mac `Cmd` already IS
+ // ctrlKey, so recording both would be the chord keybindings.ts validation forbids.
+ const mods: ChordModifiers = {
+ cmd: true,
+ ctrl: isMac && e.ctrlKey,
+ alt: e.altKey,
+ shift: e.shiftKey
+ }
+ const preview = buildModifierChord(mods)
+ return {
+ kind: 'pending',
+ state: { mods },
+ hint: preview ? `Release for ${formatShortcut(preview, isMac)} — or press a key` : ''
+ }
+ }
+
+ const combo = captureToShortcut(e, isMac)
+ if (combo) return { kind: 'commit', combo }
+ // Refused: either the primary modifier is missing, or (off-mac) Super is held. Keep whatever
+ // hold chord was armed — the user may still complete it by releasing everything.
+ return {
+ kind: 'pending',
+ state,
+ hint: !isMac && e.metaKey ? SUPER_UNSUPPORTED : keyedPrimaryHint(isMac)
+ }
+}
+
+/** Decide what a keyup during recording means: a hold chord commits once EVERY modifier is up. */
+export function recordingKeyup(
+ state: RecordingState,
+ e: ShortcutKeyEvent,
+ opts: RecordingOptions
+): RecordingAction {
+ if (!opts.allowHold || !state.mods) return { kind: 'ignored' }
+ // All four flags. `ctrlKey` is the one the legacy field forgot — see the module doc. The keyup
+ // event's own flags already exclude the key just released, so "no flag set" means fully up.
+ if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) return { kind: 'ignored' }
+ const combo = buildModifierChord(state.mods)
+ if (!combo) return { kind: 'ignored' }
+ return { kind: 'commit', combo }
+}