Skip to content

Commit

Permalink
fix: windows build error
Browse files Browse the repository at this point in the history
  • Loading branch information
0PandaDEV committed Nov 15, 2024
1 parent b712d23 commit 6250a3e
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src-tauri/src/api/hotkeys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use global_hotkey::{
GlobalHotKeyEvent, GlobalHotKeyManager, HotKeyState,
};
use std::str::FromStr;
use std::sync::Mutex;
use std::cell::RefCell;
use tauri::{AppHandle, Manager, Listener};

lazy_static::lazy_static! {
static ref HOTKEY_MANAGER: Mutex<Option<GlobalHotKeyManager>> = Mutex::new(None);
thread_local! {
static HOTKEY_MANAGER: RefCell<Option<GlobalHotKeyManager>> = RefCell::new(None);
}

pub fn setup(app_handle: tauri::AppHandle) {
let app_handle_clone = app_handle.clone();

let manager = GlobalHotKeyManager::new().expect("Failed to initialize hotkey manager");
*HOTKEY_MANAGER.lock().unwrap() = Some(manager);
HOTKEY_MANAGER.with(|m| *m.borrow_mut() = Some(manager));

let rt = app_handle.state::<tokio::runtime::Runtime>();
let initial_keybind = rt.block_on(crate::api::database::get_keybind(app_handle_clone.clone()))
Expand All @@ -33,9 +33,11 @@ pub fn setup(app_handle: tauri::AppHandle) {
let payload_str = event.payload().to_string();

if let Ok(old_hotkey) = parse_hotkey(&initial_shortcut_for_update) {
if let Some(manager) = HOTKEY_MANAGER.lock().unwrap().as_ref() {
let _ = manager.unregister(old_hotkey);
}
HOTKEY_MANAGER.with(|manager| {
if let Some(manager) = manager.borrow().as_ref() {
let _ = manager.unregister(old_hotkey);
}
});
}

if let Err(e) = register_shortcut(&payload_str) {
Expand All @@ -47,9 +49,11 @@ pub fn setup(app_handle: tauri::AppHandle) {
let payload_str = event.payload().to_string();

if let Ok(old_hotkey) = parse_hotkey(&initial_shortcut_for_save) {
if let Some(manager) = HOTKEY_MANAGER.lock().unwrap().as_ref() {
let _ = manager.unregister(old_hotkey);
}
HOTKEY_MANAGER.with(|manager| {
if let Some(manager) = manager.borrow().as_ref() {
let _ = manager.unregister(old_hotkey);
}
});
}

if let Err(e) = register_shortcut(&payload_str) {
Expand Down Expand Up @@ -77,10 +81,12 @@ pub fn setup(app_handle: tauri::AppHandle) {

fn register_shortcut(shortcut: &str) -> Result<(), Box<dyn std::error::Error>> {
let hotkey = parse_hotkey(shortcut)?;
if let Some(manager) = HOTKEY_MANAGER.lock().unwrap().as_ref() {
manager.register(hotkey)?;
}
Ok(())
HOTKEY_MANAGER.with(|manager| {
if let Some(manager) = manager.borrow().as_ref() {
manager.register(hotkey)?;
}
Ok(())
})
}

fn parse_hotkey(shortcut: &str) -> Result<HotKey, Box<dyn std::error::Error>> {
Expand Down

0 comments on commit 6250a3e

Please sign in to comment.