tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Sutichaykuna

⎇ c83608da41161141b31ab5bf6961352e7870c505 src/style.rs Rust · 734 siq'ikuna · 27.6 KB Ñawpa willay Pi imata llamkarqan ch'uya

Add support for align-items: space-evenly (#218) c83608da4 · Nico Burns ·

1//! A representation of [CSS layout properties](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) in Rust, used for flexbox layout
2
3use crate::geometry::{Rect, Size};
4
5/// How [`Nodes`](crate::node::Node) are aligned relative to the cross axis
6///
7/// The default behavior is [`AlignItems::Stretch`].
8///
9/// [Specification](https://www.w3.org/TR/css-flexbox-1/#align-items-property)
10#[derive(Copy, Clone, PartialEq, Eq, Debug)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub enum AlignItems {
13    /// Items are packed toward the start of the cross axis
14    FlexStart,
15    /// Items are packed toward the end of the cross axis
16    FlexEnd,
17    /// Items are packed along the center of the cross axis
18    Center,
19    /// Items are aligned such as their baselines align
20    Baseline,
21    /// Stretch to fill the container
22    Stretch,
23}
24
25impl Default for AlignItems {
26    fn default() -> Self {
27        Self::Stretch
28    }
29}
30
31/// Overrides the inherited [`AlignItems`] behavior for this node.
32///
33/// The behavior of any child nodes will be controlled by this node's [`AlignItems`] value.
34///
35/// The default behavior is [`AlignSelf::Auto`].
36///
37/// [Specification](https://www.w3.org/TR/css-flexbox-1/#align-items-property)
38#[derive(Copy, Clone, PartialEq, Eq, Debug)]
39#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
40pub enum AlignSelf {
41    /// Inherits the [`AlignItems`] behavior of the parent
42    Auto,
43    /// Items are packed toward the start of the cross axis
44    FlexStart,
45    /// Items are packed toward the end of the cross axis
46    FlexEnd,
47    /// Items are packed along the center of the cross axis
48    Center,
49    /// Items are aligned such as their baselines align
50    Baseline,
51    /// Distribute items evenly, but stretch them to fill the container
52    Stretch,
53}
54
55impl Default for AlignSelf {
56    fn default() -> Self {
57        Self::Auto
58    }
59}
60
61/// Sets the distribution of space between and around content items along the cross-axis
62///
63/// The default value is [`AlignContent::Stretch`].
64///
65/// [Specification](https://www.w3.org/TR/css-flexbox-1/#align-content-property)
66#[derive(Copy, Clone, PartialEq, Eq, Debug)]
67#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
68pub enum AlignContent {
69    /// Items are packed toward the start of the axis
70    FlexStart,
71    /// Items are packed toward the end of the axis
72    FlexEnd,
73    /// Items are centered around the middle of the axis
74    Center,
75    /// Items are stretched to fill the container
76    Stretch,
77    /// The first and last items are aligned flush with the edges of the container (no gap)
78    /// The gap between items is distributed evenly.
79    SpaceBetween,
80    /// The gap between the first and last items is exactly THE SAME as the gap between items.
81    /// The gaps are distributed evenly
82    SpaceEvenly,
83    /// The gap between the first and last items is exactly HALF the gap between items.
84    /// The gaps are distributed evenly in proportion to these ratios.
85    SpaceAround,
86}
87
88impl Default for AlignContent {
89    fn default() -> Self {
90        Self::Stretch
91    }
92}
93
94/// Sets the layout used for the children of this node
95///
96/// [`Display::Flex`] is the default value.
97#[derive(Copy, Clone, PartialEq, Eq, Debug)]
98#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
99pub enum Display {
100    /// The children will follow the flexbox layout algorithm
101    Flex,
102    /// The children will not be laid out, and will follow absolute positioning
103    None,
104}
105
106impl Default for Display {
107    fn default() -> Self {
108        Self::Flex
109    }
110}
111
112/// The direction of the flexbox layout main axis.
113///
114/// There are always two perpendicular layout axes: main (or primary) and cross (or secondary).
115/// Adding items will cause them to be positioned adjacent to each other along the main axis.
116/// By varying this value throughout your tree, you can create complex axis-aligned layouts.
117///
118/// Items are always aligned relative to the cross axis, and justified relative to the main axis.
119///
120/// The default behavior is [`FlexDirection::Row`].
121///
122/// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-direction-property)
123#[derive(Copy, Clone, PartialEq, Eq, Debug)]
124#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
125pub enum FlexDirection {
126    /// Defines +x as the main axis
127    ///
128    /// Items will be added from left to right in a row.
129    Row,
130    /// Defines +y as the main axis
131    ///
132    /// Items will be added from top to bottom in a column.
133    Column,
134    /// Defines -x as the main axis
135    ///
136    /// Items will be added from right to left in a row.
137    RowReverse,
138    /// Defines -y as the main axis
139    ///
140    /// Items will be added from bottom to top in a column.
141    ColumnReverse,
142}
143
144impl Default for FlexDirection {
145    fn default() -> Self {
146        Self::Row
147    }
148}
149
150impl FlexDirection {
151    #[inline]
152    /// Is the direction [`FlexDirection::Row`] or [`FlexDirection::RowReverse`]?
153    pub(crate) fn is_row(self) -> bool {
154        matches!(self, Self::Row | Self::RowReverse)
155    }
156
157    #[inline]
158    /// Is the direction [`FlexDirection::Column`] or [`FlexDirection::ColumnReverse`]?
159    pub(crate) fn is_column(self) -> bool {
160        matches!(self, Self::Column | Self::ColumnReverse)
161    }
162
163    #[inline]
164    /// Is the direction [`FlexDirection::RowReverse`] or [`FlexDirection::ColumnReverse`]?
165    pub(crate) fn is_reverse(self) -> bool {
166        matches!(self, Self::RowReverse | Self::ColumnReverse)
167    }
168}
169
170/// Sets the distribution of space between and around content items along the main-axis
171///
172/// The default value is [`JustifyContent::FlexStart`].
173///
174/// [Specification](https://www.w3.org/TR/css-flexbox-1/#justify-content-property)
175#[derive(Copy, Clone, PartialEq, Eq, Debug)]
176#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
177pub enum JustifyContent {
178    /// Items are packed toward the start of the main axis
179    FlexStart,
180    /// Items are packed toward the end of the main axis
181    FlexEnd,
182    /// Items are packed along the center of the main axis
183    Center,
184    /// The first and last items are aligned flush with the edges of the container (no gap)
185    /// The gaps between items are distributed evenly.
186    SpaceBetween,
187    /// The gap between the first and last items is exactly THE SAME as the gap between items.
188    /// The gaps are distributed evenly
189    SpaceEvenly,
190    /// The gap between the first and last items is exactly HALF the gap between items.
191    /// The gaps are distributed evenly in proportion to these ratios.
192    SpaceAround,
193}
194
195impl Default for JustifyContent {
196    fn default() -> Self {
197        Self::FlexStart
198    }
199}
200
201/// The positioning strategy for this item.
202///
203/// This controls both how the origin is determined for the [`Style::position`] field,
204/// and whether or not the item will be controlled by flexbox's layout algorithm.
205///
206/// WARNING: this enum follows the behavior of [CSS's `position` property](https://developer.mozilla.org/en-US/docs/Web/CSS/position),
207/// which can be unintuitive.
208///
209/// [`PositionType::Relative`] is the default value, in contrast to the default behavior in CSS.
210#[derive(Copy, Clone, PartialEq, Eq, Debug)]
211#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
212pub enum PositionType {
213    /// The offset is computed relative to the final position given by the layout algorithm.
214    /// Offsets do not affect the position of any other items; they are effectively a correction factor applied at the end.
215    Relative,
216    /// The offset is computed relative to this item's closest positioned ancestor, if any.
217    /// Otherwise, it is placed relative to the origin.
218    /// No space is created for the item in the page layout, and its size will not be altered.
219    ///
220    /// WARNING: to opt-out of layouting entirely, you must use [`Display::None`] instead on your [`Style`] object.
221    Absolute,
222}
223
224impl Default for PositionType {
225    fn default() -> Self {
226        Self::Relative
227    }
228}
229
230/// Controls whether flex items are forced onto one line or can wrap onto multiple lines.
231///
232/// Defaults to [`FlexWrap::NoWrap`]
233///
234/// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-wrap-property)
235#[derive(Copy, Clone, PartialEq, Eq, Debug)]
236#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
237pub enum FlexWrap {
238    /// Items will not wrap and stay on a single line
239    NoWrap,
240    /// Items will wrap according to this item's [`FlexDirection`]
241    Wrap,
242    /// Items will wrap in the opposite direction to this item's [`FlexDirection`]
243    WrapReverse,
244}
245
246impl Default for FlexWrap {
247    fn default() -> Self {
248        Self::NoWrap
249    }
250}
251
252/// A unit of linear measurement
253///
254/// This is commonly combined with [`Rect`], [`Point`](crate::geometry::Point) and [`Size<T>`].
255/// The default value is [`Dimension::Undefined`].
256#[derive(Copy, Clone, PartialEq, Debug)]
257#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
258pub enum Dimension {
259    /// The dimension is not given
260    Undefined,
261    /// The dimension should be automatically computed
262    Auto,
263    /// The dimension is stored in [points](https://en.wikipedia.org/wiki/Point_(typography))
264    ///
265    /// Each point is about 0.353 mm in size.
266    Points(f32),
267    /// The dimension is stored in percentage relative to the parent item.
268    Percent(f32),
269}
270
271impl Default for Dimension {
272    fn default() -> Self {
273        Self::Undefined
274    }
275}
276
277impl Dimension {
278    /// Is this value defined?
279    pub(crate) fn is_defined(self) -> bool {
280        matches!(self, Dimension::Points(_) | Dimension::Percent(_))
281    }
282}
283
284impl Default for Rect<Dimension> {
285    fn default() -> Self {
286        Self { start: Default::default(), end: Default::default(), top: Default::default(), bottom: Default::default() }
287    }
288}
289
290impl Rect<Dimension> {
291    /// Generates a [`Rect<Dimension>`] using [`Dimension::Points`] values for `start` and `top`
292    #[must_use]
293    pub const fn top_from_points(start: f32, top: f32) -> Rect<Dimension> {
294        Rect { start: Dimension::Points(start), top: Dimension::Points(top), ..Rect::AUTO }
295    }
296
297    /// Generates a [`Rect<Dimension>`] using [`Dimension::Points`] values for `end` and `bottom`
298    #[must_use]
299    pub const fn bot_from_points(end: f32, bottom: f32) -> Rect<Dimension> {
300        Rect { end: Dimension::Points(end), bottom: Dimension::Points(bottom), ..Rect::AUTO }
301    }
302
303    /// Generates a [`Rect<Dimension>`] using [`Dimension::Percent`] values for `start` and `top`
304    #[must_use]
305    pub const fn top_from_percent(start: f32, top: f32) -> Rect<Dimension> {
306        Rect { start: Dimension::Percent(start), top: Dimension::Percent(top), ..Rect::AUTO }
307    }
308
309    /// Generates a [`Rect<Dimension>`] using [`Dimension::Percent`] values for `end` and `bottom`
310    #[must_use]
311    pub const fn bot_from_percent(end: f32, bottom: f32) -> Rect<Dimension> {
312        Rect { end: Dimension::Percent(end), bottom: Dimension::Percent(bottom), ..Rect::AUTO }
313    }
314
315    /// Generates a [`Rect<Dimension>`] using [`Dimension::Undefined`] for all values
316    pub const UNDEFINED: Rect<Dimension> = Self {
317        start: Dimension::Undefined,
318        end: Dimension::Undefined,
319        top: Dimension::Undefined,
320        bottom: Dimension::Undefined,
321    };
322
323    /// Generates a [`Rect<Dimension>`] using [`Dimension::Auto`] for all values
324    pub const AUTO: Rect<Dimension> =
325        Self { start: Dimension::Auto, end: Dimension::Auto, top: Dimension::Auto, bottom: Dimension::Auto };
326
327    /// Create a new Rect with [`Dimension::Points`]
328    #[must_use]
329    pub const fn from_points(start: f32, end: f32, top: f32, bottom: f32) -> Self {
330        Rect {
331            start: Dimension::Points(start),
332            end: Dimension::Points(end),
333            top: Dimension::Points(top),
334            bottom: Dimension::Points(bottom),
335        }
336    }
337
338    /// Create a new Rect with [`Dimension::Percent`]
339    #[must_use]
340    pub const fn from_percent(start: f32, end: f32, top: f32, bottom: f32) -> Self {
341        Rect {
342            start: Dimension::Percent(start),
343            end: Dimension::Percent(end),
344            top: Dimension::Percent(top),
345            bottom: Dimension::Percent(bottom),
346        }
347    }
348}
349
350impl Default for Size<Dimension> {
351    fn default() -> Self {
352        Self { width: Dimension::Auto, height: Dimension::Auto }
353    }
354}
355
356/// The flexbox layout information for a single [`Node`](crate::node::Node).
357///
358/// The most important idea in flexbox is the notion of a "main" and "cross" axis, which are always perpendicular to each other.
359/// The orientation of these axes are controlled via the [`FlexDirection`] field of this struct.
360///
361/// This struct follows the [CSS equivalent](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox) directly;
362/// information about the behavior on the web should transfer directly.
363///
364/// Detailed information about the exact behavior of each of these fields
365/// can be found on [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS) by searching for the field name.
366/// The distinction between margin, padding and border is explained well in
367/// this [introduction to the box model](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model).
368///
369/// If the behavior does not match the flexbox layout algorithm on the web, please file a bug!
370#[derive(Copy, Clone, PartialEq, Debug)]
371#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
372#[cfg_attr(feature = "serde", serde(default))]
373pub struct FlexboxLayout {
374    /// What layout strategy should be used?
375    pub display: Display,
376    /// What should the `position` value of this struct use as a base offset?
377    pub position_type: PositionType,
378    /// Which direction does the main axis flow in?
379    pub flex_direction: FlexDirection,
380    /// Should elements wrap, or stay in a single line?
381    pub flex_wrap: FlexWrap,
382    /// How should items be aligned relative to the cross axis?
383    pub align_items: AlignItems,
384    /// Should this item violate the cross axis alignment specified by its parent's [`AlignItems`]?
385    pub align_self: AlignSelf,
386    /// How should content contained within this item be aligned relative to the cross axis?
387    pub align_content: AlignContent,
388    /// How should items be aligned relative to the main axis?
389    pub justify_content: JustifyContent,
390    /// How should the position of this element be tweaked relative to the layout defined?
391    pub position: Rect<Dimension>,
392    /// How large should the margin be on each side?
393    pub margin: Rect<Dimension>,
394    /// How large should the padding be on each side?
395    pub padding: Rect<Dimension>,
396    /// How large should the border be on each side?
397    pub border: Rect<Dimension>,
398    /// The relative rate at which this item grows when it is expanding to fill space
399    ///
400    /// 0.0 is the default value, and this value must not be negative.
401    pub flex_grow: f32,
402    /// The relative rate at which this item shrinks when it is contracting to fit into space
403    ///
404    /// 1.0 is the default value, and this value must not be negative.
405    pub flex_shrink: f32,
406    /// Sets the initial main axis size of the item
407    pub flex_basis: Dimension,
408    /// Sets the initial size of the item
409    // TODO: why does this exist as distinct from flex_basis? How do they interact?
410    pub size: Size<Dimension>,
411    /// Controls the minimum size of the item
412    pub min_size: Size<Dimension>,
413    /// Controls the maximum size of the item
414    pub max_size: Size<Dimension>,
415    /// Sets the preferred aspect ratio for the item
416    ///
417    /// The ratio is calculated as width divided by height.
418    pub aspect_ratio: Option<f32>,
419}
420
421impl FlexboxLayout {
422    /// The [`Default`] layout, in a form that can be used in const functions
423    pub const DEFAULT: FlexboxLayout = FlexboxLayout {
424        display: Display::Flex,
425        position_type: PositionType::Relative,
426        flex_direction: FlexDirection::Row,
427        flex_wrap: FlexWrap::NoWrap,
428        align_items: AlignItems::Stretch,
429        align_self: AlignSelf::Auto,
430        align_content: AlignContent::Stretch,
431        justify_content: JustifyContent::FlexStart,
432        position: Rect::UNDEFINED,
433        margin: Rect::UNDEFINED,
434        padding: Rect::UNDEFINED,
435        border: Rect::UNDEFINED,
436        flex_grow: 0.0,
437        flex_shrink: 1.0,
438        flex_basis: Dimension::Auto,
439        size: Size::AUTO,
440        min_size: Size::AUTO,
441        max_size: Size::AUTO,
442        aspect_ratio: None,
443    };
444}
445
446impl Default for FlexboxLayout {
447    fn default() -> Self {
448        FlexboxLayout::DEFAULT
449    }
450}
451
452impl FlexboxLayout {
453    /// If the `direction` is row-oriented, the min width. Otherwise the min height
454    pub(crate) fn min_main_size(&self, direction: FlexDirection) -> Dimension {
455        if direction.is_row() {
456            self.min_size.width
457        } else {
458            self.min_size.height
459        }
460    }
461
462    /// If the `direction` is row-oriented, the max width. Otherwise the max height
463    pub(crate) fn max_main_size(&self, direction: FlexDirection) -> Dimension {
464        if direction.is_row() {
465            self.max_size.width
466        } else {
467            self.max_size.height
468        }
469    }
470
471    /// If the `direction` is row-oriented, the margin start. Otherwise the margin top
472    pub(crate) fn main_margin_start(&self, direction: FlexDirection) -> Dimension {
473        if direction.is_row() {
474            self.margin.start
475        } else {
476            self.margin.top
477        }
478    }
479
480    /// If the `direction` is row-oriented, the margin end. Otherwise the margin bottom
481    pub(crate) fn main_margin_end(&self, direction: FlexDirection) -> Dimension {
482        if direction.is_row() {
483            self.margin.end
484        } else {
485            self.margin.bottom
486        }
487    }
488
489    /// If the `direction` is row-oriented, the height. Otherwise the width
490    pub(crate) fn cross_size(&self, direction: FlexDirection) -> Dimension {
491        if direction.is_row() {
492            self.size.height
493        } else {
494            self.size.width
495        }
496    }
497
498    /// If the `direction` is row-oriented, the min height. Otherwise the min width
499    pub(crate) fn min_cross_size(&self, direction: FlexDirection) -> Dimension {
500        if direction.is_row() {
501            self.min_size.height
502        } else {
503            self.min_size.width
504        }
505    }
506
507    /// If the `direction` is row-oriented, the max height. Otherwise the max width
508    pub(crate) fn max_cross_size(&self, direction: FlexDirection) -> Dimension {
509        if direction.is_row() {
510            self.max_size.height
511        } else {
512            self.max_size.width
513        }
514    }
515
516    /// If the `direction` is row-oriented, the margin top. Otherwise the margin start
517    pub(crate) fn cross_margin_start(&self, direction: FlexDirection) -> Dimension {
518        if direction.is_row() {
519            self.margin.top
520        } else {
521            self.margin.start
522        }
523    }
524
525    /// If the `direction` is row-oriented, the margin bottom. Otherwise the margin end
526    pub(crate) fn cross_margin_end(&self, direction: FlexDirection) -> Dimension {
527        if direction.is_row() {
528            self.margin.bottom
529        } else {
530            self.margin.end
531        }
532    }
533
534    /// Computes the final alignment of this item based on the parent's [`AlignItems`] and this item's [`AlignSelf`]
535    pub(crate) fn align_self(&self, parent: &FlexboxLayout) -> AlignSelf {
536        // FUTURE WARNING: This function should never return AlignSelf::Auto
537        // See #169 https://github.com/DioxusLabs/taffy/pull/169#issuecomment-1157698840
538
539        if self.align_self == AlignSelf::Auto {
540            match parent.align_items {
541                AlignItems::FlexStart => AlignSelf::FlexStart,
542                AlignItems::FlexEnd => AlignSelf::FlexEnd,
543                AlignItems::Center => AlignSelf::Center,
544                AlignItems::Baseline => AlignSelf::Baseline,
545                AlignItems::Stretch => AlignSelf::Stretch,
546            }
547        } else {
548            self.align_self
549        }
550    }
551}
552
553#[allow(clippy::bool_assert_comparison)]
554#[cfg(test)]
555mod tests {
556    use super::FlexboxLayout;
557
558    #[test]
559    fn defaults_match() {
560        let old_defaults = FlexboxLayout {
561            display: Default::default(),
562            position_type: Default::default(),
563            flex_direction: Default::default(),
564            flex_wrap: Default::default(),
565            align_items: Default::default(),
566            align_self: Default::default(),
567            align_content: Default::default(),
568            justify_content: Default::default(),
569            position: Default::default(),
570            margin: Default::default(),
571            padding: Default::default(),
572            border: Default::default(),
573            flex_grow: 0.0,
574            flex_shrink: 1.0,
575            flex_basis: super::Dimension::Auto,
576            size: Default::default(),
577            min_size: Default::default(),
578            max_size: Default::default(),
579            aspect_ratio: Default::default(),
580        };
581
582        assert_eq!(FlexboxLayout::DEFAULT, FlexboxLayout::default());
583        assert_eq!(FlexboxLayout::DEFAULT, old_defaults);
584    }
585
586    mod test_flex_direction {
587        use crate::style::*;
588
589        #[test]
590        fn flex_direction_is_row() {
591            assert_eq!(FlexDirection::Row.is_row(), true);
592            assert_eq!(FlexDirection::RowReverse.is_row(), true);
593            assert_eq!(FlexDirection::Column.is_row(), false);
594            assert_eq!(FlexDirection::ColumnReverse.is_row(), false);
595        }
596
597        #[test]
598        fn flex_direction_is_column() {
599            assert_eq!(FlexDirection::Row.is_column(), false);
600            assert_eq!(FlexDirection::RowReverse.is_column(), false);
601            assert_eq!(FlexDirection::Column.is_column(), true);
602            assert_eq!(FlexDirection::ColumnReverse.is_column(), true);
603        }
604
605        #[test]
606        fn flex_direction_is_reverse() {
607            assert_eq!(FlexDirection::Row.is_reverse(), false);
608            assert_eq!(FlexDirection::RowReverse.is_reverse(), true);
609            assert_eq!(FlexDirection::Column.is_reverse(), false);
610            assert_eq!(FlexDirection::ColumnReverse.is_reverse(), true);
611        }
612    }
613
614    mod test_flexbox_layout {
615        use crate::style::*;
616
617        fn layout_from_align_items(align: AlignItems) -> FlexboxLayout {
618            FlexboxLayout { align_items: align, ..Default::default() }
619        }
620
621        fn layout_from_align_self(align: AlignSelf) -> FlexboxLayout {
622            FlexboxLayout { align_self: align, ..Default::default() }
623        }
624
625        #[test]
626        fn flexbox_layout_min_main_size() {
627            let layout = FlexboxLayout { min_size: Size::from_points(1.0, 2.0), ..Default::default() };
628            assert_eq!(layout.min_main_size(FlexDirection::Row), Dimension::Points(1.0));
629            assert_eq!(layout.min_main_size(FlexDirection::Column), Dimension::Points(2.0));
630        }
631
632        #[test]
633        fn flexbox_layout_max_main_size() {
634            let layout = FlexboxLayout { max_size: Size::from_points(1.0, 2.0), ..Default::default() };
635            assert_eq!(layout.max_main_size(FlexDirection::Row), Dimension::Points(1.0));
636            assert_eq!(layout.max_main_size(FlexDirection::Column), Dimension::Points(2.0));
637        }
638
639        #[test]
640        fn flexbox_layout_main_margin_start() {
641            let layout = FlexboxLayout { margin: Rect::top_from_points(2.0, 1.0), ..Default::default() };
642            assert_eq!(layout.main_margin_start(FlexDirection::Row), Dimension::Points(2.0));
643            assert_eq!(layout.main_margin_start(FlexDirection::Column), Dimension::Points(1.0));
644        }
645
646        #[test]
647        fn flexbox_layout_main_margin_end() {
648            let layout = FlexboxLayout { margin: Rect::bot_from_points(2.0, 1.0), ..Default::default() };
649            assert_eq!(layout.main_margin_end(FlexDirection::Row), Dimension::Points(2.0));
650            assert_eq!(layout.main_margin_end(FlexDirection::Column), Dimension::Points(1.0));
651        }
652
653        #[test]
654        fn flexbox_layout_cross_size() {
655            let layout = FlexboxLayout { size: Size::from_points(1.0, 2.0), ..Default::default() };
656            assert_eq!(layout.cross_size(FlexDirection::Row), Dimension::Points(2.0));
657            assert_eq!(layout.cross_size(FlexDirection::Column), Dimension::Points(1.0));
658        }
659
660        #[test]
661        fn flexbox_layout_min_cross_size() {
662            let layout = FlexboxLayout { min_size: Size::from_points(1.0, 2.0), ..Default::default() };
663            assert_eq!(layout.min_cross_size(FlexDirection::Row), Dimension::Points(2.0));
664            assert_eq!(layout.min_cross_size(FlexDirection::Column), Dimension::Points(1.0));
665        }
666
667        #[test]
668        fn flexbox_layout_max_cross_size() {
669            let layout = FlexboxLayout { max_size: Size::from_points(1.0, 2.0), ..Default::default() };
670            assert_eq!(layout.max_cross_size(FlexDirection::Row), Dimension::Points(2.0));
671            assert_eq!(layout.max_cross_size(FlexDirection::Column), Dimension::Points(1.0));
672        }
673
674        #[test]
675        fn flexbox_layout_cross_margin_start() {
676            let layout = FlexboxLayout { margin: Rect::top_from_points(2.0, 1.0), ..Default::default() };
677            assert_eq!(layout.cross_margin_start(FlexDirection::Row), Dimension::Points(1.0));
678            assert_eq!(layout.cross_margin_start(FlexDirection::Column), Dimension::Points(2.0));
679        }
680
681        #[test]
682        fn flexbox_layout_cross_margin_end() {
683            let layout = FlexboxLayout { margin: Rect::bot_from_points(2.0, 1.0), ..Default::default() };
684            assert_eq!(layout.cross_margin_end(FlexDirection::Row), Dimension::Points(1.0));
685            assert_eq!(layout.cross_margin_end(FlexDirection::Column), Dimension::Points(2.0));
686        }
687
688        #[test]
689        fn flexbox_layout_align_self_auto() {
690            let parent = layout_from_align_items(AlignItems::FlexStart);
691            let layout = layout_from_align_self(AlignSelf::Auto);
692            assert_eq!(layout.align_self(&parent), AlignSelf::FlexStart);
693
694            let parent = layout_from_align_items(AlignItems::FlexEnd);
695            let layout = layout_from_align_self(AlignSelf::Auto);
696            assert_eq!(layout.align_self(&parent), AlignSelf::FlexEnd);
697
698            let parent = layout_from_align_items(AlignItems::Center);
699            let layout = layout_from_align_self(AlignSelf::Auto);
700            assert_eq!(layout.align_self(&parent), AlignSelf::Center);
701
702            let parent = layout_from_align_items(AlignItems::Baseline);
703            let layout = layout_from_align_self(AlignSelf::Auto);
704            assert_eq!(layout.align_self(&parent), AlignSelf::Baseline);
705
706            let parent = layout_from_align_items(AlignItems::Stretch);
707            let layout = layout_from_align_self(AlignSelf::Auto);
708            assert_eq!(layout.align_self(&parent), AlignSelf::Stretch);
709        }
710
711        #[test]
712        fn align_self() {
713            let parent = layout_from_align_items(AlignItems::FlexEnd);
714            let layout = layout_from_align_self(AlignSelf::FlexStart);
715            assert_eq!(layout.align_self(&parent), AlignSelf::FlexStart);
716
717            let parent = layout_from_align_items(AlignItems::FlexStart);
718            let layout = layout_from_align_self(AlignSelf::FlexEnd);
719            assert_eq!(layout.align_self(&parent), AlignSelf::FlexEnd);
720
721            let parent = layout_from_align_items(AlignItems::FlexStart);
722            let layout = layout_from_align_self(AlignSelf::Center);
723            assert_eq!(layout.align_self(&parent), AlignSelf::Center);
724
725            let parent = layout_from_align_items(AlignItems::FlexStart);
726            let layout = layout_from_align_self(AlignSelf::Baseline);
727            assert_eq!(layout.align_self(&parent), AlignSelf::Baseline);
728
729            let parent = layout_from_align_items(AlignItems::FlexStart);
730            let layout = layout_from_align_self(AlignSelf::Stretch);
731            assert_eq!(layout.align_self(&parent), AlignSelf::Stretch);
732        }
733    }
734}