1use std::path::PathBuf;
2
3use bevy_ecs::{entity::Entity, event::Event};
4use bevy_input::{
5 gestures::*,
6 keyboard::{KeyboardFocusLost, KeyboardInput},
7 mouse::{MouseButtonInput, MouseMotion, MouseWheel},
8 touch::TouchInput,
9};
10use bevy_math::{IVec2, Vec2};
11use bevy_reflect::Reflect;
12
13#[cfg(feature = "serialize")]
14use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
15
16use crate::WindowTheme;
17
18#[derive(Event, Debug, Clone, PartialEq, Reflect)]
20#[reflect(Debug, PartialEq)]
21#[cfg_attr(
22 feature = "serialize",
23 derive(serde::Serialize, serde::Deserialize),
24 reflect(Serialize, Deserialize)
25)]
26pub struct WindowResized {
27 pub window: Entity,
29 pub width: f32,
31 pub height: f32,
33}
34
35#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
38#[reflect(Debug, PartialEq)]
39#[cfg_attr(
40 feature = "serialize",
41 derive(serde::Serialize, serde::Deserialize),
42 reflect(Serialize, Deserialize)
43)]
44pub struct RequestRedraw;
45
46#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
50#[reflect(Debug, PartialEq)]
51#[cfg_attr(
52 feature = "serialize",
53 derive(serde::Serialize, serde::Deserialize),
54 reflect(Serialize, Deserialize)
55)]
56pub struct WindowCreated {
57 pub window: Entity,
59}
60
61#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
72#[reflect(Debug, PartialEq)]
73#[cfg_attr(
74 feature = "serialize",
75 derive(serde::Serialize, serde::Deserialize),
76 reflect(Serialize, Deserialize)
77)]
78pub struct WindowCloseRequested {
79 pub window: Entity,
81}
82
83#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
86#[reflect(Debug, PartialEq)]
87#[cfg_attr(
88 feature = "serialize",
89 derive(serde::Serialize, serde::Deserialize),
90 reflect(Serialize, Deserialize)
91)]
92pub struct WindowClosed {
93 pub window: Entity,
98}
99
100#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
103#[reflect(Debug, PartialEq)]
104#[cfg_attr(
105 feature = "serialize",
106 derive(serde::Serialize, serde::Deserialize),
107 reflect(Serialize, Deserialize)
108)]
109pub struct WindowClosing {
110 pub window: Entity,
112}
113
114#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
119#[reflect(Debug, PartialEq)]
120#[cfg_attr(
121 feature = "serialize",
122 derive(serde::Serialize, serde::Deserialize),
123 reflect(Serialize, Deserialize)
124)]
125pub struct WindowDestroyed {
126 pub window: Entity,
131}
132
133#[derive(Event, Debug, Clone, PartialEq, Reflect)]
145#[reflect(Debug, PartialEq)]
146#[cfg_attr(
147 feature = "serialize",
148 derive(serde::Serialize, serde::Deserialize),
149 reflect(Serialize, Deserialize)
150)]
151pub struct CursorMoved {
152 pub window: Entity,
154 pub position: Vec2,
156 pub delta: Option<Vec2>,
162}
163
164#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
166#[reflect(Debug, PartialEq)]
167#[cfg_attr(
168 feature = "serialize",
169 derive(serde::Serialize, serde::Deserialize),
170 reflect(Serialize, Deserialize)
171)]
172pub struct CursorEntered {
173 pub window: Entity,
175}
176
177#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
179#[reflect(Debug, PartialEq)]
180#[cfg_attr(
181 feature = "serialize",
182 derive(serde::Serialize, serde::Deserialize),
183 reflect(Serialize, Deserialize)
184)]
185pub struct CursorLeft {
186 pub window: Entity,
188}
189
190#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
196#[reflect(Debug, PartialEq)]
197#[cfg_attr(
198 feature = "serialize",
199 derive(serde::Serialize, serde::Deserialize),
200 reflect(Serialize, Deserialize)
201)]
202pub enum Ime {
203 Preedit {
205 window: Entity,
207 value: String,
209 cursor: Option<(usize, usize)>,
213 },
214 Commit {
216 window: Entity,
218 value: String,
220 },
221 Enabled {
225 window: Entity,
227 },
228 Disabled {
230 window: Entity,
232 },
233}
234
235#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
237#[reflect(Debug, PartialEq)]
238#[cfg_attr(
239 feature = "serialize",
240 derive(serde::Serialize, serde::Deserialize),
241 reflect(Serialize, Deserialize)
242)]
243pub struct WindowFocused {
244 pub window: Entity,
246 pub focused: bool,
248}
249
250#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
260#[reflect(Debug, PartialEq)]
261#[cfg_attr(
262 feature = "serialize",
263 derive(serde::Serialize, serde::Deserialize),
264 reflect(Serialize, Deserialize)
265)]
266pub struct WindowOccluded {
267 pub window: Entity,
269 pub occluded: bool,
271}
272
273#[derive(Event, Debug, Clone, PartialEq, Reflect)]
275#[reflect(Debug, PartialEq)]
276#[cfg_attr(
277 feature = "serialize",
278 derive(serde::Serialize, serde::Deserialize),
279 reflect(Serialize, Deserialize)
280)]
281pub struct WindowScaleFactorChanged {
282 pub window: Entity,
284 pub scale_factor: f64,
286}
287
288#[derive(Event, Debug, Clone, PartialEq, Reflect)]
290#[reflect(Debug, PartialEq)]
291#[cfg_attr(
292 feature = "serialize",
293 derive(serde::Serialize, serde::Deserialize),
294 reflect(Serialize, Deserialize)
295)]
296pub struct WindowBackendScaleFactorChanged {
297 pub window: Entity,
299 pub scale_factor: f64,
301}
302
303#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
305#[reflect(Debug, PartialEq)]
306#[cfg_attr(
307 feature = "serialize",
308 derive(serde::Serialize, serde::Deserialize),
309 reflect(Serialize, Deserialize)
310)]
311pub enum FileDragAndDrop {
312 DroppedFile {
314 window: Entity,
316 path_buf: PathBuf,
318 },
319
320 HoveredFile {
322 window: Entity,
324 path_buf: PathBuf,
326 },
327
328 HoveredFileCanceled {
330 window: Entity,
332 },
333}
334
335#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
337#[reflect(Debug, PartialEq)]
338#[cfg_attr(
339 feature = "serialize",
340 derive(serde::Serialize, serde::Deserialize),
341 reflect(Serialize, Deserialize)
342)]
343pub struct WindowMoved {
344 pub window: Entity,
346 pub position: IVec2,
348}
349
350#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
355#[reflect(Debug, PartialEq)]
356#[cfg_attr(
357 feature = "serialize",
358 derive(serde::Serialize, serde::Deserialize),
359 reflect(Serialize, Deserialize)
360)]
361pub struct WindowThemeChanged {
362 pub window: Entity,
364 pub theme: WindowTheme,
366}
367
368#[derive(Event, Debug, Clone, Copy, PartialEq, Eq, Reflect)]
370#[reflect(Debug, PartialEq)]
371#[cfg_attr(
372 feature = "serialize",
373 derive(serde::Serialize, serde::Deserialize),
374 reflect(Serialize, Deserialize)
375)]
376pub enum AppLifecycle {
377 Idle,
379 Running,
381 WillSuspend,
384 Suspended,
386 WillResume,
389}
390
391impl AppLifecycle {
392 #[inline]
394 pub fn is_active(&self) -> bool {
395 match self {
396 Self::Idle | Self::Suspended => false,
397 Self::Running | Self::WillSuspend | Self::WillResume => true,
398 }
399 }
400}
401
402#[derive(Event, Debug, Clone, PartialEq, Reflect)]
409#[reflect(Debug, PartialEq)]
410#[cfg_attr(
411 feature = "serialize",
412 derive(serde::Serialize, serde::Deserialize),
413 reflect(Serialize, Deserialize)
414)]
415#[allow(missing_docs, reason = "Not all docs are written yet (#3492).")]
417pub enum WindowEvent {
418 AppLifecycle(AppLifecycle),
419 CursorEntered(CursorEntered),
420 CursorLeft(CursorLeft),
421 CursorMoved(CursorMoved),
422 FileDragAndDrop(FileDragAndDrop),
423 Ime(Ime),
424 RequestRedraw(RequestRedraw),
425 WindowBackendScaleFactorChanged(WindowBackendScaleFactorChanged),
426 WindowCloseRequested(WindowCloseRequested),
427 WindowCreated(WindowCreated),
428 WindowDestroyed(WindowDestroyed),
429 WindowFocused(WindowFocused),
430 WindowMoved(WindowMoved),
431 WindowOccluded(WindowOccluded),
432 WindowResized(WindowResized),
433 WindowScaleFactorChanged(WindowScaleFactorChanged),
434 WindowThemeChanged(WindowThemeChanged),
435
436 MouseButtonInput(MouseButtonInput),
437 MouseMotion(MouseMotion),
438 MouseWheel(MouseWheel),
439
440 PinchGesture(PinchGesture),
441 RotationGesture(RotationGesture),
442 DoubleTapGesture(DoubleTapGesture),
443 PanGesture(PanGesture),
444
445 TouchInput(TouchInput),
446
447 KeyboardInput(KeyboardInput),
448 KeyboardFocusLost(KeyboardFocusLost),
449}
450
451impl From<AppLifecycle> for WindowEvent {
452 fn from(e: AppLifecycle) -> Self {
453 Self::AppLifecycle(e)
454 }
455}
456impl From<CursorEntered> for WindowEvent {
457 fn from(e: CursorEntered) -> Self {
458 Self::CursorEntered(e)
459 }
460}
461impl From<CursorLeft> for WindowEvent {
462 fn from(e: CursorLeft) -> Self {
463 Self::CursorLeft(e)
464 }
465}
466impl From<CursorMoved> for WindowEvent {
467 fn from(e: CursorMoved) -> Self {
468 Self::CursorMoved(e)
469 }
470}
471impl From<FileDragAndDrop> for WindowEvent {
472 fn from(e: FileDragAndDrop) -> Self {
473 Self::FileDragAndDrop(e)
474 }
475}
476impl From<Ime> for WindowEvent {
477 fn from(e: Ime) -> Self {
478 Self::Ime(e)
479 }
480}
481impl From<RequestRedraw> for WindowEvent {
482 fn from(e: RequestRedraw) -> Self {
483 Self::RequestRedraw(e)
484 }
485}
486impl From<WindowBackendScaleFactorChanged> for WindowEvent {
487 fn from(e: WindowBackendScaleFactorChanged) -> Self {
488 Self::WindowBackendScaleFactorChanged(e)
489 }
490}
491impl From<WindowCloseRequested> for WindowEvent {
492 fn from(e: WindowCloseRequested) -> Self {
493 Self::WindowCloseRequested(e)
494 }
495}
496impl From<WindowCreated> for WindowEvent {
497 fn from(e: WindowCreated) -> Self {
498 Self::WindowCreated(e)
499 }
500}
501impl From<WindowDestroyed> for WindowEvent {
502 fn from(e: WindowDestroyed) -> Self {
503 Self::WindowDestroyed(e)
504 }
505}
506impl From<WindowFocused> for WindowEvent {
507 fn from(e: WindowFocused) -> Self {
508 Self::WindowFocused(e)
509 }
510}
511impl From<WindowMoved> for WindowEvent {
512 fn from(e: WindowMoved) -> Self {
513 Self::WindowMoved(e)
514 }
515}
516impl From<WindowOccluded> for WindowEvent {
517 fn from(e: WindowOccluded) -> Self {
518 Self::WindowOccluded(e)
519 }
520}
521impl From<WindowResized> for WindowEvent {
522 fn from(e: WindowResized) -> Self {
523 Self::WindowResized(e)
524 }
525}
526impl From<WindowScaleFactorChanged> for WindowEvent {
527 fn from(e: WindowScaleFactorChanged) -> Self {
528 Self::WindowScaleFactorChanged(e)
529 }
530}
531impl From<WindowThemeChanged> for WindowEvent {
532 fn from(e: WindowThemeChanged) -> Self {
533 Self::WindowThemeChanged(e)
534 }
535}
536impl From<MouseButtonInput> for WindowEvent {
537 fn from(e: MouseButtonInput) -> Self {
538 Self::MouseButtonInput(e)
539 }
540}
541impl From<MouseMotion> for WindowEvent {
542 fn from(e: MouseMotion) -> Self {
543 Self::MouseMotion(e)
544 }
545}
546impl From<MouseWheel> for WindowEvent {
547 fn from(e: MouseWheel) -> Self {
548 Self::MouseWheel(e)
549 }
550}
551impl From<PinchGesture> for WindowEvent {
552 fn from(e: PinchGesture) -> Self {
553 Self::PinchGesture(e)
554 }
555}
556impl From<RotationGesture> for WindowEvent {
557 fn from(e: RotationGesture) -> Self {
558 Self::RotationGesture(e)
559 }
560}
561impl From<DoubleTapGesture> for WindowEvent {
562 fn from(e: DoubleTapGesture) -> Self {
563 Self::DoubleTapGesture(e)
564 }
565}
566impl From<PanGesture> for WindowEvent {
567 fn from(e: PanGesture) -> Self {
568 Self::PanGesture(e)
569 }
570}
571impl From<TouchInput> for WindowEvent {
572 fn from(e: TouchInput) -> Self {
573 Self::TouchInput(e)
574 }
575}
576impl From<KeyboardInput> for WindowEvent {
577 fn from(e: KeyboardInput) -> Self {
578 Self::KeyboardInput(e)
579 }
580}
581impl From<KeyboardFocusLost> for WindowEvent {
582 fn from(e: KeyboardFocusLost) -> Self {
583 Self::KeyboardFocusLost(e)
584 }
585}