tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1077HTML 1008Markdown 12TOML 83209 Definitions

⎇ 076aca1b7e6f8d292ac3a13f1565ac94aaf1f5bd src/style.rs Rust · 418 lines · 10.6 KB History Who touched what raw

Basic test generation working 076aca1b7 · Emil Sjölander ·

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