Skip to main content

torin/
node.rs

1pub use euclid::Rect;
2
3use crate::{
4    alignment::Alignment,
5    direction::Direction,
6    gaps::Gaps,
7    geometry::Length,
8    prelude::{
9        Content,
10        Position,
11        VisibleSize,
12    },
13    scaled::Scaled,
14    size::Size,
15};
16
17/// Node layout configuration
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19#[derive(PartialEq, Clone, Debug, Default)]
20pub struct Node {
21    /// Dimensions
22    pub width: Size,
23    pub height: Size,
24
25    // Minimum dimensions
26    pub minimum_width: Size,
27    pub minimum_height: Size,
28
29    // Maximum dimensions
30    pub maximum_width: Size,
31    pub maximum_height: Size,
32
33    // Visible dimensions
34    pub visible_width: VisibleSize,
35    pub visible_height: VisibleSize,
36
37    // Axis alignments for the children
38    pub main_alignment: Alignment,
39    pub cross_alignment: Alignment,
40
41    /// Inner padding
42    pub padding: Gaps,
43
44    /// Inner margin
45    pub margin: Gaps,
46
47    /// Inner position offsets
48    pub offset_x: Length,
49    pub offset_y: Length,
50
51    /// Direction in which it's inner Nodes will be stacked
52    pub direction: Direction,
53
54    /// Position config
55    pub position: Position,
56
57    pub content: Content,
58
59    /// A Node might depend on inner sizes but have a fixed position, like scroll views.
60    pub has_layout_references: bool,
61
62    pub spacing: Length,
63
64    /// A Node might need to relayout when its children change even if its own size
65    /// is fixed, like paragraphs with inline elements.
66    pub depends_on_inner: bool,
67}
68
69impl Scaled for Node {
70    fn scale(&mut self, scale_factor: f32) {
71        self.width.scale(scale_factor);
72        self.height.scale(scale_factor);
73        self.minimum_width.scale(scale_factor);
74        self.minimum_height.scale(scale_factor);
75        self.maximum_width.scale(scale_factor);
76        self.maximum_height.scale(scale_factor);
77        self.margin.scale(scale_factor);
78        self.padding.scale(scale_factor);
79        self.offset_x *= scale_factor;
80        self.offset_y *= scale_factor;
81        self.position.scale(scale_factor);
82        self.content.scale(scale_factor);
83        self.spacing *= scale_factor;
84    }
85}
86
87impl Node {
88    /// Create a Node with the default values
89    pub fn new() -> Self {
90        Self::default()
91    }
92
93    pub fn self_layout_eq(&self, other: &Self) -> bool {
94        // Excludes offset_x and offset_y
95        self.width == other.width
96            && self.height == other.height
97            && self.minimum_width == other.minimum_width
98            && self.minimum_height == other.minimum_height
99            && self.maximum_width == other.maximum_width
100            && self.maximum_height == other.maximum_height
101            && self.visible_width == other.visible_width
102            && self.visible_height == other.visible_height
103            && self.main_alignment == other.main_alignment
104            && self.cross_alignment == other.cross_alignment
105            && self.padding == other.padding
106            && self.margin == other.margin
107            && self.direction == other.direction
108            && self.position == other.position
109            && self.content == other.content
110            && self.has_layout_references == other.has_layout_references
111            && self.spacing == other.spacing
112            && self.depends_on_inner == other.depends_on_inner
113    }
114
115    pub fn inner_layout_eq(&self, other: &Self) -> bool {
116        // Excludes everything but offset_x and offset_y
117        self.offset_x == other.offset_x && self.offset_y == other.offset_y
118    }
119
120    /// Construct a new Node given a size and a direction
121    pub fn from_size_and_direction(width: Size, height: Size, direction: Direction) -> Self {
122        Self {
123            width,
124            height,
125            direction,
126            ..Default::default()
127        }
128    }
129
130    /// Construct a new Node given some sizes
131    pub fn from_sizes(
132        width: Size,
133        height: Size,
134        minimum_width: Size,
135        minimum_height: Size,
136        maximum_width: Size,
137        maximum_height: Size,
138    ) -> Self {
139        Self {
140            width,
141            height,
142            minimum_width,
143            minimum_height,
144            maximum_width,
145            maximum_height,
146            ..Default::default()
147        }
148    }
149
150    /// Construct a new Node given a size and a visible size
151    pub fn from_size_and_visible_size(
152        width: Size,
153        height: Size,
154        visible_width: VisibleSize,
155        visible_height: VisibleSize,
156    ) -> Self {
157        Self {
158            width,
159            height,
160            visible_width,
161            visible_height,
162            ..Default::default()
163        }
164    }
165
166    /// Construct a new Node given a size and some offsets
167    pub fn from_size_and_offset(
168        width: Size,
169        height: Size,
170        offset_x: Length,
171        offset_y: Length,
172    ) -> Self {
173        Self {
174            width,
175            height,
176            offset_x,
177            offset_y,
178            ..Default::default()
179        }
180    }
181
182    /// Construct a new Node given a size and padding
183    pub fn from_size_and_padding(width: Size, height: Size, padding: Gaps) -> Self {
184        Self {
185            width,
186            height,
187            padding,
188            ..Default::default()
189        }
190    }
191
192    /// Construct a new Node given a size, alignments and a direction
193    pub fn from_size_and_alignments_and_direction(
194        width: Size,
195        height: Size,
196        main_alignment: Alignment,
197        cross_alignment: Alignment,
198        direction: Direction,
199    ) -> Self {
200        Self {
201            width,
202            height,
203            main_alignment,
204            cross_alignment,
205            direction,
206            ..Default::default()
207        }
208    }
209
210    /// Construct a new Node given a size, alignments, direction and spacing
211    pub fn from_size_and_alignments_and_direction_and_spacing(
212        width: Size,
213        height: Size,
214        main_alignment: Alignment,
215        cross_alignment: Alignment,
216        direction: Direction,
217        spacing: Length,
218    ) -> Self {
219        Self {
220            width,
221            height,
222            main_alignment,
223            cross_alignment,
224            direction,
225            spacing,
226            ..Default::default()
227        }
228    }
229
230    /// Construct a new Node given a size and a direction
231    pub fn from_size_and_margin(width: Size, height: Size, margin: Gaps) -> Self {
232        Self {
233            width,
234            height,
235            margin,
236            ..Default::default()
237        }
238    }
239
240    /// Construct a new Node given a size and a direction and some margin,
241    pub fn from_size_and_direction_and_margin(
242        width: Size,
243        height: Size,
244        direction: Direction,
245        margin: Gaps,
246    ) -> Self {
247        Self {
248            width,
249            height,
250            margin,
251            direction,
252            ..Default::default()
253        }
254    }
255
256    /// Construct a new Node given a size, alignments and a direction
257    pub fn from_size_and_alignments_and_direction_and_padding(
258        width: Size,
259        height: Size,
260        main_alignment: Alignment,
261        cross_alignment: Alignment,
262        direction: Direction,
263        padding: Gaps,
264    ) -> Self {
265        Self {
266            width,
267            height,
268            main_alignment,
269            cross_alignment,
270            padding,
271            direction,
272            ..Default::default()
273        }
274    }
275
276    /// Construct a new Node given a size and a position
277    pub fn from_size_and_position(width: Size, height: Size, position: Position) -> Self {
278        Self {
279            width,
280            height,
281            position,
282            ..Default::default()
283        }
284    }
285
286    /// Construct a new Node given a size and content
287    pub fn from_size_and_content(width: Size, height: Size, content: Content) -> Self {
288        Self {
289            width,
290            height,
291            content,
292            ..Default::default()
293        }
294    }
295
296    /// Construct a new Node given a size and spacing
297    pub fn from_size_and_direction_and_spacing(
298        width: Size,
299        height: Size,
300        direction: Direction,
301        spacing: Length,
302    ) -> Self {
303        Self {
304            width,
305            height,
306            direction,
307            spacing,
308            ..Default::default()
309        }
310    }
311
312    /// Has properties that depend on the inner Nodes?
313    pub const fn does_depend_on_inner(&self) -> bool {
314        self.width.inner_sized()
315            || self.height.inner_sized()
316            || self.depends_on_inner
317            || self.do_inner_depend_on_parent()
318    }
319
320    /// Whether the alignments of this Node resize its children.
321    pub const fn alignments_change_sizes(&self) -> bool {
322        let (main_axis_inner, cross_axis_inner) = match self.direction {
323            Direction::Vertical => (self.height.inner_sized(), self.width.inner_sized()),
324            Direction::Horizontal => (self.width.inner_sized(), self.height.inner_sized()),
325        };
326        (self.main_alignment.is_not_start() && main_axis_inner)
327            || (self.cross_alignment.is_not_start() && cross_axis_inner)
328    }
329
330    /// Has properties that make its children dependant on it?
331    pub const fn do_inner_depend_on_parent(&self) -> bool {
332        self.cross_alignment.is_not_start()
333            || self.main_alignment.is_not_start()
334            || self.has_layout_references
335            || matches!(self.content, Content::Flex)
336    }
337}