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