Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Oct 3, 2023
1 parent 0c496be commit b79ede6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 39 deletions.
5 changes: 5 additions & 0 deletions src-tauri/src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
93 changes: 59 additions & 34 deletions src-tauri/src/utils/hotkey/hotkey_listener.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
25 changes: 20 additions & 5 deletions src-tauri/src/utils/setup.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<tauri::AppHandle> = OnceLock::new();

pub static HOTKEY_MANAGER: OnceLock<GlobalHotKeyManager> = OnceLock::new();
pub static HOTKEYS: OnceLock<Arc<Mutex<HashMap<u32, Key>>>> = OnceLock::new();
pub static HOTKEY_STOP_TX: OnceLock<Mutex<Option<oneshot::Sender<()>>>> = OnceLock::new();
pub static CLIPBOARD: OnceLock<Arc<Mutex<Clipboard>>> = 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();

Expand Down

0 comments on commit b79ede6

Please sign in to comment.