Cleanup bevy winit (#11489)

# Objective

Get #11257 changes merged.

I rewrote them one by one checking each to ensure correctness. In
particular, the window rescale logic changes to accomodate mut app
access are double checked. Not all changes have been included as some of
bevy_winit has since changed, and i am not confident including them.
Namely, the `run_app_update_if_should` change.

### Notes to reviewers

Review commits individually, use the "Hide whitespaces" diff display
mode.

## Changelog

* `bevy:🪟:WindowMoved`'s `entity` field has been renamed to
`window`


## Migration Guide

`bevy:🪟:WindowMoved`'s `entity` field has been renamed to
`window`. This is to be more consistent with other windowing events.

Consider changing usage:

```diff
-window_moved.entity
+window_moved.window
```

---------

Co-authored-by: François <mockersf@gmail.com>
This commit is contained in:
vero 2024-01-28 13:09:23 -08:00 committed by GitHub
parent dad379cdca
commit eb8de36922
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 473 additions and 664 deletions

View file

@ -332,7 +332,7 @@ pub enum FileDragAndDrop {
)]
pub struct WindowMoved {
/// Window that moved.
pub entity: Entity,
pub window: Entity,
/// Where the window moved to in physical pixels.
pub position: IVec2,
}

File diff suppressed because it is too large Load diff

View file

@ -1,17 +1,13 @@
use bevy_a11y::AccessibilityRequested;
use bevy_ecs::{
entity::Entity,
event::EventWriter,
prelude::{Changed, Component, Resource},
prelude::{Changed, Component},
query::QueryFilter,
removal_detection::RemovedComponents,
system::{Commands, NonSendMut, Query, ResMut},
world::Mut,
system::{NonSendMut, Query, SystemParamItem},
};
use bevy_utils::{
tracing::{error, info, warn},
EntityHashMap,
};
use bevy_window::{RawHandleWrapper, Window, WindowClosed, WindowCreated};
use bevy_utils::tracing::{error, info, warn};
use bevy_window::{RawHandleWrapper, Window, WindowClosed, WindowCreated, WindowResized};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use winit::{
@ -20,12 +16,11 @@ use winit::{
};
use crate::{
accessibility::{AccessKitAdapters, WinitActionHandlers},
converters::{
self, convert_enabled_buttons, convert_window_level, convert_window_theme,
convert_winit_theme,
},
get_best_videomode, get_fitting_videomode, WindowAndInputEventWriters, WinitWindows,
get_best_videomode, get_fitting_videomode, CreateWindowParams, WinitWindows,
};
/// Creates new windows on the [`winit`] backend for each entity with a newly-added
@ -34,17 +29,19 @@ use crate::{
/// If any of these entities are missing required components, those will be added with their
/// default values.
#[allow(clippy::too_many_arguments)]
pub(crate) fn create_windows<'a>(
pub(crate) fn create_windows<F: QueryFilter + 'static>(
event_loop: &EventLoopWindowTarget<()>,
mut commands: Commands,
created_windows: impl Iterator<Item = (Entity, Mut<'a, Window>)>,
mut event_writer: EventWriter<WindowCreated>,
mut winit_windows: NonSendMut<WinitWindows>,
mut adapters: NonSendMut<AccessKitAdapters>,
mut handlers: ResMut<WinitActionHandlers>,
accessibility_requested: ResMut<AccessibilityRequested>,
(
mut commands,
mut created_windows,
mut window_created_events,
mut winit_windows,
mut adapters,
mut handlers,
accessibility_requested,
): SystemParamItem<CreateWindowParams<F>>,
) {
for (entity, mut window) in created_windows {
for (entity, mut window) in &mut created_windows {
if winit_windows.get_window(entity).is_some() {
continue;
}
@ -81,14 +78,10 @@ pub(crate) fn create_windows<'a>(
window: window.clone(),
});
event_writer.send(WindowCreated { window: entity });
window_created_events.send(WindowCreated { window: entity });
}
}
/// Cache for closing windows so we can get better debug information.
#[derive(Debug, Clone, Resource)]
pub struct WindowTitleCache(EntityHashMap<Entity, String>);
pub(crate) fn despawn_windows(
mut closed: RemovedComponents<Window>,
window_entities: Query<&Window>,
@ -123,7 +116,7 @@ pub struct CachedWindow {
pub(crate) fn changed_windows(
mut changed_windows: Query<(Entity, &mut Window, &mut CachedWindow), Changed<Window>>,
winit_windows: NonSendMut<WinitWindows>,
mut event_writers: WindowAndInputEventWriters<'_>,
mut window_resized: EventWriter<WindowResized>,
) {
for (entity, mut window, mut cache) in &mut changed_windows {
if let Some(winit_window) = winit_windows.get_window(entity) {
@ -161,7 +154,7 @@ pub(crate) fn changed_windows(
window.resolution.physical_height(),
);
if let Some(size_now) = winit_window.request_inner_size(physical_size) {
crate::react_to_resize(&mut window, size_now, &mut event_writers, entity);
crate::react_to_resize(&mut window, size_now, &mut window_resized, entity);
}
}