Skip to content
Merged
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: 48 additions & 0 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,54 @@ impl Val {
}
}

/// Returns a [`Val::Auto`] where the value is automatically determined
/// based on the context and other [`Node`](crate::Node) properties.
pub const fn auto() -> Val {
Val::Auto
}

/// Returns a [`Val::Px`] representing a value in logical pixels.
pub const fn px(value: f32) -> Val {
Copy link
Member

Choose a reason for hiding this comment

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

The alternative to this is adding use Val::* to the prelude, which feels less "special" to me, ties the docs directly to Val, keeps our code size down, and aligns with Rust std patterns (ex: Some, None, Ok, Err, etc).

The only problem is right now bsn! breaks on this case (as it assumes it can do parent_type.value.0 to set Some(x)). This is obviously a problem! The solution is to use if let to destructure all types. I believe this will also solve one of the outstanding autocomplete issues (and it will remove the need for the touch_type hack).

Copy link
Member

Choose a reason for hiding this comment

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

I'm going to strongly advocate that we do use Val::* instead.

Copy link
Contributor

@TheBlckbird TheBlckbird Aug 12, 2025

Choose a reason for hiding this comment

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

I think it just comes down to whether we wanna allow more types besides just f32 or not #20518 (comment).
But I think a decision needs to be made and you're the one to do it. I personally like the idea of being able to omit the decimal seperator, especially because this PR is about code readability, but it is ultimately up to you.

I just think, it would be nice to ship this in 0.17 (although it isn't important enough to be blocking, as @alice-i-cecile said on discord)

Copy link
Member

Choose a reason for hiding this comment

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

Arg yeah that does make this comfier. And currently type inference actually doesn't break in bsn! right now, as function arguments don't have implicit into().

Copy link
Member

Choose a reason for hiding this comment

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

Arguably less rusty, and it means we're encouraging people to throw a bunch of "int to float" casts everywhere. But I'll admit to hitting the "oops please make it a float" friction on the regular.

Copy link
Member

Choose a reason for hiding this comment

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

It does also mean we'll likely need to special case these functions somehow in BSN asset files (if we want to support them there). But I'm content making that a problem for future us.

Consider this approval for function helpers with type conversion.

Copy link
Contributor

@TheBlckbird TheBlckbird Aug 12, 2025

Choose a reason for hiding this comment

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

Sorry to bother you one more time, but now I'm a bit confused: what do you actually mean by this? That we might add automatic type conversion in a later PR or that it will be done by BSN?

But thanks for finally merging this, this will already make things way easier than before!

Copy link
Member

Choose a reason for hiding this comment

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

It will be done by BSN. There's automatic .into() calls via the macro now.

Copy link
Member

@cart cart Aug 12, 2025

Choose a reason for hiding this comment

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

@alice-i-cecile thats not what I meant in this case (see my message above about function parameters not having implicit into). Instead, we should add a trait as mentioned elsewhere.

Val::Px(value)
}

/// Returns a [`Val::Percent`] representing a percentage of the parent node's length
/// along a specific axis.
///
/// If the UI node has no parent, the percentage is based on the window's length
/// along that axis.
///
/// Axis rules:
/// * For `flex_basis`, the percentage is relative to the main-axis length determined by the `flex_direction`.
/// * For `gap`, `min_size`, `size`, and `max_size`:
/// - `width` is relative to the parent's width.
/// - `height` is relative to the parent's height.
/// * For `margin`, `padding`, and `border` values: the percentage is relative to the parent's width.
/// * For positions, `left` and `right` are relative to the parent's width, while `bottom` and `top` are relative to the parent's height.
pub const fn percent(value: f32) -> Val {
Val::Percent(value)
}

/// Returns a [`Val::Vw`] representing a percentage of the viewport width.
pub const fn vw(value: f32) -> Val {
Val::Vw(value)
}

/// Returns a [`Val::Vh`] representing a percentage of the viewport height.
pub const fn vh(value: f32) -> Val {
Val::Vh(value)
}

/// Returns a [`Val::VMin`] representing a percentage of the viewport's smaller dimension.
pub const fn vmin(value: f32) -> Val {
Val::VMin(value)
}

/// Returns a [`Val::VMax`] representing a percentage of the viewport's larger dimension.
pub const fn vmax(value: f32) -> Val {
Val::VMax(value)
}

/// A type which is commonly used to define margins, paddings and borders.
///
/// # Examples
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: `Val` helper functions
authors: ["@Ickshonpe"]
pull_requests: [20518]
---

To make `Val`s easier to construct the following helper functions have been added: `px`, `percent`, `vw`, `vh`, `vmin` and `vmax`. Each function takes an `f32` value and returns value wrapped by its corresponding `Val` variant. There is also an `auto` helper function that maps to `Val::Auto`.
Loading