gpui_form_component/date_picker/
state.rs1use super::*;
2
3impl DatePickerState {
4 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.and_then(jiff_date_from_chrono), 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 pub fn date(&self) -> Option<JiffDate> {
37 self.date
38 }
39
40 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 pub fn display_locale(mut self, locale: impl Into<Locale>) -> Self {
52 self.display_locale = Some(locale.into());
53 self
54 }
55
56 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(
72 CalendarDate::Single(date.and_then(chrono_date_from_jiff)),
73 window,
74 cx,
75 );
76 });
77 self.open = false;
78 if emit {
79 cx.emit(DatePickerEvent::Change(date));
80 }
81 cx.notify();
82 }
83
84 pub(super) fn close_calendar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
85 self.focus_back_if_need(window, cx);
86 self.open = false;
87 cx.notify();
88 }
89
90 fn focus_back_if_need(&mut self, window: &mut Window, cx: &mut Context<Self>) {
91 if !self.open {
92 return;
93 }
94
95 if let Some(focused) = window.focused(cx)
96 && focused.contains(&self.focus_handle, window)
97 {
98 self.focus_handle.focus(window, cx);
99 }
100 }
101
102 pub(super) fn clean(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
103 cx.stop_propagation();
104 self.update_date(None, true, window, cx);
105 }
106
107 pub(super) fn toggle_calendar(
108 &mut self,
109 _: &ClickEvent,
110 _: &mut Window,
111 cx: &mut Context<Self>,
112 ) {
113 self.open = !self.open;
114 cx.notify();
115 }
116}
117
118impl DateRangePickerState {
119 pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
121 let calendar = cx.new(|cx| {
122 let mut this = CalendarState::new(window, cx);
123 this.set_date(CalendarDate::Range(None, None), window, cx);
124 this
125 });
126
127 let subscriptions = vec![cx.subscribe_in(
128 &calendar,
129 window,
130 |this, _, ev: &CalendarEvent, window, cx| match ev {
131 CalendarEvent::Selected(CalendarDate::Range(start_date, end_date)) => {
132 this.update_range(
133 start_date.and_then(jiff_date_from_chrono),
134 end_date.and_then(jiff_date_from_chrono),
135 true,
136 window,
137 cx,
138 );
139 this.focus_handle.focus(window, cx);
140 },
141 CalendarEvent::Selected(CalendarDate::Single(_)) => {},
142 },
143 )];
144
145 Self {
146 focus_handle: cx.focus_handle(),
147 start_date: None,
148 end_date: None,
149 open: false,
150 calendar,
151 display_locale: None,
152 display_style: DateDisplayStyle::default(),
153 _subscriptions: subscriptions,
154 }
155 }
156
157 pub fn range(&self) -> (Option<JiffDate>, Option<JiffDate>) {
159 (self.start_date, self.end_date)
160 }
161
162 pub fn set_range(
164 &mut self,
165 start_date: impl Into<Option<JiffDate>>,
166 end_date: impl Into<Option<JiffDate>>,
167 window: &mut Window,
168 cx: &mut Context<Self>,
169 ) {
170 self.update_range(start_date.into(), end_date.into(), false, window, cx);
171 }
172
173 pub fn display_locale(mut self, locale: impl Into<Locale>) -> Self {
175 self.display_locale = Some(locale.into());
176 self
177 }
178
179 pub fn display_style(mut self, display_style: DateDisplayStyle) -> Self {
181 self.display_style = display_style;
182 self
183 }
184
185 fn update_range(
186 &mut self,
187 start_date: Option<JiffDate>,
188 end_date: Option<JiffDate>,
189 emit: bool,
190 window: &mut Window,
191 cx: &mut Context<Self>,
192 ) {
193 self.start_date = start_date;
194 self.end_date = end_date;
195 self.calendar.update(cx, |state, cx| {
196 state.set_date(
197 CalendarDate::Range(
198 start_date.and_then(chrono_date_from_jiff),
199 end_date.and_then(chrono_date_from_jiff),
200 ),
201 window,
202 cx,
203 );
204 });
205 self.open = false;
206 if emit {
207 cx.emit(DateRangePickerEvent::Change(start_date, end_date));
208 }
209 cx.notify();
210 }
211
212 pub(super) fn close_calendar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
213 self.focus_back_if_need(window, cx);
214 self.open = false;
215 cx.notify();
216 }
217
218 fn focus_back_if_need(&mut self, window: &mut Window, cx: &mut Context<Self>) {
219 if !self.open {
220 return;
221 }
222
223 if let Some(focused) = window.focused(cx)
224 && focused.contains(&self.focus_handle, window)
225 {
226 self.focus_handle.focus(window, cx);
227 }
228 }
229
230 pub(super) fn clean(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
231 cx.stop_propagation();
232 self.update_range(None, None, true, window, cx);
233 }
234
235 pub(super) fn toggle_calendar(
236 &mut self,
237 _: &ClickEvent,
238 _: &mut Window,
239 cx: &mut Context<Self>,
240 ) {
241 self.open = !self.open;
242 cx.notify();
243 }
244}