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] - Fix the Size helper functions using the wrong default value and improve the UI examples #7626

Closed
wants to merge 4 commits into from

Conversation

ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Feb 11, 2023

Objective

Size::width sets the height field to Val::DEFAULT which is Val::Undefined, but the default for Size height is Val::Auto.
Size::height has the same problem, but with the width field.

The UI examples specify numeric values in many places where they could either be elided or replaced by composition of the Flex enum properties.

related: #7468
fixes: #6498

Solution

Change Size::width so it sets height to Val::AUTO and change Size::height so it sets width to Val::AUTO.
Added some tests so this doesn't happen again.

Changelog

Changed Size::width so it sets the height to Val::AUTO.
Changed Size::height so it sets the width to Val::AUTO.
Added tests to geometry.rs for Size and UiRect to ensure correct behaviour.
Simplified the UI examples. Replaced numeric values with the Flex property enums or elided them where possible, and removed the remaining use of auto margins.

Migration Guide

The Size::width constructor function now sets the height to Val::Auto instead of Val::Undefined.
The Size::height constructor function now sets the width to Val::Auto instead of Val::Undefined.

* fixed `Size::width` and `Size::height` using `Val::DEFAULT` instead of `Val::AUTO`
* added tests to ensure correct defaults.
* Simplified the UI examples. Replaced explicit values with the flex property enums where possible, removed values that can be ellided, and removed the remaining use of auto margins.
@ickshonpe
Copy link
Contributor Author

ickshonpe commented Feb 11, 2023

Not sure whether the height/width fix needs a migration guide, as the functions are recent additions and it takes them to the natural behaviour you'd expect.

@ickshonpe ickshonpe changed the title changes: Fix the Size helper functions using the wrong default value and improve the UI examples Feb 11, 2023
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A simple quality-of-life change that makes Bevy easier to use labels Feb 11, 2023
@alice-i-cecile alice-i-cecile added this to the 0.10 milestone Feb 11, 2023
@alice-i-cecile alice-i-cecile added the C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide label Feb 11, 2023
@github-actions
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

Copy link
Contributor

@nicoburns nicoburns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of comments. Otherwise LGTM.

Comment on lines -99 to -103
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
..default()
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto margins are not the default (or should not be). For margins, "auto" means "take up available space". The default margin is zero.

Comment on lines -151 to -155
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
..default()
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto margins are not the default (or should not be). For margins, "auto" means "take up available space". The default margin is zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I couldn't work out what you meant here for a moment.
The default for margins is Px(0.) as you'd expect.
In both cases, if you look above you can see that I replaced the auto margins with an AlignItems::Center on the parent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha.

Copy link
Contributor Author

@ickshonpe ickshonpe Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No wait the default is still Val::Undefined, that fix is in the remove Val::Undefined PR and hasn't been merged yet. It's getting hard to keep track, really do need these tests.

Comment on lines +498 to +522
#[test]
fn test_size_width() {
let width = Val::Px(10.);

assert_eq!(
Size::width(width),
Size {
width,
..Default::default()
}
);
}

#[test]
fn test_size_height() {
let height = Val::Px(7.);

assert_eq!(
Size::height(height),
Size {
height,
..Default::default()
}
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me that it might be better if these tested that you get Auto rather than ..Default::default(). If the default changed this test wouldn't catch the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of these two tests is to ensure that the helper functions return the same result as setting the field directly and leaving the rest of the struct as default (whatever default it is) as a user would naturally assume.

I'll put in a separate assertion to ensure the default is Auto.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 11, 2023
@alice-i-cecile
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Feb 11, 2023
…rove the UI examples (#7626)

# Objective
`Size::width` sets the `height` field to `Val::DEFAULT` which is `Val::Undefined`, but the default for `Size` `height` is `Val::Auto`. 
`Size::height` has the same problem, but with the `width` field. 

The UI examples specify numeric values in many places where they could either be elided or replaced by composition of the Flex enum properties.

related: #7468
fixes: #6498

## Solution
Change `Size::width` so it sets `height` to `Val::AUTO` and change `Size::height` so it sets `width` to `Val::AUTO`.
Added some tests so this doesn't happen again.

## Changelog
Changed `Size::width` so it sets the `height` to `Val::AUTO`.
Changed `Size::height` so it sets the `width` to `Val::AUTO`.
Added tests to `geometry.rs` for `Size` and `UiRect` to ensure correct behaviour.
Simplified the UI examples. Replaced numeric values with the Flex property enums or elided them where possible, and removed the remaining use of auto margins.

## Migration Guide
The `Size::width` constructor function now sets the `height` to `Val::Auto` instead of `Val::Undefined`.
The `Size::height` constructor function now sets the `width` to `Val::Auto` instead of `Val::Undefined`.
@bors bors bot changed the title Fix the Size helper functions using the wrong default value and improve the UI examples [Merged by Bors] - Fix the Size helper functions using the wrong default value and improve the UI examples Feb 11, 2023
@bors bors bot closed this Feb 11, 2023
myreprise1 pushed a commit to myreprise1/bevy that referenced this pull request Feb 14, 2023
…rove the UI examples (bevyengine#7626)

# Objective
`Size::width` sets the `height` field to `Val::DEFAULT` which is `Val::Undefined`, but the default for `Size` `height` is `Val::Auto`. 
`Size::height` has the same problem, but with the `width` field. 

The UI examples specify numeric values in many places where they could either be elided or replaced by composition of the Flex enum properties.

related: bevyengine#7468
fixes: bevyengine#6498

## Solution
Change `Size::width` so it sets `height` to `Val::AUTO` and change `Size::height` so it sets `width` to `Val::AUTO`.
Added some tests so this doesn't happen again.

## Changelog
Changed `Size::width` so it sets the `height` to `Val::AUTO`.
Changed `Size::height` so it sets the `width` to `Val::AUTO`.
Added tests to `geometry.rs` for `Size` and `UiRect` to ensure correct behaviour.
Simplified the UI examples. Replaced numeric values with the Flex property enums or elided them where possible, and removed the remaining use of auto margins.

## Migration Guide
The `Size::width` constructor function now sets the `height` to `Val::Auto` instead of `Val::Undefined`.
The `Size::height` constructor function now sets the `width` to `Val::Auto` instead of `Val::Undefined`.
myreprise1 pushed a commit to myreprise1/bevy that referenced this pull request Feb 15, 2023
…rove the UI examples (bevyengine#7626)

# Objective
`Size::width` sets the `height` field to `Val::DEFAULT` which is `Val::Undefined`, but the default for `Size` `height` is `Val::Auto`. 
`Size::height` has the same problem, but with the `width` field. 

The UI examples specify numeric values in many places where they could either be elided or replaced by composition of the Flex enum properties.

related: bevyengine#7468
fixes: bevyengine#6498

## Solution
Change `Size::width` so it sets `height` to `Val::AUTO` and change `Size::height` so it sets `width` to `Val::AUTO`.
Added some tests so this doesn't happen again.

## Changelog
Changed `Size::width` so it sets the `height` to `Val::AUTO`.
Changed `Size::height` so it sets the `width` to `Val::AUTO`.
Added tests to `geometry.rs` for `Size` and `UiRect` to ensure correct behaviour.
Simplified the UI examples. Replaced numeric values with the Flex property enums or elided them where possible, and removed the remaining use of auto margins.

## Migration Guide
The `Size::width` constructor function now sets the `height` to `Val::Auto` instead of `Val::Undefined`.
The `Size::height` constructor function now sets the `width` to `Val::Auto` instead of `Val::Undefined`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide C-Bug An unexpected or incorrect behavior C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop using auto-margin to center UI elements in our examples
3 participants