Skip to content

Commit

Permalink
Use maintained package for hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
z80maniac committed Jul 8, 2023
1 parent 60e1b2e commit 405af67
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 87 deletions.
83 changes: 32 additions & 51 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cuna = "0.7.0"
dbus = "0.9.7"
directories = "5.0.1"
fd-lock = "4.0.0"
global-hotkey = "0.2.2"
html-escape = "0.2.13"
interprocess = { version = "1.2.1", default_features = false }
ksni = "0.2.1"
Expand All @@ -49,10 +50,6 @@ ureq = { version = "2.7.1", default_features = false, features = ["native-certs"
url = "2.4.0"
walkdir = "2.3.3"

[dependencies.tauri-hotkey]
git = "https://github.com/z80maniac/tauri-hotkey-rs"
rev = "fe210a038e4b18441e4c7603ce0acae2795b3615"

[build-dependencies]
anyhow = "1.0.71"
built = { version = "0.6.1", features = ["chrono", "git2"] }
Expand Down
8 changes: 4 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,14 @@ impl AppHandle {
pub fn wait(self) {
self.player_thread.join().unwrap();
let mut app = self.app.lock().unwrap();
app.hotkeys.stop();
app.player.wait();
app.lastfm.take();
app.listenbrainz.take();
app.tray.shutdown();

// Unregistering media_controls and hotkeys adds almost 1 second to the exiting time.
//app.media_controls.take();
//app.hotkeys.unregister();
// Unregistering media_controls may take almost 1 second
// app.media_controls.take();
}
}

Expand Down Expand Up @@ -555,7 +555,7 @@ fn start_hotkey_thread(app_arc: &Arc<Mutex<App>>) -> Result<()> {
.lock()
.unwrap()
.hotkeys
.register(move |action| {
.start(move |action| {
let mut app = app_arc.lock().unwrap();
app.process_hotkey(action);
})
Expand Down
93 changes: 65 additions & 28 deletions src/hotkeys.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
// SPDX-License-Identifier: GPL-3.0-only
// 🄯 2023, Alexey Parfenov <[email protected]>

use std::{
collections::HashMap,
sync::{Arc, Mutex},
thread::JoinHandle,
time::Duration,
};

use anyhow::Result;
use tauri_hotkey::{Hotkey, HotkeyManager, Key};
use global_hotkey::{
hotkey::{Code, HotKey},
GlobalHotKeyEvent, GlobalHotKeyManager,
};

use crate::err_util::IgnoreErr;
use crate::thread_util;

#[derive(Copy, Clone)]
pub enum HotKeyAction {
Expand All @@ -20,49 +30,76 @@ pub enum HotKeyAction {
SysVolDown,
}

const ACTIONS: [(Key, HotKeyAction); 10] = [
(Key::NUM5, HotKeyAction::StopPlay),
(Key::NUM6, HotKeyAction::Next),
(Key::NUM4, HotKeyAction::Prev),
(Key::NUM9, HotKeyAction::NextDir),
(Key::NUM7, HotKeyAction::PrevDir),
(Key::NUM0, HotKeyAction::PauseToggle),
(Key::NUM2, HotKeyAction::VolDown),
(Key::NUM8, HotKeyAction::VolUp),
(Key::NUM1, HotKeyAction::SysVolDown),
(Key::NUM3, HotKeyAction::SysVolUp),
const ACTIONS: [(Code, HotKeyAction); 10] = [
(Code::Numpad5, HotKeyAction::StopPlay),
(Code::Numpad6, HotKeyAction::Next),
(Code::Numpad4, HotKeyAction::Prev),
(Code::Numpad9, HotKeyAction::NextDir),
(Code::Numpad7, HotKeyAction::PrevDir),
(Code::Numpad0, HotKeyAction::PauseToggle),
(Code::Numpad2, HotKeyAction::VolDown),
(Code::Numpad8, HotKeyAction::VolUp),
(Code::Numpad1, HotKeyAction::SysVolDown),
(Code::Numpad3, HotKeyAction::SysVolUp),
];

const THREAD_SLEEP: Duration = Duration::from_millis(100);

pub struct HotKeys {
manager: HotkeyManager,
thread: Option<JoinHandle<()>>,
stop_flag: Arc<Mutex<bool>>,
}

impl HotKeys {
pub fn new() -> Self {
return Self {
manager: HotkeyManager::new(),
thread: None,
stop_flag: Arc::new(Mutex::new(false)),
};
}

pub fn register<F>(&mut self, f: F) -> Result<()>
pub fn start<F>(&mut self, action_func: F) -> Result<()>
where
F: Fn(HotKeyAction) + Clone + Sync + Send + 'static,
{
for (key, action) in &ACTIONS {
let f = f.clone();
self.manager.register(
Hotkey {
keys: vec![*key],
modifiers: vec![],
},
move || f(*action),
)?;
let mut id_action_map = HashMap::new();
let mut hotkeys = Vec::new();
for (code, action) in ACTIONS {
let hotkey = HotKey::new(None, code);
let id = hotkey.id();
hotkeys.push(hotkey);
id_action_map.insert(id, action);
}

let manager = GlobalHotKeyManager::new()?;
manager.register_all(&hotkeys)?;

let stop_flag = self.stop_flag.clone();
let thread = thread_util::thread("hotkeys manager", move || {
while !*stop_flag.lock().unwrap() {
if let Ok(event) = GlobalHotKeyEvent::receiver().recv_timeout(THREAD_SLEEP) {
if let Some(action) = id_action_map.get(&event.id) {
action_func(*action);
}
}
}
// unregistering takes almost half a second and not actually needed if the program exits
/*
for hotkey in hotkeys {
manager.unregister(hotkey).ignore_err();
}
*/
drop(manager); // this will move the manager into the closure and will keep it alive
});
self.thread = Some(thread);

return Ok(());
}

#[allow(dead_code)]
pub fn unregister(&mut self) {
self.manager.unregister_all().ignore_err();
pub fn stop(&mut self) {
*self.stop_flag.lock().unwrap() = true;
if let Some(t) = self.thread.take() {
t.join().unwrap();
}
}
}

0 comments on commit 405af67

Please sign in to comment.