gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 684HTML 640Markdown 12TOML 62290 Definitions

⎇ f7383d3927d2bb521fa5b44e4aacf5bbecd1e5e3 tests/simple_child.rs Rust · 64 lines · 2.6 KB History Who touched what raw

Strict types for style values (#269) 0d8c198a9 · Nico Burns ·

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