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 642d485aa4
commit 5c438b349e
Signed by: bitsyndicate
GPG key ID: 443E4198D6BBA6DE

View file

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