Skip to content

Commit 5875ea7

Browse files
committed
Add additional constructors for UiRect to specify values for specific fields (#5988)
# Objective Often one wants to create a `UiRect` with a value only specifying a single field. These ways are already available, but not the most ergonomic: ```rust UiRect::new(Val::Undefined, Val::Undefined, Val::Percent(25.0), Val::Undefined) ``` ```rust UiRect { top: Val::Percent(25.0), ..default() } ``` ## Solution Introduce 6 new constructors: - `horizontal` - `vertical` - `left` - `right` - `top` - `bottom` So the above code can be written instead as: ```rust UiRect::top(Val::Percent(25.0)) ``` This solution is similar to the style fields `margin-left`, `padding-top`, etc. that you would see in CSS, from which bevy's UI has other inspiration. Therefore, it should still feel intuitive to users coming from CSS. --- ## Changelog ### Added - Additional constructors for `UiRect` to specify values for specific fields
1 parent 5b00af0 commit 5875ea7

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

crates/bevy_ui/src/geometry.rs

+128
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,134 @@ impl UiRect {
184184
bottom: value,
185185
}
186186
}
187+
188+
/// Creates a new [`UiRect`] where `left` and `right` take the given value.
189+
///
190+
/// # Example
191+
///
192+
/// ```
193+
/// # use bevy_ui::{UiRect, Val};
194+
/// #
195+
/// let ui_rect = UiRect::horizontal(Val::Px(10.0));
196+
///
197+
/// assert_eq!(ui_rect.left, Val::Px(10.0));
198+
/// assert_eq!(ui_rect.right, Val::Px(10.0));
199+
/// assert_eq!(ui_rect.top, Val::Undefined);
200+
/// assert_eq!(ui_rect.bottom, Val::Undefined);
201+
/// ```
202+
pub fn horizontal(value: Val) -> Self {
203+
UiRect {
204+
left: value,
205+
right: value,
206+
..Default::default()
207+
}
208+
}
209+
210+
/// Creates a new [`UiRect`] where `top` and `bottom` take the given value.
211+
///
212+
/// # Example
213+
///
214+
/// ```
215+
/// # use bevy_ui::{UiRect, Val};
216+
/// #
217+
/// let ui_rect = UiRect::vertical(Val::Px(10.0));
218+
///
219+
/// assert_eq!(ui_rect.left, Val::Undefined);
220+
/// assert_eq!(ui_rect.right, Val::Undefined);
221+
/// assert_eq!(ui_rect.top, Val::Px(10.0));
222+
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
223+
/// ```
224+
pub fn vertical(value: Val) -> Self {
225+
UiRect {
226+
top: value,
227+
bottom: value,
228+
..Default::default()
229+
}
230+
}
231+
232+
/// Creates a new [`UiRect`] where `left` takes the given value.
233+
///
234+
/// # Example
235+
///
236+
/// ```
237+
/// # use bevy_ui::{UiRect, Val};
238+
/// #
239+
/// let ui_rect = UiRect::left(Val::Px(10.0));
240+
///
241+
/// assert_eq!(ui_rect.left, Val::Px(10.0));
242+
/// assert_eq!(ui_rect.right, Val::Undefined);
243+
/// assert_eq!(ui_rect.top, Val::Undefined);
244+
/// assert_eq!(ui_rect.bottom, Val::Undefined);
245+
/// ```
246+
pub fn left(value: Val) -> Self {
247+
UiRect {
248+
left: value,
249+
..Default::default()
250+
}
251+
}
252+
253+
/// Creates a new [`UiRect`] where `right` takes the given value.
254+
///
255+
/// # Example
256+
///
257+
/// ```
258+
/// # use bevy_ui::{UiRect, Val};
259+
/// #
260+
/// let ui_rect = UiRect::right(Val::Px(10.0));
261+
///
262+
/// assert_eq!(ui_rect.left, Val::Undefined);
263+
/// assert_eq!(ui_rect.right, Val::Px(10.0));
264+
/// assert_eq!(ui_rect.top, Val::Undefined);
265+
/// assert_eq!(ui_rect.bottom, Val::Undefined);
266+
/// ```
267+
pub fn right(value: Val) -> Self {
268+
UiRect {
269+
right: value,
270+
..Default::default()
271+
}
272+
}
273+
274+
/// Creates a new [`UiRect`] where `top` takes the given value.
275+
///
276+
/// # Example
277+
///
278+
/// ```
279+
/// # use bevy_ui::{UiRect, Val};
280+
/// #
281+
/// let ui_rect = UiRect::top(Val::Px(10.0));
282+
///
283+
/// assert_eq!(ui_rect.left, Val::Undefined);
284+
/// assert_eq!(ui_rect.right, Val::Undefined);
285+
/// assert_eq!(ui_rect.top, Val::Px(10.0));
286+
/// assert_eq!(ui_rect.bottom, Val::Undefined);
287+
/// ```
288+
pub fn top(value: Val) -> Self {
289+
UiRect {
290+
top: value,
291+
..Default::default()
292+
}
293+
}
294+
295+
/// Creates a new [`UiRect`] where `bottom` takes the given value.
296+
///
297+
/// # Example
298+
///
299+
/// ```
300+
/// # use bevy_ui::{UiRect, Val};
301+
/// #
302+
/// let ui_rect = UiRect::bottom(Val::Px(10.0));
303+
///
304+
/// assert_eq!(ui_rect.left, Val::Undefined);
305+
/// assert_eq!(ui_rect.right, Val::Undefined);
306+
/// assert_eq!(ui_rect.top, Val::Undefined);
307+
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
308+
/// ```
309+
pub fn bottom(value: Val) -> Self {
310+
UiRect {
311+
bottom: value,
312+
..Default::default()
313+
}
314+
}
187315
}
188316

189317
/// A 2-dimensional area defined by a width and height.

0 commit comments

Comments
 (0)