Skip to content

Commit 1ec9804

Browse files
authored
Add constructor functions for each variant of Val (#20518)
# Objective Add constructor functions for each variant of `Val` Fixes #15937 ## Solution Add constructor functions for each variant of `Val`. # Doc comments feel a bit clumsy, not sure how to rephrase them, maybe doesn't matter. Perhaps it's not necessary to repeat the enum documentation either, not sure.
1 parent 8f93d3e commit 1ec9804

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

crates/bevy_ui/src/geometry.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,54 @@ impl Val {
282282
}
283283
}
284284

285+
/// Returns a [`Val::Auto`] where the value is automatically determined
286+
/// based on the context and other [`Node`](crate::Node) properties.
287+
pub const fn auto() -> Val {
288+
Val::Auto
289+
}
290+
291+
/// Returns a [`Val::Px`] representing a value in logical pixels.
292+
pub const fn px(value: f32) -> Val {
293+
Val::Px(value)
294+
}
295+
296+
/// Returns a [`Val::Percent`] representing a percentage of the parent node's length
297+
/// along a specific axis.
298+
///
299+
/// If the UI node has no parent, the percentage is based on the window's length
300+
/// along that axis.
301+
///
302+
/// Axis rules:
303+
/// * For `flex_basis`, the percentage is relative to the main-axis length determined by the `flex_direction`.
304+
/// * For `gap`, `min_size`, `size`, and `max_size`:
305+
/// - `width` is relative to the parent's width.
306+
/// - `height` is relative to the parent's height.
307+
/// * For `margin`, `padding`, and `border` values: the percentage is relative to the parent's width.
308+
/// * For positions, `left` and `right` are relative to the parent's width, while `bottom` and `top` are relative to the parent's height.
309+
pub const fn percent(value: f32) -> Val {
310+
Val::Percent(value)
311+
}
312+
313+
/// Returns a [`Val::Vw`] representing a percentage of the viewport width.
314+
pub const fn vw(value: f32) -> Val {
315+
Val::Vw(value)
316+
}
317+
318+
/// Returns a [`Val::Vh`] representing a percentage of the viewport height.
319+
pub const fn vh(value: f32) -> Val {
320+
Val::Vh(value)
321+
}
322+
323+
/// Returns a [`Val::VMin`] representing a percentage of the viewport's smaller dimension.
324+
pub const fn vmin(value: f32) -> Val {
325+
Val::VMin(value)
326+
}
327+
328+
/// Returns a [`Val::VMax`] representing a percentage of the viewport's larger dimension.
329+
pub const fn vmax(value: f32) -> Val {
330+
Val::VMax(value)
331+
}
332+
285333
/// A type which is commonly used to define margins, paddings and borders.
286334
///
287335
/// # Examples
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: `Val` helper functions
3+
authors: ["@Ickshonpe"]
4+
pull_requests: [20518]
5+
---
6+
7+
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`.

0 commit comments

Comments
 (0)