show_image/event/
window.rs

1use super::AxisId;
2use super::DeviceId;
3use super::ElementState;
4use super::KeyboardInput;
5use super::ModifiersState;
6use super::MouseButton;
7use super::MouseButtonState;
8use super::MouseScrollDelta;
9use super::Theme;
10use super::Touch;
11use super::TouchPhase;
12use crate::WindowId;
13
14use std::path::PathBuf;
15
16/// Window event.
17#[derive(Debug, Clone)]
18pub enum WindowEvent {
19	/// A redraw was requested by the OS or application code.
20	RedrawRequested(WindowRedrawRequestedEvent),
21
22	/// A window was resized.
23	Resized(WindowResizedEvent),
24
25	/// A window was moved.
26	Moved(WindowMovedEvent),
27
28	/// A window was closed.
29	CloseRequested(WindowCloseRequestedEvent),
30
31	/// A window was destroyed.
32	Destroyed(WindowDestroyedEvent),
33
34	/// A file was dropped on a window.
35	DroppedFile(WindowDroppedFileEvent),
36
37	/// A file is being hovered over a window.
38	HoveredFile(WindowHoveredFileEvent),
39
40	/// A file that was being hovered over a window was canceled..
41	HoveredFileCancelled(WindowHoveredFileCancelledEvent),
42
43	/// A window gained input focus.
44	FocusGained(WindowFocusGainedEvent),
45
46	/// A window lost input focus.
47	FocusLost(WindowFocusLostEvent),
48
49	/// A window received keyboard input.
50	KeyboardInput(WindowKeyboardInputEvent),
51
52	/// A window received text input.
53	TextInput(WindowTextInputEvent),
54
55	/// The mouse cursor entered a window.
56	MouseEnter(WindowMouseEnterEvent),
57
58	/// The mouse cursor left a window.
59	MouseLeave(WindowMouseLeaveEvent),
60
61	/// The mouse cursor was moved on a window.
62	MouseMove(WindowMouseMoveEvent),
63
64	/// A mouse button was pressed or released on a window.
65	MouseButton(WindowMouseButtonEvent),
66
67	/// A window received mouse wheel input.
68	MouseWheel(WindowMouseWheelEvent),
69
70	/// A window received axis motion input.
71	AxisMotion(WindowAxisMotionEvent),
72
73	/// A window received touchpad pressure input.
74	TouchpadPressure(WindowTouchpadPressureEvent),
75
76	/// A window received a touchpad magnify event.
77	///
78	/// On supported platforms, the event is triggered moving two fingers towards or away from each-other on the touchpad.
79	///
80	/// *Platform specific:* Only available on macOS.
81	TouchpadMagnify(WindowTouchpadMagnifyEvent),
82
83	/// A window received a touchpad rotate event.
84	///
85	/// On supported platforms, the event is triggered putting two fingers on the touchpad and rotating them.
86	///
87	/// *Platform specific:* Only available on macOS.
88	TouchpadRotate(WindowTouchpadRotateEvent),
89
90	/// A window received touch input.
91	Touch(WindowTouchEvent),
92
93	/// The scale factor between logical and physical pixels for a window changed.
94	ScaleFactorChanged(WindowScaleFactorChangedEvent),
95
96	/// The theme for a window changed.
97	ThemeChanged(WindowThemeChangedEvent),
98}
99
100impl WindowEvent {
101	/// Get the window ID of the event.
102	pub fn window_id(&self) -> WindowId {
103		match self {
104			Self::RedrawRequested(x) => x.window_id,
105			Self::Resized(x) => x.window_id,
106			Self::Moved(x) => x.window_id,
107			Self::CloseRequested(x) => x.window_id,
108			Self::Destroyed(x) => x.window_id,
109			Self::DroppedFile(x) => x.window_id,
110			Self::HoveredFile(x) => x.window_id,
111			Self::HoveredFileCancelled(x) => x.window_id,
112			Self::FocusGained(x) => x.window_id,
113			Self::FocusLost(x) => x.window_id,
114			Self::KeyboardInput(x) => x.window_id,
115			Self::TextInput(x) => x.window_id,
116			Self::MouseEnter(x) => x.window_id,
117			Self::MouseLeave(x) => x.window_id,
118			Self::MouseMove(x) => x.window_id,
119			Self::MouseButton(x) => x.window_id,
120			Self::MouseWheel(x) => x.window_id,
121			Self::AxisMotion(x) => x.window_id,
122			Self::TouchpadPressure(x) => x.window_id,
123			Self::TouchpadMagnify(x) => x.window_id,
124			Self::TouchpadRotate(x) => x.window_id,
125			Self::Touch(x) => x.window_id,
126			Self::ScaleFactorChanged(x) => x.window_id,
127			Self::ThemeChanged(x) => x.window_id,
128		}
129	}
130}
131
132/// A redraw was requested by the OS or application code.
133#[derive(Debug, Clone)]
134pub struct WindowRedrawRequestedEvent {
135	/// The ID of the window.
136	pub window_id: WindowId,
137}
138
139/// A window was resized.
140#[derive(Debug, Clone)]
141pub struct WindowResizedEvent {
142	/// The ID of the window.
143	pub window_id: WindowId,
144
145	/// The new size of the window in physical pixels.
146	pub size: glam::UVec2,
147}
148
149/// A window was moved.
150#[derive(Debug, Clone)]
151pub struct WindowMovedEvent {
152	/// The ID of the window.
153	pub window_id: WindowId,
154
155	/// The new position of the window in physical pixels.
156	pub position: glam::IVec2,
157}
158
159/// A window was closed.
160#[derive(Debug, Clone)]
161pub struct WindowCloseRequestedEvent {
162	/// The ID of the window.
163	pub window_id: WindowId,
164}
165
166/// A window was destroyed.
167#[derive(Debug, Clone)]
168pub struct WindowDestroyedEvent {
169	/// The ID of the window.
170	pub window_id: WindowId,
171}
172
173/// A file was dropped on a window.
174#[derive(Debug, Clone)]
175pub struct WindowDroppedFileEvent {
176	/// The ID of the window.
177	pub window_id: WindowId,
178
179	/// The path of the file.
180	pub file: PathBuf,
181}
182
183/// A file is being hovered over a window.
184#[derive(Debug, Clone)]
185pub struct WindowHoveredFileEvent {
186	/// The ID of the window.
187	pub window_id: WindowId,
188
189	/// The path of the file.
190	pub file: PathBuf,
191}
192
193/// A file that was being hovered over a window was canceled..
194#[derive(Debug, Clone)]
195pub struct WindowHoveredFileCancelledEvent {
196	/// The ID of the window.
197	pub window_id: WindowId,
198}
199
200/// A window gained input focus.
201#[derive(Debug, Clone)]
202pub struct WindowFocusGainedEvent {
203	/// The ID of the window.
204	pub window_id: WindowId,
205}
206
207/// A window lost input focus.
208#[derive(Debug, Clone)]
209pub struct WindowFocusLostEvent {
210	/// The ID of the window.
211	pub window_id: WindowId,
212}
213
214/// A window received keyboard input.
215#[derive(Debug, Clone)]
216pub struct WindowKeyboardInputEvent {
217	/// The ID of the window.
218	pub window_id: WindowId,
219
220	/// The device that generated the input.
221	pub device_id: DeviceId,
222
223	/// The received input.
224	pub input: KeyboardInput,
225
226	/// Flag to indicate if the input is synthetic.
227	///
228	/// Some synthetic events may be generated to report changes in keyboard state while the window did not have input focus.
229	/// This flag allows you to distinguish such events.
230	pub is_synthetic: bool,
231}
232
233/// A window received text input.
234#[derive(Debug, Clone)]
235pub struct WindowTextInputEvent {
236	/// The ID of the window.
237	pub window_id: WindowId,
238
239	/// The unicode codepoint representing the input.
240	pub character: char,
241}
242
243/// The mouse cursor entered the window area.
244#[derive(Debug, Clone)]
245pub struct WindowMouseEnterEvent {
246	/// The ID of the window.
247	pub window_id: WindowId,
248
249	/// The device that generated the input.
250	pub device_id: DeviceId,
251
252	/// The pressed state of all mouse buttons.
253	pub buttons: MouseButtonState,
254}
255
256/// The mouse cursor left the window area.
257#[derive(Debug, Clone)]
258pub struct WindowMouseLeaveEvent {
259	/// The ID of the window.
260	pub window_id: WindowId,
261
262	/// The device that generated the input.
263	pub device_id: DeviceId,
264
265	/// The pressed state of all mouse buttons.
266	pub buttons: MouseButtonState,
267}
268
269/// The mouse cursor was moved on a window.
270#[derive(Debug, Clone)]
271pub struct WindowMouseMoveEvent {
272	/// The ID of the window.
273	pub window_id: WindowId,
274
275	/// The device that generated the input.
276	pub device_id: DeviceId,
277
278	/// The new position of the cursor in physical pixels, relative to the top-left corner of the window.
279	pub position: glam::Vec2,
280
281	/// The position of the mouse cursor before the last movement.
282	pub prev_position: glam::Vec2,
283
284	/// The pressed state of all mouse buttons.
285	pub buttons: MouseButtonState,
286
287	/// The state of the keyboard modifiers at the time of the event.
288	pub modifiers: ModifiersState,
289}
290
291/// A window received mouse input.
292#[derive(Debug, Clone)]
293pub struct WindowMouseButtonEvent {
294	/// The ID of the window.
295	pub window_id: WindowId,
296
297	/// The device that generated the input.
298	pub device_id: DeviceId,
299
300	/// The mouse button that was pressed.
301	pub button: MouseButton,
302
303	/// The new state of the mouse button.
304	pub state: ElementState,
305
306	/// The current position of the mouse cursor inside the window.
307	pub position: glam::Vec2,
308
309	/// The position of the mouse cursor before the last movement.
310	pub prev_position: glam::Vec2,
311
312	/// The pressed state of all mouse buttons.
313	pub buttons: MouseButtonState,
314
315	/// The state of the keyboard modifiers at the time of the event.
316	pub modifiers: ModifiersState,
317}
318
319/// A window received mouse wheel input.
320#[derive(Debug, Clone)]
321pub struct WindowMouseWheelEvent {
322	/// The ID of the window.
323	pub window_id: WindowId,
324
325	/// The device that generated the input.
326	pub device_id: DeviceId,
327
328	/// The scroll delta of the mouse wheel.
329	pub delta: MouseScrollDelta,
330
331	/// The touch-screen input state.
332	pub phase: TouchPhase,
333
334	/// The current position of the mouse cursor inside the window.
335	pub position: Option<glam::Vec2>,
336
337	/// The pressed state of all mouse buttons.
338	pub buttons: MouseButtonState,
339
340	/// The state of the keyboard modifiers at the time of the event.
341	pub modifiers: ModifiersState,
342}
343
344/// A window received axis motion input.
345#[derive(Debug, Clone)]
346pub struct WindowAxisMotionEvent {
347	/// The ID of the window.
348	pub window_id: WindowId,
349
350	/// The device that generated the input.
351	pub device_id: DeviceId,
352
353	/// The axis that as moved.
354	pub axis: AxisId,
355
356	/// The value by which the axis moved.
357	pub value: f64,
358}
359
360/// A window received touchpad pressure input.
361#[derive(Debug, Clone)]
362pub struct WindowTouchpadPressureEvent {
363	/// The ID of the window.
364	pub window_id: WindowId,
365
366	/// The device that generated the input.
367	pub device_id: DeviceId,
368
369	/// The pressure on the touch pad, in the range 0 to 1.
370	pub pressure: f32,
371
372	/// The click level of the touch pad.
373	pub stage: i64,
374}
375
376/// A window received touchpad magnify input.
377#[derive(Debug, Clone)]
378pub struct WindowTouchpadMagnifyEvent {
379	/// The ID of the window.
380	pub window_id: WindowId,
381
382	/// The device that generated the input.
383	pub device_id: DeviceId,
384
385	/// The scaling to be applied.
386	///
387	/// Values between 0.0 and 1.0 indicate zooming out.
388	/// Values above 1.0 indicate zooming in.
389	pub scale: f64,
390
391	/// The touch phase for the event.
392	pub phase: TouchPhase,
393}
394
395/// A window received touchpad rotate input.
396#[derive(Debug, Clone)]
397pub struct WindowTouchpadRotateEvent {
398	/// The ID of the window.
399	pub window_id: WindowId,
400
401	/// The device that generated the input.
402	pub device_id: DeviceId,
403
404	/// The rotation angle in radians.
405	///
406	/// A positive rotation is counter clockwise.
407	pub angle_radians: f64,
408
409	/// The touch phase for the event.
410	pub phase: TouchPhase,
411}
412
413/// A window received touch input.
414#[derive(Debug, Clone)]
415pub struct WindowTouchEvent {
416	/// The ID of the window.
417	pub window_id: WindowId,
418
419	/// The touch input.
420	pub touch: Touch,
421}
422
423/// The scale factor between logical and physical pixels for a window changed.
424#[derive(Debug, Clone)]
425pub struct WindowScaleFactorChangedEvent {
426	/// The ID of the window.
427	pub window_id: WindowId,
428
429	/// The new scale factor as physical pixels per logical pixel.
430	pub scale_factor: f64,
431}
432
433/// The theme for a window changed.
434#[derive(Debug, Clone)]
435pub struct WindowThemeChangedEvent {
436	/// The ID of the window.
437	pub window_id: WindowId,
438
439	/// The new theme of the window.
440	pub theme: Theme,
441}
442
443impl_from_variant!(WindowEvent::RedrawRequested(WindowRedrawRequestedEvent));
444impl_from_variant!(WindowEvent::Resized(WindowResizedEvent));
445impl_from_variant!(WindowEvent::Moved(WindowMovedEvent));
446impl_from_variant!(WindowEvent::CloseRequested(WindowCloseRequestedEvent));
447impl_from_variant!(WindowEvent::Destroyed(WindowDestroyedEvent));
448impl_from_variant!(WindowEvent::DroppedFile(WindowDroppedFileEvent));
449impl_from_variant!(WindowEvent::HoveredFile(WindowHoveredFileEvent));
450impl_from_variant!(WindowEvent::HoveredFileCancelled(WindowHoveredFileCancelledEvent));
451impl_from_variant!(WindowEvent::FocusGained(WindowFocusGainedEvent));
452impl_from_variant!(WindowEvent::FocusLost(WindowFocusLostEvent));
453impl_from_variant!(WindowEvent::KeyboardInput(WindowKeyboardInputEvent));
454impl_from_variant!(WindowEvent::TextInput(WindowTextInputEvent));
455impl_from_variant!(WindowEvent::MouseEnter(WindowMouseEnterEvent));
456impl_from_variant!(WindowEvent::MouseLeave(WindowMouseLeaveEvent));
457impl_from_variant!(WindowEvent::MouseMove(WindowMouseMoveEvent));
458impl_from_variant!(WindowEvent::MouseButton(WindowMouseButtonEvent));
459impl_from_variant!(WindowEvent::MouseWheel(WindowMouseWheelEvent));
460impl_from_variant!(WindowEvent::AxisMotion(WindowAxisMotionEvent));
461impl_from_variant!(WindowEvent::TouchpadPressure(WindowTouchpadPressureEvent));
462impl_from_variant!(WindowEvent::TouchpadMagnify(WindowTouchpadMagnifyEvent));
463impl_from_variant!(WindowEvent::TouchpadRotate(WindowTouchpadRotateEvent));
464impl_from_variant!(WindowEvent::Touch(WindowTouchEvent));
465impl_from_variant!(WindowEvent::ScaleFactorChanged(WindowScaleFactorChangedEvent));
466impl_from_variant!(WindowEvent::ThemeChanged(WindowThemeChangedEvent));