add new event WindowOccluded from winit (#10735)

forward for bevy user to consume

# Objective

- since winit 0.27 an event WindowOccluded will be produced:
https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.Occluded
- on ios this is currently the only way to know if an app comes back
from the background which is an important time to to things (like
resetting the badge)

## Solution

- pick up the winit event and forward it to a new `EventWriter`

---

## Changelog

### Added
- new Event `WindowOccluded` added allowing to hook into
`WindowEvent::Occluded` of winit
This commit is contained in:
extrawurst 2023-11-26 22:58:54 +01:00 committed by GitHub
parent 73bb310304
commit 9849221522
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 2 deletions

View file

@ -57,7 +57,7 @@ pub struct WindowCreated {
/// be closed. This will be sent when the close button of the window is pressed. /// be closed. This will be sent when the close button of the window is pressed.
/// ///
/// If the default [`WindowPlugin`] is used, these events are handled /// If the default [`WindowPlugin`] is used, these events are handled
/// by closing the corresponding [`Window`]. /// by closing the corresponding [`Window`].
/// To disable this behavior, set `close_when_requested` on the [`WindowPlugin`] /// To disable this behavior, set `close_when_requested` on the [`WindowPlugin`]
/// to `false`. /// to `false`.
/// ///
@ -236,6 +236,29 @@ pub struct WindowFocused {
pub focused: bool, pub focused: bool,
} }
/// The window has been occluded (completely hidden from view).
///
/// This is different to window visibility as it depends on
/// whether the window is closed, minimised, set invisible,
/// or fully occluded by another window.
///
/// It is the translated version of [`WindowEvent::Occluded`] from the `winit` crate.
///
/// [`WindowEvent::Occluded`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.Occluded
#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct WindowOccluded {
/// Window that changed occluded state.
pub window: Entity,
/// Whether it was occluded (true) or not occluded (false).
pub occluded: bool,
}
/// An event that indicates a window's scale factor has changed. /// An event that indicates a window's scale factor has changed.
#[derive(Event, Debug, Clone, PartialEq, Reflect)] #[derive(Event, Debug, Clone, PartialEq, Reflect)]
#[reflect(Debug, PartialEq)] #[reflect(Debug, PartialEq)]

View file

@ -93,6 +93,7 @@ impl Plugin for WindowPlugin {
.add_event::<ReceivedCharacter>() .add_event::<ReceivedCharacter>()
.add_event::<Ime>() .add_event::<Ime>()
.add_event::<WindowFocused>() .add_event::<WindowFocused>()
.add_event::<WindowOccluded>()
.add_event::<WindowScaleFactorChanged>() .add_event::<WindowScaleFactorChanged>()
.add_event::<WindowBackendScaleFactorChanged>() .add_event::<WindowBackendScaleFactorChanged>()
.add_event::<FileDragAndDrop>() .add_event::<FileDragAndDrop>()
@ -137,6 +138,7 @@ impl Plugin for WindowPlugin {
.register_type::<CursorLeft>() .register_type::<CursorLeft>()
.register_type::<ReceivedCharacter>() .register_type::<ReceivedCharacter>()
.register_type::<WindowFocused>() .register_type::<WindowFocused>()
.register_type::<WindowOccluded>()
.register_type::<WindowScaleFactorChanged>() .register_type::<WindowScaleFactorChanged>()
.register_type::<WindowBackendScaleFactorChanged>() .register_type::<WindowBackendScaleFactorChanged>()
.register_type::<FileDragAndDrop>() .register_type::<FileDragAndDrop>()

View file

@ -40,7 +40,8 @@ use bevy_window::{
exit_on_all_closed, ApplicationLifetime, CursorEntered, CursorLeft, CursorMoved, exit_on_all_closed, ApplicationLifetime, CursorEntered, CursorLeft, CursorMoved,
FileDragAndDrop, Ime, ReceivedCharacter, RequestRedraw, Window, FileDragAndDrop, Ime, ReceivedCharacter, RequestRedraw, Window,
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowDestroyed, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowDestroyed,
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, WindowThemeChanged, WindowFocused, WindowMoved, WindowOccluded, WindowResized, WindowScaleFactorChanged,
WindowThemeChanged,
}; };
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
use bevy_window::{PrimaryWindow, RawHandleWrapper}; use bevy_window::{PrimaryWindow, RawHandleWrapper};
@ -275,6 +276,7 @@ struct WindowAndInputEventWriters<'w> {
window_scale_factor_changed: EventWriter<'w, WindowScaleFactorChanged>, window_scale_factor_changed: EventWriter<'w, WindowScaleFactorChanged>,
window_backend_scale_factor_changed: EventWriter<'w, WindowBackendScaleFactorChanged>, window_backend_scale_factor_changed: EventWriter<'w, WindowBackendScaleFactorChanged>,
window_focused: EventWriter<'w, WindowFocused>, window_focused: EventWriter<'w, WindowFocused>,
window_occluded: EventWriter<'w, WindowOccluded>,
window_moved: EventWriter<'w, WindowMoved>, window_moved: EventWriter<'w, WindowMoved>,
window_theme_changed: EventWriter<'w, WindowThemeChanged>, window_theme_changed: EventWriter<'w, WindowThemeChanged>,
window_destroyed: EventWriter<'w, WindowDestroyed>, window_destroyed: EventWriter<'w, WindowDestroyed>,
@ -658,6 +660,12 @@ pub fn winit_runner(mut app: App) {
focused, focused,
}); });
} }
WindowEvent::Occluded(occluded) => {
event_writers.window_occluded.send(WindowOccluded {
window: window_entity,
occluded,
});
}
WindowEvent::DroppedFile(path_buf) => { WindowEvent::DroppedFile(path_buf) => {
event_writers event_writers
.file_drag_and_drop .file_drag_and_drop