Skip to main content

gpui_material_extras/date_time/time/
state.rs

1use gpui::{
2    App, AppContext as _, Context, Entity, EntityInputHandler as _, FocusHandle, Focusable as _,
3    SharedString, Window,
4};
5use gpui_material_components::{DialogState, TextFieldEvent, TextFieldState};
6use jiff::civil::Time;
7
8use super::types::{
9    TimeFormat, TimePickerLocale, TimePickerSegment, TimePickerVariant, display_hour, parse_segment,
10};
11use crate::date_time::ScrollFieldState;
12
13/// Owns one editable time, its input fields, selection wheels, and dialog session.
14pub struct TimePickerState {
15    pub(super) draft: Time,
16    pub(super) segment: TimePickerSegment,
17    pub(super) mode: TimePickerVariant,
18    pub(super) picker_mode: TimePickerVariant,
19    pub(super) format: TimeFormat,
20    pub(super) locale: TimePickerLocale,
21    pub(super) input_attempted: bool,
22    pub(super) dial_dragging: bool,
23    pub(super) mode_revision: u64,
24    pub(super) hour: Entity<TextFieldState>,
25    pub(super) minute: Entity<TextFieldState>,
26    pub(super) hours: Entity<ScrollFieldState>,
27    pub(super) minutes: Entity<ScrollFieldState>,
28    pub(super) dialog: Entity<DialogState>,
29    pub(super) trigger: FocusHandle,
30    pub(super) panel: FocusHandle,
31    pub(super) dial: FocusHandle,
32}
33
34impl TimePickerState {
35    /// Creates a time picker state initialized to `00:00` in 24-hour dial mode.
36    pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
37        let hour = cx.new(|cx| TextFieldState::new(window, cx));
38        let minute = cx.new(|cx| TextFieldState::new(window, cx));
39        let hours = cx.new(|cx| ScrollFieldState::new(24, 0, cx));
40        let minutes = cx.new(|cx| ScrollFieldState::new(60, 0, cx));
41        let dialog = cx.new(DialogState::new);
42        for (segment, editor, wheel) in [
43            (TimePickerSegment::Hour, &hour, &hours),
44            (TimePickerSegment::Minute, &minute, &minutes),
45        ] {
46            cx.subscribe_in(editor, window, move |state, editor, event, window, cx| {
47                if matches!(event, TextFieldEvent::Change) && !has_marked_text(editor, window, cx) {
48                    let text = editor.read(cx).value();
49                    state.apply_input(segment, &text, cx);
50                }
51            })
52            .detach();
53            cx.observe_in(wheel, window, move |state, wheel, window, cx| {
54                let wheel = wheel.read(cx);
55                if !wheel.is_scroll_in_progress()
56                    && let Some(index) = wheel.selected_option()
57                    && index != state.wheel_index(segment)
58                {
59                    let mut value = index as u32;
60                    if segment == TimePickerSegment::Hour && state.format == TimeFormat::TwelveHour
61                    {
62                        value = (value + 1) % 12 + state.hour() / 12 * 12;
63                    }
64                    if !state
65                        .segment_focus(state.segment, TimePickerVariant::Scroll, cx)
66                        .is_focused(window)
67                    {
68                        state.set_segment(segment, cx);
69                    }
70                    state.update_draft(segment, value);
71                    state.sync_field(segment, window, cx);
72                    cx.notify();
73                }
74            })
75            .detach();
76            for focus in [
77                editor.read(cx).focus_handle(cx),
78                wheel.read(cx).focus_handle().clone(),
79            ] {
80                cx.on_focus(&focus, window, move |state, _, cx| {
81                    state.set_segment(segment, cx)
82                })
83                .detach();
84            }
85        }
86        cx.observe(&dialog, |_, _, cx| cx.notify()).detach();
87        let state = Self {
88            draft: Time::midnight(),
89            segment: TimePickerSegment::Hour,
90            mode: TimePickerVariant::Dial,
91            picker_mode: TimePickerVariant::Dial,
92            format: TimeFormat::TwentyFourHour,
93            locale: TimePickerLocale::default(),
94            input_attempted: false,
95            dial_dragging: false,
96            mode_revision: 0,
97            hour,
98            minute,
99            hours,
100            minutes,
101            dialog,
102            trigger: cx.focus_handle(),
103            panel: cx.focus_handle(),
104            dial: cx.focus_handle(),
105        };
106        state.sync_fields(window, cx);
107        state
108    }
109
110    /// The latest valid draft, at minute precision.
111    pub fn value(&self) -> Time {
112        self.draft
113    }
114
115    /// Returns the draft hour in 24-hour format (`0`–`23`).
116    pub fn hour(&self) -> u32 {
117        u32::from(self.draft.hour() as u8)
118    }
119
120    /// Returns the draft minute (`0`–`59`).
121    pub fn minute(&self) -> u32 {
122        u32::from(self.draft.minute() as u8)
123    }
124
125    /// Returns `true` when the draft hour is `12` or later.
126    pub fn is_pm(&self) -> bool {
127        self.hour() >= 12
128    }
129
130    /// Returns the currently active editing segment ([`TimePickerSegment::Hour`] or [`TimePickerSegment::Minute`]).
131    pub fn segment(&self) -> TimePickerSegment {
132        self.segment
133    }
134
135    /// Returns the active editor mode ([`TimePickerVariant`]).
136    pub fn mode(&self) -> TimePickerVariant {
137        self.mode
138    }
139
140    /// Returns the active 12-hour or 24-hour [`TimeFormat`].
141    pub fn format(&self) -> TimeFormat {
142        self.format
143    }
144
145    /// Returns the raw text currently in the hour input field.
146    pub fn hour_input(&self, cx: &App) -> SharedString {
147        self.hour.read(cx).value()
148    }
149
150    /// Returns the raw text currently in the minute input field.
151    pub fn minute_input(&self, cx: &App) -> SharedString {
152        self.minute.read(cx).value()
153    }
154
155    /// Replaces the text in the hour input field and updates the draft if valid.
156    pub fn set_hour_input(
157        &mut self,
158        text: impl Into<SharedString>,
159        window: &mut Window,
160        cx: &mut Context<Self>,
161    ) {
162        self.set_input(TimePickerSegment::Hour, text.into(), window, cx);
163    }
164
165    /// Replaces the text in the minute input field and updates the draft if valid.
166    pub fn set_minute_input(
167        &mut self,
168        text: impl Into<SharedString>,
169        window: &mut Window,
170        cx: &mut Context<Self>,
171    ) {
172        self.set_input(TimePickerSegment::Minute, text.into(), window, cx);
173    }
174
175    /// Returns whether the hour input field currently parses as a valid hour for the active format.
176    pub fn is_hour_input_valid(&self, cx: &App) -> bool {
177        self.valid_input(TimePickerSegment::Hour, cx)
178    }
179
180    /// Returns whether the minute input field currently parses as a valid minute (`0`–`59`).
181    pub fn is_minute_input_valid(&self, cx: &App) -> bool {
182        self.valid_input(TimePickerSegment::Minute, cx)
183    }
184
185    /// Returns whether both hour and minute input fields currently contain valid values.
186    pub fn is_input_valid(&self, cx: &App) -> bool {
187        self.invalid_segment(cx).is_none()
188    }
189
190    /// Returns whether the picker's modal dialog is currently open.
191    pub fn is_open(&self, cx: &App) -> bool {
192        self.dialog.read(cx).is_open()
193    }
194
195    /// Returns the retained focus handle of the built-in picker trigger.
196    pub fn trigger_focus(&self) -> &FocusHandle {
197        &self.trigger
198    }
199
200    /// Replaces the editable draft and cancels outstanding wheel motion.
201    pub fn set_value(&mut self, value: Time, window: &mut Window, cx: &mut Context<Self>) {
202        let next = Time::new(value.hour(), value.minute(), 0, 0)
203            .expect("hour and minute from valid Time are always valid");
204        let draft_changed = self.draft != next;
205        let attempted_changed = self.input_attempted;
206        self.draft = next;
207        self.input_attempted = false;
208        let fields_changed = self.sync_fields(window, cx);
209        let wheels_changed = self.sync_wheels(cx);
210        if draft_changed || attempted_changed || fields_changed || wheels_changed {
211            cx.notify();
212        }
213    }
214
215    /// Sets a 24-hour value. Returns whether the supplied hour was valid.
216    pub fn set_hour(&mut self, hour: u32, window: &mut Window, cx: &mut Context<Self>) -> bool {
217        self.set_part(TimePickerSegment::Hour, hour, window, cx)
218    }
219
220    /// Sets a minute in 0–59. Returns whether the supplied minute was valid.
221    pub fn set_minute(&mut self, minute: u32, window: &mut Window, cx: &mut Context<Self>) -> bool {
222        self.set_part(TimePickerSegment::Minute, minute, window, cx)
223    }
224
225    /// Switches the active editing segment between hours and minutes.
226    pub fn set_segment(&mut self, segment: TimePickerSegment, cx: &mut Context<Self>) {
227        self.dial_dragging = false;
228        if segment != self.segment {
229            self.segment = segment;
230            cx.notify();
231        }
232    }
233
234    /// Retains the active segment across mode changes. Reconciles editors with the last valid time.
235    pub fn set_mode(
236        &mut self,
237        mode: TimePickerVariant,
238        window: &mut Window,
239        cx: &mut Context<Self>,
240    ) {
241        if mode != self.mode && !self.has_composition(window, cx) {
242            if self.mode == TimePickerVariant::Input {
243                self.reconcile_input(cx);
244            }
245            let restore_focus = self.panel.contains_focused(window, cx);
246            self.mode = mode;
247            self.dial_dragging = false;
248            self.mode_revision = self.mode_revision.wrapping_add(1);
249            if mode != TimePickerVariant::Input {
250                self.picker_mode = mode;
251            }
252            self.input_attempted = false;
253            self.sync_fields(window, cx);
254            self.sync_wheels(cx);
255            if restore_focus {
256                self.focus_editor(window, cx);
257            }
258            cx.notify();
259        }
260    }
261
262    /// Switches between 12-hour and 24-hour display formats and resynchronizes editors and wheels.
263    pub fn set_format(&mut self, format: TimeFormat, window: &mut Window, cx: &mut Context<Self>) {
264        if format != self.format {
265            self.format = format;
266            self.input_attempted = false;
267            self.sync_fields(window, cx);
268            self.sync_wheels(cx);
269            cx.notify();
270        }
271    }
272
273    /// Replaces the locale hooks and reformats the hour and minute input fields.
274    pub fn set_locale(
275        &mut self,
276        locale: TimePickerLocale,
277        window: &mut Window,
278        cx: &mut Context<Self>,
279    ) {
280        self.locale = locale;
281        self.input_attempted = false;
282        self.sync_fields(window, cx);
283        cx.notify();
284    }
285
286    /// Opens a new editing session initialized from the application's value.
287    pub fn open(&mut self, value: Time, window: &mut Window, cx: &mut Context<Self>) {
288        if !self.is_open(cx) {
289            self.dial_dragging = false;
290            self.mode_revision = self.mode_revision.wrapping_add(1);
291            self.set_value(value, window, cx);
292            self.dialog.update(cx, |dialog, cx| dialog.open(window, cx));
293            self.focus_editor(window, cx);
294        }
295    }
296
297    /// Closes this session programmatically through the Material dialog.
298    pub fn close(&mut self, window: &mut Window, cx: &mut Context<Self>) {
299        if self.is_open(cx) {
300            self.dial_dragging = false;
301            self.discard_composition(window, cx);
302            self.sync_wheels(cx);
303            self.dialog
304                .update(cx, |dialog, cx| dialog.close(window, cx));
305        }
306    }
307
308    pub(super) fn has_composition(&self, window: &mut Window, cx: &mut App) -> bool {
309        [&self.hour, &self.minute]
310            .into_iter()
311            .any(|field| has_marked_text(field, window, cx))
312    }
313
314    pub(super) fn finish_composition(
315        &mut self,
316        window: &mut Window,
317        cx: &mut Context<Self>,
318    ) -> bool {
319        let mut composed = false;
320        for editor in [&self.hour, &self.minute] {
321            composed |= editor.update(cx, |editor, cx| {
322                if editor.marked_text_range(window, cx).is_none() {
323                    return false;
324                }
325                editor.unmark_text(window, cx);
326                true
327            });
328        }
329        if composed {
330            self.reconcile_input(cx);
331        }
332        composed
333    }
334
335    pub(super) fn discard_composition(&self, window: &mut Window, cx: &mut Context<Self>) {
336        if self.has_composition(window, cx) {
337            self.sync_fields(window, cx);
338        }
339    }
340
341    pub(super) fn reconcile_input(&mut self, cx: &mut Context<Self>) {
342        for segment in [TimePickerSegment::Hour, TimePickerSegment::Minute] {
343            let text = self.field(segment).read(cx).value();
344            if let Some(value) = self.parse_segment(segment, &text) {
345                self.update_draft(segment, value);
346                self.sync_wheel(segment, cx);
347            }
348        }
349        cx.notify();
350    }
351
352    pub(super) fn parse_segment(&self, segment: TimePickerSegment, text: &str) -> Option<u32> {
353        parse_segment(&self.locale, segment, text, self.format, self.is_pm())
354    }
355
356    fn valid_input(&self, segment: TimePickerSegment, cx: &App) -> bool {
357        self.parse_segment(segment, &self.field(segment).read(cx).value())
358            .is_some()
359    }
360
361    pub(super) fn invalid_segment(&self, cx: &App) -> Option<TimePickerSegment> {
362        [TimePickerSegment::Hour, TimePickerSegment::Minute]
363            .into_iter()
364            .find(|&segment| !self.valid_input(segment, cx))
365    }
366
367    fn apply_input(&mut self, segment: TimePickerSegment, text: &str, cx: &mut Context<Self>) {
368        if let Some(value) = self.parse_segment(segment, text) {
369            self.update_draft(segment, value);
370            self.sync_wheel(segment, cx);
371        }
372        cx.notify();
373    }
374
375    fn set_input(
376        &mut self,
377        segment: TimePickerSegment,
378        text: SharedString,
379        window: &mut Window,
380        cx: &mut Context<Self>,
381    ) {
382        self.field(segment)
383            .update(cx, |field, cx| field.set_value(text.clone(), window, cx));
384        self.apply_input(segment, &text, cx);
385    }
386
387    pub(super) fn field(&self, segment: TimePickerSegment) -> &Entity<TextFieldState> {
388        match segment {
389            TimePickerSegment::Hour => &self.hour,
390            TimePickerSegment::Minute => &self.minute,
391        }
392    }
393
394    pub(super) fn wheel(&self, segment: TimePickerSegment) -> &Entity<ScrollFieldState> {
395        match segment {
396            TimePickerSegment::Hour => &self.hours,
397            TimePickerSegment::Minute => &self.minutes,
398        }
399    }
400
401    fn wheel_index(&self, segment: TimePickerSegment) -> usize {
402        match segment {
403            TimePickerSegment::Hour => {
404                let hour = display_hour(self.draft, self.format) as usize;
405                hour - usize::from(self.format == TimeFormat::TwelveHour)
406            }
407            TimePickerSegment::Minute => self.minute() as usize,
408        }
409    }
410
411    fn sync_field(
412        &self,
413        segment: TimePickerSegment,
414        window: &mut Window,
415        cx: &mut Context<Self>,
416    ) -> bool {
417        let number = match segment {
418            TimePickerSegment::Hour => display_hour(self.draft, self.format),
419            TimePickerSegment::Minute => self.minute(),
420        };
421        let text = (self.locale.number)(number);
422        let field = self.field(segment);
423        if field.read(cx).value() != text || has_marked_text(field, window, cx) {
424            field.update(cx, |field, cx| field.set_value(text, window, cx));
425            true
426        } else {
427            false
428        }
429    }
430
431    fn sync_fields(&self, window: &mut Window, cx: &mut Context<Self>) -> bool {
432        self.sync_field(TimePickerSegment::Hour, window, cx)
433            | self.sync_field(TimePickerSegment::Minute, window, cx)
434    }
435
436    fn sync_wheel(&self, segment: TimePickerSegment, cx: &mut Context<Self>) -> bool {
437        let count = match (segment, self.format) {
438            (TimePickerSegment::Hour, TimeFormat::TwelveHour) => 12,
439            (TimePickerSegment::Hour, TimeFormat::TwentyFourHour) => 24,
440            (TimePickerSegment::Minute, _) => 60,
441        };
442        let index = self.wheel_index(segment);
443        self.wheel(segment).update(cx, |wheel, cx| {
444            wheel.set_item_count(count, cx);
445            if wheel.target_option() != Some(index) || wheel.is_scroll_in_progress() {
446                wheel.scroll_to_option(index, cx);
447                true
448            } else {
449                false
450            }
451        })
452    }
453
454    pub(super) fn sync_wheels(&self, cx: &mut Context<Self>) -> bool {
455        self.sync_wheel(TimePickerSegment::Hour, cx)
456            | self.sync_wheel(TimePickerSegment::Minute, cx)
457    }
458
459    fn update_draft(&mut self, segment: TimePickerSegment, value: u32) -> bool {
460        let Ok(value) = i8::try_from(value) else {
461            return false;
462        };
463        let time = match segment {
464            TimePickerSegment::Hour => self.draft.with().hour(value).build().ok(),
465            TimePickerSegment::Minute => self.draft.with().minute(value).build().ok(),
466        };
467        if let Some(time) = time {
468            self.draft = time;
469        }
470        time.is_some()
471    }
472
473    fn set_part(
474        &mut self,
475        segment: TimePickerSegment,
476        value: u32,
477        window: &mut Window,
478        cx: &mut Context<Self>,
479    ) -> bool {
480        let previous = self.draft;
481        if !self.update_draft(segment, value) {
482            return false;
483        }
484        let field_changed = self.sync_field(segment, window, cx);
485        let wheel_changed = self.sync_wheel(segment, cx);
486        if self.draft != previous || field_changed || wheel_changed {
487            cx.notify();
488        }
489        true
490    }
491
492    fn segment_focus(
493        &self,
494        segment: TimePickerSegment,
495        mode: TimePickerVariant,
496        cx: &App,
497    ) -> FocusHandle {
498        match mode {
499            TimePickerVariant::Dial => self.dial.clone(),
500            TimePickerVariant::Input => self.field(segment).read(cx).focus_handle(cx),
501            TimePickerVariant::Scroll => self.wheel(segment).read(cx).focus_handle().clone(),
502        }
503    }
504
505    fn editor_focus(&self, mode: TimePickerVariant, cx: &App) -> FocusHandle {
506        self.segment_focus(self.segment, mode, cx)
507    }
508
509    pub(super) fn focus_editor(&self, window: &mut Window, cx: &mut App) {
510        self.editor_focus(self.mode, cx).focus(window, cx);
511    }
512
513    pub(super) fn select(
514        &mut self,
515        value: u32,
516        advance: bool,
517        window: &mut Window,
518        cx: &mut Context<Self>,
519    ) {
520        let segment = self.segment;
521        let value = if segment == TimePickerSegment::Hour && self.format == TimeFormat::TwelveHour {
522            value % 12 + self.hour() / 12 * 12
523        } else {
524            value
525        };
526        if self.set_part(segment, value, window, cx)
527            && advance
528            && segment == TimePickerSegment::Hour
529        {
530            self.set_segment(TimePickerSegment::Minute, cx);
531        }
532    }
533}
534
535fn has_marked_text(field: &Entity<TextFieldState>, window: &mut Window, cx: &mut App) -> bool {
536    field.update(cx, |editor, cx| {
537        editor.marked_text_range(window, cx).is_some()
538    })
539}
540
541#[cfg(test)]
542mod tests {
543    use super::*;
544
545    #[test]
546    fn local_time_parsing_handles_midnight_noon_and_boundaries() {
547        let locale = TimePickerLocale::default();
548        let parse_hour = |text, pm| {
549            parse_segment(
550                &locale,
551                TimePickerSegment::Hour,
552                text,
553                TimeFormat::TwelveHour,
554                pm,
555            )
556        };
557        assert_eq!(parse_hour("12", false), Some(0));
558        assert_eq!(parse_hour("12", true), Some(12));
559        assert_eq!(parse_hour("11", true), Some(23));
560        assert_eq!(parse_hour("00", false), None);
561        assert_eq!(
562            parse_segment(
563                &locale,
564                TimePickerSegment::Minute,
565                "60",
566                TimeFormat::TwelveHour,
567                false
568            ),
569            None
570        );
571        assert_eq!(
572            parse_segment(
573                &locale,
574                TimePickerSegment::Hour,
575                "24",
576                TimeFormat::TwentyFourHour,
577                false
578            ),
579            None
580        );
581    }
582
583    #[test]
584    fn jiff_time_conversion_and_bounds() {
585        let locale = TimePickerLocale::default();
586        assert_eq!(
587            display_hour(Time::constant(0, 5, 0, 0), TimeFormat::TwelveHour),
588            12
589        );
590        assert_eq!(
591            display_hour(Time::constant(12, 30, 0, 0), TimeFormat::TwelveHour),
592            12
593        );
594        assert_eq!(
595            display_hour(Time::constant(13, 30, 0, 0), TimeFormat::TwelveHour),
596            1
597        );
598        assert_eq!(
599            display_hour(Time::constant(23, 59, 0, 0), TimeFormat::TwentyFourHour),
600            23
601        );
602        assert_eq!(
603            super::super::types::format_time(
604                Time::constant(0, 5, 0, 0),
605                TimeFormat::TwelveHour,
606                &locale
607            )
608            .as_ref(),
609            "12:05 AM"
610        );
611        assert_eq!(
612            super::super::types::format_time(
613                Time::constant(15, 45, 0, 0),
614                TimeFormat::TwentyFourHour,
615                &locale
616            )
617            .as_ref(),
618            "15:45"
619        );
620    }
621
622    #[cfg(feature = "test-support")]
623    #[gpui::test]
624    fn time_dialog_waits_for_ime_then_confirms_the_unmarked_value(cx: &mut gpui::TestAppContext) {
625        use std::{
626            cell::{Cell, RefCell},
627            rc::Rc,
628        };
629
630        use gpui::{IntoElement, Render, WindowOptions, div};
631        use gpui_material_components::DialogAction;
632        use gpui_material_foundation::MaterialRoot;
633        use gpui_material_theme::MaterialTheme;
634
635        use crate::date_time::time::dialog::{TimePickerDialog, TimePickerModeAction};
636
637        struct Fixture {
638            state: Entity<TimePickerState>,
639            change_mode: Rc<RefCell<Option<TimePickerModeAction>>>,
640            confirm: Rc<RefCell<Option<DialogAction>>>,
641            accepted: Rc<Cell<Option<Time>>>,
642        }
643
644        impl Render for Fixture {
645            fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
646                let change_mode = self.change_mode.clone();
647                let confirm = self.confirm.clone();
648                let accepted = self.accepted.clone();
649                TimePickerDialog::new("ime-clock", "Input composition", &self.state)
650                    .mode_toggle_content(move |action| {
651                        change_mode.replace(Some(action));
652                        div()
653                    })
654                    .confirm_content(move |action| {
655                        confirm.replace(Some(action));
656                        div()
657                    })
658                    .on_confirm(move |time, _, _| accepted.set(Some(time)))
659            }
660        }
661
662        let mode = Rc::new(RefCell::new(None));
663        let confirm = Rc::new(RefCell::new(None));
664        let accepted = Rc::new(Cell::new(None));
665        let mut state = None;
666        let window = cx
667            .update(|cx| {
668                MaterialRoot::init(MaterialTheme::default(), cx);
669                gpui_material_components::init_components(cx);
670                cx.set_reduce_motion(true);
671                MaterialRoot::open_window(WindowOptions::default(), cx, |window, cx| {
672                    let picker = cx.new(|cx| {
673                        let mut picker = TimePickerState::new(window, cx);
674                        picker.set_mode(TimePickerVariant::Input, window, cx);
675                        picker
676                    });
677                    state = Some(picker.clone());
678                    cx.new(|cx| {
679                        cx.observe(&picker, |_, _, cx| cx.notify()).detach();
680                        Fixture {
681                            state: picker,
682                            change_mode: mode.clone(),
683                            confirm: confirm.clone(),
684                            accepted: accepted.clone(),
685                        }
686                    })
687                })
688                .unwrap()
689            })
690            .into();
691        let state = state.unwrap();
692        let initial = Time::constant(15, 25, 0, 0);
693        cx.update_window(window, |_, window, cx| {
694            state.update(cx, |state, cx| state.open(initial, window, cx));
695            window.draw(cx).clear(cx);
696        })
697        .unwrap();
698        cx.run_until_parked();
699        let change_mode = mode.borrow().clone().unwrap();
700        let apply = confirm.borrow().clone().unwrap();
701
702        cx.update_window(window, |_, window, cx| {
703            let editor = state.read(cx).hour.clone();
704            editor.update(cx, |editor, cx| {
705                editor.replace_and_mark_text_in_range(Some(0..2), "19", Some(1..1), window, cx);
706            });
707            change_mode.activate(window, cx);
708            apply.activate(window, cx);
709            let model = state.read(cx);
710            assert_eq!(model.mode(), TimePickerVariant::Input);
711            assert_eq!(model.value(), initial);
712            assert_eq!(model.hour_input(cx).as_ref(), "19");
713            assert!(model.is_open(cx));
714            assert!(accepted.get().is_none());
715        })
716        .unwrap();
717
718        cx.simulate_keystrokes(window, "escape");
719        cx.update_window(window, |_, window, cx| {
720            let model = state.read(cx);
721            let editor = model.hour.clone();
722            assert!(model.is_open(cx));
723            assert!(accepted.get().is_none());
724            editor.update(cx, |editor, cx| {
725                assert!(editor.marked_text_range(window, cx).is_none());
726                editor.replace_and_mark_text_in_range(Some(0..2), "21", Some(1..1), window, cx);
727                editor.unmark_text(window, cx);
728            });
729            apply.activate(window, cx);
730            assert_eq!(accepted.get(), Some(Time::constant(21, 25, 0, 0)));
731            assert!(!state.read(cx).is_open(cx));
732
733            state.update(cx, |state, cx| state.open(initial, window, cx));
734            let editor = state.read(cx).hour.clone();
735            editor.update(cx, |editor, cx| {
736                editor.replace_and_mark_text_in_range(Some(0..2), "22", Some(1..1), window, cx);
737            });
738            state.update(cx, |state, cx| state.close(window, cx));
739            assert!(!state.read(cx).is_open(cx));
740            assert_eq!(state.read(cx).value(), initial);
741            assert_eq!(state.read(cx).hour_input(cx).as_ref(), "15");
742        })
743        .unwrap();
744    }
745}