Skip to content

Commit

Permalink
add new event WindowOccluded from winit (#10735)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
extrawurst authored Nov 26, 2023
1 parent 73bb310 commit 9849221
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
25 changes: 24 additions & 1 deletion crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct WindowCreated {
/// 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
/// by closing the corresponding [`Window`].
/// by closing the corresponding [`Window`].
/// To disable this behavior, set `close_when_requested` on the [`WindowPlugin`]
/// to `false`.
///
Expand Down Expand Up @@ -236,6 +236,29 @@ pub struct WindowFocused {
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.
#[derive(Event, Debug, Clone, PartialEq, Reflect)]
#[reflect(Debug, PartialEq)]
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl Plugin for WindowPlugin {
.add_event::<ReceivedCharacter>()
.add_event::<Ime>()
.add_event::<WindowFocused>()
.add_event::<WindowOccluded>()
.add_event::<WindowScaleFactorChanged>()
.add_event::<WindowBackendScaleFactorChanged>()
.add_event::<FileDragAndDrop>()
Expand Down Expand Up @@ -137,6 +138,7 @@ impl Plugin for WindowPlugin {
.register_type::<CursorLeft>()
.register_type::<ReceivedCharacter>()
.register_type::<WindowFocused>()
.register_type::<WindowOccluded>()
.register_type::<WindowScaleFactorChanged>()
.register_type::<WindowBackendScaleFactorChanged>()
.register_type::<FileDragAndDrop>()
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ use bevy_window::{
exit_on_all_closed, ApplicationLifetime, CursorEntered, CursorLeft, CursorMoved,
FileDragAndDrop, Ime, ReceivedCharacter, RequestRedraw, Window,
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowDestroyed,
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, WindowThemeChanged,
WindowFocused, WindowMoved, WindowOccluded, WindowResized, WindowScaleFactorChanged,
WindowThemeChanged,
};
#[cfg(target_os = "android")]
use bevy_window::{PrimaryWindow, RawHandleWrapper};
Expand Down Expand Up @@ -275,6 +276,7 @@ struct WindowAndInputEventWriters<'w> {
window_scale_factor_changed: EventWriter<'w, WindowScaleFactorChanged>,
window_backend_scale_factor_changed: EventWriter<'w, WindowBackendScaleFactorChanged>,
window_focused: EventWriter<'w, WindowFocused>,
window_occluded: EventWriter<'w, WindowOccluded>,
window_moved: EventWriter<'w, WindowMoved>,
window_theme_changed: EventWriter<'w, WindowThemeChanged>,
window_destroyed: EventWriter<'w, WindowDestroyed>,
Expand Down Expand Up @@ -658,6 +660,12 @@ pub fn winit_runner(mut app: App) {
focused,
});
}
WindowEvent::Occluded(occluded) => {
event_writers.window_occluded.send(WindowOccluded {
window: window_entity,
occluded,
});
}
WindowEvent::DroppedFile(path_buf) => {
event_writers
.file_drag_and_drop
Expand Down

0 comments on commit 9849221

Please sign in to comment.