gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 227Rust 7TOML 3CSS 1307 Sutichaykuna

⎇ 0.3.x src/style/flex.rs Rust · 112 siq'ikuna · 3.6 KB Ñawpa willay Pi imata llamkarqan ch'uya

Cleanup/old style helpers (#293) 0196192db · Nico Burns ·

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