gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1111HTML 1039Markdown 12TOML 94524 Definitions

⎇ 163284a65cd48cdf6c6b96b9216284cb1fefacd3 tests/generated/grid_min_content_flex_column.rs Rust · 84 lines · 4.7 KB History Who touched what raw

Fix sizing of flexbox nodes under a min-content constraint (#291) 163284a65 · Nico Burns ·

1#[test]
2fn grid_min_content_flex_column() {
3    use slotmap::Key;
4    #[allow(unused_imports)]
5    use taffy::{layout::Layout, prelude::*};
6    let mut taffy = taffy::Taffy::new();
7    let node00 = taffy
8        .new_leaf_with_measure(
9            taffy::style::Style { ..Default::default() },
10            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
11                const TEXT: &str = "HH\u{200b}HH";
12                super::measure_standard_text(known_dimensions, available_space, TEXT)
13            }),
14        )
15        .unwrap();
16    let node01 = taffy
17        .new_leaf_with_measure(
18            taffy::style::Style { ..Default::default() },
19            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
20                const TEXT: &str = "HH\u{200b}HH";
21                super::measure_standard_text(known_dimensions, available_space, TEXT)
22            }),
23        )
24        .unwrap();
25    let node02 = taffy
26        .new_leaf_with_measure(
27            taffy::style::Style { ..Default::default() },
28            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
29                const TEXT: &str = "HH\u{200b}HH";
30                super::measure_standard_text(known_dimensions, available_space, TEXT)
31            }),
32        )
33        .unwrap();
34    let node0 = taffy
35        .new_with_children(
36            taffy::style::Style {
37                display: taffy::style::Display::Flex,
38                flex_direction: taffy::style::FlexDirection::Column,
39                ..Default::default()
40            },
41            &[node00, node01, node02],
42        )
43        .unwrap();
44    let node = taffy
45        .new_with_children(
46            taffy::style::Style {
47                display: taffy::style::Display::Grid,
48                grid_template_rows: vec![points(40f32)],
49                grid_template_columns: vec![min_content()],
50                ..Default::default()
51            },
52            &[node0],
53        )
54        .unwrap();
55    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
56    println!("\nComputed tree:");
57    taffy::debug::print_tree(&taffy, node);
58    println!();
59    let Layout { size, location, .. } = taffy.layout(node).unwrap();
60    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 20f32, size.width);
61    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 40f32, size.height);
62    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x);
63    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y);
64    let Layout { size, location, .. } = taffy.layout(node0).unwrap();
65    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 20f32, size.width);
66    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 40f32, size.height);
67    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.x);
68    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.y);
69    let Layout { size, location, .. } = taffy.layout(node00).unwrap();
70    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node00.data(), 20f32, size.width);
71    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node00.data(), 20f32, size.height);
72    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node00.data(), 0f32, location.x);
73    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node00.data(), 0f32, location.y);
74    let Layout { size, location, .. } = taffy.layout(node01).unwrap();
75    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node01.data(), 20f32, size.width);
76    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node01.data(), 20f32, size.height);
77    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node01.data(), 0f32, location.x);
78    assert_eq!(location.y, 20f32, "y of node {:?}. Expected {}. Actual {}", node01.data(), 20f32, location.y);
79    let Layout { size, location, .. } = taffy.layout(node02).unwrap();
80    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node02.data(), 20f32, size.width);
81    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node02.data(), 20f32, size.height);
82    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node02.data(), 0f32, location.x);
83    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node02.data(), 40f32, location.y);
84}