Skip to main content

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, PartialEq)]
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 position of the [`Node`].
43    pub fn position(&self) -> Point {
44        self.bounds.position()
45    }
46
47    /// Returns the bounds of the [`Node`].
48    pub fn bounds(&self) -> Rectangle {
49        self.bounds
50    }
51
52    /// Returns the children of the [`Node`].
53    pub fn children(&self) -> &[Node] {
54        &self.children
55    }
56
57    /// Aligns the [`Node`] in the given space.
58    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    /// Mutable reference version of [`Self::align`].
64    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    /// Moves the [`Node`] to the given position.
87    pub fn move_to(mut self, position: impl Into<Point>) -> Self {
88        self.move_to_mut(position);
89        self
90    }
91
92    /// Mutable reference version of [`Self::move_to`].
93    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    /// Translates the [`Node`] by the given translation.
101    pub fn translate(mut self, translation: impl Into<Vector>) -> Self {
102        self.translate_mut(translation);
103        self
104    }
105
106    /// Translates the [`Node`] by the given translation.
107    pub fn translate_mut(&mut self, translation: impl Into<Vector>) {
108        self.bounds += translation.into();
109    }
110}