Skip to content

armgit / Handy

Public Imported from GitHub
0

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

dont fire bindings when recording (#1811)

CJ Pais16e5d48e01fc
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 0faef85e..3dec643a 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -643,8 +643,8 @@ pub fn run(cli_args: CliArgs) {
shortcut::delete_post_process_prompt,
shortcut::set_post_process_selected_prompt,
shortcut::update_custom_words,
- shortcut::suspend_binding,
- shortcut::resume_binding,
+ shortcut::suspend_all_bindings,
+ shortcut::resume_all_bindings,
shortcut::change_mute_while_recording_setting,
shortcut::change_append_trailing_space_setting,
shortcut::change_lazy_stream_close_setting,
diff --git a/src-tauri/src/shortcut/handy_keys.rs b/src-tauri/src/shortcut/handy_keys.rs
index 4c1d0c1e..bd8e562e 100644
--- a/src-tauri/src/shortcut/handy_keys.rs
+++ b/src-tauri/src/shortcut/handy_keys.rs
@@ -540,7 +540,16 @@ pub fn start_handy_keys_recording(app: AppHandle, binding_id: String) -> Result<
let state = app
.try_state::<HandyKeysState>()
.ok_or("HandyKeysState not initialized")?;
- state.start_recording(&app, binding_id)
+
+ // Suspend every registered shortcut so a combo that overlaps an existing
+ // binding can't fire it (or have its keys swallowed) mid-capture.
+ super::suspend_all_shortcuts(&app);
+
+ let result = state.start_recording(&app, binding_id);
+ if result.is_err() {
+ super::resume_all_shortcuts(&app);
+ }
+ result
}
/// Stop key recording mode
@@ -555,5 +564,11 @@ pub fn stop_handy_keys_recording(app: AppHandle) -> Result<(), String> {
let state = app
.try_state::<HandyKeysState>()
.ok_or("HandyKeysState not initialized")?;
- state.stop_recording()
+
+ // Restore shortcuts from settings regardless of how recording ended.
+ // A commit has already registered the new binding via change_binding;
+ // re-registering it here fails cleanly and is ignored.
+ let result = state.stop_recording();
+ super::resume_all_shortcuts(&app);
+ result
}
diff --git a/src-tauri/src/shortcut/mod.rs b/src-tauri/src/shortcut/mod.rs
index 20817a22..8fb2bb40 100644
--- a/src-tauri/src/shortcut/mod.rs
+++ b/src-tauri/src/shortcut/mod.rs
@@ -13,7 +13,7 @@ mod handler;
pub mod handy_keys;
pub mod tauri_impl;
-use log::{error, info, warn};
+use log::{debug, error, info, warn};
use serde::Serialize;
use specta::Type;
use tauri::{AppHandle, Emitter, Manager};
@@ -176,17 +176,19 @@ pub fn change_binding(
if let Err(e) = validate_shortcut_for_implementation(&binding, settings.keyboard_implementation)
{
warn!("change_binding validation error: {}", e);
+ restore_registration(&app, &binding_to_modify);
return Err(e);
}
// Create an updated binding
- let mut updated_binding = binding_to_modify;
+ let mut updated_binding = binding_to_modify.clone();
updated_binding.current_binding = binding;
// Register the new binding
if let Err(e) = register_shortcut(&app, updated_binding.clone()) {
let error_msg = format!("Failed to register shortcut: {}", e);
error!("change_binding error: {}", error_msg);
+ restore_registration(&app, &binding_to_modify);
return Ok(BindingResponse {
success: false,
binding: None,
@@ -209,6 +211,17 @@ pub fn change_binding(
})
}
+/// Best-effort re-register of the previous binding after a failed change,
+/// so a failure leaves the user's shortcut working exactly as before.
+fn restore_registration(app: &AppHandle, binding: &ShortcutBinding) {
+ if let Err(e) = register_shortcut(app, binding.clone()) {
+ error!(
+ "Failed to restore previous binding '{}' ({}): {}",
+ binding.id, binding.current_binding, e
+ );
+ }
+}
+
#[tauri::command]
#[specta::specta]
pub fn reset_binding(app: AppHandle, id: String) -> Result<BindingResponse, String> {
@@ -216,30 +229,56 @@ pub fn reset_binding(app: AppHandle, id: String) -> Result<BindingResponse, Stri
change_binding(app, id, binding.default_binding)
}
-/// Temporarily unregister a binding while the user is editing it in the UI.
-/// This avoids firing the action while keys are being recorded.
-#[tauri::command]
-#[specta::specta]
-pub fn suspend_binding(app: AppHandle, id: String) -> Result<(), String> {
- if let Some(b) = settings::get_bindings(&app).get(&id).cloned() {
- if let Err(e) = unregister_shortcut(&app, b) {
- error!("suspend_binding error for id '{}': {}", id, e);
- return Err(e);
+/// Unregister every binding while the user is recording a new shortcut in
+/// the UI, so no existing shortcut can fire — or swallow the keystrokes —
+/// mid-capture. The "cancel" binding is untouched: it is managed dynamically
+/// by the recording lifecycle.
+pub fn suspend_all_shortcuts(app: &AppHandle) {
+ for (id, binding) in settings::get_bindings(app) {
+ if id == "cancel" {
+ continue;
+ }
+ if let Err(e) = unregister_shortcut(app, binding) {
+ debug!(
+ "suspend_all_shortcuts: could not unregister '{}': {}",
+ id, e
+ );
+ }
+ }
+}
+
+/// Re-register every binding from settings after shortcut recording ends.
+/// Registering an already-registered shortcut fails cleanly in both
+/// implementations, so this is idempotent and safe on every exit path.
+pub fn resume_all_shortcuts(app: &AppHandle) {
+ let settings = get_settings(app);
+ for (id, binding) in &settings.bindings {
+ if id == "cancel" {
+ continue;
+ }
+ if id == "transcribe_with_post_process" && !settings.post_process_enabled {
+ continue;
+ }
+ if let Err(e) = register_shortcut(app, binding.clone()) {
+ debug!("resume_all_shortcuts: could not register '{}': {}", id, e);
}
}
+}
+
+/// Temporarily unregister all bindings while the user is recording a
+/// shortcut in the UI. This avoids firing actions while keys are recorded.
+#[tauri::command]
+#[specta::specta]
+pub fn suspend_all_bindings(app: AppHandle) -> Result<(), String> {
+ suspend_all_shortcuts(&app);
Ok(())
}
-/// Re-register the binding after the user has finished editing.
+/// Re-register all bindings after the user has finished recording.
#[tauri::command]
#[specta::specta]
-pub fn resume_binding(app: AppHandle, id: String) -> Result<(), String> {
- if let Some(b) = settings::get_bindings(&app).get(&id).cloned() {
- if let Err(e) = register_shortcut(&app, b) {
- error!("resume_binding error for id '{}': {}", id, e);
- return Err(e);
- }
- }
+pub fn resume_all_bindings(app: AppHandle) -> Result<(), String> {
+ resume_all_shortcuts(&app);
Ok(())
}
diff --git a/src/bindings.ts b/src/bindings.ts
index 3447eea7..e7ef242e 100644
--- a/src/bindings.ts
+++ b/src/bindings.ts
@@ -297,23 +297,23 @@ async updateCustomWords(words: string[]) : Promise<Result<null, string>> {
}
},
/**
- * Temporarily unregister a binding while the user is editing it in the UI.
- * This avoids firing the action while keys are being recorded.
+ * Temporarily unregister all bindings while the user is recording a
+ * shortcut in the UI. This avoids firing actions while keys are recorded.
*/
-async suspendBinding(id: string) : Promise<Result<null, string>> {
+async suspendAllBindings() : Promise<Result<null, string>> {
try {
- return { status: "ok", data: await TAURI_INVOKE("suspend_binding", { id }) };
+ return { status: "ok", data: await TAURI_INVOKE("suspend_all_bindings") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
/**
- * Re-register the binding after the user has finished editing.
+ * Re-register all bindings after the user has finished recording.
*/
-async resumeBinding(id: string) : Promise<Result<null, string>> {
+async resumeAllBindings() : Promise<Result<null, string>> {
try {
- return { status: "ok", data: await TAURI_INVOKE("resume_binding", { id }) };
+ return { status: "ok", data: await TAURI_INVOKE("resume_all_bindings") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
diff --git a/src/components/settings/GlobalShortcutInput.tsx b/src/components/settings/GlobalShortcutInput.tsx
index 537a782a..71997fc2 100644
--- a/src/components/settings/GlobalShortcutInput.tsx
+++ b/src/components/settings/GlobalShortcutInput.tsx
@@ -124,6 +124,10 @@ export const GlobalShortcutInput: React.FC<GlobalShortcutInputProps> = ({
}
}
+ // Re-register all bindings (the one just committed is already
+ // registered; re-registering it fails cleanly and is ignored)
+ await commands.resumeAllBindings().catch(console.error);
+
// Exit editing mode and reset states
setEditingShortcutId(null);
setKeyPressed([]);
@@ -146,9 +150,8 @@ export const GlobalShortcutInput: React.FC<GlobalShortcutInputProps> = ({
console.error("Failed to restore original binding:", error);
toast.error(t("settings.general.shortcut.errors.restore"));
}
- } else if (editingShortcutId) {
- commands.resumeBinding(editingShortcutId).catch(console.error);
}
+ await commands.resumeAllBindings().catch(console.error);
setEditingShortcutId(null);
setKeyPressed([]);
setRecordedKeys([]);
@@ -180,8 +183,9 @@ export const GlobalShortcutInput: React.FC<GlobalShortcutInputProps> = ({
const startRecording = async (id: string) => {
if (editingShortcutId === id) return; // Already editing this shortcut
- // Suspend current binding to avoid firing while recording
- await commands.suspendBinding(id).catch(console.error);
+ // Suspend all bindings so no shortcut fires (or swallows the
+ // keystrokes) while keys are being recorded
+ await commands.suspendAllBindings().catch(console.error);
// Store the original binding to restore if canceled
setOriginalBinding(bindings[id]?.current_binding || "");