From f8c5875fa8931a6a08916b9602db8c7dcbccf502 Mon Sep 17 00:00:00 2001 From: CJ Pais Date: Fri, 9 May 2025 12:12:44 -0700 Subject: [PATCH] refactored actions. --- src-tauri/src/actions.rs | 106 ++++++++++++++++++++++++++++++++++++ src-tauri/src/lib.rs | 1 + src-tauri/src/shortcut.rs | 111 +------------------------------------- 3 files changed, 109 insertions(+), 109 deletions(-) create mode 100644 src-tauri/src/actions.rs diff --git a/src-tauri/src/actions.rs b/src-tauri/src/actions.rs new file mode 100644 index 0000000..23ecd4f --- /dev/null +++ b/src-tauri/src/actions.rs @@ -0,0 +1,106 @@ +use crate::managers::audio::AudioRecordingManager; +use crate::managers::transcription::TranscriptionManager; +use crate::utils; +use once_cell::sync::Lazy; +use std::collections::HashMap; +use std::sync::Arc; +use tauri::image::Image; +use tauri::tray::TrayIcon; +use tauri::AppHandle; +use tauri::Manager; + +// Action Handler Types +pub type PressAction = fn(app: &AppHandle, shortcut_str: &str); +pub type ReleaseAction = fn(app: &AppHandle, shortcut_str: &str); + +pub struct ActionSet { + pub press: PressAction, + pub release: ReleaseAction, +} + +// Handler Functions +fn transcribe_pressed_action(app: &AppHandle, _shortcut_str: &str) { + let tray = app.state::(); + tray.set_icon(Some( + Image::from_path( + app.path() + .resolve( + "resources/tray_recording_64x64.png", + tauri::path::BaseDirectory::Resource, + ) + .expect("failed to resolve"), + ) + .expect("failed to set icon"), + )); + + let rm = app.state::>(); + rm.try_start_recording("transcribe"); +} + +fn transcribe_released_action(app: &AppHandle, _shortcut_str: &str) { + let tray = app.state::(); + tray.set_icon(Some( + Image::from_path( + app.path() + .resolve( + "resources/tray_64x64.png", + tauri::path::BaseDirectory::Resource, + ) + .expect("failed to resolve"), + ) + .expect("failed to set icon"), + )); + + let ah = app.clone(); + let rm = Arc::clone(&app.state::>()); + let tm = Arc::clone(&app.state::>()); + + tauri::async_runtime::spawn(async move { + if let Some(samples) = rm.stop_recording("transcribe") { + match tm.transcribe(samples) { + // Not .await, as transcribe is synchronous + Ok(transcription) => { + println!("Global Shortcut Transcription: {}", transcription); + utils::paste(transcription, ah); + } + Err(err) => println!("Global Shortcut Transcription error: {}", err), + } + } + }); +} + +fn test_binding_pressed_action(app: &AppHandle, shortcut_str: &str) { + println!( + "Shortcut ID 'test': Pressed - {} (App: {})", + shortcut_str, + app.package_info().name + ); +} + +fn test_binding_released_action(app: &AppHandle, shortcut_str: &str) { + println!( + "Shortcut ID 'test': Released - {} (App: {})", + shortcut_str, + app.package_info().name + ); +} + +// Static Action Map +pub static ACTION_MAP: Lazy> = Lazy::new(|| { + let mut map = HashMap::new(); + map.insert( + "transcribe".to_string(), + ActionSet { + press: transcribe_pressed_action, + release: transcribe_released_action, + }, + ); + map.insert( + "test".to_string(), + ActionSet { + press: test_binding_pressed_action, + release: test_binding_released_action, + }, + ); + map +}); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e7ef72f..92f312b 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,3 +1,4 @@ +mod actions; mod managers; mod settings; mod shortcut; diff --git a/src-tauri/src/shortcut.rs b/src-tauri/src/shortcut.rs index 69a3858..508ced6 100644 --- a/src-tauri/src/shortcut.rs +++ b/src-tauri/src/shortcut.rs @@ -1,118 +1,11 @@ -use std::collections::HashMap; -use std::sync::Arc; - -use once_cell::sync::Lazy; use serde::Serialize; -use tauri::image::Image; -use tauri::tray::TrayIcon; -use tauri::App; -use tauri::AppHandle; -use tauri::Manager; +use tauri::{App, AppHandle}; use tauri_plugin_global_shortcut::GlobalShortcutExt; use tauri_plugin_global_shortcut::{Shortcut, ShortcutState}; -use crate::managers::audio::AudioRecordingManager; -use crate::managers::transcription::TranscriptionManager; +use crate::actions::ACTION_MAP; use crate::settings; use crate::settings::ShortcutBinding; -use crate::utils; - -// Action Handler Types -pub type PressAction = fn(app: &AppHandle, shortcut_str: &str); -pub type ReleaseAction = fn(app: &AppHandle, shortcut_str: &str); - -pub struct ActionSet { - pub press: PressAction, - pub release: ReleaseAction, -} - -// Handler Functions -fn transcribe_pressed_action(app: &AppHandle, _shortcut_str: &str) { - let tray = app.state::(); - tray.set_icon(Some( - Image::from_path( - app.path() - .resolve( - "resources/tray_recording_64x64.png", - tauri::path::BaseDirectory::Resource, - ) - .expect("failed to resolve"), - ) - .expect("failed to set icon"), - )); - - let rm = app.state::>(); - rm.try_start_recording("transcribe"); -} - -fn transcribe_released_action(app: &AppHandle, _shortcut_str: &str) { - let tray = app.state::(); - tray.set_icon(Some( - Image::from_path( - app.path() - .resolve( - "resources/tray_64x64.png", - tauri::path::BaseDirectory::Resource, - ) - .expect("failed to resolve"), - ) - .expect("failed to set icon"), - )); - - let ah = app.clone(); - let rm = Arc::clone(&app.state::>()); - let tm = Arc::clone(&app.state::>()); - - tauri::async_runtime::spawn(async move { - if let Some(samples) = rm.stop_recording("transcribe") { - match tm.transcribe(samples) { - // Not .await, as transcribe is synchronous - Ok(transcription) => { - println!("Global Shortcut Transcription: {}", transcription); - utils::paste(transcription, ah); - } - Err(err) => println!("Global Shortcut Transcription error: {}", err), - } - } - }); -} - -fn test_binding_pressed_action(app: &AppHandle, shortcut_str: &str) { - println!( - "Shortcut ID 'test': Pressed - {} (App: {})", - shortcut_str, - app.package_info().name - ); -} - -fn test_binding_released_action(app: &AppHandle, shortcut_str: &str) { - println!( - "Shortcut ID 'test': Released - {} (App: {})", - shortcut_str, - app.package_info().name - ); -} - -// Static Action Map -pub static ACTION_MAP: Lazy> = Lazy::new(|| { - let mut map = HashMap::new(); - map.insert( - "transcribe".to_string(), - ActionSet { - press: transcribe_pressed_action, - release: transcribe_released_action, - }, - ); - map.insert( - "test".to_string(), - ActionSet { - press: test_binding_pressed_action, - release: test_binding_released_action, - }, - ); - // --- DEVELOPER ADDS NEW ACTIONS HERE --- - map -}); pub fn init_shortcuts(app: &App) { let settings = settings::load_or_create_app_settings(app);