diff --git a/engine/src/core/render/mod.rs b/engine/src/core/render/mod.rs index 14d75a4..128adfb 100644 --- a/engine/src/core/render/mod.rs +++ b/engine/src/core/render/mod.rs @@ -62,12 +62,15 @@ pub struct TerminalState { pub struct App<'window> { windows: ahash::AHashMap>, + // Number of windows that have been dropped but *NOT* destroyed yet + windows_in_limbo: usize, } impl Default for App<'_> { fn default() -> Self { Self { windows: ahash::AHashMap::new(), + windows_in_limbo: 0, } } } @@ -246,8 +249,13 @@ impl App<'_> { } fn handle_close_requested(&mut self, window_id: WindowId) { - if self.windows.remove(&window_id).is_some() { + if let Some(window) = self.windows.remove(&window_id) { debug!("Window {:?} closed", window_id); + self.windows_in_limbo += 1; + if window.is_main_window() { + self.windows_in_limbo += self.windows.len(); + self.windows.clear(); + } } else { warn!("Tried to close non-existent window {:?}", window_id); } @@ -481,9 +489,9 @@ impl App<'_> { } fn handle_destroyed(&mut self, event_loop: &ActiveEventLoop) { - if self.windows.is_empty() || !self.windows.iter().any(|(_, ctx)| ctx.is_main_window()) { - self.windows.clear(); - debug!("All main windows are closed. Exiting event loop."); + self.windows_in_limbo -= 1; + if self.windows_in_limbo == 0 && self.windows.is_empty() { + debug!("All windows are closed. Exiting event loop."); event_loop.exit(); } }