use std::sync::Arc; use winit::{ application::ApplicationHandler, event::WindowEvent, event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy}, window::{Window, WindowId}, }; use crate::client::ClientEvent; use super::Client; #[derive(Clone)] pub struct AppHandle { pub proxy: EventLoopProxy, pub window: Arc, } pub struct App { client: Option, proxy: EventLoopProxy, } impl App { pub fn run() { let event_loop = EventLoop::with_user_event().build().unwrap(); let proxy = event_loop.create_proxy(); event_loop .run_app(&mut App { client: Default::default(), proxy, }) .unwrap(); } } impl ApplicationHandler for App { fn resumed(&mut self, event_loop: &ActiveEventLoop) { if self.client.is_none() { let window = event_loop .create_window(Window::default_attributes()) .unwrap(); let client = Client::new(AppHandle { proxy: self.proxy.clone(), window: window.into(), }); self.client = Some(client); } } fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { let client = self.client.as_mut().unwrap(); client.window_event(event, event_loop); } fn user_event(&mut self, event_loop: &ActiveEventLoop, event: ClientEvent) { let client = self.client.as_mut().unwrap(); client.event(event, event_loop); } } impl AppHandle { pub fn send(&self, event: ClientEvent) { self.proxy.send_event(event).unwrap(); self.window.request_redraw(); } }