From b79ede6666ff846ef3deacb3d058dce66b4140e9 Mon Sep 17 00:00:00 2001 From: "don.cryptus" Date: Tue, 3 Oct 2023 21:19:50 +0200 Subject: [PATCH] easy --- src-tauri/src/types/types.rs | 5 + src-tauri/src/utils/hotkey/hotkey_listener.rs | 93 ++++++++++++------- src-tauri/src/utils/setup.rs | 25 ++++- 3 files changed, 84 insertions(+), 39 deletions(-) diff --git a/src-tauri/src/types/types.rs b/src-tauri/src/types/types.rs index 1ea250b..46ec767 100644 --- a/src-tauri/src/types/types.rs +++ b/src-tauri/src/types/types.rs @@ -22,6 +22,11 @@ pub struct DatabaseInfo { #[derive(Debug)] pub struct Key { pub id: u32, + pub key_str: String, + pub event: String, + pub ctrl: bool, + pub alt: bool, + pub shift: bool, pub key: String, pub hotkey: HotKey, } diff --git a/src-tauri/src/utils/hotkey/hotkey_listener.rs b/src-tauri/src/utils/hotkey/hotkey_listener.rs index f16ba82..d83d6cc 100644 --- a/src-tauri/src/utils/hotkey/hotkey_listener.rs +++ b/src-tauri/src/utils/hotkey/hotkey_listener.rs @@ -1,57 +1,82 @@ -use core::time::Duration; - -use global_hotkey::hotkey::HotKey; -use global_hotkey::GlobalHotKeyEvent; - use crate::{ - service::window::toggle_main_window, + service::{hotkey::get_all_hotkeys_db, window::toggle_main_window}, types::types::Key, - utils::setup::{HOTKEYS, HOTKEY_MANAGER}, + utils::setup::{HOTKEYS, HOTKEY_MANAGER, HOTKEY_STOP_TX}, }; +use core::time::Duration; +use global_hotkey::hotkey::HotKey; +use global_hotkey::GlobalHotKeyEvent; +use tokio::sync::oneshot; pub fn init_hotkey_listener() -> () { println!("init_hotkey_listener"); - let hotkey_manager = HOTKEY_MANAGER.get().unwrap(); - - let hotkey_str: String = parse_shortcut(true, false, false, "y"); - let hotkey: HotKey = hotkey_str.parse().unwrap(); - - HOTKEYS.get().unwrap().lock().unwrap().insert( - hotkey.id(), - Key { - id: hotkey.id(), - key: hotkey_str, - hotkey: hotkey.clone(), - }, - ); + // If there's an existing sender, send a stop signal to the previous task + if let Some(sender) = HOTKEY_STOP_TX.get().unwrap().lock().unwrap().take() { + let _ = sender.send(()); + } - let _ = hotkey_manager.register(hotkey).unwrap(); + // let (new_stop_tx, mut stop_rx) = oneshot::channel(); + // *HOTKEY_STOP_TX.get().unwrap().lock().unwrap() = Some(new_stop_tx); let receiver = GlobalHotKeyEvent::receiver(); - // Runtime; - tokio::spawn(async { + tauri::async_runtime::spawn(async move { loop { if let Ok(event) = receiver.try_recv() { let hotkeys = HOTKEYS.get().unwrap().lock().unwrap(); + println!("Hotkey Pressed: {:?}", event.id); if let Some(hotkey) = hotkeys.get(&event.id) { println!("Hotkey Pressed: {:?}", hotkey); toggle_main_window(); } } - std::thread::sleep(Duration::from_millis(100)); + println!("looping"); + + // if stop_rx.try_recv().is_ok() { + // break; + // } + std::thread::sleep(Duration::from_millis(1000)); } }); - // std::thread::spawn(|| loop { - // if let Ok(event) = receiver.try_recv() { - // let hotkeys = HOTKEYS.get().unwrap().lock().unwrap(); - // if let Some(hotkey) = hotkeys.get(&event.id) { - // println!("Hotkey Pressed: {:?}", hotkey); - // toggle_main_window(); - // } - // } - // std::thread::sleep(Duration::from_millis(100)); - // }); +} + + + +pub async fn upsert_hotkeys_in_store() -> anyhow::Result<()> { + let hotkeys = get_all_hotkeys_db().await?; + let mut hotkey_store = HOTKEYS.get().unwrap().lock().unwrap(); + + for hotkey in hotkeys { + let hotkey_str: String = parse_shortcut( + hotkey.ctrl, + hotkey.alt, + hotkey.shift, + &hotkey.key.to_lowercase(), + ); + + let key: HotKey = hotkey_str.parse()?; + + let key = Key { + id: key.id(), + event: hotkey.event, + key_str: hotkey_str, + ctrl: hotkey.ctrl, + alt: hotkey.alt, + shift: hotkey.shift, + key: hotkey.key, + hotkey: key, + }; + + if hotkey_store.get(&key.id).is_some() { + let _ = hotkey_store.remove(&key.id); + } + + hotkey_store.insert(key.id, key); + } + + println!("finsihed"); + + Ok(()) } pub fn parse_shortcut(ctrl: bool, alt: bool, shift: bool, key: &str) -> String { diff --git a/src-tauri/src/utils/setup.rs b/src-tauri/src/utils/setup.rs index 374e824..3e38b4a 100644 --- a/src-tauri/src/utils/setup.rs +++ b/src-tauri/src/utils/setup.rs @@ -1,4 +1,4 @@ -use super::hotkey::hotkey_listener::init_hotkey_listener; +use super::hotkey::hotkey_listener::{init_hotkey_listener, upsert_hotkeys_in_store}; use crate::types::types::Key; use crate::{ service::window::get_data_path, types::types::Config, @@ -11,38 +11,53 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::{fs, path::Path, sync::OnceLock}; use tauri::{LogicalSize, Manager}; +use tokio::sync::oneshot; // use window_shadows::set_shadow; pub static MAIN_WINDOW_X: i32 = 375; pub static MAIN_WINDOW_Y: i32 = 600; pub static APP: OnceLock = OnceLock::new(); + pub static HOTKEY_MANAGER: OnceLock = OnceLock::new(); pub static HOTKEYS: OnceLock>>> = OnceLock::new(); +pub static HOTKEY_STOP_TX: OnceLock>>> = OnceLock::new(); pub static CLIPBOARD: OnceLock>> = OnceLock::new(); +pub static GLOBAL_EVENTS: [&'static str; 2] = ["window_display_toggle", "recent_clipboards"]; + +pub static VIEW_MORE_EVENTS: [&'static str; 4] = + ["sync_clipboard_history", "preferences", "about", "exit"]; + +pub static SIDEBAR_ICON_EVENTS: [&'static str; 4] = [ + "recent_clipboards", + "starred_clipboards", + "history", + "view_more", +]; + pub fn setup(app: &mut tauri::App) -> Result<(), Box<(dyn std::error::Error + 'static)>> { APP.set(app.handle()).expect("error initializing tauri app"); let _ = HOTKEY_MANAGER.set(GlobalHotKeyManager::new().unwrap()); let _ = HOTKEYS.set(Arc::new(Mutex::new(HashMap::new()))); let _ = CLIPBOARD.set(Arc::new(Mutex::new(Clipboard::new()?))); + HOTKEY_STOP_TX.set(Mutex::new(None)).unwrap_or_else(|_| { + panic!("Failed to initialize HOTKEY_STOP_TX"); + }); create_config(); let window = app.get_window("main").unwrap(); - let _ = window.set_size(LogicalSize::new(MAIN_WINDOW_X, MAIN_WINDOW_Y)); - #[cfg(any(windows, target_os = "macos"))] set_shadow(&window, true).unwrap(); - #[cfg(debug_assertions)] { window.open_devtools(); } tauri::async_runtime::spawn(async { Master::new(Handler).run() }); - // tauri::async_runtime::spawn(async { init_hotkey_listener() }); + tauri::async_runtime::spawn(async { upsert_hotkeys_in_store().await }); init_hotkey_listener();