Skip to content

Commit

Permalink
fix(core): properly fire WindowEvent::Destroyed, closes #3688 (#3778)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 28, 2022
1 parent 5fb7433 commit 9ddf8d8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changes/remove-run-event-exit-requested-window-label.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

**Breaking change:** Removed `window_label` from `RunEvent::ExitRequested`.
5 changes: 5 additions & 0 deletions .changes/window-destroyed-event.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Properly fire the window destroyed event.
55 changes: 14 additions & 41 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -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()
Expand All @@ -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(),
);
}
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
)
}
Expand All @@ -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
}

Expand Down
2 changes: 0 additions & 2 deletions core/tauri-runtime/src/lib.rs
Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions core/tauri/src/app.rs
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9ddf8d8

Please sign in to comment.