tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Sutichaykuna

⎇ main examples/custom_tree_owned_unsafe.rs Rust · 283 siq'ikuna · 8.6 KB Ñawpa willay Pi imata llamkarqan ch'uya

Use &LayoutInput in cache APIs (#933) 4b6687da0 · Nico Burns ·

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(&self, node_id: NodeId, inputs: &taffy::LayoutInput) -> Option<taffy::LayoutOutput> {
189        unsafe { node_from_id(node_id) }.cache.get(inputs)
190    }
191
192    fn cache_store(&mut self, node_id: NodeId, inputs: &taffy::LayoutInput, layout_output: taffy::LayoutOutput) {
193        unsafe { node_from_id_mut(node_id) }.cache.store(inputs, layout_output)
194    }
195
196    fn cache_clear(&mut self, node_id: NodeId) {
197        unsafe { node_from_id_mut(node_id) }.cache.clear();
198    }
199}
200
201impl taffy::LayoutFlexboxContainer for StatelessLayoutTree {
202    type FlexboxContainerStyle<'a>
203        = &'a Style
204    where
205        Self: 'a;
206
207    type FlexboxItemStyle<'a>
208        = &'a Style
209    where
210        Self: 'a;
211
212    fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
213        unsafe { &node_from_id(node_id).style }
214    }
215
216    fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
217        unsafe { &node_from_id(child_node_id).style }
218    }
219}
220
221impl taffy::LayoutGridContainer for StatelessLayoutTree {
222    type GridContainerStyle<'a>
223        = &'a Style
224    where
225        Self: 'a;
226
227    type GridItemStyle<'a>
228        = &'a Style
229    where
230        Self: 'a;
231
232    fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
233        unsafe { &node_from_id(node_id).style }
234    }
235
236    fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
237        unsafe { &node_from_id(child_node_id).style }
238    }
239}
240
241impl RoundTree for StatelessLayoutTree {
242    fn get_unrounded_layout(&self, node_id: NodeId) -> Layout {
243        unsafe { node_from_id_mut(node_id).unrounded_layout }
244    }
245
246    fn set_final_layout(&mut self, node_id: NodeId, layout: &Layout) {
247        unsafe { node_from_id_mut(node_id).final_layout = *layout }
248    }
249}
250
251impl PrintTree for StatelessLayoutTree {
252    fn get_debug_label(&self, node_id: NodeId) -> &'static str {
253        match unsafe { node_from_id(node_id).kind } {
254            NodeKind::Flexbox => "FLEX",
255            NodeKind::Grid => "GRID",
256            NodeKind::Text => "TEXT",
257            NodeKind::Image => "IMAGE",
258        }
259    }
260
261    fn get_final_layout(&self, node_id: NodeId) -> Layout {
262        unsafe { node_from_id(node_id).final_layout }
263    }
264}
265
266fn main() -> Result<(), taffy::TaffyError> {
267    let mut root = Node::new_column(Style::DEFAULT);
268
269    let text_node = Node::new_text(
270        Style::default(),
271        TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal },
272    );
273    root.append_child(text_node);
274
275    let image_node = Node::new_image(Style::default(), ImageContext { width: 400.0, height: 300.0 });
276    root.append_child(image_node);
277
278    // Compute layout and print result
279    root.compute_layout(Size::MAX_CONTENT, true);
280    root.print_tree();
281
282    Ok(())
283}