Skip to content

Commit 27bfbda

Browse files
committed
Fix scale_factor_override in the winit backend (#2784)
# Objective - Fixes #2751 ## Solution - Avoid changing the window size if there is a scale factor override - Can be tested with the `scale_factor_override` example - use <kbd>⏎</kbd> to active overriding the scale factor
1 parent 4c3c4b5 commit 27bfbda

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

crates/bevy_window/src/event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ use std::path::PathBuf;
33
use super::{WindowDescriptor, WindowId};
44
use bevy_math::{IVec2, Vec2};
55

6-
/// A window event that is sent whenever a window has been resized.
6+
/// A window event that is sent whenever a windows logical size has changed
77
#[derive(Debug, Clone)]
88
pub struct WindowResized {
99
pub id: WindowId,
10+
/// The new logical width of the window
1011
pub width: f32,
12+
/// The new logical height of the window
1113
pub height: f32,
1214
}
1315

crates/bevy_winit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ bevy_utils = { path = "../bevy_utils", version = "0.5.0" }
2323

2424
# other
2525
winit = { version = "0.25.0", default-features = false }
26+
approx = { version = "0.5.0", default-features = false }
2627

2728
[target.'cfg(target_arch = "wasm32")'.dependencies]
2829
winit = { version = "0.25.0", features = ["web-sys"], default-features = false }

crates/bevy_winit/src/lib.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,20 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
395395
id: window_id,
396396
scale_factor,
397397
});
398-
#[allow(clippy::float_cmp)]
399-
if window.scale_factor() != scale_factor {
398+
let prior_factor = window.scale_factor();
399+
window.update_scale_factor_from_backend(scale_factor);
400+
let new_factor = window.scale_factor();
401+
if let Some(forced_factor) = window.scale_factor_override() {
402+
// If there is a scale factor override, then force that to be used
403+
// Otherwise, use the OS suggested size
404+
// We have already told the OS about our resize constraints, so
405+
// the new_inner_size should take those into account
406+
*new_inner_size = winit::dpi::LogicalSize::new(
407+
window.requested_width(),
408+
window.requested_height(),
409+
)
410+
.to_physical::<u32>(forced_factor);
411+
} else if approx::relative_ne!(new_factor, prior_factor) {
400412
let mut scale_factor_change_events = world
401413
.get_resource_mut::<Events<WindowScaleFactorChanged>>()
402414
.unwrap();
@@ -407,17 +419,17 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
407419
});
408420
}
409421

410-
window.update_scale_factor_from_backend(scale_factor);
411-
412-
if window.physical_width() != new_inner_size.width
413-
|| window.physical_height() != new_inner_size.height
422+
let new_logical_width = new_inner_size.width as f64 / new_factor;
423+
let new_logical_height = new_inner_size.height as f64 / new_factor;
424+
if approx::relative_ne!(window.width() as f64, new_logical_width)
425+
|| approx::relative_ne!(window.height() as f64, new_logical_height)
414426
{
415427
let mut resize_events =
416428
world.get_resource_mut::<Events<WindowResized>>().unwrap();
417429
resize_events.send(WindowResized {
418430
id: window_id,
419-
width: window.width(),
420-
height: window.height(),
431+
width: new_logical_width as f32,
432+
height: new_logical_height as f32,
421433
});
422434
}
423435
window.update_actual_size_from_backend(

0 commit comments

Comments
 (0)