Fix windows hanging on main window close

This commit is contained in:
Speedy_Lex 2025-04-11 18:55:43 +02:00 committed by BitSyndicate
parent ae5cb17b7d
commit 2be426cb02

View file

@ -62,12 +62,15 @@ pub struct TerminalState {
pub struct App<'window> { pub struct App<'window> {
windows: ahash::AHashMap<WindowId, WindowContext<'window>>, windows: ahash::AHashMap<WindowId, WindowContext<'window>>,
// Number of windows that have been dropped but *NOT* destroyed yet
windows_in_limbo: usize,
} }
impl Default for App<'_> { impl Default for App<'_> {
fn default() -> Self { fn default() -> Self {
Self { Self {
windows: ahash::AHashMap::new(), windows: ahash::AHashMap::new(),
windows_in_limbo: 0,
} }
} }
} }
@ -246,8 +249,13 @@ impl App<'_> {
} }
fn handle_close_requested(&mut self, window_id: WindowId) { 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); 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 { } else {
warn!("Tried to close non-existent window {:?}", window_id); warn!("Tried to close non-existent window {:?}", window_id);
} }
@ -481,9 +489,9 @@ impl App<'_> {
} }
fn handle_destroyed(&mut self, event_loop: &ActiveEventLoop) { fn handle_destroyed(&mut self, event_loop: &ActiveEventLoop) {
if self.windows.is_empty() || !self.windows.iter().any(|(_, ctx)| ctx.is_main_window()) { self.windows_in_limbo -= 1;
self.windows.clear(); if self.windows_in_limbo == 0 && self.windows.is_empty() {
debug!("All main windows are closed. Exiting event loop."); debug!("All windows are closed. Exiting event loop.");
event_loop.exit(); event_loop.exit();
} }
} }