tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 229Rust 8TOML 3CSS 1310 Definitions

⎇ 4490b1baff58fe94eb559d7ac152b78ca2f3bd9f examples/nested.rs Rust · 55 lines · 1.6 KB History Who touched what raw

Rename Taffy struct to TaffyTree (#580) 17235ffa2 · Nico Burns ·

1use taffy::prelude::*;
2
3fn main() -> Result<(), taffy::TaffyError> {
4    let mut taffy: TaffyTree<()> = TaffyTree::new();
5
6    // left
7    let child_t1 = taffy.new_leaf(Style {
8        size: Size { width: Dimension::Length(5.0), height: Dimension::Length(5.0) },
9        ..Default::default()
10    })?;
11
12    let div1 = taffy.new_with_children(
13        Style {
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(Style {
23        size: Size { width: Dimension::Length(5.0), height: Dimension::Length(5.0) },
24        ..Default::default()
25    })?;
26
27    let div2 = taffy.new_with_children(
28        Style {
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        Style { size: Size { width: Dimension::Percent(1.0), height: Dimension::Percent(1.0) }, ..Default::default() },
38        &[div1, div2],
39    )?;
40
41    taffy.compute_layout(
42        container,
43        Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
44    )?;
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}