tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 238Rust 13TOML 3CSS 1146 Definiciones

⎇ morphorm tests/caching.rs Rust · 56 líneas · 1.8 KB Historia Quién tocó qué crudo

Replace per-node measure functions with a single measure function + per-node context (#490) be627e7fa · 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    ) -> Size<f32> {
20        node_context.as_mut().unwrap().count += 1;
21        Size { width: known_dimensions.width.unwrap_or(50.0), height: known_dimensions.height.unwrap_or(50.0) }
22    }
23
24    #[test]
25    fn measure_count_flexbox() {
26        let mut taffy: Taffy<CountMeasure> = Taffy::new();
27
28        let leaf = taffy.new_leaf_with_context(Style::default(), CountMeasure::new()).unwrap();
29
30        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
31        for _ in 0..100 {
32            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
33        }
34
35        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, count_measure_function).unwrap();
36
37        assert_eq!(taffy.get_node_context_mut(leaf).unwrap().count, 3);
38    }
39
40    #[test]
41    #[cfg(feature = "grid")]
42    fn measure_count_grid() {
43        let mut taffy: Taffy<CountMeasure> = Taffy::new();
44
45        let style = || Style { display: Display::Grid, ..Default::default() };
46        let leaf = taffy.new_leaf_with_context(style(), CountMeasure::new()).unwrap();
47
48        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
49        for _ in 0..100 {
50            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
51        }
52
53        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, count_measure_function).unwrap();
54        assert_eq!(taffy.get_node_context_mut(leaf).unwrap().count, 3);
55    }
56}