Skip to main content

axes/
lib.rs

1#![cfg_attr(feature = "no_std", no_std)]
2mod engine;
3pub use engine::*;
4mod computed;
5pub use computed::*;
6mod styles;
7pub use styles::*;
8mod style;
9pub use style::*;
10mod tree;
11pub use tree::*;
12
13pub type NodeId = usize;
14
15#[cfg(feature = "no_std")]
16extern crate alloc;
17#[cfg(feature = "no_std")]
18#[allow(unused_imports)]
19use alloc::vec; // HACK: for some reason the vec! macro
20// doesn't make rustc think a crate is used
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn it_works() {
28        let mut tree = LayoutTree::new();
29
30        let root = tree.new_child(Style {
31            size: Size {
32                height: Units::Percentage(0.5),
33                width: Units::Percentage(0.5),
34            },
35            gap: Size {
36                width: Units::Pixels(30.0),
37                height: Units::Pixels(0.0),
38            },
39            padding: Padding {
40                top: 20.0,
41                ..Default::default()
42            },
43            direction: Direction::Row,
44            ..Default::default()
45        });
46
47        let child1 = tree.new_child(Style {
48            size: Size {
49                width: Units::Percentage(1.0),
50                height: Units::Pixels(300.0),
51            },
52            ..Default::default()
53        });
54
55        let child2 = tree.new_child(Style {
56            size: Size {
57                width: Units::Pixels(300.0),
58                height: Units::Percentage(1.0),
59            },
60            margin: Margin {
61                top: 20.0,
62                left: 20.0,
63                right: 0.0,
64                bottom: 0.0,
65            },
66            max_size: Some(Size {
67                width: Units::Pixels(200.0),
68                height: Units::Pixels(300.0),
69            }),
70            min_size: Some(Size {
71                width: Units::Pixels(300.0),
72                height: Units::Pixels(40.0),
73            }),
74            vertical_align: VerticalAlign::Center,
75            ..Default::default()
76        });
77
78        // Set parent to child
79        tree.add_children(root, vec![child1, child2].as_slice());
80
81        let mut engine = LayoutEngine::new();
82
83        engine.compute(&tree, root, 900.0, 900.0);
84
85        assert_eq!(engine.computed[0].x, 0.0);
86        assert_eq!(engine.computed[0].y, 0.0);
87
88        assert_eq!(engine.computed[1].height, 300.0);
89        assert_eq!(engine.computed[1].width, 450.0);
90        assert_eq!(engine.computed[1].x, 0.0);
91        assert_eq!(engine.computed[1].y, 20.0);
92
93        assert_eq!(engine.computed[2].height, 300.0);
94        assert_eq!(engine.computed[2].width, 200.0);
95        assert_eq!(engine.computed[2].y, 105.0);
96        assert_eq!(engine.computed[2].x, 500.0);
97    }
98}