winit_app/
app_listener.rs1use log::warn;
4use winit::application::ApplicationHandler;
5use winit::event::WindowEvent;
6use winit::event_loop::ActiveEventLoop;
7use winit::window::{Window, WindowAttributes, WindowId};
8
9pub enum AppWindowEvent<'a> {
12 NewWindow(Box<dyn Window>),
13 OnWindowEvent(WindowEvent, &'a dyn ActiveEventLoop),
14}
15
16pub(crate) struct AppListener<F>
17where
18 F: FnMut(AppWindowEvent),
19{
20 fn_on_listen_window: F,
21
22 window_attributes: WindowAttributes,
23}
24
25impl<F> AppListener<F>
26where
27 F: FnMut(AppWindowEvent),
28{
29 pub(crate) fn new(window_attributes: WindowAttributes, fn_on_listen_window: F) -> Self
30 where
31 F: FnMut(AppWindowEvent),
32 {
33 Self {
34 window_attributes,
35 fn_on_listen_window,
36 }
37 }
38}
39
40impl<F> ApplicationHandler for AppListener<F>
41where
42 F: FnMut(AppWindowEvent),
43{
44 fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
45 match event_loop.create_window(self.window_attributes.clone()) {
49 Ok(window) => {
50 (self.fn_on_listen_window)(AppWindowEvent::NewWindow(window));
51 }
52 Err(err) => {
53 warn!("Error creating a new window {:?}", err);
54 }
55 }
56 }
57
58 fn window_event(
59 &mut self,
60 event_loop: &dyn ActiveEventLoop,
61 _id: WindowId,
62 event: WindowEvent,
63 ) {
64 (self.fn_on_listen_window)(AppWindowEvent::OnWindowEvent(event, event_loop));
65 }
66}