tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 9TOML 3CSS 1345 Definitions

⎇ 859cc921b3adb8e3b184e6fc0cf0c40b6c5a625b tests/generated/grid_size_child_fixed_tracks.rs Rust · 123 lines · 7.1 KB History Who touched what raw

Grid/width depends on height tests (#300) 4f43428b5 · Nico Burns ·

1#[test]
2fn grid_size_child_fixed_tracks() {
3    use slotmap::Key;
4    #[allow(unused_imports)]
5    use taffy::{layout::Layout, prelude::*};
6    let mut taffy = taffy::Taffy::new();
7    let node0 = taffy
8        .new_leaf_with_measure(
9            taffy::style::Style {
10                align_self: Some(taffy::style::AlignSelf::Start),
11                justify_self: Some(taffy::style::JustifySelf::Start),
12                ..Default::default()
13            },
14            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
15                const TEXT: &str = "HH\u{200b}HH\u{200b}HH\u{200b}HH";
16                super::measure_standard_text(known_dimensions, available_space, TEXT, super::WritingMode::Horizontal)
17            }),
18        )
19        .unwrap();
20    let node1 = taffy
21        .new_leaf_with_measure(
22            taffy::style::Style {
23                align_self: Some(taffy::style::AlignSelf::Start),
24                justify_self: Some(taffy::style::JustifySelf::Start),
25                ..Default::default()
26            },
27            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
28                const TEXT: &str = "HHH\u{200b}HHH";
29                super::measure_standard_text(known_dimensions, available_space, TEXT, super::WritingMode::Horizontal)
30            }),
31        )
32        .unwrap();
33    let node2 = taffy
34        .new_leaf_with_measure(
35            taffy::style::Style {
36                align_self: Some(taffy::style::AlignSelf::Start),
37                justify_self: Some(taffy::style::JustifySelf::Start),
38                ..Default::default()
39            },
40            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
41                const TEXT: &str = "HH\u{200b}HHHH";
42                super::measure_standard_text(known_dimensions, available_space, TEXT, super::WritingMode::Horizontal)
43            }),
44        )
45        .unwrap();
46    let node3 = taffy
47        .new_leaf_with_measure(
48            taffy::style::Style {
49                align_self: Some(taffy::style::AlignSelf::Start),
50                justify_self: Some(taffy::style::JustifySelf::Start),
51                size: taffy::geometry::Size { width: taffy::style::Dimension::Points(20f32), height: auto() },
52                ..Default::default()
53            },
54            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
55                const TEXT: &str = "HH\u{200b}HH\u{200b}HH\u{200b}HH";
56                super::measure_standard_text(known_dimensions, available_space, TEXT, super::WritingMode::Horizontal)
57            }),
58        )
59        .unwrap();
60    let node4 = taffy
61        .new_leaf_with_measure(
62            taffy::style::Style {
63                align_self: Some(taffy::style::AlignSelf::Start),
64                justify_self: Some(taffy::style::JustifySelf::Start),
65                max_size: taffy::geometry::Size { width: taffy::style::Dimension::Points(30f32), height: auto() },
66                ..Default::default()
67            },
68            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
69                const TEXT: &str = "HH\u{200b}HH\u{200b}HH\u{200b}HH";
70                super::measure_standard_text(known_dimensions, available_space, TEXT, super::WritingMode::Horizontal)
71            }),
72        )
73        .unwrap();
74    let node = taffy
75        .new_with_children(
76            taffy::style::Style {
77                display: taffy::style::Display::Grid,
78                grid_template_rows: vec![points(40f32), points(40f32), points(40f32)],
79                grid_template_columns: vec![points(40f32), points(40f32), points(40f32)],
80                size: taffy::geometry::Size {
81                    width: taffy::style::Dimension::Points(120f32),
82                    height: taffy::style::Dimension::Points(120f32),
83                },
84                ..Default::default()
85            },
86            &[node0, node1, node2, node3, node4],
87        )
88        .unwrap();
89    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
90    println!("\nComputed tree:");
91    taffy::debug::print_tree(&taffy, node);
92    println!();
93    let Layout { size, location, .. } = taffy.layout(node).unwrap();
94    assert_eq!(size.width, 120f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 120f32, size.width);
95    assert_eq!(size.height, 120f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 120f32, size.height);
96    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x);
97    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y);
98    let Layout { size, location, .. } = taffy.layout(node0).unwrap();
99    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 40f32, size.width);
100    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 20f32, size.height);
101    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.x);
102    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.y);
103    let Layout { size, location, .. } = taffy.layout(node1).unwrap();
104    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, size.width);
105    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node1.data(), 20f32, size.height);
106    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, location.x);
107    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node1.data(), 0f32, location.y);
108    let Layout { size, location, .. } = taffy.layout(node2).unwrap();
109    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node2.data(), 40f32, size.width);
110    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node2.data(), 20f32, size.height);
111    assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node2.data(), 80f32, location.x);
112    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node2.data(), 0f32, location.y);
113    let Layout { size, location, .. } = taffy.layout(node3).unwrap();
114    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node3.data(), 20f32, size.width);
115    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3.data(), 40f32, size.height);
116    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node3.data(), 0f32, location.x);
117    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node3.data(), 40f32, location.y);
118    let Layout { size, location, .. } = taffy.layout(node4).unwrap();
119    assert_eq!(size.width, 30f32, "width of node {:?}. Expected {}. Actual {}", node4.data(), 30f32, size.width);
120    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4.data(), 40f32, size.height);
121    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node4.data(), 40f32, location.x);
122    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node4.data(), 40f32, location.y);
123}