basic/
basic.rs

1use compose_rt::{Composer, Root};
2use compose_taffy::impls::{LayoutNode, TaffyConfig};
3use compose_taffy::TaffyLayout;
4use taffy::{AvailableSpace, Dimension, JustifyContent, Size, Style};
5
6type Scope<T> = compose_taffy::impls::Scope<T, ()>;
7
8struct Container;
9
10#[track_caller]
11fn container<P, C>(s: Scope<P>, style: Style, content: C)
12where
13    P: 'static,
14    C: Fn(Scope<Container>) + Clone + 'static,
15{
16    let scope = s.child::<Container>();
17    s.create_node(
18        scope,
19        content,
20        move || style.clone(),
21        |style, _| LayoutNode::new(style),
22        |n, style, _| {
23            if n.style != style {
24                n.style = style;
25                n.mark_dirty();
26            }
27        },
28    );
29}
30
31struct Leaf;
32
33#[track_caller]
34fn leaf<P>(s: Scope<P>, style: Style)
35where
36    P: 'static,
37{
38    let scope = s.child::<Leaf>();
39    s.create_node(
40        scope,
41        |_| {},
42        move || style.clone(),
43        |style, _| LayoutNode::new(style),
44        |n, style, _| {
45            if n.style != style {
46                n.style = style;
47                n.mark_dirty();
48            }
49        },
50    );
51}
52
53fn app(s: Scope<Root>) {
54    container(
55        s,
56        Style {
57            size: Size {
58                width: Dimension::Length(100.0),
59                height: Dimension::Length(100.0),
60            },
61            justify_content: Some(JustifyContent::Center),
62            ..Default::default()
63        },
64        |s| {
65            leaf(
66                s,
67                Style {
68                    size: Size {
69                        width: Dimension::Percent(0.5),
70                        height: Dimension::Auto,
71                    },
72                    ..Default::default()
73                },
74            );
75        },
76    );
77}
78
79fn main() {
80    let mut recomposer = Composer::compose(app, TaffyConfig::default());
81    let _ = recomposer.compute_layout(Size {
82        height: AvailableSpace::Definite(100.0),
83        width: AvailableSpace::Definite(100.0),
84    });
85    let _ = recomposer.print_layout_tree();
86}