Skip to content

Commit ff082fb

Browse files
committed
Convert LayoutTree trait to implicitly reference a single node in the tree
1 parent e62a8fc commit ff082fb

File tree

12 files changed

+264
-294
lines changed

12 files changed

+264
-294
lines changed

src/compute/flexbox.rs

Lines changed: 42 additions & 57 deletions
Large diffs are not rendered by default.

src/compute/grid/alignment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(super) fn align_and_position_item(
7878
) {
7979
let grid_area_size = Size { width: grid_area.right - grid_area.left, height: grid_area.bottom - grid_area.top };
8080

81-
let style = tree.style(node);
81+
let style = tree.child_style(node);
8282
let aspect_ratio = style.aspect_ratio;
8383
let justify_self = style.justify_self;
8484
let align_self = style.align_self;
@@ -204,7 +204,7 @@ pub(super) fn align_and_position_item(
204204
margin.vertical_components(),
205205
);
206206

207-
*tree.layout_mut(node) = Layout { order, size: Size { width, height }, location: Point { x, y } };
207+
*tree.child_layout_mut(node) = Layout { order, size: Size { width, height }, location: Point { x, y } };
208208
}
209209

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

src/compute/grid/mod.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,22 @@ impl LayoutAlgorithm for CssGridAlgorithm {
3939

4040
fn perform_layout(
4141
tree: &mut impl LayoutTree,
42-
node: Node,
4342
known_dimensions: Size<Option<f32>>,
4443
parent_size: Size<Option<f32>>,
4544
available_space: Size<AvailableSpace>,
4645
_sizing_mode: SizingMode,
4746
) -> SizeAndBaselines {
48-
compute_generic(tree, node, known_dimensions, parent_size, available_space, RunMode::PeformLayout)
47+
compute_generic(tree, known_dimensions, parent_size, available_space, RunMode::PeformLayout)
4948
}
5049

5150
fn measure_size(
5251
tree: &mut impl LayoutTree,
53-
node: Node,
5452
known_dimensions: Size<Option<f32>>,
5553
parent_size: Size<Option<f32>>,
5654
available_space: Size<AvailableSpace>,
5755
_sizing_mode: SizingMode,
5856
) -> Size<f32> {
59-
compute_generic(tree, node, known_dimensions, parent_size, available_space, RunMode::ComputeSize).size
57+
compute_generic(tree, known_dimensions, parent_size, available_space, RunMode::ComputeSize).size
6058
}
6159
}
6260

@@ -68,15 +66,14 @@ impl LayoutAlgorithm for CssGridAlgorithm {
6866
/// - Alignment & Final item placement
6967
pub fn compute_generic(
7068
tree: &mut impl LayoutTree,
71-
node: Node,
7269
known_dimensions: Size<Option<f32>>,
7370
parent_size: Size<Option<f32>>,
7471
available_space: Size<AvailableSpace>,
7572
_run_mode: RunMode,
7673
) -> SizeAndBaselines {
77-
let get_child_styles_iter = |node| tree.children(node).map(|child_node: &Node| tree.style(*child_node));
78-
let style = tree.style(node).clone();
79-
let child_styles_iter = get_child_styles_iter(node);
74+
let get_child_styles_iter = || tree.children().map(|child_node: &Node| tree.child_style(*child_node));
75+
let style = tree.style().clone();
76+
let child_styles_iter = get_child_styles_iter();
8077

8178
// 1. Resolve the explicit grid
8279
// Exactly compute the number of rows and columns in the explicit grid.
@@ -91,14 +88,14 @@ pub fn compute_generic(
9188

9289
// 2. Grid Item Placement
9390
// Match items (children) to a definite grid position (row start/end and column start/end position)
94-
let mut items = Vec::with_capacity(tree.child_count(node));
91+
let mut items = Vec::with_capacity(tree.child_count());
9592
let mut cell_occupancy_matrix = CellOccupancyMatrix::with_track_counts(est_col_counts, est_row_counts);
9693
let grid_auto_flow = style.grid_auto_flow;
9794
let in_flow_children_iter = || {
98-
tree.children(node)
95+
tree.children()
9996
.copied()
10097
.enumerate()
101-
.map(|(index, child_node)| (index, child_node, tree.style(child_node)))
98+
.map(|(index, child_node)| (index, child_node, tree.child_style(child_node)))
10299
.filter(|(_, _, style)| style.display != Display::None && style.position != Position::Absolute)
103100
};
104101
place_grid_items(&mut cell_occupancy_matrix, &mut items, in_flow_children_iter, grid_auto_flow);
@@ -312,13 +309,13 @@ pub fn compute_generic(
312309

313310
// Position hidden and absolutely positioned children
314311
let mut order = items.len() as u32;
315-
(0..tree.child_count(node)).for_each(|index| {
316-
let child = tree.child(node, index);
317-
let child_style = tree.style(child);
312+
(0..tree.child_count()).for_each(|index| {
313+
let child = tree.child(index);
314+
let child_style = tree.child_style(child);
318315

319316
// Position hidden child
320317
if child_style.display == Display::None {
321-
*tree.layout_mut(node) = Layout::with_order(order);
318+
*tree.layout_mut() = Layout::with_order(order);
322319
tree.perform_child_layout(child, Size::NONE, Size::NONE, Size::MAX_CONTENT, SizingMode::InherentSize);
323320
order += 1;
324321
return;

src/compute/grid/track_sizing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ pub(super) fn determine_if_item_crosses_flexible_tracks(
158158
/// Track sizing algorithm
159159
/// Note: Gutters are treated as empty fixed-size tracks for the purpose of the track sizing algorithm.
160160
#[allow(clippy::too_many_arguments)]
161-
pub(super) fn track_sizing_algorithm<Tree: LayoutTree>(
162-
tree: &mut Tree,
161+
pub(super) fn track_sizing_algorithm<>(
162+
tree: &mut impl LayoutTree,
163163
axis: AbstractAxis,
164164
available_space: Size<AvailableSpace>,
165165
available_grid_space: Size<AvailableSpace>,

src/compute/grid/types/grid_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl GridItem {
235235
inner_node_size: Size<Option<f32>>,
236236
) -> f32 {
237237
self.minimum_contribution_cache.unwrap_or_else(|| {
238-
let style = tree.style(self.node);
238+
let style = tree.child_style(self.node);
239239
style
240240
.size
241241
.maybe_resolve(available_space.into_options())

src/compute/leaf.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::compute::LayoutAlgorithm;
44
use crate::geometry::{Point, Size};
55
use crate::layout::{SizeAndBaselines, SizingMode};
66
use crate::math::MaybeMath;
7-
use crate::node::Node;
87
use crate::resolve::{MaybeResolve, ResolveOrZero};
98
use crate::style::AvailableSpace;
109
use crate::sys::f32_max;
@@ -20,37 +19,34 @@ impl LayoutAlgorithm for LeafAlgorithm {
2019

2120
fn perform_layout(
2221
tree: &mut impl LayoutTree,
23-
node: Node,
2422
known_dimensions: Size<Option<f32>>,
2523
parent_size: Size<Option<f32>>,
2624
available_space: Size<AvailableSpace>,
2725
sizing_mode: SizingMode,
2826
) -> SizeAndBaselines {
29-
compute_generic(tree, node, known_dimensions, parent_size, available_space, sizing_mode)
27+
compute_generic(tree, known_dimensions, parent_size, available_space, sizing_mode)
3028
}
3129

3230
fn measure_size(
3331
tree: &mut impl LayoutTree,
34-
node: Node,
3532
known_dimensions: Size<Option<f32>>,
3633
parent_size: Size<Option<f32>>,
3734
available_space: Size<AvailableSpace>,
3835
sizing_mode: SizingMode,
3936
) -> Size<f32> {
40-
compute_generic(tree, node, known_dimensions, parent_size, available_space, sizing_mode).size
37+
compute_generic(tree, known_dimensions, parent_size, available_space, sizing_mode).size
4138
}
4239
}
4340

4441
/// Compute the size of a leaf node (node with no children)
4542
pub(crate) fn compute_generic(
4643
tree: &mut impl LayoutTree,
47-
node: Node,
4844
known_dimensions: Size<Option<f32>>,
4945
parent_size: Size<Option<f32>>,
5046
available_space: Size<AvailableSpace>,
5147
sizing_mode: SizingMode,
5248
) -> SizeAndBaselines {
53-
let style = tree.style(node);
49+
let style = tree.style();
5450

5551
// Resolve node's preferred/min/max sizes (width/heights) against the available space (percentages resolve to pixel values)
5652
// For ContentSize mode, we pretend that the node has no size styles as these should be ignored.
@@ -87,7 +83,7 @@ pub(crate) fn compute_generic(
8783
return SizeAndBaselines { size, first_baselines: Point::NONE };
8884
};
8985

90-
if tree.needs_measure(node) {
86+
if tree.needs_measure() {
9187
// Compute available space
9288
let available_space = Size {
9389
width: available_space
@@ -103,7 +99,7 @@ pub(crate) fn compute_generic(
10399
};
104100

105101
// Measure node
106-
let measured_size = tree.measure_node(node, known_dimensions, available_space);
102+
let measured_size = tree.measure_node(known_dimensions, available_space);
107103

108104
let measured_size = Size {
109105
width: measured_size.width,

0 commit comments

Comments
 (0)