tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 499HTML 238Markdown 7TOML 31234 Definitions

⎇ ea2beedd05271c1038a7d9539f8313e949915a14 tests/generated/grid_absolute_layout_within_border.rs Rust · 137 lines · 7.2 KB History Who touched what raw

Fixes for aspect ratio and min/max sizes (#317) 610c92f31 · Nico Burns ·

1#[test]
2fn grid_absolute_layout_within_border() {
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(taffy::style::Style {
9            position: taffy::style::Position::Absolute,
10            size: taffy::geometry::Size {
11                width: taffy::style::Dimension::Points(50f32),
12                height: taffy::style::Dimension::Points(50f32),
13            },
14            inset: taffy::geometry::Rect {
15                left: taffy::style::LengthPercentageAuto::Points(0f32),
16                right: auto(),
17                top: taffy::style::LengthPercentageAuto::Points(0f32),
18                bottom: auto(),
19            },
20            ..Default::default()
21        })
22        .unwrap();
23    let node1 = taffy
24        .new_leaf(taffy::style::Style {
25            position: taffy::style::Position::Absolute,
26            size: taffy::geometry::Size {
27                width: taffy::style::Dimension::Points(50f32),
28                height: taffy::style::Dimension::Points(50f32),
29            },
30            inset: taffy::geometry::Rect {
31                left: auto(),
32                right: taffy::style::LengthPercentageAuto::Points(0f32),
33                top: auto(),
34                bottom: taffy::style::LengthPercentageAuto::Points(0f32),
35            },
36            ..Default::default()
37        })
38        .unwrap();
39    let node2 = taffy
40        .new_leaf(taffy::style::Style {
41            position: taffy::style::Position::Absolute,
42            size: taffy::geometry::Size {
43                width: taffy::style::Dimension::Points(50f32),
44                height: taffy::style::Dimension::Points(50f32),
45            },
46            margin: taffy::geometry::Rect {
47                left: taffy::style::LengthPercentageAuto::Points(10f32),
48                right: taffy::style::LengthPercentageAuto::Points(10f32),
49                top: taffy::style::LengthPercentageAuto::Points(10f32),
50                bottom: taffy::style::LengthPercentageAuto::Points(10f32),
51            },
52            inset: taffy::geometry::Rect {
53                left: taffy::style::LengthPercentageAuto::Points(0f32),
54                right: auto(),
55                top: taffy::style::LengthPercentageAuto::Points(0f32),
56                bottom: auto(),
57            },
58            ..Default::default()
59        })
60        .unwrap();
61    let node3 = taffy
62        .new_leaf(taffy::style::Style {
63            position: taffy::style::Position::Absolute,
64            size: taffy::geometry::Size {
65                width: taffy::style::Dimension::Points(50f32),
66                height: taffy::style::Dimension::Points(50f32),
67            },
68            margin: taffy::geometry::Rect {
69                left: taffy::style::LengthPercentageAuto::Points(10f32),
70                right: taffy::style::LengthPercentageAuto::Points(10f32),
71                top: taffy::style::LengthPercentageAuto::Points(10f32),
72                bottom: taffy::style::LengthPercentageAuto::Points(10f32),
73            },
74            inset: taffy::geometry::Rect {
75                left: auto(),
76                right: taffy::style::LengthPercentageAuto::Points(0f32),
77                top: auto(),
78                bottom: taffy::style::LengthPercentageAuto::Points(0f32),
79            },
80            ..Default::default()
81        })
82        .unwrap();
83    let node = taffy
84        .new_with_children(
85            taffy::style::Style {
86                display: taffy::style::Display::Grid,
87                size: taffy::geometry::Size {
88                    width: taffy::style::Dimension::Points(100f32),
89                    height: taffy::style::Dimension::Points(100f32),
90                },
91                padding: taffy::geometry::Rect {
92                    left: taffy::style::LengthPercentage::Points(10f32),
93                    right: taffy::style::LengthPercentage::Points(10f32),
94                    top: taffy::style::LengthPercentage::Points(10f32),
95                    bottom: taffy::style::LengthPercentage::Points(10f32),
96                },
97                border: taffy::geometry::Rect {
98                    left: taffy::style::LengthPercentage::Points(10f32),
99                    right: taffy::style::LengthPercentage::Points(10f32),
100                    top: taffy::style::LengthPercentage::Points(10f32),
101                    bottom: taffy::style::LengthPercentage::Points(10f32),
102                },
103                ..Default::default()
104            },
105            &[node0, node1, node2, node3],
106        )
107        .unwrap();
108    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
109    println!("\nComputed tree:");
110    taffy::debug::print_tree(&taffy, node);
111    println!();
112    let Layout { size, location, .. } = taffy.layout(node).unwrap();
113    assert_eq!(size.width, 100f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 100f32, size.width);
114    assert_eq!(size.height, 100f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 100f32, size.height);
115    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x);
116    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y);
117    let Layout { size, location, .. } = taffy.layout(node0).unwrap();
118    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 50f32, size.width);
119    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 50f32, size.height);
120    assert_eq!(location.x, 10f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 10f32, location.x);
121    assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 10f32, location.y);
122    let Layout { size, location, .. } = taffy.layout(node1).unwrap();
123    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node1.data(), 50f32, size.width);
124    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node1.data(), 50f32, size.height);
125    assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, location.x);
126    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, location.y);
127    let Layout { size, location, .. } = taffy.layout(node2).unwrap();
128    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node2.data(), 50f32, size.width);
129    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node2.data(), 50f32, size.height);
130    assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node2.data(), 20f32, location.x);
131    assert_eq!(location.y, 20f32, "y of node {:?}. Expected {}. Actual {}", node2.data(), 20f32, location.y);
132    let Layout { size, location, .. } = taffy.layout(node3).unwrap();
133    assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node3.data(), 50f32, size.width);
134    assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node3.data(), 50f32, size.height);
135    assert_eq!(location.x, 30f32, "x of node {:?}. Expected {}. Actual {}", node3.data(), 30f32, location.x);
136    assert_eq!(location.y, 30f32, "y of node {:?}. Expected {}. Actual {}", node3.data(), 30f32, location.y);
137}