use std::sync::Arc; use tokio::runtime::Runtime; use winit::dpi::LogicalSize; use winit::event::WindowEvent; use winit::window::Window; mod rendering; pub struct Application { window_state: Option, runtime: Runtime, } impl Application { pub fn new() -> Self { Self { window_state: None, runtime: Runtime::new().expect("Failed to create tokio runtime"), } } } impl winit::application::ApplicationHandler for Application { fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { if self.window_state.is_some() { return; } // Set up window let (width, height) = (800, 600); let window_attributes = Window::default_attributes() .with_inner_size(LogicalSize::new(width as f64, height as f64)) .with_title("Terminal emulator"); let window = Arc::new(event_loop.create_window(window_attributes).unwrap()); self.window_state = Some(self.runtime.block_on(rendering::State::new(window))); } fn window_event( &mut self, event_loop: &winit::event_loop::ActiveEventLoop, _window_id: winit::window::WindowId, event: WindowEvent, ) { let Some(state) = &mut self.window_state else { return; }; match event { WindowEvent::Resized(size) => { state.resize(size); } WindowEvent::RedrawRequested => { state.render().unwrap(); } WindowEvent::KeyboardInput { device_id: _, event, is_synthetic: _, } => { state.handle_keyboard_input(&event); } WindowEvent::ModifiersChanged(modifiers) => { state.update_modifiers(modifiers.state()); } WindowEvent::CloseRequested => event_loop.exit(), _ => {} } } }