feat(shortcuts): double-tap modifier keybindings (#5516)
* docs: design for double-tap modifier keybindings * feat(keybindings): parse and normalize DoubleTap+<Mod> grammar * test(keybindings): cover DoubleTap+Ctrl positive case * feat(keybindings): match DoubleTap bindings against synthetic input * test(keybindings): cover Ctrl-on-darwin double-tap miss; document input invariant * feat(keybindings): format DoubleTap bindings as a doubled glyph * test(keybindings): cover DoubleTap+Ctrl glyph on mac; note title label semantics * feat(keybindings): add pure ModifierDoubleTapDetector state machine * test(keybindings): cover missed-keyup double-tap edge; clarify keyUp guard * feat(keybindings): capture double-tap gestures into DoubleTap+<Mod> * test(keybindings): cover Ctrl/linux and Cmd/linux double-tap canonicalization * feat(keybindings): resolve double-tap input in window shortcut policy * feat(shortcuts): detect double-tap modifiers in main before-input-event * docs(shortcuts): note dictation guards live in the renderer * feat(shortcuts): dispatch double-tap modifiers in the renderer window handler * fix(shortcuts): clear armed double-tap state on dangling modifier keyup * feat(settings): record double-tap modifier shortcuts in the editor * i18n(shortcuts): localize ShortcutKeyCombo double-tap tooltip * Fix double-tap shortcut review gaps Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Devin345458 <devin@appleidimagination.com> Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com> Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
co-authored by
Orca
Devin345458
Jinwoo-H
parent
0eec0ad74a
commit
1368ba84b8
@@ -6,8 +6,10 @@ import {
|
||||
agentTabActionId,
|
||||
getKeybindingDefinition,
|
||||
findKeybindingConflicts,
|
||||
formatKeybinding,
|
||||
formatKeybindingList,
|
||||
getEffectiveKeybindingsForAction,
|
||||
isDoubleTapBinding,
|
||||
keybindingFromInput,
|
||||
keybindingFromInputForAction,
|
||||
keybindingMatchesAction,
|
||||
@@ -36,6 +38,34 @@ describe('keybindings', () => {
|
||||
expect(normalizeKeybinding('Ctrl+Nope')).toMatchObject({ ok: false })
|
||||
})
|
||||
|
||||
it('parses, normalizes, and rejects double-tap modifier bindings', () => {
|
||||
expect(normalizeKeybinding('DoubleTap+Shift')).toEqual({ ok: true, value: 'DoubleTap+Shift' })
|
||||
expect(normalizeKeybinding(' doubletap + shift ')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Shift'
|
||||
})
|
||||
expect(normalizeKeybinding('DoubleTap+Mod')).toEqual({ ok: true, value: 'DoubleTap+Mod' })
|
||||
expect(normalizeKeybinding('DoubleTap+Cmd')).toEqual({ ok: true, value: 'DoubleTap+Cmd' })
|
||||
expect(normalizeKeybinding('DoubleTap+Alt')).toEqual({ ok: true, value: 'DoubleTap+Alt' })
|
||||
expect(normalizeKeybinding('DoubleTap+Ctrl')).toEqual({ ok: true, value: 'DoubleTap+Ctrl' })
|
||||
|
||||
// A key after DoubleTap is invalid.
|
||||
expect(normalizeKeybinding('DoubleTap+Shift+P')).toMatchObject({ ok: false })
|
||||
// Two modifiers is invalid.
|
||||
expect(normalizeKeybinding('DoubleTap+Shift+Alt')).toMatchObject({ ok: false })
|
||||
// Mod + platform-specific reuses the shared error.
|
||||
expect(normalizeKeybinding('DoubleTap+Mod+Cmd')).toEqual({
|
||||
ok: false,
|
||||
error: 'Use either Mod or a platform-specific modifier, not both.'
|
||||
})
|
||||
// Bare DoubleTap is invalid.
|
||||
expect(normalizeKeybinding('DoubleTap')).toMatchObject({ ok: false })
|
||||
|
||||
expect(isDoubleTapBinding('DoubleTap+Shift')).toBe(true)
|
||||
expect(isDoubleTapBinding('Mod+P')).toBe(false)
|
||||
expect(isDoubleTapBinding('not-a-binding')).toBe(false)
|
||||
})
|
||||
|
||||
it('allows safe bare keys only for scoped actions that opt in', () => {
|
||||
expect(normalizeKeybinding('Delete')).toMatchObject({ ok: false })
|
||||
expect(normalizeKeybindingListForAction('fileExplorer.delete', 'Delete')).toEqual(['Delete'])
|
||||
@@ -813,6 +843,95 @@ describe('keybindings', () => {
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('matches double-tap bindings only against synthetic double-tap input', () => {
|
||||
expect(
|
||||
keybindingMatchesInput('DoubleTap+Shift', { doubleTapModifier: 'Shift' }, 'darwin')
|
||||
).toBe(true)
|
||||
// Mod resolves per platform: meta on macOS, control elsewhere.
|
||||
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Cmd' }, 'darwin')).toBe(
|
||||
true
|
||||
)
|
||||
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Ctrl' }, 'win32')).toBe(
|
||||
true
|
||||
)
|
||||
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Cmd' }, 'win32')).toBe(
|
||||
false
|
||||
)
|
||||
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Ctrl' }, 'darwin')).toBe(
|
||||
false
|
||||
)
|
||||
expect(keybindingMatchesInput('DoubleTap+Shift', { doubleTapModifier: 'Alt' }, 'darwin')).toBe(
|
||||
false
|
||||
)
|
||||
|
||||
// Cross-type negatives: a double-tap binding never matches a normal keydown,
|
||||
// and a normal binding never matches a synthetic double-tap input.
|
||||
expect(
|
||||
keybindingMatchesInput('DoubleTap+Shift', { key: 'A', code: 'KeyA', shift: true }, 'darwin')
|
||||
).toBe(false)
|
||||
expect(keybindingMatchesInput('Mod+P', { doubleTapModifier: 'Cmd' }, 'darwin')).toBe(false)
|
||||
|
||||
// Action-level matching works through user overrides, for free.
|
||||
expect(
|
||||
keybindingMatchesAction('worktree.quickOpen', { doubleTapModifier: 'Shift' }, 'darwin', {
|
||||
'worktree.quickOpen': ['DoubleTap+Shift']
|
||||
})
|
||||
).toBe(true)
|
||||
expect(
|
||||
keybindingMatchesAction('worktree.quickOpen', { doubleTapModifier: 'Alt' }, 'darwin', {
|
||||
'worktree.quickOpen': ['DoubleTap+Shift']
|
||||
})
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it('captures double-tap gestures into canonical bindings', () => {
|
||||
expect(keybindingFromInput({ doubleTapModifier: 'Shift' }, 'darwin')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Shift'
|
||||
})
|
||||
// The platform primary modifier canonicalizes to Mod, matching normal capture.
|
||||
expect(keybindingFromInput({ doubleTapModifier: 'Cmd' }, 'darwin')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Mod'
|
||||
})
|
||||
expect(keybindingFromInput({ doubleTapModifier: 'Ctrl' }, 'win32')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Mod'
|
||||
})
|
||||
// A non-primary modifier keeps its explicit token.
|
||||
expect(keybindingFromInput({ doubleTapModifier: 'Ctrl' }, 'darwin')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Ctrl'
|
||||
})
|
||||
expect(keybindingFromInput({ doubleTapModifier: 'Alt' }, 'linux')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Alt'
|
||||
})
|
||||
// Ctrl is the primary modifier on Linux too, so it canonicalizes to Mod.
|
||||
expect(keybindingFromInput({ doubleTapModifier: 'Ctrl' }, 'linux')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Mod'
|
||||
})
|
||||
// Cmd is not the primary modifier off-mac, so it stays explicit.
|
||||
expect(keybindingFromInput({ doubleTapModifier: 'Cmd' }, 'linux')).toEqual({
|
||||
ok: true,
|
||||
value: 'DoubleTap+Cmd'
|
||||
})
|
||||
})
|
||||
|
||||
it('formats double-tap bindings as the modifier glyph twice', () => {
|
||||
expect(formatKeybinding('DoubleTap+Shift', 'darwin')).toEqual(['⇧', '⇧'])
|
||||
expect(formatKeybinding('DoubleTap+Shift', 'linux')).toEqual(['Shift', 'Shift'])
|
||||
expect(formatKeybinding('DoubleTap+Mod', 'darwin')).toEqual(['⌘', '⌘'])
|
||||
expect(formatKeybinding('DoubleTap+Mod', 'win32')).toEqual(['Ctrl', 'Ctrl'])
|
||||
expect(formatKeybinding('DoubleTap+Cmd', 'win32')).toEqual(['Cmd', 'Cmd'])
|
||||
expect(formatKeybinding('DoubleTap+Alt', 'darwin')).toEqual(['⌥', '⌥'])
|
||||
// Ctrl's glyph ⌃ diverges from Mod's ⌘ on Mac, so cover it explicitly.
|
||||
expect(formatKeybinding('DoubleTap+Ctrl', 'darwin')).toEqual(['⌃', '⌃'])
|
||||
expect(formatKeybindingList(['DoubleTap+Shift'], 'darwin')).toBe('⇧ ⇧')
|
||||
expect(formatKeybindingList(['DoubleTap+Shift'], 'linux')).toBe('Shift Shift')
|
||||
})
|
||||
|
||||
it('matches macOS Option-composed bracket shortcuts for all-type tab switching', () => {
|
||||
const macOptionLeftBracket = {
|
||||
key: '\u201c',
|
||||
@@ -840,4 +959,52 @@ describe('keybindings', () => {
|
||||
false
|
||||
)
|
||||
})
|
||||
|
||||
it('reports conflicts across two double-tap bindings', () => {
|
||||
// Both actions share the same DoubleTap+Shift binding via overrides, so both
|
||||
// are in customizedActions and the conflict detector must flag them.
|
||||
const conflicts = findKeybindingConflicts('darwin', {
|
||||
'worktree.quickOpen': ['DoubleTap+Shift'],
|
||||
'view.tasks': ['DoubleTap+Shift']
|
||||
})
|
||||
expect(conflicts).toContainEqual({
|
||||
binding: 'DoubleTap+Shift',
|
||||
actionIds: expect.arrayContaining(['worktree.quickOpen', 'view.tasks'])
|
||||
})
|
||||
})
|
||||
|
||||
it('reports conflicts across platform-primary double-tap aliases', () => {
|
||||
expect(
|
||||
findKeybindingConflicts('darwin', {
|
||||
'worktree.quickOpen': ['DoubleTap+Mod'],
|
||||
'view.tasks': ['DoubleTap+Cmd']
|
||||
})
|
||||
).toContainEqual({
|
||||
binding: 'DoubleTap+Mod',
|
||||
actionIds: expect.arrayContaining(['worktree.quickOpen', 'view.tasks'])
|
||||
})
|
||||
|
||||
expect(
|
||||
findKeybindingConflicts('linux', {
|
||||
'worktree.quickOpen': ['DoubleTap+Mod'],
|
||||
'view.tasks': ['DoubleTap+Ctrl']
|
||||
})
|
||||
).toContainEqual({
|
||||
binding: 'DoubleTap+Mod',
|
||||
actionIds: expect.arrayContaining(['worktree.quickOpen', 'view.tasks'])
|
||||
})
|
||||
})
|
||||
|
||||
it('does not report a conflict when one action lists double-tap aliases for itself', () => {
|
||||
expect(
|
||||
findKeybindingConflicts('darwin', {
|
||||
'worktree.quickOpen': ['DoubleTap+Mod', 'DoubleTap+Cmd']
|
||||
})
|
||||
).toEqual([])
|
||||
expect(
|
||||
findKeybindingConflicts('linux', {
|
||||
'worktree.quickOpen': ['DoubleTap+Mod', 'DoubleTap+Ctrl']
|
||||
})
|
||||
).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
+201
-39
@@ -138,6 +138,9 @@ export type KeybindingDefinition = {
|
||||
conflictGroup?: string
|
||||
}
|
||||
|
||||
export type ModifierToken = 'Mod' | 'Cmd' | 'Ctrl' | 'Alt' | 'Shift'
|
||||
export type PhysicalModifierToken = Exclude<ModifierToken, 'Mod'>
|
||||
|
||||
export type KeybindingInput = {
|
||||
key?: string
|
||||
code?: string
|
||||
@@ -149,6 +152,8 @@ export type KeybindingInput = {
|
||||
metaKey?: boolean
|
||||
ctrlKey?: boolean
|
||||
shiftKey?: boolean
|
||||
// Set only by the double-tap detector; always a physical token (never 'Mod').
|
||||
doubleTapModifier?: PhysicalModifierToken
|
||||
}
|
||||
|
||||
type ParsedKeybinding = {
|
||||
@@ -158,6 +163,7 @@ type ParsedKeybinding = {
|
||||
alt: boolean
|
||||
shift: boolean
|
||||
key: string
|
||||
doubleTapModifier?: ModifierToken
|
||||
}
|
||||
|
||||
type NormalizeKeybindingOptions = {
|
||||
@@ -982,6 +988,84 @@ function normalizeKeyToken(token: string): string | null {
|
||||
return simple[upper] ?? null
|
||||
}
|
||||
|
||||
function parseModifierToken(rawPart: string): ModifierToken | null {
|
||||
const part = rawPart.toLowerCase()
|
||||
if (part === 'mod' || part === 'cmdorctrl' || part === 'commandorcontrol') {
|
||||
return 'Mod'
|
||||
}
|
||||
if (part === 'cmd' || part === 'command' || part === 'meta' || rawPart === '⌘') {
|
||||
return 'Cmd'
|
||||
}
|
||||
if (part === 'ctrl' || part === 'control' || rawPart === '⌃') {
|
||||
return 'Ctrl'
|
||||
}
|
||||
if (part === 'alt' || part === 'option' || part === 'opt' || rawPart === '⌥') {
|
||||
return 'Alt'
|
||||
}
|
||||
if (part === 'shift' || rawPart === '⇧') {
|
||||
return 'Shift'
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function applyModifierToken(parsed: ParsedKeybinding, modifier: ModifierToken): void {
|
||||
if (modifier === 'Mod') {
|
||||
parsed.mod = true
|
||||
} else if (modifier === 'Cmd') {
|
||||
parsed.meta = true
|
||||
} else if (modifier === 'Ctrl') {
|
||||
parsed.control = true
|
||||
} else if (modifier === 'Alt') {
|
||||
parsed.alt = true
|
||||
} else {
|
||||
parsed.shift = true
|
||||
}
|
||||
}
|
||||
|
||||
function emptyParsedKeybinding(): ParsedKeybinding {
|
||||
return { mod: false, meta: false, control: false, alt: false, shift: false, key: '' }
|
||||
}
|
||||
|
||||
// Why: a double-tap is a bare modifier with no key, so it cannot reuse the
|
||||
// normal "one key required" parse path; validation of conflicting/extra
|
||||
// modifiers is deferred to normalizeKeybindingWithOptions for shared errors.
|
||||
function parseDoubleTapKeybinding(rawParts: string[]): ParsedKeybinding | null {
|
||||
const modifiers: ModifierToken[] = []
|
||||
let sawDoubleTap = false
|
||||
for (const rawPart of rawParts) {
|
||||
if (rawPart.toLowerCase() === 'doubletap') {
|
||||
if (sawDoubleTap) {
|
||||
return null
|
||||
}
|
||||
sawDoubleTap = true
|
||||
continue
|
||||
}
|
||||
const modifier = parseModifierToken(rawPart)
|
||||
if (!modifier) {
|
||||
return null
|
||||
}
|
||||
modifiers.push(modifier)
|
||||
}
|
||||
if (modifiers.length === 0) {
|
||||
return null
|
||||
}
|
||||
const parsed = emptyParsedKeybinding()
|
||||
for (const modifier of modifiers) {
|
||||
applyModifierToken(parsed, modifier)
|
||||
}
|
||||
// Mod combined with a platform-specific modifier: keep both flags so normalize
|
||||
// emits the shared "Mod or platform-specific, not both" error.
|
||||
if (parsed.mod && (parsed.meta || parsed.control)) {
|
||||
parsed.doubleTapModifier = 'Mod'
|
||||
return parsed
|
||||
}
|
||||
if (modifiers.length > 1) {
|
||||
return null
|
||||
}
|
||||
parsed.doubleTapModifier = modifiers[0]
|
||||
return parsed
|
||||
}
|
||||
|
||||
function parseKeybinding(binding: string): ParsedKeybinding | null {
|
||||
const rawParts = binding
|
||||
.split('+')
|
||||
@@ -991,35 +1075,15 @@ function parseKeybinding(binding: string): ParsedKeybinding | null {
|
||||
return null
|
||||
}
|
||||
|
||||
const parsed: ParsedKeybinding = {
|
||||
mod: false,
|
||||
meta: false,
|
||||
control: false,
|
||||
alt: false,
|
||||
shift: false,
|
||||
key: ''
|
||||
if (rawParts.some((part) => part.toLowerCase() === 'doubletap')) {
|
||||
return parseDoubleTapKeybinding(rawParts)
|
||||
}
|
||||
|
||||
const parsed = emptyParsedKeybinding()
|
||||
for (const rawPart of rawParts) {
|
||||
const part = rawPart.toLowerCase()
|
||||
if (part === 'mod' || part === 'cmdorctrl' || part === 'commandorcontrol') {
|
||||
parsed.mod = true
|
||||
continue
|
||||
}
|
||||
if (part === 'cmd' || part === 'command' || part === 'meta' || rawPart === '⌘') {
|
||||
parsed.meta = true
|
||||
continue
|
||||
}
|
||||
if (part === 'ctrl' || part === 'control' || rawPart === '⌃') {
|
||||
parsed.control = true
|
||||
continue
|
||||
}
|
||||
if (part === 'alt' || part === 'option' || part === 'opt' || rawPart === '⌥') {
|
||||
parsed.alt = true
|
||||
continue
|
||||
}
|
||||
if (part === 'shift' || rawPart === '⇧') {
|
||||
parsed.shift = true
|
||||
const modifier = parseModifierToken(rawPart)
|
||||
if (modifier) {
|
||||
applyModifierToken(parsed, modifier)
|
||||
continue
|
||||
}
|
||||
if (parsed.key) {
|
||||
@@ -1036,6 +1100,9 @@ function parseKeybinding(binding: string): ParsedKeybinding | null {
|
||||
}
|
||||
|
||||
function canonicalizeParsedKeybinding(parsed: ParsedKeybinding): string {
|
||||
if (parsed.doubleTapModifier) {
|
||||
return `DoubleTap+${parsed.doubleTapModifier}`
|
||||
}
|
||||
const parts: string[] = []
|
||||
if (parsed.mod) {
|
||||
parts.push('Mod')
|
||||
@@ -1086,6 +1153,9 @@ function normalizeKeybindingWithOptions(
|
||||
if (parsed.mod && (parsed.meta || parsed.control)) {
|
||||
return { ok: false, error: 'Use either Mod or a platform-specific modifier, not both.' }
|
||||
}
|
||||
if (parsed.doubleTapModifier) {
|
||||
return { ok: true, value: canonicalizeParsedKeybinding(parsed) }
|
||||
}
|
||||
const isShiftInsert = parsed.shift && parsed.key === 'Insert'
|
||||
const isBareAllowed = options.allowBareKeybindings === true && isSafeBareKey(parsed)
|
||||
if (
|
||||
@@ -1105,6 +1175,10 @@ export function normalizeKeybinding(binding: string): KeybindingValidationResult
|
||||
return normalizeKeybindingWithOptions(binding)
|
||||
}
|
||||
|
||||
export function isDoubleTapBinding(binding: string): boolean {
|
||||
return Boolean(parseKeybinding(binding)?.doubleTapModifier)
|
||||
}
|
||||
|
||||
function normalizeKeybindingListWithOptions(
|
||||
input: string,
|
||||
options: NormalizeKeybindingOptions = {}
|
||||
@@ -1291,11 +1365,33 @@ function keyTokenFromInput(input: KeybindingInput, platform: NodeJS.Platform): s
|
||||
return physicalCodeKeyTokenFromInput(input)
|
||||
}
|
||||
|
||||
// Why: the platform primary modifier canonicalizes to Mod, mirroring normal
|
||||
// capture where Cmd on macOS / Ctrl elsewhere both become Mod.
|
||||
function canonicalDoubleTapToken(
|
||||
modifier: PhysicalModifierToken,
|
||||
platform: NodeJS.Platform
|
||||
): ModifierToken {
|
||||
const isMac = platform === 'darwin'
|
||||
if (modifier === 'Cmd' && isMac) {
|
||||
return 'Mod'
|
||||
}
|
||||
if (modifier === 'Ctrl' && !isMac) {
|
||||
return 'Mod'
|
||||
}
|
||||
return modifier
|
||||
}
|
||||
|
||||
function keybindingFromInputWithOptions(
|
||||
input: KeybindingInput,
|
||||
platform: NodeJS.Platform,
|
||||
options: NormalizeKeybindingOptions = {}
|
||||
): KeybindingValidationResult {
|
||||
if (input.doubleTapModifier) {
|
||||
return normalizeKeybindingWithOptions(
|
||||
`DoubleTap+${canonicalDoubleTapToken(input.doubleTapModifier, platform)}`,
|
||||
options
|
||||
)
|
||||
}
|
||||
const key = keyTokenFromInput(input, platform)
|
||||
if (!key) {
|
||||
return { ok: false, error: 'Press a key, not only a modifier.' }
|
||||
@@ -1564,6 +1660,24 @@ function keyMatches(
|
||||
return canUsePhysicalCodeFallback(input) && physicalCodeKeyTokenFromInput(input) === parsedKey
|
||||
}
|
||||
|
||||
function resolveModifierToken(
|
||||
modifier: ModifierToken,
|
||||
platform: NodeJS.Platform
|
||||
): 'meta' | 'control' | 'alt' | 'shift' {
|
||||
switch (modifier) {
|
||||
case 'Mod':
|
||||
return platform === 'darwin' ? 'meta' : 'control'
|
||||
case 'Cmd':
|
||||
return 'meta'
|
||||
case 'Ctrl':
|
||||
return 'control'
|
||||
case 'Alt':
|
||||
return 'alt'
|
||||
case 'Shift':
|
||||
return 'shift'
|
||||
}
|
||||
}
|
||||
|
||||
export function keybindingMatchesInput(
|
||||
binding: string,
|
||||
input: KeybindingInput,
|
||||
@@ -1573,11 +1687,31 @@ export function keybindingMatchesInput(
|
||||
if (!parsed) {
|
||||
return false
|
||||
}
|
||||
// A double-tap binding matches only a synthetic double-tap input, resolved per
|
||||
// platform; a normal binding never matches a synthetic input, and vice-versa.
|
||||
if (parsed.doubleTapModifier) {
|
||||
return (
|
||||
input.doubleTapModifier !== undefined &&
|
||||
resolveModifierToken(parsed.doubleTapModifier, platform) ===
|
||||
resolveModifierToken(input.doubleTapModifier, platform)
|
||||
)
|
||||
}
|
||||
if (input.doubleTapModifier !== undefined) {
|
||||
return false
|
||||
}
|
||||
return (
|
||||
modifierStateMatches(parsed, input, platform) && keyMatches(parsed.key, input, parsed, platform)
|
||||
)
|
||||
}
|
||||
|
||||
function keybindingConflictIdentity(binding: string, platform: NodeJS.Platform): string {
|
||||
const parsed = parseKeybinding(binding)
|
||||
if (!parsed?.doubleTapModifier) {
|
||||
return binding
|
||||
}
|
||||
return `DoubleTap:${resolveModifierToken(parsed.doubleTapModifier, platform)}`
|
||||
}
|
||||
|
||||
export function keybindingMatchesAction(
|
||||
actionId: KeybindingActionId,
|
||||
input: KeybindingInput,
|
||||
@@ -1597,12 +1731,31 @@ export function keybindingMatchesAction(
|
||||
)
|
||||
}
|
||||
|
||||
function formatModifierGlyph(modifier: ModifierToken, isMac: boolean): string {
|
||||
switch (modifier) {
|
||||
case 'Mod':
|
||||
return isMac ? '⌘' : 'Ctrl'
|
||||
case 'Cmd':
|
||||
return isMac ? '⌘' : 'Cmd'
|
||||
case 'Ctrl':
|
||||
return isMac ? '⌃' : 'Ctrl'
|
||||
case 'Alt':
|
||||
return isMac ? '⌥' : 'Alt'
|
||||
case 'Shift':
|
||||
return isMac ? '⇧' : 'Shift'
|
||||
}
|
||||
}
|
||||
|
||||
export function formatKeybinding(binding: string, platform: NodeJS.Platform): string[] {
|
||||
const parsed = parseKeybinding(binding)
|
||||
if (!parsed) {
|
||||
return [binding]
|
||||
}
|
||||
const isMac = platform === 'darwin'
|
||||
if (parsed.doubleTapModifier) {
|
||||
const glyph = formatModifierGlyph(parsed.doubleTapModifier, isMac)
|
||||
return [glyph, glyph]
|
||||
}
|
||||
const parts: string[] = []
|
||||
if (parsed.mod) {
|
||||
parts.push(isMac ? '⌘' : 'Ctrl')
|
||||
@@ -1631,7 +1784,10 @@ export function formatKeybindingList(
|
||||
return 'Unassigned'
|
||||
}
|
||||
return bindings
|
||||
.map((binding) => formatKeybinding(binding, platform).join(platform === 'darwin' ? '' : '+'))
|
||||
.map((binding) => {
|
||||
const separator = isDoubleTapBinding(binding) ? ' ' : platform === 'darwin' ? '' : '+'
|
||||
return formatKeybinding(binding, platform).join(separator)
|
||||
})
|
||||
.join(', ')
|
||||
}
|
||||
|
||||
@@ -1674,7 +1830,7 @@ export function findKeybindingConflicts(
|
||||
overrides?: KeybindingOverrides,
|
||||
options: FindKeybindingConflictOptions = {}
|
||||
): KeybindingConflict[] {
|
||||
const owners = new Map<string, KeybindingActionId[]>()
|
||||
const owners = new Map<string, { binding: string; actionIds: Set<KeybindingActionId> }>()
|
||||
const ignoredActionIds = new Set(options.ignoredActionIds ?? [])
|
||||
const customizedActions = new Set(
|
||||
Object.keys(overrides ?? {}).filter(
|
||||
@@ -1694,21 +1850,27 @@ export function findKeybindingConflicts(
|
||||
groups.add(definition.scope)
|
||||
}
|
||||
for (const group of groups) {
|
||||
const conflictKey = `${group}\u0000${binding}`
|
||||
const current = owners.get(conflictKey) ?? []
|
||||
current.push(definition.id)
|
||||
const conflictKey = `${group}\u0000${keybindingConflictIdentity(binding, platform)}`
|
||||
const current = owners.get(conflictKey) ?? { binding, actionIds: new Set() }
|
||||
current.actionIds.add(definition.id)
|
||||
owners.set(conflictKey, current)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(owners.entries())
|
||||
.filter(
|
||||
([, actionIds]) =>
|
||||
actionIds.length > 1 && actionIds.some((actionId) => customizedActions.has(actionId))
|
||||
)
|
||||
.map(([conflictKey, actionIds]) => ({
|
||||
binding: conflictKey.slice(conflictKey.indexOf('\u0000') + 1),
|
||||
actionIds
|
||||
return Array.from(owners.values())
|
||||
.filter(({ actionIds }) => actionIds.size > 1 && setIntersects(actionIds, customizedActions))
|
||||
.map(({ binding, actionIds }) => ({
|
||||
binding,
|
||||
actionIds: Array.from(actionIds)
|
||||
}))
|
||||
}
|
||||
|
||||
function setIntersects<T>(left: ReadonlySet<T>, right: ReadonlySet<T>): boolean {
|
||||
for (const value of left) {
|
||||
if (right.has(value)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
ModifierDoubleTapDetector,
|
||||
modifierFromKeyEvent,
|
||||
toModifierDoubleTapEvent,
|
||||
type ModifierDoubleTapEvent
|
||||
} from './modifier-double-tap-detector'
|
||||
|
||||
function down(
|
||||
modifier: ModifierDoubleTapEvent['modifier'],
|
||||
overrides: Partial<ModifierDoubleTapEvent> = {}
|
||||
): ModifierDoubleTapEvent {
|
||||
return { type: 'keyDown', modifier, isModifierOnly: true, isAutoRepeat: false, ...overrides }
|
||||
}
|
||||
|
||||
function up(
|
||||
modifier: ModifierDoubleTapEvent['modifier'],
|
||||
overrides: Partial<ModifierDoubleTapEvent> = {}
|
||||
): ModifierDoubleTapEvent {
|
||||
return { type: 'keyUp', modifier, isModifierOnly: true, isAutoRepeat: false, ...overrides }
|
||||
}
|
||||
|
||||
const otherKey: ModifierDoubleTapEvent = {
|
||||
type: 'keyDown',
|
||||
modifier: null,
|
||||
isModifierOnly: false,
|
||||
isAutoRepeat: false
|
||||
}
|
||||
|
||||
describe('ModifierDoubleTapDetector', () => {
|
||||
it('emits when the second press lands inside the window', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
expect(d.process(down('Shift'), 0)).toBeNull()
|
||||
expect(d.process(up('Shift'), 10)).toBeNull()
|
||||
expect(d.process(down('Shift'), 200)).toEqual({ modifier: 'Shift' })
|
||||
})
|
||||
|
||||
it('does not emit when the second press is past the window', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
d.process(down('Shift'), 0)
|
||||
d.process(up('Shift'), 10)
|
||||
expect(d.process(down('Shift'), 400)).toBeNull()
|
||||
})
|
||||
|
||||
it('resets on an intervening non-modifier key', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
d.process(down('Shift'), 0)
|
||||
d.process(up('Shift'), 10)
|
||||
expect(d.process(otherKey, 20)).toBeNull()
|
||||
expect(d.process(down('Shift'), 100)).toBeNull()
|
||||
})
|
||||
|
||||
it('treats a different modifier as a fresh gesture, not a completion', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
d.process(down('Shift'), 0)
|
||||
d.process(up('Shift'), 10)
|
||||
// Wrong modifier: no emit, but it begins a new first tap.
|
||||
expect(d.process(down('Alt'), 100)).toBeNull()
|
||||
expect(d.process(up('Alt'), 110)).toBeNull()
|
||||
expect(d.process(down('Alt'), 150)).toEqual({ modifier: 'Alt' })
|
||||
})
|
||||
|
||||
it('does not treat an auto-repeat hold as a tap', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
d.process(down('Shift'), 0)
|
||||
// Holding the key emits auto-repeat keyDowns — this must cancel the gesture.
|
||||
expect(d.process(down('Shift', { isAutoRepeat: true }), 30)).toBeNull()
|
||||
d.process(up('Shift'), 500)
|
||||
expect(d.process(down('Shift'), 520)).toBeNull()
|
||||
})
|
||||
|
||||
it('does not emit when another modifier is held (isModifierOnly false)', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
expect(d.process(down('Shift', { isModifierOnly: false }), 0)).toBeNull()
|
||||
d.process(up('Shift'), 10)
|
||||
expect(d.process(down('Shift'), 100)).toBeNull()
|
||||
})
|
||||
|
||||
it('handles a second keyDown of the same modifier without an intervening keyUp', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
d.process(down('Shift'), 0)
|
||||
// Missed keyUp — a fresh (non-repeat) keyDown for the same modifier just
|
||||
// restarts the first tap rather than emitting.
|
||||
d.process(down('Shift'), 50)
|
||||
d.process(up('Shift'), 60)
|
||||
// The next press within the window still completes the gesture.
|
||||
expect(d.process(down('Shift'), 200)).toEqual({ modifier: 'Shift' })
|
||||
})
|
||||
|
||||
it('clears armed state when the second keydown was suppressed (allowlisted path)', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
d.process(down('Shift'), 0) // first tap down → down1
|
||||
d.process(up('Shift'), 10) // first tap up → armed
|
||||
// The main process suppressed the second keydown (an allowlisted action fired
|
||||
// there), but the second tap's keyup still reaches this detector.
|
||||
d.process(up('Shift'), 20)
|
||||
// A later lone Shift press (e.g. typing a capital) must NOT phantom-complete
|
||||
// a double-tap from the stale armed state.
|
||||
expect(d.process(down('Shift'), 200)).toBeNull()
|
||||
})
|
||||
|
||||
it('clears state on reset()', () => {
|
||||
const d = new ModifierDoubleTapDetector()
|
||||
d.process(down('Shift'), 0)
|
||||
d.process(up('Shift'), 10)
|
||||
d.reset()
|
||||
expect(d.process(down('Shift'), 100)).toBeNull()
|
||||
})
|
||||
|
||||
it('normalizes platform key events', () => {
|
||||
expect(modifierFromKeyEvent('ShiftLeft', 'Shift')).toBe('Shift')
|
||||
expect(modifierFromKeyEvent('MetaRight', 'Meta')).toBe('Cmd')
|
||||
expect(modifierFromKeyEvent('ControlLeft', 'Control')).toBe('Ctrl')
|
||||
expect(modifierFromKeyEvent('KeyA', 'a')).toBeNull()
|
||||
|
||||
expect(
|
||||
toModifierDoubleTapEvent({ type: 'keyDown', code: 'ShiftLeft', key: 'Shift', shift: true })
|
||||
).toEqual({ type: 'keyDown', modifier: 'Shift', isModifierOnly: true, isAutoRepeat: false })
|
||||
|
||||
// Another modifier held → not a bare modifier event.
|
||||
expect(
|
||||
toModifierDoubleTapEvent({
|
||||
type: 'keyDown',
|
||||
code: 'ShiftLeft',
|
||||
key: 'Shift',
|
||||
shift: true,
|
||||
meta: true
|
||||
})
|
||||
).toMatchObject({ modifier: 'Shift', isModifierOnly: false })
|
||||
|
||||
expect(
|
||||
toModifierDoubleTapEvent({ type: 'keyDown', code: 'KeyA', key: 'a' })
|
||||
).toMatchObject({ modifier: null, isModifierOnly: false })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,153 @@
|
||||
import type { PhysicalModifierToken } from './keybindings'
|
||||
|
||||
// Why: max gap between the first release and the second press. Internal — not
|
||||
// user-configurable — and tight enough that normal fast typing never triggers.
|
||||
const DOUBLE_TAP_WINDOW_MS = 300
|
||||
|
||||
export type ModifierDoubleTapEventType = 'keyDown' | 'keyUp'
|
||||
|
||||
// A keyboard event normalized to just what the detector needs.
|
||||
export type ModifierDoubleTapEvent = {
|
||||
type: ModifierDoubleTapEventType
|
||||
// Which physical modifier this event is about, or null for any other key.
|
||||
modifier: PhysicalModifierToken | null
|
||||
// True only for a bare modifier press/release with no OTHER modifier held.
|
||||
isModifierOnly: boolean
|
||||
isAutoRepeat: boolean
|
||||
}
|
||||
|
||||
export type DetectedDoubleTap = { modifier: PhysicalModifierToken }
|
||||
|
||||
export type ModifierKeyEventLike = {
|
||||
type: ModifierDoubleTapEventType
|
||||
code?: string
|
||||
key?: string
|
||||
shift?: boolean
|
||||
control?: boolean
|
||||
alt?: boolean
|
||||
meta?: boolean
|
||||
isAutoRepeat?: boolean
|
||||
}
|
||||
|
||||
const MODIFIER_BY_CODE: Record<string, PhysicalModifierToken> = {
|
||||
ShiftLeft: 'Shift',
|
||||
ShiftRight: 'Shift',
|
||||
ControlLeft: 'Ctrl',
|
||||
ControlRight: 'Ctrl',
|
||||
AltLeft: 'Alt',
|
||||
AltRight: 'Alt',
|
||||
MetaLeft: 'Cmd',
|
||||
MetaRight: 'Cmd'
|
||||
}
|
||||
|
||||
const MODIFIER_BY_KEY: Record<string, PhysicalModifierToken> = {
|
||||
Shift: 'Shift',
|
||||
Control: 'Ctrl',
|
||||
Alt: 'Alt',
|
||||
Meta: 'Cmd'
|
||||
}
|
||||
|
||||
// Maps a physical key event to the modifier it represents, or null for any
|
||||
// non-modifier key. Detector output is always a physical token (never 'Mod').
|
||||
export function modifierFromKeyEvent(
|
||||
code: string | undefined,
|
||||
key: string | undefined
|
||||
): PhysicalModifierToken | null {
|
||||
if (code && MODIFIER_BY_CODE[code]) {
|
||||
return MODIFIER_BY_CODE[code]
|
||||
}
|
||||
return key ? (MODIFIER_BY_KEY[key] ?? null) : null
|
||||
}
|
||||
|
||||
function otherModifierHeld(event: ModifierKeyEventLike, modifier: PhysicalModifierToken): boolean {
|
||||
if (modifier !== 'Shift' && event.shift) {
|
||||
return true
|
||||
}
|
||||
if (modifier !== 'Ctrl' && event.control) {
|
||||
return true
|
||||
}
|
||||
if (modifier !== 'Alt' && event.alt) {
|
||||
return true
|
||||
}
|
||||
if (modifier !== 'Cmd' && event.meta) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Normalizes a platform key event (DOM or Electron) into the detector input.
|
||||
export function toModifierDoubleTapEvent(event: ModifierKeyEventLike): ModifierDoubleTapEvent {
|
||||
const modifier = modifierFromKeyEvent(event.code, event.key)
|
||||
return {
|
||||
type: event.type,
|
||||
modifier,
|
||||
isModifierOnly: modifier !== null && !otherModifierHeld(event, modifier),
|
||||
isAutoRepeat: Boolean(event.isAutoRepeat)
|
||||
}
|
||||
}
|
||||
|
||||
type DetectorState =
|
||||
| { phase: 'idle' }
|
||||
| { phase: 'down1'; modifier: PhysicalModifierToken }
|
||||
| { phase: 'armed'; modifier: PhysicalModifierToken; deadlineMs: number }
|
||||
|
||||
export class ModifierDoubleTapDetector {
|
||||
private state: DetectorState = { phase: 'idle' }
|
||||
|
||||
process(event: ModifierDoubleTapEvent, timestampMs: number): DetectedDoubleTap | null {
|
||||
// A non-modifier key, or a modifier chorded with another, breaks the gesture.
|
||||
// (On keyUp, isModifierOnly:false means another modifier is still held — the
|
||||
// gesture was already reset at that modifier's keyDown.)
|
||||
if (event.modifier === null || !event.isModifierOnly) {
|
||||
this.state = { phase: 'idle' }
|
||||
return null
|
||||
}
|
||||
if (event.type === 'keyUp') {
|
||||
this.onModifierUp(event.modifier, timestampMs)
|
||||
return null
|
||||
}
|
||||
return this.onModifierDown(event.modifier, event.isAutoRepeat, timestampMs)
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this.state = { phase: 'idle' }
|
||||
}
|
||||
|
||||
private onModifierDown(
|
||||
modifier: PhysicalModifierToken,
|
||||
isAutoRepeat: boolean,
|
||||
timestampMs: number
|
||||
): DetectedDoubleTap | null {
|
||||
if (
|
||||
this.state.phase === 'armed' &&
|
||||
this.state.modifier === modifier &&
|
||||
!isAutoRepeat &&
|
||||
timestampMs <= this.state.deadlineMs
|
||||
) {
|
||||
this.state = { phase: 'idle' }
|
||||
return { modifier }
|
||||
}
|
||||
// Auto-repeat means the key is being held, not tapped.
|
||||
if (isAutoRepeat) {
|
||||
this.state = { phase: 'idle' }
|
||||
return null
|
||||
}
|
||||
// Any other fresh bare-modifier press (re)starts from the first tap.
|
||||
this.state = { phase: 'down1', modifier }
|
||||
return null
|
||||
}
|
||||
|
||||
private onModifierUp(modifier: PhysicalModifierToken, timestampMs: number): void {
|
||||
if (this.state.phase === 'down1' && this.state.modifier === modifier) {
|
||||
this.state = { phase: 'armed', modifier, deadlineMs: timestampMs + DOUBLE_TAP_WINDOW_MS }
|
||||
return
|
||||
}
|
||||
// Why: a keyup of the armed modifier with no intervening second keydown means
|
||||
// the second press was consumed elsewhere (the main process suppresses it for
|
||||
// an allowlisted action). Clear armed so a later lone press of the same
|
||||
// modifier can't phantom-complete a double-tap.
|
||||
if (this.state.phase === 'armed' && this.state.modifier === modifier) {
|
||||
this.state = { phase: 'idle' }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -763,4 +763,21 @@ describe('resolveWindowShortcutAction', () => {
|
||||
false
|
||||
)
|
||||
})
|
||||
|
||||
it('resolves an allowlisted action from a synthetic double-tap input', () => {
|
||||
// (a) A synthetic DoubleTap+Shift input resolves the overridden action.
|
||||
const overrides: KeybindingOverrides = { 'worktree.quickOpen': ['DoubleTap+Shift'] }
|
||||
expect(
|
||||
resolveWindowShortcutAction({ doubleTapModifier: 'Shift' }, 'darwin', overrides)
|
||||
).toEqual({ type: 'openQuickOpen' })
|
||||
|
||||
// (b) A different modifier does not resolve it.
|
||||
expect(
|
||||
resolveWindowShortcutAction({ doubleTapModifier: 'Alt' }, 'darwin', overrides)
|
||||
).toBeNull()
|
||||
|
||||
// (c) Implicit numeric shortcuts are guarded on input.key, which a double-tap
|
||||
// input never has, so they cannot accidentally match a double-tap event.
|
||||
expect(resolveWindowShortcutAction({ doubleTapModifier: 'Cmd' }, 'darwin')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,7 +6,8 @@ import {
|
||||
normalizeTerminalShortcutPolicy,
|
||||
type KeybindingActionId,
|
||||
type KeybindingMatchOptions,
|
||||
type KeybindingOverrides
|
||||
type KeybindingOverrides,
|
||||
type PhysicalModifierToken
|
||||
} from './keybindings'
|
||||
|
||||
export type WindowShortcutInput = {
|
||||
@@ -21,6 +22,9 @@ export type WindowShortcutInput = {
|
||||
metaKey?: boolean
|
||||
ctrlKey?: boolean
|
||||
shiftKey?: boolean
|
||||
// Set only by the double-tap detector; threads the synthetic input through
|
||||
// the main-process resolver so allowlisted actions can fire on double-tap.
|
||||
doubleTapModifier?: PhysicalModifierToken
|
||||
}
|
||||
|
||||
export type WindowShortcutAction =
|
||||
|
||||
Reference in New Issue
Block a user