Skip to content

Commit 44b9719

Browse files
pietrosophyamockersf
authored andcommitted
fix: upgrade to winit v0.30 (bevyengine#13366)
- Upgrade winit to v0.30 - Fixes bevyengine#13331 This is a rewrite/adaptation of the new trait system described and implemented in `winit` v0.30. The custom UserEvent is now renamed as WakeUp, used to wake up the loop if anything happens outside the app (a new [custom_user_event](https://github.com/bevyengine/bevy/pull/13366/files#diff-2de8c0a8d3028d0059a3d80ae31b2bbc1cde2595ce2d317ea378fe3e0cf6ef2d) shows this behavior. The internal `UpdateState` has been removed and replaced internally by the AppLifecycle. When changed, the AppLifecycle is sent as an event. The `UpdateMode` now accepts only two values: `Continuous` and `Reactive`, but the latter exposes 3 new properties to enable reactive to device, user or window events. The previous `UpdateMode::Reactive` is now equivalent to `UpdateMode::reactive()`, while `UpdateMode::ReactiveLowPower` to `UpdateMode::reactive_low_power()`. The `ApplicationLifecycle` has been renamed as `AppLifecycle`, and now contains the possible values of the application state inside the event loop: * `Idle`: the loop has not started yet * `Running` (previously called `Started`): the loop is running * `WillSuspend`: the loop is going to be suspended * `Suspended`: the loop is suspended * `WillResume`: the loop is going to be resumed Note: the `Resumed` state has been removed since the resumed app is just running. Finally, now that `winit` enables this, it extends the `WinitPlugin` to support custom events. - [x] Windows - [x] MacOs - [x] Linux (x11) - [x] Linux (Wayland) - [x] Android - [x] iOS - [x] WASM/WebGPU - [x] WASM/WebGL2 - [ ] iOS: build failed in CI - blocking, but may just be flakiness - [x] Cross-platform: when the window is maximised, changes in the scale factor don't apply, to make them apply one has to make the window smaller again. (Re-maximising keeps the updated scale factor) - non-blocking, but good to fix - [ ] Android: it's pretty easy to quickly open and close the app and then the music keeps playing when suspended. - non-blocking but worrying - [ ] Web: the application will hang when switching tabs - Not new, duplicate of bevyengine#13486 - [ ] Cross-platform?: Screenshot failure, `ERROR present_frames: wgpu_core::present: No work has been submitted for this frame before` taking the first screenshot, but after pressing space - non-blocking, but good to fix --------- Co-authored-by: François <[email protected]>
1 parent f677f40 commit 44b9719

File tree

4 files changed

+307
-267
lines changed

4 files changed

+307
-267
lines changed

crates/bevy_window/src/window.rs

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::num::NonZeroU32;
22

33
use bevy_ecs::{
4-
entity::{Entity, EntityMapper, MapEntities},
5-
prelude::{Component, ReflectComponent},
4+
entity::{ Entity, EntityMapper, MapEntities },
5+
prelude::{ Component, ReflectComponent },
66
};
7-
use bevy_math::{DVec2, IVec2, UVec2, Vec2};
8-
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
7+
use bevy_math::{ DVec2, IVec2, UVec2, Vec2 };
8+
use bevy_reflect::{ std_traits::ReflectDefault, Reflect };
99

1010
#[cfg(feature = "serialize")]
11-
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
11+
use bevy_reflect::{ ReflectDeserialize, ReflectSerialize };
1212

1313
use bevy_utils::tracing::warn;
1414

@@ -67,7 +67,7 @@ impl MapEntities for WindowRef {
6767
*entity = entity_mapper.map_entity(*entity);
6868
}
6969
Self::Primary => {}
70-
};
70+
}
7171
}
7272
}
7373

@@ -424,8 +424,9 @@ impl Window {
424424
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
425425
#[inline]
426426
pub fn cursor_position(&self) -> Option<Vec2> {
427-
self.physical_cursor_position()
428-
.map(|position| (position.as_dvec2() / self.scale_factor() as f64).as_vec2())
427+
self.physical_cursor_position().map(|position|
428+
(position.as_dvec2() / (self.scale_factor() as f64)).as_vec2()
429+
)
429430
}
430431

431432
/// The cursor position in this window in physical pixels.
@@ -437,10 +438,11 @@ impl Window {
437438
pub fn physical_cursor_position(&self) -> Option<Vec2> {
438439
match self.internal.physical_cursor_position {
439440
Some(position) => {
440-
if position.x >= 0.
441-
&& position.y >= 0.
442-
&& position.x < self.physical_width() as f64
443-
&& position.y < self.physical_height() as f64
441+
if
442+
position.x >= 0.0 &&
443+
position.y >= 0.0 &&
444+
position.x < (self.physical_width() as f64) &&
445+
position.y < (self.physical_height() as f64)
444446
{
445447
Some(position.as_vec2())
446448
} else {
@@ -455,8 +457,9 @@ impl Window {
455457
///
456458
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
457459
pub fn set_cursor_position(&mut self, position: Option<Vec2>) {
458-
self.internal.physical_cursor_position =
459-
position.map(|p| p.as_dvec2() * self.scale_factor() as f64);
460+
self.internal.physical_cursor_position = position.map(
461+
|p| p.as_dvec2() * (self.scale_factor() as f64)
462+
);
460463
}
461464

462465
/// Set the cursor position in this window in physical pixels.
@@ -496,8 +499,8 @@ pub struct WindowResizeConstraints {
496499
impl Default for WindowResizeConstraints {
497500
fn default() -> Self {
498501
Self {
499-
min_width: 180.,
500-
min_height: 120.,
502+
min_width: 180.0,
503+
min_height: 120.0,
501504
max_width: f32::INFINITY,
502505
max_height: f32::INFINITY,
503506
}
@@ -516,19 +519,21 @@ impl WindowResizeConstraints {
516519
mut max_width,
517520
mut max_height,
518521
} = self;
519-
min_width = min_width.max(1.);
520-
min_height = min_height.max(1.);
522+
min_width = min_width.max(1.0);
523+
min_height = min_height.max(1.0);
521524
if max_width < min_width {
522525
warn!(
523526
"The given maximum width {} is smaller than the minimum width {}",
524-
max_width, min_width
527+
max_width,
528+
min_width
525529
);
526530
max_width = min_width;
527531
}
528532
if max_height < min_height {
529533
warn!(
530534
"The given maximum height {} is smaller than the minimum height {}",
531-
max_height, min_height
535+
max_height,
536+
min_height
532537
);
533538
max_height = min_height;
534539
}
@@ -735,13 +740,13 @@ impl WindowResolution {
735740
/// The window's client area width in logical pixels.
736741
#[inline]
737742
pub fn width(&self) -> f32 {
738-
self.physical_width() as f32 / self.scale_factor()
743+
(self.physical_width() as f32) / self.scale_factor()
739744
}
740745

741746
/// The window's client area height in logical pixels.
742747
#[inline]
743748
pub fn height(&self) -> f32 {
744-
self.physical_height() as f32 / self.scale_factor()
749+
(self.physical_height() as f32) / self.scale_factor()
745750
}
746751

747752
/// The window's client size in logical pixels
@@ -772,8 +777,7 @@ impl WindowResolution {
772777
///
773778
/// `physical_pixels = logical_pixels * scale_factor`
774779
pub fn scale_factor(&self) -> f32 {
775-
self.scale_factor_override
776-
.unwrap_or_else(|| self.base_scale_factor())
780+
self.scale_factor_override.unwrap_or_else(|| self.base_scale_factor())
777781
}
778782

779783
/// The window scale factor as reported by the window backend.
@@ -797,7 +801,7 @@ impl WindowResolution {
797801
pub fn set(&mut self, width: f32, height: f32) {
798802
self.set_physical_resolution(
799803
(width * self.scale_factor()) as u32,
800-
(height * self.scale_factor()) as u32,
804+
(height * self.scale_factor()) as u32
801805
);
802806
}
803807

@@ -825,8 +829,8 @@ impl WindowResolution {
825829
#[doc(hidden)]
826830
pub fn set_scale_factor_and_apply_to_physical_size(&mut self, scale_factor: f32) {
827831
self.scale_factor = scale_factor;
828-
self.physical_width = (self.physical_width as f32 * scale_factor) as u32;
829-
self.physical_height = (self.physical_height as f32 * scale_factor) as u32;
832+
self.physical_width = ((self.physical_width as f32) * scale_factor) as u32;
833+
self.physical_height = ((self.physical_height as f32) * scale_factor) as u32;
830834
}
831835

832836
/// Set the window's scale factor, this will be used over what the backend decides.
@@ -839,19 +843,13 @@ impl WindowResolution {
839843
}
840844
}
841845

842-
impl<I> From<(I, I)> for WindowResolution
843-
where
844-
I: Into<f32>,
845-
{
846+
impl<I> From<(I, I)> for WindowResolution where I: Into<f32> {
846847
fn from((width, height): (I, I)) -> WindowResolution {
847848
WindowResolution::new(width.into(), height.into())
848849
}
849850
}
850851

851-
impl<I> From<[I; 2]> for WindowResolution
852-
where
853-
I: Into<f32>,
854-
{
852+
impl<I> From<[I; 2]> for WindowResolution where I: Into<f32> {
855853
fn from([width, height]: [I; 2]) -> WindowResolution {
856854
WindowResolution::new(width.into(), height.into())
857855
}
@@ -1224,48 +1222,42 @@ mod tests {
12241222
#[test]
12251223
fn cursor_position_within_window_bounds() {
12261224
let mut window = Window {
1227-
resolution: WindowResolution::new(800., 600.),
1225+
resolution: WindowResolution::new(800.0, 600.0),
12281226
..Default::default()
12291227
};
12301228

1231-
window.set_physical_cursor_position(Some(DVec2::new(0., 300.)));
1232-
assert_eq!(window.physical_cursor_position(), Some(Vec2::new(0., 300.)));
1229+
window.set_physical_cursor_position(Some(DVec2::new(0.0, 300.0)));
1230+
assert_eq!(window.physical_cursor_position(), Some(Vec2::new(0.0, 300.0)));
12331231

1234-
window.set_physical_cursor_position(Some(DVec2::new(400., 0.)));
1235-
assert_eq!(window.physical_cursor_position(), Some(Vec2::new(400., 0.)));
1232+
window.set_physical_cursor_position(Some(DVec2::new(400.0, 0.0)));
1233+
assert_eq!(window.physical_cursor_position(), Some(Vec2::new(400.0, 0.0)));
12361234

1237-
window.set_physical_cursor_position(Some(DVec2::new(799.999, 300.)));
1238-
assert_eq!(
1239-
window.physical_cursor_position(),
1240-
Some(Vec2::new(799.999, 300.)),
1241-
);
1235+
window.set_physical_cursor_position(Some(DVec2::new(799.999, 300.0)));
1236+
assert_eq!(window.physical_cursor_position(), Some(Vec2::new(799.999, 300.0)));
12421237

1243-
window.set_physical_cursor_position(Some(DVec2::new(400., 599.999)));
1244-
assert_eq!(
1245-
window.physical_cursor_position(),
1246-
Some(Vec2::new(400., 599.999))
1247-
);
1238+
window.set_physical_cursor_position(Some(DVec2::new(400.0, 599.999)));
1239+
assert_eq!(window.physical_cursor_position(), Some(Vec2::new(400.0, 599.999)));
12481240
}
12491241

12501242
// Checks that `Window::physical_cursor_position` returns `None` if the cursor position is not
12511243
// within the bounds of the window.
12521244
#[test]
12531245
fn cursor_position_not_within_window_bounds() {
12541246
let mut window = Window {
1255-
resolution: WindowResolution::new(800., 600.),
1247+
resolution: WindowResolution::new(800.0, 600.0),
12561248
..Default::default()
12571249
};
12581250

1259-
window.set_physical_cursor_position(Some(DVec2::new(-0.001, 300.)));
1251+
window.set_physical_cursor_position(Some(DVec2::new(-0.001, 300.0)));
12601252
assert!(window.physical_cursor_position().is_none());
12611253

1262-
window.set_physical_cursor_position(Some(DVec2::new(400., -0.001)));
1254+
window.set_physical_cursor_position(Some(DVec2::new(400.0, -0.001)));
12631255
assert!(window.physical_cursor_position().is_none());
12641256

1265-
window.set_physical_cursor_position(Some(DVec2::new(800., 300.)));
1257+
window.set_physical_cursor_position(Some(DVec2::new(800.0, 300.0)));
12661258
assert!(window.physical_cursor_position().is_none());
12671259

1268-
window.set_physical_cursor_position(Some(DVec2::new(400., 600.)));
1260+
window.set_physical_cursor_position(Some(DVec2::new(400.0, 600.0)));
12691261
assert!(window.physical_cursor_position().is_none());
12701262
}
12711263
}

0 commit comments

Comments
 (0)