Skip to main content

simple_render/ui/types/
layout.rs

1use super::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
4pub enum Direction {
5    #[default]
6    Row,
7    Column,
8}
9
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
11pub enum Align {
12    #[default]
13    Start,
14    Center,
15    End,
16    Stretch,
17}
18
19#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
20pub enum Overflow {
21    #[default]
22    Clip,
23    Visible,
24}
25
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
27pub enum Position {
28    #[default]
29    Flow,
30    Absolute,
31}
32
33#[derive(Clone, Copy, Debug, Default, PartialEq)]
34pub enum Length {
35    #[default]
36    Fit,
37    Fill,
38    Px(u32),
39    Percent(f32),
40}
41
42impl Length {
43    pub const fn px(value: u32) -> Self {
44        Self::Px(value)
45    }
46
47    pub const fn percent(value: f32) -> Self {
48        Self::Percent(value)
49    }
50}
51
52#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
53pub struct Spacing {
54    pub top: u32,
55    pub right: u32,
56    pub bottom: u32,
57    pub left: u32,
58}
59
60impl Spacing {
61    pub const ZERO: Self = Self::all(0);
62
63    pub const fn all(value: u32) -> Self {
64        Self {
65            top: value,
66            right: value,
67            bottom: value,
68            left: value,
69        }
70    }
71
72    pub const fn axis(horizontal: u32, vertical: u32) -> Self {
73        Self {
74            top: vertical,
75            right: horizontal,
76            bottom: vertical,
77            left: horizontal,
78        }
79    }
80}
81
82#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
83pub struct Inset {
84    pub top: Option<u32>,
85    pub right: Option<u32>,
86    pub bottom: Option<u32>,
87    pub left: Option<u32>,
88}
89
90impl Inset {
91    pub const ZERO: Self = Self {
92        top: None,
93        right: None,
94        bottom: None,
95        left: None,
96    };
97
98    pub const fn all(value: u32) -> Self {
99        Self {
100            top: Some(value),
101            right: Some(value),
102            bottom: Some(value),
103            left: Some(value),
104        }
105    }
106
107    pub const fn axis(horizontal: u32, vertical: u32) -> Self {
108        Self {
109            top: Some(vertical),
110            right: Some(horizontal),
111            bottom: Some(vertical),
112            left: Some(horizontal),
113        }
114    }
115}
116
117#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
118pub(in crate::ui) struct Size {
119    pub(in crate::ui) width: u32,
120    pub(in crate::ui) height: u32,
121}
122
123impl Size {
124    pub(in crate::ui) fn new(width: u32, height: u32) -> Self {
125        Self { width, height }
126    }
127
128    pub(in crate::ui) fn axis(self, direction: Direction) -> u32 {
129        match direction {
130            Direction::Row => self.width,
131            Direction::Column => self.height,
132        }
133    }
134
135    pub(in crate::ui) fn cross(self, direction: Direction) -> u32 {
136        match direction {
137            Direction::Row => self.height,
138            Direction::Column => self.width,
139        }
140    }
141}
142
143#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
144pub struct MeasuredSize {
145    pub width: u32,
146    pub height: u32,
147}
148
149impl From<Size> for MeasuredSize {
150    fn from(size: Size) -> Self {
151        Self {
152            width: size.width,
153            height: size.height,
154        }
155    }
156}
157
158#[derive(Clone, Debug, Default, PartialEq, Eq)]
159pub struct Hit {
160    /// Child-index path from the root to the hit element. The root element is an empty path.
161    pub path: Vec<usize>,
162    pub bounds: Bounds,
163}
164
165#[derive(Clone, Debug, PartialEq)]
166pub struct RectLayout {
167    pub surface: Option<Surface>,
168    pub width: Length,
169    pub height: Length,
170    pub min_width: u32,
171    pub min_height: u32,
172    pub max_width: Option<u32>,
173    pub max_height: Option<u32>,
174    pub fill: u32,
175    pub direction: Direction,
176    pub align: Align,
177    pub justify: Align,
178    pub overflow: Overflow,
179    pub position: Position,
180    pub inset: Inset,
181    pub padding: Spacing,
182    pub gap: u32,
183    pub style: Style,
184    pub content: Option<Content>,
185}
186
187impl Default for RectLayout {
188    fn default() -> Self {
189        Self {
190            surface: None,
191            width: Length::Fit,
192            height: Length::Fit,
193            min_width: 0,
194            min_height: 0,
195            max_width: None,
196            max_height: None,
197            fill: 1,
198            direction: Direction::Row,
199            align: Align::Start,
200            justify: Align::Start,
201            overflow: Overflow::Clip,
202            position: Position::Flow,
203            inset: Inset::ZERO,
204            padding: Spacing::ZERO,
205            gap: 0,
206            style: Style::default(),
207            content: None,
208        }
209    }
210}
211
212pub type RectStyle = Style;
213
214#[derive(Clone, Debug, PartialEq, Eq)]
215pub struct Surface {
216    pub namespace: String,
217    pub width: u32,
218    pub height: u32,
219    pub output: Option<OutputTarget>,
220    pub layer: Layer,
221    pub anchor: Anchor,
222    pub margins: Margins,
223    pub exclusive_zone: i32,
224    pub keyboard_interactivity: KeyboardInteractivity,
225}
226
227impl Default for Surface {
228    fn default() -> Self {
229        let options = LayerOptions::default();
230        Self {
231            namespace: options.namespace,
232            width: options.width,
233            height: options.height,
234            output: options.output,
235            layer: options.layer,
236            anchor: options.anchor,
237            margins: options.margins,
238            exclusive_zone: options.exclusive_zone,
239            keyboard_interactivity: options.keyboard_interactivity,
240        }
241    }
242}
243
244impl From<Surface> for LayerOptions {
245    fn from(surface: Surface) -> Self {
246        Self {
247            namespace: surface.namespace,
248            width: surface.width,
249            height: surface.height,
250            output: surface.output,
251            layer: surface.layer,
252            anchor: surface.anchor,
253            margins: surface.margins,
254            exclusive_zone: surface.exclusive_zone,
255            keyboard_interactivity: surface.keyboard_interactivity,
256        }
257    }
258}