From ebaa33cb47e045af75140d818565d211f45946b4 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 23 Jun 2021 16:20:19 -0300 Subject: [PATCH] fix(core): remove closed window from `window.__TAURI__.__windows` (#2057) --- .changes/fix-window-get-all.md | 5 +++++ core/tauri/src/manager.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 .changes/fix-window-get-all.md diff --git a/.changes/fix-window-get-all.md b/.changes/fix-window-get-all.md new file mode 100644 index 000000000..3328955d3 --- /dev/null +++ b/.changes/fix-window-get-all.md @@ -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`. diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index c8f94eda3..001cedd10 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -697,8 +697,9 @@ impl WindowManager

{ 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 WindowManager

{ } } -fn on_window_event(window: &Window

, event: &WindowEvent) -> crate::Result<()> { +fn on_window_event( + window: &Window

, + manager: &WindowManager

, + event: &WindowEvent, +) -> crate::Result<()> { match event { WindowEvent::Resized(size) => window.emit( &WINDOW_RESIZED_EVENT @@ -863,12 +868,21 @@ fn on_window_event(window: &Window

, 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()