Skip to main content

kova_native_layout/
style.rs

1//! Layout style vocabulary (independent of the underlying solver).
2
3use kova_native_core::Edges;
4
5/// A length that can be absolute, relative to the parent, or automatic.
6#[derive(Clone, Copy, Debug, PartialEq, Default)]
7pub enum Length {
8    #[default]
9    Auto,
10    /// Logical pixels.
11    Px(f32),
12    /// Fraction of the parent's size (`0.5` = 50%).
13    Relative(f32),
14}
15
16/// Logical pixels.
17pub const fn px(v: f32) -> Length {
18    Length::Px(v)
19}
20
21/// Percentage of the parent (`pct(50.)` = half).
22pub fn pct(v: f32) -> Length {
23    Length::Relative(v / 100.0)
24}
25
26/// Fraction of the parent (`relative(0.5)` = half).
27pub const fn relative(fraction: f32) -> Length {
28    Length::Relative(fraction)
29}
30
31/// Automatic sizing.
32pub const fn auto() -> Length {
33    Length::Auto
34}
35
36impl From<f32> for Length {
37    fn from(v: f32) -> Self {
38        Length::Px(v)
39    }
40}
41
42impl From<i32> for Length {
43    fn from(v: i32) -> Self {
44        Length::Px(v as f32)
45    }
46}
47
48impl Length {
49    pub fn is_auto(&self) -> bool {
50        matches!(self, Length::Auto)
51    }
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
55pub enum Display {
56    #[default]
57    Flex,
58    Grid,
59    Block,
60    /// Removed from layout and not painted.
61    None,
62}
63
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
65pub enum FlexDirection {
66    #[default]
67    Row,
68    Column,
69    RowReverse,
70    ColumnReverse,
71}
72
73#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
74pub enum FlexWrap {
75    #[default]
76    NoWrap,
77    Wrap,
78    WrapReverse,
79}
80
81/// Cross axis alignment (`align-items`, `align-self`, `justify-items`).
82#[derive(Clone, Copy, Debug, PartialEq, Eq)]
83pub enum Align {
84    Start,
85    End,
86    Center,
87    Stretch,
88    Baseline,
89}
90
91/// Main axis distribution (`justify-content`, `align-content`).
92#[derive(Clone, Copy, Debug, PartialEq, Eq)]
93pub enum Justify {
94    Start,
95    End,
96    Center,
97    Stretch,
98    SpaceBetween,
99    SpaceAround,
100    SpaceEvenly,
101}
102
103#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
104pub enum Position {
105    /// Participates in flow layout; insets offset it visually.
106    #[default]
107    Relative,
108    /// Taken out of flow and positioned against the parent's padding box.
109    Absolute,
110}
111
112#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
113pub enum Overflow {
114    #[default]
115    Visible,
116    /// Content is clipped; no scrolling.
117    Hidden,
118    /// Content is clipped and can be scrolled.
119    Scroll,
120}
121
122/// A grid track size.
123#[derive(Clone, Copy, Debug, PartialEq)]
124pub enum Track {
125    Px(f32),
126    Relative(f32),
127    /// Fraction of the remaining space (`fr`).
128    Fr(f32),
129    Auto,
130    MinContent,
131    MaxContent,
132    /// `minmax(px, fr)`.
133    MinMax(f32, f32),
134}
135
136/// Placement of a grid item along one axis.
137#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
138pub enum GridPlacement {
139    #[default]
140    Auto,
141    /// Spans `n` tracks from the auto placed position.
142    Span(u16),
143    /// Starts at 1-based line `start` and spans `span` tracks.
144    Line { start: i16, span: u16 },
145}
146
147#[derive(Clone, Debug, PartialEq, Default)]
148pub struct GridStyle {
149    pub template_columns: Vec<Track>,
150    pub template_rows: Vec<Track>,
151    pub auto_rows: Vec<Track>,
152    pub column: GridPlacement,
153    pub row: GridPlacement,
154}
155
156/// Width/height pair for layout properties.
157#[derive(Clone, Copy, Debug, PartialEq, Default)]
158pub struct Axes<T> {
159    pub width: T,
160    pub height: T,
161}
162
163impl<T: Copy> Axes<T> {
164    pub const fn both(v: T) -> Self {
165        Axes {
166            width: v,
167            height: v,
168        }
169    }
170}
171
172/// Everything that influences layout.
173#[derive(Clone, Debug, PartialEq)]
174pub struct LayoutStyle {
175    pub display: Display,
176    pub position: Position,
177    pub direction: FlexDirection,
178    pub wrap: FlexWrap,
179    pub align_items: Option<Align>,
180    pub align_self: Option<Align>,
181    /// Inline-axis alignment of a grid item within its cell.
182    pub justify_self: Option<Align>,
183    pub align_content: Option<Justify>,
184    pub justify_content: Option<Justify>,
185    pub size: Axes<Length>,
186    pub min_size: Axes<Length>,
187    pub max_size: Axes<Length>,
188    pub aspect_ratio: Option<f32>,
189    pub padding: Edges<Length>,
190    pub margin: Edges<Length>,
191    pub border: Edges<f32>,
192    pub inset: Edges<Length>,
193    /// `width` = column gap, `height` = row gap.
194    pub gap: Axes<Length>,
195    pub flex_grow: f32,
196    pub flex_shrink: f32,
197    pub flex_basis: Length,
198    pub overflow_x: Overflow,
199    pub overflow_y: Overflow,
200    pub grid: Option<Box<GridStyle>>,
201}
202
203impl Default for LayoutStyle {
204    fn default() -> Self {
205        LayoutStyle {
206            display: Display::Flex,
207            position: Position::Relative,
208            direction: FlexDirection::Row,
209            wrap: FlexWrap::NoWrap,
210            align_items: None,
211            align_self: None,
212            justify_self: None,
213            align_content: None,
214            justify_content: None,
215            size: Axes::both(Length::Auto),
216            min_size: Axes::both(Length::Auto),
217            max_size: Axes::both(Length::Auto),
218            aspect_ratio: None,
219            padding: Edges::all(Length::Px(0.0)),
220            margin: Edges::all(Length::Px(0.0)),
221            border: Edges::all(0.0),
222            inset: Edges::all(Length::Auto),
223            gap: Axes::both(Length::Px(0.0)),
224            flex_grow: 0.0,
225            flex_shrink: 1.0,
226            flex_basis: Length::Auto,
227            overflow_x: Overflow::Visible,
228            overflow_y: Overflow::Visible,
229            grid: None,
230        }
231    }
232}
233
234impl LayoutStyle {
235    /// Whether children are clipped to this node's bounds.
236    pub fn clips(&self) -> bool {
237        self.overflow_x != Overflow::Visible || self.overflow_y != Overflow::Visible
238    }
239
240    pub fn scrolls(&self) -> bool {
241        self.overflow_x == Overflow::Scroll || self.overflow_y == Overflow::Scroll
242    }
243
244    pub fn grid_mut(&mut self) -> &mut GridStyle {
245        self.grid.get_or_insert_with(Default::default)
246    }
247}