clean up code a bit

This commit is contained in:
CJ Pais 2025-05-14 17:22:37 -07:00
parent ef607db260
commit e7cdecf7f0
2 changed files with 69 additions and 58 deletions

View file

@ -9,78 +9,83 @@ use std::sync::Arc;
use tauri::AppHandle; use tauri::AppHandle;
use tauri::Manager; use tauri::Manager;
// Action Handler Types // Shortcut Action Trait
pub type PressAction = fn(app: &AppHandle, shortcut_str: &str); pub trait ShortcutAction: Send + Sync {
pub type ReleaseAction = fn(app: &AppHandle, shortcut_str: &str); fn start(&self, app: &AppHandle, binding_id: &str, shortcut_str: &str);
fn stop(&self, app: &AppHandle, binding_id: &str, shortcut_str: &str);
// TODO refactor to start and stop
pub struct ActionSet {
pub press: PressAction,
pub release: ReleaseAction,
} }
// Handler Functions // Transcribe Action
fn transcribe_pressed_action(app: &AppHandle, _shortcut_str: &str) { struct TranscribeAction;
change_tray_icon(app, TrayIconState::Recording);
let rm = app.state::<Arc<AudioRecordingManager>>(); impl ShortcutAction for TranscribeAction {
rm.try_start_recording("transcribe"); fn start(&self, app: &AppHandle, binding_id: &str, _shortcut_str: &str) {
} let binding_id = binding_id.to_string();
change_tray_icon(app, TrayIconState::Recording);
fn transcribe_released_action(app: &AppHandle, _shortcut_str: &str) { let rm = app.state::<Arc<AudioRecordingManager>>();
let ah = app.clone(); rm.try_start_recording(&binding_id);
let rm = Arc::clone(&app.state::<Arc<AudioRecordingManager>>()); }
let tm = Arc::clone(&app.state::<Arc<TranscriptionManager>>());
change_tray_icon(app, TrayIconState::Idle); fn stop(&self, app: &AppHandle, binding_id: &str, _shortcut_str: &str) {
let ah = app.clone();
let rm = Arc::clone(&app.state::<Arc<AudioRecordingManager>>());
let tm = Arc::clone(&app.state::<Arc<TranscriptionManager>>());
tauri::async_runtime::spawn(async move { change_tray_icon(app, TrayIconState::Idle);
if let Some(samples) = rm.stop_recording("transcribe") {
match tm.transcribe(samples) { let binding_id = binding_id.to_string(); // Clone binding_id for the async task
Ok(transcription) => {
println!("Global Shortcut Transcription: {}", transcription); tauri::async_runtime::spawn(async move {
if transcription != "" { let binding_id = binding_id.clone(); // Clone for the inner async task
utils::paste(transcription, ah); if let Some(samples) = rm.stop_recording(&binding_id) {
match tm.transcribe(samples) {
Ok(transcription) => {
println!("Global Shortcut Transcription: {}", transcription);
if !transcription.is_empty() {
utils::paste(transcription, ah);
}
} }
Err(err) => println!("Global Shortcut Transcription error: {}", err),
} }
Err(err) => println!("Global Shortcut Transcription error: {}", err),
} }
} });
}); }
} }
fn test_binding_pressed_action(app: &AppHandle, shortcut_str: &str) { // Test Action
println!( struct TestAction;
"Shortcut ID 'test': Pressed - {} (App: {})",
shortcut_str,
app.package_info().name
);
}
fn test_binding_released_action(app: &AppHandle, shortcut_str: &str) { impl ShortcutAction for TestAction {
println!( fn start(&self, app: &AppHandle, binding_id: &str, shortcut_str: &str) {
"Shortcut ID 'test': Released - {} (App: {})", println!(
shortcut_str, "Shortcut ID '{}': Started - {} (App: {})", // Changed "Pressed" to "Started" for consistency
app.package_info().name binding_id,
); shortcut_str,
app.package_info().name
);
}
fn stop(&self, app: &AppHandle, binding_id: &str, shortcut_str: &str) {
println!(
"Shortcut ID '{}': Stopped - {} (App: {})", // Changed "Released" to "Stopped" for consistency
binding_id,
shortcut_str,
app.package_info().name
);
}
} }
// Static Action Map // Static Action Map
pub static ACTION_MAP: Lazy<HashMap<String, ActionSet>> = Lazy::new(|| { pub static ACTION_MAP: Lazy<HashMap<String, Arc<dyn ShortcutAction>>> = Lazy::new(|| {
let mut map = HashMap::new(); let mut map = HashMap::new();
map.insert( map.insert(
"transcribe".to_string(), "transcribe".to_string(),
ActionSet { Arc::new(TranscribeAction) as Arc<dyn ShortcutAction>,
press: transcribe_pressed_action,
release: transcribe_released_action,
},
); );
map.insert( map.insert(
"test".to_string(), "test".to_string(),
ActionSet { Arc::new(TestAction) as Arc<dyn ShortcutAction>,
press: test_binding_pressed_action,
release: test_binding_released_action,
},
); );
map map
}); });

View file

@ -3,7 +3,7 @@ use tauri::{App, AppHandle, Manager};
use tauri_plugin_global_shortcut::GlobalShortcutExt; use tauri_plugin_global_shortcut::GlobalShortcutExt;
use tauri_plugin_global_shortcut::{Shortcut, ShortcutState}; use tauri_plugin_global_shortcut::{Shortcut, ShortcutState};
use crate::actions::ACTION_MAP; use crate::actions::{ShortcutAction, ACTION_MAP};
use crate::settings::ShortcutBinding; use crate::settings::ShortcutBinding;
use crate::settings::{self, get_settings}; use crate::settings::{self, get_settings};
use crate::ManagedToggleState; use crate::ManagedToggleState;
@ -119,12 +119,12 @@ fn _register_shortcut(app: &AppHandle, binding: ShortcutBinding) -> Result<(), S
let shortcut_string = scut.into_string(); let shortcut_string = scut.into_string();
let settings = get_settings(ah); let settings = get_settings(ah);
if let Some(action_set) = ACTION_MAP.get(&binding_id_for_closure) { if let Some(action) = ACTION_MAP.get(&binding_id_for_closure) {
if settings.push_to_talk { if settings.push_to_talk {
if event.state == ShortcutState::Pressed { if event.state == ShortcutState::Pressed {
(action_set.press)(ah, &shortcut_string); action.start(ah, &binding_id_for_closure, &shortcut_string);
} else if event.state == ShortcutState::Released { } else if event.state == ShortcutState::Released {
(action_set.release)(ah, &shortcut_string); action.stop(ah, &binding_id_for_closure, &shortcut_string);
} }
} else { } else {
if event.state == ShortcutState::Pressed { if event.state == ShortcutState::Pressed {
@ -132,13 +132,19 @@ fn _register_shortcut(app: &AppHandle, binding: ShortcutBinding) -> Result<(), S
let mut states = toggle_state_manager.lock().expect("Failed to lock toggle state manager"); let mut states = toggle_state_manager.lock().expect("Failed to lock toggle state manager");
let is_currently_active = states.active_toggles.entry(binding_id_for_closure.clone()).or_insert(false); let is_currently_active = states.active_toggles
.entry(binding_id_for_closure.clone())
.or_insert(false);
if *is_currently_active { if *is_currently_active {
(action_set.release)(ah, &shortcut_string); action.stop(
ah,
&binding_id_for_closure,
&shortcut_string,
);
*is_currently_active = false; // Update state to inactive *is_currently_active = false; // Update state to inactive
} else { } else {
(action_set.press)(ah, &shortcut_string); action.start(ah, &binding_id_for_closure, &shortcut_string);
*is_currently_active = true; // Update state to active *is_currently_active = true; // Update state to active
} }
} }