Skip to content

Commit a280ee9

Browse files
Ngo Iok Ui (Wu Yu Wei)lucasfernog
andauthored
Fix high cpu usage on mac, fix #2074 (#2125)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 1b00855 commit a280ee9

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

.changes/mac-cpu.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime-wry": patch
3+
"tauri": patch
4+
---
5+
6+
Fix macOS high CPU usage.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ unsafe impl Send for GlobalShortcutWrapper {}
131131
#[derive(Clone)]
132132
pub struct GlobalShortcutManagerHandle {
133133
context: EventLoopContext,
134-
shortcuts: HashMap<String, (AcceleratorId, GlobalShortcutWrapper)>,
134+
shortcuts: Arc<Mutex<HashMap<String, (AcceleratorId, GlobalShortcutWrapper)>>>,
135135
listeners: GlobalShortcutListeners,
136136
}
137137

@@ -159,7 +159,11 @@ impl GlobalShortcutManager for GlobalShortcutManagerHandle {
159159
)?;
160160

161161
self.listeners.lock().unwrap().insert(id, Box::new(handler));
162-
self.shortcuts.insert(accelerator.into(), (id, shortcut));
162+
self
163+
.shortcuts
164+
.lock()
165+
.unwrap()
166+
.insert(accelerator.into(), (id, shortcut));
163167

164168
Ok(())
165169
}
@@ -172,12 +176,12 @@ impl GlobalShortcutManager for GlobalShortcutManagerHandle {
172176
Message::GlobalShortcut(GlobalShortcutMessage::UnregisterAll(tx))
173177
)?;
174178
self.listeners.lock().unwrap().clear();
175-
self.shortcuts.clear();
179+
self.shortcuts.lock().unwrap().clear();
176180
Ok(())
177181
}
178182

179183
fn unregister(&mut self, accelerator: &str) -> Result<()> {
180-
if let Some((accelerator_id, shortcut)) = self.shortcuts.remove(accelerator) {
184+
if let Some((accelerator_id, shortcut)) = self.shortcuts.lock().unwrap().remove(accelerator) {
181185
let (tx, rx) = channel();
182186
getter!(
183187
self,
@@ -1373,14 +1377,14 @@ impl Runtime for Wry {
13731377
EventLoopIterationContext {
13741378
callback: &callback,
13751379
webviews: webviews.lock().expect("poisoned webview collection"),
1376-
window_event_listeners: window_event_listeners.clone(),
1380+
window_event_listeners: &window_event_listeners,
13771381
global_shortcut_manager: global_shortcut_manager.clone(),
1378-
global_shortcut_manager_handle: global_shortcut_manager_handle.clone(),
1382+
global_shortcut_manager_handle: &global_shortcut_manager_handle,
13791383
clipboard_manager: clipboard_manager.clone(),
13801384
#[cfg(feature = "menu")]
1381-
menu_event_listeners: menu_event_listeners.clone(),
1385+
menu_event_listeners: &menu_event_listeners,
13821386
#[cfg(feature = "system-tray")]
1383-
tray_context: tray_context.clone(),
1387+
tray_context: &tray_context,
13841388
},
13851389
);
13861390
});
@@ -1409,14 +1413,14 @@ impl Runtime for Wry {
14091413
EventLoopIterationContext {
14101414
callback: &callback,
14111415
webviews: webviews.lock().expect("poisoned webview collection"),
1412-
window_event_listeners: window_event_listeners.clone(),
1416+
window_event_listeners: &window_event_listeners,
14131417
global_shortcut_manager: global_shortcut_manager.clone(),
1414-
global_shortcut_manager_handle: global_shortcut_manager_handle.clone(),
1418+
global_shortcut_manager_handle: &global_shortcut_manager_handle,
14151419
clipboard_manager: clipboard_manager.clone(),
14161420
#[cfg(feature = "menu")]
1417-
menu_event_listeners: menu_event_listeners.clone(),
1421+
menu_event_listeners: &menu_event_listeners,
14181422
#[cfg(feature = "system-tray")]
1419-
tray_context: tray_context.clone(),
1423+
tray_context: &tray_context,
14201424
},
14211425
);
14221426
})
@@ -1426,14 +1430,14 @@ impl Runtime for Wry {
14261430
struct EventLoopIterationContext<'a> {
14271431
callback: &'a (dyn Fn(RunEvent) + 'static),
14281432
webviews: MutexGuard<'a, HashMap<WindowId, WebviewWrapper>>,
1429-
window_event_listeners: WindowEventListeners,
1433+
window_event_listeners: &'a WindowEventListeners,
14301434
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
1431-
global_shortcut_manager_handle: GlobalShortcutManagerHandle,
1435+
global_shortcut_manager_handle: &'a GlobalShortcutManagerHandle,
14321436
clipboard_manager: Arc<Mutex<Clipboard>>,
14331437
#[cfg(feature = "menu")]
1434-
menu_event_listeners: MenuEventListeners,
1438+
menu_event_listeners: &'a MenuEventListeners,
14351439
#[cfg(feature = "system-tray")]
1436-
tray_context: TrayContext,
1440+
tray_context: &'a TrayContext,
14371441
}
14381442

14391443
fn handle_event_loop(

0 commit comments

Comments
 (0)