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
This commit is contained in:
Daniel McNab 2021-09-10 18:46:16 +00:00
parent 4c3c4b5e40
commit 27bfbda7bc
3 changed files with 24 additions and 9 deletions

View file

@ -3,11 +3,13 @@ use std::path::PathBuf;
use super::{WindowDescriptor, WindowId};
use bevy_math::{IVec2, Vec2};
/// A window event that is sent whenever a window has been resized.
/// A window event that is sent whenever a windows logical size has changed
#[derive(Debug, Clone)]
pub struct WindowResized {
pub id: WindowId,
/// The new logical width of the window
pub width: f32,
/// The new logical height of the window
pub height: f32,
}

View file

@ -23,6 +23,7 @@ bevy_utils = { path = "../bevy_utils", version = "0.5.0" }
# other
winit = { version = "0.25.0", default-features = false }
approx = { version = "0.5.0", default-features = false }
[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = { version = "0.25.0", features = ["web-sys"], default-features = false }

View file

@ -395,8 +395,20 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
id: window_id,
scale_factor,
});
#[allow(clippy::float_cmp)]
if window.scale_factor() != scale_factor {
let prior_factor = window.scale_factor();
window.update_scale_factor_from_backend(scale_factor);
let new_factor = window.scale_factor();
if let Some(forced_factor) = window.scale_factor_override() {
// If there is a scale factor override, then force that to be used
// Otherwise, use the OS suggested size
// We have already told the OS about our resize constraints, so
// the new_inner_size should take those into account
*new_inner_size = winit::dpi::LogicalSize::new(
window.requested_width(),
window.requested_height(),
)
.to_physical::<u32>(forced_factor);
} else if approx::relative_ne!(new_factor, prior_factor) {
let mut scale_factor_change_events = world
.get_resource_mut::<Events<WindowScaleFactorChanged>>()
.unwrap();
@ -407,17 +419,17 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
});
}
window.update_scale_factor_from_backend(scale_factor);
if window.physical_width() != new_inner_size.width
|| window.physical_height() != new_inner_size.height
let new_logical_width = new_inner_size.width as f64 / new_factor;
let new_logical_height = new_inner_size.height as f64 / new_factor;
if approx::relative_ne!(window.width() as f64, new_logical_width)
|| approx::relative_ne!(window.height() as f64, new_logical_height)
{
let mut resize_events =
world.get_resource_mut::<Events<WindowResized>>().unwrap();
resize_events.send(WindowResized {
id: window_id,
width: window.width(),
height: window.height(),
width: new_logical_width as f32,
height: new_logical_height as f32,
});
}
window.update_actual_size_from_backend(