Skip to content

feat: auto-stop recording on application focus loss (opt-in)#342

Open
hanselstner wants to merge 1 commit intoOpenWhispr:mainfrom
hanselstner:feat/focus-loss-autostop
Open

feat: auto-stop recording on application focus loss (opt-in)#342
hanselstner wants to merge 1 commit intoOpenWhispr:mainfrom
hanselstner:feat/focus-loss-autostop

Conversation

@hanselstner
Copy link

Summary

Adds an opt-in setting to automatically stop (cancel) recording when the user switches away from the application that was focused when recording started. This prevents accidental long recordings when multitasking.

Changes

Core feature

  • src/helpers/focusWatcher.js (new): PowerShell-based window focus monitor that polls the foreground window PID and fires focus-lost / focus-restored events
  • resources/get-foreground-pid.ps1 (new): Minimal PowerShell script using user32.dll P/Invoke to get the foreground window's process ID
  • main.js: Integrate FocusWatcher lifecycle — start on recording, stop on idle, forward focus-lost events to renderer
  • src/hooks/useAudioRecording.js: Listen for focus-lost IPC events and cancel recording with an audio cue
  • preload.js: Expose onFocusLost / offFocusLost IPC channels

Settings UI

  • src/hooks/useSettings.ts: Add stopOnFocusLoss setting (default: false)
  • src/components/SettingsPage.tsx: Add toggle under Dictation Behavior section
  • All 10 locale files: Add stopOnFocusLoss and stopOnFocusLossDescription translations

Window management

  • src/helpers/windowConfig.js: Use floating window level instead of pop-up-menu on Windows to prevent focus stealing
  • electron-builder.json: Include get-foreground-pid.ps1 in extraResources

Test plan

  1. Enable "Stop on Focus Loss" in Settings → Dictation Behavior
  2. Start recording, then Alt+Tab to another window → recording should cancel with audio cue
  3. Start recording, stay in the same window → recording continues normally
  4. Disable the setting → focus switching should not affect recording
  5. Verify the setting defaults to false (off) for new installations

Pull Request opened by Augment Code with guidance from the PR author

When dictating into a text field and switching to another application
(e.g. clicking into a browser or IDE), the recording is automatically
cancelled to prevent stale audio from being transcribed and pasted
into the wrong window.

This feature is opt-in and disabled by default.  Users can enable it
in Settings > General > Dictation Behavior > "Stop when switching apps".

Implementation details:

- Add FocusWatcher (src/helpers/focusWatcher.js) that polls the
  foreground window handle on Windows via a lightweight PowerShell
  script (resources/get-foreground-pid.ps1).  When the handle changes,
  a "focus-changed" event is emitted.
- On focus loss during recording, the main process sends a
  "cancel-dictation" IPC event to discard the audio instead of
  transcribing it (transcribing would paste into the now-focused app).
- Add "blur-main-window" IPC handler so clicking the overlay stop
  button returns focus to the previously active application.
- Prevent Chromium renderer throttling on Windows via
  disable-renderer-backgrounding and disable-background-timer-throttling
  so the dictation overlay animations keep running while unfocused.
- Change setAlwaysOnTop level from pop-up-menu to floating on
  Windows to prevent the overlay from stealing focus.
- Guard show/focus enforceMainWindowOnTop calls behind a platform
  check to avoid repeated focus grabs on Windows.
- Add stopOnFocusLoss setting (default: false) with full i18n support
  across all 10 languages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant