1//! Style types for Flexbox layout
23use crate::geometry::AbsoluteAxis;
45/// Controls whether flex items are forced onto one line or can wrap onto multiple lines.
6///
7/// Defaults to [`FlexWrap::NoWrap`]
8///
9/// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-wrap-property)
10#[derive(Copy, Clone, PartialEq, Eq, Debug)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub enum FlexWrap {
13/// Items will not wrap and stay on a single line
14NoWrap,
15/// Items will wrap according to this item's [`FlexDirection`]
16Wrap,
17/// Items will wrap in the opposite direction to this item's [`FlexDirection`]
18WrapReverse,
19}
2021impl Default for FlexWrap {
22fn default() -> Self {
23Self::NoWrap
24 }
25}
2627/// The direction of the flexbox layout main axis.
28///
29/// There are always two perpendicular layout axes: main (or primary) and cross (or secondary).
30/// Adding items will cause them to be positioned adjacent to each other along the main axis.
31/// By varying this value throughout your tree, you can create complex axis-aligned layouts.
32///
33/// Items are always aligned relative to the cross axis, and justified relative to the main axis.
34///
35/// The default behavior is [`FlexDirection::Row`].
36///
37/// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-direction-property)
38#[derive(Copy, Clone, PartialEq, Eq, Debug)]
39#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
40pub enum FlexDirection {
41/// Defines +x as the main axis
42 ///
43 /// Items will be added from left to right in a row.
44Row,
45/// Defines +y as the main axis
46 ///
47 /// Items will be added from top to bottom in a column.
48Column,
49/// Defines -x as the main axis
50 ///
51 /// Items will be added from right to left in a row.
52RowReverse,
53/// Defines -y as the main axis
54 ///
55 /// Items will be added from bottom to top in a column.
56ColumnReverse,
57}
5859impl Default for FlexDirection {
60fn default() -> Self {
61Self::Row
62 }
63}
6465impl FlexDirection {
66#[inline]
67/// Is the direction [`FlexDirection::Row`] or [`FlexDirection::RowReverse`]?
68pub(crate) fn is_row(self) -> bool {
69matches!(self, Self::Row | Self::RowReverse)
70 }
7172#[inline]
73/// Is the direction [`FlexDirection::Column`] or [`FlexDirection::ColumnReverse`]?
74pub(crate) fn is_column(self) -> bool {
75matches!(self, Self::Column | Self::ColumnReverse)
76 }
7778#[inline]
79/// Is the direction [`FlexDirection::RowReverse`] or [`FlexDirection::ColumnReverse`]?
80pub(crate) fn is_reverse(self) -> bool {
81matches!(self, Self::RowReverse | Self::ColumnReverse)
82 }
8384#[inline]
85/// The `AbsoluteAxis` that corresponds to the main axis
86pub(crate) fn main_axis(self) -> AbsoluteAxis {
87match self {
88Self::Row | Self::RowReverse => AbsoluteAxis::Horizontal,
89Self::Column | Self::ColumnReverse => AbsoluteAxis::Vertical,
90 }
91 }
9293#[inline]
94/// The `AbsoluteAxis` that corresponds to the cross axis
95pub(crate) fn cross_axis(self) -> AbsoluteAxis {
96match self {
97Self::Row | Self::RowReverse => AbsoluteAxis::Vertical,
98Self::Column | Self::ColumnReverse => AbsoluteAxis::Horizontal,
99 }
100 }
101}
102103#[cfg(test)]
104mod tests {
105mod test_flex_direction {
106use crate::style::*;
107108#[test]
109fn flex_direction_is_row() {
110assert_eq!(FlexDirection::Row.is_row(), true);
111assert_eq!(FlexDirection::RowReverse.is_row(), true);
112assert_eq!(FlexDirection::Column.is_row(), false);
113assert_eq!(FlexDirection::ColumnReverse.is_row(), false);
114 }
115116#[test]
117fn flex_direction_is_column() {
118assert_eq!(FlexDirection::Row.is_column(), false);
119assert_eq!(FlexDirection::RowReverse.is_column(), false);
120assert_eq!(FlexDirection::Column.is_column(), true);
121assert_eq!(FlexDirection::ColumnReverse.is_column(), true);
122 }
123124#[test]
125fn flex_direction_is_reverse() {
126assert_eq!(FlexDirection::Row.is_reverse(), false);
127assert_eq!(FlexDirection::RowReverse.is_reverse(), true);
128assert_eq!(FlexDirection::Column.is_reverse(), false);
129assert_eq!(FlexDirection::ColumnReverse.is_reverse(), true);
130 }
131 }
132}