From 33710fe39a028da9c249e35cc35b7de3315fd0ee Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 05:01:15 +0000 Subject: [PATCH] refactor(dictation): simplify Auto hold timing and reuse the shared IME check --- .../normalize-loaded-global-settings.ts | 13 +++-- ...ainWindow-terminal-focus-shortcuts.test.ts | 23 ++++----- .../dictation/DictationController.tsx | 2 +- .../dictation/use-hold-dictation-gesture.ts | 51 ++++++++++--------- .../voice/VoiceDictationSettingsSection.tsx | 11 ++-- 5 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/main/persistence/loading-store/normalize-loaded-global-settings.ts b/src/main/persistence/loading-store/normalize-loaded-global-settings.ts index 7a954d5b6c3..b5915fa7007 100644 --- a/src/main/persistence/loading-store/normalize-loaded-global-settings.ts +++ b/src/main/persistence/loading-store/normalize-loaded-global-settings.ts @@ -74,13 +74,12 @@ export function normalizeLoadedGlobalSettings( const pluginOwnedSettings = normalizePluginOwnedSettings(parsed.settings ?? {}) const voice = { ...getDefaultVoiceSettings(), ...parsed.settings?.voice } // Stored 'toggle' was everyone's old default, and Auto still toggles on a tap; Hold was always a choice. - if ( - voice.dictationMode !== 'hold' && - !( - parsed.settings?.voiceDictationModeDefaultedToAuto === true && - voice.dictationMode === 'toggle' - ) - ) { + const keepsDictationMode = + voice.dictationMode === 'hold' || + voice.dictationMode === 'auto' || + (parsed.settings?.voiceDictationModeDefaultedToAuto === true && + voice.dictationMode === 'toggle') + if (!keepsDictationMode) { voice.dictationMode = 'auto' } const matchTerminalSidebarInputs = { diff --git a/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts b/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts index 48936c7ebdd..cc721ef93d1 100644 --- a/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts +++ b/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts @@ -1,4 +1,5 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { DictationMode } from '../../shared/speech-types' vi.mock('electron', async () => (await import('./createMainWindow-test-harness')).electronModuleMock() @@ -58,12 +59,11 @@ describe('createMainWindow', () => { return browserWindowInstance }) - const voice: { enabled: boolean; sttModel: string; dictationMode: 'auto' | 'toggle' | 'hold' } = - { - enabled: false, - sttModel: '', - dictationMode: 'toggle' - } + const voice: { enabled: boolean; sttModel: string; dictationMode: DictationMode } = { + enabled: false, + sttModel: '', + dictationMode: 'toggle' + } createMainWindow({ getUI: () => ({}) as never, getSettings: () => ({ windowBackgroundBlur: false, voice }) as never, @@ -173,12 +173,11 @@ describe('createMainWindow', () => { return browserWindowInstance }) - const voice: { enabled: boolean; sttModel: string; dictationMode: 'auto' | 'toggle' | 'hold' } = - { - enabled: false, - sttModel: '', - dictationMode: 'toggle' - } + const voice: { enabled: boolean; sttModel: string; dictationMode: DictationMode } = { + enabled: false, + sttModel: '', + dictationMode: 'toggle' + } createMainWindow( { getUI: () => ({}), diff --git a/src/renderer/src/components/dictation/DictationController.tsx b/src/renderer/src/components/dictation/DictationController.tsx index 0228767cf24..d509b0ef504 100644 --- a/src/renderer/src/components/dictation/DictationController.tsx +++ b/src/renderer/src/components/dictation/DictationController.tsx @@ -262,7 +262,7 @@ export function DictationController() { } }, [startDictation, stopDictation]) - // Toggle presses, and Auto double taps (which have no release), arrive as intercepted main IPC so the chord cannot reach xterm or system shortcuts. + // Main intercepts Toggle presses and Auto double taps so the chord never reaches xterm. useEffect(() => { const mode = settings?.voice?.dictationMode ?? 'auto' if (mode === 'hold' || !settings?.voice?.enabled) { 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 54841b0fcaf..efdec56f33c 100644 --- a/src/renderer/src/components/dictation/use-hold-dictation-gesture.ts +++ b/src/renderer/src/components/dictation/use-hold-dictation-gesture.ts @@ -1,6 +1,11 @@ import { useEffect, useEffectEvent, useRef } from 'react' import { getShortcutPlatform } from '@/lib/shortcut-platform' -import { keybindingMatchesAction, type KeybindingOverrides } from '../../../../shared/keybindings' +import { + keybindingMatchesAction, + normalizeTerminalShortcutPolicy, + type KeybindingOverrides +} from '../../../../shared/keybindings' +import { textInputOwnsComposition } from '../../../../shared/text-input-shortcut-policy' import type { GlobalSettings } from '../../../../shared/global-settings-types' import { addAppKeyListener, removeAppKeyListener } from '@/lib/app-keyboard-router' import { showTerminalShortcutCaptureNotification } from '@/lib/terminal-shortcut-capture-notification' @@ -20,8 +25,8 @@ type HoldDictationReleaseMatcher = (event: KeyboardEvent) => boolean type ActiveHold = { matchesRelease: HoldDictationReleaseMatcher pressedAt: number - /** Key repeat showed the key still down past the Auto threshold. */ - repeatedPastThreshold: boolean + /** Latest keydown for this press, key repeat included. */ + lastDownAt: number startedDictation: boolean } @@ -134,13 +139,15 @@ export function useHoldDictationGesture({ terminalShortcutPolicy: settings?.terminalShortcutPolicy }) ) - const notifyTerminalCapture = useEffectEvent(() => - showTerminalShortcutCaptureNotification({ - actionId: 'voice.dictation', - platform: getShortcutPlatform(), - keybindings - }) - ) + const notifyTerminalCapture = useEffectEvent(() => { + if (normalizeTerminalShortcutPolicy(settings?.terminalShortcutPolicy) === 'orca-first') { + showTerminalShortcutCaptureNotification({ + actionId: 'voice.dictation', + platform: getShortcutPlatform(), + keybindings + }) + } + }) // Hold and Auto own DOM press/release events; main before-input-event suppresses keyup after preventDefault. useEffect(() => { @@ -148,25 +155,21 @@ export function useHoldDictationGesture({ return } - // Losing the release (blur, hidden, mode change) ends the press, which inserts what was said. - // releasedAt is null when the key's own release time is unknown; only key repeat then proves an Auto hold. + // Losing the release (blur, hidden, mode change) ends the press; without a release time, key repeat times it. const endHold = (releasedAt: number | null = null): void => { const hold = activeHoldRef.current if (!hold) { return } activeHoldRef.current = null - const held = - hold.repeatedPastThreshold || - (releasedAt !== null && releasedAt - hold.pressedAt >= AUTO_HOLD_THRESHOLD_MS) + const held = (releasedAt ?? hold.lastDownAt) - hold.pressedAt >= AUTO_HOLD_THRESHOLD_MS if (mode === 'hold' || (hold.startedDictation && held)) { stop() } } const handleKeyDown = (e: KeyboardEvent): void => { - // keyCode 229: IME keydowns that some engines send without isComposing. - if (e.isComposing || e.keyCode === 229 || !matchesShortcut(e)) { + if (textInputOwnsComposition(e) || !matchesShortcut(e)) { return } if (e.target instanceof Element && e.target.closest('[data-shortcut-recorder-active]')) { @@ -178,23 +181,23 @@ export function useHoldDictationGesture({ 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' && isUnderway()) { // A tap started this dictation; the next press ends it. + activeHoldRef.current = null stop() return } + const now = performance.now() activeHoldRef.current = { matchesRelease: createHoldDictationReleaseMatcher(e), - pressedAt: performance.now(), - repeatedPastThreshold: false, + pressedAt: now, + lastDownAt: now, startedDictation: false } } const hold = activeHoldRef.current - if (hold && e.repeat && performance.now() - hold.pressedAt >= AUTO_HOLD_THRESHOLD_MS) { - hold.repeatedPastThreshold = true + if (hold) { + hold.lastDownAt = performance.now() } // One dictation per press: key repeat after an automatic stop must not start another. if (hold && !hold.startedDictation && canStart()) { @@ -206,7 +209,7 @@ 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 keyup with Cmd down or going up can't time the key. + // macOS drops the key's keyup while Cmd is held, so no keyup under Cmd times the key. const cmdHidesKeyUp = getShortcutPlatform() === 'darwin' && (e.metaKey || getReleasedModifier(e) === 'meta') endHold(cmdHidesKeyUp ? null : performance.now()) diff --git a/src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx b/src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx index 9b9d812efc6..aa7f7fa85a2 100644 --- a/src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx +++ b/src/renderer/src/components/settings/voice/VoiceDictationSettingsSection.tsx @@ -21,6 +21,11 @@ export function VoiceDictationSettingsSection({ }: VoiceDictationSettingsSectionProps): React.JSX.Element { const t = useTranslate() const shortcutLabel = useShortcutLabel('voice.dictation') + const modeLabels = { + auto: t('auto.components.settings.VoicePane.dictationModeAuto', 'Auto'), + hold: t('auto.components.settings.VoicePane.174da92062', 'Hold'), + toggle: t('auto.components.settings.VoicePane.118b3c2dee', 'Toggle') + } return ( <> @@ -72,11 +77,7 @@ export function VoiceDictationSettingsSection({ : 'text-muted-foreground hover:text-foreground' } ${!voiceSettings.enabled ? 'opacity-50 cursor-not-allowed' : ''}`} > - {mode === 'auto' - ? t('auto.components.settings.VoicePane.dictationModeAuto', 'Auto') - : mode === 'toggle' - ? t('auto.components.settings.VoicePane.118b3c2dee', 'Toggle') - : t('auto.components.settings.VoicePane.174da92062', 'Hold')} + {modeLabels[mode]} ))}