Skip to content

Commit

Permalink
Cleanup/old style helpers (#293)
Browse files Browse the repository at this point in the history
* Remove top_from_points, bot_from_points, top_from_percent, and bot_from_percent methods

* Cleanup duplicate style helper methods

* Move FlexDirection style tests to the flex style module
  • Loading branch information
nicoburns authored Dec 26, 2022
1 parent 6652882 commit 0196192
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 234 deletions.
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

- Flexbox nodes sized under a min-content constraint now size correctly (#291)

### Removed

- Removed `top_from_points`, `bot_from_points`, `top_from_percent`, and `bot_from_percent` methods removed from `Rect<Dimension>`. These functions were incredibly specific for an unusual use case, so we would be surprised if anyone was using them. Please use the new style helpers instead.
- Removed `min_main_size`, `max_main_size`, `min_cross_size`, `max_cross_size`, and `cross_size` methods from `Style`. Use the more general `cross` and `main` methods directly on the `size`, `min_size`, and `max_size` properties instead.
- Removed `main_margin_start`, `main_margin_end`, `cross_margin_start`, `cross_margin_end` from `Style`. Use the more general `main_start`, `main_end`, `cross_start`, and `cross_end` on the `margin` property instead.

## 0.3.0-alpha1

This is the first in a series of planned alpha releases to allow users of Taffy to try out the new CSS Grid layout mode in advance of a stable release. We hope that by marking this is alpha release we are clearly communicating that this a pre-release and that the implementation is not yet of production quality. But we never-the-less encourage you to try it out. Feedback is welcome, and bug reports for the Grid implementation are being accepted as of this release.
Expand Down
40 changes: 22 additions & 18 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,9 @@ fn calculate_cross_size(
.map(|child| {
let child_style = tree.style(child.node);
if child.align_self == AlignSelf::Baseline
&& child_style.cross_margin_start(constants.dir) != LengthPercentageAuto::Auto
&& child_style.cross_margin_end(constants.dir) != LengthPercentageAuto::Auto
&& child_style.cross_size(constants.dir) == Dimension::Auto
&& child_style.margin.cross_start(constants.dir) != LengthPercentageAuto::Auto
&& child_style.margin.cross_end(constants.dir) != LengthPercentageAuto::Auto
&& child_style.size.cross(constants.dir) == Dimension::Auto
{
max_baseline - child.baseline + child.hypothetical_outer_size.cross(constants.dir)
} else {
Expand Down Expand Up @@ -1160,9 +1160,9 @@ fn determine_used_cross_size(tree: &mut impl LayoutTree, flex_lines: &mut [FlexL
child.target_size.set_cross(
constants.dir,
if child.align_self == AlignSelf::Stretch
&& child_style.cross_margin_start(constants.dir) != LengthPercentageAuto::Auto
&& child_style.cross_margin_end(constants.dir) != LengthPercentageAuto::Auto
&& child_style.cross_size(constants.dir) == Dimension::Auto
&& child_style.margin.cross_start(constants.dir) != LengthPercentageAuto::Auto
&& child_style.margin.cross_end(constants.dir) != LengthPercentageAuto::Auto
&& child_style.size.cross(constants.dir) == Dimension::Auto
{
(line_cross_size - child.margin.cross_axis_sum(constants.dir))
.maybe_clamp(child.min_size.cross(constants.dir), child.max_size.cross(constants.dir))
Expand Down Expand Up @@ -1205,10 +1205,10 @@ fn distribute_remaining_free_space(

for child in line.items.iter_mut() {
let child_style = tree.style(child.node);
if child_style.main_margin_start(constants.dir) == LengthPercentageAuto::Auto {
if child_style.margin.main_start(constants.dir) == LengthPercentageAuto::Auto {
num_auto_margins += 1;
}
if child_style.main_margin_end(constants.dir) == LengthPercentageAuto::Auto {
if child_style.margin.main_end(constants.dir) == LengthPercentageAuto::Auto {
num_auto_margins += 1;
}
}
Expand All @@ -1218,14 +1218,14 @@ fn distribute_remaining_free_space(

for child in line.items.iter_mut() {
let child_style = tree.style(child.node);
if child_style.main_margin_start(constants.dir) == LengthPercentageAuto::Auto {
if child_style.margin.main_start(constants.dir) == LengthPercentageAuto::Auto {
if constants.is_row {
child.margin.left = margin;
} else {
child.margin.top = margin;
}
}
if child_style.main_margin_end(constants.dir) == LengthPercentageAuto::Auto {
if child_style.margin.main_end(constants.dir) == LengthPercentageAuto::Auto {
if constants.is_row {
child.margin.right = margin;
} else {
Expand Down Expand Up @@ -1276,8 +1276,8 @@ fn resolve_cross_axis_auto_margins(tree: &mut impl LayoutTree, flex_lines: &mut
let free_space = line_cross_size - child.outer_target_size.cross(constants.dir);
let child_style = tree.style(child.node);

if child_style.cross_margin_start(constants.dir) == LengthPercentageAuto::Auto
&& child_style.cross_margin_end(constants.dir) == LengthPercentageAuto::Auto
if child_style.margin.cross_start(constants.dir) == LengthPercentageAuto::Auto
&& child_style.margin.cross_end(constants.dir) == LengthPercentageAuto::Auto
{
if constants.is_row {
child.margin.top = free_space / 2.0;
Expand All @@ -1286,13 +1286,13 @@ fn resolve_cross_axis_auto_margins(tree: &mut impl LayoutTree, flex_lines: &mut
child.margin.left = free_space / 2.0;
child.margin.right = free_space / 2.0;
}
} else if child_style.cross_margin_start(constants.dir) == LengthPercentageAuto::Auto {
} else if child_style.margin.cross_start(constants.dir) == LengthPercentageAuto::Auto {
if constants.is_row {
child.margin.top = free_space;
} else {
child.margin.left = free_space;
}
} else if child_style.cross_margin_end(constants.dir) == LengthPercentageAuto::Auto {
} else if child_style.margin.cross_end(constants.dir) == LengthPercentageAuto::Auto {
if constants.is_row {
child.margin.bottom = free_space;
} else {
Expand Down Expand Up @@ -1618,12 +1618,14 @@ fn perform_absolute_layout_on_absolute_children(tree: &mut impl LayoutTree, node
.main(constants.dir)
.maybe_max(
child_style
.min_main_size(constants.dir)
.min_size
.main(constants.dir)
.maybe_resolve(constants.node_inner_size.main(constants.dir)),
)
.maybe_min(
child_style
.max_main_size(constants.dir)
.max_size
.main(constants.dir)
.maybe_resolve(constants.node_inner_size.main(constants.dir)),
);

Expand All @@ -1632,12 +1634,14 @@ fn perform_absolute_layout_on_absolute_children(tree: &mut impl LayoutTree, node
.cross(constants.dir)
.maybe_max(
child_style
.min_cross_size(constants.dir)
.min_size
.cross(constants.dir)
.maybe_resolve(constants.node_inner_size.cross(constants.dir)),
)
.maybe_min(
child_style
.max_cross_size(constants.dir)
.max_size
.cross(constants.dir)
.maybe_resolve(constants.node_inner_size.cross(constants.dir)),
);

Expand Down
24 changes: 0 additions & 24 deletions src/style/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,6 @@ impl Dimension {
}

impl Rect<Dimension> {
/// Generates a [`Rect<Dimension>`] using [`Dimension::Points`] values for `start` and `top`
#[must_use]
pub const fn top_from_points(start: f32, top: f32) -> Rect<Dimension> {
Rect { left: Dimension::Points(start), top: Dimension::Points(top), ..Rect::AUTO }
}

/// Generates a [`Rect<Dimension>`] using [`Dimension::Points`] values for `end` and `bottom`
#[must_use]
pub const fn bot_from_points(end: f32, bottom: f32) -> Rect<Dimension> {
Rect { right: Dimension::Points(end), bottom: Dimension::Points(bottom), ..Rect::AUTO }
}

/// Generates a [`Rect<Dimension>`] using [`Dimension::Percent`] values for `start` and `top`
#[must_use]
pub const fn top_from_percent(start: f32, top: f32) -> Rect<Dimension> {
Rect { left: Dimension::Percent(start), top: Dimension::Percent(top), ..Rect::AUTO }
}

/// Generates a [`Rect<Dimension>`] using [`Dimension::Percent`] values for `end` and `bottom`
#[must_use]
pub const fn bot_from_percent(end: f32, bottom: f32) -> Rect<Dimension> {
Rect { right: Dimension::Percent(end), bottom: Dimension::Percent(bottom), ..Rect::AUTO }
}

/// Create a new Rect with [`Dimension::Points`]
#[must_use]
pub const fn from_points(start: f32, end: f32, top: f32, bottom: f32) -> Self {
Expand Down
31 changes: 31 additions & 0 deletions src/style/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,34 @@ impl FlexDirection {
matches!(self, Self::RowReverse | Self::ColumnReverse)
}
}

#[cfg(test)]
mod tests {
mod test_flex_direction {
use crate::style::*;

#[test]
fn flex_direction_is_row() {
assert_eq!(FlexDirection::Row.is_row(), true);
assert_eq!(FlexDirection::RowReverse.is_row(), true);
assert_eq!(FlexDirection::Column.is_row(), false);
assert_eq!(FlexDirection::ColumnReverse.is_row(), false);
}

#[test]
fn flex_direction_is_column() {
assert_eq!(FlexDirection::Row.is_column(), false);
assert_eq!(FlexDirection::RowReverse.is_column(), false);
assert_eq!(FlexDirection::Column.is_column(), true);
assert_eq!(FlexDirection::ColumnReverse.is_column(), true);
}

#[test]
fn flex_direction_is_reverse() {
assert_eq!(FlexDirection::Row.is_reverse(), false);
assert_eq!(FlexDirection::RowReverse.is_reverse(), true);
assert_eq!(FlexDirection::Column.is_reverse(), false);
assert_eq!(FlexDirection::ColumnReverse.is_reverse(), true);
}
}
}
192 changes: 0 additions & 192 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,90 +228,6 @@ impl Default for Style {
}
}

impl Style {
/// If the `direction` is row-oriented, the min width. Otherwise the min height
pub(crate) fn min_main_size(&self, direction: FlexDirection) -> Dimension {
if direction.is_row() {
self.min_size.width
} else {
self.min_size.height
}
}

/// If the `direction` is row-oriented, the max width. Otherwise the max height
pub(crate) fn max_main_size(&self, direction: FlexDirection) -> Dimension {
if direction.is_row() {
self.max_size.width
} else {
self.max_size.height
}
}

/// If the `direction` is row-oriented, the margin start. Otherwise the margin top
pub(crate) fn main_margin_start(&self, direction: FlexDirection) -> LengthPercentageAuto {
if direction.is_row() {
self.margin.left
} else {
self.margin.top
}
}

/// If the `direction` is row-oriented, the margin end. Otherwise the margin bottom
pub(crate) fn main_margin_end(&self, direction: FlexDirection) -> LengthPercentageAuto {
if direction.is_row() {
self.margin.right
} else {
self.margin.bottom
}
}

/// If the `direction` is row-oriented, the height. Otherwise the width
pub(crate) fn cross_size(&self, direction: FlexDirection) -> Dimension {
if direction.is_row() {
self.size.height
} else {
self.size.width
}
}

/// If the `direction` is row-oriented, the min height. Otherwise the min width
pub(crate) fn min_cross_size(&self, direction: FlexDirection) -> Dimension {
if direction.is_row() {
self.min_size.height
} else {
self.min_size.width
}
}

/// If the `direction` is row-oriented, the max height. Otherwise the max width
pub(crate) fn max_cross_size(&self, direction: FlexDirection) -> Dimension {
if direction.is_row() {
self.max_size.height
} else {
self.max_size.width
}
}

/// If the `direction` is row-oriented, the margin top. Otherwise the margin start
pub(crate) fn cross_margin_start(&self, direction: FlexDirection) -> LengthPercentageAuto {
if direction.is_row() {
self.margin.top
} else {
self.margin.left
}
}

/// If the `direction` is row-oriented, the margin bottom. Otherwise the margin end
pub(crate) fn cross_margin_end(&self, direction: FlexDirection) -> LengthPercentageAuto {
if direction.is_row() {
self.margin.bottom
} else {
self.margin.right
}
}
}

#[allow(clippy::bool_assert_comparison)]
#[cfg(test)]
mod tests {
use super::Style;
Expand Down Expand Up @@ -365,112 +281,4 @@ mod tests {
assert_eq!(Style::DEFAULT, Style::default());
assert_eq!(Style::DEFAULT, old_defaults);
}

mod test_flex_direction {
use crate::style::*;

#[test]
fn flex_direction_is_row() {
assert_eq!(FlexDirection::Row.is_row(), true);
assert_eq!(FlexDirection::RowReverse.is_row(), true);
assert_eq!(FlexDirection::Column.is_row(), false);
assert_eq!(FlexDirection::ColumnReverse.is_row(), false);
}

#[test]
fn flex_direction_is_column() {
assert_eq!(FlexDirection::Row.is_column(), false);
assert_eq!(FlexDirection::RowReverse.is_column(), false);
assert_eq!(FlexDirection::Column.is_column(), true);
assert_eq!(FlexDirection::ColumnReverse.is_column(), true);
}

#[test]
fn flex_direction_is_reverse() {
assert_eq!(FlexDirection::Row.is_reverse(), false);
assert_eq!(FlexDirection::RowReverse.is_reverse(), true);
assert_eq!(FlexDirection::Column.is_reverse(), false);
assert_eq!(FlexDirection::ColumnReverse.is_reverse(), true);
}
}

mod test_flexbox_layout {
use crate::style::*;
use crate::style_helpers::*;

#[test]
fn flexbox_layout_min_main_size() {
let layout = Style { min_size: Size::from_points(1.0, 2.0), ..Default::default() };
assert_eq!(layout.min_main_size(FlexDirection::Row), Dimension::Points(1.0));
assert_eq!(layout.min_main_size(FlexDirection::Column), Dimension::Points(2.0));
}

#[test]
fn flexbox_layout_max_main_size() {
let layout = Style { max_size: Size::from_points(1.0, 2.0), ..Default::default() };
assert_eq!(layout.max_main_size(FlexDirection::Row), Dimension::Points(1.0));
assert_eq!(layout.max_main_size(FlexDirection::Column), Dimension::Points(2.0));
}

#[test]
fn flexbox_layout_main_margin_start() {
let layout = Style {
margin: Rect { top: points(1.0), bottom: auto(), left: points(2.0), right: auto() },
..Default::default()
};
assert_eq!(layout.main_margin_start(FlexDirection::Row), points(2.0));
assert_eq!(layout.main_margin_start(FlexDirection::Column), points(1.0));
}

#[test]
fn flexbox_layout_main_margin_end() {
let layout = Style {
margin: Rect { top: auto(), bottom: points(1.0), left: auto(), right: points(2.0) },
..Default::default()
};
assert_eq!(layout.main_margin_end(FlexDirection::Row), points(2.0));
assert_eq!(layout.main_margin_end(FlexDirection::Column), points(1.0));
}

#[test]
fn flexbox_layout_cross_size() {
let layout = Style { size: Size::from_points(1.0, 2.0), ..Default::default() };
assert_eq!(layout.cross_size(FlexDirection::Row), Dimension::Points(2.0));
assert_eq!(layout.cross_size(FlexDirection::Column), Dimension::Points(1.0));
}

#[test]
fn flexbox_layout_min_cross_size() {
let layout = Style { min_size: Size::from_points(1.0, 2.0), ..Default::default() };
assert_eq!(layout.min_cross_size(FlexDirection::Row), Dimension::Points(2.0));
assert_eq!(layout.min_cross_size(FlexDirection::Column), Dimension::Points(1.0));
}

#[test]
fn flexbox_layout_max_cross_size() {
let layout = Style { max_size: Size::from_points(1.0, 2.0), ..Default::default() };
assert_eq!(layout.max_cross_size(FlexDirection::Row), Dimension::Points(2.0));
assert_eq!(layout.max_cross_size(FlexDirection::Column), Dimension::Points(1.0));
}

#[test]
fn flexbox_layout_cross_margin_start() {
let layout = Style {
margin: Rect { top: points(1.0), bottom: auto(), left: points(2.0), right: auto() },
..Default::default()
};
assert_eq!(layout.cross_margin_start(FlexDirection::Row), points(1.0));
assert_eq!(layout.cross_margin_start(FlexDirection::Column), points(2.0));
}

#[test]
fn flexbox_layout_cross_margin_end() {
let layout = Style {
margin: Rect { top: auto(), bottom: points(1.0), left: auto(), right: points(2.0) },
..Default::default()
};
assert_eq!(layout.cross_margin_end(FlexDirection::Row), points(1.0));
assert_eq!(layout.cross_margin_end(FlexDirection::Column), points(2.0));
}
}
}

0 comments on commit 0196192

Please sign in to comment.