tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

⎇ 9118437ed51cb90c74f2880c9c2b728e8b0c890d src/tree.rs Rust · 59 siq'ikuna · 2.4 KB Ñawpa willay Pi imata llamkarqan ch'uya

Abstract flexbox over LayoutTree (rebased on top of latest main) (#238) 9118437ed · Nico Burns ·

1//! The baseline requirements of any UI Tree so Taffy can efficiently calculate the layout
2
3use crate::{
4    layout::{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(&self, node: Node, node_size: Size<Option<f32>>) -> Size<f32>;
39
40    /// Node needs to be measured
41    fn needs_measure(&self, node: Node) -> bool;
42
43    /// Get the primary cache for this Node.
44    ///
45    /// Taffy caches results of computations for nodes to not need re-caculating nodes it already knows
46    ///
47    /// When a node does not have a cache, Taffy will layout that node appropriately.
48    fn primary_cache(&mut self, node: Node) -> &mut Option<Cache>;
49
50    /// Get the secondary cache for this Node.
51    ///
52    /// Taffy caches results of computations for nodes to not need re-caculating nodes it already knows
53    ///
54    /// When a node does not have a cache, Taffy will layout that node appropriately.
55    ///
56    /// The secondary cache is for nodes who have a main size already calculated, but need to calculate a secondary size.
57    /// This typically happens due to conflicting constraints.
58    fn secondary_cache(&mut self, node: Node) -> &mut Option<Cache>;
59}