Files
openworm/src/client/app.rs
2025-11-15 01:10:27 -05:00

69 lines
1.7 KiB
Rust

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<ClientEvent>,
pub window: Arc<Window>,
}
pub struct App {
client: Option<Client>,
proxy: EventLoopProxy<ClientEvent>,
}
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<ClientEvent> 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();
}
}