From 72f795e6875cb7b0a0e885e67b7086864f0a3e79 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Sat, 6 Feb 2021 12:11:40 +0200 Subject: [PATCH 01/20] Add Window Resize Constraints --- crates/bevy_window/src/window.rs | 30 ++++++++++++++++++++ crates/bevy_winit/src/winit_windows.rs | 38 +++++++++++++++++++++++++- examples/window/window_settings.rs | 8 +++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index fc9d800cdaeff..a4a5c2c964845 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -32,6 +32,25 @@ impl Default for WindowId { } } +#[derive(Debug, Clone)] +pub struct WindowResizeConstraints { + pub min_width: f32, + pub min_height: f32, + pub max_width: f32, + pub max_height: f32, +} + +impl Default for WindowResizeConstraints { + fn default() -> Self { + Self { + min_width: 360., + min_height: 240., + max_width: 23040., + max_height: 8640., + } + } +} + /// An operating system window that can present content and receive user input. /// /// ## Window Sizes @@ -54,6 +73,7 @@ pub struct Window { requested_height: f32, physical_width: u32, physical_height: u32, + resize_constraints: WindowResizeConstraints, position: Option, scale_factor_override: Option, backend_scale_factor: f64, @@ -143,6 +163,7 @@ impl Window { position, physical_width, physical_height, + resize_constraints: window_descriptor.resize_constraints.clone(), scale_factor_override: window_descriptor.scale_factor_override, backend_scale_factor: scale_factor, title: window_descriptor.title.clone(), @@ -208,6 +229,12 @@ impl Window { self.physical_height } + /// The window's client resize constraint in physical pixels. + #[inline] + pub fn resize_constraints(&self) -> WindowResizeConstraints { + self.resize_constraints.clone() + } + /// The window's client position in physical pixels. #[inline] pub fn position(&self) -> Option { @@ -255,6 +282,7 @@ impl Window { if self.requested_width == width && self.requested_height == height { return; } + self.requested_width = width; self.requested_height = height; self.command_queue.push(WindowCommand::SetResolution { @@ -424,6 +452,7 @@ impl Window { pub struct WindowDescriptor { pub width: f32, pub height: f32, + pub resize_constraints: WindowResizeConstraints, pub scale_factor_override: Option, pub title: String, pub vsync: bool, @@ -442,6 +471,7 @@ impl Default for WindowDescriptor { title: "bevy".to_string(), width: 1280., height: 720., + resize_constraints: WindowResizeConstraints::default(), scale_factor_override: None, vsync: true, resizable: true, diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 736b98e762305..9b2eba4588042 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -1,6 +1,7 @@ use bevy_math::IVec2; use bevy_utils::HashMap; -use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode}; +use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode, WindowResizeConstraints}; +use winit::dpi::LogicalSize; #[derive(Debug, Default)] pub struct WinitWindows { @@ -10,6 +11,7 @@ pub struct WinitWindows { } impl WinitWindows { + #[allow(clippy::float_cmp)] pub fn create_window( &mut self, event_loop: &winit::event_loop::EventLoopWindowTarget<()>, @@ -25,6 +27,38 @@ impl WinitWindows { #[cfg(not(target_os = "windows"))] let mut winit_window_builder = winit::window::WindowBuilder::new(); + let default_resize_constraints = WindowResizeConstraints::default(); + let mut final_resize_constraints = window_descriptor.resize_constraints.clone(); + + if final_resize_constraints.min_width < default_resize_constraints.min_width { + final_resize_constraints.min_width = default_resize_constraints.min_width; + } + if final_resize_constraints.min_height < default_resize_constraints.min_height { + final_resize_constraints.min_height = default_resize_constraints.min_height; + } + if final_resize_constraints.max_width > default_resize_constraints.max_width { + final_resize_constraints.max_width = default_resize_constraints.max_width; + } + if final_resize_constraints.max_height > default_resize_constraints.max_height { + final_resize_constraints.max_height = default_resize_constraints.max_height; + } + if final_resize_constraints.max_width < final_resize_constraints.min_width { + final_resize_constraints.max_width = default_resize_constraints.max_width; + } + if final_resize_constraints.max_height < final_resize_constraints.min_height { + final_resize_constraints.max_height = default_resize_constraints.max_height; + } + + let min_inner_size = LogicalSize { + width: final_resize_constraints.min_width, + height: final_resize_constraints.min_height, + }; + + let max_inner_size = LogicalSize { + width: final_resize_constraints.max_width, + height: final_resize_constraints.max_height, + }; + winit_window_builder = match window_descriptor.mode { WindowMode::BorderlessFullscreen => winit_window_builder.with_fullscreen(Some( winit::window::Fullscreen::Borderless(event_loop.primary_monitor()), @@ -56,6 +90,8 @@ impl WinitWindows { } } .with_resizable(window_descriptor.resizable) + .with_min_inner_size(min_inner_size) + .with_max_inner_size(max_inner_size) .with_decorations(window_descriptor.decorations), }; diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index 3c7719916ac2e..df850dee010a0 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{prelude::*, window::WindowResizeConstraints}; /// This example illustrates how to customize the default window settings fn main() { @@ -8,6 +8,12 @@ fn main() { width: 500., height: 300., vsync: true, + resizable: true, + resize_constraints: WindowResizeConstraints { + max_width: 1280., + max_height: 720., + ..Default::default() + }, ..Default::default() }) .add_plugins(DefaultPlugins) From bb4f1bb086100bf4c8c4f48bf69ce1235430376f Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Sat, 6 Feb 2021 12:22:15 +0200 Subject: [PATCH 02/20] Fix format --- crates/bevy_winit/src/winit_windows.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 9b2eba4588042..fc1c876d4e7e0 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -29,7 +29,6 @@ impl WinitWindows { let default_resize_constraints = WindowResizeConstraints::default(); let mut final_resize_constraints = window_descriptor.resize_constraints.clone(); - if final_resize_constraints.min_width < default_resize_constraints.min_width { final_resize_constraints.min_width = default_resize_constraints.min_width; } From 6140363aa82e94f35ac132619cd4d560d6b17607 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Sat, 6 Feb 2021 12:43:46 +0200 Subject: [PATCH 03/20] Fix resize_constraints docs --- crates/bevy_window/src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index a4a5c2c964845..c45f7c20837b0 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -229,7 +229,7 @@ impl Window { self.physical_height } - /// The window's client resize constraint in physical pixels. + /// The window's client resize constraint in logical pixels. #[inline] pub fn resize_constraints(&self) -> WindowResizeConstraints { self.resize_constraints.clone() From 0599ae4fb6c4aa4e2f5396ade402760a5c4412e6 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Sat, 6 Feb 2021 14:47:07 +0200 Subject: [PATCH 04/20] Change minimum resolution to 180x120 --- crates/bevy_window/src/window.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index c45f7c20837b0..2c8ad069b24c3 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -43,8 +43,8 @@ pub struct WindowResizeConstraints { impl Default for WindowResizeConstraints { fn default() -> Self { Self { - min_width: 360., - min_height: 240., + min_width: 180., + min_height: 120., max_width: 23040., max_height: 8640., } From 978562794f8ce942156c80caf7b20b871d5a59fc Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Sat, 6 Feb 2021 15:10:03 +0200 Subject: [PATCH 05/20] Make max inner size infinite by default --- crates/bevy_window/src/window.rs | 5 +++-- crates/bevy_winit/src/winit_windows.rs | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 2c8ad069b24c3..baef92b279dc8 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -18,6 +18,7 @@ impl WindowId { } } +use core::f32; use std::fmt; impl fmt::Display for WindowId { @@ -45,8 +46,8 @@ impl Default for WindowResizeConstraints { Self { min_width: 180., min_height: 120., - max_width: 23040., - max_height: 8640., + max_width: f32::INFINITY, + max_height: f32::INFINITY, } } } diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index fc1c876d4e7e0..f322c3d5c2b57 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -1,3 +1,5 @@ +use core::f32; + use bevy_math::IVec2; use bevy_utils::HashMap; use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode, WindowResizeConstraints}; @@ -89,11 +91,19 @@ impl WinitWindows { } } .with_resizable(window_descriptor.resizable) - .with_min_inner_size(min_inner_size) - .with_max_inner_size(max_inner_size) .with_decorations(window_descriptor.decorations), }; + #[allow(unused_mut)] + let mut winit_window_builder = if final_resize_constraints.max_width != f32::INFINITY && final_resize_constraints.max_height != f32::INFINITY { + winit_window_builder + .with_min_inner_size(min_inner_size) + .with_max_inner_size(max_inner_size) + } else { + winit_window_builder + .with_min_inner_size(min_inner_size) + }; + #[allow(unused_mut)] let mut winit_window_builder = winit_window_builder.with_title(&window_descriptor.title); From b3e9147ebbfff87658c3a6e48dca39f315137fde Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Sat, 6 Feb 2021 15:21:40 +0200 Subject: [PATCH 06/20] Fix format --- crates/bevy_winit/src/winit_windows.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index f322c3d5c2b57..61f780f879afb 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -95,13 +95,14 @@ impl WinitWindows { }; #[allow(unused_mut)] - let mut winit_window_builder = if final_resize_constraints.max_width != f32::INFINITY && final_resize_constraints.max_height != f32::INFINITY { + let mut winit_window_builder = if final_resize_constraints.max_width != f32::INFINITY + && final_resize_constraints.max_height != f32::INFINITY + { winit_window_builder .with_min_inner_size(min_inner_size) .with_max_inner_size(max_inner_size) } else { - winit_window_builder - .with_min_inner_size(min_inner_size) + winit_window_builder.with_min_inner_size(min_inner_size) }; #[allow(unused_mut)] From f15e8dc83f32d2ba0606dcdd854ee649c88f2fd2 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Sat, 6 Feb 2021 15:39:20 +0200 Subject: [PATCH 07/20] Remove Resize Constraints from Window struct --- crates/bevy_window/src/window.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index baef92b279dc8..bca20732b2a58 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -74,7 +74,6 @@ pub struct Window { requested_height: f32, physical_width: u32, physical_height: u32, - resize_constraints: WindowResizeConstraints, position: Option, scale_factor_override: Option, backend_scale_factor: f64, @@ -164,7 +163,6 @@ impl Window { position, physical_width, physical_height, - resize_constraints: window_descriptor.resize_constraints.clone(), scale_factor_override: window_descriptor.scale_factor_override, backend_scale_factor: scale_factor, title: window_descriptor.title.clone(), @@ -230,12 +228,6 @@ impl Window { self.physical_height } - /// The window's client resize constraint in logical pixels. - #[inline] - pub fn resize_constraints(&self) -> WindowResizeConstraints { - self.resize_constraints.clone() - } - /// The window's client position in physical pixels. #[inline] pub fn position(&self) -> Option { From 6e32651259244e1b4140cb13181e5b23af6a938a Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Tue, 9 Feb 2021 22:44:51 +0200 Subject: [PATCH 08/20] Update Window Resize Constraints --- crates/bevy_window/src/window.rs | 1 - crates/bevy_winit/Cargo.toml | 1 + crates/bevy_winit/src/winit_windows.rs | 53 +++++++++++++------------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index bca20732b2a58..6ef4ccc6c961b 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -18,7 +18,6 @@ impl WindowId { } } -use core::f32; use std::fmt; impl fmt::Display for WindowId { diff --git a/crates/bevy_winit/Cargo.toml b/crates/bevy_winit/Cargo.toml index f9ef5394d147a..18a0b9c6a4899 100644 --- a/crates/bevy_winit/Cargo.toml +++ b/crates/bevy_winit/Cargo.toml @@ -21,6 +21,7 @@ x11 = ["winit/x11"] bevy_app = { path = "../bevy_app", version = "0.4.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.4.0" } bevy_input = { path = "../bevy_input", version = "0.4.0" } +bevy_log = { path = "../bevy_log", version = "0.4.0" } bevy_math = { path = "../bevy_math", version = "0.4.0" } bevy_window = { path = "../bevy_window", version = "0.4.0" } bevy_utils = { path = "../bevy_utils", version = "0.4.0" } diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 61f780f879afb..a040e1f3dd20a 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -1,5 +1,4 @@ -use core::f32; - +use bevy_log::warn; use bevy_math::IVec2; use bevy_utils::HashMap; use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode, WindowResizeConstraints}; @@ -29,35 +28,37 @@ impl WinitWindows { #[cfg(not(target_os = "windows"))] let mut winit_window_builder = winit::window::WindowBuilder::new(); - let default_resize_constraints = WindowResizeConstraints::default(); - let mut final_resize_constraints = window_descriptor.resize_constraints.clone(); - if final_resize_constraints.min_width < default_resize_constraints.min_width { - final_resize_constraints.min_width = default_resize_constraints.min_width; - } - if final_resize_constraints.min_height < default_resize_constraints.min_height { - final_resize_constraints.min_height = default_resize_constraints.min_height; - } - if final_resize_constraints.max_width > default_resize_constraints.max_width { - final_resize_constraints.max_width = default_resize_constraints.max_width; + let WindowResizeConstraints { + mut min_width, + mut min_height, + mut max_width, + mut max_height, + } = window_descriptor.resize_constraints.clone(); + min_width = min_width.max(1.); + min_height = min_height.max(1.); + if max_width < min_width { + warn!( + "The given maximum width {} is smaller than the minimum width {}", + max_width, min_width + ); + max_width = min_width; } - if final_resize_constraints.max_height > default_resize_constraints.max_height { - final_resize_constraints.max_height = default_resize_constraints.max_height; - } - if final_resize_constraints.max_width < final_resize_constraints.min_width { - final_resize_constraints.max_width = default_resize_constraints.max_width; - } - if final_resize_constraints.max_height < final_resize_constraints.min_height { - final_resize_constraints.max_height = default_resize_constraints.max_height; + if max_height < min_height { + warn!( + "The given maximum height {} is smaller than the minimum height {}", + max_height, min_height + ); + max_height = min_height; } let min_inner_size = LogicalSize { - width: final_resize_constraints.min_width, - height: final_resize_constraints.min_height, + width: min_width, + height: min_height, }; let max_inner_size = LogicalSize { - width: final_resize_constraints.max_width, - height: final_resize_constraints.max_height, + width: max_width, + height: max_height, }; winit_window_builder = match window_descriptor.mode { @@ -95,9 +96,7 @@ impl WinitWindows { }; #[allow(unused_mut)] - let mut winit_window_builder = if final_resize_constraints.max_width != f32::INFINITY - && final_resize_constraints.max_height != f32::INFINITY - { + let mut winit_window_builder = if max_width.is_finite() && max_height.is_finite() { winit_window_builder .with_min_inner_size(min_inner_size) .with_max_inner_size(max_inner_size) From 5480a40965b6b35bae247671780f59daa745d9f3 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 11:46:30 +0200 Subject: [PATCH 09/20] Add Window Set Resize Constraints Command --- crates/bevy_window/src/window.rs | 18 ++++++++++ crates/bevy_winit/src/lib.rs | 21 +++++++++++ crates/bevy_winit/src/utils.rs | 30 ++++++++++++++++ crates/bevy_winit/src/winit_windows.rs | 49 +++++++++----------------- 4 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 crates/bevy_winit/src/utils.rs diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 6ef4ccc6c961b..61320290e1d9a 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -73,6 +73,7 @@ pub struct Window { requested_height: f32, physical_width: u32, physical_height: u32, + resize_constraints: WindowResizeConstraints, position: Option, scale_factor_override: Option, backend_scale_factor: f64, @@ -132,6 +133,9 @@ pub enum WindowCommand { SetPosition { position: IVec2, }, + SetResizeConstraints { + resize_constraints: WindowResizeConstraints, + }, } /// Defines the way a window is displayed @@ -162,6 +166,7 @@ impl Window { position, physical_width, physical_height, + resize_constraints: window_descriptor.resize_constraints.clone(), scale_factor_override: window_descriptor.scale_factor_override, backend_scale_factor: scale_factor, title: window_descriptor.title.clone(), @@ -227,6 +232,12 @@ impl Window { self.physical_height } + /// The window's client resize constraint in logical pixels. + #[inline] + pub fn resize_constraints(&self) -> WindowResizeConstraints { + self.resize_constraints.clone() + } + /// The window's client position in physical pixels. #[inline] pub fn position(&self) -> Option { @@ -267,6 +278,13 @@ impl Window { .push(WindowCommand::SetPosition { position }) } + /// Modifies the minimum and maximum window bounds for resizing in logical pixels. + #[inline] + pub fn set_resize_constraints(&mut self, resize_constraints: WindowResizeConstraints) { + self.command_queue + .push(WindowCommand::SetResizeConstraints { resize_constraints }); + } + /// Request the OS to resize the window such the the client area matches the /// specified width and height. #[allow(clippy::float_cmp)] diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 0f5d082a3739a..157134fdbd8d2 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -1,4 +1,5 @@ mod converters; +mod utils; mod winit_config; mod winit_windows; @@ -33,6 +34,8 @@ use winit::{ target_os = "openbsd" ))] use winit::platform::unix::EventLoopExtUnix; +use winit::dpi::LogicalSize; +use crate::utils::check_resize_constraints; #[derive(Default)] pub struct WinitPlugin; @@ -139,6 +142,24 @@ fn change_window(_: &mut World, resources: &mut Resources) { y: position[1], }); } + bevy_window::WindowCommand::SetResizeConstraints { resize_constraints } => { + let window = winit_windows.get_window(id).unwrap(); + let resize_constraints = check_resize_constraints(resize_constraints); + let min_inner_size = LogicalSize { + width: resize_constraints.min_width, + height: resize_constraints.min_height, + }; + let max_inner_size = LogicalSize { + width: resize_constraints.max_width, + height: resize_constraints.max_height, + }; + if resize_constraints.max_width.is_finite() && resize_constraints.max_height.is_finite() { + window.set_min_inner_size(Some(min_inner_size)); + window.set_max_inner_size(Some(max_inner_size)); + } else { + window.set_min_inner_size(Some(min_inner_size)); + } + } } } } diff --git a/crates/bevy_winit/src/utils.rs b/crates/bevy_winit/src/utils.rs new file mode 100644 index 0000000000000..adf983a9dc97d --- /dev/null +++ b/crates/bevy_winit/src/utils.rs @@ -0,0 +1,30 @@ +use bevy_log::warn; +use bevy_window::WindowResizeConstraints; + +pub(crate) fn check_resize_constraints( + resize_constraints: WindowResizeConstraints, +) -> WindowResizeConstraints { + let WindowResizeConstraints { + mut min_width, + mut min_height, + mut max_width, + mut max_height, + } = resize_constraints.clone(); + min_width = min_width.max(1.); + min_height = min_height.max(1.); + if max_width < min_width { + warn!( + "The given maximum width {} is smaller than the minimum width {}", + max_width, min_width + ); + max_width = min_width; + } + if max_height < min_height { + warn!( + "The given maximum height {} is smaller than the minimum height {}", + max_height, min_height + ); + max_height = min_height; + } + return WindowResizeConstraints{min_width, min_height, max_width, max_height}; +} \ No newline at end of file diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index a040e1f3dd20a..08d3d3769996c 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -3,6 +3,7 @@ use bevy_math::IVec2; use bevy_utils::HashMap; use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode, WindowResizeConstraints}; use winit::dpi::LogicalSize; +use crate::utils::check_resize_constraints; #[derive(Debug, Default)] pub struct WinitWindows { @@ -28,39 +29,6 @@ impl WinitWindows { #[cfg(not(target_os = "windows"))] let mut winit_window_builder = winit::window::WindowBuilder::new(); - let WindowResizeConstraints { - mut min_width, - mut min_height, - mut max_width, - mut max_height, - } = window_descriptor.resize_constraints.clone(); - min_width = min_width.max(1.); - min_height = min_height.max(1.); - if max_width < min_width { - warn!( - "The given maximum width {} is smaller than the minimum width {}", - max_width, min_width - ); - max_width = min_width; - } - if max_height < min_height { - warn!( - "The given maximum height {} is smaller than the minimum height {}", - max_height, min_height - ); - max_height = min_height; - } - - let min_inner_size = LogicalSize { - width: min_width, - height: min_height, - }; - - let max_inner_size = LogicalSize { - width: max_width, - height: max_height, - }; - winit_window_builder = match window_descriptor.mode { WindowMode::BorderlessFullscreen => winit_window_builder.with_fullscreen(Some( winit::window::Fullscreen::Borderless(event_loop.primary_monitor()), @@ -95,6 +63,21 @@ impl WinitWindows { .with_decorations(window_descriptor.decorations), }; + let WindowResizeConstraints { + mut min_width, + mut min_height, + mut max_width, + mut max_height, + } = check_resize_constraints(window_descriptor.resize_constraints.clone()); + let min_inner_size = LogicalSize { + width: min_width, + height: min_height, + }; + let max_inner_size = LogicalSize { + width: max_width, + height: max_height, + }; + #[allow(unused_mut)] let mut winit_window_builder = if max_width.is_finite() && max_height.is_finite() { winit_window_builder From 4e52e5f3d58868412be8377c038f6281ac442885 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 12:00:24 +0200 Subject: [PATCH 10/20] Fix format and warnings --- crates/bevy_winit/src/lib.rs | 23 ++++++++++++++--------- crates/bevy_winit/src/utils.rs | 7 ++++++- crates/bevy_winit/src/winit_windows.rs | 11 +++++------ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 157134fdbd8d2..c7cc2b8a5f6d4 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -18,7 +18,7 @@ use bevy_utils::tracing::{error, trace, warn}; use bevy_window::{ CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused, - WindowMoved, WindowResized, WindowScaleFactorChanged, Windows, + WindowMoved, WindowResizeConstraints, WindowResized, WindowScaleFactorChanged, Windows, }; use winit::{ dpi::PhysicalPosition, @@ -26,6 +26,8 @@ use winit::{ event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget}, }; +use crate::utils::check_resize_constraints; +use winit::dpi::LogicalSize; #[cfg(any( target_os = "linux", target_os = "dragonfly", @@ -34,8 +36,6 @@ use winit::{ target_os = "openbsd" ))] use winit::platform::unix::EventLoopExtUnix; -use winit::dpi::LogicalSize; -use crate::utils::check_resize_constraints; #[derive(Default)] pub struct WinitPlugin; @@ -144,16 +144,21 @@ fn change_window(_: &mut World, resources: &mut Resources) { } bevy_window::WindowCommand::SetResizeConstraints { resize_constraints } => { let window = winit_windows.get_window(id).unwrap(); - let resize_constraints = check_resize_constraints(resize_constraints); + let WindowResizeConstraints { + min_width, + min_height, + max_width, + max_height, + } = check_resize_constraints(resize_constraints); let min_inner_size = LogicalSize { - width: resize_constraints.min_width, - height: resize_constraints.min_height, + width: min_width, + height: min_height, }; let max_inner_size = LogicalSize { - width: resize_constraints.max_width, - height: resize_constraints.max_height, + width: max_width, + height: max_height, }; - if resize_constraints.max_width.is_finite() && resize_constraints.max_height.is_finite() { + if max_width.is_finite() && max_height.is_finite() { window.set_min_inner_size(Some(min_inner_size)); window.set_max_inner_size(Some(max_inner_size)); } else { diff --git a/crates/bevy_winit/src/utils.rs b/crates/bevy_winit/src/utils.rs index adf983a9dc97d..3abb27b7e53f5 100644 --- a/crates/bevy_winit/src/utils.rs +++ b/crates/bevy_winit/src/utils.rs @@ -26,5 +26,10 @@ pub(crate) fn check_resize_constraints( ); max_height = min_height; } - return WindowResizeConstraints{min_width, min_height, max_width, max_height}; + return WindowResizeConstraints { + min_width, + min_height, + max_width, + max_height + }; } \ No newline at end of file diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 08d3d3769996c..57973384b94cf 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -1,9 +1,8 @@ -use bevy_log::warn; +use crate::utils::check_resize_constraints; use bevy_math::IVec2; use bevy_utils::HashMap; use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode, WindowResizeConstraints}; use winit::dpi::LogicalSize; -use crate::utils::check_resize_constraints; #[derive(Debug, Default)] pub struct WinitWindows { @@ -64,10 +63,10 @@ impl WinitWindows { }; let WindowResizeConstraints { - mut min_width, - mut min_height, - mut max_width, - mut max_height, + min_width, + min_height, + max_width, + max_height, } = check_resize_constraints(window_descriptor.resize_constraints.clone()); let min_inner_size = LogicalSize { width: min_width, From 6d834e5e927d0c1e4bda0ff21f1e10d1170da789 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 12:03:41 +0200 Subject: [PATCH 11/20] Fix format in utils --- crates/bevy_winit/src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_winit/src/utils.rs b/crates/bevy_winit/src/utils.rs index 3abb27b7e53f5..e641ec010abaf 100644 --- a/crates/bevy_winit/src/utils.rs +++ b/crates/bevy_winit/src/utils.rs @@ -30,6 +30,6 @@ pub(crate) fn check_resize_constraints( min_width, min_height, max_width, - max_height + max_height, }; -} \ No newline at end of file +} From 015883f9c7ab4383ee640170a3b4c548fe2e1acd Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 12:12:28 +0200 Subject: [PATCH 12/20] Fix clippy errors --- crates/bevy_winit/src/utils.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_winit/src/utils.rs b/crates/bevy_winit/src/utils.rs index e641ec010abaf..ddb55807eee65 100644 --- a/crates/bevy_winit/src/utils.rs +++ b/crates/bevy_winit/src/utils.rs @@ -9,7 +9,7 @@ pub(crate) fn check_resize_constraints( mut min_height, mut max_width, mut max_height, - } = resize_constraints.clone(); + } = resize_constraints; min_width = min_width.max(1.); min_height = min_height.max(1.); if max_width < min_width { @@ -26,10 +26,10 @@ pub(crate) fn check_resize_constraints( ); max_height = min_height; } - return WindowResizeConstraints { + WindowResizeConstraints { min_width, min_height, max_width, max_height, - }; + } } From 0f9d101e5a4044219c36586da71063e5a555fe1a Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 12:20:03 +0200 Subject: [PATCH 13/20] Move float_cmp to check_resize_constraints --- crates/bevy_winit/src/utils.rs | 1 + crates/bevy_winit/src/winit_windows.rs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_winit/src/utils.rs b/crates/bevy_winit/src/utils.rs index ddb55807eee65..22f5571ed7969 100644 --- a/crates/bevy_winit/src/utils.rs +++ b/crates/bevy_winit/src/utils.rs @@ -1,6 +1,7 @@ use bevy_log::warn; use bevy_window::WindowResizeConstraints; +#[allow(clippy::float_cmp)] pub(crate) fn check_resize_constraints( resize_constraints: WindowResizeConstraints, ) -> WindowResizeConstraints { diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 57973384b94cf..cceef505abd47 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -12,7 +12,6 @@ pub struct WinitWindows { } impl WinitWindows { - #[allow(clippy::float_cmp)] pub fn create_window( &mut self, event_loop: &winit::event_loop::EventLoopWindowTarget<()>, From 0cf3b88ee16f240ef31275a867c280b9ac3ddc12 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 15:33:04 +0200 Subject: [PATCH 14/20] Code refactoring --- crates/bevy_window/src/window.rs | 2 +- crates/bevy_winit/src/utils.rs | 8 +++----- crates/bevy_winit/src/winit_windows.rs | 3 +-- examples/window/window_settings.rs | 6 ------ 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 61320290e1d9a..79fff1b96874f 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -32,7 +32,7 @@ impl Default for WindowId { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct WindowResizeConstraints { pub min_width: f32, pub min_height: f32, diff --git a/crates/bevy_winit/src/utils.rs b/crates/bevy_winit/src/utils.rs index 22f5571ed7969..f1c26ef6d05b8 100644 --- a/crates/bevy_winit/src/utils.rs +++ b/crates/bevy_winit/src/utils.rs @@ -1,16 +1,14 @@ use bevy_log::warn; use bevy_window::WindowResizeConstraints; -#[allow(clippy::float_cmp)] pub(crate) fn check_resize_constraints( - resize_constraints: WindowResizeConstraints, -) -> WindowResizeConstraints { - let WindowResizeConstraints { + WindowResizeConstraints { mut min_width, mut min_height, mut max_width, mut max_height, - } = resize_constraints; + }: WindowResizeConstraints, +) -> WindowResizeConstraints { min_width = min_width.max(1.); min_height = min_height.max(1.); if max_width < min_width { diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index cceef505abd47..a2fd58975789d 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -76,8 +76,7 @@ impl WinitWindows { height: max_height, }; - #[allow(unused_mut)] - let mut winit_window_builder = if max_width.is_finite() && max_height.is_finite() { + let winit_window_builder = if max_width.is_finite() && max_height.is_finite() { winit_window_builder .with_min_inner_size(min_inner_size) .with_max_inner_size(max_inner_size) diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index df850dee010a0..9a20ad211ff23 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -8,12 +8,6 @@ fn main() { width: 500., height: 300., vsync: true, - resizable: true, - resize_constraints: WindowResizeConstraints { - max_width: 1280., - max_height: 720., - ..Default::default() - }, ..Default::default() }) .add_plugins(DefaultPlugins) From 4cfd81db06e8023f104a16954ef78834c40b78fa Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 15:52:51 +0200 Subject: [PATCH 15/20] Remove .clone() call on resize constraints data --- crates/bevy_window/src/window.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 79fff1b96874f..9015e98de9ecd 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -166,7 +166,7 @@ impl Window { position, physical_width, physical_height, - resize_constraints: window_descriptor.resize_constraints.clone(), + resize_constraints: window_descriptor.resize_constraints, scale_factor_override: window_descriptor.scale_factor_override, backend_scale_factor: scale_factor, title: window_descriptor.title.clone(), @@ -235,7 +235,7 @@ impl Window { /// The window's client resize constraint in logical pixels. #[inline] pub fn resize_constraints(&self) -> WindowResizeConstraints { - self.resize_constraints.clone() + self.resize_constraints } /// The window's client position in physical pixels. From d7d463263e576a9335099a4f89d4fe638762bb13 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 16:04:06 +0200 Subject: [PATCH 16/20] Remove another .clone() --- crates/bevy_winit/src/winit_windows.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index a2fd58975789d..a42146be7f710 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -66,7 +66,7 @@ impl WinitWindows { min_height, max_width, max_height, - } = check_resize_constraints(window_descriptor.resize_constraints.clone()); + } = check_resize_constraints(window_descriptor.resize_constraints); let min_inner_size = LogicalSize { width: min_width, height: min_height, From 07e14e9a0daa4a53df2398f0c513fe2c3e91dc47 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 16:11:00 +0200 Subject: [PATCH 17/20] Remove unused imports --- examples/window/window_settings.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index 9a20ad211ff23..7e779cd0decd4 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -1,4 +1,4 @@ -use bevy::{prelude::*, window::WindowResizeConstraints}; +use bevy::{prelude::*}; /// This example illustrates how to customize the default window settings fn main() { From e84d4cbd8af47cf15996ddada831e1c5997350f1 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Wed, 10 Feb 2021 16:17:00 +0200 Subject: [PATCH 18/20] Fix format --- examples/window/window_settings.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index 7e779cd0decd4..3c7719916ac2e 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -1,4 +1,4 @@ -use bevy::{prelude::*}; +use bevy::prelude::*; /// This example illustrates how to customize the default window settings fn main() { From 5c888e86e3b63fec6faf4eac2c3b791358c05064 Mon Sep 17 00:00:00 2001 From: Gabriel Gheorghe Date: Fri, 12 Feb 2021 23:14:27 +0200 Subject: [PATCH 19/20] Add docs to WindowResizeConstraints --- crates/bevy_window/src/window.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 9015e98de9ecd..637d4164f9512 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -32,6 +32,12 @@ impl Default for WindowId { } } +/// The size limits on a window. +/// These values are measured in logical pixels, so the user's +/// scale factor does affect the size limits on the window. +/// Please note that if the window is resizable, then when the window is +/// maximized it may have a size outside of these limits. The functionality +/// required to disable maximizing is not yet exposed by winit. #[derive(Debug, Clone, Copy)] pub struct WindowResizeConstraints { pub min_width: f32, From 31fa5f469b064ee223b34f855a09bc8878550e49 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 2 Mar 2021 18:53:41 -0800 Subject: [PATCH 20/20] remove utils method in favor of WindowResizeConstraints::check_constraints --- crates/bevy_window/src/window.rs | 35 +++++++++++++++++++++++++- crates/bevy_winit/src/lib.rs | 21 ++++++---------- crates/bevy_winit/src/utils.rs | 34 ------------------------- crates/bevy_winit/src/winit_windows.rs | 33 +++++++++++------------- 4 files changed, 55 insertions(+), 68 deletions(-) delete mode 100644 crates/bevy_winit/src/utils.rs diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 637d4164f9512..488dcfe5ec3dd 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -1,5 +1,5 @@ use bevy_math::{IVec2, Vec2}; -use bevy_utils::Uuid; +use bevy_utils::{tracing::warn, Uuid}; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub struct WindowId(Uuid); @@ -57,6 +57,39 @@ impl Default for WindowResizeConstraints { } } +impl WindowResizeConstraints { + pub fn check_constraints(&self) -> WindowResizeConstraints { + let WindowResizeConstraints { + mut min_width, + mut min_height, + mut max_width, + mut max_height, + } = self; + min_width = min_width.max(1.); + min_height = min_height.max(1.); + if max_width < min_width { + warn!( + "The given maximum width {} is smaller than the minimum width {}", + max_width, min_width + ); + max_width = min_width; + } + if max_height < min_height { + warn!( + "The given maximum height {} is smaller than the minimum height {}", + max_height, min_height + ); + max_height = min_height; + } + WindowResizeConstraints { + min_width, + min_height, + max_width, + max_height, + } + } +} + /// An operating system window that can present content and receive user input. /// /// ## Window Sizes diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index c7cc2b8a5f6d4..3afe0900ee549 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -1,5 +1,4 @@ mod converters; -mod utils; mod winit_config; mod winit_windows; @@ -18,7 +17,7 @@ use bevy_utils::tracing::{error, trace, warn}; use bevy_window::{ CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused, - WindowMoved, WindowResizeConstraints, WindowResized, WindowScaleFactorChanged, Windows, + WindowMoved, WindowResized, WindowScaleFactorChanged, Windows, }; use winit::{ dpi::PhysicalPosition, @@ -26,7 +25,6 @@ use winit::{ event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget}, }; -use crate::utils::check_resize_constraints; use winit::dpi::LogicalSize; #[cfg(any( target_os = "linux", @@ -144,21 +142,16 @@ fn change_window(_: &mut World, resources: &mut Resources) { } bevy_window::WindowCommand::SetResizeConstraints { resize_constraints } => { let window = winit_windows.get_window(id).unwrap(); - let WindowResizeConstraints { - min_width, - min_height, - max_width, - max_height, - } = check_resize_constraints(resize_constraints); + let constraints = resize_constraints.check_constraints(); let min_inner_size = LogicalSize { - width: min_width, - height: min_height, + width: constraints.min_width, + height: constraints.min_height, }; let max_inner_size = LogicalSize { - width: max_width, - height: max_height, + width: constraints.max_width, + height: constraints.max_height, }; - if max_width.is_finite() && max_height.is_finite() { + if constraints.max_width.is_finite() && constraints.max_height.is_finite() { window.set_min_inner_size(Some(min_inner_size)); window.set_max_inner_size(Some(max_inner_size)); } else { diff --git a/crates/bevy_winit/src/utils.rs b/crates/bevy_winit/src/utils.rs deleted file mode 100644 index f1c26ef6d05b8..0000000000000 --- a/crates/bevy_winit/src/utils.rs +++ /dev/null @@ -1,34 +0,0 @@ -use bevy_log::warn; -use bevy_window::WindowResizeConstraints; - -pub(crate) fn check_resize_constraints( - WindowResizeConstraints { - mut min_width, - mut min_height, - mut max_width, - mut max_height, - }: WindowResizeConstraints, -) -> WindowResizeConstraints { - min_width = min_width.max(1.); - min_height = min_height.max(1.); - if max_width < min_width { - warn!( - "The given maximum width {} is smaller than the minimum width {}", - max_width, min_width - ); - max_width = min_width; - } - if max_height < min_height { - warn!( - "The given maximum height {} is smaller than the minimum height {}", - max_height, min_height - ); - max_height = min_height; - } - WindowResizeConstraints { - min_width, - min_height, - max_width, - max_height, - } -} diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index a42146be7f710..e3a3c518c0421 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -1,7 +1,6 @@ -use crate::utils::check_resize_constraints; use bevy_math::IVec2; use bevy_utils::HashMap; -use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode, WindowResizeConstraints}; +use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode}; use winit::dpi::LogicalSize; #[derive(Debug, Default)] @@ -61,28 +60,24 @@ impl WinitWindows { .with_decorations(window_descriptor.decorations), }; - let WindowResizeConstraints { - min_width, - min_height, - max_width, - max_height, - } = check_resize_constraints(window_descriptor.resize_constraints); + let constraints = window_descriptor.resize_constraints.check_constraints(); let min_inner_size = LogicalSize { - width: min_width, - height: min_height, + width: constraints.min_width, + height: constraints.min_height, }; let max_inner_size = LogicalSize { - width: max_width, - height: max_height, + width: constraints.max_width, + height: constraints.max_height, }; - let winit_window_builder = if max_width.is_finite() && max_height.is_finite() { - winit_window_builder - .with_min_inner_size(min_inner_size) - .with_max_inner_size(max_inner_size) - } else { - winit_window_builder.with_min_inner_size(min_inner_size) - }; + let winit_window_builder = + if constraints.max_width.is_finite() && constraints.max_height.is_finite() { + winit_window_builder + .with_min_inner_size(min_inner_size) + .with_max_inner_size(max_inner_size) + } else { + winit_window_builder.with_min_inner_size(min_inner_size) + }; #[allow(unused_mut)] let mut winit_window_builder = winit_window_builder.with_title(&window_descriptor.title);