Răsfoiți Sursa

fix(tauri-runtime-wry): menu even panic on macOS inspector, closes #3875 (#4027)

Lucas Fernandes Nogueira 3 ani în urmă
părinte
comite
891eb748cf
2 a modificat fișierele cu 17 adăugiri și 1 ștergeri
  1. 5 0
      .changes/fix-menu-crash.md
  2. 12 1
      core/tauri-runtime-wry/src/lib.rs

+ 5 - 0
.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.

+ 12 - 1
core/tauri-runtime-wry/src/lib.rs

@@ -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]
@@ -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
@@ -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()
       };