Skip to main content

wde_ui/
style.rs

1//! Layout style of UI nodes, following the CSS box model. Converted to taffy's style when laid out.
2use taffy::{Dimension, LengthPercentage, LengthPercentageAuto};
3
4pub use taffy::{
5    AlignContent, AlignItems, AlignSelf, Display as UiDisplay, FlexDirection, FlexWrap,
6    JustifyContent
7};
8
9/// How a node is positioned. There is no CSS `static`: every node is the containing block of
10/// its absolute children, which keeps layout positions relative to the parent entity.
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
12pub enum UiPosition {
13    /// In the flow of its parent, offset by its inset.
14    #[default]
15    Relative,
16    /// Out of the flow, placed by its inset relative to its parent.
17    Absolute
18}
19impl From<UiPosition> for taffy::Position {
20    fn from(position: UiPosition) -> Self {
21        match position {
22            UiPosition::Relative => taffy::Position::Relative,
23            UiPosition::Absolute => taffy::Position::Absolute
24        }
25    }
26}
27
28/// A length.
29#[derive(Clone, Copy, Debug, Default, PartialEq)]
30pub enum Val {
31    #[default]
32    Auto,
33    /// Logical pixels.
34    Px(f32),
35    /// Percentage (0 to 100) of the parent size.
36    Percent(f32)
37}
38impl Val {
39    fn dimension(self) -> Dimension {
40        match self {
41            Val::Auto => Dimension::auto(),
42            Val::Px(v) => Dimension::length(v),
43            Val::Percent(p) => Dimension::percent(p / 100.0)
44        }
45    }
46
47    fn length_percentage_auto(self) -> LengthPercentageAuto {
48        match self {
49            Val::Auto => LengthPercentageAuto::auto(),
50            Val::Px(v) => LengthPercentageAuto::length(v),
51            Val::Percent(p) => LengthPercentageAuto::percent(p / 100.0)
52        }
53    }
54
55    /// Padding, borders and gaps can't be auto: it resolves to zero.
56    fn length_percentage(self) -> LengthPercentage {
57        match self {
58            Val::Auto => LengthPercentage::length(0.0),
59            Val::Px(v) => LengthPercentage::length(v),
60            Val::Percent(p) => LengthPercentage::percent(p / 100.0)
61        }
62    }
63}
64
65/// A length for each side of a node.
66#[derive(Clone, Copy, Debug, PartialEq)]
67pub struct UiRect {
68    pub left: Val,
69    pub right: Val,
70    pub top: Val,
71    pub bottom: Val
72}
73impl UiRect {
74    pub const ZERO: Self = Self::all(Val::Px(0.0));
75
76    pub const fn all(val: Val) -> Self {
77        Self::axes(val, val)
78    }
79    pub const fn axes(horizontal: Val, vertical: Val) -> Self {
80        Self {
81            left: horizontal,
82            right: horizontal,
83            top: vertical,
84            bottom: vertical
85        }
86    }
87    pub const fn px(left: f32, right: f32, top: f32, bottom: f32) -> Self {
88        Self {
89            left: Val::Px(left),
90            right: Val::Px(right),
91            top: Val::Px(top),
92            bottom: Val::Px(bottom)
93        }
94    }
95
96    fn map<T>(self, f: impl Fn(Val) -> T) -> taffy::Rect<T> {
97        taffy::Rect {
98            left: f(self.left),
99            right: f(self.right),
100            top: f(self.top),
101            bottom: f(self.bottom)
102        }
103    }
104}
105impl Default for UiRect {
106    fn default() -> Self {
107        Self::ZERO
108    }
109}
110
111/// Layout style of a [`UiNode`](crate::node::UiNode). See the CSS properties of the same names.
112#[derive(Clone, Debug, PartialEq)]
113pub struct UiStyle {
114    pub display: UiDisplay,
115    pub position: UiPosition,
116    /// Offsets of relative and absolute nodes.
117    pub inset: UiRect,
118
119    pub width: Val,
120    pub height: Val,
121    pub min_width: Val,
122    pub min_height: Val,
123    pub max_width: Val,
124    pub max_height: Val,
125    pub aspect_ratio: Option<f32>,
126
127    pub margin: UiRect,
128    pub padding: UiRect,
129    /// Border widths, drawn with a [`UiBorderColor`](crate::node::UiBorderColor).
130    pub border: UiRect,
131
132    pub flex_direction: FlexDirection,
133    pub flex_wrap: FlexWrap,
134    pub flex_grow: f32,
135    pub flex_shrink: f32,
136    pub flex_basis: Val,
137    pub row_gap: Val,
138    pub column_gap: Val,
139
140    pub align_items: AlignItems,
141    pub align_self: Option<AlignSelf>,
142    pub align_content: AlignContent,
143    pub justify_content: JustifyContent
144}
145impl Default for UiStyle {
146    fn default() -> Self {
147        let taffy: taffy::Style = taffy::Style::DEFAULT;
148        Self {
149            display: taffy.display,
150            position: UiPosition::Relative,
151            inset: UiRect::all(Val::Auto),
152            width: Val::Auto,
153            height: Val::Auto,
154            min_width: Val::Auto,
155            min_height: Val::Auto,
156            max_width: Val::Auto,
157            max_height: Val::Auto,
158            aspect_ratio: None,
159            margin: UiRect::ZERO,
160            padding: UiRect::ZERO,
161            border: UiRect::ZERO,
162            flex_direction: taffy.flex_direction,
163            flex_wrap: taffy.flex_wrap,
164            flex_grow: taffy.flex_grow,
165            flex_shrink: taffy.flex_shrink,
166            flex_basis: Val::Auto,
167            row_gap: Val::Px(0.0),
168            column_gap: Val::Px(0.0),
169            align_items: taffy.align_items,
170            align_self: taffy.align_self,
171            align_content: taffy.align_content,
172            justify_content: taffy.justify_content
173        }
174    }
175}
176impl UiStyle {
177    pub(crate) fn to_taffy(&self) -> taffy::Style {
178        taffy::Style {
179            display: self.display,
180            position: self.position.into(),
181            inset: self.inset.map(Val::length_percentage_auto),
182            size: taffy::Size {
183                width: self.width.dimension(),
184                height: self.height.dimension()
185            },
186            min_size: taffy::Size {
187                width: self.min_width.length_percentage_auto(),
188                height: self.min_height.length_percentage_auto()
189            },
190            max_size: taffy::Size {
191                width: self.max_width.length_percentage_auto(),
192                height: self.max_height.length_percentage_auto()
193            },
194            aspect_ratio: self.aspect_ratio,
195            margin: self.margin.map(Val::length_percentage_auto),
196            padding: self.padding.map(Val::length_percentage),
197            border: self.border.map(Val::length_percentage),
198            flex_direction: self.flex_direction,
199            flex_wrap: self.flex_wrap,
200            flex_grow: self.flex_grow,
201            flex_shrink: self.flex_shrink,
202            flex_basis: self.flex_basis.dimension(),
203            gap: taffy::Size {
204                width: self.column_gap.length_percentage(),
205                height: self.row_gap.length_percentage()
206            },
207            align_items: self.align_items,
208            align_self: self.align_self,
209            align_content: self.align_content,
210            justify_content: self.justify_content,
211            ..taffy::Style::DEFAULT
212        }
213    }
214}