-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Remove Val::Undefined
#7485
Remove Val::Undefined
#7485
Conversation
* Removed the `Undefined` variant of the `Val` enum. * Changed `UiRect::default()` to set all fields to `Val::Px(0.0)`. * Added the `Inset` type that is a copy of `UiRect` but its defaults are all `Val::Auto`. * Renamed the `position` field of `Style` to `inset` and changed its type to `Inset`. * Updated the UI examples to remove or replace any use of `Val::Undefined`. * Updated the UI examples to use `Inset`.
I am on board with removing |
np, changed it to |
When I made the equivalent changes to Taffy I just removed the default values for the individual field structs (what in bevy would be position: UiRect { top: px(0.), left: px(0.), bottom: px(10.), right: px(15.) }, is much nicer than: position: Position(UiRect {
bottom: Val::Px(10.),
right: Val::Px(15.),
..Default::default() // left and top set to Px(0.), not Auto
}), It would also be possible to make the style helpers accept integers which would give you the even nicer: position: UiRect { top: px(0), left: px(0), bottom: px(10), right: px(15) }, |
Oh, that's got me thinking.. unlike all the other fields like margin or size, you almost never want to set all four position values and I've always felt it's kind of annoying having them grouped together when you normally only ever set two of the fields at once. Instead, we could just have the Like I think
is a lot more ergnomic than: Style {
position: Position {
left: Val::Px(10.),
top: Val::Px(15.),
..Default::default()
},
..Default::default()
} |
Indeed. I think my ultimate endgame is to have a builder for Style::new()
.left(Val::Px(10.))
.top(Val::Px(15.)) At which point the internal representation doesn't matter. |
and you could combine the variant helper functions into the interface, like: Style::new()
.left(10.) // `Val::Px(10.)`
.top_percent(15.); |
Or even have the chained builder functions take an |
Yeah, that one would be ideal. It would be great if you could also do thing like |
Would there be any drawbacks to just adding |
May be a bit unclear / implicit. I think it's likely worth it: I import them for my UI projects regularly. And |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a small suggestions to improve some docs, but otherwise I really like it.
The undefined value has been a source of confusion which is nice to remove. This should also bring us closer to how things are defined in the web.
I think UiRect
having a default of zero is confusing conceptually, but this is more because of the name: It doesn't represent a rectangle, but more a border around a rectangle.
Anyway, this is kind of a naming issue separate from this PR.
/// assert_eq!(ui_rect.left, Val::Px(0.)); | ||
/// assert_eq!(ui_rect.right, Val::Px(0.)); | ||
/// assert_eq!(ui_rect.top, Val::Px(0.)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub won't comment me there directly, but can we maybe add a line to the documentation to make it explicit that all other values will be zero?
So something like:
Creates a new [`UiRect`] where `bottom` takes the given value.
All other values will be zero.
And then for the other methods (e.g. top
) as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that makes sense and the same should be done for the Size
helper functions too.
…unctions set to fields with no given value.
UiRect is such a terrible name, we had a bunch of discussions spanning a couple of PRs; #7656 and #7569. My proposal is #7710 which replaces |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've little understanding of Bevy internals, but as long as UiRect
is used for margin, border, padding it should be fine (?). I have a hunch that if it was used for position, the default 0px
would give problems with absolute positioning.
Other name ideas for UiRect
: Box
(like in "box model"), Edge
. I think CSS spec calls it BoxEdge
(?). I still like Frame
the most.
Speaking of Val
"shortcuts", what about?
15.
forVal::Px(15.)
25_f32.percent()
forVal::Percent(25)
()
forVal::Auto()
Adding From
for this then would need to change Style
to accept Into
, etc., no? Maybe px()
, percent()
and auto()
would be the less intrusive, although pollutes the scope.
Margins need to be separated from borders and padding too. There's no sensible way for padding and border values to be inferred by the layout algorithm, so it makes no sense for them to have an The problem for positions is that |
There actually is a sensible interpretation of "auto" padding. Which would be that the auto padding of a container takes up a share of the same space that the auto margins of it's children do. I am considering adding opt-in support for this to Taffy, because it is similar to way in which morphorm layout works. But CSS does not allow this, so it may well not make sense to enable such support in Bevy. In any case, this wouldn't be the default value for |
Could this PR cause issues when defining the size via flex grow/shrink/basis in conjunction with I'm saying this because width/height are joined in |
Yes, it should be |
I'm relieved to see this gone. Good work, and excellent reviews. |
Objective
Val::Undefined
is unnecessary and very confusing.Currently, it's used to represent a lot of different things in Bevy depending on the context.
The details, as I understand them:
CSS doesn't have "undefined" values. Only
auto
.Undefined
isVal
's default variant.UiRect
's fields are defaultVal::Undefined
[Merged by Bors] - change the default
width
andheight
ofSize
toVal::Auto
#7475 changes the defaultwidth
andheight
ofSize
toVal::Auto
.For the
width
andheight
ofsize
inStyle
,Val::Undefined
is equivalent toVal::Px(0.)
andVal::Percent(0.)
(except with text nodes because the implementation is incomplete. With a complete implementation, they would be equivalent).
For the
width
andheight
ofmin_size
andmax_size
,Val::Undefined
is equivalent toVal::Auto
. This behaviour is different fromsize
because otherwise whenVal::Undefined
was theSize
default every node would have been constrained to a zero size by default.When
UiRect
is used to represent positions,Val::Undefined
is equivalent toVal::Auto
. Positions are either specified explicitly by the user or determined automatically by Taffy. There are no undefined positions.When
UiRect
is used to represent margins,Val::Undefined
is equivalent toVal::Px(0.)
andVal::Percent(0.)
. This difference from positions is because a position of zero still represents a position, whereas a margin of zero is no margin at all.When
UiRect
is used to represent padding or borders, bothVal::Undefined
andVal::Auto
are the same asVal::Px(0.)
(no padding or borders). Unlike with margins, there is no concept ofauto
borders or padding.The Taffy equivalent
Dimension::Undefined
has been removed in Taffy 3 without problems.related issues: #7120, #5513
Solution
Remove the
Undefined
variant ofVal
.Change the UiRect default values to
Val::Px(0)
(no margin/border/padding).Remove the
position
field fromStyle
, and replace it with separateleft
,right
,top
andbottom
fields.Set the default value for
left
,right
,top
andbottom
onStyle
toVal::Auto
.Changelog
Undefined
variant of theVal
enum.UiRect::default()
to set all fields toVal::Px(0.)
.left
,right
,top
andbottom
fields toStyle
with default values ofVal::Auto
.Migration Guide
Val::Undefined
has been removed. Bevy UI's behaviour with default values should remain the same.UiRect
's fields have been changed toVal::Px(0.)
.Style
'sposition
field has been removed. Itsleft
,right
,top
andbottom
fields have been added toStyle
directly.size
,margin
,border
, andpadding
fields ofStyle
,Val::Undefined
should be replaced withVal::Px(0.)
.min_size
,max_size
,left
,right
,top
andbottom
fields ofStyle
,Val::Undefined
should be replaced withVal::Auto