convert the vec of windows to a hashmap
This commit is contained in:
parent
7b6e3a2d8e
commit
53f0070f7d
1 changed files with 14 additions and 13 deletions
|
@ -1,3 +1,4 @@
|
|||
use std::collections::HashMap;
|
||||
use std::io::{Read, Write};
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
@ -13,20 +14,21 @@ pub mod ctx;
|
|||
|
||||
#[derive(Default)]
|
||||
pub struct App<'window> {
|
||||
window: Vec<Arc<Window>>,
|
||||
windows: HashMap<WindowId, Arc<Window>>,
|
||||
ctx: Option<WgpuCtx<'window>>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App<'_> {
|
||||
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 window = Arc::new(
|
||||
event_loop
|
||||
.create_window(win_attr)
|
||||
.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();
|
||||
self.ctx = Some(wgpu_ctx)
|
||||
}
|
||||
|
@ -40,16 +42,14 @@ impl ApplicationHandler for App<'_> {
|
|||
) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
if let Some(index) = self.window.iter().position(|window| window.id() == window_id) {
|
||||
let mut window = self.window.remove(index);
|
||||
if let Some(window) = self.windows.remove(&window_id) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -82,7 +82,8 @@ impl ApplicationHandler for App<'_> {
|
|||
.create_window(win_attr)
|
||||
.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();
|
||||
self.ctx = Some(wgpu_ctx);
|
||||
}
|
||||
|
@ -92,7 +93,7 @@ impl ApplicationHandler for App<'_> {
|
|||
_ => {}
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
for window in &self.window {
|
||||
for window in self.windows.values() {
|
||||
if let Some(ctx) = &mut self.ctx {
|
||||
ctx.draw();
|
||||
}
|
||||
|
@ -100,7 +101,7 @@ impl ApplicationHandler for App<'_> {
|
|||
}
|
||||
}
|
||||
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 {
|
||||
wgpu_ctx.resize(size.into());
|
||||
window.request_redraw();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue