crcbl-ui 0.3.0

Crucible immediate-mode GUI
Documentation
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
//! Typed property values: what a declaration sets on a [`NodeStyle`].
//!
//! [`Declaration`] is one property set to one typed value — what a stylesheet
//! declaration becomes once parsed, and what a builder's inline override is
//! written in. The CSS each one parses from is [`super::property`]'s.

use crcbl_core::input::CursorIcon;

use crate::draw_list::CornerRadii;
use crate::tree::{
    Align, BorderImageWidth, Direction, Display, Edges, FamilyName, FlexDirection, FlexWrap,
    FontFamily, FontStyle, FontWeight, ImageName, Justify, Length, LengthAuto, LineHeight,
    NavTarget, NavWrap, NodeStyle, Overflow, Position, TextAlign, TextDecoration, TextOverflow,
    WhiteSpace,
};

/// Which sides of a box a declaration sets.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Sides {
    /// All four.
    All,
    /// The top.
    Top,
    /// The right.
    Right,
    /// The bottom.
    Bottom,
    /// The left.
    Left,
}

/// Which corners of a box a declaration rounds.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Corners {
    /// All four.
    All,
    /// The top-left.
    TopLeft,
    /// The top-right.
    TopRight,
    /// The bottom-right.
    BottomRight,
    /// The bottom-left.
    BottomLeft,
}

/// One property set to one typed value: what a stylesheet's declaration
/// becomes, and what a builder's inline override is written in.
///
/// Shorthands expand to these, so `padding: 1px 2px` is four
/// [`Declaration::Padding`]s. See the module docs for the CSS each one
/// parses from.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Declaration {
    /// `display`.
    Display(Display),
    /// `position`.
    Position(Position),
    /// `top`, `right`, `bottom`, `left`.
    Inset(Sides, LengthAuto),
    /// `width`.
    Width(LengthAuto),
    /// `height`.
    Height(LengthAuto),
    /// `min-width`.
    MinWidth(LengthAuto),
    /// `min-height`.
    MinHeight(LengthAuto),
    /// `max-width`.
    MaxWidth(LengthAuto),
    /// `max-height`.
    MaxHeight(LengthAuto),
    /// `margin-*`.
    Margin(Sides, LengthAuto),
    /// `padding-*`.
    Padding(Sides, Length),
    /// `border-*-width`, in pixels.
    BorderWidth(Sides, f32),
    /// `overflow`.
    Overflow(Overflow),
    /// `flex-direction`.
    FlexDirection(FlexDirection),
    /// `flex-wrap`.
    FlexWrap(FlexWrap),
    /// `justify-content`; `None` is `normal`.
    JustifyContent(Option<Justify>),
    /// `align-items`; `None` is `normal`.
    AlignItems(Option<Align>),
    /// `align-self`; `None` is `auto`.
    AlignSelf(Option<Align>),
    /// `flex-grow`.
    FlexGrow(f32),
    /// `flex-shrink`.
    FlexShrink(f32),
    /// `flex-basis`.
    FlexBasis(LengthAuto),
    /// `column-gap`.
    ColumnGap(Length),
    /// `row-gap`.
    RowGap(Length),
    /// `background-color`, in linear light.
    Background([f32; 4]),
    /// `border-color`, in linear light.
    BorderColor([f32; 4]),
    /// `border-*-radius`, in pixels.
    BorderRadius(Corners, f32),
    /// `color`, in linear light. Inherited.
    Color([f32; 4]),
    /// `font-size`, in pixels. Inherited.
    FontSize(f32),
    /// `font-family`'s first built-in family. Inherited.
    FontFamily(FontFamily),
    /// The family `font-family` names ahead of its first built-in one, which
    /// a registered font answers to; `None` when it names none. Inherited.
    FamilyName(Option<FamilyName>),
    /// `font-weight`. Inherited.
    FontWeight(FontWeight),
    /// `font-style`. Inherited.
    FontStyle(FontStyle),
    /// `line-height`. Inherited.
    LineHeight(LineHeight),
    /// `text-align`. Inherited.
    TextAlign(TextAlign),
    /// `white-space`. Inherited.
    WhiteSpace(WhiteSpace),
    /// `cursor`: a shape, or `None` for `none`. Inherited.
    Cursor(Option<CursorIcon>),
    /// `text-overflow`.
    TextOverflow(TextOverflow),
    /// `text-decoration`. Inherited, unlike CSS's; see [`TextDecoration`].
    TextDecoration(TextDecoration),
    /// `outline-width`, in pixels.
    OutlineWidth(f32),
    /// `outline-color`, in linear light.
    OutlineColor([f32; 4]),
    /// `outline-offset`, in pixels.
    OutlineOffset(f32),
    /// `background-image`; `None` is `none`.
    BackgroundImage(Option<ImageName>),
    /// `border-image-source`; `None` is `none`.
    BorderImageSource(Option<ImageName>),
    /// One side of `border-image-slice`, in texels.
    BorderImageSlice(Sides, f32),
    /// `border-image-slice`'s `fill` keyword.
    BorderImageFill(bool),
    /// One side of `border-image-width`.
    BorderImageWidth(Sides, BorderImageWidth),
    /// `nav-up`, `nav-right`, `nav-down` or `nav-left`.
    Nav(Direction, NavTarget),
    /// `nav-wrap`.
    NavWrap(NavWrap),
}

fn set_sides<T: Copy>(edges: &mut Edges<T>, sides: Sides, value: T) {
    match sides {
        Sides::All => *edges = Edges::all(value),
        Sides::Top => edges.top = value,
        Sides::Right => edges.right = value,
        Sides::Bottom => edges.bottom = value,
        Sides::Left => edges.left = value,
    }
}

impl Declaration {
    /// Sets this declaration's property on `style`.
    pub fn apply(self, style: &mut NodeStyle) {
        match self {
            Self::Display(value) => style.display = value,
            Self::Position(value) => style.position = value,
            Self::Inset(sides, value) => set_sides(&mut style.inset, sides, value),
            Self::Width(value) => style.width = value,
            Self::Height(value) => style.height = value,
            Self::MinWidth(value) => style.min_width = value,
            Self::MinHeight(value) => style.min_height = value,
            Self::MaxWidth(value) => style.max_width = value,
            Self::MaxHeight(value) => style.max_height = value,
            Self::Margin(sides, value) => set_sides(&mut style.margin, sides, value),
            Self::Padding(sides, value) => set_sides(&mut style.padding, sides, value),
            Self::BorderWidth(sides, value) => set_sides(&mut style.border, sides, value),
            Self::Overflow(value) => style.overflow = value,
            Self::FlexDirection(value) => style.flex_direction = value,
            Self::FlexWrap(value) => style.flex_wrap = value,
            Self::JustifyContent(value) => style.justify_content = value,
            Self::AlignItems(value) => style.align_items = value,
            Self::AlignSelf(value) => style.align_self = value,
            Self::FlexGrow(value) => style.flex_grow = value,
            Self::FlexShrink(value) => style.flex_shrink = value,
            Self::FlexBasis(value) => style.flex_basis = value,
            Self::ColumnGap(value) => style.column_gap = value,
            Self::RowGap(value) => style.row_gap = value,
            Self::Background(value) => style.background = value,
            Self::BorderColor(value) => style.border_color = value,
            Self::BorderRadius(corners, value) => {
                let radii = &mut style.radii;
                match corners {
                    Corners::All => *radii = CornerRadii::uniform(value),
                    Corners::TopLeft => radii.top_left = value,
                    Corners::TopRight => radii.top_right = value,
                    Corners::BottomRight => radii.bottom_right = value,
                    Corners::BottomLeft => radii.bottom_left = value,
                }
            }
            Self::Color(value) => style.color = value,
            Self::FontSize(value) => style.font_size = value,
            Self::FontFamily(value) => style.font_family = value,
            Self::FamilyName(value) => style.family_name = value,
            Self::FontWeight(value) => style.font_weight = value,
            Self::FontStyle(value) => style.font_style = value,
            Self::LineHeight(value) => style.line_height = value,
            Self::TextAlign(value) => style.text_align = value,
            Self::WhiteSpace(value) => style.white_space = value,
            Self::Cursor(value) => style.cursor = value,
            Self::TextOverflow(value) => style.text_overflow = value,
            Self::TextDecoration(value) => style.text_decoration = value,
            Self::OutlineWidth(value) => style.outline_width = value,
            Self::OutlineColor(value) => style.outline_color = value,
            Self::OutlineOffset(value) => style.outline_offset = value,
            Self::BackgroundImage(value) => style.background_image = value,
            Self::BorderImageSource(value) => style.border_image.source = value,
            Self::BorderImageSlice(sides, value) => {
                set_sides(&mut style.border_image.slice, sides, value);
            }
            Self::BorderImageFill(value) => style.border_image.fill = value,
            Self::BorderImageWidth(sides, value) => {
                set_sides(&mut style.border_image.width, sides, value);
            }
            Self::Nav(direction, value) => *style.nav_mut(direction) = value,
            Self::NavWrap(value) => style.nav_wrap = value,
        }
    }
}

impl NodeStyle {
    /// Every property of this style as a declaration, per side and per corner,
    /// so that applying them in order onto any style yields exactly this one.
    ///
    /// What a caller with a whole [`NodeStyle`] in hand passes as an inline
    /// override to take no part of the cascade.
    #[must_use]
    pub fn declarations(&self) -> Vec<Declaration> {
        use Declaration as D;
        let sides = [Sides::Top, Sides::Right, Sides::Bottom, Sides::Left];
        let edges = |edges: Edges<LengthAuto>| [edges.top, edges.right, edges.bottom, edges.left];
        let mut all = vec![
            D::Display(self.display),
            D::Position(self.position),
            D::Width(self.width),
            D::Height(self.height),
            D::MinWidth(self.min_width),
            D::MinHeight(self.min_height),
            D::MaxWidth(self.max_width),
            D::MaxHeight(self.max_height),
            D::Overflow(self.overflow),
            D::FlexDirection(self.flex_direction),
            D::FlexWrap(self.flex_wrap),
            D::JustifyContent(self.justify_content),
            D::AlignItems(self.align_items),
            D::AlignSelf(self.align_self),
            D::FlexGrow(self.flex_grow),
            D::FlexShrink(self.flex_shrink),
            D::FlexBasis(self.flex_basis),
            D::ColumnGap(self.column_gap),
            D::RowGap(self.row_gap),
            D::Background(self.background),
            D::BorderColor(self.border_color),
            D::Color(self.color),
            D::FontSize(self.font_size),
            D::FontFamily(self.font_family),
            D::FamilyName(self.family_name),
            D::FontWeight(self.font_weight),
            D::FontStyle(self.font_style),
            D::LineHeight(self.line_height),
            D::TextAlign(self.text_align),
            D::WhiteSpace(self.white_space),
            D::Cursor(self.cursor),
            D::TextOverflow(self.text_overflow),
            D::TextDecoration(self.text_decoration),
            D::OutlineWidth(self.outline_width),
            D::OutlineColor(self.outline_color),
            D::OutlineOffset(self.outline_offset),
            D::BackgroundImage(self.background_image),
            D::BorderImageSource(self.border_image.source),
            D::BorderImageFill(self.border_image.fill),
            D::NavWrap(self.nav_wrap),
        ];
        for direction in Direction::ALL {
            all.push(D::Nav(direction, self.nav(direction)));
        }
        let padding = self.padding;
        let border = self.border;
        for (at, side) in sides.into_iter().enumerate() {
            all.push(D::Inset(side, edges(self.inset)[at]));
            all.push(D::Margin(side, edges(self.margin)[at]));
            all.push(D::Padding(
                side,
                [padding.top, padding.right, padding.bottom, padding.left][at],
            ));
            all.push(D::BorderWidth(
                side,
                [border.top, border.right, border.bottom, border.left][at],
            ));
            let image = self.border_image;
            all.push(D::BorderImageSlice(
                side,
                [
                    image.slice.top,
                    image.slice.right,
                    image.slice.bottom,
                    image.slice.left,
                ][at],
            ));
            all.push(D::BorderImageWidth(
                side,
                [
                    image.width.top,
                    image.width.right,
                    image.width.bottom,
                    image.width.left,
                ][at],
            ));
        }
        let radii = self.radii;
        all.extend([
            D::BorderRadius(Corners::TopLeft, radii.top_left),
            D::BorderRadius(Corners::TopRight, radii.top_right),
            D::BorderRadius(Corners::BottomRight, radii.bottom_right),
            D::BorderRadius(Corners::BottomLeft, radii.bottom_left),
        ]);
        all
    }
}

#[cfg(all(test, feature = "parsed-font"))]
mod tests {
    use super::*;

    const fn px(value: f32) -> LengthAuto {
        LengthAuto::Px(value)
    }

    /// **`declarations` reproduces a style exactly** from any starting point.
    #[test]
    fn a_styles_declarations_rebuild_it_exactly() {
        let style = NodeStyle {
            inset: Edges {
                top: px(1.0),
                right: LengthAuto::Auto,
                bottom: LengthAuto::Percent(0.2),
                left: px(-3.0),
            },
            padding: Edges {
                top: Length::Px(1.0),
                right: Length::Px(2.0),
                bottom: Length::Percent(0.3),
                left: Length::Px(4.0),
            },
            border: Edges {
                top: 1.0,
                right: 2.0,
                bottom: 3.0,
                left: 4.0,
            },
            radii: CornerRadii {
                top_left: 1.0,
                top_right: 2.0,
                bottom_right: 3.0,
                bottom_left: 4.0,
            },
            background: [0.1, 0.2, 0.3, 0.4],
            color: [0.5; 4],
            font_size: 20.0,
            font_family: FontFamily::Sans,
            font_weight: FontWeight::BOLD,
            font_style: FontStyle::Italic,
            text_decoration: TextDecoration::Underline,
            line_height: LineHeight::Multiple(1.5),
            text_align: TextAlign::Center,
            white_space: WhiteSpace::NoWrap,
            cursor: Some(CursorIcon::Pointer),
            text_overflow: TextOverflow::Ellipsis,
            align_self: Some(Align::Center),
            outline_width: 2.0,
            outline_color: [0.9, 0.8, 0.7, 0.6],
            outline_offset: -1.0,
            nav_up: NavTarget::None,
            nav_right: NavTarget::Id(crate::tree::NavId::new("next")),
            nav_wrap: NavWrap::Horizontal,
            background_image: Some(crate::tree::ImageName::new("sky")),
            border_image: crate::tree::BorderImage {
                source: Some(crate::tree::ImageName::new("frame")),
                slice: Edges {
                    top: 1.0,
                    right: 2.0,
                    bottom: 3.0,
                    left: 4.0,
                },
                fill: true,
                width: Edges {
                    top: BorderImageWidth::Px(5.0),
                    right: BorderImageWidth::Multiple(2.0),
                    bottom: BorderImageWidth::Px(7.0),
                    left: BorderImageWidth::Multiple(0.5),
                },
            },
            ..NodeStyle::DEFAULT
        };
        let mut rebuilt = NodeStyle {
            width: px(99.0),
            margin: Edges::all(LengthAuto::Auto),
            color: [0.0; 4],
            ..NodeStyle::DEFAULT
        };
        for declaration in style.declarations() {
            declaration.apply(&mut rebuilt);
        }
        assert_eq!(rebuilt, style);
    }
}