use std::{rc::Rc, sync::Arc}; use crate::mvu::{self, MvuApp, Size2i}; use { futures::executor::block_on, winit::{ application::ApplicationHandler, dpi::PhysicalSize, event::WindowEvent, event_loop::{ActiveEventLoop, ControlFlow, EventLoop}, window::{Window, WindowId}, }, }; struct AppHost { window: Option>, app: A, model: Rc, } impl AppHost { fn new(app: A, model: Rc) -> Self { Self { window: None, app, model, } } } impl ApplicationHandler for AppHost where A: MvuApp, { fn resumed(&mut self, event_loop: &ActiveEventLoop) { let window = Arc::new( event_loop .create_window(Window::default_attributes()) .unwrap(), ); let instance = wgpu::Instance::default(); let surface = instance.create_surface(Arc::clone(&window)).unwrap(); let PhysicalSize { width, height } = window.inner_size(); self.window = Some(window); block_on(self.app.init(&instance, surface, Size2i { width, height })); } fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { match event { WindowEvent::Resized(new_size) => block_on(self.app.resize(Size2i { width: new_size.width, height: new_size.height, })), WindowEvent::CloseRequested => { println!("The close button was pressed; stopping"); event_loop.exit(); } WindowEvent::RedrawRequested => block_on(self.app.view(self.model.clone())).unwrap(), WindowEvent::DroppedFile(file_path) => { self.model = block_on( self.app .update(self.model.clone(), mvu::Event::OpenTestFile(file_path)), ) } _ => (), } } } pub fn run(app: A, model: Rc) where A: MvuApp, { env_logger::init(); let event_loop = EventLoop::new().unwrap(); event_loop.set_control_flow(ControlFlow::Poll); let mut app_host = AppHost::new(app, model); event_loop.run_app(&mut app_host).unwrap(); }