1use crate::{Alignment, Padding, Point, Rectangle, Size, Vector};
2
3#[derive(Debug, Clone, Default, PartialEq)]
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 position(&self) -> Point {
44 self.bounds.position()
45 }
46
47 pub fn bounds(&self) -> Rectangle {
49 self.bounds
50 }
51
52 pub fn children(&self) -> &[Node] {
54 &self.children
55 }
56
57 pub fn align(mut self, align_x: Alignment, align_y: Alignment, space: Size) -> Self {
59 self.align_mut(align_x, align_y, space);
60 self
61 }
62
63 pub fn align_mut(&mut self, align_x: Alignment, align_y: Alignment, space: Size) {
65 match align_x {
66 Alignment::Start => {}
67 Alignment::Center => {
68 self.bounds.x += (space.width - self.bounds.width) / 2.0;
69 }
70 Alignment::End => {
71 self.bounds.x += space.width - self.bounds.width;
72 }
73 }
74
75 match align_y {
76 Alignment::Start => {}
77 Alignment::Center => {
78 self.bounds.y += (space.height - self.bounds.height) / 2.0;
79 }
80 Alignment::End => {
81 self.bounds.y += space.height - self.bounds.height;
82 }
83 }
84 }
85
86 pub fn move_to(mut self, position: impl Into<Point>) -> Self {
88 self.move_to_mut(position);
89 self
90 }
91
92 pub fn move_to_mut(&mut self, position: impl Into<Point>) {
94 let position = position.into();
95
96 self.bounds.x = position.x;
97 self.bounds.y = position.y;
98 }
99
100 pub fn translate(mut self, translation: impl Into<Vector>) -> Self {
102 self.translate_mut(translation);
103 self
104 }
105
106 pub fn translate_mut(&mut self, translation: impl Into<Vector>) {
108 self.bounds += translation.into();
109 }
110}