iced_core/layout/
node.rs
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(
54 mut self,
55 horizontal_alignment: Alignment,
56 vertical_alignment: Alignment,
57 space: Size,
58 ) -> Self {
59 self.align_mut(horizontal_alignment, vertical_alignment, space);
60 self
61 }
62
63 pub fn align_mut(
65 &mut self,
66 horizontal_alignment: Alignment,
67 vertical_alignment: Alignment,
68 space: Size,
69 ) {
70 match horizontal_alignment {
71 Alignment::Start => {}
72 Alignment::Center => {
73 self.bounds.x += (space.width - self.bounds.width) / 2.0;
74 }
75 Alignment::End => {
76 self.bounds.x += space.width - self.bounds.width;
77 }
78 }
79
80 match vertical_alignment {
81 Alignment::Start => {}
82 Alignment::Center => {
83 self.bounds.y += (space.height - self.bounds.height) / 2.0;
84 }
85 Alignment::End => {
86 self.bounds.y += space.height - self.bounds.height;
87 }
88 }
89 }
90
91 pub fn move_to(mut self, position: impl Into<Point>) -> Self {
93 self.move_to_mut(position);
94 self
95 }
96
97 pub fn move_to_mut(&mut self, position: impl Into<Point>) {
99 let position = position.into();
100
101 self.bounds.x = position.x;
102 self.bounds.y = position.y;
103 }
104
105 pub fn translate(mut self, translation: impl Into<Vector>) -> Self {
107 self.translate_mut(translation);
108 self
109 }
110
111 pub fn translate_mut(&mut self, translation: impl Into<Vector>) {
113 self.bounds = self.bounds + translation.into();
114 }
115}