Skip to content

armgit / Handy

Public Imported from GitHub
0

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

Fixed bindings blowing up.

CJ Paisb47a89716476
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index c9089dd6..cdb9c4c9 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -20,12 +20,7 @@ fn try_send_event(event: &EventType) {
fn send(event: EventType) {
try_send_event(&event);
- thread::sleep(time::Duration::from_millis(20));
-}
-
-fn send_multiple(events: &[EventType]) {
- events.iter().for_each(try_send_event);
- thread::sleep(time::Duration::from_millis(20));
+ thread::sleep(time::Duration::from_millis(30));
}
fn send_paste() {
@@ -39,14 +34,9 @@ fn send_paste() {
send(EventType::KeyPress(modifier_key));
send(EventType::KeyPress(Key::KeyV));
- // Release both keys simultaneously
- send_multiple(&[
- EventType::KeyRelease(Key::KeyV),
- EventType::KeyRelease(modifier_key),
- ]);
-
- // Additional delay after the complete combination
- thread::sleep(time::Duration::from_millis(20));
+ // Release both keys
+ send(EventType::KeyRelease(Key::KeyV));
+ send(EventType::KeyRelease(modifier_key));
}
fn send_copy() {
@@ -60,11 +50,9 @@ fn send_copy() {
send(EventType::KeyPress(modifier_key));
send(EventType::KeyPress(Key::KeyC));
- // Release both keys simultaneously
- send_multiple(&[
- EventType::KeyRelease(Key::KeyC),
- EventType::KeyRelease(modifier_key),
- ]);
+ // Release both keys
+ send(EventType::KeyRelease(Key::KeyC));
+ send(EventType::KeyRelease(modifier_key));
}
fn paste(text: String, app_handle: tauri::AppHandle) {
diff --git a/src-tauri/src/managers/keybinding.rs b/src-tauri/src/managers/keybinding.rs
index b61b6671..44fcdad0 100644
--- a/src-tauri/src/managers/keybinding.rs
+++ b/src-tauri/src/managers/keybinding.rs
@@ -1,12 +1,11 @@
use super::audio::AudioRecordingManager;
use super::transcription::TranscriptionManager;
-use rdev::EventType;
-use rig::agent::Agent;
+use rdev::{EventType, Key};
use rig::providers::anthropic;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
-type KeySet = HashSet<rdev::Key>;
+type KeySet = HashSet<Key>;
#[derive(Clone)]
pub struct BindingContext {
@@ -25,11 +24,10 @@ pub struct KeyBinding {
on_release: Box<
dyn Fn(&BindingContext) -> Option<tauri::async_runtime::JoinHandle<()>> + Send + 'static,
>,
- currently_pressed: Arc<Mutex<KeySet>>,
}
impl KeyBinding {
- fn new<F, G>(id: String, keys: Vec<rdev::Key>, on_press: F, on_release: G) -> Self
+ fn new<F, G>(id: String, keys: Vec<Key>, on_press: F, on_release: G) -> Self
where
F: Fn(&BindingContext) -> Option<tauri::async_runtime::JoinHandle<()>> + Send + 'static,
G: Fn(&BindingContext) -> Option<tauri::async_runtime::JoinHandle<()>> + Send + 'static,
@@ -39,52 +37,14 @@ impl KeyBinding {
keys: keys.into_iter().collect(),
on_press: Box::new(on_press),
on_release: Box::new(on_release),
- currently_pressed: Arc::new(Mutex::new(HashSet::new())),
}
}
-
- fn handle_event(&self, key: rdev::Key, is_press: bool, context: &BindingContext) -> bool {
- let mut pressed = self.currently_pressed.lock().unwrap();
-
- if is_press {
- if !self.keys.contains(&key) {
- return false; // Ignore keys that aren't part of this binding
- }
-
- if pressed.contains(&key) {
- return false;
- }
-
- pressed.insert(key);
- if pressed.len() == self.keys.len() && pressed.is_subset(&self.keys) {
- let _ = (self.on_press)(context);
- return true;
- }
- } else {
- // Release event
- if !self.keys.contains(&key) {
- return false;
- }
-
- if pressed.remove(&key) {
- if pressed.len() == self.keys.len() - 1 {
- let _ = (self.on_release)(context);
-
- // Clear the currently_pressed set if all keys are released
- if pressed.is_empty() {
- pressed.clear();
- }
-
- return true;
- }
- }
- }
- false
- }
}
pub struct KeyBindingManager {
bindings: Vec<KeyBinding>,
+ global_pressed: Arc<Mutex<HashSet<Key>>>,
+ active_bindings: Arc<Mutex<HashSet<String>>>,
context: BindingContext,
}
@@ -97,6 +57,8 @@ impl KeyBindingManager {
) -> Self {
Self {
bindings: Vec::new(),
+ global_pressed: Arc::new(Mutex::new(HashSet::new())),
+ active_bindings: Arc::new(Mutex::new(HashSet::new())),
context: BindingContext {
recording_manager,
transcription_manager,
@@ -106,7 +68,7 @@ impl KeyBindingManager {
}
}
- pub fn register<F, G>(&mut self, id: String, keys: Vec<rdev::Key>, on_press: F, on_release: G)
+ pub fn register<F, G>(&mut self, id: String, keys: Vec<Key>, on_press: F, on_release: G)
where
F: Fn(&BindingContext) -> Option<tauri::async_runtime::JoinHandle<()>> + Send + 'static,
G: Fn(&BindingContext) -> Option<tauri::async_runtime::JoinHandle<()>> + Send + 'static,
@@ -117,12 +79,51 @@ impl KeyBindingManager {
pub fn handle_event(&self, event: &rdev::Event) {
if let EventType::KeyPress(key) | EventType::KeyRelease(key) = event.event_type {
- let is_press = matches!(event.event_type, EventType::KeyPress(_));
+ let mut is_press = matches!(event.event_type, EventType::KeyPress(_));
+
+ // Update global pressed keys
+ let mut pressed = self.global_pressed.lock().unwrap();
- // Only process the first binding that successfully handles the event
+ // If we get a release event for a key that wasn't pressed,
+ // treat it as a press event instead
+ if !is_press && !pressed.contains(&key) {
+ is_press = true;
+ }
+
+ if is_press {
+ pressed.insert(key);
+ } else {
+ pressed.remove(&key);
+ }
+ let pressed_snapshot = pressed.clone();
+ drop(pressed);
+
+ // Check binding states
+ let mut active = self.active_bindings.lock().unwrap();
for binding in &self.bindings {
- if binding.handle_event(key, is_press, &self.context) {
- break; // Exit after first successful handling
+ let is_active = active.contains(&binding.id);
+ let all_required_pressed = binding.keys.is_subset(&pressed_snapshot);
+ let any_required_key = binding.keys.contains(&key);
+
+ // Only process if this event is for a key we care about
+ if any_required_key {
+ match (all_required_pressed, is_active) {
+ (true, false) => {
+ (binding.on_press)(&self.context);
+ active.insert(binding.id.clone());
+ }
+ (false, true) => {
+ // Only trigger release if none of the required keys are pressed
+ let any_binding_key_pressed =
+ binding.keys.iter().any(|k| pressed_snapshot.contains(k));
+
+ if !any_binding_key_pressed {
+ (binding.on_release)(&self.context);
+ active.remove(&binding.id);
+ }
+ }
+ _ => {}
+ }
}
}
}
diff --git a/src-tauri/src/managers/transcription.rs b/src-tauri/src/managers/transcription.rs
index a4114ab1..ab90b92d 100644
--- a/src-tauri/src/managers/transcription.rs
+++ b/src-tauri/src/managers/transcription.rs
@@ -63,7 +63,6 @@ impl TranscriptionManager {
.full_get_segment_text(i)
.expect("failed to get segment");
result.push_str(&segment);
- result.push(' ');
}
let et = std::time::Instant::now();