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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(keybindings): pure event-to-command resolver for the dispatch layer
enesecde6495658a
diff --git a/src/shared/keybindings.test.ts b/src/shared/keybindings.test.tsindex e8f054ec9..942379342 100644--- a/src/shared/keybindings.test.ts+++ b/src/shared/keybindings.test.ts@@ -7,9 +7,11 @@ import {getEffectiveBindings,bindingIdentity,findKeybindingConflicts,- sanitizeKeybindingOverrides+ sanitizeKeybindingOverrides,+ resolveCommandForKeyEvent} from './keybindings'import { parseShortcut } from './shortcut'+import type { ShortcutKeyEvent } from './shortcut'describe('registry invariants', () => {it('has unique ids and a map that covers them all', () => {@@ -226,3 +228,68 @@ describe('sanitizeKeybindingOverrides', () => {expect(twice).toEqual({ overrides: once.overrides, warnings: [] })})})++const ev = (over: Partial<ShortcutKeyEvent>): ShortcutKeyEvent => ({+ metaKey: false, ctrlKey: false, shiftKey: false, altKey: false, key: '', ...over+})+const ctx = (over: Partial<{ typing: boolean; terminal: boolean; kanbanOpen: boolean }> = {}) => ({+ typing: false, terminal: false, kanbanOpen: false, ...over+})++describe('resolveCommandForKeyEvent', () => {+ it('matches a default app chord', () => {+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'k' }), ctx(), {}, true))+ .toBe('app.commandPalette')+ })+ it('typing blocks commands without allowWhileTyping, allows the flagged ones', () => {+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, key: 't' }), ctx({ typing: true }), {}, true+ )).toBeNull()+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, key: 'w' }), ctx({ typing: true }), {}, true+ )).toBe('node.close')+ })+ it('terminal focus blocks canvas commands but passes allowInTerminal + terminal scope', () => {+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, key: 't' }), ctx({ terminal: true }), {}, true+ )).toBeNull()+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, key: 'k' }), ctx({ terminal: true }), {}, true+ )).toBe('app.commandPalette')+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, key: 'f' }), ctx({ terminal: true }), {}, true+ )).toBe('terminal.find')+ })+ it('kanban open makes canvas-scope commands inert, app scope still fires', () => {+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, key: 'z' }), ctx({ kanbanOpen: true }), {}, true+ )).toBeNull()+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, shiftKey: true, key: 'b' }), ctx({ kanbanOpen: true }), {}, true+ )).toBe('view.kanbanToggle')+ })+ it('terminal.find does NOT fire outside terminal focus (scope-gated)', () => {+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'f' }), ctx(), {}, true)).toBeNull()+ })+ it('respects overrides and disabled commands', () => {+ const o = { 'app.commandPalette': [] as string[], 'canvas.fitAll': ['Cmd+K'] }+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'k' }), ctx(), o, true))+ .toBe('canvas.fitAll')+ })+ it('never resolves a hold chord', () => {+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, altKey: true, key: 'Alt' }), ctx(), {}, true+ )).toBeNull()+ })+ it('never resolves scm-scope commands — they dispatch from their own composer', () => {+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'Enter' }), ctx(), {}, true))+ .toBeNull()+ })+ it('registry order breaks cross-bucket identity ties deterministically', () => {+ // Cmd+F sits on terminal.find (terminal bucket); an app-bucket override may share it.+ const o = { 'canvas.fitAll': ['Cmd+F'] }+ expect(resolveCommandForKeyEvent(+ ev({ metaKey: true, key: 'f' }), ctx({ terminal: true }), o, true+ )).toBe('terminal.find')+ })+})diff --git a/src/shared/keybindings.ts b/src/shared/keybindings.tsindex 3ca7fa197..efcfec75e 100644--- a/src/shared/keybindings.ts+++ b/src/shared/keybindings.ts@@ -6,7 +6,13 @@* canonical binding strings are shortcut.ts strings (`Cmd` = abstract primary modifier,* `Ctrl` = literal Control, modifier-only chords = hold-to-talk).*/-import { parseShortcut, serializeShortcut, resolvedModifiers } from './shortcut'+import {+ parseShortcut,+ serializeShortcut,+ resolvedModifiers,+ matchesShortcut,+ isHoldChord+} from './shortcut'import type { ParsedShortcut, ShortcutKeyEvent } from './shortcut'export type CommandScope = 'app' | 'canvas' | 'terminal' | 'scm'@@ -313,3 +319,38 @@ export function sanitizeKeybindingOverrides(}return { overrides, warnings }}++export interface KeyDispatchContext {+ /** A real input/textarea/contentEditable has focus (xterm's hidden textarea excluded). */+ typing: boolean+ /** An xterm has focus. */+ terminal: boolean+ /** The kanban board is open for the active project. */+ kanbanOpen: boolean+}++/** Pure dispatch core: the first registry command (source order) whose effective bindings+ * match `e` and whose flags permit the context. Keyed chords only — hold chords are the+ * dedicated hold listener's job (`chordHeld`). Callers preventDefault only when the+ * returned command's handler actually claims the chord. */+export function resolveCommandForKeyEvent(+ e: ShortcutKeyEvent,+ ctx: KeyDispatchContext,+ overrides: KeybindingOverrides,+ isMac: boolean+): CommandId | null {+ for (const def of COMMAND_DEFINITIONS) {+ // scm commands dispatch from their own focused composer (local onKeyDown), never from+ // the window listener — resolving them here would fire Commit with no composer focused.+ if (def.scope === 'scm') continue+ if (ctx.typing && !def.allowWhileTyping) continue+ if (ctx.terminal && !(def.scope === 'terminal' || def.allowInTerminal)) continue+ if (!ctx.terminal && def.scope === 'terminal') continue+ if (ctx.kanbanOpen && def.scope === 'canvas') continue+ for (const binding of getEffectiveBindings(def.id, overrides, isMac)) {+ if (isHoldChord(binding)) continue+ if (matchesShortcut(e, binding, isMac)) return def.id+ }+ }+ return null+}