Skip to content

Commit

Permalink
fix(tauri-runtime-wry): menu even panic on macOS inspector, closes #3875
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 3, 2022
1 parent 164078c commit 891eb74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/fix-menu-crash.md
@@ -0,0 +1,5 @@
---
"tauri-runtime-wry": patch
---

Fixes a crash when using the menu with the inspector window focused on macOS. In this case the `window_id` will be the id of the first app window.
13 changes: 12 additions & 1 deletion core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -136,6 +136,10 @@ impl WebviewIdStore {
fn get(&self, w: &WindowId) -> WebviewId {
*self.0.lock().unwrap().get(w).unwrap()
}

fn try_get(&self, w: &WindowId) -> Option<WebviewId> {
self.0.lock().unwrap().get(w).copied()
}
}

#[macro_export]
Expand Down Expand Up @@ -2462,6 +2466,8 @@ fn handle_event_loop<T: UserEvent>(
#[allow(unused_mut)]
let mut window_id = window_id.unwrap(); // always Some on MenuBar event

println!("got {:?}", window_id);

#[cfg(target_os = "macos")]
{
// safety: we're only checking to see if the window_id is 0
Expand All @@ -2475,7 +2481,12 @@ fn handle_event_loop<T: UserEvent>(
menu_item_id: menu_id.0,
};
let window_menu_event_listeners = {
let window_id = webview_id_map.get(&window_id);
// on macOS the window id might be the inspector window if it is detached
let window_id = if let Some(window_id) = webview_id_map.try_get(&window_id) {
window_id
} else {
*webview_id_map.0.lock().unwrap().values().next().unwrap()
};
let listeners = menu_event_listeners.lock().unwrap();
listeners.get(&window_id).cloned().unwrap_or_default()
};
Expand Down

0 comments on commit 891eb74

Please sign in to comment.