feat(shortcuts): make Select Tab/Workspace 1–9 remappable (#5789)

* feat(shortcuts): make Select Tab/Workspace 1-9 remappable (#5741)

Expose Select Tab 1-9 and Select Workspace 1-9 as single remappable rows in
Settings -> Shortcuts. Previously both chords were hardcoded in
window-shortcut-policy.ts and absent from the Shortcuts UI.

Each is modeled as one ranged action whose stored chord is a representative
(digit canonicalized to 1) that fires for any of 1-9, mirroring the cmux
single-row UX. Defaults reproduce existing behavior (mac: tab Ctrl+1-9,
workspace Cmd+1-9; win/linux: tab Alt+1-9, workspace Ctrl+1-9). Swapping the
two modifiers - the headline use case - works without a false conflict.

Co-authored-by: Orca <help@stably.ai>

* refactor(shortcuts): tighten digit-index review nits

- Canonicalize digit-index overrides in getEffectiveKeybindingsForAction so
  display/conflict detection stay consistent regardless of stored digit.
- Use a shared DIGIT_INDEX_KEY_PATTERN regex instead of string comparison.
- Document that extra modifiers (e.g. Shift) are allowed on digit-index chords
  and that the matcher honors terminal-context gating.
- Clarify the workspace-first precedence comment.
- Add tests: Shift modifier, physical-code fallback matching, both ranges disabled.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Neil
2026-06-19 02:29:07 -07:00
committed by GitHub
co-authored by Orca
parent 4e4fa9b8ee
commit d855c69fe3
5 changed files with 470 additions and 56 deletions
@@ -2,6 +2,7 @@ import React, { useEffect, useRef } from 'react'
import { Ban, Plus, RotateCcw, Terminal } from 'lucide-react'
import {
formatKeybinding,
isDigitIndexActionId,
isDoubleTapBinding,
type KeybindingActionId,
type KeybindingDefinition,
@@ -43,6 +44,16 @@ export type ShortcutTerminalStatus = {
description: string
}
// Why: digit-index rows store one representative chord (e.g. ⌘1) but fire for
// 1-9, so the trailing number cap renders the whole range.
function toDigitRangeKeys(keys: string[]): string[] {
const last = keys.at(-1)
if (last === undefined || !/^[1-9]$/.test(last)) {
return keys
}
return [...keys.slice(0, -1), `${last}–9`]
}
export function ShortcutBindingRow({
item,
groupTitle,
@@ -173,6 +184,8 @@ export function ShortcutBindingRow({
? `Change shortcut for ${item.title}`
: `Add shortcut for ${item.title}`
const isDigitIndex = isDigitIndexActionId(item.id)
return (
<SearchableSetting
title={item.title}
@@ -315,13 +328,16 @@ export function ShortcutBindingRow({
</span>
) : hasBinding ? (
<span className="flex flex-wrap items-center justify-end gap-1.5">
{effective.map((binding) => (
<ShortcutKeyCombo
key={binding}
keys={formatKeybinding(binding, platform)}
doubleTap={isDoubleTapBinding(binding)}
/>
))}
{effective.map((binding) => {
const keys = formatKeybinding(binding, platform)
return (
<ShortcutKeyCombo
key={binding}
keys={isDigitIndex ? toDigitRangeKeys(keys) : keys}
doubleTap={isDoubleTapBinding(binding)}
/>
)
})}
</span>
) : (
<span className="flex items-center gap-1">
+193
View File
@@ -9,12 +9,15 @@ import {
formatKeybinding,
formatKeybindingList,
getEffectiveKeybindingsForAction,
isDigitIndexActionId,
isDoubleTapBinding,
keybindingFromInput,
keybindingFromInputForAction,
keybindingMatchesAction,
keybindingMatchesInput,
matchKeybindingDigitIndex,
normalizeKeybinding,
normalizeKeybindingArrayForAction,
normalizeKeybindingListForAction,
normalizeKeybindingList
} from './keybindings'
@@ -1008,3 +1011,193 @@ describe('keybindings', () => {
).toEqual([])
})
})
describe('digit-index shortcuts', () => {
const digitInput = (
digit: string,
modifiers: { meta?: boolean; control?: boolean; alt?: boolean; shift?: boolean }
): Parameters<typeof matchKeybindingDigitIndex>[1] => ({
key: digit,
code: `Digit${digit}`,
meta: Boolean(modifiers.meta),
control: Boolean(modifiers.control),
alt: Boolean(modifiers.alt),
shift: Boolean(modifiers.shift)
})
it('flags the two ranged actions as digit-index rows', () => {
expect(isDigitIndexActionId('tab.selectByIndex')).toBe(true)
expect(isDigitIndexActionId('workspace.selectByIndex')).toBe(true)
expect(isDigitIndexActionId('tab.newTerminal')).toBe(false)
})
it('resolves the default ranges per platform', () => {
// macOS: workspace = Cmd+1-9, tab = Ctrl+1-9.
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('3', { meta: true }),
'darwin'
)
).toBe(2)
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('3', { meta: true }), 'darwin')
).toBeNull()
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('3', { control: true }), 'darwin')
).toBe(2)
// Windows/Linux: workspace = Ctrl+1-9, tab = Alt+1-9.
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('4', { control: true }),
'linux'
)
).toBe(3)
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('4', { alt: true }), 'linux')
).toBe(3)
})
it('ignores non-range presses and extra modifiers', () => {
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('3', { meta: true, shift: true }),
'darwin'
)
).toBeNull()
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
{ key: 'p', code: 'KeyP', meta: false, control: true, alt: false, shift: false },
'darwin'
)
).toBeNull()
})
it('honors custom bindings, including swapping tab and workspace modifiers', () => {
const swapped = {
'tab.selectByIndex': ['Mod+1'],
'workspace.selectByIndex': ['Ctrl+1']
}
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
digitInput('5', { meta: true }),
'darwin',
swapped
)
).toBe(4)
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('5', { control: true }),
'darwin',
swapped
)
).toBe(4)
// A disabled (empty) override never fires.
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('5', { control: true }), 'darwin', {
'tab.selectByIndex': []
})
).toBeNull()
})
it('respects the terminal-first context gate', () => {
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
digitInput('2', { control: true }),
'darwin',
undefined,
{
context: 'terminal',
terminalShortcutPolicy: 'terminal-first'
}
)
).toBeNull()
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
digitInput('2', { control: true }),
'darwin',
undefined,
{
context: 'terminal',
terminalShortcutPolicy: 'orca-first'
}
)
).toBe(1)
})
it('canonicalizes a captured chord to the digit-1 representative', () => {
expect(
keybindingFromInputForAction(
'workspace.selectByIndex',
digitInput('7', { meta: true }),
'darwin'
)
).toEqual({ ok: true, value: 'Mod+1' })
expect(
keybindingFromInputForAction(
'tab.selectByIndex',
digitInput('9', { control: true }),
'darwin'
)
).toEqual({ ok: true, value: 'Ctrl+1' })
// A non-number chord is rejected with guidance.
expect(
keybindingFromInputForAction(
'tab.selectByIndex',
{ key: 'p', code: 'KeyP', meta: true, control: false, alt: false, shift: false },
'darwin'
)
).toMatchObject({ ok: false })
})
it('allows extra modifiers (e.g. Shift) on a digit-index chord', () => {
expect(
keybindingFromInputForAction(
'tab.selectByIndex',
digitInput('5', { control: true, shift: true }),
'darwin'
)
).toEqual({ ok: true, value: 'Ctrl+Shift+1' })
expect(normalizeKeybindingListForAction('workspace.selectByIndex', 'Mod+Shift+5')).toEqual([
'Mod+Shift+1'
])
})
it('matches via the physical-code fallback when the key value is unavailable', () => {
// macOS/IME edge cases can leave key empty while code carries the digit.
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
{ key: '', code: 'Digit5', meta: false, control: true, alt: false, shift: false },
'darwin'
)
).toBe(4)
})
it('canonicalizes stored bindings and rejects non-number chords', () => {
expect(normalizeKeybindingListForAction('workspace.selectByIndex', 'Mod+5')).toEqual(['Mod+1'])
expect(normalizeKeybindingArrayForAction('tab.selectByIndex', ['Ctrl+9'])).toEqual(['Ctrl+1'])
expect(normalizeKeybindingListForAction('tab.selectByIndex', 'Mod+P')).toMatchObject({
ok: false
})
})
it('lets the two ranges swap modifiers without a false conflict', () => {
// The headline use case: tab → Cmd, workspace → Ctrl. They live in
// different scopes, so neither edit is blocked as a conflict.
expect(
findKeybindingConflicts('darwin', {
'tab.selectByIndex': ['Mod+1'],
'workspace.selectByIndex': ['Ctrl+1']
})
).toEqual([])
})
})
+169 -3
View File
@@ -38,6 +38,7 @@ export type KeybindingActionId =
| 'workspace.rename'
| 'workspace.delete'
| 'workspace.openBoard'
| 'workspace.selectByIndex'
| 'voice.dictation'
| 'view.tasks'
| 'sidebar.left.toggle'
@@ -72,6 +73,7 @@ export type KeybindingActionId =
| 'tab.previousRecent'
| 'tab.nextTerminal'
| 'tab.previousTerminal'
| 'tab.selectByIndex'
| 'browser.find'
| 'browser.back'
| 'browser.forward'
@@ -291,6 +293,28 @@ export const KEYBINDING_DEFINITIONS: readonly KeybindingDefinition[] = [
defaultBindings: platformBindings([]),
allowInTerminal: true
},
{
id: 'workspace.selectByIndex',
title: 'Select Workspace 1–9',
group: 'Global',
scope: 'global',
searchKeywords: [
'shortcut',
'global',
'workspace',
'worktree',
'select',
'switch',
'number',
'digit',
'1-9',
'index'
],
// Why: one remappable row for the whole 1-9 range. The stored chord is a
// representative — its digit normalizes to 1, but the modifier set is what
// matters and any of 1-9 fires it. mac Cmd+1-9, Windows/Linux Ctrl+1-9 → Mod+1.
defaultBindings: platformBindings(['Mod+1'])
},
{
id: 'voice.dictation',
title: 'Dictation',
@@ -584,6 +608,25 @@ export const KEYBINDING_DEFINITIONS: readonly KeybindingDefinition[] = [
defaultBindings: platformBindings(['Ctrl+PageUp']),
allowInTerminal: true
},
{
id: 'tab.selectByIndex',
title: 'Select Tab 1–9',
group: 'Tab Navigation',
scope: 'tabs',
// Why: deliberately no shared conflictGroup with workspace.selectByIndex.
// They live in different scopes, so swapping their modifiers (the headline
// use case) is never blocked as a false conflict; runtime stays deterministic
// because resolveWindowShortcutAction checks the workspace range first.
searchKeywords: ['shortcut', 'tab', 'select', 'switch', 'number', 'digit', '1-9', 'index'],
// Why: representative chord for the 1-9 range (see workspace.selectByIndex).
// mac Ctrl+1-9 (Cmd+1-9 is the workspace jump); Windows/Linux Alt+1-9
// (Ctrl+1-9 is the workspace jump), so each platform gets a free chord.
defaultBindings: {
darwin: ['Ctrl+1'],
linux: ['Alt+1'],
win32: ['Alt+1']
}
},
{
id: 'browser.find',
title: 'Find in Browser',
@@ -878,6 +921,23 @@ const DEFINITION_IDS = new Set<KeybindingActionId>(
KEYBINDING_DEFINITIONS.map((definition) => definition.id)
)
// Why: "Select Tab 1-9" / "Select Workspace 1-9" are single remappable rows
// whose chord is a representative — the digit is canonicalized to 1, but the
// binding fires for any of 1-9. These ids opt into that range behavior.
export const DIGIT_INDEX_ACTION_IDS: readonly KeybindingActionId[] = [
'tab.selectByIndex',
'workspace.selectByIndex'
]
const DIGIT_INDEX_ACTION_ID_SET = new Set<KeybindingActionId>(DIGIT_INDEX_ACTION_IDS)
// The representative key for a digit-index chord is a single 1-9 number key.
const DIGIT_INDEX_KEY_PATTERN = /^[1-9]$/
export function isDigitIndexActionId(actionId: KeybindingActionId): boolean {
return DIGIT_INDEX_ACTION_ID_SET.has(actionId)
}
function platformBindings(bindings: readonly string[]): PlatformBindings {
return {
darwin: bindings,
@@ -1229,18 +1289,60 @@ function normalizeOptionsForAction(actionId: KeybindingActionId): NormalizeKeybi
}
}
// Why: a digit-index row stores one representative chord. Rewrite the key to 1
// so display and conflict detection stay stable across the 1-9 range, and
// reject anything that is not a number key 1-9. Extra modifiers (e.g. Shift) are
// intentionally allowed — only the key must be a digit; parseKeybinding has
// already enforced that at least one modifier is present.
function canonicalizeDigitIndexBinding(binding: string): KeybindingValidationResult {
const parsed = parseKeybinding(binding)
if (!parsed || parsed.doubleTapModifier || !DIGIT_INDEX_KEY_PATTERN.test(parsed.key)) {
return {
ok: false,
error: 'Pick a number key 1–9 with a modifier, like Cmd+1 or Ctrl+1.'
}
}
return { ok: true, value: canonicalizeParsedKeybinding({ ...parsed, key: '1' }) }
}
function finalizeDigitIndexBindings(
actionId: KeybindingActionId,
result: KeybindingValidationResult | string[]
): KeybindingValidationResult | string[] {
if (!isDigitIndexActionId(actionId) || !Array.isArray(result)) {
return result
}
const canonical: string[] = []
for (const binding of result) {
const normalized = canonicalizeDigitIndexBinding(binding)
if (!normalized.ok) {
return normalized
}
if (!canonical.includes(normalized.value)) {
canonical.push(normalized.value)
}
}
return canonical
}
export function normalizeKeybindingListForAction(
actionId: KeybindingActionId,
input: string
): KeybindingValidationResult | string[] {
return normalizeKeybindingListWithOptions(input, normalizeOptionsForAction(actionId))
return finalizeDigitIndexBindings(
actionId,
normalizeKeybindingListWithOptions(input, normalizeOptionsForAction(actionId))
)
}
export function normalizeKeybindingArrayForAction(
actionId: KeybindingActionId,
input: readonly string[]
): KeybindingValidationResult | string[] {
return normalizeKeybindingArrayWithOptions(input, normalizeOptionsForAction(actionId))
return finalizeDigitIndexBindings(
actionId,
normalizeKeybindingArrayWithOptions(input, normalizeOptionsForAction(actionId))
)
}
const MODIFIER_KEYS = new Set([
@@ -1432,7 +1534,15 @@ export function keybindingFromInputForAction(
input: KeybindingInput,
platform: NodeJS.Platform
): KeybindingValidationResult {
return keybindingFromInputWithOptions(input, platform, normalizeOptionsForAction(actionId))
const result = keybindingFromInputWithOptions(
input,
platform,
normalizeOptionsForAction(actionId)
)
if (!result.ok || !isDigitIndexActionId(actionId)) {
return result
}
return canonicalizeDigitIndexBinding(result.value)
}
function getDefaultBindings(definition: KeybindingDefinition, platform: NodeJS.Platform): string[] {
@@ -1455,6 +1565,19 @@ export function getEffectiveKeybindingsForAction(
}
const override = overrides?.[actionId]
if (Array.isArray(override)) {
// Why: digit-index overrides resolve to their canonical <mods>+1 representative
// (deduped) so effective bindings stay consistent for display and conflict
// detection even if a hand-edited file stored a different digit.
if (isDigitIndexActionId(actionId)) {
const canonical: string[] = []
for (const binding of override) {
const normalized = canonicalizeDigitIndexBinding(binding)
if (normalized.ok && !canonical.includes(normalized.value)) {
canonical.push(normalized.value)
}
}
return canonical
}
return override.flatMap((binding) => {
const normalized = normalizeKeybindingWithOptions(
binding,
@@ -1731,6 +1854,49 @@ export function keybindingMatchesAction(
)
}
function digitFromInput(input: KeybindingInput): string | null {
for (let value = 1; value <= 9; value++) {
const digit = String(value)
if (digitKeyMatches(input, digit)) {
return digit
}
}
return null
}
// Why: digit-index rows bind a representative chord but fire for 1-9. Reuse the
// representative's modifier set with the pressed digit, then match it through the
// normal input matcher so Mod/Cmd resolution and layout fallbacks stay shared.
// Honors keybindingIsActiveInContext, so terminal-first focus disables the range
// just like the scope-based gating for every other shortcut.
export function matchKeybindingDigitIndex(
actionId: KeybindingActionId,
input: KeybindingInput,
platform: NodeJS.Platform,
overrides?: KeybindingOverrides,
options: KeybindingMatchOptions = {}
): number | null {
const definition = DEFINITIONS_BY_ID.get(actionId)
if (!definition || !keybindingIsActiveInContext(definition, options)) {
return null
}
const digit = digitFromInput(input)
if (!digit) {
return null
}
for (const binding of getEffectiveKeybindingsForAction(actionId, platform, overrides)) {
const parsed = parseKeybinding(binding)
if (!parsed || parsed.doubleTapModifier || !DIGIT_INDEX_KEY_PATTERN.test(parsed.key)) {
continue
}
const candidate = canonicalizeParsedKeybinding({ ...parsed, key: digit })
if (keybindingMatchesInput(candidate, input, platform)) {
return Number(digit) - 1
}
}
return null
}
function formatModifierGlyph(modifier: ModifierToken, isMac: boolean): string {
switch (modifier) {
case 'Mod':
+60
View File
@@ -104,6 +104,66 @@ describe('resolveWindowShortcutAction', () => {
).toBeNull()
})
it('honors remapped tab/workspace number ranges, including swapping the modifiers', () => {
// Swap on macOS: tab now uses Cmd+1-9, workspace uses Ctrl+1-9.
const swapped: KeybindingOverrides = {
'tab.selectByIndex': ['Mod+1'],
'workspace.selectByIndex': ['Ctrl+1']
}
expect(
resolveWindowShortcutAction(
{ code: 'Digit3', key: '3', meta: true, control: false, alt: false, shift: false },
'darwin',
swapped
)
).toEqual({ type: 'jumpToTabIndex', index: 2 })
expect(
resolveWindowShortcutAction(
{ code: 'Digit3', key: '3', meta: false, control: true, alt: false, shift: false },
'darwin',
swapped
)
).toEqual({ type: 'jumpToWorktreeIndex', index: 2 })
// A custom chord with an extra modifier also resolves.
expect(
resolveWindowShortcutAction(
{ code: 'Digit2', key: '2', meta: false, control: true, alt: false, shift: true },
'linux',
{ 'tab.selectByIndex': ['Mod+Shift+1'] }
)
).toEqual({ type: 'jumpToTabIndex', index: 1 })
// Disabling the range leaves the chord unclaimed.
expect(
resolveWindowShortcutAction(
{ code: 'Digit3', key: '3', meta: false, control: true, alt: false, shift: false },
'linux',
{ 'workspace.selectByIndex': [] }
)
).toBeNull()
// Both ranges disabled: neither the workspace nor the tab digit chord resolves.
const bothDisabled: KeybindingOverrides = {
'workspace.selectByIndex': [],
'tab.selectByIndex': []
}
expect(
resolveWindowShortcutAction(
{ code: 'Digit3', key: '3', meta: true, control: false, alt: false, shift: false },
'darwin',
bothDisabled
)
).toBeNull()
expect(
resolveWindowShortcutAction(
{ code: 'Digit3', key: '3', meta: false, control: true, alt: false, shift: false },
'darwin',
bothDisabled
)
).toBeNull()
})
it('keeps Orca-first active in terminal context but lets Terminal-first pass risky app chords', () => {
const macWorktreePalette = {
code: 'KeyJ',
+25 -46
View File
@@ -3,7 +3,7 @@ import {
isKeybindingAllowedInTerminal,
isKeybindingPotentialTerminalConflict,
keybindingMatchesAction,
normalizeTerminalShortcutPolicy,
matchKeybindingDigitIndex,
type KeybindingActionId,
type KeybindingMatchOptions,
type KeybindingOverrides,
@@ -132,28 +132,6 @@ function actionMatches(
return keybindingMatchesAction(actionId, input, platform, keybindings, options)
}
function implicitWorktreeIndexShortcutAllowed(options: WindowShortcutResolveOptions): boolean {
if (options.context !== 'terminal') {
return true
}
return normalizeTerminalShortcutPolicy(options.terminalShortcutPolicy) === 'orca-first'
}
function implicitTabIndexShortcutAllowed(options: WindowShortcutResolveOptions): boolean {
return implicitWorktreeIndexShortcutAllowed(options)
}
function tabIndexModifierPressed(input: WindowShortcutInput, platform: NodeJS.Platform): boolean {
const meta = Boolean(input.meta ?? input.metaKey)
const control = Boolean(input.control ?? input.ctrlKey)
const alt = Boolean(input.alt ?? input.altKey)
// Why: Ctrl+1-9 is free on macOS because workspace jumps use Cmd+1-9.
// On Windows/Linux Ctrl+1-9 is already the workspace jump, so Alt+1-9
// gives tab indexing a non-conflicting hardcoded chord.
return platform === 'darwin' ? control && !meta && !alt : alt && !meta && !control
}
export function resolveWindowShortcutAction(
input: WindowShortcutInput,
platform: NodeJS.Platform,
@@ -244,27 +222,30 @@ export function resolveWindowShortcutAction(
return { type: 'switchRecentTab' }
}
if (
implicitWorktreeIndexShortcutAllowed(options) &&
platformPrimaryModifier(input, platform) &&
!input.alt &&
!input.shift &&
input.key &&
input.key >= '1' &&
input.key <= '9'
) {
return { type: 'jumpToWorktreeIndex', index: parseInt(input.key, 10) - 1 }
// Why: the two ranges live in different scopes (no shared conflictGroup), so a
// user is free to map both onto the same modifier without it being blocked as a
// conflict. Checking workspace first gives that overlap deterministic
// precedence — workspace wins — matching the historical Cmd-before-Ctrl order.
const worktreeIndex = matchKeybindingDigitIndex(
'workspace.selectByIndex',
input,
platform,
keybindings,
options
)
if (worktreeIndex !== null) {
return { type: 'jumpToWorktreeIndex', index: worktreeIndex }
}
if (
implicitTabIndexShortcutAllowed(options) &&
tabIndexModifierPressed(input, platform) &&
!input.shift &&
input.key &&
input.key >= '1' &&
input.key <= '9'
) {
return { type: 'jumpToTabIndex', index: parseInt(input.key, 10) - 1 }
const tabIndex = matchKeybindingDigitIndex(
'tab.selectByIndex',
input,
platform,
keybindings,
options
)
if (tabIndex !== null) {
return { type: 'jumpToTabIndex', index: tabIndex }
}
// Why: this helper is the explicit allowlist for main-process interception.
@@ -311,15 +292,13 @@ export function getWindowShortcutActionId(action: WindowShortcutAction): Keybind
case 'dictationKeyDown':
return 'voice.dictation'
case 'jumpToWorktreeIndex':
return 'workspace.selectByIndex'
case 'jumpToTabIndex':
return null
return 'tab.selectByIndex'
}
}
export function windowShortcutActionCapturesTerminal(action: WindowShortcutAction): boolean {
if (action.type === 'jumpToWorktreeIndex' || action.type === 'jumpToTabIndex') {
return true
}
const actionId = getWindowShortcutActionId(action)
if (!actionId) {
return false