Browse Source

feat(core): add `window_label` to the `ExitRequested` event payload (#2410)

Lucas Fernandes Nogueira 4 years ago
parent
commit
f4eafec705
3 changed files with 23 additions and 15 deletions
  1. 14 10
      core/tauri-runtime-wry/src/lib.rs
  2. 2 0
      core/tauri-runtime/src/lib.rs
  3. 7 5
      core/tauri/src/app.rs

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

@@ -2080,18 +2080,22 @@ fn on_window_close<'a>(
   if let Some(webview) = windows.remove(&window_id) {
     #[cfg(feature = "menu")]
     menu_event_listeners.lock().unwrap().remove(&window_id);
-    callback(RunEvent::WindowClose(webview.label));
-  }
-  if windows.is_empty() {
-    let (tx, rx) = channel();
-    callback(RunEvent::ExitRequested { tx });
+    callback(RunEvent::WindowClose(webview.label.clone()));
 
-    let recv = rx.try_recv();
-    let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
+    if windows.is_empty() {
+      let (tx, rx) = channel();
+      callback(RunEvent::ExitRequested {
+        window_label: webview.label,
+        tx,
+      });
+
+      let recv = rx.try_recv();
+      let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
 
-    if !should_prevent {
-      *control_flow = ControlFlow::Exit;
-      callback(RunEvent::Exit);
+      if !should_prevent {
+        *control_flow = ControlFlow::Exit;
+        callback(RunEvent::Exit);
+      }
     }
   }
   // TODO: tao does not fire the destroyed event properly

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

@@ -173,6 +173,8 @@ pub enum RunEvent {
   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.

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

@@ -78,6 +78,9 @@ pub enum Event {
   /// 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,
   },
@@ -405,16 +408,15 @@ impl<R: Runtime> App<R> {
           &app_handle,
           match event {
             RunEvent::Exit => Event::Exit,
-            RunEvent::ExitRequested { tx } => Event::ExitRequested {
+            RunEvent::ExitRequested { window_label, tx } => Event::ExitRequested {
+              window_label,
               api: ExitRequestApi(tx),
             },
             RunEvent::CloseRequested { label, signal_tx } => Event::CloseRequested {
-              label: label.parse().unwrap_or_else(|_| unreachable!()),
+              label,
               api: CloseRequestApi(signal_tx),
             },
-            RunEvent::WindowClose(label) => {
-              Event::WindowClosed(label.parse().unwrap_or_else(|_| unreachable!()))
-            }
+            RunEvent::WindowClose(label) => Event::WindowClosed(label),
             _ => unimplemented!(),
           },
         );