tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 10TOML 3CSS 1350 Sutichaykuna

⎇ 837d03fde9d8a4efa0f4eaf1e8be2fd34a719a50 tests/generated/grid_size_child_fixed_tracks.rs Rust · 153 siq'ikuna · 7.7 KB Ñawpa willay Pi imata llamkarqan ch'uya

Fixes for aspect ratio and min/max sizes (#317) 610c92f31 · 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(
17                    known_dimensions,
18                    available_space,
19                    TEXT,
20                    super::WritingMode::Horizontal,
21                    None,
22                )
23            }),
24        )
25        .unwrap();
26    let node1 = taffy
27        .new_leaf_with_measure(
28            taffy::style::Style {
29                align_self: Some(taffy::style::AlignSelf::Start),
30                justify_self: Some(taffy::style::JustifySelf::Start),
31                ..Default::default()
32            },
33            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
34                const TEXT: &str = "HHH\u{200b}HHH";
35                super::measure_standard_text(
36                    known_dimensions,
37                    available_space,
38                    TEXT,
39                    super::WritingMode::Horizontal,
40                    None,
41                )
42            }),
43        )
44        .unwrap();
45    let node2 = taffy
46        .new_leaf_with_measure(
47            taffy::style::Style {
48                align_self: Some(taffy::style::AlignSelf::Start),
49                justify_self: Some(taffy::style::JustifySelf::Start),
50                ..Default::default()
51            },
52            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
53                const TEXT: &str = "HH\u{200b}HHHH";
54                super::measure_standard_text(
55                    known_dimensions,
56                    available_space,
57                    TEXT,
58                    super::WritingMode::Horizontal,
59                    None,
60                )
61            }),
62        )
63        .unwrap();
64    let node3 = taffy
65        .new_leaf_with_measure(
66            taffy::style::Style {
67                align_self: Some(taffy::style::AlignSelf::Start),
68                justify_self: Some(taffy::style::JustifySelf::Start),
69                size: taffy::geometry::Size { width: taffy::style::Dimension::Points(20f32), height: auto() },
70                ..Default::default()
71            },
72            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
73                const TEXT: &str = "HH\u{200b}HH\u{200b}HH\u{200b}HH";
74                super::measure_standard_text(
75                    known_dimensions,
76                    available_space,
77                    TEXT,
78                    super::WritingMode::Horizontal,
79                    None,
80                )
81            }),
82        )
83        .unwrap();
84    let node4 = taffy
85        .new_leaf_with_measure(
86            taffy::style::Style {
87                align_self: Some(taffy::style::AlignSelf::Start),
88                justify_self: Some(taffy::style::JustifySelf::Start),
89                max_size: taffy::geometry::Size { width: taffy::style::Dimension::Points(30f32), height: auto() },
90                ..Default::default()
91            },
92            taffy::node::MeasureFunc::Raw(|known_dimensions, available_space| {
93                const TEXT: &str = "HH\u{200b}HH\u{200b}HH\u{200b}HH";
94                super::measure_standard_text(
95                    known_dimensions,
96                    available_space,
97                    TEXT,
98                    super::WritingMode::Horizontal,
99                    None,
100                )
101            }),
102        )
103        .unwrap();
104    let node = taffy
105        .new_with_children(
106            taffy::style::Style {
107                display: taffy::style::Display::Grid,
108                grid_template_rows: vec![points(40f32), points(40f32), points(40f32)],
109                grid_template_columns: vec![points(40f32), points(40f32), points(40f32)],
110                size: taffy::geometry::Size {
111                    width: taffy::style::Dimension::Points(120f32),
112                    height: taffy::style::Dimension::Points(120f32),
113                },
114                ..Default::default()
115            },
116            &[node0, node1, node2, node3, node4],
117        )
118        .unwrap();
119    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
120    println!("\nComputed tree:");
121    taffy::debug::print_tree(&taffy, node);
122    println!();
123    let Layout { size, location, .. } = taffy.layout(node).unwrap();
124    assert_eq!(size.width, 120f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 120f32, size.width);
125    assert_eq!(size.height, 120f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 120f32, size.height);
126    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x);
127    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y);
128    let Layout { size, location, .. } = taffy.layout(node0).unwrap();
129    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 40f32, size.width);
130    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 20f32, size.height);
131    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.x);
132    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.y);
133    let Layout { size, location, .. } = taffy.layout(node1).unwrap();
134    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, size.width);
135    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node1.data(), 20f32, size.height);
136    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, location.x);
137    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node1.data(), 0f32, location.y);
138    let Layout { size, location, .. } = taffy.layout(node2).unwrap();
139    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node2.data(), 40f32, size.width);
140    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node2.data(), 20f32, size.height);
141    assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node2.data(), 80f32, location.x);
142    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node2.data(), 0f32, location.y);
143    let Layout { size, location, .. } = taffy.layout(node3).unwrap();
144    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node3.data(), 20f32, size.width);
145    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3.data(), 40f32, size.height);
146    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node3.data(), 0f32, location.x);
147    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node3.data(), 40f32, location.y);
148    let Layout { size, location, .. } = taffy.layout(node4).unwrap();
149    assert_eq!(size.width, 30f32, "width of node {:?}. Expected {}. Actual {}", node4.data(), 30f32, size.width);
150    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4.data(), 40f32, size.height);
151    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node4.data(), 40f32, location.x);
152    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node4.data(), 40f32, location.y);
153}