Skip to content

Commit

Permalink
Fix high cpu usage on mac, fix #2074 (#2125)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) and lucasfernog committed Jun 30, 2021
1 parent 1b00855 commit a280ee9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changes/mac-cpu.md
@@ -0,0 +1,6 @@
---
"tauri-runtime-wry": patch
"tauri": patch
---

Fix macOS high CPU usage.
36 changes: 20 additions & 16 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -131,7 +131,7 @@ unsafe impl Send for GlobalShortcutWrapper {}
#[derive(Clone)]
pub struct GlobalShortcutManagerHandle {
context: EventLoopContext,
shortcuts: HashMap<String, (AcceleratorId, GlobalShortcutWrapper)>,
shortcuts: Arc<Mutex<HashMap<String, (AcceleratorId, GlobalShortcutWrapper)>>>,
listeners: GlobalShortcutListeners,
}

Expand Down Expand Up @@ -159,7 +159,11 @@ impl GlobalShortcutManager for GlobalShortcutManagerHandle {
)?;

self.listeners.lock().unwrap().insert(id, Box::new(handler));
self.shortcuts.insert(accelerator.into(), (id, shortcut));
self
.shortcuts
.lock()
.unwrap()
.insert(accelerator.into(), (id, shortcut));

Ok(())
}
Expand All @@ -172,12 +176,12 @@ impl GlobalShortcutManager for GlobalShortcutManagerHandle {
Message::GlobalShortcut(GlobalShortcutMessage::UnregisterAll(tx))
)?;
self.listeners.lock().unwrap().clear();
self.shortcuts.clear();
self.shortcuts.lock().unwrap().clear();
Ok(())
}

fn unregister(&mut self, accelerator: &str) -> Result<()> {
if let Some((accelerator_id, shortcut)) = self.shortcuts.remove(accelerator) {
if let Some((accelerator_id, shortcut)) = self.shortcuts.lock().unwrap().remove(accelerator) {
let (tx, rx) = channel();
getter!(
self,
Expand Down Expand Up @@ -1373,14 +1377,14 @@ impl Runtime for Wry {
EventLoopIterationContext {
callback: &callback,
webviews: webviews.lock().expect("poisoned webview collection"),
window_event_listeners: window_event_listeners.clone(),
window_event_listeners: &window_event_listeners,
global_shortcut_manager: global_shortcut_manager.clone(),
global_shortcut_manager_handle: global_shortcut_manager_handle.clone(),
global_shortcut_manager_handle: &global_shortcut_manager_handle,
clipboard_manager: clipboard_manager.clone(),
#[cfg(feature = "menu")]
menu_event_listeners: menu_event_listeners.clone(),
menu_event_listeners: &menu_event_listeners,
#[cfg(feature = "system-tray")]
tray_context: tray_context.clone(),
tray_context: &tray_context,
},
);
});
Expand Down Expand Up @@ -1409,14 +1413,14 @@ impl Runtime for Wry {
EventLoopIterationContext {
callback: &callback,
webviews: webviews.lock().expect("poisoned webview collection"),
window_event_listeners: window_event_listeners.clone(),
window_event_listeners: &window_event_listeners,
global_shortcut_manager: global_shortcut_manager.clone(),
global_shortcut_manager_handle: global_shortcut_manager_handle.clone(),
global_shortcut_manager_handle: &global_shortcut_manager_handle,
clipboard_manager: clipboard_manager.clone(),
#[cfg(feature = "menu")]
menu_event_listeners: menu_event_listeners.clone(),
menu_event_listeners: &menu_event_listeners,
#[cfg(feature = "system-tray")]
tray_context: tray_context.clone(),
tray_context: &tray_context,
},
);
})
Expand All @@ -1426,14 +1430,14 @@ impl Runtime for Wry {
struct EventLoopIterationContext<'a> {
callback: &'a (dyn Fn(RunEvent) + 'static),
webviews: MutexGuard<'a, HashMap<WindowId, WebviewWrapper>>,
window_event_listeners: WindowEventListeners,
window_event_listeners: &'a WindowEventListeners,
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
global_shortcut_manager_handle: GlobalShortcutManagerHandle,
global_shortcut_manager_handle: &'a GlobalShortcutManagerHandle,
clipboard_manager: Arc<Mutex<Clipboard>>,
#[cfg(feature = "menu")]
menu_event_listeners: MenuEventListeners,
menu_event_listeners: &'a MenuEventListeners,
#[cfg(feature = "system-tray")]
tray_context: TrayContext,
tray_context: &'a TrayContext,
}

fn handle_event_loop(
Expand Down

0 comments on commit a280ee9

Please sign in to comment.