Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add width, height and all constructor functions to Size #7468

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -360,17 +357,35 @@ 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);
}

impl Default for Size {
Expand Down Expand Up @@ -443,14 +458,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]
Expand Down
4 changes: 2 additions & 2 deletions examples/ui/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn(NodeBundle {
style: Style {
// 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,
align_items: AlignItems::Center,
..Default::default()
Expand Down Expand Up @@ -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()
},
Expand Down