Skip to content

Commit

Permalink
fix(core): remove closed window from window.__TAURI__.__windows (#2057
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lucasfernog authored Jun 23, 2021
1 parent fca9764 commit ebaa33c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-window-get-all.md
Original file line number Diff line number Diff line change
@@ -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`.
30 changes: 22 additions & 8 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit ebaa33c

Please sign in to comment.