async_winit/window/
registration.rs

1/*
2
3`async-winit` is free software: you can redistribute it and/or modify it under the terms of one of
4the following licenses:
5
6* GNU Lesser General Public License as published by the Free Software Foundation, either
7  version 3 of the License, or (at your option) any later version.
8* Mozilla Public License as published by the Mozilla Foundation, version 2.
9
10`async-winit` is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
11the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
12Public License and the Patron License for more details.
13
14You should have received a copy of the GNU Lesser General Public License and the Mozilla
15Public License along with `async-winit`. If not, see <https://www.gnu.org/licenses/>.
16
17*/
18
19//! Registration of the window into the reactor.
20
21use crate::dpi::PhysicalSize;
22use crate::handler::Handler;
23use crate::sync::ThreadSafety;
24use crate::Event;
25
26use winit::dpi::PhysicalPosition;
27use winit::event::{
28    AxisId, DeviceId, ElementState, Ime, ModifiersState, MouseButton, MouseScrollDelta, Touch,
29    TouchPhase, WindowEvent,
30};
31use winit::window::Theme;
32
33#[derive(Clone)]
34pub struct KeyboardInput {
35    pub device_id: DeviceId,
36    pub input: winit::event::KeyboardInput,
37    pub is_synthetic: bool,
38}
39
40#[derive(Clone)]
41pub struct CursorMoved {
42    pub device_id: DeviceId,
43    pub position: PhysicalPosition<f64>,
44}
45
46#[derive(Clone)]
47pub struct MouseWheel {
48    pub device_id: DeviceId,
49    pub delta: MouseScrollDelta,
50    pub phase: TouchPhase,
51}
52
53#[derive(Clone)]
54pub struct MouseInput {
55    pub device_id: DeviceId,
56    pub state: ElementState,
57    pub button: MouseButton,
58}
59
60#[derive(Clone)]
61pub struct TouchpadMagnify {
62    pub device_id: DeviceId,
63    pub delta: f64,
64    pub phase: TouchPhase,
65}
66
67#[derive(Clone)]
68pub struct TouchpadRotate {
69    pub device_id: DeviceId,
70    pub delta: f32,
71    pub phase: TouchPhase,
72}
73
74#[derive(Clone)]
75pub struct TouchpadPressure {
76    pub device_id: DeviceId,
77    pub pressure: f32,
78    pub stage: i64,
79}
80
81#[derive(Clone)]
82pub struct AxisMotion {
83    pub device_id: DeviceId,
84    pub axis: AxisId,
85    pub value: f64,
86}
87
88pub struct ScaleFactor;
89
90pub struct ScaleFactorChanging<'a> {
91    pub scale_factor: f64,
92    pub new_inner_size: &'a mut PhysicalSize<u32>,
93}
94
95#[derive(Clone)]
96pub struct ScaleFactorChanged {
97    pub scale_factor: f64,
98    pub new_inner_size: PhysicalSize<u32>,
99}
100
101impl Event for ScaleFactor {
102    type Clonable = ScaleFactorChanged;
103    type Unique<'a> = ScaleFactorChanging<'a>;
104
105    fn downgrade(unique: &mut Self::Unique<'_>) -> Self::Clonable {
106        ScaleFactorChanged {
107            scale_factor: unique.scale_factor,
108            new_inner_size: *unique.new_inner_size,
109        }
110    }
111}
112
113pub(crate) struct Registration<TS: ThreadSafety> {
114    /// `RedrawRequested`
115    pub(crate) redraw_requested: Handler<(), TS>,
116
117    /// `Event::CloseRequested`.
118    pub(crate) close_requested: Handler<(), TS>,
119
120    /// `Event::Resized`.
121    pub(crate) resized: Handler<PhysicalSize<u32>, TS>,
122
123    /// `Event::Moved`.
124    pub(crate) moved: Handler<PhysicalPosition<i32>, TS>,
125
126    /// `Event::Destroyed`.
127    pub(crate) destroyed: Handler<(), TS>,
128
129    /// `Event::Focused`.
130    pub(crate) focused: Handler<bool, TS>,
131
132    /// `Event::ReceivedCharacter`.
133    pub(crate) received_character: Handler<char, TS>,
134
135    /// `Event::KeyboardInput`.
136    pub(crate) keyboard_input: Handler<KeyboardInput, TS>,
137
138    /// `Event::ModifiersState`
139    pub(crate) modifiers_changed: Handler<ModifiersState, TS>,
140
141    /// `Event::Ime`
142    pub(crate) ime: Handler<Ime, TS>,
143
144    /// `Event::CursorMoved`
145    pub(crate) cursor_moved: Handler<CursorMoved, TS>,
146
147    /// `Event::CursorEntered`
148    pub(crate) cursor_entered: Handler<DeviceId, TS>,
149
150    /// `Event::CursorLeft`
151    pub(crate) cursor_left: Handler<DeviceId, TS>,
152
153    /// `Event::MouseWheel`
154    pub(crate) mouse_wheel: Handler<MouseWheel, TS>,
155
156    /// `Event::MouseInput`
157    pub(crate) mouse_input: Handler<MouseInput, TS>,
158
159    /// `Event::TouchpadMagnify`
160    pub(crate) touchpad_magnify: Handler<TouchpadMagnify, TS>,
161
162    /// `Event::SmartMagnify`.
163    pub(crate) smart_magnify: Handler<DeviceId, TS>,
164
165    /// `Event::TouchpadRotate`
166    pub(crate) touchpad_rotate: Handler<TouchpadRotate, TS>,
167
168    /// `Event::TouchpadPressure`
169    pub(crate) touchpad_pressure: Handler<TouchpadPressure, TS>,
170
171    /// `Event::AxisMotion`
172    pub(crate) axis_motion: Handler<AxisMotion, TS>,
173
174    /// `Event::Touch`
175    pub(crate) touch: Handler<Touch, TS>,
176
177    /// `Event::ScaleFactorChanged`
178    pub(crate) scale_factor_changed: Handler<ScaleFactor, TS>,
179
180    /// `Event::ThemeChanged`
181    pub(crate) theme_changed: Handler<Theme, TS>,
182
183    /// `Event::Occluded`
184    pub(crate) occluded: Handler<bool, TS>,
185}
186
187impl<TS: ThreadSafety> Registration<TS> {
188    pub(crate) fn new() -> Self {
189        Self {
190            close_requested: Handler::new(),
191            resized: Handler::new(),
192            redraw_requested: Handler::new(),
193            moved: Handler::new(),
194            destroyed: Handler::new(),
195            focused: Handler::new(),
196            keyboard_input: Handler::new(),
197            received_character: Handler::new(),
198            modifiers_changed: Handler::new(),
199            ime: Handler::new(),
200            cursor_entered: Handler::new(),
201            cursor_left: Handler::new(),
202            cursor_moved: Handler::new(),
203            axis_motion: Handler::new(),
204            scale_factor_changed: Handler::new(),
205            smart_magnify: Handler::new(),
206            theme_changed: Handler::new(),
207            touch: Handler::new(),
208            touchpad_magnify: Handler::new(),
209            touchpad_pressure: Handler::new(),
210            touchpad_rotate: Handler::new(),
211            mouse_input: Handler::new(),
212            mouse_wheel: Handler::new(),
213            occluded: Handler::new(),
214        }
215    }
216
217    pub(crate) async fn signal(&self, event: WindowEvent<'_>) {
218        match event {
219            WindowEvent::CloseRequested => self.close_requested.run_with(&mut ()).await,
220            WindowEvent::Resized(mut size) => self.resized.run_with(&mut size).await,
221            WindowEvent::Moved(mut posn) => self.moved.run_with(&mut posn).await,
222            WindowEvent::AxisMotion {
223                device_id,
224                axis,
225                value,
226            } => {
227                self.axis_motion
228                    .run_with(&mut AxisMotion {
229                        device_id,
230                        axis,
231                        value,
232                    })
233                    .await
234            }
235            WindowEvent::CursorEntered { mut device_id } => {
236                self.cursor_entered.run_with(&mut device_id).await
237            }
238            WindowEvent::CursorLeft { mut device_id } => {
239                self.cursor_left.run_with(&mut device_id).await
240            }
241            WindowEvent::CursorMoved {
242                device_id,
243                position,
244                ..
245            } => {
246                self.cursor_moved
247                    .run_with(&mut CursorMoved {
248                        device_id,
249                        position,
250                    })
251                    .await
252            }
253            WindowEvent::Destroyed => self.destroyed.run_with(&mut ()).await,
254            WindowEvent::Focused(mut foc) => self.focused.run_with(&mut foc).await,
255            WindowEvent::Ime(mut ime) => self.ime.run_with(&mut ime).await,
256            WindowEvent::KeyboardInput {
257                device_id,
258                input,
259                is_synthetic,
260            } => {
261                self.keyboard_input
262                    .run_with(&mut KeyboardInput {
263                        device_id,
264                        input,
265                        is_synthetic,
266                    })
267                    .await
268            }
269            WindowEvent::ModifiersChanged(mut mods) => {
270                self.modifiers_changed.run_with(&mut mods).await
271            }
272            WindowEvent::MouseInput {
273                device_id,
274                state,
275                button,
276                ..
277            } => {
278                self.mouse_input
279                    .run_with(&mut MouseInput {
280                        device_id,
281                        state,
282                        button,
283                    })
284                    .await
285            }
286            WindowEvent::MouseWheel {
287                device_id,
288                delta,
289                phase,
290                ..
291            } => {
292                self.mouse_wheel
293                    .run_with(&mut MouseWheel {
294                        device_id,
295                        delta,
296                        phase,
297                    })
298                    .await
299            }
300            WindowEvent::Occluded(mut occ) => self.occluded.run_with(&mut occ).await,
301            WindowEvent::ReceivedCharacter(mut ch) => {
302                self.received_character.run_with(&mut ch).await
303            }
304            WindowEvent::ScaleFactorChanged {
305                scale_factor,
306                new_inner_size,
307            } => {
308                self.scale_factor_changed
309                    .run_with(&mut ScaleFactorChanging {
310                        scale_factor,
311                        new_inner_size,
312                    })
313                    .await
314            }
315            WindowEvent::SmartMagnify { mut device_id } => {
316                self.smart_magnify.run_with(&mut device_id).await
317            }
318            WindowEvent::ThemeChanged(mut theme) => self.theme_changed.run_with(&mut theme).await,
319            WindowEvent::Touch(mut touch) => self.touch.run_with(&mut touch).await,
320            WindowEvent::TouchpadMagnify {
321                device_id,
322                delta,
323                phase,
324            } => {
325                self.touchpad_magnify
326                    .run_with(&mut TouchpadMagnify {
327                        device_id,
328                        delta,
329                        phase,
330                    })
331                    .await
332            }
333            WindowEvent::TouchpadPressure {
334                device_id,
335                pressure,
336                stage,
337            } => {
338                self.touchpad_pressure
339                    .run_with(&mut TouchpadPressure {
340                        device_id,
341                        pressure,
342                        stage,
343                    })
344                    .await
345            }
346            WindowEvent::TouchpadRotate {
347                device_id,
348                delta,
349                phase,
350            } => {
351                self.touchpad_rotate
352                    .run_with(&mut TouchpadRotate {
353                        device_id,
354                        delta,
355                        phase,
356                    })
357                    .await
358            }
359            _ => {}
360        }
361    }
362}