Skip to content

Commit bb3bb23

Browse files
committed
Refactor LayoutTree trait (squashed)
1 parent 08e47a4 commit bb3bb23

15 files changed

+565
-573
lines changed

src/compute/flexbox.rs

Lines changed: 143 additions & 147 deletions
Large diffs are not rendered by default.

src/compute/grid/alignment.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
use super::types::GridTrack;
33
use crate::axis::InBothAbsAxis;
44
use crate::compute::common::alignment::compute_alignment_offset;
5-
use crate::compute::{GenericAlgorithm, LayoutAlgorithm};
65
use crate::geometry::{Line, Point, Rect, Size};
76
use crate::layout::{Layout, SizingMode};
87
use crate::math::MaybeMath;
9-
use crate::node::Node;
108
use crate::resolve::MaybeResolve;
119
use crate::style::{AlignContent, AlignItems, AlignSelf, AvailableSpace, Position};
1210
use crate::sys::{f32_max, f32_min};
@@ -72,17 +70,17 @@ pub(super) fn align_tracks(
7270
}
7371

7472
/// Align and size a grid item into it's final position
75-
pub(super) fn align_and_position_item(
76-
tree: &mut impl LayoutTree,
77-
node: Node,
73+
pub(super) fn align_and_position_item<Tree: LayoutTree>(
74+
tree: &mut Tree,
75+
node: Tree::ChildId,
7876
order: u32,
7977
grid_area: Rect<f32>,
8078
container_alignment_styles: InBothAbsAxis<Option<AlignItems>>,
8179
baseline_shim: f32,
8280
) {
8381
let grid_area_size = Size { width: grid_area.right - grid_area.left, height: grid_area.bottom - grid_area.top };
8482

85-
let style = tree.style(node);
83+
let style = tree.child_style(node);
8684
let aspect_ratio = style.aspect_ratio;
8785
let justify_self = style.justify_self;
8886
let align_self = style.align_self;
@@ -180,8 +178,7 @@ pub(super) fn align_and_position_item(
180178
let Size { width, height } = Size { width, height }.maybe_clamp(min_size, max_size);
181179

182180
// Layout node
183-
let measured_size_and_baselines = GenericAlgorithm::perform_layout(
184-
tree,
181+
let measured_size_and_baselines = tree.perform_child_layout(
185182
node,
186183
Size { width, height },
187184
grid_area_size.map(Option::Some),
@@ -212,7 +209,7 @@ pub(super) fn align_and_position_item(
212209
baseline_shim,
213210
);
214211

215-
*tree.layout_mut(node) = Layout { order, size: Size { width, height }, location: Point { x, y } };
212+
*tree.child_layout_mut(node) = Layout { order, size: Size { width, height }, location: Point { x, y } };
216213
}
217214

218215
/// Align and size a grid item along a single axis

src/compute/grid/mod.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::axis::{AbsoluteAxis, AbstractAxis, InBothAbsAxis};
44
use crate::geometry::{Line, Point, Rect, Size};
55
use crate::layout::{Layout, RunMode, SizeAndBaselines, SizingMode};
66
use crate::math::MaybeMath;
7-
use crate::node::Node;
87
use crate::resolve::{MaybeResolve, ResolveOrZero};
98
use crate::style::{AlignContent, AlignItems, AlignSelf, AvailableSpace, Display, Position};
109
use crate::style_helpers::*;
@@ -17,14 +16,14 @@ use placement::place_grid_items;
1716
use track_sizing::{
1817
determine_if_item_crosses_flexible_or_intrinsic_tracks, resolve_item_track_indexes, track_sizing_algorithm,
1918
};
20-
use types::{CellOccupancyMatrix, GridTrack};
19+
use types::{CellOccupancyMatrix, GridItem, GridTrack};
2120

2221
pub(crate) use types::{GridCoordinate, GridLine, OriginZeroLine};
2322

2423
#[cfg(feature = "debug")]
2524
use crate::debug::NODE_LOGGER;
2625

27-
use super::{GenericAlgorithm, LayoutAlgorithm};
26+
use super::LayoutAlgorithm;
2827

2928
mod alignment;
3029
mod explicit_grid;
@@ -35,30 +34,30 @@ mod types;
3534
mod util;
3635

3736
/// The public interface to Taffy's CSS Grid algorithm implementation
38-
pub(crate) struct CssGridAlgorithm;
37+
pub struct CssGridAlgorithm;
3938
impl LayoutAlgorithm for CssGridAlgorithm {
4039
const NAME: &'static str = "CSS GRID";
4140

41+
#[inline(always)]
4242
fn perform_layout(
4343
tree: &mut impl LayoutTree,
44-
node: Node,
4544
known_dimensions: Size<Option<f32>>,
4645
parent_size: Size<Option<f32>>,
4746
available_space: Size<AvailableSpace>,
4847
_sizing_mode: SizingMode,
4948
) -> SizeAndBaselines {
50-
compute(tree, node, known_dimensions, parent_size, available_space, RunMode::PeformLayout)
49+
compute(tree, known_dimensions, parent_size, available_space, RunMode::PeformLayout)
5150
}
5251

52+
#[inline(always)]
5353
fn measure_size(
5454
tree: &mut impl LayoutTree,
55-
node: Node,
5655
known_dimensions: Size<Option<f32>>,
5756
parent_size: Size<Option<f32>>,
5857
available_space: Size<AvailableSpace>,
5958
_sizing_mode: SizingMode,
6059
) -> Size<f32> {
61-
compute(tree, node, known_dimensions, parent_size, available_space, RunMode::ComputeSize).size
60+
compute(tree, known_dimensions, parent_size, available_space, RunMode::ComputeSize).size
6261
}
6362
}
6463

@@ -68,17 +67,16 @@ impl LayoutAlgorithm for CssGridAlgorithm {
6867
/// - Placing items (which also resolves the implicit grid)
6968
/// - Track (row/column) sizing
7069
/// - Alignment & Final item placement
71-
pub fn compute(
72-
tree: &mut impl LayoutTree,
73-
node: Node,
70+
pub fn compute<Tree: LayoutTree>(
71+
tree: &mut Tree,
7472
known_dimensions: Size<Option<f32>>,
7573
parent_size: Size<Option<f32>>,
7674
available_space: Size<AvailableSpace>,
7775
run_mode: RunMode,
7876
) -> SizeAndBaselines {
79-
let get_child_styles_iter = |node| tree.children(node).map(|child_node: &Node| tree.style(*child_node));
80-
let style = tree.style(node).clone();
81-
let child_styles_iter = get_child_styles_iter(node);
77+
let get_child_styles_iter = || tree.children().map(|child_node: Tree::ChildId| tree.child_style(child_node));
78+
let style = tree.style().clone();
79+
let child_styles_iter = get_child_styles_iter();
8280

8381
// 1. Resolve the explicit grid
8482
// Exactly compute the number of rows and columns in the explicit grid.
@@ -93,13 +91,12 @@ pub fn compute(
9391

9492
// 2. Grid Item Placement
9593
// Match items (children) to a definite grid position (row start/end and column start/end position)
96-
let mut items = Vec::with_capacity(tree.child_count(node));
94+
let mut items: Vec<GridItem<Tree>> = Vec::with_capacity(tree.child_count());
9795
let mut cell_occupancy_matrix = CellOccupancyMatrix::with_track_counts(est_col_counts, est_row_counts);
9896
let in_flow_children_iter = || {
99-
tree.children(node)
100-
.copied()
97+
tree.children()
10198
.enumerate()
102-
.map(|(index, child_node)| (index, child_node, tree.style(child_node)))
99+
.map(|(index, child_node)| (index, child_node, tree.child_style(child_node)))
103100
.filter(|(_, _, style)| style.display != Display::None && style.position != Position::Absolute)
104101
};
105102
place_grid_items(
@@ -443,21 +440,14 @@ pub fn compute(
443440

444441
// Position hidden and absolutely positioned children
445442
let mut order = items.len() as u32;
446-
(0..tree.child_count(node)).for_each(|index| {
447-
let child = tree.child(node, index);
448-
let child_style = tree.style(child);
443+
(0..tree.child_count()).for_each(|index| {
444+
let child = tree.child(index);
445+
let child_style = tree.child_style(child);
449446

450447
// Position hidden child
451448
if child_style.display == Display::None {
452-
*tree.layout_mut(node) = Layout::with_order(order);
453-
GenericAlgorithm::perform_layout(
454-
tree,
455-
child,
456-
Size::NONE,
457-
Size::NONE,
458-
Size::MAX_CONTENT,
459-
SizingMode::InherentSize,
460-
);
449+
*tree.layout_mut() = Layout::with_order(order);
450+
tree.perform_child_layout(child, Size::NONE, Size::NONE, Size::MAX_CONTENT, SizingMode::InherentSize);
461451
order += 1;
462452
return;
463453
}
@@ -526,7 +516,7 @@ pub fn compute(
526516
&first_row_items[0]
527517
};
528518

529-
let layout = tree.layout_mut(item.node);
519+
let layout = tree.child_layout_mut(item.node);
530520
layout.location.y + item.baseline.unwrap_or(layout.size.height)
531521
};
532522

src/compute/grid/placement.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ use super::types::{CellOccupancyMatrix, CellOccupancyState, GridItem};
44
use super::OriginZeroLine;
55
use crate::axis::{AbsoluteAxis, InBothAbsAxis};
66
use crate::geometry::Line;
7-
use crate::node::Node;
87
use crate::style::{AlignItems, GridAutoFlow, OriginZeroGridPlacement, Style};
98
use crate::sys::Vec;
9+
use crate::tree::LayoutTree;
1010

1111
/// 8.5. Grid Item Placement Algorithm
1212
/// Place items into the grid, generating new rows/column into the implicit grid as required
1313
///
1414
/// [Specification](https://www.w3.org/TR/css-grid-2/#auto-placement-algo)
15-
pub(super) fn place_grid_items<'a, ChildIter>(
15+
pub(super) fn place_grid_items<'a, ChildIter, Tree: LayoutTree>(
1616
cell_occupancy_matrix: &mut CellOccupancyMatrix,
17-
items: &mut Vec<GridItem>,
17+
items: &mut Vec<GridItem<Tree>>,
1818
children_iter: impl Fn() -> ChildIter,
1919
grid_auto_flow: GridAutoFlow,
2020
align_items: AlignItems,
2121
justify_items: AlignItems,
2222
) where
23-
ChildIter: Iterator<Item = (usize, Node, &'a Style)>,
23+
ChildIter: Iterator<Item = (usize, Tree::ChildId, &'a Style)>,
2424
{
2525
let primary_axis = grid_auto_flow.primary_axis();
2626
let secondary_axis = primary_axis.other_axis();
2727

2828
let map_child_style_to_origin_zero_placement = {
2929
let explicit_col_count = cell_occupancy_matrix.track_counts(AbsoluteAxis::Horizontal).explicit;
3030
let explicit_row_count = cell_occupancy_matrix.track_counts(AbsoluteAxis::Vertical).explicit;
31-
move |(index, node, style): (usize, Node, &'a Style)| -> (_, _, _, &'a Style) {
31+
move |(index, node, style): (usize, Tree::ChildId, &'a Style)| -> (_, _, _, &'a Style) {
3232
let origin_zero_placement = InBothAbsAxis {
3333
horizontal: style.grid_column.map(|placement| placement.into_origin_zero_placement(explicit_col_count)),
3434
vertical: style.grid_row.map(|placement| placement.into_origin_zero_placement(explicit_row_count)),
@@ -294,10 +294,10 @@ fn place_indefinitely_positioned_item(
294294
/// Record the grid item in both CellOccupancyMatric and the GridItems list
295295
/// once a definite placement has been determined
296296
#[allow(clippy::too_many_arguments)]
297-
fn record_grid_placement(
297+
fn record_grid_placement<Tree: LayoutTree>(
298298
cell_occupancy_matrix: &mut CellOccupancyMatrix,
299-
items: &mut Vec<GridItem>,
300-
node: Node,
299+
items: &mut Vec<GridItem<Tree>>,
300+
node: Tree::ChildId,
301301
index: usize,
302302
style: &Style,
303303
parent_align_items: AlignItems,
@@ -346,9 +346,10 @@ mod tests {
346346

347347
mod test_placement_algorithm {
348348
use crate::compute::grid::implicit_grid::compute_grid_size_estimate;
349-
use crate::compute::grid::types::TrackCounts;
349+
use crate::compute::grid::types::{GridItem, TrackCounts};
350350
use crate::compute::grid::util::*;
351351
use crate::compute::grid::CellOccupancyMatrix;
352+
use crate::node::TaffyNodeRef;
352353
use crate::prelude::*;
353354
use crate::style::GridAutoFlow;
354355
use slotmap::SlotMap;
@@ -369,7 +370,7 @@ mod tests {
369370
let children_iter = || children.iter().map(|(index, node, style, _)| (*index, *node, style));
370371
let child_styles_iter = children.iter().map(|(_, _, style, _)| style);
371372
let estimated_sizes = compute_grid_size_estimate(explicit_col_count, explicit_row_count, child_styles_iter);
372-
let mut items = Vec::new();
373+
let mut items: Vec<GridItem<TaffyNodeRef>> = Vec::new();
373374
let mut cell_occupancy_matrix =
374375
CellOccupancyMatrix::with_track_counts(estimated_sizes.0, estimated_sizes.1);
375376

0 commit comments

Comments
 (0)