masonry 0.5.0

Traits and types of the Masonry toolkit.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright 2019 the Xilem Authors and the Druid Authors
// SPDX-License-Identifier: Apache-2.0

//! Default values used by various widgets in their paint methods.

#![allow(missing_docs, reason = "Names are self-explanatory.")]

use crate::core::{
    DefaultProperties, PropertySet, PropertyStack, Selector, StyleProperty, StyleSet,
};
use crate::layout::{AsUnit, Length};
use crate::palette::css::DIM_GRAY;
use crate::parley::{GenericFamily, LineHeight};
use crate::peniko::Color;

// We use glob imports here to avoid frequent merge conflicts.
use crate::properties::*;
use crate::widgets::*;

/// Default color for the app background.
///
/// If the app driver does some kind beginning-of-frame clearing,
/// it should clear with this color by default.
pub const BACKGROUND_COLOR: Color = Color::from_rgb8(0x1D, 0x1D, 0x1D);

pub const BORDER_WIDTH: Length = Length::const_px(1.);

// Zync color variations from https://tailwindcss.com/docs/colors
pub const ZYNC_900: Color = Color::from_rgb8(0x18, 0x18, 0x1b);
pub const ZYNC_800: Color = Color::from_rgb8(0x27, 0x27, 0x2a);
pub const ZYNC_700: Color = Color::from_rgb8(0x3f, 0x3f, 0x46);
pub const ZYNC_600: Color = Color::from_rgb8(0x52, 0x52, 0x5b);
pub const ZYNC_500: Color = Color::from_rgb8(0x71, 0x71, 0x7a);

pub const ACCENT_COLOR: Color = Color::from_rgb8(0x3b, 0x7e, 0xe4);
pub const TEXT_COLOR: Color = Color::from_rgb8(0xf2, 0xf2, 0xf2);
pub const DISABLED_TEXT_COLOR: Color = Color::from_rgb8(0xa0, 0xa0, 0x9a);
pub const PLACEHOLDER_COLOR: Color = Color::from_rgba8(0xFF, 0xFF, 0xFF, 0x8F);
pub const TEXT_BACKGROUND_COLOR: Color = Color::from_rgb8(0x16, 0x16, 0x16);
pub const FOCUS_COLOR: Color = Color::from_rgb8(0xff, 0xff, 0xff);

// TODO: The following constants are not being used in properties
pub const TEXT_SIZE_NORMAL: f32 = 15.0;
pub const BASIC_WIDGET_HEIGHT: Length = Length::const_px(18.0);
pub const BORDERED_WIDGET_HEIGHT: f64 = 24.0;
pub const SCROLLBAR_COLOR: Color = Color::from_rgb8(0xff, 0xff, 0xff);
pub const SCROLLBAR_BORDER_COLOR: Color = Color::from_rgb8(0x77, 0x77, 0x77);
pub const SCROLLBAR_WIDTH: f64 = 8.;
pub const SCROLLBAR_PAD: f64 = 2.;
pub const SCROLLBAR_MIN_SIZE: f64 = 45.;
pub const SCROLLBAR_RADIUS: f64 = 5.;
pub const SCROLLBAR_EDGE_WIDTH: f64 = 1.;
pub const DEFAULT_GAP: Length = Length::const_px(10.0);
pub const DEFAULT_SPACER_LEN: Length = Length::const_px(10.0);
pub const WIDGET_CONTROL_COMPONENT_PADDING: Length = Length::const_px(4.0);
pub const SELECTOR_MIN_WIDTH: f64 = 100.0;

pub fn default_property_set() -> DefaultProperties {
    let mut properties = DefaultProperties::new();

    // Badge
    properties.insert::<Badge, _>(Padding::from_vh(3.px(), 5.px()));
    properties.insert::<Badge, _>(CornerRadius { radius: 999.px() });
    properties.insert::<Badge, _>(BorderWidth { width: 0.px() });
    properties.insert::<Badge, _>(Background::Color(ACCENT_COLOR));
    properties.insert::<Badge, _>(BorderColor { color: ZYNC_700 });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_disabled(true),
            Background::Color(ZYNC_800),
        );
        properties.insert_stack::<Badge>(stack);
    }

    // Button
    properties.insert::<Button, _>(Padding::from_vh(6.px(), 16.px()));
    properties.insert::<Button, _>(CornerRadius { radius: 6.px() });
    properties.insert::<Button, _>(BorderWidth {
        width: BORDER_WIDTH,
    });
    properties.insert::<Button, _>(Background::Color(ZYNC_800));
    properties.insert::<Button, _>(BorderColor { color: ZYNC_700 });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_hovered(true),
            BorderColor { color: ZYNC_500 },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );
        stack.push_layer(
            Selector::new().with_active(true),
            Background::Color(ZYNC_700),
        );
        stack.push_layer(
            Selector::new().with_disabled(true),
            Background::Color(Color::BLACK),
        );
        properties.insert_stack::<Button>(stack);
    }

    // Checkbox
    properties.insert::<Checkbox, _>(CornerRadius { radius: 4.px() });
    properties.insert::<Checkbox, _>(BorderWidth {
        width: BORDER_WIDTH,
    });
    properties.insert::<Checkbox, _>(Background::Color(ZYNC_800));
    properties.insert::<Checkbox, _>(BorderColor { color: ZYNC_700 });
    properties.insert::<Checkbox, _>(CheckmarkStrokeWidth { width: 2.0 });
    properties.insert::<Checkbox, _>(CheckmarkColor { color: TEXT_COLOR });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_hovered(true),
            BorderColor { color: ZYNC_500 },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );
        stack.push_layer(
            Selector::new().with_active(true),
            Background::Color(ZYNC_700),
        );
        stack.push_layer(
            Selector::new().with_disabled(true),
            PropertySet::new()
                .with(Background::Color(Color::BLACK))
                .with(CheckmarkColor {
                    color: DISABLED_TEXT_COLOR,
                }),
        );
        properties.insert_stack::<Checkbox>(stack);
    }

    // DisclosureButton
    properties.insert::<DisclosureButton, _>(ContentColor::new(DIM_GRAY));
    properties.insert::<DisclosureButton, _>(Dimensions::fixed(
        Length::const_px(16.),
        Length::const_px(16.),
    ));
    properties.insert::<DisclosureButton, _>(Padding::all(4.px()));

    // Divider
    properties.insert::<Divider, _>(ContentColor::new(ZYNC_500));

    // Switch
    properties.insert::<Switch, _>(CornerRadius { radius: 10.px() }); // Full pill shape
    properties.insert::<Switch, _>(BorderWidth {
        width: BORDER_WIDTH,
    });
    properties.insert::<Switch, _>(Background::Color(ZYNC_700));
    properties.insert::<Switch, _>(BorderColor { color: ZYNC_700 });
    properties.insert::<Switch, _>(ThumbColor(Color::WHITE));
    properties.insert::<Switch, _>(ThumbRadius(8.px()));
    properties.insert::<Switch, _>(TrackThickness(20.px()));
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_hovered(true),
            BorderColor { color: ZYNC_500 },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );
        stack.push_layer(
            Selector::classes(&["#toggled"]),
            Background::Color(ACCENT_COLOR),
        );
        stack.push_layer(
            Selector::new().with_active(true),
            Background::Color(ZYNC_600),
        );
        stack.push_layer(
            Selector::new().with_disabled(true),
            Background::Color(Color::BLACK),
        );
        properties.insert_stack::<Switch>(stack);
    }

    // FIXME
    use crate::widgets::Selector as SelectorButton;

    // Selector
    properties.insert::<SelectorButton, _>(Padding::from_vh(6.px(), 16.px()));
    properties.insert::<SelectorButton, _>(CornerRadius { radius: 2.px() });
    properties.insert::<SelectorButton, _>(BorderWidth {
        width: BORDER_WIDTH,
    });

    properties.insert::<SelectorButton, _>(Background::Color(ZYNC_800));
    properties.insert::<SelectorButton, _>(BorderColor { color: ZYNC_700 });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_active(true),
            Background::Color(ZYNC_700),
        );
        stack.push_layer(
            Selector::new().with_disabled(true),
            Background::Color(Color::BLACK),
        );
        stack.push_layer(
            Selector::new().with_hovered(true),
            BorderColor { color: ZYNC_500 },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );
        properties.insert_stack::<SelectorButton>(stack);
    }

    // SelectorItem
    properties.insert::<SelectorItem, _>(Padding::from_vh(6.px(), 16.px()));
    properties.insert::<SelectorItem, _>(Background::Color(ZYNC_900));
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_hovered(true),
            Background::Color(ZYNC_800),
        );
        stack.push_layer(
            Selector::new().with_active(true),
            Background::Color(ZYNC_700),
        );
        stack.push_layer(
            Selector::new().with_disabled(true),
            Background::Color(Color::BLACK),
        );
        properties.insert_stack::<SelectorItem>(stack);
    }

    // Flex
    properties.insert::<Flex, _>(Gap::new(DEFAULT_GAP));

    // Grid
    properties.insert::<Grid, _>(Gap::ZERO);

    // TextInput
    properties.insert::<TextInput, _>(Padding::from_vh(6.px(), 12.px()));
    properties.insert::<TextInput, _>(CornerRadius { radius: 4.px() });
    properties.insert::<TextInput, _>(BorderWidth {
        width: BORDER_WIDTH,
    });
    properties.insert::<TextInput, _>(BorderColor { color: ZYNC_600 });
    properties.insert::<TextInput, _>(PlaceholderColor::new(PLACEHOLDER_COLOR));
    properties.insert::<TextInput, _>(CaretColor { color: TEXT_COLOR });
    properties.insert::<TextInput, _>(SelectionColor {
        color: ACCENT_COLOR,
    });
    properties.insert::<TextInput, _>(Background::Color(TEXT_BACKGROUND_COLOR));
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::classes(&["#unfocused"]),
            SelectionColor {
                color: DISABLED_TEXT_COLOR,
            },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );
        stack.push_layer(
            Selector::new().with_disabled(true),
            Background::Color(TEXT_BACKGROUND_COLOR),
        );
        properties.insert_stack::<TextInput>(stack);
    }

    // TextArea
    properties.insert::<TextArea<false>, _>(ContentColor::new(TEXT_COLOR));
    properties.insert::<TextArea<false>, _>(CaretColor { color: TEXT_COLOR });
    properties.insert::<TextArea<false>, _>(SelectionColor {
        color: ACCENT_COLOR,
    });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_disabled(true),
            ContentColor::new(DISABLED_TEXT_COLOR),
        );
        properties.insert_stack::<TextArea<false>>(stack);
    }
    properties.insert::<TextArea<true>, _>(ContentColor::new(TEXT_COLOR));
    properties.insert::<TextArea<true>, _>(CaretColor { color: TEXT_COLOR });
    properties.insert::<TextArea<true>, _>(SelectionColor {
        color: ACCENT_COLOR,
    });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_disabled(true),
            ContentColor::new(DISABLED_TEXT_COLOR),
        );
        properties.insert_stack::<TextArea<true>>(stack);
    }

    // Prose
    properties.insert::<Prose, _>(CaretColor { color: TEXT_COLOR });
    properties.insert::<Prose, _>(ContentColor::new(TEXT_COLOR));
    properties.insert::<Prose, _>(SelectionColor {
        color: ACCENT_COLOR,
    });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::classes(&["#unfocused"]),
            SelectionColor {
                color: DISABLED_TEXT_COLOR,
            },
        );
        properties.insert_stack::<Prose>(stack);
    }

    // Label
    properties.insert::<Label, _>(ContentColor::new(TEXT_COLOR));
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_disabled(true),
            ContentColor::new(DISABLED_TEXT_COLOR),
        );
        properties.insert_stack::<Label>(stack);
    }

    // ProgressBar
    properties.insert::<ProgressBar, _>(CornerRadius { radius: 2.px() });
    properties.insert::<ProgressBar, _>(BorderWidth {
        width: BORDER_WIDTH,
    });

    properties.insert::<ProgressBar, _>(Background::Color(ZYNC_900));
    properties.insert::<ProgressBar, _>(BorderColor { color: ZYNC_800 });
    properties.insert::<ProgressBar, _>(BarColor(ACCENT_COLOR));

    // RadioButton
    properties.insert::<RadioButton, _>(BorderWidth {
        width: BORDER_WIDTH,
    });

    properties.insert::<RadioButton, _>(Background::Color(ZYNC_800));
    properties.insert::<RadioButton, _>(BorderColor { color: ZYNC_700 });
    properties.insert::<RadioButton, _>(CheckmarkColor { color: TEXT_COLOR });
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_active(true),
            Background::Color(ZYNC_700),
        );
        stack.push_layer(
            Selector::new().with_hovered(true),
            BorderColor { color: ZYNC_500 },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );
        stack.push_layer(
            Selector::new().with_disabled(true),
            (
                CheckmarkColor::new(DISABLED_TEXT_COLOR),
                Background::Color(Color::BLACK),
            ),
        );
        properties.insert_stack::<RadioButton>(stack);
    }

    // Slider
    properties.insert::<Slider, _>(TrackThickness(4.px()));
    properties.insert::<Slider, _>(TrackColor {
        active: ACCENT_COLOR,
        inactive: ZYNC_800,
    });
    properties.insert::<Slider, _>(ThumbColor(TEXT_COLOR));
    properties.insert::<Slider, _>(ThumbRadius(7.px()));
    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_hovered(true),
            BorderColor { color: ZYNC_500 },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );
        properties.insert_stack::<Slider>(stack);
    }

    // Spinner
    properties.insert::<Spinner, _>(TrackColor {
        active: TEXT_COLOR,
        inactive: Color::TRANSPARENT,
    });
    properties.insert::<Spinner, _>(TrackThickness(8.px()));
    properties.insert::<Spinner, _>(AnimationDuration { seconds: 1.2 });

    // StepInput
    default_step_input_style::<u8>(&mut properties);
    default_step_input_style::<i8>(&mut properties);
    default_step_input_style::<u16>(&mut properties);
    default_step_input_style::<i16>(&mut properties);
    default_step_input_style::<u32>(&mut properties);
    default_step_input_style::<i32>(&mut properties);
    default_step_input_style::<u64>(&mut properties);
    default_step_input_style::<i64>(&mut properties);
    default_step_input_style::<usize>(&mut properties);
    default_step_input_style::<isize>(&mut properties);
    default_step_input_style::<f32>(&mut properties);
    default_step_input_style::<f64>(&mut properties);

    properties
}

/// Applies the default text styles for Masonry into `styles`.
pub fn default_text_styles(styles: &mut StyleSet) {
    styles.insert(StyleProperty::LineHeight(LineHeight::FontSizeRelative(1.2)));
    styles.insert(GenericFamily::SystemUi.into());
}

fn default_step_input_style<T: Steppable>(properties: &mut DefaultProperties) {
    properties.insert::<StepInput<T>, _>(Padding::from_vh(6.px(), 0.px()));
    properties.insert::<StepInput<T>, _>(CornerRadius { radius: 6.px() });
    properties.insert::<StepInput<T>, _>(BorderWidth {
        width: BORDER_WIDTH,
    });

    properties.insert::<StepInput<T>, _>(ContentColor::new(TEXT_COLOR));
    properties.insert::<StepInput<T>, _>(Background::Color(ZYNC_800));
    properties.insert::<StepInput<T>, _>(BorderColor { color: ZYNC_700 });

    {
        let mut stack = PropertyStack::new();
        stack.push_layer(
            Selector::new().with_disabled(true),
            (
                ContentColor::new(DISABLED_TEXT_COLOR),
                Background::Color(Color::BLACK),
            ),
        );
        stack.push_layer(
            Selector::new().with_hovered(true),
            BorderColor { color: ZYNC_500 },
        );
        stack.push_layer(
            Selector::new().with_focused(true),
            BorderColor { color: FOCUS_COLOR },
        );

        properties.insert_stack::<StepInput<T>>(stack);
    }
}

/// Set of default properties used in unit tests.
///
/// This lets us change default properties without having to reset all screenshots every time.
/// This should still be kept relatively close to `default_property_set()` so that screenshots look like end user apps.
#[cfg(test)]
pub(crate) fn test_property_set() -> DefaultProperties {
    #[allow(unused_mut, reason = "Sometimes we don't have anything to change")]
    let mut properties = default_property_set();

    properties
}