refactored actions.

This commit is contained in:
CJ Pais 2025-05-09 12:12:44 -07:00
parent 5043033881
commit f8c5875fa8
3 changed files with 109 additions and 109 deletions

106
src-tauri/src/actions.rs Normal file
View file

@ -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::<TrayIcon>();
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::<Arc<AudioRecordingManager>>();
rm.try_start_recording("transcribe");
}
fn transcribe_released_action(app: &AppHandle, _shortcut_str: &str) {
let tray = app.state::<TrayIcon>();
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::<Arc<AudioRecordingManager>>());
let tm = Arc::clone(&app.state::<Arc<TranscriptionManager>>());
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<HashMap<String, ActionSet>> = 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
});

View file

@ -1,3 +1,4 @@
mod actions;
mod managers;
mod settings;
mod shortcut;

View file

@ -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::<TrayIcon>();
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::<Arc<AudioRecordingManager>>();
rm.try_start_recording("transcribe");
}
fn transcribe_released_action(app: &AppHandle, _shortcut_str: &str) {
let tray = app.state::<TrayIcon>();
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::<Arc<AudioRecordingManager>>());
let tm = Arc::clone(&app.state::<Arc<TranscriptionManager>>());
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<HashMap<String, ActionSet>> = 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);