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#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19#[derive(PartialEq, Clone, Debug, Default)]
20pub struct Node {
21 pub width: Size,
23 pub height: Size,
24
25 pub minimum_width: Size,
27 pub minimum_height: Size,
28
29 pub maximum_width: Size,
31 pub maximum_height: Size,
32
33 pub visible_width: VisibleSize,
35 pub visible_height: VisibleSize,
36
37 pub main_alignment: Alignment,
39 pub cross_alignment: Alignment,
40
41 pub padding: Gaps,
43
44 pub margin: Gaps,
46
47 pub offset_x: Length,
49 pub offset_y: Length,
50
51 pub direction: Direction,
53
54 pub position: Position,
56
57 pub content: Content,
58
59 pub has_layout_references: bool,
61
62 pub spacing: Length,
63
64 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 pub fn new() -> Self {
90 Self::default()
91 }
92
93 pub fn self_layout_eq(&self, other: &Self) -> bool {
94 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 self.offset_x == other.offset_x && self.offset_y == other.offset_y
118 }
119
120 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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}