1use super::{AlignContent, AlignItems, AlignSelf, CoreStyle, Dimension, JustifyContent, LengthPercentage, Style};
3use crate::geometry::Size;
4
5pub trait FlexboxContainerStyle: CoreStyle {
7 #[inline(always)]
9 fn flex_direction(&self) -> FlexDirection {
10 Style::<Self::CustomIdent>::DEFAULT.flex_direction
11 }
12 #[inline(always)]
14 fn flex_wrap(&self) -> FlexWrap {
15 Style::<Self::CustomIdent>::DEFAULT.flex_wrap
16 }
17
18 #[inline(always)]
20 fn gap(&self) -> Size<LengthPercentage> {
21 Style::<Self::CustomIdent>::DEFAULT.gap
22 }
23
24 #[inline(always)]
28 fn align_content(&self) -> Option<AlignContent> {
29 Style::<Self::CustomIdent>::DEFAULT.align_content
30 }
31 #[inline(always)]
33 fn align_items(&self) -> Option<AlignItems> {
34 Style::<Self::CustomIdent>::DEFAULT.align_items
35 }
36 #[inline(always)]
38 fn justify_content(&self) -> Option<JustifyContent> {
39 Style::<Self::CustomIdent>::DEFAULT.justify_content
40 }
41}
42
43pub trait FlexboxItemStyle: CoreStyle {
45 #[inline(always)]
47 fn flex_basis(&self) -> Dimension {
48 Style::<Self::CustomIdent>::DEFAULT.flex_basis
49 }
50 #[inline(always)]
52 fn flex_grow(&self) -> f32 {
53 Style::<Self::CustomIdent>::DEFAULT.flex_grow
54 }
55 #[inline(always)]
57 fn flex_shrink(&self) -> f32 {
58 Style::<Self::CustomIdent>::DEFAULT.flex_shrink
59 }
60
61 #[inline(always)]
64 fn align_self(&self) -> Option<AlignSelf> {
65 Style::<Self::CustomIdent>::DEFAULT.align_self
66 }
67}
68
69use crate::geometry::AbsoluteAxis;
70
71#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
77#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
78pub enum FlexWrap {
79 #[default]
81 NoWrap,
82 Wrap,
84 WrapReverse,
86}
87
88#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
101pub enum FlexDirection {
102 #[default]
106 Row,
107 Column,
111 RowReverse,
115 ColumnReverse,
119}
120
121impl FlexDirection {
122 #[inline]
123 pub(crate) fn is_row(self) -> bool {
125 matches!(self, Self::Row | Self::RowReverse)
126 }
127
128 #[inline]
129 pub(crate) fn is_column(self) -> bool {
131 matches!(self, Self::Column | Self::ColumnReverse)
132 }
133
134 #[inline]
135 pub(crate) fn is_reverse(self) -> bool {
137 matches!(self, Self::RowReverse | Self::ColumnReverse)
138 }
139
140 #[inline]
141 pub(crate) fn main_axis(self) -> AbsoluteAxis {
143 match self {
144 Self::Row | Self::RowReverse => AbsoluteAxis::Horizontal,
145 Self::Column | Self::ColumnReverse => AbsoluteAxis::Vertical,
146 }
147 }
148
149 #[inline]
150 pub(crate) fn cross_axis(self) -> AbsoluteAxis {
152 match self {
153 Self::Row | Self::RowReverse => AbsoluteAxis::Vertical,
154 Self::Column | Self::ColumnReverse => AbsoluteAxis::Horizontal,
155 }
156 }
157}
158
159#[cfg(test)]
160mod tests {
161 mod test_flex_direction {
162 use crate::style::*;
163
164 #[test]
165 fn flex_direction_is_row() {
166 assert!(FlexDirection::Row.is_row());
167 assert!(FlexDirection::RowReverse.is_row());
168 assert!(!FlexDirection::Column.is_row());
169 assert!(!FlexDirection::ColumnReverse.is_row());
170 }
171
172 #[test]
173 fn flex_direction_is_column() {
174 assert!(!FlexDirection::Row.is_column());
175 assert!(!FlexDirection::RowReverse.is_column());
176 assert!(FlexDirection::Column.is_column());
177 assert!(FlexDirection::ColumnReverse.is_column());
178 }
179
180 #[test]
181 fn flex_direction_is_reverse() {
182 assert!(!FlexDirection::Row.is_reverse());
183 assert!(FlexDirection::RowReverse.is_reverse());
184 assert!(!FlexDirection::Column.is_reverse());
185 assert!(FlexDirection::ColumnReverse.is_reverse());
186 }
187 }
188}