浏览代码

fix(core): properly fire `WindowEvent::Destroyed`, closes #3688 (#3778)

Lucas Fernandes Nogueira 3 年之前
父节点
当前提交
9ddf8d84a2

+ 5 - 0
.changes/remove-run-event-exit-requested-window-label.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+**Breaking change:** Removed `window_label` from `RunEvent::ExitRequested`.

+ 5 - 0
.changes/window-destroyed-event.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Properly fire the window destroyed event.

+ 14 - 41
core/tauri-runtime-wry/src/lib.rs

@@ -2449,11 +2449,24 @@ fn handle_event_loop<T: UserEvent>(
             callback,
             window_id,
             windows.clone(),
-            control_flow,
             window_event_listeners,
             menu_event_listeners.clone(),
           );
         }
+        WryWindowEvent::Destroyed => {
+          let is_empty = windows.lock().unwrap().is_empty();
+          if is_empty {
+            let (tx, rx) = channel();
+            callback(RunEvent::ExitRequested { tx });
+
+            let recv = rx.try_recv();
+            let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
+
+            if !should_prevent {
+              *control_flow = ControlFlow::Exit;
+            }
+          }
+        }
         WryWindowEvent::Resized(_) => {
           if let Some(WindowHandle::Webview(webview)) = windows
             .lock()
@@ -2476,9 +2489,6 @@ fn handle_event_loop<T: UserEvent>(
           callback,
           id,
           windows.lock().expect("poisoned webview collection"),
-          control_flow,
-          #[cfg(target_os = "linux")]
-          window_event_listeners,
           menu_event_listeners.clone(),
         );
       }
@@ -2513,7 +2523,6 @@ fn on_close_requested<'a, T: UserEvent>(
   callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
   window_id: WebviewId,
   windows: Arc<Mutex<HashMap<WebviewId, WindowWrapper>>>,
-  control_flow: &mut ControlFlow,
   window_event_listeners: &WindowEventListeners,
   menu_event_listeners: MenuEventListeners,
 ) -> Option<WindowWrapper> {
@@ -2547,9 +2556,6 @@ fn on_close_requested<'a, T: UserEvent>(
         callback,
         window_id,
         windows.lock().expect("poisoned webview collection"),
-        control_flow,
-        #[cfg(target_os = "linux")]
-        window_event_listeners,
         menu_event_listeners,
       )
     }
@@ -2562,50 +2568,17 @@ fn on_window_close<'a, T: UserEvent>(
   callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
   window_id: WebviewId,
   mut windows: MutexGuard<'a, HashMap<WebviewId, WindowWrapper>>,
-  control_flow: &mut ControlFlow,
-  #[cfg(target_os = "linux")] window_event_listeners: &WindowEventListeners,
   menu_event_listeners: MenuEventListeners,
 ) -> Option<WindowWrapper> {
   #[allow(unused_mut)]
   let w = if let Some(mut webview) = windows.remove(&window_id) {
-    let is_empty = windows.is_empty();
     drop(windows);
     menu_event_listeners.lock().unwrap().remove(&window_id);
     callback(RunEvent::WindowClose(webview.label.clone()));
-
-    if is_empty {
-      let (tx, rx) = channel();
-      callback(RunEvent::ExitRequested {
-        window_label: webview.label.clone(),
-        tx,
-      });
-
-      let recv = rx.try_recv();
-      let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
-
-      if !should_prevent {
-        *control_flow = ControlFlow::Exit;
-      }
-    }
     Some(webview)
   } else {
     None
   };
-  // TODO: tao does not fire the destroyed event properly
-  #[cfg(target_os = "linux")]
-  {
-    for handler in window_event_listeners
-      .lock()
-      .unwrap()
-      .get(&window_id)
-      .unwrap()
-      .lock()
-      .unwrap()
-      .values()
-    {
-      handler(&WindowEvent::Destroyed);
-    }
-  }
   w
 }
 

+ 0 - 2
core/tauri-runtime/src/lib.rs

@@ -206,8 +206,6 @@ pub enum RunEvent<T: UserEvent> {
   Exit,
   /// Event loop is about to exit
   ExitRequested {
-    /// Label of the last window managed by the runtime.
-    window_label: String,
     tx: Sender<ExitRequestedEventAction>,
   },
   /// Window close was requested by the user.

+ 1 - 5
core/tauri/src/app.rs

@@ -88,9 +88,6 @@ pub enum RunEvent {
   /// The app is about to exit
   #[non_exhaustive]
   ExitRequested {
-    /// The label of the window that requested the exit.
-    /// It is the last window managed by tauri.
-    window_label: String,
     /// Event API
     api: ExitRequestApi,
   },
@@ -1422,8 +1419,7 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
 
   let event = match event {
     RuntimeRunEvent::Exit => RunEvent::Exit,
-    RuntimeRunEvent::ExitRequested { window_label, tx } => RunEvent::ExitRequested {
-      window_label,
+    RuntimeRunEvent::ExitRequested { tx } => RunEvent::ExitRequested {
       api: ExitRequestApi(tx),
     },
     RuntimeRunEvent::CloseRequested { label, signal_tx } => RunEvent::CloseRequested {