fix multi window support

This commit is contained in:
Chance 2025-03-25 01:40:46 -04:00 committed by BitSyndicate
parent 883bd7ea69
commit 8c6051c79d
2 changed files with 94 additions and 43 deletions

64
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,64 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'zenyx'",
"cargo": {
"args": [
"build",
"--bin=zenyx",
"--package=zenyx"
],
"filter": {
"name": "zenyx",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'zenyx'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=zenyx",
"--package=zenyx"
],
"filter": {
"name": "zenyx",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'zen_core'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=zen_core"
],
"filter": {
"name": "zen_core",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

View file

@ -10,10 +10,14 @@ use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::window::{Window, WindowId}; use winit::window::{Window, WindowId};
pub mod ctx; pub mod ctx;
struct WindowContext<'window> {
window: Arc<Window>,
ctx: WgpuCtx<'window>,
}
#[derive(Default)] #[derive(Default)]
pub struct App<'window> { pub struct App<'window> {
windows: HashMap<WindowId, Arc<Window>>, windows: HashMap<WindowId, WindowContext<'window>>,
ctx: Option<WgpuCtx<'window>>,
} }
impl ApplicationHandler for App<'_> { impl ApplicationHandler for App<'_> {
@ -26,12 +30,11 @@ impl ApplicationHandler for App<'_> {
.expect("create window err."), .expect("create window err."),
); );
let window_id = window.id(); 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.windows.insert(window_id, WindowContext { window, ctx: wgpu_ctx });
} }
} }
fn window_event( fn window_event(
&mut self, &mut self,
event_loop: &ActiveEventLoop, event_loop: &ActiveEventLoop,
@ -40,17 +43,13 @@ impl ApplicationHandler for App<'_> {
) { ) {
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
if let Some(window) = self.windows.remove(&window_id) { if let Some(window_context) = self.windows.remove(&window_id) {
let mut window = Arc::into_inner(window); drop(window_context);
window = None;
// _ = window;
drop(window);
event_loop.exit();
println!("Window: {:?} closed, exiting", window_id); println!("Window: {:?} closed, exiting", window_id);
} }
// if self.windows.is_empty() { if self.windows.is_empty() {
// event_loop.exit(); event_loop.exit();
// } }
} }
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
device_id, device_id,
@ -64,27 +63,26 @@ impl ApplicationHandler for App<'_> {
match code { match code {
winit::keyboard::KeyCode::Space => { winit::keyboard::KeyCode::Space => {
debug!("Space key pressed"); debug!("Space key pressed");
if let Some(ctx) = &mut self.ctx { if let Some(window_context) = self.windows.values_mut().next() {
match ctx.bg_color() { match window_context.ctx.bg_color() {
wgpu::Color::WHITE => ctx.change_bg_color(wgpu::Color::BLACK), wgpu::Color::WHITE => window_context.ctx.change_bg_color(wgpu::Color::BLACK),
wgpu::Color::BLACK => ctx.change_bg_color(wgpu::Color::WHITE), wgpu::Color::BLACK => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
_ => ctx.change_bg_color(wgpu::Color::WHITE), _ => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
} }
} }
} }
winit::keyboard::KeyCode::Escape => { winit::keyboard::KeyCode::Escape => {
debug!("Escape key pressed, spawning new window"); debug!("Escape key pressed, spawning new window");
let win_attr = let win_attr = Window::default_attributes()
Window::default_attributes().with_title(format!("Zenyx - New Window {}",self.windows.len())); .with_title(format!("Zenyx - New Window {}", self.windows.len()));
let new_window = Arc::new( let new_window = Arc::new(
event_loop event_loop
.create_window(win_attr) .create_window(win_attr)
.expect("create window err."), .expect("create window err."),
); );
let window_id = new_window.id(); 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.windows.insert(window_id, WindowContext { window: new_window, ctx: wgpu_ctx });
} }
_ => info!("Unimplemented keycode: {:?}", code), _ => info!("Unimplemented keycode: {:?}", code),
} }
@ -92,29 +90,18 @@ impl ApplicationHandler for App<'_> {
_ => {} _ => {}
}, },
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
if let Some(ctx) = &mut self.ctx { if let Some(window_context) = self.windows.get_mut(&window_id) {
ctx.draw(); window_context.ctx.draw();
} window_context.window.request_redraw();
match self.windows.get(&window_id) {
Some(window) => {
if let Some(ctx) = &mut self.ctx {
ctx.draw();
}
window.request_redraw();
}
None => ()
} }
} }
WindowEvent::Resized(size) => { WindowEvent::Resized(size) => {
if let Some(window) = self.windows.get(&window_id) { if let Some(window_context) = self.windows.get_mut(&window_id) {
if let Some(wgpu_ctx) = &mut self.ctx { window_context.ctx.resize(size.into());
wgpu_ctx.resize(size.into()); window_context.window.request_redraw();
window.request_redraw(); let size_str: String =
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
size.height.to_string() + "x" + &size.width.to_string(); debug!("Window resized to {:?}", size_str);
debug!("Window resized to {:?}", size_str);
}
} }
} }
_ => trace!("Unhandled window event"), _ => trace!("Unhandled window event"),