Skip to content

Commit 9ddf8d8

Browse files
authored
fix(core): properly fire WindowEvent::Destroyed, closes #3688 (#3778)
1 parent 5fb7433 commit 9ddf8d8

File tree

5 files changed

+25
-48
lines changed

5 files changed

+25
-48
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change:** Removed `window_label` from `RunEvent::ExitRequested`.

.changes/window-destroyed-event.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Properly fire the window destroyed event.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,11 +2449,24 @@ fn handle_event_loop<T: UserEvent>(
24492449
callback,
24502450
window_id,
24512451
windows.clone(),
2452-
control_flow,
24532452
window_event_listeners,
24542453
menu_event_listeners.clone(),
24552454
);
24562455
}
2456+
WryWindowEvent::Destroyed => {
2457+
let is_empty = windows.lock().unwrap().is_empty();
2458+
if is_empty {
2459+
let (tx, rx) = channel();
2460+
callback(RunEvent::ExitRequested { tx });
2461+
2462+
let recv = rx.try_recv();
2463+
let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
2464+
2465+
if !should_prevent {
2466+
*control_flow = ControlFlow::Exit;
2467+
}
2468+
}
2469+
}
24572470
WryWindowEvent::Resized(_) => {
24582471
if let Some(WindowHandle::Webview(webview)) = windows
24592472
.lock()
@@ -2476,9 +2489,6 @@ fn handle_event_loop<T: UserEvent>(
24762489
callback,
24772490
id,
24782491
windows.lock().expect("poisoned webview collection"),
2479-
control_flow,
2480-
#[cfg(target_os = "linux")]
2481-
window_event_listeners,
24822492
menu_event_listeners.clone(),
24832493
);
24842494
}
@@ -2513,7 +2523,6 @@ fn on_close_requested<'a, T: UserEvent>(
25132523
callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
25142524
window_id: WebviewId,
25152525
windows: Arc<Mutex<HashMap<WebviewId, WindowWrapper>>>,
2516-
control_flow: &mut ControlFlow,
25172526
window_event_listeners: &WindowEventListeners,
25182527
menu_event_listeners: MenuEventListeners,
25192528
) -> Option<WindowWrapper> {
@@ -2547,9 +2556,6 @@ fn on_close_requested<'a, T: UserEvent>(
25472556
callback,
25482557
window_id,
25492558
windows.lock().expect("poisoned webview collection"),
2550-
control_flow,
2551-
#[cfg(target_os = "linux")]
2552-
window_event_listeners,
25532559
menu_event_listeners,
25542560
)
25552561
}
@@ -2562,50 +2568,17 @@ fn on_window_close<'a, T: UserEvent>(
25622568
callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
25632569
window_id: WebviewId,
25642570
mut windows: MutexGuard<'a, HashMap<WebviewId, WindowWrapper>>,
2565-
control_flow: &mut ControlFlow,
2566-
#[cfg(target_os = "linux")] window_event_listeners: &WindowEventListeners,
25672571
menu_event_listeners: MenuEventListeners,
25682572
) -> Option<WindowWrapper> {
25692573
#[allow(unused_mut)]
25702574
let w = if let Some(mut webview) = windows.remove(&window_id) {
2571-
let is_empty = windows.is_empty();
25722575
drop(windows);
25732576
menu_event_listeners.lock().unwrap().remove(&window_id);
25742577
callback(RunEvent::WindowClose(webview.label.clone()));
2575-
2576-
if is_empty {
2577-
let (tx, rx) = channel();
2578-
callback(RunEvent::ExitRequested {
2579-
window_label: webview.label.clone(),
2580-
tx,
2581-
});
2582-
2583-
let recv = rx.try_recv();
2584-
let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
2585-
2586-
if !should_prevent {
2587-
*control_flow = ControlFlow::Exit;
2588-
}
2589-
}
25902578
Some(webview)
25912579
} else {
25922580
None
25932581
};
2594-
// TODO: tao does not fire the destroyed event properly
2595-
#[cfg(target_os = "linux")]
2596-
{
2597-
for handler in window_event_listeners
2598-
.lock()
2599-
.unwrap()
2600-
.get(&window_id)
2601-
.unwrap()
2602-
.lock()
2603-
.unwrap()
2604-
.values()
2605-
{
2606-
handler(&WindowEvent::Destroyed);
2607-
}
2608-
}
26092582
w
26102583
}
26112584

core/tauri-runtime/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ pub enum RunEvent<T: UserEvent> {
206206
Exit,
207207
/// Event loop is about to exit
208208
ExitRequested {
209-
/// Label of the last window managed by the runtime.
210-
window_label: String,
211209
tx: Sender<ExitRequestedEventAction>,
212210
},
213211
/// Window close was requested by the user.

core/tauri/src/app.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ pub enum RunEvent {
8888
/// The app is about to exit
8989
#[non_exhaustive]
9090
ExitRequested {
91-
/// The label of the window that requested the exit.
92-
/// It is the last window managed by tauri.
93-
window_label: String,
9491
/// Event API
9592
api: ExitRequestApi,
9693
},
@@ -1422,8 +1419,7 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
14221419

14231420
let event = match event {
14241421
RuntimeRunEvent::Exit => RunEvent::Exit,
1425-
RuntimeRunEvent::ExitRequested { window_label, tx } => RunEvent::ExitRequested {
1426-
window_label,
1422+
RuntimeRunEvent::ExitRequested { tx } => RunEvent::ExitRequested {
14271423
api: ExitRequestApi(tx),
14281424
},
14291425
RuntimeRunEvent::CloseRequested { label, signal_tx } => RunEvent::CloseRequested {

0 commit comments

Comments
 (0)