feat: add the ability to open and close new windows

This commit is contained in:
Chance 2025-04-16 16:52:19 +00:00 committed by lily
parent 2f35a3dd25
commit 29522c14f9
Signed by: lily
GPG key ID: 601F3263FBCBC4B9

View file

@ -10,8 +10,10 @@ use wgpu::{
PipelineCompilationOptions, Surface, PipelineCompilationOptions, Surface,
}; };
use winit::application::ApplicationHandler; use winit::application::ApplicationHandler;
use winit::event_loop::EventLoop; use winit::event::{ElementState, MouseButton};
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::window::{Window, WindowAttributes, WindowId}; use winit::window::{Window, WindowAttributes, WindowId};
use zlog::LogLevel;
use zlog::{config::LoggerConfig, query::LogQuery}; use zlog::{config::LoggerConfig, query::LogQuery};
struct WindowContext<'window> { struct WindowContext<'window> {
window: Arc<Window>, window: Arc<Window>,
@ -47,6 +49,21 @@ impl App<'_> {
windows: BTreeMap::new(), windows: BTreeMap::new(),
} }
} }
pub fn spawn_window(&mut self, event_loop: &ActiveEventLoop) {
let attr = WindowAttributes::default()
.with_title("Zenyx - SubWindow")
.with_min_inner_size(winit::dpi::LogicalSize::new(1, 1));
let window = event_loop.create_window(attr).unwrap();
let window = Arc::new(window);
let renderer = self.state.create_renderer(window.clone());
let window_ctx = WindowContext {
renderer: smol::block_on(renderer),
window: window.clone(),
};
let window_id = window.id();
self.windows.insert(window_id, window_ctx);
}
} }
struct WgpuState { struct WgpuState {
@ -282,8 +299,20 @@ impl<'window> ApplicationHandler for App<'window> {
surface_texture.present() surface_texture.present()
} }
winit::event::WindowEvent::CloseRequested => { winit::event::WindowEvent::CloseRequested => {
let _ = self.windows.remove(&window_id);
if self.windows.is_empty() {
event_loop.exit(); event_loop.exit();
} }
}
winit::event::WindowEvent::MouseInput {
device_id,
state,
button,
} => {
if button == MouseButton::Left && state == ElementState::Pressed {
self.spawn_window(event_loop);
}
}
winit::event::WindowEvent::Resized(size) => { winit::event::WindowEvent::Resized(size) => {
if let Some(window_ctx) = self.windows.get(&window_id) { if let Some(window_ctx) = self.windows.get(&window_id) {
if size.width == 0 || size.height == 0 { if size.width == 0 || size.height == 0 {
@ -327,6 +356,7 @@ fn main() -> Result<(), terminator::Terminator> {
.log_to_stdout(true) .log_to_stdout(true)
.file_include_time(true) .file_include_time(true)
.log_to_file(true) .log_to_file(true)
.level(LogLevel::Info)
.log_path("zenyx.log"); .log_path("zenyx.log");
let _logger = zlog::Logger::new(config); let _logger = zlog::Logger::new(config);
let event_loop = EventLoop::new()?; let event_loop = EventLoop::new()?;