1use crate::{Alignment, Padding, Point, Rectangle, Size, Vector};
2
3#[derive(Debug, Clone, Default)]
5pub struct Node {
6 bounds: Rectangle,
7 children: Vec<Node>,
8}
9
10impl Node {
11 pub const fn new(size: Size) -> Self {
13 Self::with_children(size, Vec::new())
14 }
15
16 pub const fn with_children(size: Size, children: Vec<Node>) -> Self {
18 Node {
19 bounds: Rectangle {
20 x: 0.0,
21 y: 0.0,
22 width: size.width,
23 height: size.height,
24 },
25 children,
26 }
27 }
28
29 pub fn container(child: Self, padding: Padding) -> Self {
31 Self::with_children(
32 child.bounds.size().expand(padding),
33 vec![child.move_to(Point::new(padding.left, padding.top))],
34 )
35 }
36
37 pub fn size(&self) -> Size {
39 Size::new(self.bounds.width, self.bounds.height)
40 }
41
42 pub fn bounds(&self) -> Rectangle {
44 self.bounds
45 }
46
47 pub fn children(&self) -> &[Node] {
49 &self.children
50 }
51
52 pub fn align(mut self, align_x: Alignment, align_y: Alignment, space: Size) -> Self {
54 self.align_mut(align_x, align_y, space);
55 self
56 }
57
58 pub fn align_mut(&mut self, align_x: Alignment, align_y: Alignment, space: Size) {
60 match align_x {
61 Alignment::Start => {}
62 Alignment::Center => {
63 self.bounds.x += (space.width - self.bounds.width) / 2.0;
64 }
65 Alignment::End => {
66 self.bounds.x += space.width - self.bounds.width;
67 }
68 }
69
70 match align_y {
71 Alignment::Start => {}
72 Alignment::Center => {
73 self.bounds.y += (space.height - self.bounds.height) / 2.0;
74 }
75 Alignment::End => {
76 self.bounds.y += space.height - self.bounds.height;
77 }
78 }
79 }
80
81 pub fn move_to(mut self, position: impl Into<Point>) -> Self {
83 self.move_to_mut(position);
84 self
85 }
86
87 pub fn move_to_mut(&mut self, position: impl Into<Point>) {
89 let position = position.into();
90
91 self.bounds.x = position.x;
92 self.bounds.y = position.y;
93 }
94
95 pub fn translate(mut self, translation: impl Into<Vector>) -> Self {
97 self.translate_mut(translation);
98 self
99 }
100
101 pub fn translate_mut(&mut self, translation: impl Into<Vector>) {
103 self.bounds = self.bounds + translation.into();
104 }
105}