mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-12 17:01:36 +00:00
Replaced manual enum display logic with strum_macros.
This commit is contained in:
parent
89b9e1469d
commit
089da12b23
13
runtime/Cargo.lock
generated
13
runtime/Cargo.lock
generated
@ -2772,6 +2772,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
|
"strum_macros",
|
||||||
"sys-locale",
|
"sys-locale",
|
||||||
"tauri",
|
"tauri",
|
||||||
"tauri-build",
|
"tauri-build",
|
||||||
@ -4767,6 +4768,18 @@ version = "0.11.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strum_macros"
|
||||||
|
version = "0.27.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7695ce3845ea4b33927c055a39dc438a45b059f7c1b3d91d38d10355fb8cbca7"
|
||||||
|
dependencies = [
|
||||||
|
"heck 0.5.0",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.93",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "subtle"
|
name = "subtle"
|
||||||
version = "2.6.1"
|
version = "2.6.1"
|
||||||
|
|||||||
@ -39,6 +39,7 @@ pdfium-render = "0.8.34"
|
|||||||
sys-locale = "0.3.2"
|
sys-locale = "0.3.2"
|
||||||
cfg-if = "1.0.1"
|
cfg-if = "1.0.1"
|
||||||
pptx-to-md = "0.4.0"
|
pptx-to-md = "0.4.0"
|
||||||
|
strum_macros = "0.27"
|
||||||
|
|
||||||
# Fixes security vulnerability downstream, where the upstream is not fixed yet:
|
# Fixes security vulnerability downstream, where the upstream is not fixed yet:
|
||||||
url = "2.5.7"
|
url = "2.5.7"
|
||||||
|
|||||||
@ -8,6 +8,7 @@ use rocket::response::stream::TextStream;
|
|||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use rocket::serde::Serialize;
|
use rocket::serde::Serialize;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use strum_macros::Display;
|
||||||
use tauri::updater::UpdateResponse;
|
use tauri::updater::UpdateResponse;
|
||||||
use tauri::{FileDropEvent, GlobalShortcutManager, UpdaterEvent, RunEvent, Manager, PathResolver, Window, WindowEvent};
|
use tauri::{FileDropEvent, GlobalShortcutManager, UpdaterEvent, RunEvent, Manager, PathResolver, Window, WindowEvent};
|
||||||
use tauri::api::dialog::blocking::FileDialogBuilder;
|
use tauri::api::dialog::blocking::FileDialogBuilder;
|
||||||
@ -32,22 +33,13 @@ static EVENT_BROADCAST: Lazy<Mutex<Option<broadcast::Sender<Event>>>> = Lazy::ne
|
|||||||
static REGISTERED_SHORTCUTS: Lazy<Mutex<HashMap<Shortcut, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));
|
static REGISTERED_SHORTCUTS: Lazy<Mutex<HashMap<Shortcut, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));
|
||||||
|
|
||||||
/// Enum identifying global keyboard shortcuts.
|
/// Enum identifying global keyboard shortcuts.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
|
||||||
|
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
|
||||||
pub enum Shortcut {
|
pub enum Shortcut {
|
||||||
None = 0,
|
None = 0,
|
||||||
VoiceRecordingToggle,
|
VoiceRecordingToggle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Shortcut {
|
|
||||||
/// Returns the display name for logging.
|
|
||||||
pub fn display_name(&self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Shortcut::None => "none",
|
|
||||||
Shortcut::VoiceRecordingToggle => "voice_recording_toggle",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts the Tauri app.
|
/// Starts the Tauri app.
|
||||||
pub fn start_tauri() {
|
pub fn start_tauri() {
|
||||||
info!("Starting Tauri app...");
|
info!("Starting Tauri app...");
|
||||||
@ -731,7 +723,7 @@ fn register_shortcut_with_callback(
|
|||||||
// Match the shortcut registration to transform the Tauri result into the Rust result:
|
// Match the shortcut registration to transform the Tauri result into the Rust result:
|
||||||
//
|
//
|
||||||
match shortcut_manager.register(shortcut, move || {
|
match shortcut_manager.register(shortcut, move || {
|
||||||
info!(Source = "Tauri"; "Global shortcut triggered for '{}'.", shortcut_id.display_name());
|
info!(Source = "Tauri"; "Global shortcut triggered for '{}'.", shortcut_id);
|
||||||
let event = Event::new(TauriEventType::GlobalShortcutPressed, vec![shortcut_id.to_string()]);
|
let event = Event::new(TauriEventType::GlobalShortcutPressed, vec![shortcut_id.to_string()]);
|
||||||
let sender = event_sender.clone();
|
let sender = event_sender.clone();
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
@ -753,7 +745,7 @@ pub fn register_shortcut(_token: APIToken, payload: Json<RegisterShortcutRequest
|
|||||||
let id = payload.id;
|
let id = payload.id;
|
||||||
let new_shortcut = payload.shortcut.clone();
|
let new_shortcut = payload.shortcut.clone();
|
||||||
|
|
||||||
info!(Source = "Tauri"; "Registering global shortcut '{}' with key '{new_shortcut}'.", id.display_name());
|
info!(Source = "Tauri"; "Registering global shortcut '{}' with key '{new_shortcut}'.", id);
|
||||||
|
|
||||||
// Get the main window to access the global shortcut manager:
|
// Get the main window to access the global shortcut manager:
|
||||||
let main_window_lock = MAIN_WINDOW.lock().unwrap();
|
let main_window_lock = MAIN_WINDOW.lock().unwrap();
|
||||||
@ -775,7 +767,7 @@ pub fn register_shortcut(_token: APIToken, payload: Json<RegisterShortcutRequest
|
|||||||
if let Some(old_shortcut) = registered_shortcuts.get(&id) {
|
if let Some(old_shortcut) = registered_shortcuts.get(&id) {
|
||||||
if !old_shortcut.is_empty() {
|
if !old_shortcut.is_empty() {
|
||||||
match shortcut_manager.unregister(old_shortcut.as_str()) {
|
match shortcut_manager.unregister(old_shortcut.as_str()) {
|
||||||
Ok(_) => info!(Source = "Tauri"; "Unregistered old shortcut '{old_shortcut}' for '{}'.", id.display_name()),
|
Ok(_) => info!(Source = "Tauri"; "Unregistered old shortcut '{old_shortcut}' for '{}'.", id),
|
||||||
Err(error) => warn!(Source = "Tauri"; "Failed to unregister old shortcut '{old_shortcut}': {error}"),
|
Err(error) => warn!(Source = "Tauri"; "Failed to unregister old shortcut '{old_shortcut}': {error}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -784,7 +776,7 @@ pub fn register_shortcut(_token: APIToken, payload: Json<RegisterShortcutRequest
|
|||||||
// When the new shortcut is empty, we're done (just unregistering):
|
// When the new shortcut is empty, we're done (just unregistering):
|
||||||
if new_shortcut.is_empty() {
|
if new_shortcut.is_empty() {
|
||||||
registered_shortcuts.remove(&id);
|
registered_shortcuts.remove(&id);
|
||||||
info!(Source = "Tauri"; "Shortcut '{}' has been disabled.", id.display_name());
|
info!(Source = "Tauri"; "Shortcut '{}' has been disabled.", id);
|
||||||
return Json(ShortcutResponse {
|
return Json(ShortcutResponse {
|
||||||
success: true,
|
success: true,
|
||||||
error_message: String::new(),
|
error_message: String::new(),
|
||||||
@ -809,7 +801,7 @@ pub fn register_shortcut(_token: APIToken, payload: Json<RegisterShortcutRequest
|
|||||||
// Register the new shortcut:
|
// Register the new shortcut:
|
||||||
match register_shortcut_with_callback(&mut shortcut_manager, &new_shortcut, id, event_sender) {
|
match register_shortcut_with_callback(&mut shortcut_manager, &new_shortcut, id, event_sender) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
info!(Source = "Tauri"; "Global shortcut '{new_shortcut}' registered successfully for '{}'.", id.display_name());
|
info!(Source = "Tauri"; "Global shortcut '{new_shortcut}' registered successfully for '{}'.", id);
|
||||||
registered_shortcuts.insert(id, new_shortcut);
|
registered_shortcuts.insert(id, new_shortcut);
|
||||||
Json(ShortcutResponse {
|
Json(ShortcutResponse {
|
||||||
success: true,
|
success: true,
|
||||||
@ -869,7 +861,7 @@ pub fn validate_shortcut(_token: APIToken, payload: Json<ValidateShortcutRequest
|
|||||||
is_valid: true,
|
is_valid: true,
|
||||||
error_message: String::new(),
|
error_message: String::new(),
|
||||||
has_conflict: true,
|
has_conflict: true,
|
||||||
conflict_description: format!("Already used by: {}", name.display_name()),
|
conflict_description: format!("Already used by: {}", name),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -924,8 +916,8 @@ pub fn suspend_shortcuts(_token: APIToken) -> Json<ShortcutResponse> {
|
|||||||
for (name, shortcut) in registered_shortcuts.iter() {
|
for (name, shortcut) in registered_shortcuts.iter() {
|
||||||
if !shortcut.is_empty() {
|
if !shortcut.is_empty() {
|
||||||
match shortcut_manager.unregister(shortcut.as_str()) {
|
match shortcut_manager.unregister(shortcut.as_str()) {
|
||||||
Ok(_) => info!(Source = "Tauri"; "Temporarily unregistered shortcut '{shortcut}' for '{}'.", name.display_name()),
|
Ok(_) => info!(Source = "Tauri"; "Temporarily unregistered shortcut '{shortcut}' for '{}'.", name),
|
||||||
Err(error) => warn!(Source = "Tauri"; "Failed to unregister shortcut '{shortcut}' for '{}': {error}", name.display_name()),
|
Err(error) => warn!(Source = "Tauri"; "Failed to unregister shortcut '{shortcut}' for '{}': {error}", name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -980,11 +972,11 @@ pub fn resume_shortcuts(_token: APIToken) -> Json<ShortcutResponse> {
|
|||||||
|
|
||||||
match register_shortcut_with_callback(&mut shortcut_manager, shortcut, *shortcut_id, event_sender.clone()) {
|
match register_shortcut_with_callback(&mut shortcut_manager, shortcut, *shortcut_id, event_sender.clone()) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
info!(Source = "Tauri"; "Re-registered shortcut '{shortcut}' for '{}'.", shortcut_id.display_name());
|
info!(Source = "Tauri"; "Re-registered shortcut '{shortcut}' for '{}'.", shortcut_id);
|
||||||
success_count += 1;
|
success_count += 1;
|
||||||
},
|
},
|
||||||
|
|
||||||
Err(error) => warn!(Source = "Tauri"; "Failed to re-register shortcut '{shortcut}' for '{}': {error}", shortcut_id.display_name()),
|
Err(error) => warn!(Source = "Tauri"; "Failed to re-register shortcut '{shortcut}' for '{}': {error}", shortcut_id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user