gpui-material-extras 0.3.2

Optional Material 3 Expressive component families for GPUI
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use std::{cell::Cell, f32::consts::TAU, rc::Rc};

use chrono::Timelike;
use gpui::{
    AccessibleAction, AnyElement, App, Bounds, ElementId, Entity, Hsla, InteractiveElement as _,
    IntoElement, MouseButton, ParentElement as _, PathBuilder, Pixels, Point, RenderOnce, Role,
    SharedString, StatefulInteractiveElement as _, Styled as _, Window, canvas, div, point,
    prelude::FluentBuilder as _, px,
};
use gpui_base::{Button as BaseButton, TestSupportExt as _};
use gpui_material_components::{Text, TextField, TextFieldColors, TextFieldVariant};
use gpui_material_foundation::{color, current_material_theme};
use gpui_material_theme::{ColorRole, CornerRole, CornerShape, Direction, MaterialTheme, TypeRole};

use super::{
    TimeFormat, TimePickerLayout, TimePickerSegment, TimePickerState, TimePickerVariant,
    dialog::{ModeSlot, TimePickerModeAction, mode_action},
    display_hour, format_time,
};
use crate::date_time::{ScrollField, ScrollFieldColors, status};

/// Container and content colors for selected and unselected controls.
#[derive(Clone, Copy, Debug, Default)]
pub struct TimePickerSelectionColors {
    /// Background fill color when the control is selected.
    pub selected_container: Option<Hsla>,
    /// Background fill color when the control is unselected.
    pub container: Option<Hsla>,
    /// Foreground text color when the control is selected.
    pub selected_content: Option<Hsla>,
    /// Foreground text color when the control is unselected.
    pub content: Option<Hsla>,
}

/// Instance appearance for the dial, input, and scroll panels.
#[derive(Clone, Copy, Debug, Default)]
pub struct TimePickerStyle {
    /// Corner shape override for hour and minute selector boxes and inputs.
    pub time_field_shape: Option<CornerShape>,
    /// Corner shape override for the AM/PM period selector.
    pub period_selector_shape: Option<CornerShape>,
    /// Background fill color for the panel container.
    pub container: Option<Hsla>,
    /// Background fill color for the circular clock dial face.
    pub clock_dial: Option<Hsla>,
    /// Text color for unselected numbers on the clock dial.
    pub clock_dial_content: Option<Hsla>,
    /// Text color for the currently selected number on the clock dial.
    pub clock_dial_selected_content: Option<Hsla>,
    /// Accent color for the clock dial hand and selection handle.
    pub selector: Option<Hsla>,
    /// Colors for the hour and minute header selector tiles.
    pub time_selector: TimePickerSelectionColors,
    /// Colors for the AM/PM period selector buttons.
    pub period_selector: TimePickerSelectionColors,
    /// Outline border color around the AM/PM period selector.
    pub period_selector_border: Option<Hsla>,
    /// Color overrides applied to the text-input fields.
    pub input: TextFieldColors,
    /// Color overrides applied to the scroll-wheel fields.
    pub scroll: ScrollFieldColors,
}

#[derive(Clone, Copy)]
struct Presentation {
    style: TimePickerStyle,
    vibrant: bool,
}

impl Presentation {
    fn selector_shape(self) -> CornerShape {
        self.style.time_field_shape.unwrap_or_else(|| {
            if self.vibrant {
                CornerRole::Large
            } else {
                CornerRole::Small
            }
            .shape()
        })
    }

    fn period_shape(self) -> CornerShape {
        self.style.period_selector_shape.unwrap_or_else(|| {
            if self.vibrant {
                CornerRole::Full
            } else {
                CornerRole::Small
            }
            .shape()
        })
    }

    fn input_colors(self, theme: &MaterialTheme) -> TextFieldColors {
        let mut input = self.style.input;
        if self.vibrant {
            let tone = |role| color(theme.colors.color(role));
            for state in [&mut input.unfocused, &mut input.focused] {
                state
                    .container
                    .get_or_insert_with(|| tone(ColorRole::SurfaceContainerLowest));
            }
            input
                .focused
                .text
                .get_or_insert_with(|| tone(ColorRole::Primary));
            input
                .invalid
                .container
                .get_or_insert_with(|| tone(ColorRole::ErrorContainer));
            input
                .invalid
                .text
                .get_or_insert_with(|| tone(ColorRole::OnErrorContainer));
        }
        input
    }
}

/// An independently mountable Material time-editing panel.
#[derive(IntoElement)]
pub struct TimePickerPanel {
    id: ElementId,
    state: Entity<TimePickerState>,
    variant: Option<TimePickerVariant>,
    layout: TimePickerLayout,
    presentation: Presentation,
    toggle: Option<ModeSlot>,
}

impl TimePickerPanel {
    /// Creates a panel that follows `state.mode()` dynamically.
    pub fn new(id: impl Into<ElementId>, state: &Entity<TimePickerState>) -> Self {
        Self {
            id: id.into(),
            state: state.clone(),
            variant: None,
            layout: TimePickerLayout::Auto,
            presentation: Presentation {
                style: TimePickerStyle::default(),
                vibrant: false,
            },
            toggle: None,
        }
    }

    /// Creates a panel pinned to [`TimePickerVariant::Dial`].
    pub fn dial(id: impl Into<ElementId>, state: &Entity<TimePickerState>) -> Self {
        Self::new(id, state).fixed(TimePickerVariant::Dial)
    }

    /// Creates a panel pinned to [`TimePickerVariant::Input`].
    pub fn input(id: impl Into<ElementId>, state: &Entity<TimePickerState>) -> Self {
        Self::new(id, state).fixed(TimePickerVariant::Input)
    }

    fn fixed(mut self, variant: TimePickerVariant) -> Self {
        self.variant = Some(variant);
        self
    }

    /// Sets the dial layout orientation. Defaults to [`TimePickerLayout::Auto`].
    pub fn layout(mut self, layout: TimePickerLayout) -> Self {
        self.layout = layout;
        self
    }

    /// Overrides the panel's appearance and color tokens.
    pub fn style(mut self, style: TimePickerStyle) -> Self {
        self.presentation.style = style;
        self
    }

    /// Enables or disables the expressive vibrant styling preset.
    pub fn vibrant(mut self, vibrant: bool) -> Self {
        self.presentation.vibrant = vibrant;
        self
    }

    /// Renders a custom mode-toggle control inside the panel.
    pub fn mode_toggle_content<E: IntoElement>(
        mut self,
        content: impl FnOnce(TimePickerModeAction) -> E + 'static,
    ) -> Self {
        self.toggle = Some(Box::new(move |action| content(action).into_any_element()));
        self
    }
}

impl RenderOnce for TimePickerPanel {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let model = self.state.read(cx);
        let mode = self.variant.unwrap_or(model.mode);
        let appearance = self.presentation;
        let controls = match mode {
            TimePickerVariant::Dial => time_selector(&self.id, &self.state, appearance, window, cx),
            TimePickerVariant::Input => time_input(&self.id, &self.state, appearance, window, cx),
            TimePickerVariant::Scroll => time_scroll(&self.id, model, appearance, window, cx),
        };
        let period = (model.format == TimeFormat::TwelveHour)
            .then(|| period_selector(&self.id, &self.state, appearance, window, cx));
        let horizontal = mode == TimePickerVariant::Dial
            && window.viewport_size().width >= px(600.)
            && match self.layout {
                TimePickerLayout::Vertical => false,
                TimePickerLayout::Horizontal => true,
                TimePickerLayout::Auto => window.viewport_size().height < px(650.),
            };
        let rtl = current_material_theme(window, cx).layout.direction == Direction::Rtl;
        let toggle = self
            .toggle
            .map(|content| content(mode_action(&self.state, model, mode, false)));
        div()
            .id((self.id.clone(), "panel"))
            .track_focus(&model.panel)
            .flex()
            .gap(px(12.))
            .items_center()
            .when(!horizontal, gpui::Styled::flex_col)
            .when(horizontal && rtl, gpui::Styled::flex_row_reverse)
            .when_some(appearance.style.container, |body, container| {
                body.bg(container)
            })
            .child(
                div()
                    .min_w_0()
                    .w(px(216.))
                    .max_w_full()
                    .flex()
                    .flex_col()
                    .gap(px(8.))
                    .child(controls)
                    .children(period)
                    .children(toggle),
            )
            .children(
                (mode == TimePickerVariant::Dial)
                    .then(|| dial(&self.id, &self.state, appearance, window, cx)),
            )
    }
}

/// An uncontained vibrant panel with separate, wrapping hour and minute wheels.
#[derive(IntoElement)]
pub struct TimeScroll(TimePickerPanel);

impl TimeScroll {
    /// Creates a vibrant scroll-wheel time panel bound to `state`.
    pub fn new(id: impl Into<ElementId>, state: &Entity<TimePickerState>) -> Self {
        Self(
            TimePickerPanel::new(id, state)
                .fixed(TimePickerVariant::Scroll)
                .vibrant(true),
        )
    }

    /// Overrides the panel's appearance and color tokens.
    pub fn style(mut self, style: TimePickerStyle) -> Self {
        self.0 = self.0.style(style);
        self
    }

    /// Renders a custom mode-toggle control inside the scroll panel.
    pub fn mode_toggle_content<E: IntoElement>(
        mut self,
        content: impl FnOnce(TimePickerModeAction) -> E + 'static,
    ) -> Self {
        self.0 = self.0.mode_toggle_content(content);
        self
    }
}

impl RenderOnce for TimeScroll {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        self.0
    }
}

fn time_input(
    id: &ElementId,
    state: &Entity<TimePickerState>,
    appearance: Presentation,
    window: &Window,
    cx: &App,
) -> AnyElement {
    let model = state.read(cx);
    let state = state.clone();
    let theme = current_material_theme(window, cx);
    let colors = appearance.input_colors(&theme);
    let field = |segment, label: &SharedString, part| {
        let editor = model.field(segment);
        let invalid = model.input_attempted
            && model
                .parse_segment(segment, &editor.read(cx).value())
                .is_none();
        let control = TextField::new((id.clone(), part), label.clone(), editor)
            .variant(TextFieldVariant::Outlined)
            .colors(colors)
            .invalid(invalid)
            .when_some(
                appearance
                    .style
                    .time_field_shape
                    .or_else(|| appearance.vibrant.then(|| appearance.selector_shape())),
                |field, shape| field.shape(shape),
            );
        div().flex_1().min_w_0().child(control)
    };
    div()
        .capture_action(move |_: &gpui_base::input::Escape, window, cx| {
            if state.update(cx, |state, cx| state.finish_composition(window, cx)) {
                cx.stop_propagation();
            }
        })
        .flex()
        .flex_col()
        .gap(px(8.))
        .child(
            div()
                .flex()
                .items_start()
                .gap(px(12.))
                .w_full()
                .when(
                    theme.layout.direction == Direction::Rtl,
                    gpui::Styled::flex_row_reverse,
                )
                .child(field(TimePickerSegment::Hour, &model.locale.hour, "hour"))
                .child(field(
                    TimePickerSegment::Minute,
                    &model.locale.minute,
                    "minute",
                )),
        )
        .when(
            model.input_attempted && !model.is_input_valid(cx),
            |column| column.child(status(model.locale.validation_message.clone(), true)),
        )
        .into_any_element()
}

fn time_scroll(
    id: &ElementId,
    model: &TimePickerState,
    appearance: Presentation,
    window: &Window,
    cx: &App,
) -> AnyElement {
    let theme = current_material_theme(window, cx);
    let wheel = |segment, name: &SharedString, part| {
        let offset = usize::from(
            segment == TimePickerSegment::Hour && model.format == TimeFormat::TwelveHour,
        );
        let describe = model.locale.number.clone();
        let display = describe.clone();
        let scroll = ScrollField::new((id.clone(), part), name.clone(), model.wheel(segment))
            .colors(appearance.style.scroll)
            .time_field(appearance.style.time_field_shape, offset)
            .value_description(move |index| describe((index + offset) as u32))
            .item_content(move |index, selected, _| {
                Text::new(display((index + offset) as u32))
                    .role(if selected {
                        TypeRole::DisplayLarge
                    } else {
                        TypeRole::DisplayMedium
                    })
                    .emphasized(selected)
            });
        div().flex_1().min_w_0().child(scroll)
    };
    div()
        .flex()
        .w_full()
        .gap(px(4.))
        .when(
            theme.layout.direction == Direction::Rtl,
            gpui::Styled::flex_row_reverse,
        )
        .child(wheel(
            TimePickerSegment::Hour,
            &model.locale.hour,
            "hour-scroll",
        ))
        .child(wheel(
            TimePickerSegment::Minute,
            &model.locale.minute,
            "minute-scroll",
        ))
        .into_any_element()
}

fn time_selector(
    id: &ElementId,
    state: &Entity<TimePickerState>,
    appearance: Presentation,
    window: &Window,
    cx: &App,
) -> AnyElement {
    let model = state.read(cx);
    let (format, locale) = (model.format, &model.locale);
    let theme = current_material_theme(window, cx);
    let rtl = theme.layout.direction == Direction::Rtl;
    let tone = |role| color(theme.colors.color(role));
    let palette = appearance.style.time_selector;
    let [tl, tr, br, bl] = appearance
        .selector_shape()
        .radii(96., 80., theme.layout.direction)
        .map(px);
    let segment = |part, label: &SharedString, value| {
        let selected = part == model.segment;
        let state = state.clone();
        let text = (locale.number)(value);
        let action = Rc::new(move |window: &mut Window, cx: &mut App| {
            state.update(cx, |state, cx| {
                state.set_segment(part, cx);
                state.dial.focus(window, cx);
            });
        });
        let click = action.clone();
        BaseButton::new((
            id.clone(),
            if part == TimePickerSegment::Hour {
                "select-hour"
            } else {
                "select-minute"
            },
        ))
        .role(Role::RadioButton)
        .aria_toggled(if selected {
            gpui::Toggled::True
        } else {
            gpui::Toggled::False
        })
        .accessibility_label(format!("{label} {text}"))
        .on_a11y_action(AccessibleAction::Click, move |_, window, cx| {
            action(window, cx)
        })
        .on_click(move |_, window, cx| click(window, cx))
        .w(px(96.))
        .h(px(80.))
        .max_w_full()
        .min_w_0()
        .flex_shrink(1.)
        .rounded_tl(tl)
        .rounded_tr(tr)
        .rounded_br(br)
        .rounded_bl(bl)
        .bg(if appearance.vibrant {
            (if selected {
                palette.selected_container
            } else {
                palette.container
            })
            .unwrap_or_else(|| tone(ColorRole::SurfaceContainerLowest))
        } else if selected {
            palette
                .selected_container
                .unwrap_or_else(|| tone(ColorRole::PrimaryContainer))
        } else {
            palette
                .container
                .unwrap_or_else(|| tone(ColorRole::SurfaceContainerHighest))
        })
        .text_color(if selected {
            palette.selected_content.unwrap_or_else(|| {
                tone(if appearance.vibrant {
                    ColorRole::Primary
                } else {
                    ColorRole::OnPrimaryContainer
                })
            })
        } else {
            palette
                .content
                .unwrap_or_else(|| tone(ColorRole::OnSurface))
        })
        .when(appearance.vibrant && selected, |button| {
            button.border_2().border_color(tone(ColorRole::Primary))
        })
        .child(Text::new(text).role(TypeRole::DisplayMedium))
    };
    div()
        .flex()
        .items_center()
        .justify_center()
        .gap(px(4.))
        .when(rtl, gpui::Styled::flex_row_reverse)
        .child(segment(
            TimePickerSegment::Hour,
            &locale.hour,
            display_hour(model.draft, format),
        ))
        .child(Text::new(locale.separator.clone()).role(TypeRole::DisplayMedium))
        .child(segment(
            TimePickerSegment::Minute,
            &locale.minute,
            model.draft.minute(),
        ))
        .into_any_element()
}

fn period_selector(
    id: &ElementId,
    state: &Entity<TimePickerState>,
    appearance: Presentation,
    window: &Window,
    cx: &App,
) -> AnyElement {
    let model = state.read(cx);
    let locale = &model.locale;
    let theme = current_material_theme(window, cx);
    let rtl = theme.layout.direction == Direction::Rtl;
    let tone = |role| color(theme.colors.color(role));
    let palette = appearance.style.period_selector;
    let [tl, tr, br, bl] = appearance
        .period_shape()
        .radii(108., 38., theme.layout.direction)
        .map(px);
    let button = |pm, label: SharedString| {
        let selected = (model.draft.hour() >= 12) == pm;
        let state = state.clone();
        let activate = Rc::new(move |_: &mut Window, cx: &mut App| {
            state.update(cx, |state, cx| {
                let next = state
                    .draft
                    .with_hour(state.draft.hour() % 12 + if pm { 12 } else { 0 })
                    .unwrap();
                if state.draft != next {
                    state.draft = next;
                    cx.notify();
                }
            });
        });
        let click = activate.clone();
        BaseButton::new((id.clone(), if pm { "pm" } else { "am" }))
            .role(Role::RadioButton)
            .aria_toggled(if selected {
                gpui::Toggled::True
            } else {
                gpui::Toggled::False
            })
            .accessibility_label(label.clone())
            .on_a11y_action(AccessibleAction::Click, move |_, window, cx| {
                activate(window, cx)
            })
            .on_click(move |_, window, cx| click(window, cx))
            .flex_1()
            .h(px(38.))
            .rounded_tl(tl)
            .rounded_tr(tr)
            .rounded_br(br)
            .rounded_bl(bl)
            .when_some(
                if selected {
                    Some(palette.selected_container.unwrap_or_else(|| {
                        tone(if appearance.vibrant {
                            ColorRole::PrimaryContainer
                        } else {
                            ColorRole::TertiaryContainer
                        })
                    }))
                } else {
                    palette.container.or_else(|| {
                        appearance
                            .vibrant
                            .then(|| tone(ColorRole::SurfaceContainerLowest))
                    })
                },
                |button, container| button.bg(container),
            )
            .text_color(if selected {
                palette.selected_content.unwrap_or_else(|| {
                    tone(if appearance.vibrant {
                        ColorRole::OnPrimaryContainer
                    } else {
                        ColorRole::OnTertiaryContainer
                    })
                })
            } else {
                palette
                    .content
                    .unwrap_or_else(|| tone(ColorRole::OnSurfaceVariant))
            })
            .child(Text::new(label).role(TypeRole::TitleMedium))
    };
    div()
        .flex()
        .w_full()
        .rounded_tl(tl)
        .rounded_tr(tr)
        .rounded_br(br)
        .rounded_bl(bl)
        .border_1()
        .border_color(
            appearance
                .style
                .period_selector_border
                .unwrap_or_else(|| tone(ColorRole::Outline)),
        )
        .when(rtl, gpui::Styled::flex_row_reverse)
        .child(button(false, locale.am.clone()))
        .child(button(true, locale.pm.clone()))
        .into_any_element()
}

fn polar(step: u32, divisions: u32, radius: Pixels) -> Point<Pixels> {
    let angle = step as f32 * TAU / divisions as f32;
    point(radius * angle.sin(), -radius * angle.cos())
}

fn dial_position(
    value: u32,
    segment: TimePickerSegment,
    format: TimeFormat,
    diameter: Pixels,
) -> Point<Pixels> {
    if segment == TimePickerSegment::Minute {
        return polar(value, 60, diameter * 0.365);
    }
    let inner = format == TimeFormat::TwentyFourHour && (value == 0 || value > 12);
    polar(value % 12, 12, diameter * if inner { 0.205 } else { 0.365 })
}

fn dial_value(
    offset: Point<Pixels>,
    segment: TimePickerSegment,
    format: TimeFormat,
    diameter: Pixels,
) -> u32 {
    let (x, y) = (f32::from(offset.x), f32::from(offset.y));
    let turn = x.atan2(-y).rem_euclid(TAU) / TAU;
    if segment == TimePickerSegment::Minute {
        return ((turn * 60.).round() as u32) % 60;
    }
    let hour = ((turn * 12.).round() as u32) % 12;
    let inner = format == TimeFormat::TwentyFourHour && x.hypot(y) < f32::from(diameter * 0.285);
    match (inner, hour) {
        (true, 0) => 0,
        (true, _) => hour + 12,
        (_, 0) => 12,
        _ => hour,
    }
}

const MINUTE_MARKS: &[u32] = &[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
const TWELVE_HOUR_MARKS: &[u32] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const TWENTY_FOUR_HOUR_MARKS: &[u32] = &[
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
];
const MARK_IDS: [&str; 60] = [
    "mark-0", "mark-1", "mark-2", "mark-3", "mark-4", "mark-5", "mark-6", "mark-7", "mark-8",
    "mark-9", "mark-10", "mark-11", "mark-12", "mark-13", "mark-14", "mark-15", "mark-16",
    "mark-17", "mark-18", "mark-19", "mark-20", "mark-21", "mark-22", "mark-23", "mark-24",
    "mark-25", "mark-26", "mark-27", "mark-28", "mark-29", "mark-30", "mark-31", "mark-32",
    "mark-33", "mark-34", "mark-35", "mark-36", "mark-37", "mark-38", "mark-39", "mark-40",
    "mark-41", "mark-42", "mark-43", "mark-44", "mark-45", "mark-46", "mark-47", "mark-48",
    "mark-49", "mark-50", "mark-51", "mark-52", "mark-53", "mark-54", "mark-55", "mark-56",
    "mark-57", "mark-58", "mark-59",
];

fn dial(
    id: &ElementId,
    state: &Entity<TimePickerState>,
    appearance: Presentation,
    window: &Window,
    cx: &App,
) -> AnyElement {
    let model = state.read(cx);
    let (format, locale) = (model.format, &model.locale);
    let segment = model.segment;
    let focus = model.dial.clone();
    let selected = if segment == TimePickerSegment::Hour {
        display_hour(model.draft, format)
    } else {
        model.draft.minute()
    };
    let label = if segment == TimePickerSegment::Hour {
        &locale.hour
    } else {
        &locale.minute
    };
    let theme = current_material_theme(window, cx);
    let tone = |role| color(theme.colors.color(role));
    let diameter = (window.viewport_size().width - px(110.)).clamp(px(120.), px(256.));
    let center = diameter / 2.;
    let handle = diameter * (48. / 256.);
    let selected_position = dial_position(selected, segment, format, diameter);
    let bounds = Rc::new(Cell::new(Bounds::default()));
    let measured = bounds.clone();
    let accent = appearance
        .style
        .selector
        .unwrap_or_else(|| tone(ColorRole::Primary));
    let content = appearance
        .style
        .clock_dial_content
        .unwrap_or_else(|| tone(ColorRole::OnSurface));
    let selected_content = appearance
        .style
        .clock_dial_selected_content
        .unwrap_or_else(|| tone(ColorRole::OnPrimary));
    let track = canvas(
        move |bounds, _, _| {
            measured.set(bounds);
        },
        move |bounds, _, window, _| {
            let start = bounds.center();
            let mut line = PathBuilder::stroke(px(2.));
            line.move_to(start);
            line.line_to(start + selected_position);
            if let Ok(path) = line.build() {
                window.paint_path(path, accent);
            }
        },
    )
    .absolute()
    .size_full();
    let key_state = state.clone();
    let click_state = state.clone();
    let click_focus = focus.clone();
    let keyboard_focus = focus.is_focused(window) && window.last_input_was_keyboard();
    let marks: &[u32] = match (segment, format) {
        (TimePickerSegment::Minute, _) => MINUTE_MARKS,
        (_, TimeFormat::TwelveHour) => TWELVE_HOUR_MARKS,
        (_, TimeFormat::TwentyFourHour) => TWENTY_FOUR_HOUR_MARKS,
    };
    div()
        .id((id.clone(), "dial"))
        .test_support()
        .role(Role::Group)
        .aria_label(format!(
            "{label}, {}",
            format_time(model.draft, format, locale)
        ))
        .track_focus(&focus)
        .tab_stop(true)
        .size(diameter)
        .flex_none()
        .relative()
        .rounded_full()
        .bg(appearance.style.clock_dial.unwrap_or_else(|| {
            tone(if appearance.vibrant {
                ColorRole::SurfaceContainerLowest
            } else {
                ColorRole::SurfaceContainerHighest
            })
        }))
        .when(keyboard_focus, |dial| {
            dial.border_2().border_color(tone(ColorRole::Secondary))
        })
        .child(track)
        .child(
            div()
                .absolute()
                .size(px(8.))
                .left(center - px(4.))
                .top(center - px(4.))
                .rounded_full()
                .bg(accent),
        )
        .child(
            div()
                .absolute()
                .size(handle)
                .left(center + selected_position.x - handle / 2.)
                .top(center + selected_position.y - handle / 2.)
                .rounded_full()
                .bg(accent),
        )
        .on_mouse_down(MouseButton::Left, move |event, window, cx| {
            let offset = event.position - bounds.get().center();
            let distance = f32::from(offset.x).hypot(f32::from(offset.y));
            if distance <= f32::from(diameter / 2.) {
                click_focus.focus(window, cx);
                let value = dial_value(offset, segment, format, diameter);
                click_state.update(cx, |state, cx| state.select(value, true, window, cx));
            }
        })
        .capture_key_down(move |event, window, cx| {
            if !focus.is_focused(window) || event.keystroke.modifiers.modified() {
                return;
            }
            let divisions = match (segment, format) {
                (TimePickerSegment::Minute, _) => 60,
                (_, TimeFormat::TwelveHour) => 12,
                (_, TimeFormat::TwentyFourHour) => 24,
            };
            let value = match event.keystroke.key.as_str() {
                "left" | "down" => Some((selected + divisions - 1) % divisions),
                "up" | "right" => Some((selected + 1) % divisions),
                "home" => Some(0),
                "end" => Some(divisions - 1),
                "space" if segment == TimePickerSegment::Hour => Some(selected),
                _ => None,
            };
            if let Some(value) = value {
                key_state.update(cx, |state, cx| {
                    state.select(value, event.keystroke.key == "space", window, cx)
                });
                cx.stop_propagation();
            }
        })
        .children(marks.iter().copied().map(|value| {
            let position = dial_position(value, segment, format, diameter);
            let active = value == selected;
            let label = (locale.number)(value);
            let state = state.clone();
            let activate = Rc::new(move |window: &mut Window, cx: &mut App| {
                state.update(cx, |state, cx| {
                    state.dial.focus(window, cx);
                    state.select(value, true, window, cx);
                });
            });
            let click = activate.clone();
            BaseButton::new((id.clone(), MARK_IDS[value as usize]))
                .role(Role::ListBoxOption)
                .aria_selected(active)
                .accessibility_label(label.clone())
                .tab_stop(false)
                .absolute()
                .size(handle)
                .left(center + position.x - handle / 2.)
                .top(center + position.y - handle / 2.)
                .on_mouse_down(MouseButton::Left, move |_, _, cx| cx.stop_propagation())
                .on_a11y_action(AccessibleAction::Click, move |_, window, cx| {
                    activate(window, cx)
                })
                .on_click(move |_, window, cx| click(window, cx))
                .text_color(if active { selected_content } else { content })
                .child(Text::new(label).role(TypeRole::BodyLarge))
        }))
        .into_any_element()
}

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

    #[test]
    fn dial_position_round_trips_both_hour_rings_and_every_minute() {
        let size = px(256.);
        for (segment, format, values) in [
            (TimePickerSegment::Hour, TimeFormat::TwentyFourHour, 0..24),
            (TimePickerSegment::Hour, TimeFormat::TwelveHour, 1..13),
            (TimePickerSegment::Minute, TimeFormat::TwentyFourHour, 0..60),
        ] {
            for value in values {
                let point = dial_position(value, segment, format, size);
                assert_eq!(dial_value(point, segment, format, size), value);
            }
        }
    }
}