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(keybindings): shared key context + parse memo

enes149eb62224c7
diff --git a/src/renderer/lib/keyContext.test.ts b/src/renderer/lib/keyContext.test.ts
new file mode 100644
index 000000000..a780071a6
--- /dev/null
+++ b/src/renderer/lib/keyContext.test.ts
@@ -0,0 +1,64 @@
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'fs'
+import {
+ XTERM_INPUT_CLASS,
+ isTerminalTarget,
+ isTypingTarget,
+ keyDispatchContextFor
+} from './keyContext'
+
+const el = (
+ tagName: string,
+ extra: Partial<{ isContentEditable: boolean; classes: string[] }> = {}
+) => ({
+ tagName,
+ isContentEditable: extra.isContentEditable,
+ classList: { contains: (n: string) => (extra.classes ?? []).includes(n) }
+})
+
+describe('isTerminalTarget', () => {
+ it('true only for the xterm helper textarea', () => {
+ expect(isTerminalTarget(el('TEXTAREA', { classes: [XTERM_INPUT_CLASS] }))).toBe(true)
+ expect(isTerminalTarget(el('TEXTAREA'))).toBe(false)
+ expect(isTerminalTarget(null)).toBe(false)
+ })
+})
+
+describe('isTypingTarget', () => {
+ it('inputs, textareas and contentEditable are typing', () => {
+ expect(isTypingTarget(el('INPUT'))).toBe(true)
+ expect(isTypingTarget(el('TEXTAREA'))).toBe(true)
+ expect(isTypingTarget(el('DIV', { isContentEditable: true }))).toBe(true)
+ })
+ it('the xterm helper textarea is NOT typing (it is the terminal)', () => {
+ expect(isTypingTarget(el('TEXTAREA', { classes: [XTERM_INPUT_CLASS] }))).toBe(false)
+ })
+ it('plain elements and null are not typing', () => {
+ expect(isTypingTarget(el('DIV'))).toBe(false)
+ expect(isTypingTarget(null)).toBe(false)
+ })
+})
+
+describe('keyDispatchContextFor', () => {
+ it('typing and terminal are disjoint by construction', () => {
+ expect(keyDispatchContextFor(el('TEXTAREA', { classes: [XTERM_INPUT_CLASS] }), false)).toEqual({
+ typing: false,
+ terminal: true,
+ kanbanOpen: false
+ })
+ expect(keyDispatchContextFor(el('INPUT'), true)).toEqual({
+ typing: true,
+ terminal: false,
+ kanbanOpen: true
+ })
+ })
+})
+
+describe('xterm version pin', () => {
+ it('the installed xterm dist still renders the helper-textarea class', () => {
+ // The entire terminal-vs-typing split keys off this class name. If an xterm upgrade
+ // renames it, this test fails instead of every terminal keystroke silently becoming 'app'.
+ const dist = readFileSync('node_modules/@xterm/xterm/lib/xterm.js', 'utf8')
+ expect(dist.includes(XTERM_INPUT_CLASS)).toBe(true)
+ })
+})
diff --git a/src/renderer/lib/keyContext.ts b/src/renderer/lib/keyContext.ts
new file mode 100644
index 000000000..1bcc52eb5
--- /dev/null
+++ b/src/renderer/lib/keyContext.ts
@@ -0,0 +1,37 @@
+/**
+ * One shared answer to "who owns this keystroke's focus" for the global-keybinding
+ * dispatcher. Replaces the three inline `tagName` guards the Canvas keydown effects carried
+ * (which disagreed about contentEditable and counted xterm's hidden textarea as typing).
+ */
+import type { KeyDispatchContext } from '@shared/keybindings'
+
+/** xterm.js takes keyboard input through a hidden <textarea> with this class. Pinned against
+ * the installed dist by keyContext.test.ts — an upgrade that renames it must fail loudly. */
+export const XTERM_INPUT_CLASS = 'xterm-helper-textarea'
+
+/** Structural element shape so node-env tests need no DOM. */
+export interface ContextElement {
+ tagName: string
+ isContentEditable?: boolean
+ classList?: { contains(name: string): boolean }
+}
+
+export function isTerminalTarget(el: ContextElement | null): boolean {
+ return el?.classList?.contains(XTERM_INPUT_CLASS) === true
+}
+
+/** True when the keystroke belongs to text the user is editing. The xterm textarea is
+ * deliberately excluded — a focused terminal is `terminal`, never `typing`, and the two
+ * must stay disjoint (see KeyDispatchContext). */
+export function isTypingTarget(el: ContextElement | null): boolean {
+ if (!el || isTerminalTarget(el)) return false
+ if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') return true
+ return el.isContentEditable === true
+}
+
+export function keyDispatchContextFor(
+ el: ContextElement | null,
+ kanbanOpen: boolean
+): KeyDispatchContext {
+ return { typing: isTypingTarget(el), terminal: isTerminalTarget(el), kanbanOpen }
+}
diff --git a/src/shared/shortcut.test.ts b/src/shared/shortcut.test.ts
index 6bd332e9c..3e0cc75a2 100644
--- a/src/shared/shortcut.test.ts
+++ b/src/shared/shortcut.test.ts
@@ -478,3 +478,12 @@ describe('resolvedModifiers', () => {
expect(resolvedModifiers(p, false)).toEqual({ meta: false, ctrl: true, alt: false, shift: true })
})
})
+
+describe('parseShortcut memoization', () => {
+ it('returns the same frozen object for repeated input', () => {
+ const a = parseShortcut('Cmd+Shift+D')
+ const b = parseShortcut('Cmd+Shift+D')
+ expect(b).toBe(a)
+ expect(Object.isFrozen(a)).toBe(true)
+ })
+})
diff --git a/src/shared/shortcut.ts b/src/shared/shortcut.ts
index 6113f87ff..89166f093 100644
--- a/src/shared/shortcut.ts
+++ b/src/shared/shortcut.ts
@@ -75,7 +75,14 @@ export function isModifierEventKey(key: string): boolean {
* alt:false, key:'D'}`; a modifier-only chord, e.g. `"Cmd+Alt"` -> `{cmd:true, ctrl:false,
* shift:false, alt:true, key:null}` (see `isHoldChord`). `Ctrl`/`Control` set the LITERAL `ctrl`
* field — they are no longer a spelling of the abstract `Cmd`. */
+// Memo: dispatch re-parses every candidate binding on every keydown once the registry sits on
+// a window listener; distinct binding strings are few, so a small frozen-value cache removes
+// the hot-path cost for every consumer at once.
+const parseCache = new Map<string, ParsedShortcut>()
+
export function parseShortcut(s: string): ParsedShortcut {
+ const hit = parseCache.get(s)
+ if (hit) return hit
const parts = s
.split('+')
.map((p) => p.trim())
@@ -100,7 +107,10 @@ export function parseShortcut(s: string): ParsedShortcut {
key = normalizeKey(part)
}
}
- return { cmd, ctrl, shift, alt, key }
+ const parsed = Object.freeze({ cmd, ctrl, shift, alt, key })
+ if (parseCache.size > 512) parseCache.clear() // hand-edited garbage churn guard
+ parseCache.set(s, parsed)
+ return parsed
}
/** Canonical spellings for the key tokens that are not a single character. Serializing through