Add width, height and all constructor functions to Size (#7468)

## Objective

A common easy to miss mistake is to write something like:
``` rust
Size::new(Val::Percent(100.), Val::Px(100.));
```

`UiRect` has the `left`, `right`, `all`, `vertical`, etc constructor functions, `Size` is used a lot more frequently but lacks anything similar.

## Solution

Implement `all`, `width` and `height` functions for `Size`.

## Changelog

* Added `all`, `width` and `height` functions to `Size`.
This commit is contained in:
ickshonpe 2023-02-02 14:29:39 +00:00
parent e5b522064c
commit fbd569c791
2 changed files with 32 additions and 20 deletions

View File

@ -339,10 +339,7 @@ pub struct Size {
} }
impl Size { impl Size {
pub const DEFAULT: Self = Self { pub const DEFAULT: Self = Self::all(Val::DEFAULT);
width: Val::DEFAULT,
height: Val::DEFAULT,
};
/// Creates a new [`Size`] from a width and a height. /// Creates a new [`Size`] from a width and a height.
/// ///
@ -360,17 +357,35 @@ impl Size {
Size { width, height } 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`]. /// Creates a Size where both values are [`Val::Auto`].
pub const AUTO: Size = Size { pub const AUTO: Self = Self::all(Val::Auto);
width: Val::Auto,
height: Val::Auto,
};
/// Creates a Size where both values are [`Val::Undefined`]. /// Creates a Size where both values are [`Val::Undefined`].
pub const UNDEFINED: Size = Size { pub const UNDEFINED: Self = Self::all(Val::Undefined);
width: Val::Undefined,
height: Val::Undefined,
};
} }
impl Default for Size { impl Default for Size {
@ -443,14 +458,11 @@ mod tests {
#[test] #[test]
fn test_size_mul() { fn test_size_mul() {
assert_eq!( assert_eq!(Size::all(Val::Px(10.)) * 2., Size::all(Val::Px(20.)));
Size::new(Val::Px(10.), Val::Px(10.)) * 2.,
Size::new(Val::Px(20.), Val::Px(20.))
);
let mut size = Size::new(Val::Px(10.), Val::Px(10.)); let mut size = Size::all(Val::Px(10.));
size *= 2.; size *= 2.;
assert_eq!(size, Size::new(Val::Px(20.), Val::Px(20.))); assert_eq!(size, Size::all(Val::Px(20.)));
} }
#[test] #[test]

View File

@ -26,7 +26,7 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn(NodeBundle { .spawn(NodeBundle {
style: Style { style: Style {
// fill the entire window // fill the entire window
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), size: Size::all(Val::Percent(100.)),
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
align_items: AlignItems::Center, align_items: AlignItems::Center,
..Default::default() ..Default::default()
@ -124,7 +124,7 @@ fn spawn_child_node(
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
align_items, align_items,
justify_content, justify_content,
size: Size::new(Val::Px(160.), Val::Px(160.)), size: Size::all(Val::Px(160.)),
margin: UiRect::all(MARGIN), margin: UiRect::all(MARGIN),
..Default::default() ..Default::default()
}, },