1use crate::number::Number;
2
3#[derive(Copy, Clone, PartialEq, Debug)]
4pub enum AlignItems {
5 FlexStart,
6 FlexEnd,
7 Center,
8 Baseline,
9 Stretch,
10}
11
12impl Default for AlignItems {
13 fn default() -> AlignItems {
14 AlignItems::Stretch
15 }
16}
17
18#[derive(Copy, Clone, PartialEq, Debug)]
19pub enum AlignSelf {
20 Auto,
21 FlexStart,
22 FlexEnd,
23 Center,
24 Baseline,
25 Stretch,
26}
27
28impl Default for AlignSelf {
29 fn default() -> AlignSelf {
30 AlignSelf::Auto
31 }
32}
33
34#[derive(Copy, Clone, PartialEq, Debug)]
35pub enum AlignContent {
36 FlexStart,
37 FlexEnd,
38 Center,
39 Stretch,
40 SpaceBetween,
41 SpaceAround,
42}
43
44impl Default for AlignContent {
45 fn default() -> AlignContent {
46 AlignContent::Stretch
47 }
48}
49
50#[derive(Copy, Clone, PartialEq, Debug)]
51pub enum Direction {
52 Inherit,
53 LTR,
54 RTL,
55}
56
57impl Default for Direction {
58 fn default() -> Direction {
59 Direction::Inherit
60 }
61}
62
63#[derive(Copy, Clone, PartialEq, Debug)]
64pub enum Display {
65 Flex,
66 None,
67}
68
69impl Default for Display {
70 fn default() -> Display {
71 Display::Flex
72 }
73}
74
75#[derive(Copy, Clone, PartialEq, Debug)]
76pub enum FlexDirection {
77 Row,
78 Column,
79 RowReverse,
80 ColumnReverse,
81}
82
83impl Default for FlexDirection {
84 fn default() -> FlexDirection {
85 FlexDirection::Row
86 }
87}
88
89impl FlexDirection {
90 pub(crate) fn is_row(self) -> bool {
91 self == FlexDirection::Row || self == FlexDirection::RowReverse
92 }
93
94 pub(crate) fn is_column(self) -> bool {
95 self == FlexDirection::Column || self == FlexDirection::ColumnReverse
96 }
97
98 pub(crate) fn is_reverse(self) -> bool {
99 self == FlexDirection::RowReverse || self == FlexDirection::ColumnReverse
100 }
101}
102
103#[derive(Copy, Clone, PartialEq, Debug)]
104pub enum JustifyContent {
105 FlexStart,
106 FlexEnd,
107 Center,
108 SpaceBetween,
109 SpaceAround,
110 SpaceEvenly,
111}
112
113impl Default for JustifyContent {
114 fn default() -> JustifyContent {
115 JustifyContent::FlexStart
116 }
117}
118
119#[derive(Copy, Clone, PartialEq, Debug)]
120pub enum Overflow {
121 Visible,
122 Hidden,
123 Scroll,
124}
125
126impl Default for Overflow {
127 fn default() -> Overflow {
128 Overflow::Visible
129 }
130}
131
132#[derive(Copy, Clone, PartialEq, Debug)]
133pub enum Position {
134 Relative,
135 Absolute,
136}
137
138impl Default for Position {
139 fn default() -> Position {
140 Position::Relative
141 }
142}
143
144#[derive(Copy, Clone, PartialEq, Debug)]
145pub enum FlexWrap {
146 NoWrap,
147 Wrap,
148 WrapReverse,
149}
150
151impl Default for FlexWrap {
152 fn default() -> FlexWrap {
153 FlexWrap::NoWrap
154 }
155}
156
157#[derive(Copy, Clone, PartialEq, Debug)]
158pub enum Dimension {
159 Auto,
160 Points(f32),
161 Percent(f32),
162}
163
164impl Default for Dimension {
165 fn default() -> Dimension {
166 Dimension::Auto
167 }
168}
169
170impl Dimension {
171 pub(crate) fn resolve(self, parent_width: Number) -> Number {
172 match self {
173 Dimension::Points(points) => Number::Defined(points),
174 Dimension::Percent(percent) => parent_width * percent,
175 _ => Number::Undefined,
176 }
177 }
178}
179
180#[derive(Debug)]
181pub struct Edges {
182 pub start: Dimension,
183 pub end: Dimension,
184 pub top: Dimension,
185 pub bottom: Dimension,
186}
187
188impl Default for Edges {
189 fn default() -> Edges {
190 Edges {
191 start: Dimension::Points(0.0),
192 end: Dimension::Points(0.0),
193 top: Dimension::Points(0.0),
194 bottom: Dimension::Points(0.0),
195 }
196 }
197}
198
199#[derive(Debug)]
200pub struct Node {
201 pub position: Position,
202 pub direction: Direction,
203 pub flex_direction: FlexDirection,
204
205 pub flex_wrap: FlexWrap,
206 pub overflow: Overflow,
207
208 pub align_items: AlignItems,
209 pub align_self: AlignSelf,
210 pub align_content: AlignContent,
211
212 pub justify_content: JustifyContent,
213
214 pub margin: Edges,
215 pub padding: Edges,
216 pub border: Edges,
217
218 pub flex_grow: f32,
219 pub flex_shrink: f32,
220 pub flex_basis: Dimension,
221
222 pub start: Dimension,
223 pub end: Dimension,
224 pub top: Dimension,
225 pub bottom: Dimension,
226
227 pub width: Dimension,
228 pub min_width: Dimension,
229 pub max_width: Dimension,
230
231 pub height: Dimension,
232 pub min_height: Dimension,
233 pub max_height: Dimension,
234
235 pub aspect_ratio: Number,
236
237 pub children: Vec<Node>,
238}
239
240impl Default for Node {
241 fn default() -> Node {
242 Node {
243 position: Default::default(),
244 direction: Default::default(),
245 flex_direction: Default::default(),
246
247 flex_wrap: Default::default(),
248 overflow: Default::default(),
249
250 align_items: Default::default(),
251 align_self: Default::default(),
252 align_content: Default::default(),
253
254 justify_content: Default::default(),
255
256 margin: Default::default(),
257 padding: Default::default(),
258 border: Default::default(),
259
260 flex_grow: 0.0,
261 flex_shrink: 1.0,
262 flex_basis: Default::default(),
263
264 start: Default::default(),
265 end: Default::default(),
266 top: Default::default(),
267 bottom: Default::default(),
268
269 width: Default::default(),
270 min_width: Default::default(),
271 max_width: Default::default(),
272
273 height: Default::default(),
274 min_height: Default::default(),
275 max_height: Default::default(),
276
277 aspect_ratio: Default::default(),
278
279 children: vec![],
280 }
281 }
282}
283
284impl Node {
285 pub(crate) fn min_main_size(&self, direction: FlexDirection) -> Dimension {
286 match direction {
287 FlexDirection::Row | FlexDirection::RowReverse => self.min_width,
288 FlexDirection::Column | FlexDirection::ColumnReverse => self.min_height,
289 }
290 }
291
292 pub(crate) fn max_main_size(&self, direction: FlexDirection) -> Dimension {
293 match direction {
294 FlexDirection::Row | FlexDirection::RowReverse => self.max_width,
295 FlexDirection::Column | FlexDirection::ColumnReverse => self.max_height,
296 }
297 }
298
299 pub(crate) fn main_margin_start(&self, direction: FlexDirection) -> Dimension {
300 match direction {
301 FlexDirection::Row | FlexDirection::RowReverse => self.margin.start,
302 FlexDirection::Column | FlexDirection::ColumnReverse => self.margin.top,
303 }
304 }
305
306 pub(crate) fn main_margin_end(&self, direction: FlexDirection) -> Dimension {
307 match direction {
308 FlexDirection::Row | FlexDirection::RowReverse => self.margin.end,
309 FlexDirection::Column | FlexDirection::ColumnReverse => self.margin.bottom,
310 }
311 }
312
313 pub(crate) fn cross_size(&self, direction: FlexDirection) -> Dimension {
314 match direction {
315 FlexDirection::Row | FlexDirection::RowReverse => self.height,
316 FlexDirection::Column | FlexDirection::ColumnReverse => self.width,
317 }
318 }
319
320 pub(crate) fn min_cross_size(&self, direction: FlexDirection) -> Dimension {
321 match direction {
322 FlexDirection::Row | FlexDirection::RowReverse => self.min_height,
323 FlexDirection::Column | FlexDirection::ColumnReverse => self.min_width,
324 }
325 }
326
327 pub(crate) fn max_cross_size(&self, direction: FlexDirection) -> Dimension {
328 match direction {
329 FlexDirection::Row | FlexDirection::RowReverse => self.max_height,
330 FlexDirection::Column | FlexDirection::ColumnReverse => self.max_width,
331 }
332 }
333
334 pub(crate) fn cross_margin_start(&self, direction: FlexDirection) -> Dimension {
335 match direction {
336 FlexDirection::Row | FlexDirection::RowReverse => self.margin.top,
337 FlexDirection::Column | FlexDirection::ColumnReverse => self.margin.start,
338 }
339 }
340
341 pub(crate) fn cross_margin_end(&self, direction: FlexDirection) -> Dimension {
342 match direction {
343 FlexDirection::Row | FlexDirection::RowReverse => self.margin.bottom,
344 FlexDirection::Column | FlexDirection::ColumnReverse => self.margin.end,
345 }
346 }
347
348 pub(crate) fn align_self(&self, parent: &Node) -> AlignSelf {
349 if self.align_self == AlignSelf::Auto {
350 match parent.align_items {
351 AlignItems::FlexStart => AlignSelf::FlexStart,
352 AlignItems::FlexEnd => AlignSelf::FlexEnd,
353 AlignItems::Center => AlignSelf::Center,
354 AlignItems::Baseline => AlignSelf::Baseline,
355 AlignItems::Stretch => AlignSelf::Stretch,
356 }
357 } else {
358 self.align_self
359 }
360 }
361}