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,37 +9,40 @@ 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;
impl ShortcutAction for TranscribeAction {
fn start(&self, app: &AppHandle, binding_id: &str, _shortcut_str: &str) {
let binding_id = binding_id.to_string();
change_tray_icon(app, TrayIconState::Recording); change_tray_icon(app, TrayIconState::Recording);
let rm = app.state::<Arc<AudioRecordingManager>>(); let rm = app.state::<Arc<AudioRecordingManager>>();
rm.try_start_recording("transcribe"); rm.try_start_recording(&binding_id);
} }
fn transcribe_released_action(app: &AppHandle, _shortcut_str: &str) { fn stop(&self, app: &AppHandle, binding_id: &str, _shortcut_str: &str) {
let ah = app.clone(); let ah = app.clone();
let rm = Arc::clone(&app.state::<Arc<AudioRecordingManager>>()); let rm = Arc::clone(&app.state::<Arc<AudioRecordingManager>>());
let tm = Arc::clone(&app.state::<Arc<TranscriptionManager>>()); let tm = Arc::clone(&app.state::<Arc<TranscriptionManager>>());
change_tray_icon(app, TrayIconState::Idle); change_tray_icon(app, TrayIconState::Idle);
let binding_id = binding_id.to_string(); // Clone binding_id for the async task
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
if let Some(samples) = rm.stop_recording("transcribe") { let binding_id = binding_id.clone(); // Clone for the inner async task
if let Some(samples) = rm.stop_recording(&binding_id) {
match tm.transcribe(samples) { match tm.transcribe(samples) {
Ok(transcription) => { Ok(transcription) => {
println!("Global Shortcut Transcription: {}", transcription); println!("Global Shortcut Transcription: {}", transcription);
if transcription != "" { if !transcription.is_empty() {
utils::paste(transcription, ah); utils::paste(transcription, ah);
} }
} }
@ -48,39 +51,41 @@ fn transcribe_released_action(app: &AppHandle, _shortcut_str: &str) {
} }
}); });
} }
}
fn test_binding_pressed_action(app: &AppHandle, shortcut_str: &str) { // Test Action
struct TestAction;
impl ShortcutAction for TestAction {
fn start(&self, app: &AppHandle, binding_id: &str, shortcut_str: &str) {
println!( println!(
"Shortcut ID 'test': Pressed - {} (App: {})", "Shortcut ID '{}': Started - {} (App: {})", // Changed "Pressed" to "Started" for consistency
binding_id,
shortcut_str, shortcut_str,
app.package_info().name app.package_info().name
); );
} }
fn test_binding_released_action(app: &AppHandle, shortcut_str: &str) { fn stop(&self, app: &AppHandle, binding_id: &str, shortcut_str: &str) {
println!( println!(
"Shortcut ID 'test': Released - {} (App: {})", "Shortcut ID '{}': Stopped - {} (App: {})", // Changed "Released" to "Stopped" for consistency
binding_id,
shortcut_str, shortcut_str,
app.package_info().name 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
} }
} }