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): settings.keybindings overrides + renderer accessors

enes04af9813b189
diff --git a/src/renderer/lib/keybindingOverrides.test.ts b/src/renderer/lib/keybindingOverrides.test.ts
new file mode 100644
index 000000000..abb125a66
--- /dev/null
+++ b/src/renderer/lib/keybindingOverrides.test.ts
@@ -0,0 +1,53 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { DEFAULT_SETTINGS } from '@shared/types'
+import { useSettings } from '../state/settings'
+import { activeKeybindingOverrides, effectiveBindings, commandKeys, commandTooltip } from './keybindingOverrides'
+
+const setKb = (kb: unknown) =>
+ useSettings.setState({ settings: { ...DEFAULT_SETTINGS, keybindings: kb as never } })
+
+beforeEach(() => setKb(undefined))
+
+describe('activeKeybindingOverrides', () => {
+ it('absent key means no overrides, silently', () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ expect(activeKeybindingOverrides()).toEqual({})
+ expect(warn).not.toHaveBeenCalled()
+ warn.mockRestore()
+ })
+ it('sanitizes and memoizes by reference, warning once per change', () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ setKb({ 'node.newTerminal': ['Cmd+Shift+T'], 'bogus.command': ['Cmd+X'] })
+ const first = activeKeybindingOverrides()
+ expect(first).toEqual({ 'node.newTerminal': ['Cmd+Shift+T'] })
+ expect(activeKeybindingOverrides()).toBe(first)
+ expect(warn).toHaveBeenCalledTimes(1)
+ warn.mockRestore()
+ })
+})
+
+describe('effectiveBindings / commandKeys / commandTooltip', () => {
+ it('defaults flow through when no override exists', () => {
+ expect(effectiveBindings('app.commandPalette')).toEqual(['Cmd+K'])
+ })
+ it('an override replaces the default everywhere', () => {
+ setKb({ 'panel.sessions': ['Cmd+Alt+L'] })
+ expect(commandKeys('panel.sessions', true)).toEqual(['⌘', '⌥', 'L'])
+ expect(commandTooltip('Sessions', 'panel.sessions', true)).toBe('Sessions (⌘⌥L)')
+ })
+ it('matches the legacy hintLabel formatting on both platforms for the defaults', () => {
+ expect(commandTooltip('Sessions', 'panel.sessions', true)).toBe('Sessions (⌘⇧L)')
+ expect(commandTooltip('Sessions', 'panel.sessions', false)).toBe('Sessions (Ctrl+Shift+L)')
+ })
+ it('resolves the defaults with the SAME platform it formats with', () => {
+ // terminal.copySelection is the one command whose defaults differ per platform, so it is
+ // the only case that can catch a commandKeys that resolves with isMacPlatform() (true in
+ // node) while formatting for the caller's platform.
+ expect(commandKeys('terminal.copySelection', true)).toEqual(['⌘', 'C'])
+ expect(commandKeys('terminal.copySelection', false)).toEqual(['Ctrl', 'Shift', 'C'])
+ })
+ it('unbound commands render without a chord suffix', () => {
+ expect(commandTooltip('Fit all', 'canvas.fitAll', true)).toBe('Fit all')
+ expect(commandKeys('canvas.fitAll', true)).toEqual([])
+ })
+})
diff --git a/src/renderer/lib/keybindingOverrides.ts b/src/renderer/lib/keybindingOverrides.ts
new file mode 100644
index 000000000..d5f49d128
--- /dev/null
+++ b/src/renderer/lib/keybindingOverrides.ts
@@ -0,0 +1,47 @@
+/**
+ * The renderer's single read path for shortcut overrides: sanitize once per settings change
+ * (memoized by the raw object's reference — `useSettings.update` replaces the settings object
+ * but keeps untouched sub-objects), warn once, and serve effective bindings + display strings
+ * from it. Everything that shows or matches a registry chord goes through here so dispatch,
+ * ShortcutsPanel, and tooltips cannot disagree.
+ */
+import {
+ getEffectiveBindings, sanitizeKeybindingOverrides,
+ type CommandId, type KeybindingOverrides
+} from '@shared/keybindings'
+import { shortcutKeyParts } from '@shared/shortcut'
+import { isMacPlatform } from '@shared/platform-utils'
+import { useSettings } from '../state/settings'
+
+const UNSET = Symbol('unset')
+let lastRaw: unknown = UNSET
+let lastSanitized: KeybindingOverrides = {}
+
+export function activeKeybindingOverrides(): KeybindingOverrides {
+ const raw = useSettings.getState().settings.keybindings
+ if (raw === lastRaw) return lastSanitized
+ const { overrides, warnings } = sanitizeKeybindingOverrides(raw, isMacPlatform())
+ if (warnings.length) console.warn(`[keybindings] ${warnings.join(' ')}`)
+ lastRaw = raw
+ lastSanitized = overrides
+ return overrides
+}
+
+export function effectiveBindings(id: CommandId): readonly string[] {
+ return getEffectiveBindings(id, activeKeybindingOverrides(), isMacPlatform())
+}
+
+/** Display parts of the command's first effective binding; [] when unbound. */
+export function commandKeys(id: CommandId, isMac: boolean = isMacPlatform()): string[] {
+ const first = getEffectiveBindings(id, activeKeybindingOverrides(), isMac)[0]
+ return first ? shortcutKeyParts(first, isMac) : []
+}
+
+/** `('Sessions', 'panel.sessions')` -> `'Sessions (⌘⇧L)'` (mac) / `'Sessions (Ctrl+Shift+L)'`
+ * — the same strings hintLabel produced for the defaults, but following remaps; bare text
+ * when unbound. */
+export function commandTooltip(text: string, id: CommandId, isMac: boolean = isMacPlatform()): string {
+ const parts = commandKeys(id, isMac)
+ if (!parts.length) return text
+ return `${text} (${isMac ? parts.join('') : parts.join('+')})`
+}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index b3ca27dee..6497c954a 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -2,6 +2,7 @@
import { DEFAULT_WORKTREE_PATH_TEMPLATE } from './worktree'
import type { CloneProgress } from './clone-url'
+import type { KeybindingOverrides } from './keybindings'
import type { NormalizedAgentEvent } from './agents/normalize'
import type { AgentId, AgentPermissionMode, BuiltinAgentId, PromptInjectionMode } from './agents/config'
import type { AgentMessageDeliverRequest, AgentMessageReply } from './agents/agent-messaging'
@@ -1194,6 +1195,11 @@ export interface Settings {
notchHoverExpand: boolean
/** Dictation (desktop/server). Written as a whole object by the renderer. */
speech: SpeechSettings
+ /** Keyboard-shortcut overrides by command id (see shared/keybindings.ts). Absent id = the
+ * command's default bindings; `[]` = disabled. Hand-editable; invalid or conflicting
+ * entries are dropped with a console warning at read time (sanitizeKeybindingOverrides).
+ * Optional and deliberately not in DEFAULT_SETTINGS: absent simply means "no overrides". */
+ keybindings?: KeybindingOverrides
/** Per-node hook identity enforcement (src/core/agents/node-identity-policy.ts).
*
* The ONLY optional key in this interface, and deliberately so: it is a TRI-state, and the two