From 23882309e3b0168234908b12467766f066f5d268 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 7 Sep 2021 21:32:20 +0100 Subject: [PATCH 1/4] Fix scale_factor_override in the winit backend Fixes #2751 --- crates/bevy_winit/src/lib.rs | 62 ++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index af072b7a60f9f..8615a0b3b1526 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -395,35 +395,41 @@ 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 mut scale_factor_change_events = world - .get_resource_mut::>() - .unwrap(); - - scale_factor_change_events.send(WindowScaleFactorChanged { - id: window_id, - scale_factor, - }); + if let Some(old_factor) = window.scale_factor_override() { + *new_inner_size = winit::dpi::LogicalSize::new( + window.requested_width(), + window.requested_height(), + ) + .to_physical::(old_factor); + } else { + if window.scale_factor() != scale_factor { + let mut scale_factor_change_events = world + .get_resource_mut::>() + .unwrap(); + + scale_factor_change_events.send(WindowScaleFactorChanged { + id: window_id, + scale_factor, + }); + } + window.update_scale_factor_from_backend(scale_factor); + + if window.physical_width() != new_inner_size.width + || window.physical_height() != new_inner_size.height + { + let mut resize_events = + world.get_resource_mut::>().unwrap(); + resize_events.send(WindowResized { + id: window_id, + width: window.width(), + height: window.height(), + }); + } + window.update_actual_size_from_backend( + new_inner_size.width, + new_inner_size.height, + ); } - - window.update_scale_factor_from_backend(scale_factor); - - if window.physical_width() != new_inner_size.width - || window.physical_height() != new_inner_size.height - { - let mut resize_events = - world.get_resource_mut::>().unwrap(); - resize_events.send(WindowResized { - id: window_id, - width: window.width(), - height: window.height(), - }); - } - window.update_actual_size_from_backend( - new_inner_size.width, - new_inner_size.height, - ); } WindowEvent::Focused(focused) => { window.update_focused_status_from_backend(focused); From bd9a035806598af22ead178ff99f2cda555f9398 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 7 Sep 2021 21:53:28 +0100 Subject: [PATCH 2/4] Fix clippy failure --- crates/bevy_winit/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 8615a0b3b1526..063045407dd60 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -402,7 +402,7 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) { ) .to_physical::(old_factor); } else { - if window.scale_factor() != scale_factor { + if (window.scale_factor() - scale_factor).abs() > f64::EPSILON { let mut scale_factor_change_events = world .get_resource_mut::>() .unwrap(); From c2e4ddcfda98f04103f8cf832a8f0b4ae07512a5 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 8 Sep 2021 12:39:31 +0100 Subject: [PATCH 3/4] Fixup docs for WindowResized --- crates/bevy_window/src/event.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index 9feb54c4ed66e..72d6483d9b0df 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -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, } From 013713fbd771b61a0e255b4dbb17f1f1588264c3 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 8 Sep 2021 12:47:04 +0100 Subject: [PATCH 4/4] Correctly handle scale factors on resizing --- crates/bevy_winit/Cargo.toml | 1 + crates/bevy_winit/src/lib.rs | 62 ++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/crates/bevy_winit/Cargo.toml b/crates/bevy_winit/Cargo.toml index 9ae4d552a0a86..3b8c56af12645 100644 --- a/crates/bevy_winit/Cargo.toml +++ b/crates/bevy_winit/Cargo.toml @@ -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 } diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 063045407dd60..294ba67cd9fc7 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -395,41 +395,47 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) { id: window_id, scale_factor, }); - if let Some(old_factor) = window.scale_factor_override() { + 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::(old_factor); - } else { - if (window.scale_factor() - scale_factor).abs() > f64::EPSILON { - let mut scale_factor_change_events = world - .get_resource_mut::>() - .unwrap(); + .to_physical::(forced_factor); + } else if approx::relative_ne!(new_factor, prior_factor) { + let mut scale_factor_change_events = world + .get_resource_mut::>() + .unwrap(); - scale_factor_change_events.send(WindowScaleFactorChanged { - id: window_id, - scale_factor, - }); - } - window.update_scale_factor_from_backend(scale_factor); + scale_factor_change_events.send(WindowScaleFactorChanged { + id: window_id, + scale_factor, + }); + } - if window.physical_width() != new_inner_size.width - || window.physical_height() != new_inner_size.height - { - let mut resize_events = - world.get_resource_mut::>().unwrap(); - resize_events.send(WindowResized { - id: window_id, - width: window.width(), - height: window.height(), - }); - } - window.update_actual_size_from_backend( - new_inner_size.width, - 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::>().unwrap(); + resize_events.send(WindowResized { + id: window_id, + width: new_logical_width as f32, + height: new_logical_height as f32, + }); } + window.update_actual_size_from_backend( + new_inner_size.width, + new_inner_size.height, + ); } WindowEvent::Focused(focused) => { window.update_focused_status_from_backend(focused);