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
@@ -414,6 +414,14 @@ describe('setupGuestShortcutForwarding', () => {
|
||||
return preventDefault
|
||||
}
|
||||
|
||||
function triggerGuestBlur(): void {
|
||||
const handler = guestOnMock.mock.calls.find((call) => call[0] === 'blur')?.[1] as
|
||||
| (() => void)
|
||||
| undefined
|
||||
expect(handler).toBeTypeOf('function')
|
||||
handler!()
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
rendererSendMock = vi.fn()
|
||||
guestOnMock = vi.fn()
|
||||
@@ -552,4 +560,85 @@ describe('setupGuestShortcutForwarding', () => {
|
||||
expect(customPreventDefault).toHaveBeenCalledTimes(1)
|
||||
expect(rendererSendMock).toHaveBeenCalledWith('ui:zoomBrowserPage', 'in')
|
||||
})
|
||||
|
||||
it('forwards double-tap window shortcuts from focused guest pages', () => {
|
||||
setupGuestShortcutForwarding({
|
||||
browserTabId,
|
||||
guest: makeGuest(),
|
||||
resolveRenderer: () => makeRenderer(),
|
||||
getKeybindings: () => ({
|
||||
'worktree.quickOpen': ['DoubleTap+Shift']
|
||||
})
|
||||
})
|
||||
|
||||
const modifierInput = {
|
||||
code: 'ShiftLeft',
|
||||
key: 'Shift',
|
||||
shift: true,
|
||||
meta: false,
|
||||
control: false,
|
||||
alt: false
|
||||
}
|
||||
const firstDownPreventDefault = triggerBeforeInput(modifierInput)
|
||||
const firstUpPreventDefault = triggerBeforeInput({ ...modifierInput, type: 'keyUp' })
|
||||
const secondDownPreventDefault = triggerBeforeInput(modifierInput)
|
||||
|
||||
expect(firstDownPreventDefault).not.toHaveBeenCalled()
|
||||
expect(firstUpPreventDefault).not.toHaveBeenCalled()
|
||||
expect(secondDownPreventDefault).toHaveBeenCalledTimes(1)
|
||||
expect(rendererSendMock).toHaveBeenCalledWith('ui:openQuickOpen')
|
||||
})
|
||||
|
||||
it('forwards double-tap tab shortcuts from focused guest pages', () => {
|
||||
setupGuestShortcutForwarding({
|
||||
browserTabId,
|
||||
guest: makeGuest(),
|
||||
resolveRenderer: () => makeRenderer(),
|
||||
getKeybindings: () => ({
|
||||
'tab.newBrowser': ['DoubleTap+Shift']
|
||||
})
|
||||
})
|
||||
|
||||
const modifierInput = {
|
||||
code: 'ShiftLeft',
|
||||
key: 'Shift',
|
||||
shift: true,
|
||||
meta: false,
|
||||
control: false,
|
||||
alt: false
|
||||
}
|
||||
triggerBeforeInput(modifierInput)
|
||||
triggerBeforeInput({ ...modifierInput, type: 'keyUp' })
|
||||
const secondDownPreventDefault = triggerBeforeInput(modifierInput)
|
||||
|
||||
expect(secondDownPreventDefault).toHaveBeenCalledTimes(1)
|
||||
expect(rendererSendMock).toHaveBeenCalledWith('ui:newBrowserTab')
|
||||
})
|
||||
|
||||
it('resets guest double-tap detection on blur', () => {
|
||||
setupGuestShortcutForwarding({
|
||||
browserTabId,
|
||||
guest: makeGuest(),
|
||||
resolveRenderer: () => makeRenderer(),
|
||||
getKeybindings: () => ({
|
||||
'worktree.quickOpen': ['DoubleTap+Shift']
|
||||
})
|
||||
})
|
||||
|
||||
const modifierInput = {
|
||||
code: 'ShiftLeft',
|
||||
key: 'Shift',
|
||||
shift: true,
|
||||
meta: false,
|
||||
control: false,
|
||||
alt: false
|
||||
}
|
||||
triggerBeforeInput(modifierInput)
|
||||
triggerBeforeInput({ ...modifierInput, type: 'keyUp' })
|
||||
triggerGuestBlur()
|
||||
const nextDownPreventDefault = triggerBeforeInput(modifierInput)
|
||||
|
||||
expect(nextDownPreventDefault).not.toHaveBeenCalled()
|
||||
expect(rendererSendMock).not.toHaveBeenCalledWith('ui:openQuickOpen')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -11,11 +11,16 @@ import {
|
||||
import {
|
||||
isRecentTabSwitcherCommitRelease,
|
||||
matchesRecentTabSwitcherChord,
|
||||
resolveWindowShortcutAction
|
||||
resolveWindowShortcutAction,
|
||||
type WindowShortcutInput
|
||||
} from '../../shared/window-shortcut-policy'
|
||||
import { readGuestNavigationState } from './browser-guest-navigation-state'
|
||||
import { keybindingMatchesAction, type KeybindingOverrides } from '../../shared/keybindings'
|
||||
import type { BrowserPageZoomDirection } from '../../shared/browser-page-zoom'
|
||||
import {
|
||||
ModifierDoubleTapDetector,
|
||||
toModifierDoubleTapEvent
|
||||
} from '../../shared/modifier-double-tap-detector'
|
||||
|
||||
type ResolveRenderer = (browserTabId: string) => Electron.WebContents | null
|
||||
type ShouldForwardDictationShortcut = () => boolean
|
||||
@@ -263,49 +268,30 @@ export function setupGuestShortcutForwarding(args: {
|
||||
getKeybindings
|
||||
} = args
|
||||
let ctrlTabSwitching = false
|
||||
const handler = (event: Electron.Event, input: Electron.Input): void => {
|
||||
const doubleTapDetector = new ModifierDoubleTapDetector()
|
||||
const resetDoubleTapDetector = (): void => doubleTapDetector.reset()
|
||||
type GuestShortcutInput = WindowShortcutInput & { isAutoRepeat?: boolean }
|
||||
|
||||
const forwardShortcutInput = (
|
||||
event: Electron.Event,
|
||||
input: GuestShortcutInput,
|
||||
action = resolveWindowShortcutAction(input, process.platform, getKeybindings?.())
|
||||
): boolean => {
|
||||
const keybindings = getKeybindings?.()
|
||||
if (
|
||||
input.type === 'keyDown' &&
|
||||
matchesRecentTabSwitcherChord(input, process.platform, keybindings)
|
||||
) {
|
||||
event.preventDefault()
|
||||
ctrlTabSwitching = true
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:ctrlTabKeyDown', { shiftKey: input.shift === true })
|
||||
return
|
||||
}
|
||||
|
||||
if (ctrlTabSwitching && isRecentTabSwitcherCommitRelease(input)) {
|
||||
event.preventDefault()
|
||||
ctrlTabSwitching = false
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:ctrlTabKeyUp')
|
||||
return
|
||||
}
|
||||
|
||||
if (input.type !== 'keyDown') {
|
||||
return
|
||||
}
|
||||
// Why: resolve the policy action once per keystroke. The history-navigate
|
||||
// chord (Cmd/Ctrl+Alt+Arrow) is the only allowlisted chord that carries
|
||||
// Alt and must be handled before the generic modifier-chord gate below,
|
||||
// which rejects Alt. Every other chord handled further down can reuse
|
||||
// the same `action` rather than re-running the full predicate chain.
|
||||
const action = resolveWindowShortcutAction(input, process.platform, keybindings)
|
||||
if (action?.type === 'zoom') {
|
||||
// Why: browser page zoom must consume repeats and teardown races before
|
||||
// Chromium or the guest page can apply its own shortcut behavior.
|
||||
event.preventDefault()
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:zoomBrowserPage', action.direction)
|
||||
return
|
||||
return true
|
||||
}
|
||||
if (input.isAutoRepeat) {
|
||||
if (action?.type === 'dictationKeyDown' && shouldForwardDictationShortcut?.()) {
|
||||
event.preventDefault()
|
||||
return true
|
||||
}
|
||||
return
|
||||
return false
|
||||
}
|
||||
if (action?.type === 'worktreeHistoryNavigate') {
|
||||
// Why: preventDefault unconditionally — if we cannot resolve the
|
||||
@@ -317,14 +303,14 @@ export function setupGuestShortcutForwarding(args: {
|
||||
event.preventDefault()
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:worktreeHistoryNavigate', action.direction)
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
if (action?.type === 'toggleFloatingTerminal') {
|
||||
event.preventDefault()
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:toggleFloatingTerminal')
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
// Why: Cmd/Ctrl+Alt+[ / ] cycles across every tab type. Handled before
|
||||
@@ -344,14 +330,14 @@ export function setupGuestShortcutForwarding(args: {
|
||||
event.preventDefault()
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:switchTabAcrossAllTypes', switchAllTypesDirection)
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
if (keybindingMatchesAction('tab.previousRecent', input, process.platform, keybindings)) {
|
||||
event.preventDefault()
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:switchRecentTab')
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
// Why: terminal-only tab switching defaults to Ctrl+PageUp/PageDown on every
|
||||
@@ -370,12 +356,12 @@ export function setupGuestShortcutForwarding(args: {
|
||||
event.preventDefault()
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:switchTerminalTab', terminalTabDirection)
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
if (!renderer) {
|
||||
return
|
||||
return false
|
||||
}
|
||||
if (keybindingMatchesAction('tab.newBrowser', input, process.platform, keybindings)) {
|
||||
renderer.send('ui:newBrowserTab')
|
||||
@@ -456,21 +442,84 @@ export function setupGuestShortcutForwarding(args: {
|
||||
renderer.send('ui:jumpToTabIndex', action.index)
|
||||
} else if (action?.type === 'dictationKeyDown') {
|
||||
if (!shouldForwardDictationShortcut?.()) {
|
||||
return
|
||||
return false
|
||||
}
|
||||
renderer.send('ui:dictationKeyDown')
|
||||
} else {
|
||||
return
|
||||
return false
|
||||
}
|
||||
// Why: preventDefault stops the guest page from also processing the chord
|
||||
// (e.g. Cmd+T opening a browser-internal new-tab page).
|
||||
event.preventDefault()
|
||||
return true
|
||||
}
|
||||
|
||||
const handler = (event: Electron.Event, input: Electron.Input): void => {
|
||||
const keybindings = getKeybindings?.()
|
||||
if (
|
||||
input.type === 'keyDown' &&
|
||||
matchesRecentTabSwitcherChord(input, process.platform, keybindings)
|
||||
) {
|
||||
event.preventDefault()
|
||||
ctrlTabSwitching = true
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:ctrlTabKeyDown', { shiftKey: input.shift === true })
|
||||
return
|
||||
}
|
||||
|
||||
if (ctrlTabSwitching && isRecentTabSwitcherCommitRelease(input)) {
|
||||
event.preventDefault()
|
||||
ctrlTabSwitching = false
|
||||
const renderer = resolveRenderer(browserTabId)
|
||||
renderer?.send('ui:ctrlTabKeyUp')
|
||||
return
|
||||
}
|
||||
|
||||
if (input.type === 'keyDown' || input.type === 'keyUp') {
|
||||
const detected = doubleTapDetector.process(
|
||||
toModifierDoubleTapEvent({
|
||||
type: input.type,
|
||||
code: input.code,
|
||||
key: input.key,
|
||||
shift: input.shift,
|
||||
control: input.control,
|
||||
alt: input.alt,
|
||||
meta: input.meta,
|
||||
isAutoRepeat: input.isAutoRepeat
|
||||
}),
|
||||
Date.now()
|
||||
)
|
||||
if (detected) {
|
||||
const doubleTapInput: GuestShortcutInput = { doubleTapModifier: detected.modifier }
|
||||
forwardShortcutInput(
|
||||
event,
|
||||
doubleTapInput,
|
||||
resolveWindowShortcutAction(doubleTapInput, process.platform, keybindings, {
|
||||
context: 'app'
|
||||
})
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (input.type !== 'keyDown') {
|
||||
return
|
||||
}
|
||||
// Why: resolve the policy action once per keystroke. The history-navigate
|
||||
// chord (Cmd/Ctrl+Alt+Arrow) is the only allowlisted chord that carries
|
||||
// Alt and must be handled before the generic modifier-chord gate below,
|
||||
// which rejects Alt. Every other chord handled further down can reuse
|
||||
// the same `action` rather than re-running the full predicate chain.
|
||||
const action = resolveWindowShortcutAction(input, process.platform, keybindings)
|
||||
forwardShortcutInput(event, input, action)
|
||||
}
|
||||
|
||||
guest.on('before-input-event', handler)
|
||||
guest.on('blur', resetDoubleTapDetector)
|
||||
return () => {
|
||||
try {
|
||||
guest.off('before-input-event', handler)
|
||||
guest.off('blur', resetDoubleTapDetector)
|
||||
} catch {
|
||||
// Why: best-effort — guest may already be destroyed during teardown.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user