1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use super::AxisId;
use super::DeviceId;
use super::ElementState;
use super::KeyboardInput;
use super::ModifiersState;
use super::MouseButton;
use super::MouseButtonState;
use super::MouseScrollDelta;
use super::Theme;
use super::Touch;
use super::TouchPhase;
use crate::WindowId;

use std::path::PathBuf;

/// Window event.
#[derive(Debug, Clone)]
pub enum WindowEvent {
	/// A redraw was requested by the OS or application code.
	RedrawRequested(WindowRedrawRequestedEvent),

	/// A window was resized.
	Resized(WindowResizedEvent),

	/// A window was moved.
	Moved(WindowMovedEvent),

	/// A window was closed.
	CloseRequested(WindowCloseRequestedEvent),

	/// A window was destroyed.
	Destroyed(WindowDestroyedEvent),

	/// A file was dropped on a window.
	DroppedFile(WindowDroppedFileEvent),

	/// A file is being hovered over a window.
	HoveredFile(WindowHoveredFileEvent),

	/// A file that was being hovered over a window was canceled..
	HoveredFileCancelled(WindowHoveredFileCancelledEvent),

	/// A window gained input focus.
	FocusGained(WindowFocusGainedEvent),

	/// A window lost input focus.
	FocusLost(WindowFocusLostEvent),

	/// A window received keyboard input.
	KeyboardInput(WindowKeyboardInputEvent),

	/// A window received text input.
	TextInput(WindowTextInputEvent),

	/// The mouse cursor entered a window.
	MouseEnter(WindowMouseEnterEvent),

	/// The mouse cursor left a window.
	MouseLeave(WindowMouseLeaveEvent),

	/// The mouse cursor was moved on a window.
	MouseMove(WindowMouseMoveEvent),

	/// A mouse button was pressed or released on a window.
	MouseButton(WindowMouseButtonEvent),

	/// A window received mouse wheel input.
	MouseWheel(WindowMouseWheelEvent),

	/// A window received axis motion input.
	AxisMotion(WindowAxisMotionEvent),

	/// A window received touchpad pressure input.
	TouchpadPressure(WindowTouchpadPressureEvent),

	/// A window received touch input.
	Touch(WindowTouchEvent),

	/// The scale factor between logical and physical pixels for a window changed.
	ScaleFactorChanged(WindowScaleFactorChangedEvent),

	/// The theme for a window changed.
	ThemeChanged(WindowThemeChangedEvent),
}

impl WindowEvent {
	/// Get the window ID of the event.
	pub fn window_id(&self) -> WindowId {
		match self {
			Self::RedrawRequested(x) => x.window_id,
			Self::Resized(x) => x.window_id,
			Self::Moved(x) => x.window_id,
			Self::CloseRequested(x) => x.window_id,
			Self::Destroyed(x) => x.window_id,
			Self::DroppedFile(x) => x.window_id,
			Self::HoveredFile(x) => x.window_id,
			Self::HoveredFileCancelled(x) => x.window_id,
			Self::FocusGained(x) => x.window_id,
			Self::FocusLost(x) => x.window_id,
			Self::KeyboardInput(x) => x.window_id,
			Self::TextInput(x) => x.window_id,
			Self::MouseEnter(x) => x.window_id,
			Self::MouseLeave(x) => x.window_id,
			Self::MouseMove(x) => x.window_id,
			Self::MouseButton(x) => x.window_id,
			Self::MouseWheel(x) => x.window_id,
			Self::AxisMotion(x) => x.window_id,
			Self::TouchpadPressure(x) => x.window_id,
			Self::Touch(x) => x.window_id,
			Self::ScaleFactorChanged(x) => x.window_id,
			Self::ThemeChanged(x) => x.window_id,
		}
	}
}

/// A redraw was requested by the OS or application code.
#[derive(Debug, Clone)]
pub struct WindowRedrawRequestedEvent {
	/// The ID of the window.
	pub window_id: WindowId,
}

/// A window was resized.
#[derive(Debug, Clone)]
pub struct WindowResizedEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The new size of the window in physical pixels.
	pub size: glam::UVec2,
}

/// A window was moved.
#[derive(Debug, Clone)]
pub struct WindowMovedEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The new position of the window in physical pixels.
	pub position: glam::IVec2,
}

/// A window was closed.
#[derive(Debug, Clone)]
pub struct WindowCloseRequestedEvent {
	/// The ID of the window.
	pub window_id: WindowId,
}

/// A window was destroyed.
#[derive(Debug, Clone)]
pub struct WindowDestroyedEvent {
	/// The ID of the window.
	pub window_id: WindowId,
}

/// A file was dropped on a window.
#[derive(Debug, Clone)]
pub struct WindowDroppedFileEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The path of the file.
	pub file: PathBuf,
}

/// A file is being hovered over a window.
#[derive(Debug, Clone)]
pub struct WindowHoveredFileEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The path of the file.
	pub file: PathBuf,
}

/// A file that was being hovered over a window was canceled..
#[derive(Debug, Clone)]
pub struct WindowHoveredFileCancelledEvent {
	/// The ID of the window.
	pub window_id: WindowId,
}

/// A window gained input focus.
#[derive(Debug, Clone)]
pub struct WindowFocusGainedEvent {
	/// The ID of the window.
	pub window_id: WindowId,
}

/// A window lost input focus.
#[derive(Debug, Clone)]
pub struct WindowFocusLostEvent {
	/// The ID of the window.
	pub window_id: WindowId,
}

/// A window received keyboard input.
#[derive(Debug, Clone)]
pub struct WindowKeyboardInputEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The received input.
	pub input: KeyboardInput,

	/// Flag to indicate if the input is synthetic.
	///
	/// Some synthetic events may be generated to report changes in keyboard state while the window did not have input focus.
	/// This flag allows you to distinguish such events.
	pub is_synthetic: bool,
}

/// A window received text input.
#[derive(Debug, Clone)]
pub struct WindowTextInputEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The unicode codepoint representing the input.
	pub character: char,
}

/// The mouse cursor entered the window area.
#[derive(Debug, Clone)]
pub struct WindowMouseEnterEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The pressed state of all mouse buttons.
	pub buttons: MouseButtonState,
}

/// The mouse cursor left the window area.
#[derive(Debug, Clone)]
pub struct WindowMouseLeaveEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The pressed state of all mouse buttons.
	pub buttons: MouseButtonState,
}

/// The mouse cursor was moved on a window.
#[derive(Debug, Clone)]
pub struct WindowMouseMoveEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The new position of the cursor in physical pixels, relative to the top-left corner of the window.
	pub position: glam::Vec2,

	/// The position of the mouse cursor before the last movement.
	pub prev_position: glam::Vec2,

	/// The pressed state of all mouse buttons.
	pub buttons: MouseButtonState,

	/// The state of the keyboard modifiers at the time of the event.
	pub modifiers: ModifiersState,
}

/// A window received mouse input.
#[derive(Debug, Clone)]
pub struct WindowMouseButtonEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The mouse button that was pressed.
	pub button: MouseButton,

	/// The new state of the mouse button.
	pub state: ElementState,

	/// The current position of the mouse cursor inside the window.
	pub position: glam::Vec2,

	/// The position of the mouse cursor before the last movement.
	pub prev_position: glam::Vec2,

	/// The pressed state of all mouse buttons.
	pub buttons: MouseButtonState,

	/// The state of the keyboard modifiers at the time of the event.
	pub modifiers: ModifiersState,
}

/// A window received mouse wheel input.
#[derive(Debug, Clone)]
pub struct WindowMouseWheelEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The scroll delta of the mouse wheel.
	pub delta: MouseScrollDelta,

	/// The touch-screen input state.
	pub phase: TouchPhase,

	/// The current position of the mouse cursor inside the window.
	pub position: Option<glam::Vec2>,

	/// The pressed state of all mouse buttons.
	pub buttons: MouseButtonState,

	/// The state of the keyboard modifiers at the time of the event.
	pub modifiers: ModifiersState,
}

/// A window received axis motion input.
#[derive(Debug, Clone)]
pub struct WindowAxisMotionEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The axis that as moved.
	pub axis: AxisId,

	/// The value by which the axis moved.
	pub value: f64,
}

/// A window received touchpad pressure input.
#[derive(Debug, Clone)]
pub struct WindowTouchpadPressureEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The device that generated the input.
	pub device_id: DeviceId,

	/// The pressure on the touch pad, in the range 0 to 1.
	pub pressure: f32,

	/// The click level of the touch pad.
	pub stage: i64,
}

/// A window received touch input.
#[derive(Debug, Clone)]
pub struct WindowTouchEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The touch input.
	pub touch: Touch,
}

/// The scale factor between logical and physical pixels for a window changed.
#[derive(Debug, Clone)]
pub struct WindowScaleFactorChangedEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The new scale factor as physical pixels per logical pixel.
	pub scale_factor: f64,
}

/// The theme for a window changed.
#[derive(Debug, Clone)]
pub struct WindowThemeChangedEvent {
	/// The ID of the window.
	pub window_id: WindowId,

	/// The new theme of the window.
	pub theme: Theme,
}

impl_from_variant!(WindowEvent::RedrawRequested(WindowRedrawRequestedEvent));
impl_from_variant!(WindowEvent::Resized(WindowResizedEvent));
impl_from_variant!(WindowEvent::Moved(WindowMovedEvent));
impl_from_variant!(WindowEvent::CloseRequested(WindowCloseRequestedEvent));
impl_from_variant!(WindowEvent::Destroyed(WindowDestroyedEvent));
impl_from_variant!(WindowEvent::DroppedFile(WindowDroppedFileEvent));
impl_from_variant!(WindowEvent::HoveredFile(WindowHoveredFileEvent));
impl_from_variant!(WindowEvent::HoveredFileCancelled(WindowHoveredFileCancelledEvent));
impl_from_variant!(WindowEvent::FocusGained(WindowFocusGainedEvent));
impl_from_variant!(WindowEvent::FocusLost(WindowFocusLostEvent));
impl_from_variant!(WindowEvent::KeyboardInput(WindowKeyboardInputEvent));
impl_from_variant!(WindowEvent::TextInput(WindowTextInputEvent));
impl_from_variant!(WindowEvent::MouseEnter(WindowMouseEnterEvent));
impl_from_variant!(WindowEvent::MouseLeave(WindowMouseLeaveEvent));
impl_from_variant!(WindowEvent::MouseMove(WindowMouseMoveEvent));
impl_from_variant!(WindowEvent::MouseButton(WindowMouseButtonEvent));
impl_from_variant!(WindowEvent::MouseWheel(WindowMouseWheelEvent));
impl_from_variant!(WindowEvent::AxisMotion(WindowAxisMotionEvent));
impl_from_variant!(WindowEvent::TouchpadPressure(WindowTouchpadPressureEvent));
impl_from_variant!(WindowEvent::Touch(WindowTouchEvent));
impl_from_variant!(WindowEvent::ScaleFactorChanged(WindowScaleFactorChangedEvent));
impl_from_variant!(WindowEvent::ThemeChanged(WindowThemeChangedEvent));