gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 499HTML 254C 19JavaScript 81372 Definitions

⎇ b0c7d824daea2c453850812d532c277f8ac37736 tests/generated/grid_percent_items_nested_moderate.rs Rust · 82 lines · 4.6 KB History Who touched what raw

Fix percent sized tracks (#314) 06a168cb1 · Nico Burns ·

1#[test]
2fn grid_percent_items_nested_moderate() {
3    use slotmap::Key;
4    #[allow(unused_imports)]
5    use taffy::{layout::Layout, prelude::*};
6    let mut taffy = taffy::Taffy::new();
7    let node00 = taffy
8        .new_leaf(taffy::style::Style {
9            size: taffy::geometry::Size { width: taffy::style::Dimension::Percent(0.45f32), height: auto() },
10            margin: taffy::geometry::Rect {
11                left: taffy::style::LengthPercentageAuto::Percent(0.05f32),
12                right: taffy::style::LengthPercentageAuto::Percent(0.05f32),
13                top: taffy::style::LengthPercentageAuto::Percent(0.05f32),
14                bottom: taffy::style::LengthPercentageAuto::Percent(0.05f32),
15            },
16            padding: taffy::geometry::Rect {
17                left: taffy::style::LengthPercentage::Points(3f32),
18                right: taffy::style::LengthPercentage::Points(3f32),
19                top: taffy::style::LengthPercentage::Points(3f32),
20                bottom: taffy::style::LengthPercentage::Points(3f32),
21            },
22            ..Default::default()
23        })
24        .unwrap();
25    let node0 = taffy
26        .new_with_children(
27            taffy::style::Style {
28                display: taffy::style::Display::Grid,
29                size: taffy::geometry::Size { width: taffy::style::Dimension::Percent(0.5f32), height: auto() },
30                margin: taffy::geometry::Rect {
31                    left: taffy::style::LengthPercentageAuto::Points(5f32),
32                    right: taffy::style::LengthPercentageAuto::Points(5f32),
33                    top: taffy::style::LengthPercentageAuto::Points(5f32),
34                    bottom: taffy::style::LengthPercentageAuto::Points(5f32),
35                },
36                padding: taffy::geometry::Rect {
37                    left: taffy::style::LengthPercentage::Percent(0.03f32),
38                    right: taffy::style::LengthPercentage::Percent(0.03f32),
39                    top: taffy::style::LengthPercentage::Percent(0.03f32),
40                    bottom: taffy::style::LengthPercentage::Percent(0.03f32),
41                },
42                ..Default::default()
43            },
44            &[node00],
45        )
46        .unwrap();
47    let node = taffy
48        .new_with_children(
49            taffy::style::Style {
50                display: taffy::style::Display::Grid,
51                size: taffy::geometry::Size { width: taffy::style::Dimension::Points(200f32), height: auto() },
52                padding: taffy::geometry::Rect {
53                    left: taffy::style::LengthPercentage::Points(3f32),
54                    right: taffy::style::LengthPercentage::Points(3f32),
55                    top: taffy::style::LengthPercentage::Points(3f32),
56                    bottom: taffy::style::LengthPercentage::Points(3f32),
57                },
58                ..Default::default()
59            },
60            &[node0],
61        )
62        .unwrap();
63    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
64    println!("\nComputed tree:");
65    taffy::debug::print_tree(&taffy, node);
66    println!();
67    let Layout { size, location, .. } = taffy.layout(node).unwrap();
68    assert_eq!(size.width, 200f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 200f32, size.width);
69    assert_eq!(size.height, 42f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 42f32, size.height);
70    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x);
71    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y);
72    let Layout { size, location, .. } = taffy.layout(node0).unwrap();
73    assert_eq!(size.width, 97f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 97f32, size.width);
74    assert_eq!(size.height, 26f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 26f32, size.height);
75    assert_eq!(location.x, 8f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 8f32, location.x);
76    assert_eq!(location.y, 8f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 8f32, location.y);
77    let Layout { size, location, .. } = taffy.layout(node00).unwrap();
78    assert_eq!(size.width, 38f32, "width of node {:?}. Expected {}. Actual {}", node00.data(), 38f32, size.width);
79    assert_eq!(size.height, 6f32, "height of node {:?}. Expected {}. Actual {}", node00.data(), 6f32, size.height);
80    assert_eq!(location.x, 10f32, "x of node {:?}. Expected {}. Actual {}", node00.data(), 10f32, location.x);
81    assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node00.data(), 10f32, location.y);
82}