From f1b9678ad0774626e69304bae1fa5f3c49296f42 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 11 Aug 2025 19:43:59 +0100 Subject: [PATCH 1/5] Add constructor functions for each variant of Val --- crates/bevy_ui/src/geometry.rs | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 7e48abe7be207..6a3679539e5d4 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -282,6 +282,54 @@ impl Val { } } +/// Returns a [`Val::Auto`] that lets the system determine the value +/// 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 { + 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 From 694b92649045619583750ad4fc62d8b937ce41fa Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 11 Aug 2025 19:51:58 +0100 Subject: [PATCH 2/5] Edit comment --- crates/bevy_ui/src/geometry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 6a3679539e5d4..d93771d0fefdc 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -282,7 +282,7 @@ impl Val { } } -/// Returns a [`Val::Auto`] that lets the system determine the value +/// 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 From ebc897940aef69f07abdf720ddaf50b9b04a817f Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 11 Aug 2025 21:48:57 +0100 Subject: [PATCH 3/5] Add a release note. --- .../constructor_functions_for_val_variants.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 release-content/release-notes/constructor_functions_for_val_variants.md diff --git a/release-content/release-notes/constructor_functions_for_val_variants.md b/release-content/release-notes/constructor_functions_for_val_variants.md new file mode 100644 index 0000000000000..5812f5bd64513 --- /dev/null +++ b/release-content/release-notes/constructor_functions_for_val_variants.md @@ -0,0 +1,9 @@ +--- +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`. From 52b8f8b419c8574bb18d839d8869b7f18dbb0897 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 11 Aug 2025 22:41:15 +0100 Subject: [PATCH 4/5] fixed formatting --- .../release-notes/constructor_functions_for_val_variants.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release-content/release-notes/constructor_functions_for_val_variants.md b/release-content/release-notes/constructor_functions_for_val_variants.md index 5812f5bd64513..279dd5f5d1d73 100644 --- a/release-content/release-notes/constructor_functions_for_val_variants.md +++ b/release-content/release-notes/constructor_functions_for_val_variants.md @@ -7,3 +7,4 @@ 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`. + From 145ecc9198f4683ed0e5942bc594a955d4555d59 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 11 Aug 2025 23:02:53 +0100 Subject: [PATCH 5/5] Fix release note formatting. --- .../release-notes/constructor_functions_for_val_variants.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/release-content/release-notes/constructor_functions_for_val_variants.md b/release-content/release-notes/constructor_functions_for_val_variants.md index 279dd5f5d1d73..01d410d677949 100644 --- a/release-content/release-notes/constructor_functions_for_val_variants.md +++ b/release-content/release-notes/constructor_functions_for_val_variants.md @@ -4,7 +4,4 @@ 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`. - +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`.