From 1cf29a6bdede5cd8082f08bf28fe851ff6e09d72 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Feb 2023 10:29:51 +0000 Subject: [PATCH 1/4] changes: * added `width`, `height`, and `all` const functions to `Size` * added the constant `Size::FILL`, a `Size` with width and height set to `Val::Percent(100.0)` --- crates/bevy_ui/src/geometry.rs | 51 ++++++++++++++++++++++------------ examples/ui/text_layout.rs | 4 +-- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 42eaa5651c665..6b9954357161d 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -339,10 +339,7 @@ pub struct Size { } impl Size { - pub const DEFAULT: Self = Self { - width: Val::DEFAULT, - height: Val::DEFAULT, - }; + pub const DEFAULT: Self = Self::all(Val::DEFAULT); /// Creates a new [`Size`] from a width and a height. /// @@ -360,17 +357,38 @@ impl Size { Size { width, height } } + /// Creates a new [`Size`] where both sides take the given value. + pub const fn all(value: Val) -> Self { + Self { + width: value, + height: value, + } + } + + /// Creates a new [`Size`] where `width` takes the given value. + pub const fn width(width: Val) -> Self { + Self { + width, + height: Val::DEFAULT, + } + } + + /// Creates a new [`Size`] where `height` takes the given value. + pub const fn height(width: Val) -> Self { + Self { + width, + height: Val::DEFAULT, + } + } + /// Creates a Size where both values are [`Val::Auto`]. - pub const AUTO: Size = Size { - width: Val::Auto, - height: Val::Auto, - }; + pub const AUTO: Self = Self::all(Val::Auto); /// Creates a Size where both values are [`Val::Undefined`]. - pub const UNDEFINED: Size = Size { - width: Val::Undefined, - height: Val::Undefined, - }; + pub const UNDEFINED: Self = Self::all(Val::Undefined); + + /// Creates a Size where both values are [`Val::Percent(100.)`]. + pub const FILL: Self = Self::all(Val::Percent(100.)); } impl Default for Size { @@ -443,14 +461,11 @@ mod tests { #[test] fn test_size_mul() { - assert_eq!( - Size::new(Val::Px(10.), Val::Px(10.)) * 2., - Size::new(Val::Px(20.), Val::Px(20.)) - ); + assert_eq!(Size::all(Val::Px(10.)) * 2., Size::all(Val::Px(20.))); - let mut size = Size::new(Val::Px(10.), Val::Px(10.)); + let mut size = Size::all(Val::Px(10.)); size *= 2.; - assert_eq!(size, Size::new(Val::Px(20.), Val::Px(20.))); + assert_eq!(size, Size::all(Val::Px(20.))); } #[test] diff --git a/examples/ui/text_layout.rs b/examples/ui/text_layout.rs index 35f741582a034..ac9710ecb8698 100644 --- a/examples/ui/text_layout.rs +++ b/examples/ui/text_layout.rs @@ -26,7 +26,7 @@ fn spawn_layout(mut commands: Commands, asset_server: Res) { .spawn(NodeBundle { style: Style { // fill the entire window - size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), + size: Size::FILL, flex_direction: FlexDirection::Column, align_items: AlignItems::Center, ..Default::default() @@ -124,7 +124,7 @@ fn spawn_child_node( flex_direction: FlexDirection::Column, align_items, justify_content, - size: Size::new(Val::Px(160.), Val::Px(160.)), + size: Size::all(Val::Px(160.)), margin: UiRect::all(MARGIN), ..Default::default() }, From 7f184c2359cd87f8ac7be990d7944d91ad43fe5f Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Feb 2023 13:38:34 +0000 Subject: [PATCH 2/4] removed the Size::FILL constant --- crates/bevy_ui/src/geometry.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 6b9954357161d..291bc5301fba6 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -386,9 +386,6 @@ impl Size { /// Creates a Size where both values are [`Val::Undefined`]. pub const UNDEFINED: Self = Self::all(Val::Undefined); - - /// Creates a Size where both values are [`Val::Percent(100.)`]. - pub const FILL: Self = Self::all(Val::Percent(100.)); } impl Default for Size { From 0de482ae125b0fa6fddbfc205f40d861f2993480 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Feb 2023 13:41:41 +0000 Subject: [PATCH 3/4] removed Size::ALL from example --- examples/ui/text_layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ui/text_layout.rs b/examples/ui/text_layout.rs index ac9710ecb8698..2514e2ddd2f78 100644 --- a/examples/ui/text_layout.rs +++ b/examples/ui/text_layout.rs @@ -26,7 +26,7 @@ fn spawn_layout(mut commands: Commands, asset_server: Res) { .spawn(NodeBundle { style: Style { // fill the entire window - size: Size::FILL, + size: Size::all(Val::Percent(100.)), flex_direction: FlexDirection::Column, align_items: AlignItems::Center, ..Default::default() From 9160795d98b5932c36ba2ab4ec4573a5c84211d3 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Feb 2023 13:41:41 +0000 Subject: [PATCH 4/4] removed Size::Fill from example --- examples/ui/text_layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ui/text_layout.rs b/examples/ui/text_layout.rs index ac9710ecb8698..2514e2ddd2f78 100644 --- a/examples/ui/text_layout.rs +++ b/examples/ui/text_layout.rs @@ -26,7 +26,7 @@ fn spawn_layout(mut commands: Commands, asset_server: Res) { .spawn(NodeBundle { style: Style { // fill the entire window - size: Size::FILL, + size: Size::all(Val::Percent(100.)), flex_direction: FlexDirection::Column, align_items: AlignItems::Center, ..Default::default()