tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 492HTML 238TOML 3CSS 11112 Sutichaykuna

⎇ morphorm tests/generated/grid/grid_min_content_flex_column.rs Rust · 86 siq'ikuna · 4.4 KB Ñawpa willay Pi imata llamkarqan ch'uya

Replace per-node measure functions with a single measure function + per-node context (#490) be627e7fa · Nico Burns ·

1#[test]
2fn grid_min_content_flex_column() {
3    #[allow(unused_imports)]
4    use taffy::{prelude::*, tree::Layout, Taffy};
5    let mut taffy: Taffy<crate::TextMeasure> = Taffy::new();
6    let node00 = taffy
7        .new_leaf_with_context(
8            taffy::style::Style { ..Default::default() },
9            crate::TextMeasure {
10                text_content: "HH\u{200b}HH",
11                writing_mode: crate::WritingMode::Horizontal,
12                _aspect_ratio: None,
13            },
14        )
15        .unwrap();
16    let node01 = taffy
17        .new_leaf_with_context(
18            taffy::style::Style { ..Default::default() },
19            crate::TextMeasure {
20                text_content: "HH\u{200b}HH",
21                writing_mode: crate::WritingMode::Horizontal,
22                _aspect_ratio: None,
23            },
24        )
25        .unwrap();
26    let node02 = taffy
27        .new_leaf_with_context(
28            taffy::style::Style { ..Default::default() },
29            crate::TextMeasure {
30                text_content: "HH\u{200b}HH",
31                writing_mode: crate::WritingMode::Horizontal,
32                _aspect_ratio: None,
33            },
34        )
35        .unwrap();
36    let node0 = taffy
37        .new_with_children(
38            taffy::style::Style {
39                display: taffy::style::Display::Flex,
40                flex_direction: taffy::style::FlexDirection::Column,
41                ..Default::default()
42            },
43            &[node00, node01, node02],
44        )
45        .unwrap();
46    let node = taffy
47        .new_with_children(
48            taffy::style::Style {
49                display: taffy::style::Display::Grid,
50                grid_template_rows: vec![length(40f32)],
51                grid_template_columns: vec![min_content()],
52                ..Default::default()
53            },
54            &[node0],
55        )
56        .unwrap();
57    taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
58    println!("\nComputed tree:");
59    taffy.print_tree(node);
60    println!();
61    let Layout { size, location, .. } = taffy.layout(node).unwrap();
62    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node, 20f32, size.width);
63    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node, 40f32, size.height);
64    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
65    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
66    let Layout { size, location, .. } = taffy.layout(node0).unwrap();
67    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node0, 20f32, size.width);
68    assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node0, 40f32, size.height);
69    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0, 0f32, location.x);
70    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0, 0f32, location.y);
71    let Layout { size, location, .. } = taffy.layout(node00).unwrap();
72    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node00, 20f32, size.width);
73    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node00, 20f32, size.height);
74    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node00, 0f32, location.x);
75    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node00, 0f32, location.y);
76    let Layout { size, location, .. } = taffy.layout(node01).unwrap();
77    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node01, 20f32, size.width);
78    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node01, 20f32, size.height);
79    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node01, 0f32, location.x);
80    assert_eq!(location.y, 20f32, "y of node {:?}. Expected {}. Actual {}", node01, 20f32, location.y);
81    let Layout { size, location, .. } = taffy.layout(node02).unwrap();
82    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node02, 20f32, size.width);
83    assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node02, 20f32, size.height);
84    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node02, 0f32, location.x);
85    assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node02, 40f32, location.y);
86}