use std::collections::HashMap; use snafu::prelude::*; use winit::{ application::ApplicationHandler, error::{EventLoopError, OsError}, event::{StartCause, WindowEvent}, event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy}, window::WindowId, }; use crate::{event::Event, window::WindowContext}; #[derive(Debug, Snafu)] pub enum Error { #[snafu(display("failure to create the initial window"))] CreateInitialWindow { source: OsError }, #[snafu(display("failure to initialize event loop"))] InitializeEventLoop { source: EventLoopError }, } #[derive(Debug)] pub struct Processor { windows: HashMap, proxy: EventLoopProxy, initial_window_error: Option, } impl Processor { #[must_use] pub fn new(event_loop: &EventLoop) -> Self { Self { windows: Default::default(), proxy: event_loop.create_proxy(), initial_window_error: None, } } pub fn create_window(&mut self, event_loop: &ActiveEventLoop) -> Result<(), OsError> { let window_context = WindowContext::new(event_loop)?; self.windows.insert(window_context.id(), window_context); Ok(()) } pub fn run(&mut self, event_loop: EventLoop) -> Result<(), Error> { event_loop.run_app(self).context(InitializeEventLoopSnafu)?; match self.initial_window_error.take() { Some(initial_window_error) => { Err(initial_window_error).context(CreateInitialWindowSnafu) } None => Ok(()), } } } impl ApplicationHandler for Processor { fn resumed(&mut self, event_loop: &ActiveEventLoop) { if let Err(err) = self.create_window(event_loop) { self.initial_window_error = Some(err); event_loop.exit(); return; } } fn window_event( &mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent, ) { } }