tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5334Rust 85Markdown 12TOML 91813 Sutichaykuna

⎇ 0.4.x tests/generated/grid/grid_margins_auto_margins.rs Rust · 316 siq'ikuna · 13.7 KB Ñawpa willay Pi imata llamkarqan ch'uya

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

1#[test]
2fn grid_margins_auto_margins() {
3    #[allow(unused_imports)]
4    use taffy::{prelude::*, tree::Layout, TaffyTree};
5    let mut taffy: TaffyTree<crate::TextMeasure> = TaffyTree::new();
6    let node0 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
7    let node1 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
8    let node2 = taffy
9        .new_leaf(taffy::style::Style {
10            justify_self: Some(taffy::style::JustifySelf::Start),
11            size: taffy::geometry::Size { width: taffy::style::Dimension::Length(20f32), height: auto() },
12            margin: taffy::geometry::Rect {
13                left: taffy::style::LengthPercentageAuto::Auto,
14                right: taffy::style::LengthPercentageAuto::Auto,
15                top: taffy::style::LengthPercentageAuto::Length(0f32),
16                bottom: taffy::style::LengthPercentageAuto::Length(0f32),
17            },
18            ..Default::default()
19        })
20        .unwrap();
21    let node3 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
22    let node4 = taffy
23        .new_leaf(taffy::style::Style {
24            align_self: Some(taffy::style::AlignSelf::Start),
25            size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Length(20f32) },
26            margin: taffy::geometry::Rect {
27                left: taffy::style::LengthPercentageAuto::Length(0f32),
28                right: taffy::style::LengthPercentageAuto::Length(0f32),
29                top: taffy::style::LengthPercentageAuto::Auto,
30                bottom: taffy::style::LengthPercentageAuto::Auto,
31            },
32            ..Default::default()
33        })
34        .unwrap();
35    let node5 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
36    let node6 = taffy
37        .new_leaf(taffy::style::Style {
38            align_self: Some(taffy::style::AlignSelf::Start),
39            justify_self: Some(taffy::style::JustifySelf::Start),
40            size: taffy::geometry::Size {
41                width: taffy::style::Dimension::Length(20f32),
42                height: taffy::style::Dimension::Length(20f32),
43            },
44            margin: taffy::geometry::Rect {
45                left: taffy::style::LengthPercentageAuto::Auto,
46                right: taffy::style::LengthPercentageAuto::Auto,
47                top: taffy::style::LengthPercentageAuto::Auto,
48                bottom: taffy::style::LengthPercentageAuto::Auto,
49            },
50            ..Default::default()
51        })
52        .unwrap();
53    let node7 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
54    let node8 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
55    let node = taffy
56        .new_with_children(
57            taffy::style::Style {
58                display: taffy::style::Display::Grid,
59                grid_template_rows: vec![length(40f32), length(40f32), length(40f32)],
60                grid_template_columns: vec![length(40f32), length(40f32), length(40f32)],
61                padding: taffy::geometry::Rect {
62                    left: taffy::style::LengthPercentage::Length(40f32),
63                    right: taffy::style::LengthPercentage::Length(20f32),
64                    top: taffy::style::LengthPercentage::Length(10f32),
65                    bottom: taffy::style::LengthPercentage::Length(30f32),
66                },
67                ..Default::default()
68            },
69            &[node0, node1, node2, node3, node4, node5, node6, node7, node8],
70        )
71        .unwrap();
72    taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
73    println!("\nComputed tree:");
74    taffy.print_tree(node);
75    println!();
76    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
77    let layout @ Layout { size, location, .. } = taffy.layout(node).unwrap();
78    assert_eq!(size.width, 180f32, "width of node {:?}. Expected {}. Actual {}", node, 180f32, size.width);
79    assert_eq!(size.height, 160f32, "height of node {:?}. Expected {}. Actual {}", node, 160f32, size.height);
80    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
81    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
82    #[cfg(feature = "content_size")]
83    assert_eq!(
84        layout.scroll_width(),
85        0f32,
86        "scroll_width of node {:?}. Expected {}. Actual {}",
87        node,
88        0f32,
89        layout.scroll_width()
90    );
91    #[cfg(feature = "content_size")]
92    assert_eq!(
93        layout.scroll_height(),
94        0f32,
95        "scroll_height of node {:?}. Expected {}. Actual {}",
96        node,
97        0f32,
98        layout.scroll_height()
99    );
100    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
101    let layout @ Layout { size, location, .. } = taffy.layout(node0).unwrap();
102    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node0, 40f32, size.width);
103    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node0, 40f32, size.height);
104    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node0, 40f32, location.x);
105    assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node0, 10f32, location.y);
106    #[cfg(feature = "content_size")]
107    assert_eq!(
108        layout.scroll_width(),
109        0f32,
110        "scroll_width of node {:?}. Expected {}. Actual {}",
111        node0,
112        0f32,
113        layout.scroll_width()
114    );
115    #[cfg(feature = "content_size")]
116    assert_eq!(
117        layout.scroll_height(),
118        0f32,
119        "scroll_height of node {:?}. Expected {}. Actual {}",
120        node0,
121        0f32,
122        layout.scroll_height()
123    );
124    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
125    let layout @ Layout { size, location, .. } = taffy.layout(node1).unwrap();
126    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node1, 40f32, size.width);
127    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node1, 40f32, size.height);
128    assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node1, 80f32, location.x);
129    assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node1, 10f32, location.y);
130    #[cfg(feature = "content_size")]
131    assert_eq!(
132        layout.scroll_width(),
133        0f32,
134        "scroll_width of node {:?}. Expected {}. Actual {}",
135        node1,
136        0f32,
137        layout.scroll_width()
138    );
139    #[cfg(feature = "content_size")]
140    assert_eq!(
141        layout.scroll_height(),
142        0f32,
143        "scroll_height of node {:?}. Expected {}. Actual {}",
144        node1,
145        0f32,
146        layout.scroll_height()
147    );
148    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
149    let layout @ Layout { size, location, .. } = taffy.layout(node2).unwrap();
150    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node2, 20f32, size.width);
151    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node2, 40f32, size.height);
152    assert_eq!(location.x, 130f32, "x of node {:?}. Expected {}. Actual {}", node2, 130f32, location.x);
153    assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node2, 10f32, location.y);
154    #[cfg(feature = "content_size")]
155    assert_eq!(
156        layout.scroll_width(),
157        0f32,
158        "scroll_width of node {:?}. Expected {}. Actual {}",
159        node2,
160        0f32,
161        layout.scroll_width()
162    );
163    #[cfg(feature = "content_size")]
164    assert_eq!(
165        layout.scroll_height(),
166        0f32,
167        "scroll_height of node {:?}. Expected {}. Actual {}",
168        node2,
169        0f32,
170        layout.scroll_height()
171    );
172    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
173    let layout @ Layout { size, location, .. } = taffy.layout(node3).unwrap();
174    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node3, 40f32, size.width);
175    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3, 40f32, size.height);
176    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node3, 40f32, location.x);
177    assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node3, 50f32, location.y);
178    #[cfg(feature = "content_size")]
179    assert_eq!(
180        layout.scroll_width(),
181        0f32,
182        "scroll_width of node {:?}. Expected {}. Actual {}",
183        node3,
184        0f32,
185        layout.scroll_width()
186    );
187    #[cfg(feature = "content_size")]
188    assert_eq!(
189        layout.scroll_height(),
190        0f32,
191        "scroll_height of node {:?}. Expected {}. Actual {}",
192        node3,
193        0f32,
194        layout.scroll_height()
195    );
196    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
197    let layout @ Layout { size, location, .. } = taffy.layout(node4).unwrap();
198    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node4, 40f32, size.width);
199    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node4, 20f32, size.height);
200    assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node4, 80f32, location.x);
201    assert_eq!(location.y, 60f32, "y of node {:?}. Expected {}. Actual {}", node4, 60f32, location.y);
202    #[cfg(feature = "content_size")]
203    assert_eq!(
204        layout.scroll_width(),
205        0f32,
206        "scroll_width of node {:?}. Expected {}. Actual {}",
207        node4,
208        0f32,
209        layout.scroll_width()
210    );
211    #[cfg(feature = "content_size")]
212    assert_eq!(
213        layout.scroll_height(),
214        0f32,
215        "scroll_height of node {:?}. Expected {}. Actual {}",
216        node4,
217        0f32,
218        layout.scroll_height()
219    );
220    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
221    let layout @ Layout { size, location, .. } = taffy.layout(node5).unwrap();
222    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node5, 40f32, size.width);
223    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node5, 40f32, size.height);
224    assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node5, 120f32, location.x);
225    assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node5, 50f32, location.y);
226    #[cfg(feature = "content_size")]
227    assert_eq!(
228        layout.scroll_width(),
229        0f32,
230        "scroll_width of node {:?}. Expected {}. Actual {}",
231        node5,
232        0f32,
233        layout.scroll_width()
234    );
235    #[cfg(feature = "content_size")]
236    assert_eq!(
237        layout.scroll_height(),
238        0f32,
239        "scroll_height of node {:?}. Expected {}. Actual {}",
240        node5,
241        0f32,
242        layout.scroll_height()
243    );
244    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
245    let layout @ Layout { size, location, .. } = taffy.layout(node6).unwrap();
246    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node6, 20f32, size.width);
247    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node6, 20f32, size.height);
248    assert_eq!(location.x, 50f32, "x of node {:?}. Expected {}. Actual {}", node6, 50f32, location.x);
249    assert_eq!(location.y, 100f32, "y of node {:?}. Expected {}. Actual {}", node6, 100f32, location.y);
250    #[cfg(feature = "content_size")]
251    assert_eq!(
252        layout.scroll_width(),
253        0f32,
254        "scroll_width of node {:?}. Expected {}. Actual {}",
255        node6,
256        0f32,
257        layout.scroll_width()
258    );
259    #[cfg(feature = "content_size")]
260    assert_eq!(
261        layout.scroll_height(),
262        0f32,
263        "scroll_height of node {:?}. Expected {}. Actual {}",
264        node6,
265        0f32,
266        layout.scroll_height()
267    );
268    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
269    let layout @ Layout { size, location, .. } = taffy.layout(node7).unwrap();
270    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node7, 40f32, size.width);
271    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node7, 40f32, size.height);
272    assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node7, 80f32, location.x);
273    assert_eq!(location.y, 90f32, "y of node {:?}. Expected {}. Actual {}", node7, 90f32, location.y);
274    #[cfg(feature = "content_size")]
275    assert_eq!(
276        layout.scroll_width(),
277        0f32,
278        "scroll_width of node {:?}. Expected {}. Actual {}",
279        node7,
280        0f32,
281        layout.scroll_width()
282    );
283    #[cfg(feature = "content_size")]
284    assert_eq!(
285        layout.scroll_height(),
286        0f32,
287        "scroll_height of node {:?}. Expected {}. Actual {}",
288        node7,
289        0f32,
290        layout.scroll_height()
291    );
292    #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
293    let layout @ Layout { size, location, .. } = taffy.layout(node8).unwrap();
294    assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node8, 40f32, size.width);
295    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node8, 40f32, size.height);
296    assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node8, 120f32, location.x);
297    assert_eq!(location.y, 90f32, "y of node {:?}. Expected {}. Actual {}", node8, 90f32, location.y);
298    #[cfg(feature = "content_size")]
299    assert_eq!(
300        layout.scroll_width(),
301        0f32,
302        "scroll_width of node {:?}. Expected {}. Actual {}",
303        node8,
304        0f32,
305        layout.scroll_width()
306    );
307    #[cfg(feature = "content_size")]
308    assert_eq!(
309        layout.scroll_height(),
310        0f32,
311        "scroll_height of node {:?}. Expected {}. Actual {}",
312        node8,
313        0f32,
314        layout.scroll_height()
315    );
316}