close all windows if the main window is destroyed

This commit is contained in:
Chance 2025-03-25 15:33:08 -04:00 committed by BitSyndicate
parent ceefdaf46b
commit 6b1b0d30f4

View file

@ -13,6 +13,12 @@ pub mod ctx;
struct WindowContext<'window> { struct WindowContext<'window> {
window: Arc<Window>, window: Arc<Window>,
ctx: WgpuCtx<'window>, ctx: WgpuCtx<'window>,
main_window: bool
}
impl WindowContext<'_> {
pub fn is_main_window(&self) -> bool {
self.main_window
}
} }
#[derive(Default)] #[derive(Default)]
@ -36,6 +42,7 @@ impl ApplicationHandler for App<'_> {
WindowContext { WindowContext {
window, window,
ctx: wgpu_ctx, ctx: wgpu_ctx,
main_window: true,
}, },
); );
} }
@ -50,12 +57,10 @@ impl ApplicationHandler for App<'_> {
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
if let Some(window_context) = self.windows.remove(&window_id) { if let Some(window_context) = self.windows.remove(&window_id) {
drop(window_context); drop(window_context);
debug!("Window: {:?} closed, exiting", window_id); debug!("Window: {:?} closed, exiting", window_id);
} }
if self.windows.is_empty() {
event_loop.exit();
}
} }
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
device_id, device_id,
@ -97,6 +102,7 @@ impl ApplicationHandler for App<'_> {
WindowContext { WindowContext {
window: new_window, window: new_window,
ctx: wgpu_ctx, ctx: wgpu_ctx,
main_window: false,
}, },
); );
} }
@ -119,6 +125,12 @@ impl ApplicationHandler for App<'_> {
debug!("Window resized to {:?}", size_str); debug!("Window resized to {:?}", size_str);
} }
} }
WindowEvent::Destroyed => {
if !self.windows.iter().any(|(_,ctx)| ctx.is_main_window()) || self.windows.is_empty() {
self.windows.clear();
event_loop.exit();
}
}
_ => trace!("Unhandled window event"), _ => trace!("Unhandled window event"),
} }
} }