Skip to content

Commit daa07e0

Browse files
authored
Remove LayoutAlgorithm trait (#562)
* Rename SizeBaselinesAndMargins to LayoutOutput * Remove LayoutAlgorithm trait + add LayoutInput struct * Implement fmt::Display for style::Display + use in taffy_tree compute function * Update release notes
1 parent 67f286f commit daa07e0

File tree

13 files changed

+166
-501
lines changed

13 files changed

+166
-501
lines changed

RELEASES.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,16 @@ Example usage change:
8787
### Added
8888

8989
- Support for [CSS Block layout](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flow_Layout/Block_and_Inline_Layout_in_Normal_Flow#elements_participating_in_a_block_formatting_context) has been added. This can be used via the new `Display::Block` variant of the `Display` enum. Note that inline, inline-block and float have *not* been implemented. The use case supported is block container nodes which contain block-level children.
90+
- Support for running each layout algorithm individually on a single node via the following top-level functions:
91+
- `compute_flexbox_layout`
92+
- `compute_grid_layout`
93+
- `compute_leaf_layout`
94+
- `compute_hidden_layout`
9095
- Added `insert_child_at_index()` method to the `Taffy` tree. This can be used to insert a child node at any position instead of just the end.
9196

9297
### Removed
9398

94-
- `layout_flexbox()` has been removed from the prelude. Use `FlexboxAlgorithm::perform_layout()` instead.
99+
- `layout_flexbox()` has been removed from the prelude. Use `taffy::compute_flexbox_layout` instead.
95100
- The following methods have been removed from the `LayoutTree` trait: `parent`, `is_childless`, `layout`, `measure_node`, `needs_measure`, `cache_mut` and `mark_dirty`. These no longer need to be implemented in custom implementations of `LayoutTree`.
96101

97102
### Changes

src/compute/block.rs

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,15 @@
11
//! Computes the CSS block layout algorithm in the case that the block container being laid out contains only block-level boxes
2-
use crate::compute::LayoutAlgorithm;
32
use crate::geometry::{Line, Point, Rect, Size};
43
use crate::style::{AvailableSpace, Display, LengthPercentageAuto, Overflow, Position};
54
use crate::style_helpers::TaffyMaxContent;
6-
use crate::tree::{CollapsibleMarginSet, Layout, RunMode, SizeBaselinesAndMargins, SizingMode};
5+
use crate::tree::{CollapsibleMarginSet, Layout, LayoutInput, LayoutOutput, RunMode, SizingMode};
76
use crate::tree::{LayoutTree, NodeId};
87
use crate::util::debug::debug_log;
98
use crate::util::sys::f32_max;
109
use crate::util::sys::Vec;
1110
use crate::util::MaybeMath;
1211
use crate::util::{MaybeResolve, ResolveOrZero};
1312

14-
/// The public interface to Taffy's Block algorithm implementation
15-
pub struct BlockAlgorithm;
16-
impl LayoutAlgorithm for BlockAlgorithm {
17-
const NAME: &'static str = "BLOCK";
18-
19-
fn perform_layout(
20-
tree: &mut impl LayoutTree,
21-
node: NodeId,
22-
known_dimensions: Size<Option<f32>>,
23-
parent_size: Size<Option<f32>>,
24-
available_space: Size<AvailableSpace>,
25-
_sizing_mode: SizingMode,
26-
vertical_margins_are_collapsible: Line<bool>,
27-
) -> SizeBaselinesAndMargins {
28-
compute(
29-
tree,
30-
node,
31-
known_dimensions,
32-
parent_size,
33-
available_space,
34-
RunMode::PerformLayout,
35-
vertical_margins_are_collapsible,
36-
)
37-
}
38-
39-
fn measure_size(
40-
tree: &mut impl LayoutTree,
41-
node: NodeId,
42-
known_dimensions: Size<Option<f32>>,
43-
parent_size: Size<Option<f32>>,
44-
available_space: Size<AvailableSpace>,
45-
_sizing_mode: SizingMode,
46-
vertical_margins_are_collapsible: Line<bool>,
47-
) -> Size<f32> {
48-
compute(
49-
tree,
50-
node,
51-
known_dimensions,
52-
parent_size,
53-
available_space,
54-
RunMode::ComputeSize,
55-
vertical_margins_are_collapsible,
56-
)
57-
.size
58-
}
59-
}
60-
6113
/// Per-child data that is accumulated and modified over the course of the layout algorithm
6214
struct BlockItem {
6315
/// The identifier for the associated node
@@ -93,15 +45,8 @@ struct BlockItem {
9345
}
9446

9547
/// Computes the layout of [`LayoutTree`] according to the block layout algorithm
96-
pub fn compute(
97-
tree: &mut impl LayoutTree,
98-
node_id: NodeId,
99-
known_dimensions: Size<Option<f32>>,
100-
parent_size: Size<Option<f32>>,
101-
available_space: Size<AvailableSpace>,
102-
run_mode: RunMode,
103-
vertical_margins_are_collapsible: Line<bool>,
104-
) -> SizeBaselinesAndMargins {
48+
pub fn compute_block_layout(tree: &mut impl LayoutTree, node_id: NodeId, inputs: LayoutInput) -> LayoutOutput {
49+
let LayoutInput { known_dimensions, parent_size, available_space, run_mode, .. } = inputs;
10550
let style = tree.style(node_id);
10651

10752
// Pull these out earlier to avoid borrowing issues
@@ -140,27 +85,15 @@ pub fn compute(
14085
}
14186

14287
debug_log!("BLOCK");
143-
compute_inner(
144-
tree,
145-
node_id,
146-
styled_based_known_dimensions,
147-
parent_size,
148-
available_space,
149-
run_mode,
150-
vertical_margins_are_collapsible,
151-
)
88+
compute_inner(tree, node_id, LayoutInput { known_dimensions: styled_based_known_dimensions, ..inputs })
15289
}
15390

15491
/// Computes the layout of [`LayoutTree`] according to the block layout algorithm
155-
fn compute_inner(
156-
tree: &mut impl LayoutTree,
157-
node_id: NodeId,
158-
known_dimensions: Size<Option<f32>>,
159-
parent_size: Size<Option<f32>>,
160-
available_space: Size<AvailableSpace>,
161-
run_mode: RunMode,
162-
vertical_margins_are_collapsible: Line<bool>,
163-
) -> SizeBaselinesAndMargins {
92+
fn compute_inner(tree: &mut impl LayoutTree, node_id: NodeId, inputs: LayoutInput) -> LayoutOutput {
93+
let LayoutInput {
94+
known_dimensions, parent_size, available_space, run_mode, vertical_margins_are_collapsible, ..
95+
} = inputs;
96+
16497
let style = tree.style(node_id);
16598
let raw_padding = style.padding;
16699
let raw_border = style.border;
@@ -282,7 +215,7 @@ fn compute_inner(
282215
let can_be_collapsed_through =
283216
!has_styles_preventing_being_collapsed_through && all_in_flow_children_can_be_collapsed_through;
284217

285-
SizeBaselinesAndMargins {
218+
LayoutOutput {
286219
size: final_outer_size,
287220
first_baselines: Point::NONE,
288221
top_margin: if own_margins_collapse_with_children.start {

src/compute/flexbox.rs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,21 @@
22
use core::f32;
33

44
use crate::compute::common::alignment::compute_alignment_offset;
5-
use crate::compute::LayoutAlgorithm;
65
use crate::geometry::{Line, Point, Rect, Size};
76
use crate::prelude::{TaffyMaxContent, TaffyMinContent};
87
use crate::style::{
98
AlignContent, AlignItems, AlignSelf, AvailableSpace, Dimension, Display, FlexWrap, JustifyContent,
109
LengthPercentageAuto, Overflow, Position,
1110
};
1211
use crate::style::{FlexDirection, Style};
13-
use crate::tree::{Layout, RunMode, SizeBaselinesAndMargins, SizingMode};
12+
use crate::tree::{Layout, LayoutInput, LayoutOutput, RunMode, SizingMode};
1413
use crate::tree::{LayoutTree, NodeId};
1514
use crate::util::debug::debug_log;
1615
use crate::util::sys::Vec;
1716
use crate::util::sys::{f32_max, new_vec_with_capacity};
1817
use crate::util::MaybeMath;
1918
use crate::util::{MaybeResolve, ResolveOrZero};
2019

21-
/// The public interface to Taffy's Flexbox algorithm implementation
22-
pub struct FlexboxAlgorithm;
23-
impl LayoutAlgorithm for FlexboxAlgorithm {
24-
const NAME: &'static str = "FLEXBOX";
25-
26-
fn perform_layout(
27-
tree: &mut impl LayoutTree,
28-
node: NodeId,
29-
known_dimensions: Size<Option<f32>>,
30-
parent_size: Size<Option<f32>>,
31-
available_space: Size<AvailableSpace>,
32-
_sizing_mode: SizingMode,
33-
_vertical_margins_are_collapsible: Line<bool>,
34-
) -> SizeBaselinesAndMargins {
35-
compute(tree, node, known_dimensions, parent_size, available_space, RunMode::PerformLayout)
36-
}
37-
38-
fn measure_size(
39-
tree: &mut impl LayoutTree,
40-
node: NodeId,
41-
known_dimensions: Size<Option<f32>>,
42-
parent_size: Size<Option<f32>>,
43-
available_space: Size<AvailableSpace>,
44-
_sizing_mode: SizingMode,
45-
_vertical_margins_are_collapsible: Line<bool>,
46-
) -> Size<f32> {
47-
compute(tree, node, known_dimensions, parent_size, available_space, RunMode::ComputeSize).size
48-
}
49-
}
50-
5120
/// The intermediate results of a flexbox calculation for a single item
5221
struct FlexItem {
5322
/// The identifier for the associated node
@@ -181,14 +150,8 @@ struct AlgoConstants {
181150
}
182151

183152
/// Computes the layout of [`LayoutTree`] according to the flexbox algorithm
184-
pub fn compute(
185-
tree: &mut impl LayoutTree,
186-
node: NodeId,
187-
known_dimensions: Size<Option<f32>>,
188-
parent_size: Size<Option<f32>>,
189-
available_space: Size<AvailableSpace>,
190-
run_mode: RunMode,
191-
) -> SizeBaselinesAndMargins {
153+
pub fn compute_flexbox_layout(tree: &mut impl LayoutTree, node: NodeId, inputs: LayoutInput) -> LayoutOutput {
154+
let LayoutInput { known_dimensions, parent_size, run_mode, .. } = inputs;
192155
let style = tree.style(node);
193156

194157
// Pull these out earlier to avoid borrowing issues
@@ -214,18 +177,13 @@ pub fn compute(
214177
}
215178

216179
debug_log!("FLEX: single-pass");
217-
compute_preliminary(tree, node, styled_based_known_dimensions, parent_size, available_space, run_mode)
180+
compute_preliminary(tree, node, LayoutInput { known_dimensions: styled_based_known_dimensions, ..inputs })
218181
}
219182

220183
/// Compute a preliminary size for an item
221-
fn compute_preliminary(
222-
tree: &mut impl LayoutTree,
223-
node: NodeId,
224-
known_dimensions: Size<Option<f32>>,
225-
parent_size: Size<Option<f32>>,
226-
available_space: Size<AvailableSpace>,
227-
run_mode: RunMode,
228-
) -> SizeBaselinesAndMargins {
184+
fn compute_preliminary(tree: &mut impl LayoutTree, node: NodeId, inputs: LayoutInput) -> LayoutOutput {
185+
let LayoutInput { known_dimensions, parent_size, available_space, run_mode, .. } = inputs;
186+
229187
// Define some general constants we will need for the remainder of the algorithm.
230188
let mut constants = compute_constants(tree.style(node), known_dimensions, parent_size);
231189

@@ -402,10 +360,7 @@ fn compute_preliminary(
402360
})
403361
};
404362

405-
SizeBaselinesAndMargins::from_size_and_baselines(
406-
constants.container_size,
407-
Point { x: None, y: first_vertical_baseline },
408-
)
363+
LayoutOutput::from_size_and_baselines(constants.container_size, Point { x: None, y: first_vertical_baseline })
409364
}
410365

411366
/// Compute constants that can be reused during the flexbox algorithm.

src/compute/grid/mod.rs

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::geometry::{AbsoluteAxis, AbstractAxis, InBothAbsAxis};
44
use crate::geometry::{Line, Point, Rect, Size};
55
use crate::style::{AlignContent, AlignItems, AlignSelf, AvailableSpace, Display, Overflow, Position};
66
use crate::style_helpers::*;
7-
use crate::tree::{Layout, RunMode, SizeBaselinesAndMargins, SizingMode};
7+
use crate::tree::{Layout, LayoutInput, LayoutOutput, RunMode, SizingMode};
88
use crate::tree::{LayoutTree, NodeId};
99
use crate::util::debug::debug_log;
1010
use crate::util::sys::{f32_max, GridTrackVec, Vec};
@@ -21,8 +21,6 @@ use types::{CellOccupancyMatrix, GridTrack};
2121

2222
pub(crate) use types::{GridCoordinate, GridLine, OriginZeroLine};
2323

24-
use super::LayoutAlgorithm;
25-
2624
mod alignment;
2725
mod explicit_grid;
2826
mod implicit_grid;
@@ -31,50 +29,15 @@ mod track_sizing;
3129
mod types;
3230
mod util;
3331

34-
/// The public interface to Taffy's CSS Grid algorithm implementation
35-
pub struct CssGridAlgorithm;
36-
impl LayoutAlgorithm for CssGridAlgorithm {
37-
const NAME: &'static str = "CSS GRID";
38-
39-
fn perform_layout(
40-
tree: &mut impl LayoutTree,
41-
node: NodeId,
42-
known_dimensions: Size<Option<f32>>,
43-
parent_size: Size<Option<f32>>,
44-
available_space: Size<AvailableSpace>,
45-
_sizing_mode: SizingMode,
46-
_vertical_margins_are_collapsible: Line<bool>,
47-
) -> SizeBaselinesAndMargins {
48-
compute(tree, node, known_dimensions, parent_size, available_space, RunMode::PerformLayout)
49-
}
50-
51-
fn measure_size(
52-
tree: &mut impl LayoutTree,
53-
node: NodeId,
54-
known_dimensions: Size<Option<f32>>,
55-
parent_size: Size<Option<f32>>,
56-
available_space: Size<AvailableSpace>,
57-
_sizing_mode: SizingMode,
58-
_vertical_margins_are_collapsible: Line<bool>,
59-
) -> Size<f32> {
60-
compute(tree, node, known_dimensions, parent_size, available_space, RunMode::ComputeSize).size
61-
}
62-
}
63-
6432
/// Grid layout algorithm
6533
/// This consists of a few phases:
6634
/// - Resolving the explicit grid
6735
/// - Placing items (which also resolves the implicit grid)
6836
/// - Track (row/column) sizing
6937
/// - Alignment & Final item placement
70-
pub fn compute(
71-
tree: &mut impl LayoutTree,
72-
node: NodeId,
73-
known_dimensions: Size<Option<f32>>,
74-
parent_size: Size<Option<f32>>,
75-
available_space: Size<AvailableSpace>,
76-
run_mode: RunMode,
77-
) -> SizeBaselinesAndMargins {
38+
pub fn compute_grid_layout(tree: &mut impl LayoutTree, node: NodeId, inputs: LayoutInput) -> LayoutOutput {
39+
let LayoutInput { known_dimensions, parent_size, available_space, run_mode, .. } = inputs;
40+
7841
let get_child_styles_iter = |node| tree.children(node).map(|child_node: NodeId| tree.style(child_node));
7942
let style = tree.style(node).clone();
8043
let child_styles_iter = get_child_styles_iter(node);
@@ -542,8 +505,5 @@ pub fn compute(
542505
layout.location.y + item.baseline.unwrap_or(layout.size.height)
543506
};
544507

545-
SizeBaselinesAndMargins::from_size_and_baselines(
546-
container_border_box,
547-
Point { x: None, y: Some(grid_container_baseline) },
548-
)
508+
LayoutOutput::from_size_and_baselines(container_border_box, Point { x: None, y: Some(grid_container_baseline) })
549509
}

0 commit comments

Comments
 (0)