Skip to content

Commit

Permalink
Add zero, auto and points style helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Nov 28, 2022
1 parent dff7bef commit 1d62a71
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 41 deletions.
24 changes: 12 additions & 12 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ fn compute_constants(style: &Style, node_size: Size<Option<f32>>, parent_size: S
width: node_size.width.maybe_sub(padding_border.horizontal_axis_sum()),
height: node_size.height.maybe_sub(padding_border.vertical_axis_sum()),
};
let gap = style.gap.resolve_or_default(node_inner_size.or(Size { width: Some(0.0), height: Some(0.0) }));
let gap = style.gap.resolve_or_default(node_inner_size.or(Size::zero()));

let container_size = Size::ZERO;
let inner_container_size = Size::ZERO;
let container_size = Size::zero();
let inner_container_size = Size::zero();

AlgoConstants {
dir,
Expand Down Expand Up @@ -413,10 +413,10 @@ fn generate_anonymous_flex_items(tree: &impl LayoutTree, node: Node, constants:
violation: 0.0,
frozen: false,

hypothetical_inner_size: Size::ZERO,
hypothetical_outer_size: Size::ZERO,
target_size: Size::ZERO,
outer_target_size: Size::ZERO,
hypothetical_inner_size: Size::zero(),
hypothetical_outer_size: Size::zero(),
target_size: Size::zero(),
outer_target_size: Size::zero(),

baseline: 0.0,

Expand Down Expand Up @@ -1020,7 +1020,7 @@ fn calculate_children_base_lines(
&Layout {
order: tree.children(node).position(|n| *n == child.node).unwrap() as u32,
size: preliminary_size,
location: Point::ZERO,
location: Point::zero(),
},
);
}
Expand Down Expand Up @@ -1900,8 +1900,8 @@ mod tests {
};
assert_eq!(constants.node_inner_size, inner_size);

assert_eq!(constants.container_size, Size::ZERO);
assert_eq!(constants.inner_container_size, Size::ZERO);
assert_eq!(constants.container_size, Size::zero());
assert_eq!(constants.inner_container_size, Size::zero());
}

#[test]
Expand Down Expand Up @@ -1933,8 +1933,8 @@ mod tests {
// all layouts should resolve to ZERO due to the root's DISPLAY::NONE
for (node, _) in &taffy.nodes {
if let Ok(layout) = taffy.layout(node) {
assert_eq!(layout.size, Size::ZERO);
assert_eq!(layout.location, Point::ZERO);
assert_eq!(layout.size, Size::zero());
assert_eq!(layout.location, Point::zero());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Layout {
/// This means it should be rendered below all other [`Layout`]s.
#[must_use]
pub const fn new() -> Self {
Self { order: 0, size: Size::ZERO, location: Point::ZERO }
Self { order: 0, size: Size::zero(), location: Point::ZERO }
}

/// Creates a new zero-[`Layout`] with the supplied `order` value.
Expand All @@ -135,7 +135,7 @@ impl Layout {
/// The Zero-layout has size and location set to ZERO.
#[must_use]
pub const fn with_order(order: u32) -> Self {
Self { order, size: Size::ZERO, location: Point::ZERO }
Self { order, size: Size::zero(), location: Point::ZERO }
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod math;
pub mod node;
pub mod prelude;
pub mod style;
pub mod style_helpers;
pub mod tree;

#[cfg(feature = "random")]
Expand Down
48 changes: 24 additions & 24 deletions src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ mod tests {
///
/// The parent / context should not affect the outcome.
#[rstest]
#[case(Size::AUTO, Size::NONE, Size::NONE)]
#[case(Size::AUTO, Size::new(5.0, 5.0), Size::NONE)]
#[case(Size::AUTO, Size::new(-5.0, -5.0), Size::NONE)]
#[case(Size::AUTO, Size::new(0.0, 0.0), Size::NONE)]
#[case(Size::auto(), Size::NONE, Size::NONE)]
#[case(Size::auto(), Size::new(5.0, 5.0), Size::NONE)]
#[case(Size::auto(), Size::new(-5.0, -5.0), Size::NONE)]
#[case(Size::auto(), Size::new(0.0, 0.0), Size::NONE)]
fn maybe_resolve_auto(
#[case] input: Size<Dimension>,
#[case] context: Size<Option<f32>>,
Expand Down Expand Up @@ -274,10 +274,10 @@ mod tests {
use rstest::rstest;

#[rstest]
#[case(Rect::UNDEFINED, Size::NONE, Rect::ZERO)]
#[case(Rect::UNDEFINED, Size::new(5.0, 5.0), Rect::ZERO)]
#[case(Rect::UNDEFINED, Size::new(-5.0, -5.0), Rect::ZERO)]
#[case(Rect::UNDEFINED, Size::new(0.0, 0.0), Rect::ZERO)]
#[case(Rect::UNDEFINED, Size::NONE, Rect::zero())]
#[case(Rect::UNDEFINED, Size::new(5.0, 5.0), Rect::zero())]
#[case(Rect::UNDEFINED, Size::new(-5.0, -5.0), Rect::zero())]
#[case(Rect::UNDEFINED, Size::new(0.0, 0.0), Rect::zero())]
fn resolve_or_default_undefined(
#[case] input: Rect<Dimension>,
#[case] context: Size<Option<f32>>,
Expand All @@ -287,10 +287,10 @@ mod tests {
}

#[rstest]
#[case(Rect::AUTO, Size::NONE, Rect::ZERO)]
#[case(Rect::AUTO, Size::new(5.0, 5.0), Rect::ZERO)]
#[case(Rect::AUTO, Size::new(-5.0, -5.0), Rect::ZERO)]
#[case(Rect::AUTO, Size::new(0.0, 0.0), Rect::ZERO)]
#[case(Rect::auto(), Size::NONE, Rect::zero())]
#[case(Rect::auto(), Size::new(5.0, 5.0), Rect::zero())]
#[case(Rect::auto(), Size::new(-5.0, -5.0), Rect::zero())]
#[case(Rect::auto(), Size::new(0.0, 0.0), Rect::zero())]
fn resolve_or_default_auto(
#[case] input: Rect<Dimension>,
#[case] context: Size<Option<f32>>,
Expand All @@ -313,10 +313,10 @@ mod tests {
}

#[rstest]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Size::NONE, Rect::ZERO)]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Size::NONE, Rect::zero())]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Size::new(5.0, 5.0), Rect::new(25.0, 25.0, 25.0, 25.0))]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Size::new(-5.0, -5.0), Rect::new(-25.0, -25.0, -25.0, -25.0))]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Size::new(0.0, 0.0), Rect::ZERO)]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Size::new(0.0, 0.0), Rect::zero())]
fn resolve_or_default_percent(
#[case] input: Rect<Dimension>,
#[case] context: Size<Option<f32>>,
Expand All @@ -333,10 +333,10 @@ mod tests {
use rstest::rstest;

#[rstest]
#[case(Rect::UNDEFINED, None, Rect::ZERO)]
#[case(Rect::UNDEFINED, Some(5.0), Rect::ZERO)]
#[case(Rect::UNDEFINED, Some(-5.0), Rect::ZERO)]
#[case(Rect::UNDEFINED, Some(0.0), Rect::ZERO)]
#[case(Rect::UNDEFINED, None, Rect::zero())]
#[case(Rect::UNDEFINED, Some(5.0), Rect::zero())]
#[case(Rect::UNDEFINED, Some(-5.0), Rect::zero())]
#[case(Rect::UNDEFINED, Some(0.0), Rect::zero())]
fn resolve_or_default_undefined(
#[case] input: Rect<Dimension>,
#[case] context: Option<f32>,
Expand All @@ -346,10 +346,10 @@ mod tests {
}

#[rstest]
#[case(Rect::AUTO, None, Rect::ZERO)]
#[case(Rect::AUTO, Some(5.0), Rect::ZERO)]
#[case(Rect::AUTO, Some(-5.0), Rect::ZERO)]
#[case(Rect::AUTO, Some(0.0), Rect::ZERO)]
#[case(Rect::auto(), None, Rect::zero())]
#[case(Rect::auto(), Some(5.0), Rect::zero())]
#[case(Rect::auto(), Some(-5.0), Rect::zero())]
#[case(Rect::auto(), Some(0.0), Rect::zero())]
fn resolve_or_default_auto(
#[case] input: Rect<Dimension>,
#[case] context: Option<f32>,
Expand All @@ -372,10 +372,10 @@ mod tests {
}

#[rstest]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), None, Rect::ZERO)]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), None, Rect::zero())]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Some(5.0), Rect::new(25.0, 25.0, 25.0, 25.0))]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Some(-5.0), Rect::new(-25.0, -25.0, -25.0, -25.0))]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Some(0.0), Rect::ZERO)]
#[case(Rect::from_percent(5.0, 5.0, 5.0, 5.0), Some(0.0), Rect::zero())]
fn resolve_or_default_percent(
#[case] input: Rect<Dimension>,
#[case] context: Option<f32>,
Expand Down
6 changes: 3 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,9 @@ impl Style {
flex_grow: 0.0,
flex_shrink: 1.0,
flex_basis: Dimension::Auto,
size: Size::AUTO,
min_size: Size::AUTO,
max_size: Size::AUTO,
size: Size::auto(),
min_size: Size::auto(),
max_size: Size::auto(),
aspect_ratio: None,
};
}
Expand Down
168 changes: 168 additions & 0 deletions src/style_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
//! Helper functions which it make it easier to create instances of types in the `style` and `geometry` modules.
use crate::geometry::{Point, Rect, Size};
use crate::style::Dimension;

/// Returns the zero value for that type
pub const fn zero<T: TaffyZero>() -> T {
T::ZERO
}

/// Trait to abstract over zero values
pub trait TaffyZero {
/// The zero value for type implementing TaffyZero
const ZERO: Self;
}
impl TaffyZero for f32 {
const ZERO: f32 = 0.0;
}
impl TaffyZero for Dimension {
const ZERO: Dimension = Dimension::Points(0.0);
}
impl<T: TaffyZero> TaffyZero for Option<T> {
const ZERO: Option<T> = Some(T::ZERO);
}
impl<T: TaffyZero> TaffyZero for Point<T> {
const ZERO: Point<T> = Point { x: T::ZERO, y: T::ZERO };
}
impl<T: TaffyZero> Point<T> {
/// Returns a Point where both the x and y values are the zero value of the contained type
/// (e.g. 0.0, Some(0.0), or Dimension::Points(0.0))
pub const fn zero() -> Self {
zero::<Self>()
}
}
impl<T: TaffyZero> TaffyZero for Size<T> {
const ZERO: Size<T> = Size { width: T::ZERO, height: T::ZERO };
}
impl<T: TaffyZero> Size<T> {
/// Returns a Size where both the width and height values are the zero value of the contained type
/// (e.g. 0.0, Some(0.0), or Dimension::Points(0.0))
pub const fn zero() -> Self {
zero::<Self>()
}
}
impl<T: TaffyZero> TaffyZero for Rect<T> {
const ZERO: Rect<T> = Rect { left: T::ZERO, right: T::ZERO, top: T::ZERO, bottom: T::ZERO };
}
impl<T: TaffyZero> Rect<T> {
/// Returns a Size where the left, right, top, and bottom values are all the zero value of the contained type
/// (e.g. 0.0, Some(0.0), or Dimension::Points(0.0))
pub const fn zero() -> Self {
zero::<Self>()
}
}

/// Returns the auto value for that type
pub const fn auto<T: TaffyAuto>() -> T {
T::AUTO
}

/// Trait to abstract over auto values
pub trait TaffyAuto {
/// The auto value for type implementing TaffyZero
const AUTO: Self;
}
impl TaffyAuto for Dimension {
const AUTO: Dimension = Dimension::Auto;
}
impl<T: TaffyAuto> TaffyAuto for Option<T> {
const AUTO: Option<T> = Some(T::AUTO);
}
impl<T: TaffyAuto> TaffyAuto for Point<T> {
const AUTO: Point<T> = Point { x: T::AUTO, y: T::AUTO };
}
impl<T: TaffyAuto> Point<T> {
/// Returns a Point where both the x and y values are the auto value of the contained type
/// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
pub const fn auto() -> Self {
auto::<Self>()
}
}
impl<T: TaffyAuto> TaffyAuto for Size<T> {
const AUTO: Size<T> = Size { width: T::AUTO, height: T::AUTO };
}
impl<T: TaffyAuto> Size<T> {
/// Returns a Size where both the width and height values are the auto value of the contained type
/// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
pub const fn auto() -> Self {
auto::<Self>()
}
}
impl<T: TaffyAuto> TaffyAuto for Rect<T> {
const AUTO: Rect<T> = Rect { left: T::AUTO, right: T::AUTO, top: T::AUTO, bottom: T::AUTO };
}
impl<T: TaffyAuto> Rect<T> {
/// Returns a Size where the left, right, top, and bottom values are all the auto value of the contained type
/// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
pub const fn auto() -> Self {
auto::<Self>()
}
}

/// Returns a value of the inferred type which represent a constant of points
pub fn points<Input: Into<f32> + Copy, T: FromPoints>(points: Input) -> T {
T::from_points(points)
}

/// Trait to create constant points values from plain numbers
pub trait FromPoints {
/// Converts into an Into<f32> into Self
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self;
}
impl FromPoints for f32 {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
points.into()
}
}
impl FromPoints for Option<f32> {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Some(points.into())
}
}
impl FromPoints for Dimension {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Dimension::Points(points.into())
}
}
impl<T: FromPoints> FromPoints for Point<T> {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Point { x: T::from_points(points.into()), y: T::from_points(points.into()) }
}
}
impl<T: FromPoints> Point<T> {
/// Returns a Point where both the x and y values are the constant points value of the contained type
/// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
pub fn points<Input: Into<f32> + Copy>(points_value: Input) -> Self {
points::<Input, Self>(points_value)
}
}
impl<T: FromPoints> FromPoints for Size<T> {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Size { width: T::from_points(points.into()), height: T::from_points(points.into()) }
}
}
impl<T: FromPoints> Size<T> {
/// Returns a Size where both the width and height values are the constant points value of the contained type
/// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
pub fn points<Input: Into<f32> + Copy>(points_value: Input) -> Self {
points::<Input, Self>(points_value)
}
}
impl<T: FromPoints> FromPoints for Rect<T> {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Rect {
left: T::from_points(points.into()),
right: T::from_points(points.into()),
top: T::from_points(points.into()),
bottom: T::from_points(points.into()),
}
}
}
impl<T: FromPoints> Rect<T> {
/// Returns a Rect where the left, right, top and bottom values are all constant points value of the contained type
/// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
pub fn points<Input: Into<f32> + Copy>(points_value: Input) -> Self {
points::<Input, Self>(points_value)
}
}

0 comments on commit 1d62a71

Please sign in to comment.