|
1 | 1 | /// Common boilerplate for setting up a winit application. |
2 | | -mod winit_app { |
3 | | - use std::marker::PhantomData; |
4 | | - use std::rc::Rc; |
5 | | - |
6 | | - use winit::application::ApplicationHandler; |
7 | | - use winit::event::{Event, WindowEvent}; |
8 | | - use winit::event_loop::{EventLoop, ActiveEventLoop}; |
9 | | - use winit::window::{WindowAttributes, WindowId, Window}; |
10 | | - |
11 | | - /// Run a Winit application. |
12 | | - #[allow(unused_mut)] |
13 | | - pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) { |
14 | | - #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] |
15 | | - event_loop.run_app(&mut app).unwrap(); |
16 | | - |
17 | | - #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] |
18 | | - winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app); |
19 | | - } |
20 | | - |
21 | | - /// Create a window from a set of window attributes. |
22 | | - #[allow(dead_code)] |
23 | | - pub(crate) fn make_window(elwt: &ActiveEventLoop, f: impl FnOnce(WindowAttributes) -> WindowAttributes) -> Rc<Window> { |
24 | | - let attributes = f(WindowAttributes::default()); |
25 | | - #[cfg(target_arch = "wasm32")] |
26 | | - let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append( |
27 | | - attributes, |
28 | | - true |
29 | | - ); |
30 | | - let window = elwt.create_window(attributes); |
31 | | - Rc::new(window.unwrap()) |
32 | | - } |
| 2 | +use std::marker::PhantomData; |
| 3 | +use std::rc::Rc; |
| 4 | + |
| 5 | +use winit::application::ApplicationHandler; |
| 6 | +use winit::event::{Event, WindowEvent}; |
| 7 | +use winit::event_loop::{ActiveEventLoop, EventLoop}; |
| 8 | +use winit::window::{Window, WindowAttributes, WindowId}; |
| 9 | + |
| 10 | +/// Run a Winit application. |
| 11 | +#[allow(unused_mut)] |
| 12 | +pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) { |
| 13 | + #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] |
| 14 | + event_loop.run_app(&mut app).unwrap(); |
| 15 | + |
| 16 | + #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] |
| 17 | + winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app); |
| 18 | +} |
33 | 19 |
|
34 | | - /// Easily constructable winit application. |
35 | | - pub(crate) struct WinitApp<T, Init, Handler> { |
36 | | - /// Closure to initialize state. |
37 | | - init: Init, |
| 20 | +/// Create a window from a set of window attributes. |
| 21 | +#[allow(dead_code)] |
| 22 | +pub(crate) fn make_window( |
| 23 | + elwt: &ActiveEventLoop, |
| 24 | + f: impl FnOnce(WindowAttributes) -> WindowAttributes, |
| 25 | +) -> Rc<Window> { |
| 26 | + let attributes = f(WindowAttributes::default()); |
| 27 | + #[cfg(target_arch = "wasm32")] |
| 28 | + let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append(attributes, true); |
| 29 | + let window = elwt.create_window(attributes); |
| 30 | + Rc::new(window.unwrap()) |
| 31 | +} |
38 | 32 |
|
39 | | - /// Closure to run on window events. |
40 | | - event: Handler, |
| 33 | +/// Easily constructable winit application. |
| 34 | +pub(crate) struct WinitApp<T, Init, Handler> { |
| 35 | + /// Closure to initialize state. |
| 36 | + init: Init, |
41 | 37 |
|
42 | | - /// Contained state. |
43 | | - state: Option<T> |
44 | | - } |
| 38 | + /// Closure to run on window events. |
| 39 | + event: Handler, |
45 | 40 |
|
46 | | - /// Builder that makes it so we don't have to name `T`. |
47 | | - pub(crate) struct WinitAppBuilder<T, Init> { |
48 | | - /// Closure to initialize state. |
49 | | - init: Init, |
| 41 | + /// Contained state. |
| 42 | + state: Option<T>, |
| 43 | +} |
50 | 44 |
|
51 | | - /// Eat the type parameter. |
52 | | - _marker: PhantomData<Option<T>>, |
53 | | - } |
| 45 | +/// Builder that makes it so we don't have to name `T`. |
| 46 | +pub(crate) struct WinitAppBuilder<T, Init> { |
| 47 | + /// Closure to initialize state. |
| 48 | + init: Init, |
54 | 49 |
|
55 | | - impl<T, Init> WinitAppBuilder<T, Init> |
56 | | - where Init: FnMut(&ActiveEventLoop) -> T, |
57 | | - { |
58 | | - /// Create with an "init" closure. |
59 | | - pub(crate) fn with_init(init: Init) -> Self { |
60 | | - Self { |
61 | | - init, |
62 | | - _marker: PhantomData |
63 | | - } |
64 | | - } |
| 50 | + /// Eat the type parameter. |
| 51 | + _marker: PhantomData<Option<T>>, |
| 52 | +} |
65 | 53 |
|
66 | | - /// Build a new application. |
67 | | - pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, Init, F> |
68 | | - where F: FnMut(&mut T, Event<()>, &ActiveEventLoop) |
69 | | - { |
70 | | - WinitApp::new(self.init, handler) |
| 54 | +impl<T, Init> WinitAppBuilder<T, Init> |
| 55 | +where |
| 56 | + Init: FnMut(&ActiveEventLoop) -> T, |
| 57 | +{ |
| 58 | + /// Create with an "init" closure. |
| 59 | + pub(crate) fn with_init(init: Init) -> Self { |
| 60 | + Self { |
| 61 | + init, |
| 62 | + _marker: PhantomData, |
71 | 63 | } |
72 | 64 | } |
73 | 65 |
|
74 | | - impl<T, Init, Handler> WinitApp<T, Init, Handler> |
75 | | - where Init: FnMut(&ActiveEventLoop) -> T, |
76 | | - Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop) |
| 66 | + /// Build a new application. |
| 67 | + pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, Init, F> |
| 68 | + where |
| 69 | + F: FnMut(&mut T, Event<()>, &ActiveEventLoop), |
77 | 70 | { |
78 | | - /// Create a new application. |
79 | | - pub(crate) fn new(init: Init, event: Handler) -> Self { |
80 | | - Self { |
81 | | - init, |
82 | | - event, |
83 | | - state: None |
84 | | - } |
85 | | - } |
| 71 | + WinitApp::new(self.init, handler) |
86 | 72 | } |
| 73 | +} |
87 | 74 |
|
88 | | - impl<T, Init, Handler> ApplicationHandler for WinitApp<T, Init, Handler> |
89 | | - where Init: FnMut(&ActiveEventLoop) -> T, |
90 | | - Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop) { |
91 | | - |
92 | | - fn resumed(&mut self, el: &ActiveEventLoop) { |
93 | | - debug_assert!(self.state.is_none()); |
94 | | - self.state = Some((self.init)(el)); |
| 75 | +impl<T, Init, Handler> WinitApp<T, Init, Handler> |
| 76 | +where |
| 77 | + Init: FnMut(&ActiveEventLoop) -> T, |
| 78 | + Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop), |
| 79 | +{ |
| 80 | + /// Create a new application. |
| 81 | + pub(crate) fn new(init: Init, event: Handler) -> Self { |
| 82 | + Self { |
| 83 | + init, |
| 84 | + event, |
| 85 | + state: None, |
95 | 86 | } |
| 87 | + } |
| 88 | +} |
96 | 89 |
|
97 | | - fn suspended(&mut self, _event_loop: &ActiveEventLoop) { |
98 | | - let state = self.state.take(); |
99 | | - debug_assert!(state.is_some()); |
100 | | - drop(state); |
101 | | - } |
| 90 | +impl<T, Init, Handler> ApplicationHandler for WinitApp<T, Init, Handler> |
| 91 | +where |
| 92 | + Init: FnMut(&ActiveEventLoop) -> T, |
| 93 | + Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop), |
| 94 | +{ |
| 95 | + fn resumed(&mut self, el: &ActiveEventLoop) { |
| 96 | + debug_assert!(self.state.is_none()); |
| 97 | + self.state = Some((self.init)(el)); |
| 98 | + } |
102 | 99 |
|
103 | | - fn window_event( |
104 | | - &mut self, |
105 | | - event_loop: &ActiveEventLoop, |
106 | | - window_id: WindowId, |
107 | | - event: WindowEvent, |
108 | | - ) { |
109 | | - let state = self.state.as_mut().unwrap(); |
110 | | - (self.event)(state, Event::WindowEvent { |
111 | | - window_id, |
112 | | - event |
113 | | - }, event_loop); |
114 | | - } |
| 100 | + fn suspended(&mut self, _event_loop: &ActiveEventLoop) { |
| 101 | + let state = self.state.take(); |
| 102 | + debug_assert!(state.is_some()); |
| 103 | + drop(state); |
| 104 | + } |
| 105 | + |
| 106 | + fn window_event( |
| 107 | + &mut self, |
| 108 | + event_loop: &ActiveEventLoop, |
| 109 | + window_id: WindowId, |
| 110 | + event: WindowEvent, |
| 111 | + ) { |
| 112 | + let state = self.state.as_mut().unwrap(); |
| 113 | + (self.event)(state, Event::WindowEvent { window_id, event }, event_loop); |
| 114 | + } |
115 | 115 |
|
116 | | - fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { |
117 | | - if let Some(state) = self.state.as_mut() { |
118 | | - (self.event)(state, Event::AboutToWait, event_loop); |
119 | | - } |
| 116 | + fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { |
| 117 | + if let Some(state) = self.state.as_mut() { |
| 118 | + (self.event)(state, Event::AboutToWait, event_loop); |
120 | 119 | } |
121 | 120 | } |
122 | 121 | } |
0 commit comments