tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Sutichaykuna

⎇ traitify-style tests/caching.rs Rust · 57 siq'ikuna · 1.9 KB Ñawpa willay Pi imata llamkarqan ch'uya

Enable correct image measure functions (#661) 09c7a718b · Nico Burns ·

1#[cfg(test)]
2mod caching {
3    use taffy::prelude::*;
4
5    struct CountMeasure {
6        count: usize,
7    }
8    impl CountMeasure {
9        fn new() -> CountMeasure {
10            CountMeasure { count: 0 }
11        }
12    }
13
14    fn count_measure_function(
15        known_dimensions: Size<Option<f32>>,
16        _available_space: Size<AvailableSpace>,
17        _node_id: NodeId,
18        mut node_context: Option<&mut CountMeasure>,
19        _style: &Style,
20    ) -> Size<f32> {
21        node_context.as_mut().unwrap().count += 1;
22        Size { width: known_dimensions.width.unwrap_or(50.0), height: known_dimensions.height.unwrap_or(50.0) }
23    }
24
25    #[test]
26    fn measure_count_flexbox() {
27        let mut taffy: TaffyTree<CountMeasure> = TaffyTree::new();
28
29        let leaf = taffy.new_leaf_with_context(Style::default(), CountMeasure::new()).unwrap();
30
31        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
32        for _ in 0..100 {
33            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
34        }
35
36        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, count_measure_function).unwrap();
37
38        assert_eq!(taffy.get_node_context_mut(leaf).unwrap().count, 4);
39    }
40
41    #[test]
42    #[cfg(feature = "grid")]
43    fn measure_count_grid() {
44        let mut taffy: TaffyTree<CountMeasure> = TaffyTree::new();
45
46        let style = || Style { display: Display::Grid, ..Default::default() };
47        let leaf = taffy.new_leaf_with_context(style(), CountMeasure::new()).unwrap();
48
49        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
50        for _ in 0..100 {
51            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
52        }
53
54        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, count_measure_function).unwrap();
55        assert_eq!(taffy.get_node_context_mut(leaf).unwrap().count, 4);
56    }
57}