iced_core/
layout.rs

1//! Position your widgets properly.
2mod limits;
3mod node;
4
5pub mod flex;
6
7pub use limits::Limits;
8pub use node::Node;
9
10use crate::{Length, Padding, Point, Rectangle, Size, Vector};
11
12/// The bounds of a [`Node`] and its children, using absolute coordinates.
13#[derive(Debug, Clone, Copy)]
14pub struct Layout<'a> {
15    position: Point,
16    node: &'a Node,
17}
18
19impl<'a> Layout<'a> {
20    /// Creates a new [`Layout`] for the given [`Node`] at the origin.
21    pub fn new(node: &'a Node) -> Self {
22        Self::with_offset(Vector::new(0.0, 0.0), node)
23    }
24
25    /// Creates a new [`Layout`] for the given [`Node`] with the provided offset
26    /// from the origin.
27    pub fn with_offset(offset: Vector, node: &'a Node) -> Self {
28        let bounds = node.bounds();
29
30        Self {
31            position: Point::new(bounds.x, bounds.y) + offset,
32            node,
33        }
34    }
35
36    /// Returns the position of the [`Layout`].
37    pub fn position(&self) -> Point {
38        self.position
39    }
40
41    /// Returns the bounds of the [`Layout`].
42    ///
43    /// The returned [`Rectangle`] describes the position and size of a
44    /// [`Node`].
45    pub fn bounds(&self) -> Rectangle {
46        let bounds = self.node.bounds();
47
48        Rectangle {
49            x: self.position.x,
50            y: self.position.y,
51            width: bounds.width,
52            height: bounds.height,
53        }
54    }
55
56    /// Returns an iterator over the children of this [`Layout`].
57    pub fn children(self) -> impl DoubleEndedIterator<Item = Layout<'a>> {
58        self.node.children().iter().map(move |node| {
59            Layout::with_offset(
60                Vector::new(self.position.x, self.position.y),
61                node,
62            )
63        })
64    }
65
66    /// Returns the [`Layout`] of the child at the given index.
67    ///
68    /// This can be useful if you ever need to access children out of order
69    /// for layering purposes.
70    ///
71    /// # Panics
72    /// Panics if index is out of bounds.
73    pub fn child(self, index: usize) -> Layout<'a> {
74        let node = &self.node.children()[index];
75
76        Layout::with_offset(Vector::new(self.position.x, self.position.y), node)
77    }
78}
79
80/// Produces a [`Node`] with two children nodes one right next to each other.
81pub fn next_to_each_other(
82    limits: &Limits,
83    spacing: f32,
84    left: impl FnOnce(&Limits) -> Node,
85    right: impl FnOnce(&Limits) -> Node,
86) -> Node {
87    let left_node = left(limits);
88    let left_size = left_node.size();
89
90    let right_limits = limits.shrink(Size::new(left_size.width + spacing, 0.0));
91
92    let right_node = right(&right_limits);
93    let right_size = right_node.size();
94
95    let (left_y, right_y) = if left_size.height > right_size.height {
96        (0.0, (left_size.height - right_size.height) / 2.0)
97    } else {
98        ((right_size.height - left_size.height) / 2.0, 0.0)
99    };
100
101    Node::with_children(
102        Size::new(
103            left_size.width + spacing + right_size.width,
104            left_size.height.max(right_size.height),
105        ),
106        vec![
107            left_node.move_to(Point::new(0.0, left_y)),
108            right_node.move_to(Point::new(left_size.width + spacing, right_y)),
109        ],
110    )
111}
112
113/// Computes the resulting [`Node`] that fits the [`Limits`] given
114/// some width and height requirements and no intrinsic size.
115pub fn atomic(
116    limits: &Limits,
117    width: impl Into<Length>,
118    height: impl Into<Length>,
119) -> Node {
120    let width = width.into();
121    let height = height.into();
122
123    Node::new(limits.resolve(width, height, Size::ZERO))
124}
125
126/// Computes the resulting [`Node`] that fits the [`Limits`] given
127/// some width and height requirements and a closure that produces
128/// the intrinsic [`Size`] inside the given [`Limits`].
129pub fn sized(
130    limits: &Limits,
131    width: impl Into<Length>,
132    height: impl Into<Length>,
133    f: impl FnOnce(&Limits) -> Size,
134) -> Node {
135    let width = width.into();
136    let height = height.into();
137
138    let limits = limits.width(width).height(height);
139    let intrinsic_size = f(&limits);
140
141    Node::new(limits.resolve(width, height, intrinsic_size))
142}
143
144/// Computes the resulting [`Node`] that fits the [`Limits`] given
145/// some width and height requirements and a closure that produces
146/// the content [`Node`] inside the given [`Limits`].
147pub fn contained(
148    limits: &Limits,
149    width: impl Into<Length>,
150    height: impl Into<Length>,
151    f: impl FnOnce(&Limits) -> Node,
152) -> Node {
153    let width = width.into();
154    let height = height.into();
155
156    let limits = limits.width(width).height(height);
157    let content = f(&limits);
158
159    Node::with_children(
160        limits.resolve(width, height, content.size()),
161        vec![content],
162    )
163}
164
165/// Computes the [`Node`] that fits the [`Limits`] given some width, height, and
166/// [`Padding`] requirements and a closure that produces the content [`Node`]
167/// inside the given [`Limits`].
168pub fn padded(
169    limits: &Limits,
170    width: impl Into<Length>,
171    height: impl Into<Length>,
172    padding: impl Into<Padding>,
173    layout: impl FnOnce(&Limits) -> Node,
174) -> Node {
175    positioned(limits, width, height, padding, layout, |content, _| content)
176}
177
178/// Computes a [`padded`] [`Node`] with a positioning step.
179pub fn positioned(
180    limits: &Limits,
181    width: impl Into<Length>,
182    height: impl Into<Length>,
183    padding: impl Into<Padding>,
184    layout: impl FnOnce(&Limits) -> Node,
185    position: impl FnOnce(Node, Size) -> Node,
186) -> Node {
187    let width = width.into();
188    let height = height.into();
189    let padding = padding.into();
190
191    let limits = limits.width(width).height(height);
192    let content = layout(&limits.shrink(padding));
193    let padding = padding.fit(content.size(), limits.max());
194
195    let size = limits
196        .shrink(padding)
197        .resolve(width, height, content.size());
198
199    Node::with_children(
200        size.expand(padding),
201        vec![position(content.move_to((padding.left, padding.top)), size)],
202    )
203}