custom_tree_vec/
custom_tree_vec.rs

1mod common {
2    pub mod image;
3    pub mod text;
4}
5use common::image::{image_measure_function, ImageContext};
6use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode, LOREM_IPSUM};
7use taffy::util::print_tree;
8use taffy::{
9    compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
10    prelude::*, round_layout, Cache, CacheTree,
11};
12
13#[derive(Debug, Copy, Clone)]
14#[allow(dead_code)]
15enum NodeKind {
16    Flexbox,
17    Grid,
18    Text,
19    Image,
20}
21
22struct Node {
23    kind: NodeKind,
24    style: Style,
25    text_data: Option<TextContext>,
26    image_data: Option<ImageContext>,
27    cache: Cache,
28    unrounded_layout: Layout,
29    final_layout: Layout,
30    children: Vec<usize>,
31}
32
33impl Default for Node {
34    fn default() -> Self {
35        Node {
36            kind: NodeKind::Flexbox,
37            style: Style::default(),
38            text_data: None,
39            image_data: None,
40            cache: Cache::new(),
41            unrounded_layout: Layout::with_order(0),
42            final_layout: Layout::with_order(0),
43            children: Vec::new(),
44        }
45    }
46}
47
48#[allow(dead_code)]
49impl Node {
50    pub fn new_row(style: Style) -> Node {
51        Node {
52            kind: NodeKind::Flexbox,
53            style: Style { display: Display::Flex, flex_direction: FlexDirection::Row, ..style },
54            ..Node::default()
55        }
56    }
57    pub fn new_column(style: Style) -> Node {
58        Node {
59            kind: NodeKind::Flexbox,
60            style: Style { display: Display::Flex, flex_direction: FlexDirection::Column, ..style },
61            ..Node::default()
62        }
63    }
64    pub fn new_grid(style: Style) -> Node {
65        Node { kind: NodeKind::Grid, style: Style { display: Display::Grid, ..style }, ..Node::default() }
66    }
67    pub fn new_text(style: Style, text_data: TextContext) -> Node {
68        Node { kind: NodeKind::Text, style, text_data: Some(text_data), ..Node::default() }
69    }
70    pub fn new_image(style: Style, image_data: ImageContext) -> Node {
71        Node { kind: NodeKind::Image, style, image_data: Some(image_data), ..Node::default() }
72    }
73}
74
75struct Tree {
76    nodes: Vec<Node>,
77}
78
79impl Tree {
80    pub fn new() -> Tree {
81        Tree { nodes: Vec::new() }
82    }
83
84    pub fn add_node(&mut self, node: Node) -> usize {
85        self.nodes.push(node);
86        self.nodes.len() - 1
87    }
88
89    pub fn append_child(&mut self, parent: usize, child: usize) {
90        self.nodes[parent].children.push(child);
91    }
92
93    #[inline(always)]
94    fn node_from_id(&self, node_id: NodeId) -> &Node {
95        &self.nodes[usize::from(node_id)]
96    }
97
98    #[inline(always)]
99    fn node_from_id_mut(&mut self, node_id: NodeId) -> &mut Node {
100        &mut self.nodes[usize::from(node_id)]
101    }
102
103    pub fn compute_layout(&mut self, root: usize, available_space: Size<AvailableSpace>, use_rounding: bool) {
104        compute_root_layout(self, NodeId::from(root), available_space);
105        if use_rounding {
106            round_layout(self, NodeId::from(root))
107        }
108    }
109
110    pub fn print_tree(&mut self, root: usize) {
111        print_tree(self, NodeId::from(root));
112    }
113}
114
115struct ChildIter<'a>(std::slice::Iter<'a, usize>);
116impl Iterator for ChildIter<'_> {
117    type Item = NodeId;
118    fn next(&mut self) -> Option<Self::Item> {
119        self.0.next().copied().map(NodeId::from)
120    }
121}
122
123impl taffy::TraversePartialTree for Tree {
124    type ChildIter<'a> = ChildIter<'a>;
125
126    fn child_ids(&self, node_id: NodeId) -> Self::ChildIter<'_> {
127        ChildIter(self.node_from_id(node_id).children.iter())
128    }
129
130    fn child_count(&self, node_id: NodeId) -> usize {
131        self.node_from_id(node_id).children.len()
132    }
133
134    fn get_child_id(&self, node_id: NodeId, index: usize) -> NodeId {
135        NodeId::from(self.node_from_id(node_id).children[index])
136    }
137}
138
139impl taffy::TraverseTree for Tree {}
140
141impl taffy::LayoutPartialTree for Tree {
142    type CustomIdent = String;
143
144    type CoreContainerStyle<'a>
145        = &'a Style
146    where
147        Self: 'a;
148
149    fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
150        &self.node_from_id(node_id).style
151    }
152
153    fn set_unrounded_layout(&mut self, node_id: NodeId, layout: &Layout) {
154        self.node_from_id_mut(node_id).unrounded_layout = *layout;
155    }
156
157    fn resolve_calc_value(&self, _val: *const (), _basis: f32) -> f32 {
158        0.0
159    }
160
161    fn compute_child_layout(&mut self, node_id: NodeId, inputs: taffy::tree::LayoutInput) -> taffy::tree::LayoutOutput {
162        compute_cached_layout(self, node_id, inputs, |tree, node_id, inputs| {
163            let node = &mut tree.nodes[usize::from(node_id)];
164            let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
165
166            match node.kind {
167                NodeKind::Flexbox => compute_flexbox_layout(tree, node_id, inputs),
168                NodeKind::Grid => compute_grid_layout(tree, node_id, inputs),
169                NodeKind::Text => compute_leaf_layout(
170                    inputs,
171                    &node.style,
172                    |_val, _basis| 0.0,
173                    |known_dimensions, available_space| {
174                        text_measure_function(
175                            known_dimensions,
176                            available_space,
177                            node.text_data.as_ref().unwrap(),
178                            &font_metrics,
179                        )
180                    },
181                ),
182                NodeKind::Image => compute_leaf_layout(
183                    inputs,
184                    &node.style,
185                    |_val, _basis| 0.0,
186                    |known_dimensions, _available_space| {
187                        image_measure_function(known_dimensions, node.image_data.as_ref().unwrap())
188                    },
189                ),
190            }
191        })
192    }
193}
194
195impl CacheTree for Tree {
196    fn cache_get(
197        &self,
198        node_id: NodeId,
199        known_dimensions: Size<Option<f32>>,
200        available_space: Size<AvailableSpace>,
201        run_mode: taffy::RunMode,
202    ) -> Option<taffy::LayoutOutput> {
203        self.node_from_id(node_id).cache.get(known_dimensions, available_space, run_mode)
204    }
205
206    fn cache_store(
207        &mut self,
208        node_id: NodeId,
209        known_dimensions: Size<Option<f32>>,
210        available_space: Size<AvailableSpace>,
211        run_mode: taffy::RunMode,
212        layout_output: taffy::LayoutOutput,
213    ) {
214        self.node_from_id_mut(node_id).cache.store(known_dimensions, available_space, run_mode, layout_output)
215    }
216
217    fn cache_clear(&mut self, node_id: NodeId) {
218        self.node_from_id_mut(node_id).cache.clear();
219    }
220}
221
222impl taffy::LayoutFlexboxContainer for Tree {
223    type FlexboxContainerStyle<'a>
224        = &'a Style
225    where
226        Self: 'a;
227
228    type FlexboxItemStyle<'a>
229        = &'a Style
230    where
231        Self: 'a;
232
233    fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
234        &self.node_from_id(node_id).style
235    }
236
237    fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
238        &self.node_from_id(child_node_id).style
239    }
240}
241
242impl taffy::LayoutGridContainer for Tree {
243    type GridContainerStyle<'a>
244        = &'a Style
245    where
246        Self: 'a;
247
248    type GridItemStyle<'a>
249        = &'a Style
250    where
251        Self: 'a;
252
253    fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
254        &self.node_from_id(node_id).style
255    }
256
257    fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
258        &self.node_from_id(child_node_id).style
259    }
260}
261
262impl taffy::RoundTree for Tree {
263    fn get_unrounded_layout(&self, node_id: NodeId) -> Layout {
264        self.node_from_id(node_id).unrounded_layout
265    }
266
267    fn set_final_layout(&mut self, node_id: NodeId, layout: &Layout) {
268        self.node_from_id_mut(node_id).final_layout = *layout;
269    }
270}
271
272impl taffy::PrintTree for Tree {
273    fn get_debug_label(&self, node_id: NodeId) -> &'static str {
274        match self.node_from_id(node_id).kind {
275            NodeKind::Flexbox => "FLEX",
276            NodeKind::Grid => "GRID",
277            NodeKind::Text => "TEXT",
278            NodeKind::Image => "IMAGE",
279        }
280    }
281
282    fn get_final_layout(&self, node_id: NodeId) -> Layout {
283        self.node_from_id(node_id).final_layout
284    }
285}
286
287fn main() -> Result<(), taffy::TaffyError> {
288    let mut tree = Tree::new();
289
290    let root = Node::new_column(Style::DEFAULT);
291    let root_id = tree.add_node(root);
292
293    let text_node = Node::new_text(
294        Style::default(),
295        TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal },
296    );
297    let text_node_id = tree.add_node(text_node);
298    tree.append_child(root_id, text_node_id);
299
300    let image_node = Node::new_image(Style::default(), ImageContext { width: 400.0, height: 300.0 });
301    let image_node_id = tree.add_node(image_node);
302    tree.append_child(root_id, image_node_id);
303
304    // Compute layout and print result
305    tree.compute_layout(root_id, Size::MAX_CONTENT, true);
306    tree.print_tree(root_id);
307
308    Ok(())
309}