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