Skip to main content

gpui_form_component/date_picker/
state.rs

1use super::*;
2
3impl DatePickerState {
4    /// Create a new date state.
5    pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
6        let calendar = cx.new(|cx| {
7            let mut this = CalendarState::new(window, cx);
8            this.set_date(CalendarDate::Single(None), window, cx);
9            this
10        });
11
12        let subscriptions = vec![cx.subscribe_in(
13            &calendar,
14            window,
15            |this, _, ev: &CalendarEvent, window, cx| match ev {
16                CalendarEvent::Selected(CalendarDate::Single(date)) => {
17                    this.update_date(*date, true, window, cx);
18                    this.focus_handle.focus(window, cx);
19                },
20                CalendarEvent::Selected(CalendarDate::Range(_, _)) => {},
21            },
22        )];
23
24        Self {
25            focus_handle: cx.focus_handle(),
26            date: None,
27            open: false,
28            calendar,
29            display_locale: None,
30            display_style: DateDisplayStyle::default(),
31            _subscriptions: subscriptions,
32        }
33    }
34
35    /// Get the selected date.
36    pub fn date(&self) -> Option<JiffDate> {
37        self.date
38    }
39
40    /// Set the selected date.
41    pub fn set_date(
42        &mut self,
43        date: impl Into<Option<JiffDate>>,
44        window: &mut Window,
45        cx: &mut Context<Self>,
46    ) {
47        self.update_date(date.into(), false, window, cx);
48    }
49
50    /// Override the locale used for display formatting.
51    pub fn display_locale(mut self, locale: impl Into<Locale>) -> Self {
52        self.display_locale = Some(locale.into());
53        self
54    }
55
56    /// Set the localized display width used for the input text.
57    pub fn display_style(mut self, display_style: DateDisplayStyle) -> Self {
58        self.display_style = display_style;
59        self
60    }
61
62    fn update_date(
63        &mut self,
64        date: Option<JiffDate>,
65        emit: bool,
66        window: &mut Window,
67        cx: &mut Context<Self>,
68    ) {
69        self.date = date;
70        self.calendar.update(cx, |state, cx| {
71            state.set_date(CalendarDate::Single(date), window, cx);
72        });
73        self.open = false;
74        if emit {
75            cx.emit(DatePickerEvent::Change(date));
76        }
77        cx.notify();
78    }
79
80    pub(super) fn close_calendar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
81        self.focus_back_if_need(window, cx);
82        self.open = false;
83        cx.notify();
84    }
85
86    fn focus_back_if_need(&mut self, window: &mut Window, cx: &mut Context<Self>) {
87        if !self.open {
88            return;
89        }
90
91        if let Some(focused) = window.focused(cx)
92            && focused.contains(&self.focus_handle, window)
93        {
94            self.focus_handle.focus(window, cx);
95        }
96    }
97
98    pub(super) fn clean(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
99        cx.stop_propagation();
100        self.update_date(None, true, window, cx);
101    }
102
103    pub(super) fn toggle_calendar(
104        &mut self,
105        _: &ClickEvent,
106        _: &mut Window,
107        cx: &mut Context<Self>,
108    ) {
109        self.open = !self.open;
110        cx.notify();
111    }
112}
113
114impl DateRangePickerState {
115    /// Create a new date range state.
116    pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
117        let calendar = cx.new(|cx| {
118            let mut this = CalendarState::new(window, cx);
119            this.set_date(CalendarDate::Range(None, None), window, cx);
120            this
121        });
122
123        let subscriptions = vec![cx.subscribe_in(
124            &calendar,
125            window,
126            |this, _, ev: &CalendarEvent, window, cx| match ev {
127                CalendarEvent::Selected(CalendarDate::Range(start_date, end_date)) => {
128                    this.update_range(*start_date, *end_date, true, window, cx);
129                    this.focus_handle.focus(window, cx);
130                },
131                CalendarEvent::Selected(CalendarDate::Single(_)) => {},
132            },
133        )];
134
135        Self {
136            focus_handle: cx.focus_handle(),
137            start_date: None,
138            end_date: None,
139            open: false,
140            calendar,
141            display_locale: None,
142            display_style: DateDisplayStyle::default(),
143            _subscriptions: subscriptions,
144        }
145    }
146
147    /// Get the selected date range.
148    pub fn range(&self) -> (Option<JiffDate>, Option<JiffDate>) {
149        (self.start_date, self.end_date)
150    }
151
152    /// Set the selected date range.
153    pub fn set_range(
154        &mut self,
155        start_date: impl Into<Option<JiffDate>>,
156        end_date: impl Into<Option<JiffDate>>,
157        window: &mut Window,
158        cx: &mut Context<Self>,
159    ) {
160        self.update_range(start_date.into(), end_date.into(), false, window, cx);
161    }
162
163    /// Override the locale used for display formatting.
164    pub fn display_locale(mut self, locale: impl Into<Locale>) -> Self {
165        self.display_locale = Some(locale.into());
166        self
167    }
168
169    /// Set the localized display width used for the input text.
170    pub fn display_style(mut self, display_style: DateDisplayStyle) -> Self {
171        self.display_style = display_style;
172        self
173    }
174
175    fn update_range(
176        &mut self,
177        start_date: Option<JiffDate>,
178        end_date: Option<JiffDate>,
179        emit: bool,
180        window: &mut Window,
181        cx: &mut Context<Self>,
182    ) {
183        self.start_date = start_date;
184        self.end_date = end_date;
185        self.calendar.update(cx, |state, cx| {
186            state.set_date(CalendarDate::Range(start_date, end_date), window, cx);
187        });
188        self.open = false;
189        if emit {
190            cx.emit(DateRangePickerEvent::Change(start_date, end_date));
191        }
192        cx.notify();
193    }
194
195    pub(super) fn close_calendar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
196        self.focus_back_if_need(window, cx);
197        self.open = false;
198        cx.notify();
199    }
200
201    fn focus_back_if_need(&mut self, window: &mut Window, cx: &mut Context<Self>) {
202        if !self.open {
203            return;
204        }
205
206        if let Some(focused) = window.focused(cx)
207            && focused.contains(&self.focus_handle, window)
208        {
209            self.focus_handle.focus(window, cx);
210        }
211    }
212
213    pub(super) fn clean(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
214        cx.stop_propagation();
215        self.update_range(None, None, true, window, cx);
216    }
217
218    pub(super) fn toggle_calendar(
219        &mut self,
220        _: &ClickEvent,
221        _: &mut Window,
222        cx: &mut Context<Self>,
223    ) {
224        self.open = !self.open;
225        cx.notify();
226    }
227}