Skip to content

Commit 99c1f9a

Browse files
amrbashirSir-Thom
authored andcommitted
chore(deps): update windows-sys crate to 0.59 and global-hotkey to 0.6 (tauri-apps#1665)
* chore(deps): update windows-sys crate to `0.59` * update global-hotkey as well * change files
1 parent e186bf9 commit 99c1f9a

File tree

8 files changed

+72
-34
lines changed

8 files changed

+72
-34
lines changed

.changes/global-shortcut-0.6.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"global-shortcut": "patch"
3+
---
4+
5+
Updated `global-hotkey` crate dependency to `0.6`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"single-instance": "patch"
3+
---
4+
5+
Updated `windows-sys` crate to `0.59`

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/global-shortcut/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ log = { workspace = true }
2424
thiserror = { workspace = true }
2525

2626
[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies]
27-
global-hotkey = { version = "0.5", features = ["serde"] }
27+
global-hotkey = { version = "0.6", features = ["serde"] }

plugins/global-shortcut/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use serde::{Serialize, Serializer};
66

77
#[derive(Debug, thiserror::Error)]
8+
#[non_exhaustive]
89
pub enum Error {
910
#[error("{0}")]
1011
GlobalHotkey(String),
12+
#[error(transparent)]
13+
RecvError(#[from] std::sync::mpsc::RecvError),
14+
#[error(transparent)]
15+
Tauri(#[from] tauri::Error),
1116
}
1217

1318
impl Serialize for Error {

plugins/global-shortcut/src/lib.rs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ use std::{
2020
sync::{Arc, Mutex},
2121
};
2222

23+
use global_hotkey::GlobalHotKeyEvent;
2324
pub use global_hotkey::{
2425
hotkey::{Code, HotKey as Shortcut, Modifiers},
2526
GlobalHotKeyEvent as ShortcutEvent, HotKeyState as ShortcutState,
2627
};
27-
use global_hotkey::{GlobalHotKeyEvent, GlobalHotKeyManager};
2828
use serde::Serialize;
2929
use tauri::{
3030
ipc::Channel,
@@ -60,13 +60,33 @@ struct RegisteredShortcut<R: Runtime> {
6060
handler: Option<Arc<HandlerFn<R>>>,
6161
}
6262

63+
struct GlobalHotKeyManager(global_hotkey::GlobalHotKeyManager);
64+
65+
/// SAFETY: we ensure it is run on main thread only
66+
unsafe impl Send for GlobalHotKeyManager {}
67+
/// SAFETY: we ensure it is run on main thread only
68+
unsafe impl Sync for GlobalHotKeyManager {}
69+
6370
pub struct GlobalShortcut<R: Runtime> {
6471
#[allow(dead_code)]
6572
app: AppHandle<R>,
66-
manager: GlobalHotKeyManager,
73+
manager: Arc<GlobalHotKeyManager>,
6774
shortcuts: Arc<Mutex<HashMap<HotKeyId, RegisteredShortcut<R>>>>,
6875
}
6976

77+
macro_rules! run_main_thread {
78+
($handle:expr, $manager:expr, |$m:ident| $ex:expr) => {{
79+
let (tx, rx) = std::sync::mpsc::channel();
80+
let manager = $manager.clone();
81+
let task = move || {
82+
let f = |$m: &GlobalHotKeyManager| $ex;
83+
let _ = tx.send(f(&*manager));
84+
};
85+
$handle.run_on_main_thread(task)?;
86+
rx.recv()?
87+
}};
88+
}
89+
7090
impl<R: Runtime> GlobalShortcut<R> {
7191
fn register_internal<F: Fn(&AppHandle<R>, &Shortcut, ShortcutEvent) + Send + Sync + 'static>(
7292
&self,
@@ -75,8 +95,7 @@ impl<R: Runtime> GlobalShortcut<R> {
7595
) -> Result<()> {
7696
let id = shortcut.id();
7797
let handler = handler.map(|h| Arc::new(Box::new(h) as HandlerFn<R>));
78-
79-
self.manager.register(shortcut)?;
98+
run_main_thread!(self.app, self.manager, |m| m.0.register(shortcut))?;
8099
self.shortcuts
81100
.lock()
82101
.unwrap()
@@ -95,7 +114,7 @@ impl<R: Runtime> GlobalShortcut<R> {
95114

96115
let mut shortcuts = self.shortcuts.lock().unwrap();
97116
for shortcut in hotkeys {
98-
self.manager.register(shortcut)?;
117+
run_main_thread!(self.app, self.manager, |m| m.0.register(shortcut))?;
99118
shortcuts.insert(
100119
shortcut.id(),
101120
RegisteredShortcut {
@@ -167,7 +186,7 @@ impl<R: Runtime> GlobalShortcut<R> {
167186
S::Error: std::error::Error,
168187
{
169188
let shortcut = try_into_shortcut(shortcut)?;
170-
self.manager.unregister(shortcut)?;
189+
run_main_thread!(self.app, self.manager, |m| m.0.unregister(shortcut))?;
171190
self.shortcuts.lock().unwrap().remove(&shortcut.id());
172191
Ok(())
173192
}
@@ -180,15 +199,19 @@ impl<R: Runtime> GlobalShortcut<R> {
180199
where
181200
T::Error: std::error::Error,
182201
{
183-
let mut s = Vec::new();
202+
let mut mapped_shortcuts = Vec::new();
184203
for shortcut in shortcuts {
185-
s.push(try_into_shortcut(shortcut)?);
204+
mapped_shortcuts.push(try_into_shortcut(shortcut)?);
186205
}
187206

188-
self.manager.unregister_all(&s)?;
207+
{
208+
let mapped_shortcuts = mapped_shortcuts.clone();
209+
#[rustfmt::skip]
210+
run_main_thread!(self.app, self.manager, |m| m.0.unregister_all(&mapped_shortcuts))?;
211+
}
189212

190213
let mut shortcuts = self.shortcuts.lock().unwrap();
191-
for s in s {
214+
for s in mapped_shortcuts {
192215
shortcuts.remove(&s.id());
193216
}
194217

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

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

406429
app.manage(GlobalShortcut {
407430
app: app.clone(),
408-
manager,
431+
manager: Arc::new(GlobalHotKeyManager(manager)),
409432
shortcuts,
410433
});
411434
Ok(())

plugins/single-instance/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ thiserror = { workspace = true }
2222
semver = { version = "1", optional = true }
2323

2424
[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
25-
version = "0.52"
25+
version = "0.59"
2626
features = [
2727
"Win32_System_Threading",
2828
"Win32_System_DataExchange",

plugins/single-instance/src/platform_impl/windows.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
5353
unsafe {
5454
let hwnd = FindWindowW(class_name.as_ptr(), window_name.as_ptr());
5555

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

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

88-
app.manage(TargetWindowHandle(hwnd));
88+
app.manage(TargetWindowHandle(hwnd as _));
8989
}
9090

9191
Ok(())
@@ -101,12 +101,12 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
101101
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
102102
if let Some(hmutex) = manager.try_state::<MutexHandle>() {
103103
unsafe {
104-
ReleaseMutex(hmutex.0);
105-
CloseHandle(hmutex.0);
104+
ReleaseMutex(hmutex.0 as _);
105+
CloseHandle(hmutex.0 as _);
106106
}
107107
}
108108
if let Some(hwnd) = manager.try_state::<TargetWindowHandle>() {
109-
unsafe { DestroyWindow(hwnd.0) };
109+
unsafe { DestroyWindow(hwnd.0 as _) };
110110
}
111111
}
112112

@@ -150,12 +150,12 @@ fn create_event_target_window<R: Runtime>(class_name: &[u16], window_name: &[u16
150150
cbClsExtra: 0,
151151
cbWndExtra: 0,
152152
hInstance: GetModuleHandleW(std::ptr::null()),
153-
hIcon: 0,
154-
hCursor: 0,
155-
hbrBackground: 0,
153+
hIcon: std::ptr::null_mut(),
154+
hCursor: std::ptr::null_mut(),
155+
hbrBackground: std::ptr::null_mut(),
156156
lpszMenuName: std::ptr::null(),
157157
lpszClassName: class_name.as_ptr(),
158-
hIconSm: 0,
158+
hIconSm: std::ptr::null_mut(),
159159
};
160160

161161
RegisterClassExW(&class);
@@ -179,8 +179,8 @@ fn create_event_target_window<R: Runtime>(class_name: &[u16], window_name: &[u16
179179
0,
180180
0,
181181
0,
182-
0,
183-
0,
182+
std::ptr::null_mut(),
183+
std::ptr::null_mut(),
184184
GetModuleHandleW(std::ptr::null()),
185185
std::ptr::null(),
186186
);

0 commit comments

Comments
 (0)