Skip to content

Commit

Permalink
chore(deps): update windows-sys crate to 0.59 and global-hotkey to …
Browse files Browse the repository at this point in the history
…`0.6` (#1665)

* chore(deps): update windows-sys crate to `0.59`

* update global-hotkey as well

* change files
  • Loading branch information
amrbashir authored Aug 21, 2024
1 parent f690777 commit 3c52f30
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changes/global-shortcut-0.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"global-shortcut": "patch"
---

Updated `global-hotkey` crate dependency to `0.6`
5 changes: 5 additions & 0 deletions .changes/single-instance-windows-sys.0.59.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"single-instance": "patch"
---

Updated `windows-sys` crate to `0.59`
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion plugins/global-shortcut/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ log = { workspace = true }
thiserror = { workspace = true }

[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies]
global-hotkey = { version = "0.5", features = ["serde"] }
global-hotkey = { version = "0.6", features = ["serde"] }
5 changes: 5 additions & 0 deletions plugins/global-shortcut/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
use serde::{Serialize, Serializer};

#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("{0}")]
GlobalHotkey(String),
#[error(transparent)]
RecvError(#[from] std::sync::mpsc::RecvError),
#[error(transparent)]
Tauri(#[from] tauri::Error),
}

impl Serialize for Error {
Expand Down
53 changes: 38 additions & 15 deletions plugins/global-shortcut/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use std::{
sync::{Arc, Mutex},
};

use global_hotkey::GlobalHotKeyEvent;
pub use global_hotkey::{
hotkey::{Code, HotKey as Shortcut, Modifiers},
GlobalHotKeyEvent as ShortcutEvent, HotKeyState as ShortcutState,
};
use global_hotkey::{GlobalHotKeyEvent, GlobalHotKeyManager};
use serde::Serialize;
use tauri::{
ipc::Channel,
Expand Down Expand Up @@ -60,13 +60,33 @@ struct RegisteredShortcut<R: Runtime> {
handler: Option<Arc<HandlerFn<R>>>,
}

struct GlobalHotKeyManager(global_hotkey::GlobalHotKeyManager);

/// SAFETY: we ensure it is run on main thread only
unsafe impl Send for GlobalHotKeyManager {}
/// SAFETY: we ensure it is run on main thread only
unsafe impl Sync for GlobalHotKeyManager {}

pub struct GlobalShortcut<R: Runtime> {
#[allow(dead_code)]
app: AppHandle<R>,
manager: GlobalHotKeyManager,
manager: Arc<GlobalHotKeyManager>,
shortcuts: Arc<Mutex<HashMap<HotKeyId, RegisteredShortcut<R>>>>,
}

macro_rules! run_main_thread {
($handle:expr, $manager:expr, |$m:ident| $ex:expr) => {{
let (tx, rx) = std::sync::mpsc::channel();
let manager = $manager.clone();
let task = move || {
let f = |$m: &GlobalHotKeyManager| $ex;
let _ = tx.send(f(&*manager));
};
$handle.run_on_main_thread(task)?;
rx.recv()?
}};
}

impl<R: Runtime> GlobalShortcut<R> {
fn register_internal<F: Fn(&AppHandle<R>, &Shortcut, ShortcutEvent) + Send + Sync + 'static>(
&self,
Expand All @@ -75,8 +95,7 @@ impl<R: Runtime> GlobalShortcut<R> {
) -> Result<()> {
let id = shortcut.id();
let handler = handler.map(|h| Arc::new(Box::new(h) as HandlerFn<R>));

self.manager.register(shortcut)?;
run_main_thread!(self.app, self.manager, |m| m.0.register(shortcut))?;
self.shortcuts
.lock()
.unwrap()
Expand All @@ -95,7 +114,7 @@ impl<R: Runtime> GlobalShortcut<R> {

let mut shortcuts = self.shortcuts.lock().unwrap();
for shortcut in hotkeys {
self.manager.register(shortcut)?;
run_main_thread!(self.app, self.manager, |m| m.0.register(shortcut))?;
shortcuts.insert(
shortcut.id(),
RegisteredShortcut {
Expand Down Expand Up @@ -167,7 +186,7 @@ impl<R: Runtime> GlobalShortcut<R> {
S::Error: std::error::Error,
{
let shortcut = try_into_shortcut(shortcut)?;
self.manager.unregister(shortcut)?;
run_main_thread!(self.app, self.manager, |m| m.0.unregister(shortcut))?;
self.shortcuts.lock().unwrap().remove(&shortcut.id());
Ok(())
}
Expand All @@ -180,15 +199,19 @@ impl<R: Runtime> GlobalShortcut<R> {
where
T::Error: std::error::Error,
{
let mut s = Vec::new();
let mut mapped_shortcuts = Vec::new();
for shortcut in shortcuts {
s.push(try_into_shortcut(shortcut)?);
mapped_shortcuts.push(try_into_shortcut(shortcut)?);
}

self.manager.unregister_all(&s)?;
{
let mapped_shortcuts = mapped_shortcuts.clone();
#[rustfmt::skip]
run_main_thread!(self.app, self.manager, |m| m.0.unregister_all(&mapped_shortcuts))?;
}

let mut shortcuts = self.shortcuts.lock().unwrap();
for s in s {
for s in mapped_shortcuts {
shortcuts.remove(&s.id());
}

Expand All @@ -200,9 +223,9 @@ impl<R: Runtime> GlobalShortcut<R> {
let mut shortcuts = self.shortcuts.lock().unwrap();
let hotkeys = std::mem::take(&mut *shortcuts);
let hotkeys = hotkeys.values().map(|s| s.shortcut).collect::<Vec<_>>();
self.manager
.unregister_all(hotkeys.as_slice())
.map_err(Into::into)
#[rustfmt::skip]
let res = run_main_thread!(self.app, self.manager, |m| m.0.unregister_all(hotkeys.as_slice()));
res.map_err(Into::into)
}

/// Determines whether the given shortcut is registered by this application or not.
Expand Down Expand Up @@ -375,7 +398,7 @@ impl<R: Runtime> Builder<R> {
is_registered,
])
.setup(move |app, _api| {
let manager = GlobalHotKeyManager::new()?;
let manager = global_hotkey::GlobalHotKeyManager::new()?;
let mut store = HashMap::<HotKeyId, RegisteredShortcut<R>>::new();
for shortcut in shortcuts {
manager.register(shortcut)?;
Expand Down Expand Up @@ -405,7 +428,7 @@ impl<R: Runtime> Builder<R> {

app.manage(GlobalShortcut {
app: app.clone(),
manager,
manager: Arc::new(GlobalHotKeyManager(manager)),
shortcuts,
});
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion plugins/single-instance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ thiserror = { workspace = true }
semver = { version = "1", optional = true }

[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
version = "0.52"
version = "0.59"
features = [
"Win32_System_Threading",
"Win32_System_DataExchange",
Expand Down
24 changes: 12 additions & 12 deletions plugins/single-instance/src/platform_impl/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
unsafe {
let hwnd = FindWindowW(class_name.as_ptr(), window_name.as_ptr());

if hwnd != 0 {
if !hwnd.is_null() {
let data = format!(
"{}|{}\0",
std::env::current_dir()
Expand All @@ -74,7 +74,7 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
}
}
} else {
app.manage(MutexHandle(hmutex));
app.manage(MutexHandle(hmutex as _));

let hwnd = create_event_target_window::<R>(&class_name, &window_name);
unsafe {
Expand All @@ -85,7 +85,7 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
)
};

app.manage(TargetWindowHandle(hwnd));
app.manage(TargetWindowHandle(hwnd as _));
}

Ok(())
Expand All @@ -101,12 +101,12 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
if let Some(hmutex) = manager.try_state::<MutexHandle>() {
unsafe {
ReleaseMutex(hmutex.0);
CloseHandle(hmutex.0);
ReleaseMutex(hmutex.0 as _);
CloseHandle(hmutex.0 as _);
}
}
if let Some(hwnd) = manager.try_state::<TargetWindowHandle>() {
unsafe { DestroyWindow(hwnd.0) };
unsafe { DestroyWindow(hwnd.0 as _) };
}
}

Expand Down Expand Up @@ -150,12 +150,12 @@ fn create_event_target_window<R: Runtime>(class_name: &[u16], window_name: &[u16
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: GetModuleHandleW(std::ptr::null()),
hIcon: 0,
hCursor: 0,
hbrBackground: 0,
hIcon: std::ptr::null_mut(),
hCursor: std::ptr::null_mut(),
hbrBackground: std::ptr::null_mut(),
lpszMenuName: std::ptr::null(),
lpszClassName: class_name.as_ptr(),
hIconSm: 0,
hIconSm: std::ptr::null_mut(),
};

RegisterClassExW(&class);
Expand All @@ -179,8 +179,8 @@ fn create_event_target_window<R: Runtime>(class_name: &[u16], window_name: &[u16
0,
0,
0,
0,
0,
std::ptr::null_mut(),
std::ptr::null_mut(),
GetModuleHandleW(std::ptr::null()),
std::ptr::null(),
);
Expand Down

0 comments on commit 3c52f30

Please sign in to comment.