convert the vec of windows to a hashmap

This commit is contained in:
Chance 2025-03-24 23:26:33 -04:00 committed by BitSyndicate
parent 8f0c8dbb3d
commit 06de4a85e5

View file

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
@ -13,20 +14,21 @@ pub mod ctx;
#[derive(Default)] #[derive(Default)]
pub struct App<'window> { pub struct App<'window> {
window: Vec<Arc<Window>>, windows: HashMap<WindowId, Arc<Window>>,
ctx: Option<WgpuCtx<'window>>, ctx: Option<WgpuCtx<'window>>,
} }
impl ApplicationHandler for App<'_> { impl ApplicationHandler for App<'_> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) { fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.window.is_empty() { if self.windows.is_empty() {
let win_attr = Window::default_attributes().with_title("Zenyx"); let win_attr = Window::default_attributes().with_title("Zenyx");
let window = Arc::new( let window = Arc::new(
event_loop event_loop
.create_window(win_attr) .create_window(win_attr)
.expect("create window err."), .expect("create window err."),
); );
self.window.push(window.clone()); let window_id = window.id();
self.windows.insert(window_id, window.clone());
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap(); let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
self.ctx = Some(wgpu_ctx) self.ctx = Some(wgpu_ctx)
} }
@ -40,16 +42,14 @@ impl ApplicationHandler for App<'_> {
) { ) {
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
if let Some(index) = self.window.iter().position(|window| window.id() == window_id) { if let Some(window) = self.windows.remove(&window_id) {
let mut window = self.window.remove(index);
let mut window = Arc::into_inner(window); let mut window = Arc::into_inner(window);
window = None;
drop(window);
println!("Window: {:?} closed, exiting",window_id); drop(window);
println!("Window: {:?} closed, exiting", window_id);
} }
if self.window.is_empty() { if self.windows.is_empty() {
event_loop.exit(); event_loop.exit();
} }
} }
@ -82,7 +82,8 @@ impl ApplicationHandler for App<'_> {
.create_window(win_attr) .create_window(win_attr)
.expect("create window err."), .expect("create window err."),
); );
self.window.push(new_window.clone()); let window_id = new_window.id();
self.windows.insert(window_id, new_window.clone());
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap(); let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
self.ctx = Some(wgpu_ctx); self.ctx = Some(wgpu_ctx);
} }
@ -92,7 +93,7 @@ impl ApplicationHandler for App<'_> {
_ => {} _ => {}
}, },
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
for window in &self.window { for window in self.windows.values() {
if let Some(ctx) = &mut self.ctx { if let Some(ctx) = &mut self.ctx {
ctx.draw(); ctx.draw();
} }
@ -100,7 +101,7 @@ impl ApplicationHandler for App<'_> {
} }
} }
WindowEvent::Resized(size) => { WindowEvent::Resized(size) => {
if let Some(window) = self.window.iter().find(|w| w.id() == window_id) { if let Some(window) = self.windows.get(&window_id) {
if let Some(wgpu_ctx) = &mut self.ctx { if let Some(wgpu_ctx) = &mut self.ctx {
wgpu_ctx.resize(size.into()); wgpu_ctx.resize(size.into());
window.request_redraw(); window.request_redraw();