Layout algorithm decoupling, Sizing Constraints & Perf Improvements (#246)
1//! The baseline requirements of any UI Tree so Taffy can efficiently calculate the layout
2
3use crate::{
4 layout::{AvailableSpace, Cache, Layout},
5 prelude::*,
6};
7
8/// Any item that implements the LayoutTree can be layed out using Taffy's algorithms.
9///
10/// Generally, Taffy expects your Node tree to be indexable by stable indices. A "stable" index means that the Node's ID
11/// remains the same between re-layouts.
12pub trait LayoutTree {
13 /// Get the list of children IDs for the given node
14 fn children(&self, node: Node) -> &[Node];
15
16 /// Get a specific child of a node, where the index represents the nth child
17 fn child(&self, node: Node, index: usize) -> Node;
18
19 /// Get any available parent for this node
20 fn parent(&self, node: Node) -> Option<Node>;
21
22 // todo: allow abstractions over this so we don't prescribe how layout works
23 // for reference, CSS cascades require context, and storing a full flexbox layout for each node could be inefficient
24 //
25 /// Get the [`FlexboxLayout`] for this Node.
26 fn style(&self, node: Node) -> &FlexboxLayout;
27
28 /// Get the node's output "Final Layout"
29 fn layout(&self, node: Node) -> &Layout;
30
31 /// Modify the node's output layout
32 fn layout_mut(&mut self, node: Node) -> &mut Layout;
33
34 /// Mark a node as finished
35 fn mark_dirty(&mut self, node: Node, dirty: bool);
36
37 /// Measure a node. Taffy uses this to force reflows of things like text and overflowing content.
38 fn measure_node(
39 &self,
40 node: Node,
41 known_dimensions: Size<Option<f32>>,
42 available_space: Size<AvailableSpace>,
43 ) -> Size<f32>;
44
45 /// Node needs to be measured
46 fn needs_measure(&self, node: Node) -> bool;
47
48 /// Get a cache entry for this Node by index
49 fn cache_mut(&mut self, node: Node, index: usize) -> &mut Option<Cache>;
50}