ailloli_ui_core/style/flex_item_style.rs
1//! Per-child growth, shrinkage, basis, and cross-axis override.
2
3use super::{AlignItems, Length};
4
5/// Flex item style for a child of `Row` / `Column` (`flex_grow`, `align_self`, …).
6///
7/// # Examples
8///
9/// ```
10/// use ailloli_ui_core::style::{AlignItems, FlexItemStyle};
11/// let item = FlexItemStyle::new().flex_grow(1.0).align_self(AlignItems::Center);
12/// assert_eq!(item.flex_grow, 1.0);
13/// ```
14#[derive(Debug, Clone, Copy, PartialEq, Default)]
15pub struct FlexItemStyle {
16 /// Non-negative share of remaining main-axis space; default `0.0`.
17 pub flex_grow: f32,
18 /// Non-negative shrink weight under deficit; default `0.0`.
19 pub flex_shrink: f32,
20 /// Preferred main-axis size before grow/shrink; default [`Length::Auto`].
21 pub flex_basis: Length,
22 /// Child-specific cross-axis alignment, or `None` to inherit the container.
23 pub align_self: Option<AlignItems>,
24}
25
26impl FlexItemStyle {
27 /// Creates a non-growing, non-shrinking item with automatic basis/alignment.
28 ///
29 /// # Examples
30 ///
31 /// ```
32 /// use ailloli_ui_core::style::{FlexItemStyle, Length};
33 /// assert_eq!(FlexItemStyle::new().flex_basis, Length::Auto);
34 /// ```
35 pub fn new() -> Self {
36 Self::default()
37 }
38
39 /// Sets the grow factor, clamped to at least zero.
40 ///
41 /// NaN and negative infinity become zero through floating-point `max`
42 /// semantics; positive infinity remains infinite.
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// use ailloli_ui_core::style::FlexItemStyle;
48 /// assert_eq!(FlexItemStyle::new().flex_grow(-1.0).flex_grow, 0.0);
49 /// ```
50 pub fn flex_grow(mut self, value: f32) -> Self {
51 self.flex_grow = value.max(0.0);
52 self
53 }
54
55 /// Sets the shrink factor, clamped to at least zero.
56 ///
57 /// NaN and negative infinity become zero through floating-point `max`
58 /// semantics; positive infinity remains infinite.
59 ///
60 /// # Examples
61 ///
62 /// ```
63 /// use ailloli_ui_core::style::FlexItemStyle;
64 /// assert_eq!(FlexItemStyle::new().flex_shrink(2.0).flex_shrink, 2.0);
65 /// ```
66 pub fn flex_shrink(mut self, value: f32) -> Self {
67 self.flex_shrink = value.max(0.0);
68 self
69 }
70
71 /// Replaces the preferred main-axis basis.
72 ///
73 /// # Examples
74 ///
75 /// ```
76 /// use ailloli_ui_core::style::{FlexItemStyle, Length};
77 /// assert_eq!(FlexItemStyle::new().flex_basis(20.0).flex_basis, Length::Px(20.0));
78 /// ```
79 pub fn flex_basis(mut self, value: impl Into<Length>) -> Self {
80 self.flex_basis = value.into();
81 self
82 }
83
84 /// Overrides the container's cross-axis alignment for this child.
85 ///
86 /// # Examples
87 ///
88 /// ```
89 /// use ailloli_ui_core::style::{AlignItems, FlexItemStyle};
90 /// assert_eq!(FlexItemStyle::new().align_self(AlignItems::End).align_self, Some(AlignItems::End));
91 /// ```
92 pub fn align_self(mut self, value: AlignItems) -> Self {
93 self.align_self = Some(value);
94 self
95 }
96}