Skip to content

armgit / Handy

Public Imported from GitHub
0

A free, open source, and extensible speech-to-text application that works completely offline.

fix: push-to-talk rapidly toggles recording under X11 auto-repeat (defer synthesized releases) (#1605)

Matt Van Horn438582fc6432
diff --git a/src-tauri/src/transcription_coordinator.rs b/src-tauri/src/transcription_coordinator.rs
index e35495c0..0d0af232 100644
--- a/src-tauri/src/transcription_coordinator.rs
+++ b/src-tauri/src/transcription_coordinator.rs
@@ -8,6 +8,20 @@ use std::time::{Duration, Instant};
use tauri::{AppHandle, Manager};
const DEBOUNCE: Duration = Duration::from_millis(30);
+const RELEASE_GRACE: Duration = Duration::from_millis(50);
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum PttAction {
+ Passthrough,
+ DeferRelease,
+ CancelRelease,
+}
+
+struct PendingRelease {
+ binding_id: String,
+ hotkey_string: String,
+ deadline: Instant,
+}
/// Commands processed sequentially by the coordinator thread.
enum Command {
@@ -30,6 +44,30 @@ enum Stage {
Processing,
}
+fn classify_ptt_event(
+ pending_release_binding: Option<&str>,
+ is_pressed: bool,
+ push_to_talk: bool,
+ binding_id: &str,
+ recording_binding: Option<&str>,
+) -> PttAction {
+ if !push_to_talk {
+ return PttAction::Passthrough;
+ }
+
+ if is_pressed {
+ if pending_release_binding == Some(binding_id) {
+ PttAction::CancelRelease
+ } else {
+ PttAction::Passthrough
+ }
+ } else if recording_binding == Some(binding_id) && pending_release_binding.is_none() {
+ PttAction::DeferRelease
+ } else {
+ PttAction::Passthrough
+ }
+}
+
/// Serialises all transcription lifecycle events through a single thread
/// to eliminate race conditions between keyboard shortcuts, signals, and
/// the async transcribe-paste pipeline.
@@ -49,8 +87,37 @@ impl TranscriptionCoordinator {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let mut stage = Stage::Idle;
let mut last_press: Option<Instant> = None;
+ let mut pending_release: Option<PendingRelease> = None;
+
+ loop {
+ let cmd = if let Some(pending) = &pending_release {
+ match rx.recv_timeout(
+ pending.deadline.saturating_duration_since(Instant::now()),
+ ) {
+ Ok(cmd) => cmd,
+ Err(mpsc::RecvTimeoutError::Timeout) => {
+ if let Some(pending) = pending_release.take() {
+ if matches!(&stage, Stage::Recording(id) if id == &pending.binding_id)
+ {
+ stop(
+ &app,
+ &mut stage,
+ &pending.binding_id,
+ &pending.hotkey_string,
+ );
+ }
+ }
+ continue;
+ }
+ Err(mpsc::RecvTimeoutError::Disconnected) => break,
+ }
+ } else {
+ match rx.recv() {
+ Ok(cmd) => cmd,
+ Err(_) => break,
+ }
+ };
- while let Ok(cmd) = rx.recv() {
match cmd {
Command::Input {
binding_id,
@@ -58,8 +125,38 @@ impl TranscriptionCoordinator {
is_pressed,
push_to_talk,
} => {
+ let pending_release_binding = pending_release
+ .as_ref()
+ .map(|pending| pending.binding_id.as_str());
+ let recording_binding = match &stage {
+ Stage::Recording(id) => Some(id.as_str()),
+ _ => None,
+ };
+
+ match classify_ptt_event(
+ pending_release_binding,
+ is_pressed,
+ push_to_talk,
+ &binding_id,
+ recording_binding,
+ ) {
+ PttAction::CancelRelease => {
+ pending_release = None;
+ continue;
+ }
+ PttAction::DeferRelease => {
+ pending_release = Some(PendingRelease {
+ binding_id,
+ hotkey_string,
+ deadline: Instant::now() + RELEASE_GRACE,
+ });
+ continue;
+ }
+ PttAction::Passthrough => {}
+ }
+
// Debounce rapid-fire press events (key repeat / double-tap).
- // Releases always pass through for push-to-talk.
+ // Push-to-talk releases may be deferred above to absorb X11 auto-repeat.
if is_pressed {
let now = Instant::now();
if last_press.is_some_and(|t| now.duration_since(t) < DEBOUNCE) {
@@ -94,6 +191,7 @@ impl TranscriptionCoordinator {
Command::Cancel {
recording_was_active,
} => {
+ pending_release = None;
// Don't reset during processing — wait for the pipeline to finish.
if !matches!(stage, Stage::Processing)
&& (recording_was_active || matches!(stage, Stage::Recording(_)))
@@ -182,3 +280,242 @@ fn stop(app: &AppHandle, stage: &mut Stage, binding_id: &str, hotkey_string: &st
action.stop(app, binding_id, hotkey_string);
*stage = Stage::Processing;
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn push_to_talk_release_while_recording_defers_release() {
+ assert_eq!(
+ classify_ptt_event(None, false, true, "transcribe", Some("transcribe")),
+ PttAction::DeferRelease
+ );
+ }
+
+ #[test]
+ fn push_to_talk_press_matching_pending_release_cancels_release() {
+ assert_eq!(
+ classify_ptt_event(
+ Some("transcribe"),
+ true,
+ true,
+ "transcribe",
+ Some("transcribe")
+ ),
+ PttAction::CancelRelease
+ );
+ }
+
+ #[test]
+ fn toggle_mode_press_and_release_pass_through() {
+ assert_eq!(
+ classify_ptt_event(
+ Some("transcribe"),
+ true,
+ false,
+ "transcribe",
+ Some("transcribe")
+ ),
+ PttAction::Passthrough
+ );
+ assert_eq!(
+ classify_ptt_event(None, false, false, "transcribe", Some("transcribe")),
+ PttAction::Passthrough
+ );
+ }
+
+ #[test]
+ fn press_for_different_binding_than_pending_release_passes_through() {
+ assert_eq!(
+ classify_ptt_event(
+ Some("transcribe"),
+ true,
+ true,
+ "transcribe_with_post_process",
+ Some("transcribe")
+ ),
+ PttAction::Passthrough
+ );
+ }
+
+ #[test]
+ fn press_matching_pending_release_cancels_without_recording_state() {
+ assert_eq!(
+ classify_ptt_event(Some("transcribe"), true, true, "transcribe", None),
+ PttAction::CancelRelease
+ );
+ }
+
+ // ---------------------------------------------------------------------
+ // Sequence-level regression coverage for issue #1539.
+ //
+ // Under X11 key auto-repeat, holding a push-to-talk key does not emit one
+ // long press. It emits the initial press followed by a stream of
+ // synthesized release/press pairs, then a single genuine release on key-up.
+ // Before the fix, every synthesized release passed straight through and
+ // stopped recording, so holding the key "rapidly toggled" recording on and
+ // off. The fix defers each release for a short grace window and cancels it
+ // when the matching auto-repeat press arrives.
+ //
+ // The unit tests above assert `classify_ptt_event` in isolation. The
+ // simulator below threads that classifier through the same `pending_release`
+ // / `stage` state transitions the coordinator loop performs (lines that
+ // handle `Command::Input` and the `recv_timeout` grace expiry), so a whole
+ // event burst can be exercised deterministically without a Tauri AppHandle
+ // or real timers.
+ // ---------------------------------------------------------------------
+
+ const BINDING: &str = "transcribe";
+
+ #[derive(Clone, Copy)]
+ enum Ev {
+ /// A key-down event (real initial press or a synthesized auto-repeat press).
+ Press,
+ /// A key-up event (synthesized auto-repeat release or the genuine key-up).
+ Release,
+ /// The `RELEASE_GRACE` window elapsed with no cancelling press arriving.
+ Grace,
+ }
+
+ #[derive(Debug, PartialEq, Eq)]
+ enum SimStage {
+ Idle,
+ Recording,
+ Processing,
+ }
+
+ struct SimResult {
+ starts: u32,
+ stops: u32,
+ stage: SimStage,
+ }
+
+ /// Mirror of the coordinator loop's decision logic for a single push-to-talk
+ /// binding: it calls the real `classify_ptt_event` and applies the exact same
+ /// Defer / Cancel / debounce / start / stop transitions.
+ fn simulate(events: &[Ev]) -> SimResult {
+ let mut stage = SimStage::Idle;
+ let mut pending: Option<String> = None;
+ let mut last_press_ms: Option<u64> = None;
+ let mut clock_ms: u64 = 0;
+ let mut starts = 0u32;
+ let mut stops = 0u32;
+ let debounce_ms = DEBOUNCE.as_millis() as u64;
+
+ for ev in events {
+ // Auto-repeat events arrive a few ms apart, well inside DEBOUNCE.
+ clock_ms += 5;
+
+ match ev {
+ Ev::Grace => {
+ // Coordinator's `RecvTimeoutError::Timeout` arm: fire the
+ // deferred release iff we are still recording that binding.
+ if let Some(pending_binding) = pending.take() {
+ if stage == SimStage::Recording && pending_binding == BINDING {
+ stage = SimStage::Processing;
+ stops += 1;
+ }
+ }
+ }
+ Ev::Press | Ev::Release => {
+ let is_pressed = matches!(ev, Ev::Press);
+ let pending_binding = pending.as_deref();
+ let recording_binding = if stage == SimStage::Recording {
+ Some(BINDING)
+ } else {
+ None
+ };
+
+ match classify_ptt_event(
+ pending_binding,
+ is_pressed,
+ true, // push_to_talk
+ BINDING,
+ recording_binding,
+ ) {
+ PttAction::CancelRelease => {
+ pending = None;
+ continue;
+ }
+ PttAction::DeferRelease => {
+ pending = Some(BINDING.to_string());
+ continue;
+ }
+ PttAction::Passthrough => {}
+ }
+
+ if is_pressed {
+ if last_press_ms.is_some_and(|t| clock_ms - t < debounce_ms) {
+ continue;
+ }
+ last_press_ms = Some(clock_ms);
+ }
+
+ if is_pressed && stage == SimStage::Idle {
+ stage = SimStage::Recording;
+ starts += 1;
+ } else if !is_pressed && stage == SimStage::Recording {
+ stage = SimStage::Processing;
+ stops += 1;
+ }
+ }
+ }
+ }
+
+ SimResult {
+ starts,
+ stops,
+ stage,
+ }
+ }
+
+ /// Initial press plus several synthesized release/press pairs, as X11 emits
+ /// while a push-to-talk key is held down.
+ fn autorepeat_burst() -> Vec<Ev> {
+ let mut events = vec![Ev::Press];
+ for _ in 0..6 {
+ events.push(Ev::Release);
+ events.push(Ev::Press);
+ }
+ events
+ }
+
+ /// Regression for #1539: a burst of X11 auto-repeat release/press pairs must
+ /// not stop recording. Before the fix the first synthesized release stopped
+ /// recording immediately (stops == 1, stage left Recording), which produced
+ /// the rapid on/off toggling. With the fix the releases are coalesced and
+ /// recording stays continuously active for the whole burst.
+ #[test]
+ fn x11_autorepeat_burst_does_not_toggle_recording() {
+ let result = simulate(&autorepeat_burst());
+ assert_eq!(result.starts, 1, "recording should start exactly once");
+ assert_eq!(
+ result.stops, 0,
+ "synthesized auto-repeat releases must not stop recording mid-burst"
+ );
+ assert_eq!(
+ result.stage,
+ SimStage::Recording,
+ "recording must remain active across the entire auto-repeat burst"
+ );
+ }
+
+ /// Complements the burst test: once the key is genuinely released and the
+ /// grace window elapses with no re-press, recording stops exactly once. This
+ /// proves the debounce only coalesces synthesized releases and does not wedge
+ /// the coordinator or swallow the real key-up.
+ #[test]
+ fn genuine_release_after_grace_stops_recording_once() {
+ let mut events = autorepeat_burst();
+ events.push(Ev::Release); // genuine key-up
+ events.push(Ev::Grace); // grace window elapses, no cancelling press
+ let result = simulate(&events);
+ assert_eq!(result.starts, 1, "recording should start exactly once");
+ assert_eq!(
+ result.stops, 1,
+ "a genuine release should stop recording exactly once"
+ );
+ assert_eq!(result.stage, SimStage::Processing);
+ }
+}