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: keep the cancel shortcut from staying registered after a short recording (#2190)

Shivank Kapurb38987a9075e
diff --git a/src-tauri/src/shortcut/handy_keys.rs b/src-tauri/src/shortcut/handy_keys.rs
index bd8e562e..a3131cbd 100644
--- a/src-tauri/src/shortcut/handy_keys.rs
+++ b/src-tauri/src/shortcut/handy_keys.rs
@@ -457,51 +457,6 @@ pub fn init_shortcuts(app: &AppHandle) -> Result<(), String> {
Ok(())
}
-/// Register the cancel shortcut (called when recording starts)
-pub fn register_cancel_shortcut(app: &AppHandle) {
- // Disabled on Linux due to instability
- #[cfg(target_os = "linux")]
- {
- let _ = app;
- return;
- }
-
- #[cfg(not(target_os = "linux"))]
- {
- let app_clone = app.clone();
- tauri::async_runtime::spawn(async move {
- if let Some(cancel_binding) = get_settings(&app_clone).bindings.get("cancel").cloned() {
- if let Some(state) = app_clone.try_state::<HandyKeysState>() {
- if let Err(e) = state.register(&cancel_binding) {
- error!("Failed to register cancel shortcut: {}", e);
- }
- }
- }
- });
- }
-}
-
-/// Unregister the cancel shortcut (called when recording stops)
-pub fn unregister_cancel_shortcut(app: &AppHandle) {
- #[cfg(target_os = "linux")]
- {
- let _ = app;
- return;
- }
-
- #[cfg(not(target_os = "linux"))]
- {
- let app_clone = app.clone();
- tauri::async_runtime::spawn(async move {
- if let Some(cancel_binding) = get_settings(&app_clone).bindings.get("cancel").cloned() {
- if let Some(state) = app_clone.try_state::<HandyKeysState>() {
- let _ = state.unregister(&cancel_binding);
- }
- }
- });
- }
-}
-
/// Register a shortcut
pub fn register_shortcut(app: &AppHandle, binding: ShortcutBinding) -> Result<(), String> {
let state = app
diff --git a/src-tauri/src/shortcut/mod.rs b/src-tauri/src/shortcut/mod.rs
index d02285de..607ef521 100644
--- a/src-tauri/src/shortcut/mod.rs
+++ b/src-tauri/src/shortcut/mod.rs
@@ -16,6 +16,7 @@ pub mod tauri_impl;
use log::{debug, error, info, warn};
use serde::Serialize;
use specta::Type;
+use std::sync::atomic::{AtomicBool, Ordering};
use tauri::{AppHandle, Emitter, Manager};
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
@@ -55,27 +56,76 @@ pub fn init_shortcuts(app: &AppHandle) {
}
}
+/// Whether the recording lifecycle currently wants the cancel shortcut.
+/// Written synchronously by start/stop, so it always holds the latest request.
+static CANCEL_REQUESTED: AtomicBool = AtomicBool::new(false);
+
+/// Whether the cancel shortcut is actually registered with the backend.
+/// The lock also serializes reconciliation passes.
+#[cfg(not(target_os = "linux"))]
+static CANCEL_REGISTERED: std::sync::Mutex<bool> = std::sync::Mutex::new(false);
+
/// Register the cancel shortcut (called when recording starts)
pub fn register_cancel_shortcut(app: &AppHandle) {
// Track recording lifecycle independently of the current implementation so
// switching implementations mid-recording cannot leave stale fallback state.
crate::secure_input::register_cancel_fallback(app);
- let settings = get_settings(app);
- match settings.keyboard_implementation {
- KeyboardImplementation::Tauri => tauri_impl::register_cancel_shortcut(app),
- KeyboardImplementation::HandyKeys => handy_keys::register_cancel_shortcut(app),
- }
+ CANCEL_REQUESTED.store(true, Ordering::SeqCst);
+ schedule_cancel_reconcile(app);
}
/// Unregister the cancel shortcut (called when recording stops)
pub fn unregister_cancel_shortcut(app: &AppHandle) {
crate::secure_input::unregister_cancel_fallback(app);
- let settings = get_settings(app);
- match settings.keyboard_implementation {
- KeyboardImplementation::Tauri => tauri_impl::unregister_cancel_shortcut(app),
- KeyboardImplementation::HandyKeys => handy_keys::unregister_cancel_shortcut(app),
+ CANCEL_REQUESTED.store(false, Ordering::SeqCst);
+ schedule_cancel_reconcile(app);
+}
+
+/// Apply the requested cancel state off the calling thread. Start and stop run
+/// on the shortcut handler thread, which must not block on registration, so the
+/// work is spawned. Spawned tasks may run in any order (a very short recording
+/// can see its unregister task run before its register task), so each pass
+/// applies the latest request instead of the action that scheduled it.
+fn schedule_cancel_reconcile(app: &AppHandle) {
+ let app = app.clone();
+ tauri::async_runtime::spawn(async move {
+ reconcile_cancel_shortcut(&app);
+ });
+}
+
+fn reconcile_cancel_shortcut(app: &AppHandle) {
+ // Cancel shortcut is disabled on Linux due to instability with dynamic shortcut registration
+ #[cfg(target_os = "linux")]
+ {
+ let _ = app;
+ return;
+ }
+
+ #[cfg(not(target_os = "linux"))]
+ {
+ let mut registered = CANCEL_REGISTERED.lock().unwrap_or_else(|e| e.into_inner());
+ let requested = CANCEL_REQUESTED.load(Ordering::SeqCst);
+ if requested == *registered {
+ return;
+ }
+
+ let Some(cancel_binding) = get_settings(app).bindings.get("cancel").cloned() else {
+ return;
+ };
+
+ if requested {
+ match register_shortcut(app, cancel_binding) {
+ Ok(()) => *registered = true,
+ Err(e) => error!("Failed to register cancel shortcut: {}", e),
+ }
+ } else {
+ match unregister_shortcut(app, cancel_binding) {
+ Ok(()) => *registered = false,
+ Err(e) => error!("Failed to unregister cancel shortcut: {}", e),
+ }
+ }
}
}
diff --git a/src-tauri/src/shortcut/tauri_impl.rs b/src-tauri/src/shortcut/tauri_impl.rs
index 163a9b59..ee249471 100644
--- a/src-tauri/src/shortcut/tauri_impl.rs
+++ b/src-tauri/src/shortcut/tauri_impl.rs
@@ -7,8 +7,6 @@ use log::{debug, error, warn};
use tauri::AppHandle;
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut, ShortcutState};
-#[cfg(not(target_os = "linux"))]
-use crate::settings::get_settings;
use crate::settings::{self, ShortcutBinding};
use super::handler::handle_shortcut_event;
@@ -167,49 +165,6 @@ pub fn unregister_shortcut(app: &AppHandle, binding: ShortcutBinding) -> Result<
Ok(())
}
-/// Register the cancel shortcut (called when recording starts)
-pub fn register_cancel_shortcut(app: &AppHandle) {
- // Cancel shortcut is disabled on Linux due to instability with dynamic shortcut registration
- #[cfg(target_os = "linux")]
- {
- let _ = app;
- return;
- }
-
- #[cfg(not(target_os = "linux"))]
- {
- let app_clone = app.clone();
- tauri::async_runtime::spawn(async move {
- if let Some(cancel_binding) = get_settings(&app_clone).bindings.get("cancel").cloned() {
- if let Err(e) = register_shortcut(&app_clone, cancel_binding) {
- error!("Failed to register cancel shortcut: {}", e);
- }
- }
- });
- }
-}
-
-/// Unregister the cancel shortcut (called when recording stops)
-pub fn unregister_cancel_shortcut(app: &AppHandle) {
- // Cancel shortcut is disabled on Linux due to instability with dynamic shortcut registration
- #[cfg(target_os = "linux")]
- {
- let _ = app;
- return;
- }
-
- #[cfg(not(target_os = "linux"))]
- {
- let app_clone = app.clone();
- tauri::async_runtime::spawn(async move {
- if let Some(cancel_binding) = get_settings(&app_clone).bindings.get("cancel").cloned() {
- // We ignore errors here as it might already be unregistered
- let _ = unregister_shortcut(&app_clone, cancel_binding);
- }
- });
- }
-}
-
#[cfg(test)]
mod tests {
use super::validate_shortcut;