gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 89Markdown 11TOML 91930 Definitions

⎇ 0.1.x tests/simple_child.rs Rust · 79 lines · 2.9 KB History Who touched what raw

Rename sprawl to taffy (#134) 8936790d6 · Alice Cecile ·

1use taffy::{geometry::Point, style::Dimension};
2
3#[test]
4fn simple_child() {
5    let mut taffy = taffy::Taffy::new();
6    let node0_0 = taffy
7        .new_node(
8            taffy::style::Style {
9                align_self: taffy::prelude::AlignSelf::Center,
10                size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
11                ..Default::default()
12            },
13            &[],
14        )
15        .unwrap();
16    let node0 = taffy
17        .new_node(
18            taffy::style::Style {
19                size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
20                ..Default::default()
21            },
22            &[node0_0],
23        )
24        .unwrap();
25    let node1_0 = taffy
26        .new_node(
27            taffy::style::Style {
28                align_self: taffy::prelude::AlignSelf::Center,
29                size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
30                ..Default::default()
31            },
32            &[],
33        )
34        .unwrap();
35    let node1_1 = taffy
36        .new_node(
37            taffy::style::Style {
38                align_self: taffy::prelude::AlignSelf::Center,
39                size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
40                ..Default::default()
41            },
42            &[],
43        )
44        .unwrap();
45    let node1 = taffy
46        .new_node(
47            taffy::style::Style {
48                size: taffy::geometry::Size { width: Dimension::Undefined, height: Dimension::Undefined },
49                ..Default::default()
50            },
51            &[node1_0, node1_1],
52        )
53        .unwrap();
54    let node = taffy
55        .new_node(
56            taffy::style::Style {
57                size: taffy::geometry::Size { width: Dimension::Percent(100.0), height: Dimension::Percent(100.0) },
58                ..Default::default()
59            },
60            &[node0, node1],
61        )
62        .unwrap();
63    taffy
64        .compute_layout(
65            node,
66            taffy::geometry::Size {
67                width: taffy::prelude::Number::Defined(100f32),
68                height: taffy::prelude::Number::Defined(100f32),
69            },
70        )
71        .unwrap();
72    assert_eq!(taffy.layout(node).unwrap().location, Point { x: 0.0, y: 0.0 });
73    assert_eq!(taffy.layout(node0).unwrap().location, Point { x: 0.0, y: 0.0 });
74    assert_eq!(taffy.layout(node1).unwrap().location, Point { x: 10.0, y: 0.0 });
75    assert_eq!(taffy.layout(node0_0).unwrap().location, Point { x: 0.0, y: 0.0 });
76    // Layout is relative so node1_0 location starts at (0,0) and is not ofset by it's parent location
77    assert_eq!(taffy.layout(node1_0).unwrap().location, Point { x: 00.0, y: 0.0 });
78    assert_eq!(taffy.layout(node1_1).unwrap().location, Point { x: 10.0, y: 0.0 });
79}