slate-framework 1.0.1

GPU-accelerated Rust UI framework — umbrella crate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Style types for layout configuration.
//!
//! Provides a framework-level `Style` type that abstracts over Taffy's
//! layout properties. This isolation layer:
//! - Simplifies the API for framework users
//! - Insulates against Taffy API changes
//! - Provides sensible defaults for UI development

use taffy::prelude::*;

use crate::types::Edges;

/// Length value for layout properties.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum Length {
    /// Size determined by content or parent.
    #[default]
    Auto,
    /// Fixed size in logical pixels.
    Px(f32),
    /// Percentage of parent size.
    Percent(f32),
}

impl Length {
    /// Create a pixel length.
    pub const fn px(value: f32) -> Self {
        Self::Px(value)
    }

    /// Create a percentage length.
    pub const fn percent(value: f32) -> Self {
        Self::Percent(value)
    }
}

/// Display mode for an element.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum DisplayMode {
    /// Flexbox layout (default).
    #[default]
    Flex,
    /// CSS Grid layout.
    Grid,
    /// Element is hidden and takes no space.
    None,
}

/// Position mode for an element.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum Position {
    /// Normal flow positioning (default).
    #[default]
    Relative,
    /// Positioned relative to nearest positioned ancestor.
    Absolute,
}

/// Overflow handling.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum Overflow {
    /// Content is visible outside bounds.
    #[default]
    Visible,
    /// Content is clipped to bounds.
    Hidden,
    /// Scroll if content overflows.
    Scroll,
}

/// Size constraint with min/max.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct SizeConstraint {
    /// Width constraint.
    pub width: Length,
    /// Height constraint.
    pub height: Length,
}

impl SizeConstraint {
    /// Auto on both axes — let layout determine the size.
    pub const AUTO: Self = Self {
        width: Length::Auto,
        height: Length::Auto,
    };

    /// Construct from explicit width and height [`Length`]s.
    pub const fn new(width: Length, height: Length) -> Self {
        Self { width, height }
    }

    /// Construct from pixel width and height.
    pub const fn px(width: f32, height: f32) -> Self {
        Self {
            width: Length::Px(width),
            height: Length::Px(height),
        }
    }
}

/// Layout style for elements.
///
/// Covers common Flexbox properties with sensible defaults.
/// Grid properties to be added in future versions.
#[derive(Clone, Debug, PartialEq)]
pub struct Style {
    /// Display mode (Flex, Grid, None).
    pub display: DisplayMode,

    /// Flex main-axis direction.
    pub flex_direction: FlexDirection,
    /// Cross-axis item alignment.
    pub align_items: AlignItems,
    /// Main-axis content distribution.
    pub justify_content: JustifyContent,
    /// Whether items wrap onto multiple lines.
    pub flex_wrap: FlexWrap,
    /// Flex grow factor.
    pub flex_grow: f32,
    /// Flex shrink factor.
    pub flex_shrink: f32,
    /// Flex basis (initial main-axis size).
    pub flex_basis: Length,

    /// Inner padding on each edge.
    pub padding: Edges<Length>,
    /// Outer margin on each edge.
    pub margin: Edges<Length>,
    /// Gap between children in logical pixels.
    pub gap: f32,

    /// Preferred size constraint.
    pub size: SizeConstraint,
    /// Minimum size constraint.
    pub min_size: SizeConstraint,
    /// Maximum size constraint.
    pub max_size: SizeConstraint,

    /// Positioning mode (Relative, Absolute).
    pub position: Position,
    /// Overflow handling (Visible, Hidden, Scroll).
    pub overflow: Overflow,
}

impl Default for Style {
    fn default() -> Self {
        Self {
            display: DisplayMode::Flex,
            flex_direction: FlexDirection::Row,
            align_items: AlignItems::Stretch,
            justify_content: JustifyContent::FlexStart,
            flex_wrap: FlexWrap::NoWrap,
            flex_grow: 0.0,
            flex_shrink: 1.0,
            flex_basis: Length::Auto,
            padding: Edges::default(),
            margin: Edges::default(),
            gap: 0.0,
            size: SizeConstraint::AUTO,
            min_size: SizeConstraint::AUTO,
            max_size: SizeConstraint::AUTO,
            position: Position::Relative,
            overflow: Overflow::Visible,
        }
    }
}

impl Style {
    /// Create a new style with defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set display mode.
    pub fn display(mut self, display: DisplayMode) -> Self {
        self.display = display;
        self
    }

    /// Set flex direction.
    pub fn flex_direction(mut self, direction: FlexDirection) -> Self {
        self.flex_direction = direction;
        self
    }

    /// Set flex direction to column.
    pub fn column(mut self) -> Self {
        self.flex_direction = FlexDirection::Column;
        self
    }

    /// Set flex direction to row.
    pub fn row(mut self) -> Self {
        self.flex_direction = FlexDirection::Row;
        self
    }

    /// Set align items.
    pub fn align_items(mut self, align: AlignItems) -> Self {
        self.align_items = align;
        self
    }

    /// Set justify content.
    pub fn justify_content(mut self, justify: JustifyContent) -> Self {
        self.justify_content = justify;
        self
    }

    /// Set flex grow.
    pub fn flex_grow(mut self, grow: f32) -> Self {
        self.flex_grow = grow;
        self
    }

    /// Set flex shrink.
    pub fn flex_shrink(mut self, shrink: f32) -> Self {
        self.flex_shrink = shrink;
        self
    }

    /// Set uniform padding in logical pixels.
    pub fn padding_all(mut self, value: f32) -> Self {
        self.padding = Edges::all(Length::Px(value));
        self
    }

    /// Set padding with edges.
    pub fn padding(mut self, edges: Edges<Length>) -> Self {
        self.padding = edges;
        self
    }

    /// Set uniform margin in logical pixels.
    pub fn margin_all(mut self, value: f32) -> Self {
        self.margin = Edges::all(Length::Px(value));
        self
    }

    /// Set margin with edges.
    pub fn margin(mut self, edges: Edges<Length>) -> Self {
        self.margin = edges;
        self
    }

    /// Set gap between children.
    pub fn gap(mut self, gap: f32) -> Self {
        self.gap = gap;
        self
    }

    /// Set fixed width in logical pixels.
    pub fn width(mut self, width: f32) -> Self {
        self.size.width = Length::Px(width);
        self
    }

    /// Set fixed height in logical pixels.
    pub fn height(mut self, height: f32) -> Self {
        self.size.height = Length::Px(height);
        self
    }

    /// Set size constraint.
    pub fn size(mut self, size: SizeConstraint) -> Self {
        self.size = size;
        self
    }

    /// Set min size constraint.
    pub fn min_size(mut self, size: SizeConstraint) -> Self {
        self.min_size = size;
        self
    }

    /// Set max size constraint.
    pub fn max_size(mut self, size: SizeConstraint) -> Self {
        self.max_size = size;
        self
    }

    /// Set position mode.
    pub fn position(mut self, position: Position) -> Self {
        self.position = position;
        self
    }

    /// Set overflow handling.
    pub fn overflow(mut self, overflow: Overflow) -> Self {
        self.overflow = overflow;
        self
    }
}

// Conversion: Length -> taffy::Dimension
fn length_to_dimension(len: Length) -> Dimension {
    match len {
        Length::Auto => Dimension::auto(),
        Length::Px(v) => Dimension::length(v),
        Length::Percent(v) => Dimension::percent(v / 100.0),
    }
}

// Conversion: Length -> taffy::LengthPercentage
fn length_to_length_percentage(len: Length) -> LengthPercentage {
    match len {
        Length::Auto => LengthPercentage::length(0.0),
        Length::Px(v) => LengthPercentage::length(v),
        Length::Percent(v) => LengthPercentage::percent(v / 100.0),
    }
}

// Conversion: Length -> taffy::LengthPercentageAuto
fn length_to_length_percentage_auto(len: Length) -> LengthPercentageAuto {
    match len {
        Length::Auto => LengthPercentageAuto::auto(),
        Length::Px(v) => LengthPercentageAuto::length(v),
        Length::Percent(v) => LengthPercentageAuto::percent(v / 100.0),
    }
}

impl From<&Style> for taffy::Style {
    fn from(s: &Style) -> taffy::Style {
        taffy::Style {
            display: match s.display {
                DisplayMode::Flex => Display::Flex,
                DisplayMode::Grid => Display::Grid,
                DisplayMode::None => Display::None,
            },
            flex_direction: s.flex_direction,
            align_items: Some(s.align_items),
            justify_content: Some(s.justify_content),
            flex_wrap: s.flex_wrap,
            flex_grow: s.flex_grow,
            flex_shrink: s.flex_shrink,
            flex_basis: length_to_dimension(s.flex_basis),
            padding: Rect {
                left: length_to_length_percentage(s.padding.left),
                right: length_to_length_percentage(s.padding.right),
                top: length_to_length_percentage(s.padding.top),
                bottom: length_to_length_percentage(s.padding.bottom),
            },
            margin: Rect {
                left: length_to_length_percentage_auto(s.margin.left),
                right: length_to_length_percentage_auto(s.margin.right),
                top: length_to_length_percentage_auto(s.margin.top),
                bottom: length_to_length_percentage_auto(s.margin.bottom),
            },
            gap: taffy::Size {
                width: LengthPercentage::length(s.gap),
                height: LengthPercentage::length(s.gap),
            },
            size: taffy::Size {
                width: length_to_dimension(s.size.width),
                height: length_to_dimension(s.size.height),
            },
            min_size: taffy::Size {
                width: length_to_dimension(s.min_size.width),
                height: length_to_dimension(s.min_size.height),
            },
            max_size: taffy::Size {
                width: length_to_dimension(s.max_size.width),
                height: length_to_dimension(s.max_size.height),
            },
            position: match s.position {
                Position::Relative => taffy::Position::Relative,
                Position::Absolute => taffy::Position::Absolute,
            },
            overflow: taffy::Point {
                x: match s.overflow {
                    Overflow::Visible => taffy::Overflow::Visible,
                    Overflow::Hidden => taffy::Overflow::Clip,
                    Overflow::Scroll => taffy::Overflow::Scroll,
                },
                y: match s.overflow {
                    Overflow::Visible => taffy::Overflow::Visible,
                    Overflow::Hidden => taffy::Overflow::Clip,
                    Overflow::Scroll => taffy::Overflow::Scroll,
                },
            },
            ..Default::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn style_default() {
        let s = Style::default();
        assert_eq!(s.display, DisplayMode::Flex);
        assert_eq!(s.flex_direction, FlexDirection::Row);
        assert_eq!(s.flex_grow, 0.0);
        assert_eq!(s.flex_shrink, 1.0);
    }

    #[test]
    fn style_builder() {
        let s = Style::new()
            .column()
            .padding_all(16.0)
            .gap(8.0)
            .flex_grow(1.0);

        assert_eq!(s.flex_direction, FlexDirection::Column);
        assert_eq!(s.padding, Edges::all(Length::Px(16.0)));
        assert_eq!(s.gap, 8.0);
        assert_eq!(s.flex_grow, 1.0);
    }

    #[test]
    fn style_to_taffy() {
        let s = Style::new().padding_all(10.0).width(100.0).height(50.0);
        let taffy_style: taffy::Style = (&s).into();

        assert_eq!(taffy_style.display, Display::Flex);
        // Check padding conversion
        assert_eq!(taffy_style.padding.left, LengthPercentage::length(10.0));
    }
}