fix(core): remove closed window from window.__TAURI__.__windows (#2057)

This commit is contained in:
Lucas Fernandes Nogueira 2021-06-23 16:20:19 -03:00 committed by GitHub
parent fca976404e
commit ebaa33cb47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 8 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Remove closed window from the `window.__TAURI__.__windows` array, used by the `window.getAll` API from `@tauri-apps/api`.

View File

@ -697,8 +697,9 @@ impl<P: Params> WindowManager<P> {
let window_ = window.clone();
let window_event_listeners = self.inner.window_event_listeners.clone();
let manager = self.clone();
window.on_window_event(move |event| {
let _ = on_window_event(&window_, event);
let _ = on_window_event(&window_, &manager, event);
for handler in window_event_listeners.iter() {
handler(GlobalWindowEvent {
window: window_.clone(),
@ -849,7 +850,11 @@ impl<P: Params> WindowManager<P> {
}
}
fn on_window_event<P: Params>(window: &Window<P>, event: &WindowEvent) -> crate::Result<()> {
fn on_window_event<P: Params>(
window: &Window<P>,
manager: &WindowManager<P>,
event: &WindowEvent,
) -> crate::Result<()> {
match event {
WindowEvent::Resized(size) => window.emit(
&WINDOW_RESIZED_EVENT
@ -863,12 +868,21 @@ fn on_window_event<P: Params>(window: &Window<P>, event: &WindowEvent) -> crate:
.unwrap_or_else(|_| panic!("unhandled event")),
Some(position),
)?,
WindowEvent::CloseRequested => window.emit(
&WINDOW_CLOSE_REQUESTED_EVENT
.parse()
.unwrap_or_else(|_| panic!("unhandled event")),
Some(()),
)?,
WindowEvent::CloseRequested => {
window.emit(
&WINDOW_CLOSE_REQUESTED_EVENT
.parse()
.unwrap_or_else(|_| panic!("unhandled event")),
Some(()),
)?;
let label = window.label();
for window in manager.inner.windows.lock().unwrap().values() {
window.eval(&format!(
r#"window.__TAURI__.__windows = window.__TAURI__.__windows.filter(w => w.label !== "{}");"#,
label
))?;
}
}
WindowEvent::Destroyed => window.emit(
&WINDOW_DESTROYED_EVENT
.parse()