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: reject shortcuts the Tauri backend cannot parse (#2158)

Aktan Azatf5c27e69f4d4
diff --git a/src-tauri/src/shortcut/tauri_impl.rs b/src-tauri/src/shortcut/tauri_impl.rs
index 6e19c03c..163a9b59 100644
--- a/src-tauri/src/shortcut/tauri_impl.rs
+++ b/src-tauri/src/shortcut/tauri_impl.rs
@@ -62,11 +62,17 @@ pub fn validate_shortcut(raw: &str) -> Result<(), String> {
// Check for at least one non-modifier key
let has_non_modifier = parts.iter().any(|part| !modifiers.contains(&part.as_str()));
- if has_non_modifier {
- Ok(())
- } else {
- Err("Tauri shortcuts must include a main key (letter, number, F-key, etc.) in addition to modifiers".into())
+ if !has_non_modifier {
+ return Err("Tauri shortcuts must include a main key (letter, number, F-key, etc.) in addition to modifiers".into());
}
+
+ // The name check above passes side-specific modifiers such as
+ // `option_left`, which the handy-keys recorder saves but the accelerator
+ // parser rejects. Parse here so a binding carried over from handy-keys is
+ // reset to the default instead of failing to register.
+ raw.parse::<Shortcut>()
+ .map(|_| ())
+ .map_err(|e| format!("Failed to parse shortcut '{}': {}", raw, e))
}
/// Register a shortcut using Tauri's global-shortcut plugin
@@ -203,3 +209,29 @@ pub fn unregister_cancel_shortcut(app: &AppHandle) {
});
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::validate_shortcut;
+
+ #[test]
+ fn rejects_side_specific_modifiers_the_parser_cannot_register() {
+ for raw in ["option_left+space", "ctrl_right+space"] {
+ assert!(validate_shortcut(raw).is_err(), "{raw} should be rejected");
+ }
+ }
+
+ #[test]
+ fn accepts_the_default_shortcuts() {
+ for raw in [
+ "option+space",
+ "option+shift+space",
+ "ctrl+space",
+ "ctrl+shift+space",
+ "alt+space",
+ "escape",
+ ] {
+ assert_eq!(validate_shortcut(raw), Ok(()), "{raw} should be accepted");
+ }
+ }
+}