From a34145add44d4279cc4e733f94678f68dd68fbac Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:53:13 +0000 Subject: [PATCH] fix(dictation): start an Auto hold pressed while the last dictation finishes --- .../dictation/DictationController.tsx | 1 + .../use-hold-dictation-gesture.test.tsx | 17 ++++++++++++++++ .../dictation/use-hold-dictation-gesture.ts | 20 ++++++++++--------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/renderer/src/components/dictation/DictationController.tsx b/src/renderer/src/components/dictation/DictationController.tsx index 65bd521ed17..0228767cf24 100644 --- a/src/renderer/src/components/dictation/DictationController.tsx +++ b/src/renderer/src/components/dictation/DictationController.tsx @@ -298,6 +298,7 @@ export function DictationController() { useHoldDictationGesture({ canStartDictation: () => canStartDictation(sessionRef.current.phase), + isDictationUnderway: () => isDictationUnderway(sessionRef.current.phase), keybindings, settings, startDictation, diff --git a/src/renderer/src/components/dictation/use-hold-dictation-gesture.test.tsx b/src/renderer/src/components/dictation/use-hold-dictation-gesture.test.tsx index a90b942d7ed..b0ba49397e8 100644 --- a/src/renderer/src/components/dictation/use-hold-dictation-gesture.test.tsx +++ b/src/renderer/src/components/dictation/use-hold-dictation-gesture.test.tsx @@ -36,6 +36,7 @@ function holdSettings(): GlobalSettings { function Probe(): null { useHoldDictationGesture({ canStartDictation: () => dictationState === 'idle', + isDictationUnderway: () => dictationState === 'starting' || dictationState === 'listening', keybindings: {}, settings: holdSettings(), startDictation, @@ -200,6 +201,22 @@ describe('useHoldDictationGesture in Auto mode', () => { expect(stopDictation).toHaveBeenCalledTimes(1) }) + it('starts a hold pressed while the last dictation was still finishing', async () => { + await renderProbe() + dictationState = 'stopping' + + act(() => { + dispatchKeyDown({ key: 'e', code: 'KeyE', metaKey: true }) + }) + dictationState = 'idle' + act(() => { + dispatchKeyDown({ key: 'e', code: 'KeyE', metaKey: true, repeat: true }) + }) + + expect(stopDictation).not.toHaveBeenCalled() + expect(startDictation).toHaveBeenCalledTimes(1) + }) + it('leaves the shortcut to the IME while composing', async () => { await renderProbe() diff --git a/src/renderer/src/components/dictation/use-hold-dictation-gesture.ts b/src/renderer/src/components/dictation/use-hold-dictation-gesture.ts index 25f08638e20..54841b0fcaf 100644 --- a/src/renderer/src/components/dictation/use-hold-dictation-gesture.ts +++ b/src/renderer/src/components/dictation/use-hold-dictation-gesture.ts @@ -4,9 +4,11 @@ import { keybindingMatchesAction, type KeybindingOverrides } from '../../../../s import type { GlobalSettings } from '../../../../shared/global-settings-types' import { addAppKeyListener, removeAppKeyListener } from '@/lib/app-keyboard-router' import { showTerminalShortcutCaptureNotification } from '@/lib/terminal-shortcut-capture-notification' +import { getKeybindingContext } from '../terminal-workspace-model' type HoldDictationGestureOptions = { canStartDictation: () => boolean + isDictationUnderway: () => boolean keybindings: KeybindingOverrides settings: GlobalSettings | null startDictation: () => Promise | void @@ -109,12 +111,9 @@ function createHoldDictationReleaseMatcher(event: KeyboardEvent): HoldDictationR } } -function isTerminalInputTarget(target: EventTarget | null): boolean { - return target instanceof HTMLElement && target.classList.contains('xterm-helper-textarea') -} - export function useHoldDictationGesture({ canStartDictation, + isDictationUnderway, keybindings, settings, startDictation, @@ -127,10 +126,11 @@ export function useHoldDictationGesture({ const start = useEffectEvent(() => void startDictation()) const stop = useEffectEvent(() => void stopDictation()) const canStart = useEffectEvent(() => canStartDictation()) + const isUnderway = useEffectEvent(() => isDictationUnderway()) // Main forwards Hold and Auto presses, so the terminal shortcut policy it would apply is applied here. const matchesShortcut = useEffectEvent((e: KeyboardEvent) => keybindingMatchesAction('voice.dictation', e, getShortcutPlatform(), keybindings, { - context: isTerminalInputTarget(e.target) ? 'terminal' : 'app', + context: getKeybindingContext(e.target), terminalShortcutPolicy: settings?.terminalShortcutPolicy }) ) @@ -175,12 +175,12 @@ export function useHoldDictationGesture({ e.preventDefault() e.stopPropagation() if (!e.repeat) { - if (isTerminalInputTarget(e.target)) { + if (getKeybindingContext(e.target) === 'terminal') { notifyTerminalCapture() } // macOS drops the shortcut key's keyup while Cmd is held, so every fresh press starts over. activeHoldRef.current = null - if (mode === 'auto' && !canStart()) { + if (mode === 'auto' && isUnderway()) { // A tap started this dictation; the next press ends it. stop() return @@ -206,8 +206,10 @@ export function useHoldDictationGesture({ const handleKeyUp = (e: KeyboardEvent): void => { const hold = activeHoldRef.current if (hold && (matchesShortcut(e) || hold.matchesRelease(e))) { - // macOS drops the key's keyup while Cmd is held, so a Cmd keyup says nothing about when the key went up. - endHold(getReleasedModifier(e) === 'meta' ? null : performance.now()) + // macOS drops the key's keyup while Cmd is held, so a keyup with Cmd down or going up can't time the key. + const cmdHidesKeyUp = + getShortcutPlatform() === 'darwin' && (e.metaKey || getReleasedModifier(e) === 'meta') + endHold(cmdHidesKeyUp ? null : performance.now()) } }