custom_tree_owned_unsafe/
custom_tree_owned_unsafe.rs

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