Pass cross-axis known_dimension when computing flex item min size (0.3.x) (#544)
1#[cfg(test)]
2mod caching {
3 use taffy::node::MeasureFunc;
4 use taffy::prelude::*;
5
6 #[test]
7 fn measure_count_flexbox() {
8 use std::sync::atomic::{AtomicU32, Ordering};
9
10 let mut taffy = Taffy::new();
11 static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
12
13 let leaf = taffy
14 .new_leaf_with_measure(
15 Style { ..Default::default() },
16 MeasureFunc::Raw(|known_dimensions, _available_space| {
17 NUM_MEASURES.fetch_add(1, Ordering::SeqCst);
18 Size {
19 width: known_dimensions.width.unwrap_or(50.0),
20 height: known_dimensions.height.unwrap_or(50.0),
21 }
22 }),
23 )
24 .unwrap();
25
26 let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
27 for _ in 0..100 {
28 node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
29 }
30
31 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
32
33 assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 5);
34 }
35
36 #[test]
37 #[cfg(feature = "grid")]
38 fn measure_count_grid() {
39 use std::sync::atomic::{AtomicU32, Ordering};
40
41 let style = || Style { display: Display::Grid, ..Default::default() };
42
43 let mut taffy = Taffy::new();
44 static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
45
46 let leaf = taffy
47 .new_leaf_with_measure(
48 style(),
49 MeasureFunc::Raw(|known_dimensions, _available_space| {
50 NUM_MEASURES.fetch_add(1, Ordering::SeqCst);
51 Size {
52 width: known_dimensions.width.unwrap_or(50.0),
53 height: known_dimensions.height.unwrap_or(50.0),
54 }
55 }),
56 )
57 .unwrap();
58
59 let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
60 for _ in 0..100 {
61 node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
62 }
63
64 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
65 assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 5);
66 }
67}