tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1076HTML 1008Markdown 12TOML 83208 Sutichaykuna

⎇ 0.4.x tests/generated/grid/grid_absolute_layout_within_border.rs Rust · 231 siq'ikuna · 9.7 KB Ñawpa willay Pi imata llamkarqan ch'uya

Rename Taffy struct to TaffyTree (#580) 17235ffa2 · Nico Burns ·

1#[test]
2fn grid_absolute_layout_within_border() {
3    #[allow(unused_imports)]
4    use taffy::{prelude::*, tree::Layout, TaffyTree};
5    let mut taffy: TaffyTree<crate::TextMeasure> = TaffyTree::new();
6    let node0 = taffy
7        .new_leaf(taffy::style::Style {
8            position: taffy::style::Position::Absolute,
9            size: taffy::geometry::Size {
10                width: taffy::style::Dimension::Length(50f32),
11                height: taffy::style::Dimension::Length(50f32),
12            },
13            inset: taffy::geometry::Rect {
14                left: taffy::style::LengthPercentageAuto::Length(0f32),
15                right: auto(),
16                top: taffy::style::LengthPercentageAuto::Length(0f32),
17                bottom: auto(),
18            },
19            ..Default::default()
20        })
21        .unwrap();
22    let node1 = taffy
23        .new_leaf(taffy::style::Style {
24            position: taffy::style::Position::Absolute,
25            size: taffy::geometry::Size {
26                width: taffy::style::Dimension::Length(50f32),
27                height: taffy::style::Dimension::Length(50f32),
28            },
29            inset: taffy::geometry::Rect {
30                left: auto(),
31                right: taffy::style::LengthPercentageAuto::Length(0f32),
32                top: auto(),
33                bottom: taffy::style::LengthPercentageAuto::Length(0f32),
34            },
35            ..Default::default()
36        })
37        .unwrap();
38    let node2 = taffy
39        .new_leaf(taffy::style::Style {
40            position: taffy::style::Position::Absolute,
41            size: taffy::geometry::Size {
42                width: taffy::style::Dimension::Length(50f32),
43                height: taffy::style::Dimension::Length(50f32),
44            },
45            margin: taffy::geometry::Rect {
46                left: taffy::style::LengthPercentageAuto::Length(10f32),
47                right: taffy::style::LengthPercentageAuto::Length(10f32),
48                top: taffy::style::LengthPercentageAuto::Length(10f32),
49                bottom: taffy::style::LengthPercentageAuto::Length(10f32),
50            },
51            inset: taffy::geometry::Rect {
52                left: taffy::style::LengthPercentageAuto::Length(0f32),
53                right: auto(),
54                top: taffy::style::LengthPercentageAuto::Length(0f32),
55                bottom: auto(),
56            },
57            ..Default::default()
58        })
59        .unwrap();
60    let node3 = taffy
61        .new_leaf(taffy::style::Style {
62            position: taffy::style::Position::Absolute,
63            size: taffy::geometry::Size {
64                width: taffy::style::Dimension::Length(50f32),
65                height: taffy::style::Dimension::Length(50f32),
66            },
67            margin: taffy::geometry::Rect {
68                left: taffy::style::LengthPercentageAuto::Length(10f32),
69                right: taffy::style::LengthPercentageAuto::Length(10f32),
70                top: taffy::style::LengthPercentageAuto::Length(10f32),
71                bottom: taffy::style::LengthPercentageAuto::Length(10f32),
72            },
73            inset: taffy::geometry::Rect {
74                left: auto(),
75                right: taffy::style::LengthPercentageAuto::Length(0f32),
76                top: auto(),
77                bottom: taffy::style::LengthPercentageAuto::Length(0f32),
78            },
79            ..Default::default()
80        })
81        .unwrap();
82    let node = taffy
83        .new_with_children(
84            taffy::style::Style {
85                display: taffy::style::Display::Grid,
86                size: taffy::geometry::Size {
87                    width: taffy::style::Dimension::Length(100f32),
88                    height: taffy::style::Dimension::Length(100f32),
89                },
90                padding: taffy::geometry::Rect {
91                    left: taffy::style::LengthPercentage::Length(10f32),
92                    right: taffy::style::LengthPercentage::Length(10f32),
93                    top: taffy::style::LengthPercentage::Length(10f32),
94                    bottom: taffy::style::LengthPercentage::Length(10f32),
95                },
96                border: taffy::geometry::Rect {
97                    left: taffy::style::LengthPercentage::Length(10f32),
98                    right: taffy::style::LengthPercentage::Length(10f32),
99                    top: taffy::style::LengthPercentage::Length(10f32),
100                    bottom: taffy::style::LengthPercentage::Length(10f32),
101                },
102                ..Default::default()
103            },
104            &[node0, node1, node2, node3],
105        )
106        .unwrap();
107    taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
108    println!("\nComputed tree:");
109    taffy.print_tree(node);
110    println!();
111    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
112    let layout @ Layout { size, location, .. } = taffy.layout(node).unwrap();
113    assert_eq!(size.width, 100f32, "width of node {:?}. Expected {}. Actual {}", node, 100f32, size.width);
114    assert_eq!(size.height, 100f32, "height of node {:?}. Expected {}. Actual {}", node, 100f32, size.height);
115    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
116    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
117    #[cfg(feature = "content_size")]
118    assert_eq!(
119        layout.scroll_width(),
120        0f32,
121        "scroll_width of node {:?}. Expected {}. Actual {}",
122        node,
123        0f32,
124        layout.scroll_width()
125    );
126    #[cfg(feature = "content_size")]
127    assert_eq!(
128        layout.scroll_height(),
129        0f32,
130        "scroll_height of node {:?}. Expected {}. Actual {}",
131        node,
132        0f32,
133        layout.scroll_height()
134    );
135    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
136    let layout @ Layout { size, location, .. } = taffy.layout(node0).unwrap();
137    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node0, 50f32, size.width);
138    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node0, 50f32, size.height);
139    assert_eq!(location.x, 10f32, "x of node {:?}. Expected {}. Actual {}", node0, 10f32, location.x);
140    assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node0, 10f32, location.y);
141    #[cfg(feature = "content_size")]
142    assert_eq!(
143        layout.scroll_width(),
144        0f32,
145        "scroll_width of node {:?}. Expected {}. Actual {}",
146        node0,
147        0f32,
148        layout.scroll_width()
149    );
150    #[cfg(feature = "content_size")]
151    assert_eq!(
152        layout.scroll_height(),
153        0f32,
154        "scroll_height of node {:?}. Expected {}. Actual {}",
155        node0,
156        0f32,
157        layout.scroll_height()
158    );
159    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
160    let layout @ Layout { size, location, .. } = taffy.layout(node1).unwrap();
161    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node1, 50f32, size.width);
162    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node1, 50f32, size.height);
163    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node1, 40f32, location.x);
164    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node1, 40f32, location.y);
165    #[cfg(feature = "content_size")]
166    assert_eq!(
167        layout.scroll_width(),
168        0f32,
169        "scroll_width of node {:?}. Expected {}. Actual {}",
170        node1,
171        0f32,
172        layout.scroll_width()
173    );
174    #[cfg(feature = "content_size")]
175    assert_eq!(
176        layout.scroll_height(),
177        0f32,
178        "scroll_height of node {:?}. Expected {}. Actual {}",
179        node1,
180        0f32,
181        layout.scroll_height()
182    );
183    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
184    let layout @ Layout { size, location, .. } = taffy.layout(node2).unwrap();
185    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node2, 50f32, size.width);
186    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node2, 50f32, size.height);
187    assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node2, 20f32, location.x);
188    assert_eq!(location.y, 20f32, "y of node {:?}. Expected {}. Actual {}", node2, 20f32, location.y);
189    #[cfg(feature = "content_size")]
190    assert_eq!(
191        layout.scroll_width(),
192        0f32,
193        "scroll_width of node {:?}. Expected {}. Actual {}",
194        node2,
195        0f32,
196        layout.scroll_width()
197    );
198    #[cfg(feature = "content_size")]
199    assert_eq!(
200        layout.scroll_height(),
201        0f32,
202        "scroll_height of node {:?}. Expected {}. Actual {}",
203        node2,
204        0f32,
205        layout.scroll_height()
206    );
207    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
208    let layout @ Layout { size, location, .. } = taffy.layout(node3).unwrap();
209    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node3, 50f32, size.width);
210    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node3, 50f32, size.height);
211    assert_eq!(location.x, 30f32, "x of node {:?}. Expected {}. Actual {}", node3, 30f32, location.x);
212    assert_eq!(location.y, 30f32, "y of node {:?}. Expected {}. Actual {}", node3, 30f32, location.y);
213    #[cfg(feature = "content_size")]
214    assert_eq!(
215        layout.scroll_width(),
216        0f32,
217        "scroll_width of node {:?}. Expected {}. Actual {}",
218        node3,
219        0f32,
220        layout.scroll_width()
221    );
222    #[cfg(feature = "content_size")]
223    assert_eq!(
224        layout.scroll_height(),
225        0f32,
226        "scroll_height of node {:?}. Expected {}. Actual {}",
227        node3,
228        0f32,
229        layout.scroll_height()
230    );
231}