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 transform: PaintTransform,
185    pub content: Option<Content>,
186}
187
188impl Default for RectLayout {
189    fn default() -> Self {
190        Self {
191            surface: None,
192            width: Length::Fit,
193            height: Length::Fit,
194            min_width: 0,
195            min_height: 0,
196            max_width: None,
197            max_height: None,
198            fill: 1,
199            direction: Direction::Row,
200            align: Align::Start,
201            justify: Align::Start,
202            overflow: Overflow::Clip,
203            position: Position::Flow,
204            inset: Inset::ZERO,
205            padding: Spacing::ZERO,
206            gap: 0,
207            style: Style::default(),
208            transform: PaintTransform::IDENTITY,
209            content: None,
210        }
211    }
212}
213
214pub type RectStyle = Style;
215
216#[derive(Clone, Debug, PartialEq, Eq)]
217pub struct Surface {
218    pub namespace: String,
219    pub width: u32,
220    pub height: u32,
221    pub output: Option<OutputTarget>,
222    pub layer: Layer,
223    pub anchor: Anchor,
224    pub margins: Margins,
225    pub exclusive_zone: i32,
226    pub keyboard_interactivity: KeyboardInteractivity,
227}
228
229impl Default for Surface {
230    fn default() -> Self {
231        let options = LayerOptions::default();
232        Self {
233            namespace: options.namespace,
234            width: options.width,
235            height: options.height,
236            output: options.output,
237            layer: options.layer,
238            anchor: options.anchor,
239            margins: options.margins,
240            exclusive_zone: options.exclusive_zone,
241            keyboard_interactivity: options.keyboard_interactivity,
242        }
243    }
244}
245
246impl From<Surface> for LayerOptions {
247    fn from(surface: Surface) -> Self {
248        Self {
249            namespace: surface.namespace,
250            width: surface.width,
251            height: surface.height,
252            output: surface.output,
253            layer: surface.layer,
254            anchor: surface.anchor,
255            margins: surface.margins,
256            exclusive_zone: surface.exclusive_zone,
257            keyboard_interactivity: surface.keyboard_interactivity,
258        }
259    }
260}