1use std::rc::Rc;
2
3use gpui::{
4 AnyElement, App, ElementId, Entity, InteractiveElement as _, IntoElement, ParentElement as _,
5 RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, WeakEntity,
6 Window, div, px,
7};
8use gpui_material_components::{
9 Button, ButtonVariant, Dialog, DialogAction, DialogCloseReason, DialogColors, DialogProperties,
10};
11use gpui_material_foundation::{color, current_material_theme};
12use gpui_material_theme::{ColorRole, CornerShape};
13
14use super::{
15 NaiveTime, TimeFormat, TimePickerLayout, TimePickerLocale, TimePickerPanel, TimePickerSegment,
16 TimePickerState, TimePickerStyle, TimePickerVariant,
17};
18use crate::date_time::Confirm;
19
20type ActionSlot = Box<dyn FnOnce(DialogAction) -> AnyElement>;
21pub(super) type ModeSlot = Box<dyn FnOnce(TimePickerModeAction) -> AnyElement>;
22type DismissRequest = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App) -> bool>;
23type OnClose = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App)>;
24
25#[derive(Clone)]
27pub struct TimePickerModeAction {
28 state: WeakEntity<TimePickerState>,
29 revision: u64,
30 target: TimePickerVariant,
31 displayed: TimePickerVariant,
32 dialog: bool,
33}
34
35impl TimePickerModeAction {
36 pub fn target(&self) -> TimePickerVariant {
38 self.target
39 }
40
41 pub fn activate(&self, window: &mut Window, cx: &mut App) {
43 let _ = self.state.update(cx, |state, cx| {
44 if state.mode_revision == self.revision && (!self.dialog || state.is_open(cx)) {
45 state.set_mode(self.target, window, cx);
46 if state.mode == self.target && self.target == TimePickerVariant::Input {
47 state.picker_mode = self.displayed;
48 }
49 state.focus_editor(window, cx);
50 }
51 });
52 }
53}
54
55pub(super) fn mode_action(
56 state: &Entity<TimePickerState>,
57 model: &TimePickerState,
58 displayed: TimePickerVariant,
59 dialog: bool,
60) -> TimePickerModeAction {
61 TimePickerModeAction {
62 state: state.downgrade(),
63 revision: model.mode_revision,
64 target: if displayed == TimePickerVariant::Input {
65 model.picker_mode
66 } else {
67 TimePickerVariant::Input
68 },
69 displayed,
70 dialog,
71 }
72}
73
74pub type TimePickerDialog = TimeDialog<false>;
76pub type VibrantTimePickerDialog = TimeDialog<true>;
78
79#[derive(IntoElement)]
81pub struct TimeDialog<const VIBRANT: bool> {
82 id: ElementId,
83 name: SharedString,
84 state: Entity<TimePickerState>,
85 layout: TimePickerLayout,
86 style: TimePickerStyle,
87 shape: Option<CornerShape>,
88 colors: DialogColors,
89 body: Option<AnyElement>,
90 title: Option<AnyElement>,
91 toggle: Option<ModeSlot>,
92 confirm: Option<ActionSlot>,
93 dismiss: Option<ActionSlot>,
94 show_toggle: bool,
95 show_dismiss: bool,
96 properties: DialogProperties,
97 on_confirm: Option<Confirm<NaiveTime>>,
98 on_dismiss_request: Option<DismissRequest>,
99 on_close: Option<OnClose>,
100}
101
102impl<const VIBRANT: bool> TimeDialog<VIBRANT> {
103 pub fn new(
105 id: impl Into<ElementId>,
106 name: impl Into<SharedString>,
107 state: &Entity<TimePickerState>,
108 ) -> Self {
109 Self {
110 id: id.into(),
111 name: name.into(),
112 state: state.clone(),
113 layout: TimePickerLayout::Auto,
114 style: TimePickerStyle::default(),
115 shape: None,
116 colors: DialogColors::default(),
117 body: None,
118 title: None,
119 toggle: None,
120 confirm: None,
121 dismiss: None,
122 show_toggle: true,
123 show_dismiss: true,
124 properties: DialogProperties::default(),
125 on_confirm: None,
126 on_dismiss_request: None,
127 on_close: None,
128 }
129 }
130
131 pub fn layout(mut self, layout: TimePickerLayout) -> Self {
133 self.layout = layout;
134 self
135 }
136
137 pub fn style(mut self, style: TimePickerStyle) -> Self {
139 self.style = style;
140 self
141 }
142
143 pub fn colors(mut self, colors: DialogColors) -> Self {
145 self.colors = colors;
146 self
147 }
148
149 pub fn shape(mut self, shape: CornerShape) -> Self {
151 self.shape = Some(shape);
152 self
153 }
154
155 pub fn body(mut self, body: impl IntoElement) -> Self {
157 self.body = Some(body.into_any_element());
158 self
159 }
160
161 pub fn title_content(mut self, content: impl IntoElement) -> Self {
163 self.title = Some(content.into_any_element());
164 self
165 }
166
167 pub fn mode_toggle_content<E: IntoElement>(
169 mut self,
170 content: impl FnOnce(TimePickerModeAction) -> E + 'static,
171 ) -> Self {
172 self.toggle = Some(Box::new(move |action| content(action).into_any_element()));
173 self
174 }
175
176 pub fn confirm_content<E: IntoElement>(
178 mut self,
179 content: impl FnOnce(DialogAction) -> E + 'static,
180 ) -> Self {
181 self.confirm = Some(Box::new(move |action| content(action).into_any_element()));
182 self
183 }
184
185 pub fn dismiss_content<E: IntoElement>(
187 mut self,
188 content: impl FnOnce(DialogAction) -> E + 'static,
189 ) -> Self {
190 self.dismiss = Some(Box::new(move |action| content(action).into_any_element()));
191 self
192 }
193
194 pub fn show_mode_toggle(mut self, show: bool) -> Self {
196 self.show_toggle = show;
197 self
198 }
199
200 pub fn show_dismiss(mut self, show: bool) -> Self {
202 self.show_dismiss = show;
203 self
204 }
205
206 pub fn properties(mut self, properties: DialogProperties) -> Self {
208 self.properties = properties;
209 self
210 }
211
212 pub fn on_confirm(
214 mut self,
215 confirm: impl Fn(NaiveTime, &mut Window, &mut App) + 'static,
216 ) -> Self {
217 self.on_confirm = Some(Rc::new(confirm));
218 self
219 }
220
221 pub fn on_dismiss_request(
223 mut self,
224 request: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
225 ) -> Self {
226 self.on_dismiss_request = Some(Rc::new(request));
227 self
228 }
229
230 pub fn on_close(
232 mut self,
233 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
234 ) -> Self {
235 self.on_close = Some(Rc::new(callback));
236 self
237 }
238}
239
240impl<const VIBRANT: bool> RenderOnce for TimeDialog<VIBRANT> {
241 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
242 let model = self.state.read(cx);
243 let toggle = self.show_toggle.then(|| {
244 let action = mode_action(&self.state, model, model.mode, true);
245 if let Some(custom) = self.toggle {
246 custom(action)
247 } else {
248 Button::new(
249 (self.id.clone(), "display-mode"),
250 match action.target() {
251 TimePickerVariant::Dial => "Show clock",
252 TimePickerVariant::Input => "Enter time",
253 TimePickerVariant::Scroll => "Show scroll wheels",
254 },
255 )
256 .variant(ButtonVariant::Text)
257 .on_activate(move |window, cx| action.activate(window, cx))
258 .into_any_element()
259 }
260 });
261 let body = self.body.unwrap_or_else(|| {
262 TimePickerPanel::new(self.id.clone(), &self.state)
263 .layout(self.layout)
264 .style(self.style)
265 .vibrant(VIBRANT)
266 .into_any_element()
267 });
268 if VIBRANT {
269 let theme = current_material_theme(window, cx);
270 self.colors
271 .container
272 .get_or_insert_with(|| color(theme.colors.color(ColorRole::SurfaceContainer)));
273 }
274 let state = self.state.clone();
275 let dismiss_state = self.state.clone();
276 let close_state = self.state;
277 let confirm = self.on_confirm;
278 let close = self.on_close;
279 let dismiss_request = self.on_dismiss_request;
280 let mut dialog = Dialog::new(self.id, &model.dialog, self.name)
281 .show_title(!VIBRANT || self.title.is_some())
282 .colors(self.colors)
283 .properties(self.properties)
284 .show_cancel(self.show_dismiss)
285 .body(
286 div()
287 .flex()
288 .flex_col()
289 .items_center()
290 .gap(px(8.))
291 .child(body)
292 .children(toggle),
293 )
294 .confirm("OK", move |window, cx| {
295 state.update(cx, |state, cx| {
296 if state.has_composition(window, cx) {
297 return false;
298 }
299 if state.mode != TimePickerVariant::Input {
300 return true;
301 }
302 if let Some(segment) = state.invalid_segment(cx) {
303 state.input_attempted = true;
304 state.set_segment(segment, cx);
305 state.focus_editor(window, cx);
306 cx.notify();
307 return false;
308 }
309 state.reconcile_input(cx);
310 true
311 })
312 })
313 .on_dismiss_request(move |reason, window, cx| {
314 if reason == DialogCloseReason::Escape
315 && dismiss_state.update(cx, |state, cx| state.finish_composition(window, cx))
316 {
317 return false;
318 }
319 dismiss_request
320 .as_ref()
321 .is_none_or(|request| request(reason, window, cx))
322 })
323 .on_close(move |reason, window, cx| {
324 let value = close_state.update(cx, |state, cx| {
325 state.discard_composition(window, cx);
326 state.sync_wheels(cx);
327 state.value()
328 });
329 if reason == DialogCloseReason::Confirm
330 && let Some(confirm) = &confirm
331 {
332 confirm(value, window, cx);
333 }
334 if let Some(close) = &close {
335 close(reason, window, cx);
336 }
337 });
338 if let Some(shape) = self.shape {
339 dialog = dialog.shape(shape);
340 }
341 if let Some(title) = self.title {
342 dialog = dialog.title_content(title);
343 }
344 if let Some(content) = self.confirm {
345 dialog = dialog.confirm_content(content);
346 }
347 if self.show_dismiss
348 && let Some(content) = self.dismiss
349 {
350 dialog = dialog.dismiss_content(content);
351 }
352 dialog
353 }
354}
355
356struct TimeOptions {
357 value: NaiveTime,
358 format: TimeFormat,
359 variant: TimePickerVariant,
360 layout: TimePickerLayout,
361 locale: TimePickerLocale,
362}
363
364#[derive(IntoElement)]
366pub struct TimePicker {
367 id: ElementId,
368 label: SharedString,
369 state: Entity<TimePickerState>,
370 options: TimeOptions,
371 confirm: Option<Confirm<NaiveTime>>,
372 style: TimePickerStyle,
373 colors: DialogColors,
374 show_mode_toggle: bool,
375 properties: DialogProperties,
376 on_dismiss_request: Option<DismissRequest>,
377 on_close: Option<OnClose>,
378 disabled: bool,
379}
380
381impl TimePicker {
382 pub fn new(
384 id: impl Into<ElementId>,
385 label: impl Into<SharedString>,
386 state: &Entity<TimePickerState>,
387 value: NaiveTime,
388 ) -> Self {
389 Self {
390 id: id.into(),
391 label: label.into(),
392 state: state.clone(),
393 options: TimeOptions {
394 value,
395 format: TimeFormat::default(),
396 variant: TimePickerVariant::default(),
397 layout: TimePickerLayout::default(),
398 locale: TimePickerLocale::default(),
399 },
400 confirm: None,
401 style: TimePickerStyle::default(),
402 colors: DialogColors::default(),
403 show_mode_toggle: true,
404 properties: DialogProperties::default(),
405 on_dismiss_request: None,
406 on_close: None,
407 disabled: false,
408 }
409 }
410
411 pub fn format(mut self, format: TimeFormat) -> Self {
413 self.options.format = format;
414 self
415 }
416
417 pub fn variant(mut self, variant: TimePickerVariant) -> Self {
419 self.options.variant = variant;
420 self
421 }
422
423 pub fn layout(mut self, layout: TimePickerLayout) -> Self {
425 self.options.layout = layout;
426 self
427 }
428
429 pub fn locale(mut self, locale: TimePickerLocale) -> Self {
431 self.options.locale = locale;
432 self
433 }
434
435 pub fn style(mut self, style: TimePickerStyle) -> Self {
437 self.style = style;
438 self
439 }
440
441 pub fn colors(mut self, colors: DialogColors) -> Self {
443 self.colors = colors;
444 self
445 }
446
447 pub fn show_mode_toggle(mut self, show: bool) -> Self {
449 self.show_mode_toggle = show;
450 self
451 }
452
453 pub fn properties(mut self, properties: DialogProperties) -> Self {
455 self.properties = properties;
456 self
457 }
458
459 pub fn on_dismiss_request(
461 mut self,
462 request: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
463 ) -> Self {
464 self.on_dismiss_request = Some(Rc::new(request));
465 self
466 }
467
468 pub fn on_close(
470 mut self,
471 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
472 ) -> Self {
473 self.on_close = Some(Rc::new(callback));
474 self
475 }
476
477 pub fn disabled(mut self, disabled: bool) -> Self {
479 self.disabled = disabled;
480 self
481 }
482
483 pub fn on_confirm(
485 mut self,
486 callback: impl Fn(NaiveTime, &mut Window, &mut App) + 'static,
487 ) -> Self {
488 self.confirm = Some(Rc::new(callback));
489 self
490 }
491}
492
493impl RenderOnce for TimePicker {
494 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
495 let model = self.state.read(cx);
496 let state = self.state.clone();
497 let options = self.options;
498 let layout = options.layout;
499 let trigger = div()
500 .id((self.id.clone(), "trigger-group"))
501 .role(Role::Group)
502 .aria_expanded(model.is_open(cx))
503 .child(
504 Button::new((self.id.clone(), "open"), self.label.clone())
505 .variant(ButtonVariant::Outlined)
506 .disabled(self.disabled)
507 .track_focus(&model.trigger)
508 .on_activate(move |window, cx| {
509 state.update(cx, |state, cx| {
510 if !state.is_open(cx) {
511 state.trigger.focus(window, cx);
512 state.set_segment(TimePickerSegment::Hour, cx);
513 state.picker_mode = TimePickerVariant::Dial;
514 state.set_mode(options.variant, window, cx);
515 state.set_format(options.format, window, cx);
516 state.set_locale(options.locale.clone(), window, cx);
517 state.open(options.value, window, cx);
518 }
519 });
520 }),
521 );
522 let mut dialog = TimePickerDialog::new(self.id, self.label, &self.state)
523 .layout(layout)
524 .style(self.style)
525 .colors(self.colors)
526 .show_mode_toggle(self.show_mode_toggle)
527 .properties(self.properties);
528 dialog.on_confirm = self.confirm;
529 dialog.on_dismiss_request = self.on_dismiss_request;
530 dialog.on_close = self.on_close;
531 div().child(trigger).child(dialog)
532 }
533}