Skip to content

Commit 08bf1a6

Browse files
nicoburnsickshonpe
andauthored
Flatten UI Style properties that use Size + remove Size (#8548)
# Objective - Simplify API and make authoring styles easier See: #8540 (comment) ## Solution - The `size`, `min_size`, `max_size`, and `gap` properties have been replaced by `width`, `height`, `min_width`, `min_height`, `max_width`, `max_height`, `row_gap`, and `column_gap` properties --- ## Changelog - Flattened `Style` properties that have a `Size` value directly into `Style` ## Migration Guide - The `size`, `min_size`, `max_size`, and `gap` properties have been replaced by the `width`, `height`, `min_width`, `min_height`, `max_width`, `max_height`, `row_gap`, and `column_gap` properties. Use the new properties instead. --------- Co-authored-by: ickshonpe <[email protected]>
1 parent 17f045e commit 08bf1a6

25 files changed

+199
-384
lines changed

crates/bevy_ui/src/geometry.rs

Lines changed: 0 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::Val;
22
use bevy_reflect::{FromReflect, Reflect, ReflectFromReflect};
3-
use std::ops::{Div, DivAssign, Mul, MulAssign};
43

54
/// A type which is commonly used to define margins, paddings and borders.
65
///
@@ -282,152 +281,6 @@ impl Default for UiRect {
282281
}
283282
}
284283

285-
/// A 2-dimensional area defined by a width and height.
286-
///
287-
/// It is commonly used to define the size of a text or UI element.
288-
#[derive(Copy, Clone, PartialEq, Debug, Reflect, FromReflect)]
289-
#[reflect(FromReflect, PartialEq)]
290-
pub struct Size {
291-
/// The width of the 2-dimensional area.
292-
pub width: Val,
293-
/// The height of the 2-dimensional area.
294-
pub height: Val,
295-
}
296-
297-
impl Size {
298-
pub const DEFAULT: Self = Self::AUTO;
299-
300-
/// Creates a new [`Size`] from a width and a height.
301-
///
302-
/// # Example
303-
///
304-
/// ```
305-
/// # use bevy_ui::{Size, Val};
306-
/// #
307-
/// let size = Size::new(Val::Px(100.0), Val::Px(200.0));
308-
///
309-
/// assert_eq!(size.width, Val::Px(100.0));
310-
/// assert_eq!(size.height, Val::Px(200.0));
311-
/// ```
312-
pub const fn new(width: Val, height: Val) -> Self {
313-
Size { width, height }
314-
}
315-
316-
/// Creates a new [`Size`] where both sides take the given value.
317-
///
318-
/// # Example
319-
///
320-
/// ```
321-
/// # use bevy_ui::{Size, Val};
322-
/// #
323-
/// let size = Size::all(Val::Px(10.));
324-
///
325-
/// assert_eq!(size.width, Val::Px(10.0));
326-
/// assert_eq!(size.height, Val::Px(10.0));
327-
/// ```
328-
pub const fn all(value: Val) -> Self {
329-
Self {
330-
width: value,
331-
height: value,
332-
}
333-
}
334-
335-
/// Creates a new [`Size`] where `width` takes the given value,
336-
/// and `height` is set to [`Val::Auto`].
337-
338-
///
339-
/// # Example
340-
///
341-
/// ```
342-
/// # use bevy_ui::{Size, Val};
343-
/// #
344-
/// let size = Size::width(Val::Px(10.));
345-
///
346-
/// assert_eq!(size.width, Val::Px(10.0));
347-
/// assert_eq!(size.height, Val::Auto);
348-
/// ```
349-
pub const fn width(width: Val) -> Self {
350-
Self {
351-
width,
352-
height: Val::Auto,
353-
}
354-
}
355-
356-
/// Creates a new [`Size`] where `height` takes the given value,
357-
/// and `width` is set to [`Val::Auto`].
358-
///
359-
/// # Example
360-
///
361-
/// ```
362-
/// # use bevy_ui::{Size, Val};
363-
/// #
364-
/// let size = Size::height(Val::Px(10.));
365-
///
366-
/// assert_eq!(size.width, Val::Auto);
367-
/// assert_eq!(size.height, Val::Px(10.));
368-
/// ```
369-
pub const fn height(height: Val) -> Self {
370-
Self {
371-
width: Val::Auto,
372-
height,
373-
}
374-
}
375-
376-
/// Creates a Size where both values are [`Val::Auto`].
377-
pub const AUTO: Self = Self::all(Val::Auto);
378-
}
379-
380-
impl Default for Size {
381-
fn default() -> Self {
382-
Self::DEFAULT
383-
}
384-
}
385-
386-
impl From<(Val, Val)> for Size {
387-
fn from(vals: (Val, Val)) -> Self {
388-
Self {
389-
width: vals.0,
390-
height: vals.1,
391-
}
392-
}
393-
}
394-
395-
impl Mul<f32> for Size {
396-
type Output = Size;
397-
398-
fn mul(self, rhs: f32) -> Self::Output {
399-
Self::Output {
400-
width: self.width * rhs,
401-
height: self.height * rhs,
402-
}
403-
}
404-
}
405-
406-
impl MulAssign<f32> for Size {
407-
fn mul_assign(&mut self, rhs: f32) {
408-
self.width *= rhs;
409-
self.height *= rhs;
410-
}
411-
}
412-
413-
impl Div<f32> for Size {
414-
type Output = Size;
415-
416-
fn div(self, rhs: f32) -> Self::Output {
417-
Self::Output {
418-
width: self.width / rhs,
419-
height: self.height / rhs,
420-
}
421-
}
422-
}
423-
424-
impl DivAssign<f32> for Size {
425-
fn div_assign(&mut self, rhs: f32) {
426-
self.width /= rhs;
427-
self.height /= rhs;
428-
}
429-
}
430-
431284
#[cfg(test)]
432285
mod tests {
433286
use super::*;
@@ -446,91 +299,6 @@ mod tests {
446299
assert_eq!(UiRect::default(), UiRect::DEFAULT);
447300
}
448301

449-
#[test]
450-
fn test_size_from() {
451-
let size: Size = (Val::Px(20.), Val::Px(30.)).into();
452-
453-
assert_eq!(
454-
size,
455-
Size {
456-
width: Val::Px(20.),
457-
height: Val::Px(30.),
458-
}
459-
);
460-
}
461-
462-
#[test]
463-
fn test_size_mul() {
464-
assert_eq!(Size::all(Val::Px(10.)) * 2., Size::all(Val::Px(20.)));
465-
466-
let mut size = Size::all(Val::Px(10.));
467-
size *= 2.;
468-
assert_eq!(size, Size::all(Val::Px(20.)));
469-
}
470-
471-
#[test]
472-
fn test_size_div() {
473-
assert_eq!(
474-
Size::new(Val::Px(20.), Val::Px(20.)) / 2.,
475-
Size::new(Val::Px(10.), Val::Px(10.))
476-
);
477-
478-
let mut size = Size::new(Val::Px(20.), Val::Px(20.));
479-
size /= 2.;
480-
assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.)));
481-
}
482-
483-
#[test]
484-
fn test_size_all() {
485-
let length = Val::Px(10.);
486-
487-
assert_eq!(
488-
Size::all(length),
489-
Size {
490-
width: length,
491-
height: length
492-
}
493-
);
494-
}
495-
496-
#[test]
497-
fn test_size_width() {
498-
let width = Val::Px(10.);
499-
500-
assert_eq!(
501-
Size::width(width),
502-
Size {
503-
width,
504-
..Default::default()
505-
}
506-
);
507-
}
508-
509-
#[test]
510-
fn test_size_height() {
511-
let height = Val::Px(7.);
512-
513-
assert_eq!(
514-
Size::height(height),
515-
Size {
516-
height,
517-
..Default::default()
518-
}
519-
);
520-
}
521-
522-
#[test]
523-
fn size_default_equals_const_default() {
524-
assert_eq!(
525-
Size::default(),
526-
Size {
527-
width: Val::Auto,
528-
height: Val::Auto
529-
}
530-
);
531-
assert_eq!(Size::default(), Size::DEFAULT);
532-
}
533-
534302
#[test]
535303
fn test_uirect_axes() {
536304
let x = Val::Px(1.);

crates/bevy_ui/src/layout/convert.rs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use taffy::style_helpers;
33
use crate::{
44
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, GridAutoFlow,
55
GridPlacement, GridTrack, GridTrackRepetition, JustifyContent, JustifyItems, JustifySelf,
6-
MaxTrackSizingFunction, MinTrackSizingFunction, PositionType, RepeatedGridTrack, Size, Style,
7-
UiRect, Val,
6+
MaxTrackSizingFunction, MinTrackSizingFunction, PositionType, RepeatedGridTrack, Style, UiRect,
7+
Val,
88
};
99

1010
use super::LayoutContext;
@@ -63,15 +63,6 @@ impl UiRect {
6363
}
6464
}
6565

66-
impl Size {
67-
fn map_to_taffy_size<T>(self, map_fn: impl Fn(Val) -> T) -> taffy::geometry::Size<T> {
68-
taffy::geometry::Size {
69-
width: map_fn(self.width),
70-
height: map_fn(self.height),
71-
}
72-
}
73-
}
74-
7566
pub fn from_style(context: &LayoutContext, style: &Style) -> taffy::style::Style {
7667
taffy::style::Style {
7768
display: style.display.into(),
@@ -102,17 +93,23 @@ pub fn from_style(context: &LayoutContext, style: &Style) -> taffy::style::Style
10293
flex_grow: style.flex_grow,
10394
flex_shrink: style.flex_shrink,
10495
flex_basis: style.flex_basis.into_dimension(context),
105-
size: style.size.map_to_taffy_size(|s| s.into_dimension(context)),
106-
min_size: style
107-
.min_size
108-
.map_to_taffy_size(|s| s.into_dimension(context)),
109-
max_size: style
110-
.max_size
111-
.map_to_taffy_size(|s| s.into_dimension(context)),
96+
size: taffy::prelude::Size {
97+
width: style.width.into_dimension(context),
98+
height: style.height.into_dimension(context),
99+
},
100+
min_size: taffy::prelude::Size {
101+
width: style.min_width.into_dimension(context),
102+
height: style.min_height.into_dimension(context),
103+
},
104+
max_size: taffy::prelude::Size {
105+
width: style.max_width.into_dimension(context),
106+
height: style.max_height.into_dimension(context),
107+
},
112108
aspect_ratio: style.aspect_ratio,
113-
gap: style
114-
.gap
115-
.map_to_taffy_size(|s| s.into_length_percentage(context)),
109+
gap: taffy::prelude::Size {
110+
width: style.column_gap.into_length_percentage(context),
111+
height: style.row_gap.into_length_percentage(context),
112+
},
116113
grid_auto_flow: style.grid_auto_flow.into(),
117114
grid_template_rows: style
118115
.grid_template_rows
@@ -439,24 +436,16 @@ mod tests {
439436
flex_grow: 1.,
440437
flex_shrink: 0.,
441438
flex_basis: Val::Px(0.),
442-
size: Size {
443-
width: Val::Px(0.),
444-
height: Val::Auto,
445-
},
446-
min_size: Size {
447-
width: Val::Px(0.),
448-
height: Val::Percent(0.),
449-
},
450-
max_size: Size {
451-
width: Val::Auto,
452-
height: Val::Px(0.),
453-
},
439+
width: Val::Px(0.),
440+
height: Val::Auto,
441+
min_width: Val::Px(0.),
442+
min_height: Val::Percent(0.),
443+
max_width: Val::Auto,
444+
max_height: Val::Px(0.),
454445
aspect_ratio: None,
455446
overflow: crate::Overflow::clip(),
456-
gap: Size {
457-
width: Val::Px(0.),
458-
height: Val::Percent(0.),
459-
},
447+
column_gap: Val::Px(0.),
448+
row_gap: Val::Percent(0.),
460449
grid_auto_flow: GridAutoFlow::ColumnDense,
461450
grid_template_rows: vec![
462451
GridTrack::px(10.0),

crates/bevy_ui/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ impl Plugin for UiPlugin {
109109
.register_type::<Overflow>()
110110
.register_type::<OverflowAxis>()
111111
.register_type::<PositionType>()
112-
.register_type::<Size>()
113112
.register_type::<UiRect>()
114113
.register_type::<Style>()
115114
.register_type::<BackgroundColor>()

0 commit comments

Comments
 (0)