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 b79ede6 commit d192eb5
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 36 deletions.
65 changes: 45 additions & 20 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct DatabaseInfo {
#[derive(Debug)]
pub struct Key {
pub id: u32,
pub global: bool,
pub key_str: String,
pub event: String,
pub ctrl: bool,
Expand Down
67 changes: 54 additions & 13 deletions src-tauri/src/utils/hotkey/hotkey_listener.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,86 @@
use crate::{
service::{hotkey::get_all_hotkeys_db, window::toggle_main_window},
types::types::Key,
utils::setup::{HOTKEYS, HOTKEY_MANAGER, HOTKEY_STOP_TX},
utils::setup::{APP, GLOBAL_EVENTS, HOTKEYS, HOTKEY_MANAGER, HOTKEY_STOP_TX},
};
use core::time::Duration;
use global_hotkey::hotkey::HotKey;
use global_hotkey::GlobalHotKeyEvent;
use tauri::Manager;
use tokio::sync::oneshot;

pub fn init_hotkey_listener() -> () {
pub fn init_hotkey_listener(i: i32) -> () {
println!("init_hotkey_listener");

tauri::async_runtime::spawn(async {
unregister_hotkeys(true);
let _ = upsert_hotkeys_in_store().await;
register_hotkeys()
});

// 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 (new_stop_tx, mut stop_rx) = oneshot::channel();
// *HOTKEY_STOP_TX.get().unwrap().lock().unwrap() = Some(new_stop_tx);
let (new_stop_tx, mut stop_rx) = oneshot::channel();
*HOTKEY_STOP_TX.get().unwrap().lock().unwrap() = Some(new_stop_tx);
let receiver = GlobalHotKeyEvent::receiver();
tauri::async_runtime::spawn(async move {
loop {
println!("{}", i);
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();
}
}
println!("looping");

// if stop_rx.try_recv().is_ok() {
// break;
// }
std::thread::sleep(Duration::from_millis(1000));
if stop_rx.try_recv().is_ok() {
break;
}
std::thread::sleep(Duration::from_millis(100));
}
});
}

pub fn register_hotkeys() {
let hotkeys_store: std::sync::MutexGuard<'_, std::collections::HashMap<u32, Key>> =
HOTKEYS.get().unwrap().lock().unwrap();
let hotkey_manager = HOTKEY_MANAGER.get().unwrap();
let window = APP.get().unwrap().get_window("main").unwrap();

for (_, hotkey) in hotkeys_store.iter() {
if window.is_visible().unwrap() {
println!("register_hotkeys");
hotkey_manager.register(hotkey.hotkey.clone()).unwrap();
} else if hotkey.global {
let key = hotkey_manager.register(hotkey.hotkey.clone());
if key.is_err() {
hotkey_manager.unregister(hotkey.hotkey.clone()).unwrap();
hotkey_manager.register(hotkey.hotkey.clone()).unwrap();
} else {
key.unwrap();
}
}
}
}

pub fn unregister_hotkeys(all: bool) {
let hotkeys_store: std::sync::MutexGuard<'_, std::collections::HashMap<u32, Key>> =
HOTKEYS.get().unwrap().lock().unwrap();
let hotkey_manager = HOTKEY_MANAGER.get().unwrap();

for (_, hotkey) in hotkeys_store.iter() {
if all {
hotkey_manager.unregister(hotkey.hotkey.clone()).unwrap();
} else if !hotkey.global {
hotkey_manager.unregister(hotkey.hotkey.clone()).unwrap();
}
}
}

pub async fn upsert_hotkeys_in_store() -> anyhow::Result<()> {
let hotkeys = get_all_hotkeys_db().await?;
Expand All @@ -56,8 +96,11 @@ pub async fn upsert_hotkeys_in_store() -> anyhow::Result<()> {

let key: HotKey = hotkey_str.parse()?;

let global = GLOBAL_EVENTS.contains(&hotkey.event.as_str());

let key = Key {
id: key.id(),
global,
event: hotkey.event,
key_str: hotkey_str,
ctrl: hotkey.ctrl,
Expand All @@ -74,8 +117,6 @@ pub async fn upsert_hotkeys_in_store() -> anyhow::Result<()> {
hotkey_store.insert(key.id, key);
}

println!("finsihed");

Ok(())
}

Expand Down
6 changes: 3 additions & 3 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, upsert_hotkeys_in_store};
use super::hotkey::hotkey_listener::init_hotkey_listener;
use crate::types::types::Key;
use crate::{
service::window::get_data_path, types::types::Config,
Expand Down Expand Up @@ -57,9 +57,9 @@ pub fn setup(app: &mut tauri::App) -> Result<(), Box<(dyn std::error::Error + 's
}

tauri::async_runtime::spawn(async { Master::new(Handler).run() });
tauri::async_runtime::spawn(async { upsert_hotkeys_in_store().await });

init_hotkey_listener();
init_hotkey_listener(1);
init_hotkey_listener(2);

Ok(())
}
Expand Down

0 comments on commit d192eb5

Please sign in to comment.