gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1111HTML 1039Markdown 12TOML 94541 Sutichaykuna

⎇ 8a41d7181043b8561bf8cb19dd61ae7d08b34d38 examples/nested.rs Rust · 55 siq'ikuna · 1.6 KB Ñawpa willay Pi imata llamkarqan ch'uya

Use slotmap instead of bespoke arena, limiting to one parent only (#196) c3f6a1727 · Jon Kelley ·

1use taffy::prelude::*;
2
3fn main() -> Result<(), taffy::error::TaffyError> {
4    let mut taffy = Taffy::new();
5
6    // left
7    let child_t1 = taffy.new_leaf(FlexboxLayout {
8        size: Size { width: Dimension::Points(5.0), height: Dimension::Points(5.0) },
9        ..Default::default()
10    })?;
11
12    let div1 = taffy.new_with_children(
13        FlexboxLayout {
14            size: Size { width: Dimension::Percent(0.5), height: Dimension::Percent(1.0) },
15            // justify_content: JustifyContent::Center,
16            ..Default::default()
17        },
18        &[child_t1],
19    )?;
20
21    // right
22    let child_t2 = taffy.new_leaf(FlexboxLayout {
23        size: Size { width: Dimension::Points(5.0), height: Dimension::Points(5.0) },
24        ..Default::default()
25    })?;
26
27    let div2 = taffy.new_with_children(
28        FlexboxLayout {
29            size: Size { width: Dimension::Percent(0.5), height: Dimension::Percent(1.0) },
30            // justify_content: JustifyContent::Center,
31            ..Default::default()
32        },
33        &[child_t2],
34    )?;
35
36    let container = taffy.new_with_children(
37        FlexboxLayout {
38            size: Size { width: Dimension::Percent(1.0), height: Dimension::Percent(1.0) },
39            ..Default::default()
40        },
41        &[div1, div2],
42    )?;
43
44    taffy.compute_layout(container, Size { height: Some(100.0), width: Some(100.0) })?;
45
46    println!("node: {:#?}", taffy.layout(container)?);
47
48    println!("div1: {:#?}", taffy.layout(div1)?);
49    println!("div2: {:#?}", taffy.layout(div2)?);
50
51    println!("child1: {:#?}", taffy.layout(child_t1)?);
52    println!("child2: {:#?}", taffy.layout(child_t2)?);
53
54    Ok(())
55}