Skip to content

Commit 9849221

Browse files
authored
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
1 parent 73bb310 commit 9849221

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

crates/bevy_window/src/event.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct WindowCreated {
5757
/// be closed. This will be sent when the close button of the window is pressed.
5858
///
5959
/// If the default [`WindowPlugin`] is used, these events are handled
60-
/// by closing the corresponding [`Window`].
60+
/// by closing the corresponding [`Window`].
6161
/// To disable this behavior, set `close_when_requested` on the [`WindowPlugin`]
6262
/// to `false`.
6363
///
@@ -236,6 +236,29 @@ pub struct WindowFocused {
236236
pub focused: bool,
237237
}
238238

239+
/// The window has been occluded (completely hidden from view).
240+
///
241+
/// This is different to window visibility as it depends on
242+
/// whether the window is closed, minimised, set invisible,
243+
/// or fully occluded by another window.
244+
///
245+
/// It is the translated version of [`WindowEvent::Occluded`] from the `winit` crate.
246+
///
247+
/// [`WindowEvent::Occluded`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.Occluded
248+
#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
249+
#[reflect(Debug, PartialEq)]
250+
#[cfg_attr(
251+
feature = "serialize",
252+
derive(serde::Serialize, serde::Deserialize),
253+
reflect(Serialize, Deserialize)
254+
)]
255+
pub struct WindowOccluded {
256+
/// Window that changed occluded state.
257+
pub window: Entity,
258+
/// Whether it was occluded (true) or not occluded (false).
259+
pub occluded: bool,
260+
}
261+
239262
/// An event that indicates a window's scale factor has changed.
240263
#[derive(Event, Debug, Clone, PartialEq, Reflect)]
241264
#[reflect(Debug, PartialEq)]

crates/bevy_window/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl Plugin for WindowPlugin {
9393
.add_event::<ReceivedCharacter>()
9494
.add_event::<Ime>()
9595
.add_event::<WindowFocused>()
96+
.add_event::<WindowOccluded>()
9697
.add_event::<WindowScaleFactorChanged>()
9798
.add_event::<WindowBackendScaleFactorChanged>()
9899
.add_event::<FileDragAndDrop>()
@@ -137,6 +138,7 @@ impl Plugin for WindowPlugin {
137138
.register_type::<CursorLeft>()
138139
.register_type::<ReceivedCharacter>()
139140
.register_type::<WindowFocused>()
141+
.register_type::<WindowOccluded>()
140142
.register_type::<WindowScaleFactorChanged>()
141143
.register_type::<WindowBackendScaleFactorChanged>()
142144
.register_type::<FileDragAndDrop>()

crates/bevy_winit/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ use bevy_window::{
4040
exit_on_all_closed, ApplicationLifetime, CursorEntered, CursorLeft, CursorMoved,
4141
FileDragAndDrop, Ime, ReceivedCharacter, RequestRedraw, Window,
4242
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowDestroyed,
43-
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, WindowThemeChanged,
43+
WindowFocused, WindowMoved, WindowOccluded, WindowResized, WindowScaleFactorChanged,
44+
WindowThemeChanged,
4445
};
4546
#[cfg(target_os = "android")]
4647
use bevy_window::{PrimaryWindow, RawHandleWrapper};
@@ -275,6 +276,7 @@ struct WindowAndInputEventWriters<'w> {
275276
window_scale_factor_changed: EventWriter<'w, WindowScaleFactorChanged>,
276277
window_backend_scale_factor_changed: EventWriter<'w, WindowBackendScaleFactorChanged>,
277278
window_focused: EventWriter<'w, WindowFocused>,
279+
window_occluded: EventWriter<'w, WindowOccluded>,
278280
window_moved: EventWriter<'w, WindowMoved>,
279281
window_theme_changed: EventWriter<'w, WindowThemeChanged>,
280282
window_destroyed: EventWriter<'w, WindowDestroyed>,
@@ -658,6 +660,12 @@ pub fn winit_runner(mut app: App) {
658660
focused,
659661
});
660662
}
663+
WindowEvent::Occluded(occluded) => {
664+
event_writers.window_occluded.send(WindowOccluded {
665+
window: window_entity,
666+
occluded,
667+
});
668+
}
661669
WindowEvent::DroppedFile(path_buf) => {
662670
event_writers
663671
.file_drag_and_drop

0 commit comments

Comments
 (0)