dinamika_core/shape/layout.rs
1//! Flex-layout parameters for children: axis ([`Direction`]), distribution
2//! ([`Justify`]), alignment ([`Align`]) and padding ([`Padding`]).
3
4/// The children's layout axis (analogous to `flex-direction`).
5#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
6pub enum Direction {
7 /// Left to right (the main axis is horizontal).
8 #[default]
9 Row,
10 /// Top to bottom (the main axis is vertical).
11 Column,
12}
13
14/// Distribution along the main axis (analogous to `justify-content`).
15#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
16pub enum Justify {
17 #[default]
18 Start,
19 Center,
20 End,
21 /// The outer items are pressed to the edges, the gaps are equal.
22 SpaceBetween,
23 /// Equal gaps around each item.
24 SpaceAround,
25}
26
27/// Alignment along the cross axis (analogous to `align-items`).
28#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
29pub enum Align {
30 #[default]
31 Start,
32 Center,
33 End,
34 /// Stretch across the whole cross axis of the container.
35 Stretch,
36}
37
38/// The shape's inner padding by side.
39///
40/// Built from a value in the style of CSS shorthands:
41///
42/// - `20.0` — the same on all sides;
43/// - `(10.0, 20.0)` — `(vertical, horizontal)`;
44/// - `(5.0, 10.0, 15.0, 20.0)` — `(top, right, bottom, left)`.
45#[derive(Copy, Clone, Debug, Default, PartialEq)]
46pub struct Padding {
47 pub top: f32,
48 pub right: f32,
49 pub bottom: f32,
50 pub left: f32,
51}
52
53impl From<f32> for Padding {
54 /// The same padding on all sides.
55 fn from(v: f32) -> Self {
56 Padding { top: v, right: v, bottom: v, left: v }
57 }
58}
59
60impl From<(f32, f32)> for Padding {
61 /// `(vertical, horizontal)` — like the two-value CSS shorthand.
62 fn from((vertical, horizontal): (f32, f32)) -> Self {
63 Padding { top: vertical, right: horizontal, bottom: vertical, left: horizontal }
64 }
65}
66
67impl From<(f32, f32, f32, f32)> for Padding {
68 /// `(top, right, bottom, left)` — like the four-value CSS shorthand.
69 fn from((top, right, bottom, left): (f32, f32, f32, f32)) -> Self {
70 Padding { top, right, bottom, left }
71 }
72}
73
74/// A length along one axis: absolute in pixels, or a fraction of the parent's
75/// content area in percent. Passed to [`width`](crate::Shape::width) and
76/// [`height`](crate::Shape::height).
77///
78/// Constructed with the constructor methods:
79///
80/// - [`Length::pixel`] — an absolute size in pixels; participates in animation
81/// (via [`over`](crate::Tween::over)) and in the parent's natural size;
82/// - [`Length::percent`] — a fraction of the parent in percent (`100.0` — 100%,
83/// `50.0` — half); resolved on the second layout pass relative to the parent
84/// and overrides the pixel size. The fraction itself is not animated.
85///
86/// ```
87/// # use dinamika_core::*;
88/// let full = Shape::rect().width(Length::percent(100.0)); // the parent's full width
89/// let fixed = Shape::rect().width(Length::pixel(120.0)); // exactly 120 px
90/// ```
91#[derive(Copy, Clone, Debug, PartialEq)]
92pub enum Length {
93 /// An absolute size in pixels.
94 Pixel(f32),
95 /// A fraction of the parent's content area in percent (`100.0` — 100%).
96 Percent(f32),
97}
98
99impl Length {
100 /// An absolute length in pixels (`<= 0` — "auto", size by content).
101 pub fn pixel(value: f32) -> Length {
102 Length::Pixel(value)
103 }
104
105 /// A length as a fraction of the parent in percent: `percent(100.0)` — 100%,
106 /// `percent(50.0)` — half.
107 pub fn percent(value: f32) -> Length {
108 Length::Percent(value)
109 }
110}