icedtea 0.17.2

Native desktop widgets and chrome for iced
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
//! M3 shape scale and component → corner map.
//!
//! See <https://m3.material.io/styles/shape/overview>.

use iced::border::Radius;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Shape {
    None,
    ExtraSmall,
    Small,
    Medium,
    Large,
    LargeIncreased,
    ExtraLarge,
    ExtraLargeIncreased,
    ExtraExtraLarge,
    Full,
}

impl Shape {
    /// Ten-step M3 corner scale plus Full (snapshot `styles/shape`).
    pub const STEPS: [(Self, f32); 10] = [
        (Self::None, 0.0),
        (Self::ExtraSmall, 4.0),
        (Self::Small, 8.0),
        (Self::Medium, 12.0),
        (Self::Large, 16.0),
        (Self::LargeIncreased, 20.0),
        (Self::ExtraLarge, 28.0),
        (Self::ExtraLargeIncreased, 32.0),
        (Self::ExtraExtraLarge, 48.0),
        (Self::Full, 9999.0),
    ];

    pub fn dp(self) -> f32 {
        match self {
            Self::None => 0.0,
            Self::ExtraSmall => 4.0,
            Self::Small => 8.0,
            Self::Medium => 12.0,
            Self::Large => 16.0,
            Self::LargeIncreased => 20.0,
            Self::ExtraLarge => 28.0,
            Self::ExtraLargeIncreased => 32.0,
            Self::ExtraExtraLarge => 48.0,
            Self::Full => 9999.0,
        }
    }
    pub fn radius(self) -> Radius {
        self.dp().into()
    }
}

/// Closed public surface → M3 corner shape (one path per control family).
///
/// icedtea is **desktop-first**: all families use M3 shape **None** (0 dp)
/// so chrome reads crisp and rectangular. Touch-pill Full / mobile Soft are
/// not the product default; apps that want round can restyle later.
///
/// Switch thumbs stay circular via iced geometry. The track uses
/// [`Component::Track`] (Full under Material and Pill).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Component {
    /// Filled / tonal / text buttons, chips.
    Button,
    Chip,
    /// Count or status mark (M3 Badge).
    Badge,
    /// Text fields, pickers, checkbox container.
    Field,
    Checkbox,
    /// Cards, menus, list card rows.
    Card,
    Menu,
    /// Dialog / modal sheet.
    Dialog,
    /// Transient notice (M3 snackbar).
    Toast,
    /// Hover tip.
    Tooltip,
    /// Page-level message (M3 banner). Flush under Material and Pill.
    Banner,
    /// Search field (M3 search). Extra-large under Material, full under Pill.
    Search,
    /// Switch track, slider rail, linear progress.
    Track,
    /// App bar / shell / tab strip (flush).
    AppBar,
    Shell,
    /// Tab *label* is flush; active indicator is a separate underbar.
    Tab,
    /// Exclusive in-pane segment (joined strip). Flush; not a stadium.
    Segment,
}

/// How constructors pick a corner from [`Component`].
///
/// Default is [`Self::Desktop`] (every family 0 dp). Apps that want
/// Material corners, or one radius on rounded families, set this on
/// [`crate::theme::Tokens`]. Flush chrome (tabs, app bars, banners,
/// exclusive segments) stays shape None under every policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ShapePolicy {
    /// Every family is M3 None (0 dp). Desktop default.
    #[default]
    Desktop,
    /// Extra-small (4 dp) on rounded families. Flush chrome stays None.
    Tight,
    /// Medium (12 dp) on rounded families. Flush chrome stays None.
    /// Checkbox stays extra-small so the box is not a circle.
    Soft,
    /// Full stadium on buttons, chips, badges, search, and tracks.
    /// Cards, fields, menus, dialogs, toasts, and tooltips stay boxes.
    /// Tabs, app bars, banners, and exclusive segments stay flush.
    Pill,
    /// Documented Material map (buttons extra-small, chips and badges
    /// small, cards medium, toasts and tooltips extra-small, dialogs
    /// and search extra-large, tracks full, flush chrome None).
    Material,
}

impl Component {
    /// Desktop flat: every family is M3 shape None (0 dp).
    pub fn shape(self) -> Shape {
        self.shape_for(ShapePolicy::Desktop)
    }

    /// Corner for this family under `policy`.
    pub fn shape_for(self, policy: ShapePolicy) -> Shape {
        match policy {
            ShapePolicy::Desktop => Shape::None,
            ShapePolicy::Tight => {
                if self.is_flush() {
                    Shape::None
                } else {
                    Shape::ExtraSmall
                }
            }
            ShapePolicy::Soft => {
                if self.is_flush() {
                    Shape::None
                } else if matches!(self, Self::Checkbox) {
                    Shape::ExtraSmall
                } else {
                    Shape::Medium
                }
            }
            ShapePolicy::Pill => self.pill_shape(),
            ShapePolicy::Material => self.material_shape(),
        }
    }

    /// Tabs, app bars, banners, and exclusive segments stay rectangular.
    pub fn is_flush(self) -> bool {
        matches!(
            self,
            Self::AppBar | Self::Shell | Self::Tab | Self::Banner | Self::Segment
        )
    }

    fn pill_shape(self) -> Shape {
        match self {
            Self::Button | Self::Chip | Self::Badge | Self::Search | Self::Track => Shape::Full,
            Self::AppBar | Self::Shell | Self::Tab | Self::Banner | Self::Segment => Shape::None,
            Self::Checkbox => Shape::ExtraSmall,
            Self::Field | Self::Card | Self::Menu | Self::Dialog | Self::Toast | Self::Tooltip => {
                Shape::Medium
            }
        }
    }

    fn material_shape(self) -> Shape {
        match self {
            Self::Button
            | Self::Field
            | Self::Checkbox
            | Self::Menu
            | Self::Toast
            | Self::Tooltip => Shape::ExtraSmall,
            Self::Chip | Self::Badge => Shape::Small,
            Self::Card => Shape::Medium,
            Self::Dialog | Self::Search => Shape::ExtraLarge,
            Self::Track => Shape::Full,
            Self::AppBar | Self::Shell | Self::Tab | Self::Banner | Self::Segment => Shape::None,
        }
    }

    pub fn radius(self) -> Radius {
        self.shape().radius()
    }

    pub fn radius_for(self, policy: ShapePolicy) -> Radius {
        self.shape_for(policy).radius()
    }

    /// Material resting elevation (snapshot `styles/elevation`).
    ///
    /// Search is Level 0: the current Search page dropped the old
    /// 6 dp island and paints no shadow. Toast (snackbar) is Level 3.
    /// Elevated *faces* (elevated button, elevated card, elevated chip)
    /// are Level 1 on the constructor, not this family rest.
    pub fn elevation(self) -> crate::m3::Elevation {
        use crate::m3::Elevation;
        match self {
            Self::Dialog | Self::Toast => Elevation::Level3,
            Self::Menu | Self::Tooltip => Elevation::Level2,
            Self::Card | Self::Banner => Elevation::Level1,
            _ => Elevation::Level0,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Corner {
    #[default]
    None,
    Tight,
    Soft,
    Pill,
}

impl Corner {
    pub fn shape(self) -> Shape {
        match self {
            Self::None => Shape::None,
            Self::Tight => Shape::ExtraSmall,
            Self::Soft => Shape::Medium,
            Self::Pill => Shape::Full,
        }
    }
    pub fn radius_px(self) -> f32 {
        self.shape().dp()
    }
    pub fn radius(self) -> Radius {
        self.shape().radius()
    }
}

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

    fn must(ok: bool, msg: impl std::fmt::Display) {
        if !ok {
            panic!("{msg}");
        }
    }

    #[test]
    #[should_panic(expected = "cover-must")]
    fn must_rejects_a_failed_check() {
        must(false, "cover-must");
    }

    #[test]
    fn shape_scale() {
        for (step, dp) in Shape::STEPS {
            assert_eq!(step.dp(), dp, "{step:?}");
            let _ = step.radius();
        }
        assert_eq!(
            Shape::STEPS.map(|(_, d)| d),
            [0.0, 4.0, 8.0, 12.0, 16.0, 20.0, 28.0, 32.0, 48.0, 9999.0]
        );
        assert!(Shape::Small.dp() < Shape::Large.dp());
        assert!(Shape::Large.dp() < Shape::LargeIncreased.dp());
        assert!(Shape::ExtraLarge.dp() < Shape::ExtraLargeIncreased.dp());
        assert_eq!(Corner::None.radius_px(), 0.0);
        assert_eq!(Corner::None.radius().top_left, 0.0);
        assert_eq!(Corner::Tight.radius_px(), 4.0);
        assert_eq!(Corner::Soft.radius_px(), 12.0);
        assert_eq!(Corner::Pill.shape(), Shape::Full);
        assert_eq!(Corner::default(), Corner::None);
        // Desktop flat policy: all components map to M3 None (0 dp).
        for c in [
            Component::Button,
            Component::Chip,
            Component::Badge,
            Component::Field,
            Component::Checkbox,
            Component::Card,
            Component::Menu,
            Component::Dialog,
            Component::Toast,
            Component::Tooltip,
            Component::Banner,
            Component::Search,
            Component::Track,
            Component::AppBar,
            Component::Shell,
            Component::Tab,
            Component::Segment,
        ] {
            assert_eq!(c.shape(), Shape::None, "{c:?}");
            assert_eq!(c.shape().dp(), 0.0);
            assert_eq!(std::hint::black_box(c.radius()).top_left, 0.0);
            assert_eq!(c.shape_for(ShapePolicy::Desktop), Shape::None);
            if c.is_flush() {
                assert_eq!(c.shape_for(ShapePolicy::Tight), Shape::None, "{c:?}");
                assert_eq!(c.shape_for(ShapePolicy::Soft), Shape::None, "{c:?}");
                assert_eq!(c.shape_for(ShapePolicy::Pill), Shape::None, "{c:?}");
                assert_eq!(c.shape_for(ShapePolicy::Material), Shape::None, "{c:?}");
            } else if matches!(c, Component::Checkbox) {
                assert_eq!(c.shape_for(ShapePolicy::Tight), Shape::ExtraSmall);
                assert_eq!(c.shape_for(ShapePolicy::Soft), Shape::ExtraSmall);
            } else {
                assert_eq!(c.shape_for(ShapePolicy::Tight), Shape::ExtraSmall, "{c:?}");
                assert_eq!(c.shape_for(ShapePolicy::Soft), Shape::Medium, "{c:?}");
            }
        }
        assert_eq!(Component::Button.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(Component::Chip.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(Component::Badge.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(Component::Card.shape_for(ShapePolicy::Pill), Shape::Medium);
        assert_eq!(Component::Menu.shape_for(ShapePolicy::Pill), Shape::Medium);
        assert_eq!(
            Component::Dialog.shape_for(ShapePolicy::Pill),
            Shape::Medium
        );
        assert_eq!(Component::Toast.shape_for(ShapePolicy::Pill), Shape::Medium);
        assert_eq!(
            Component::Tooltip.shape_for(ShapePolicy::Pill),
            Shape::Medium
        );
        assert_eq!(Component::Field.shape_for(ShapePolicy::Pill), Shape::Medium);
        assert_eq!(
            Component::Checkbox.shape_for(ShapePolicy::Pill),
            Shape::ExtraSmall
        );
        assert_eq!(Component::AppBar.shape_for(ShapePolicy::Pill), Shape::None);
        assert_eq!(Component::Shell.shape_for(ShapePolicy::Pill), Shape::None);
        assert_eq!(Component::Tab.shape_for(ShapePolicy::Pill), Shape::None);
        assert_eq!(Component::Banner.shape_for(ShapePolicy::Pill), Shape::None);
        assert_eq!(Component::Segment.shape_for(ShapePolicy::Pill), Shape::None);
        assert_eq!(Component::Search.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(Component::Track.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(
            Component::Button.shape_for(ShapePolicy::Material),
            Shape::ExtraSmall
        );
        assert_eq!(
            Component::Chip.shape_for(ShapePolicy::Material),
            Shape::Small
        );
        assert_eq!(
            Component::Badge.shape_for(ShapePolicy::Material),
            Shape::Small
        );
        assert_eq!(
            Component::Card.shape_for(ShapePolicy::Material),
            Shape::Medium
        );
        assert_eq!(
            Component::Dialog.shape_for(ShapePolicy::Material),
            Shape::ExtraLarge
        );
        assert_eq!(
            Component::AppBar.shape_for(ShapePolicy::Material),
            Shape::None
        );
        assert_eq!(
            Component::Field.shape_for(ShapePolicy::Material),
            Shape::ExtraSmall
        );
        assert_eq!(
            Component::Checkbox.shape_for(ShapePolicy::Material),
            Shape::ExtraSmall
        );
        assert_eq!(
            Component::Menu.shape_for(ShapePolicy::Material),
            Shape::ExtraSmall
        );
        assert_eq!(
            Component::Toast.shape_for(ShapePolicy::Material),
            Shape::ExtraSmall
        );
        assert_eq!(
            Component::Tooltip.shape_for(ShapePolicy::Material),
            Shape::ExtraSmall
        );
        assert_eq!(
            Component::Shell.shape_for(ShapePolicy::Material),
            Shape::None
        );
        assert_eq!(Component::Tab.shape_for(ShapePolicy::Material), Shape::None);
        assert_eq!(
            Component::Segment.shape_for(ShapePolicy::Material),
            Shape::None
        );
        assert_eq!(
            Component::Banner.shape_for(ShapePolicy::Material),
            Shape::None
        );
        assert_eq!(
            Component::Search.shape_for(ShapePolicy::Material),
            Shape::ExtraLarge
        );
        assert_eq!(
            Component::Track.shape_for(ShapePolicy::Material),
            Shape::Full
        );
        assert_eq!(Component::Dialog.elevation(), crate::m3::Elevation::Level3);
        assert_eq!(Component::Toast.elevation(), crate::m3::Elevation::Level3);
        assert_eq!(Component::Search.elevation(), crate::m3::Elevation::Level0);
        assert_eq!(Component::Menu.elevation(), crate::m3::Elevation::Level2);
        assert_eq!(Component::Tooltip.elevation(), crate::m3::Elevation::Level2);
        assert_eq!(Component::Card.elevation(), crate::m3::Elevation::Level1);
        assert_eq!(Component::Banner.elevation(), crate::m3::Elevation::Level1);
        assert_eq!(Component::Button.elevation(), crate::m3::Elevation::Level0);
        assert_eq!(Component::Chip.elevation(), crate::m3::Elevation::Level0);
        assert_eq!(Component::Field.elevation(), crate::m3::Elevation::Level0);
        assert_eq!(Component::AppBar.elevation(), crate::m3::Elevation::Level0);
        assert_eq!(
            Component::Field.radius_for(ShapePolicy::Tight).top_left,
            4.0
        );
        for policy in [
            ShapePolicy::Desktop,
            ShapePolicy::Tight,
            ShapePolicy::Soft,
            ShapePolicy::Pill,
            ShapePolicy::Material,
        ] {
            must(
                Component::Menu.shape_for(policy) == Component::Field.shape_for(policy),
                format!("menu overlay matches the closed field on {policy:?}"),
            );
        }
        assert_eq!(ShapePolicy::default(), ShapePolicy::Desktop);
    }

    #[test]
    fn flush_chrome_stays_none_on_every_policy() {
        for c in [
            Component::Tab,
            Component::AppBar,
            Component::Shell,
            Component::Banner,
            Component::Segment,
        ] {
            assert!(c.is_flush(), "{c:?}");
            for policy in [
                ShapePolicy::Desktop,
                ShapePolicy::Tight,
                ShapePolicy::Soft,
                ShapePolicy::Pill,
                ShapePolicy::Material,
            ] {
                assert_eq!(c.shape_for(policy), Shape::None, "{c:?} {policy:?}");
                assert_eq!(c.radius_for(policy).top_left, 0.0, "{c:?} {policy:?}");
            }
        }
        assert!(!Component::Button.is_flush());
        assert!(!Component::Chip.is_flush());
        assert_eq!(Component::Button.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(Component::Chip.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(Component::Search.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_eq!(Component::Track.shape_for(ShapePolicy::Pill), Shape::Full);
        assert_ne!(
            Component::Segment.shape_for(ShapePolicy::Pill),
            Component::Button.shape_for(ShapePolicy::Pill)
        );
    }
}