iced_core/layout/
node.rs

1use crate::{Alignment, Padding, Point, Rectangle, Size, Vector};
2
3/// The bounds of an element and its children.
4#[derive(Debug, Clone, Default)]
5pub struct Node {
6    bounds: Rectangle,
7    children: Vec<Node>,
8}
9
10impl Node {
11    /// Creates a new [`Node`] with the given [`Size`].
12    pub const fn new(size: Size) -> Self {
13        Self::with_children(size, Vec::new())
14    }
15
16    /// Creates a new [`Node`] with the given [`Size`] and children.
17    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    /// Creates a new [`Node`] that wraps a single child with some [`Padding`].
30    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    /// Returns the [`Size`] of the [`Node`].
38    pub fn size(&self) -> Size {
39        Size::new(self.bounds.width, self.bounds.height)
40    }
41
42    /// Returns the bounds of the [`Node`].
43    pub fn bounds(&self) -> Rectangle {
44        self.bounds
45    }
46
47    /// Returns the children of the [`Node`].
48    pub fn children(&self) -> &[Node] {
49        &self.children
50    }
51
52    /// Aligns the [`Node`] in the given space.
53    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    /// Mutable reference version of [`Self::align`].
59    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    /// Moves the [`Node`] to the given position.
82    pub fn move_to(mut self, position: impl Into<Point>) -> Self {
83        self.move_to_mut(position);
84        self
85    }
86
87    /// Mutable reference version of [`Self::move_to`].
88    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    /// Translates the [`Node`] by the given translation.
96    pub fn translate(mut self, translation: impl Into<Vector>) -> Self {
97        self.translate_mut(translation);
98        self
99    }
100
101    /// Translates the [`Node`] by the given translation.
102    pub fn translate_mut(&mut self, translation: impl Into<Vector>) {
103        self.bounds = self.bounds + translation.into();
104    }
105}