Skip to main content

gpui_base/
calendar.rs

1use crate::TestSupportExt as _;
2use std::rc::Rc;
3
4use crate::{LogicalStyledExt as _, h_flex, styled::StyledExt as _, v_flex};
5use chrono::{Datelike, Local, NaiveDate, Weekday};
6use gpui::{
7    AnyElement, App, Context, ElementId, Empty, Entity, EventEmitter, FocusHandle,
8    InteractiveElement, IntoElement, ParentElement, Render, RenderOnce, SharedString,
9    StatefulInteractiveElement, StyleRefinement, Styled, Window, div, px,
10};
11
12/// A controlled calendar value.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Date {
15    Single(Option<NaiveDate>),
16    Range(Option<NaiveDate>, Option<NaiveDate>),
17}
18
19impl std::fmt::Display for Date {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Self::Single(Some(date)) => write!(f, "{date}"),
23            Self::Single(None) | Self::Range(None, None) => write!(f, "nil"),
24            Self::Range(Some(start), Some(end)) => write!(f, "{start} - {end}"),
25            Self::Range(Some(start), None) => write!(f, "{start} - nil"),
26            Self::Range(None, Some(end)) => write!(f, "nil - {end}"),
27        }
28    }
29}
30
31impl From<NaiveDate> for Date {
32    fn from(value: NaiveDate) -> Self {
33        Self::Single(Some(value))
34    }
35}
36impl From<(NaiveDate, NaiveDate)> for Date {
37    fn from((start, end): (NaiveDate, NaiveDate)) -> Self {
38        Self::Range(Some(start), Some(end))
39    }
40}
41
42impl Date {
43    pub fn is_some(&self) -> bool {
44        matches!(self, Self::Single(Some(_)) | Self::Range(Some(_), _))
45    }
46    pub fn is_complete(&self) -> bool {
47        matches!(self, Self::Single(Some(_)) | Self::Range(Some(_), Some(_)))
48    }
49    pub fn start(&self) -> Option<NaiveDate> {
50        match self {
51            Self::Single(Some(v)) | Self::Range(Some(v), _) => Some(*v),
52            _ => None,
53        }
54    }
55    pub fn end(&self) -> Option<NaiveDate> {
56        match self {
57            Self::Range(_, Some(v)) => Some(*v),
58            _ => None,
59        }
60    }
61    pub fn format(&self, format: &str) -> Option<SharedString> {
62        match self {
63            Self::Single(Some(v)) => Some(v.format(format).to_string().into()),
64            Self::Range(Some(a), Some(b)) => {
65                Some(format!("{} - {}", a.format(format), b.format(format)).into())
66            }
67            _ => None,
68        }
69    }
70    pub fn is_active(&self, value: &NaiveDate) -> bool {
71        match self {
72            Self::Single(v) => *v == Some(*value),
73            Self::Range(a, b) => *a == Some(*value) || *b == Some(*value),
74        }
75    }
76    pub fn is_single(&self) -> bool {
77        matches!(self, Self::Single(_))
78    }
79    pub fn is_in_range(&self, value: &NaiveDate) -> bool {
80        matches!(self, Self::Range(Some(a), Some(b)) if value >= a && value <= b)
81    }
82}
83
84pub struct IntervalMatcher {
85    before: Option<NaiveDate>,
86    after: Option<NaiveDate>,
87}
88pub struct RangeMatcher {
89    from: Option<NaiveDate>,
90    to: Option<NaiveDate>,
91}
92pub enum Matcher {
93    DayOfWeek(Vec<u32>),
94    Interval(IntervalMatcher),
95    Range(RangeMatcher),
96    Custom(Box<dyn Fn(&NaiveDate) -> bool + Send + Sync>),
97}
98impl From<Vec<u32>> for Matcher {
99    fn from(v: Vec<u32>) -> Self {
100        Self::DayOfWeek(v)
101    }
102}
103impl<F: Fn(&NaiveDate) -> bool + Send + Sync + 'static> From<F> for Matcher {
104    fn from(v: F) -> Self {
105        Self::Custom(Box::new(v))
106    }
107}
108impl Matcher {
109    pub fn interval(before: Option<NaiveDate>, after: Option<NaiveDate>) -> Self {
110        Self::Interval(IntervalMatcher { before, after })
111    }
112    pub fn range(from: Option<NaiveDate>, to: Option<NaiveDate>) -> Self {
113        Self::Range(RangeMatcher { from, to })
114    }
115    pub fn custom<F: Fn(&NaiveDate) -> bool + Send + Sync + 'static>(f: F) -> Self {
116        Self::Custom(Box::new(f))
117    }
118    pub fn is_match(&self, date: &Date) -> bool {
119        match date {
120            Date::Single(Some(v)) => self.matched(v),
121            Date::Range(Some(a), Some(b)) => self.matched(a) || self.matched(b),
122            _ => false,
123        }
124    }
125    pub fn matched(&self, date: &NaiveDate) -> bool {
126        match self {
127            Self::DayOfWeek(days) => days.contains(&date.weekday().num_days_from_sunday()),
128            Self::Interval(v) => {
129                v.before.is_some_and(|x| date < &x) || v.after.is_some_and(|x| date > &x)
130            }
131            Self::Range(v) => {
132                !v.from.is_some_and(|x| date < &x) && !v.to.is_some_and(|x| date > &x)
133            }
134            Self::Custom(f) => f(date),
135        }
136    }
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub enum CalendarView {
141    Day,
142    Month,
143    Year,
144}
145impl CalendarView {
146    pub fn is_day(self) -> bool {
147        self == Self::Day
148    }
149    pub fn is_month(self) -> bool {
150        self == Self::Month
151    }
152    pub fn is_year(self) -> bool {
153        self == Self::Year
154    }
155}
156
157fn picker_grid_layout(view: CalendarView) -> Option<(u16, f32)> {
158    match view {
159        CalendarView::Day => None,
160        CalendarView::Month => Some((3, 4.)),
161        CalendarView::Year => Some((5, 4.)),
162    }
163}
164
165pub enum CalendarEvent {
166    Selected(Date),
167}
168
169pub struct CalendarState {
170    pub focus_handle: FocusHandle,
171    view: CalendarView,
172    date: Date,
173    current_year: i32,
174    current_month: u8,
175    years: Vec<Vec<i32>>,
176    year_page: i32,
177    today: NaiveDate,
178    number_of_months: usize,
179    disabled_matcher: Option<Rc<Matcher>>,
180}
181
182impl CalendarState {
183    pub fn new(_: &mut Window, cx: &mut Context<Self>) -> Self {
184        let today = Local::now().date_naive();
185        Self {
186            focus_handle: cx.focus_handle(),
187            view: CalendarView::Day,
188            date: Date::Single(None),
189            current_year: today.year(),
190            current_month: today.month() as u8,
191            years: vec![],
192            year_page: 0,
193            today,
194            number_of_months: 1,
195            disabled_matcher: None,
196        }
197        .year_range((today.year() - 50, today.year() + 50))
198    }
199    pub fn disabled_matcher(mut self, matcher: impl Into<Matcher>) -> Self {
200        self.disabled_matcher = Some(Rc::new(matcher.into()));
201        self
202    }
203    pub fn set_disabled_matcher(
204        &mut self,
205        matcher: impl Into<Matcher>,
206        _: &mut Window,
207        _: &mut Context<Self>,
208    ) {
209        self.disabled_matcher = Some(Rc::new(matcher.into()));
210    }
211    pub fn set_disabled_matcher_shared(&mut self, matcher: Option<Rc<Matcher>>) {
212        self.disabled_matcher = matcher;
213    }
214    pub fn disabled_matcher_ref(&self) -> Option<&Matcher> {
215        self.disabled_matcher.as_deref()
216    }
217    pub fn set_date(&mut self, date: impl Into<Date>, _: &mut Window, cx: &mut Context<Self>) {
218        if self.apply_date(date.into()) {
219            cx.notify();
220        }
221    }
222    pub fn apply_date(&mut self, date: Date) -> bool {
223        if self
224            .disabled_matcher
225            .as_ref()
226            .is_some_and(|m| m.is_match(&date))
227        {
228            return false;
229        }
230        self.date = date;
231        if let Some(v) = date.start() {
232            self.current_month = v.month() as u8;
233            self.current_year = v.year();
234        }
235        true
236    }
237    pub fn select_date(&mut self, value: NaiveDate) -> bool {
238        if self
239            .disabled_matcher
240            .as_ref()
241            .is_some_and(|m| m.matched(&value))
242        {
243            return false;
244        }
245        let next = match self.date {
246            Date::Single(_) => Date::Single(Some(value)),
247            Date::Range(None, None) | Date::Range(None, Some(_)) => Date::Range(Some(value), None),
248            Date::Range(Some(start), None) if value >= start => {
249                Date::Range(Some(start), Some(value))
250            }
251            Date::Range(Some(_), None) | Date::Range(Some(_), Some(_)) => {
252                Date::Range(Some(value), None)
253            }
254        };
255        self.apply_date(next);
256        self.date.is_complete()
257    }
258    /// Activates a day item and emits [`CalendarEvent::Selected`] once the
259    /// controlled value is complete. This is the single pointer/keyboard
260    /// activation path used by the calendar root.
261    pub fn activate_date(&mut self, value: NaiveDate, cx: &mut Context<Self>) -> bool {
262        let complete = self.select_date(value);
263        if complete {
264            cx.emit(CalendarEvent::Selected(self.date()));
265        }
266        cx.notify();
267        complete
268    }
269    pub fn date(&self) -> Date {
270        self.date
271    }
272    pub fn set_number_of_months(&mut self, n: usize, _: &mut Window, cx: &mut Context<Self>) {
273        self.number_of_months = n;
274        cx.notify();
275    }
276    pub fn number_of_months(&self) -> usize {
277        self.number_of_months
278    }
279    pub fn year_range(mut self, range: (i32, i32)) -> Self {
280        self.apply_year_range(range);
281        self
282    }
283    pub fn set_year_range(&mut self, range: (i32, i32), cx: &mut Context<Self>) {
284        self.apply_year_range(range);
285        cx.notify();
286    }
287    fn apply_year_range(&mut self, range: (i32, i32)) {
288        self.years = (range.0..range.1)
289            .collect::<Vec<_>>()
290            .chunks(20)
291            .map(<[_]>::to_vec)
292            .collect();
293        self.year_page = self
294            .years
295            .iter()
296            .position(|v| v.contains(&self.current_year))
297            .unwrap_or(0) as i32;
298    }
299    pub fn offset_year_month(&self, offset: usize) -> (i32, u32) {
300        let n = self.current_month as i64 - 1 + offset as i64;
301        (
302            self.current_year + n.div_euclid(12) as i32,
303            n.rem_euclid(12) as u32 + 1,
304        )
305    }
306    pub fn days(&self) -> Vec<Vec<NaiveDate>> {
307        self.month_days().into_iter().flatten().collect()
308    }
309    /// Calendar weeks grouped by visible month. This preserves six-week
310    /// months and is the preferred rendering API.
311    pub fn month_days(&self) -> Vec<Vec<Vec<NaiveDate>>> {
312        (0..self.number_of_months)
313            .map(|n| {
314                days_in_month(
315                    self.current_year,
316                    self.current_month as u32 + n as u32,
317                    Weekday::Sun,
318                )
319            })
320            .collect()
321    }
322    pub fn has_prev_year_page(&self) -> bool {
323        self.year_page > 0
324    }
325    pub fn has_next_year_page(&self) -> bool {
326        self.year_page < self.years.len() as i32 - 1
327    }
328    pub fn prev_year_page(&mut self) -> bool {
329        if !self.has_prev_year_page() {
330            false
331        } else {
332            self.year_page -= 1;
333            true
334        }
335    }
336    pub fn next_year_page(&mut self) -> bool {
337        if !self.has_next_year_page() {
338            false
339        } else {
340            self.year_page += 1;
341            true
342        }
343    }
344    pub fn prev_month(&mut self) {
345        if self.current_month == 1 {
346            self.current_year -= 1;
347            self.current_month = 12;
348        } else {
349            self.current_month -= 1;
350        }
351    }
352    pub fn next_month(&mut self) {
353        if self.current_month == 12 {
354            self.current_year += 1;
355            self.current_month = 1;
356        } else {
357            self.current_month += 1;
358        }
359    }
360    pub fn view(&self) -> CalendarView {
361        self.view
362    }
363    pub fn set_view(&mut self, view: CalendarView) {
364        self.view = view;
365    }
366    pub fn current_year(&self) -> i32 {
367        self.current_year
368    }
369    pub fn current_month(&self) -> u8 {
370        self.current_month
371    }
372    pub fn today(&self) -> NaiveDate {
373        self.today
374    }
375    pub fn years_on_page(&self) -> &[i32] {
376        self.years
377            .get(self.year_page as usize)
378            .map(Vec::as_slice)
379            .unwrap_or_default()
380    }
381    pub fn select_month(&mut self, month: u8) {
382        self.current_month = month;
383        self.view = CalendarView::Day;
384    }
385    pub fn select_year(&mut self, year: i32) {
386        self.current_year = year;
387        self.view = CalendarView::Day;
388    }
389}
390impl EventEmitter<CalendarEvent> for CalendarState {}
391impl Render for CalendarState {
392    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
393        Empty
394    }
395}
396
397/// The semantic kind of a calendar control rendered by [`Calendar`].
398#[derive(Clone, Copy, Debug, PartialEq, Eq)]
399pub enum CalendarItemKind {
400    Previous,
401    MonthToggle,
402    YearToggle,
403    Next,
404    Weekday,
405    Day,
406    Month,
407    Year,
408}
409
410/// State exposed to a calendar item slot. Applications may use it solely to
411/// decorate the unstyled primitive; all interaction remains owned by base.
412///
413/// The fields are private and reached through the methods below, so that a new
414/// one can be added without breaking the item slots.
415#[derive(Clone, Copy, Debug)]
416pub struct CalendarItemState {
417    kind: CalendarItemKind,
418    active: bool,
419    in_range: bool,
420    muted: bool,
421    disabled: bool,
422    today: bool,
423}
424
425impl CalendarItemState {
426    /// Create a state for `kind`, with every flag off.
427    pub fn new(kind: CalendarItemKind) -> Self {
428        Self {
429            kind,
430            active: false,
431            in_range: false,
432            muted: false,
433            disabled: false,
434            today: false,
435        }
436    }
437
438    pub fn active(mut self, active: bool) -> Self {
439        self.active = active;
440        self
441    }
442
443    /// Set whether the item is between the two ends of a range selection.
444    pub fn in_range(mut self, in_range: bool) -> Self {
445        self.in_range = in_range;
446        self
447    }
448
449    /// Set whether the item is shown as secondary, e.g.: a weekday header or a
450    /// day that belongs to the neighboring month.
451    pub fn muted(mut self, muted: bool) -> Self {
452        self.muted = muted;
453        self
454    }
455
456    pub fn disabled(mut self, disabled: bool) -> Self {
457        self.disabled = disabled;
458        self
459    }
460
461    pub fn today(mut self, today: bool) -> Self {
462        self.today = today;
463        self
464    }
465
466    pub fn kind(&self) -> CalendarItemKind {
467        self.kind
468    }
469
470    pub fn is_active(&self) -> bool {
471        self.active
472    }
473
474    pub fn is_in_range(&self) -> bool {
475        self.in_range
476    }
477
478    pub fn is_muted(&self) -> bool {
479        self.muted
480    }
481
482    pub fn is_disabled(&self) -> bool {
483        self.disabled
484    }
485
486    pub fn is_today(&self) -> bool {
487        self.today
488    }
489}
490
491/// An unstyled, pre-wired calendar item passed to the item slot.
492#[derive(IntoElement)]
493pub struct CalendarItem {
494    base: crate::ObservedElement<gpui::Stateful<gpui::Div>>,
495    state: CalendarItemState,
496    style: StyleRefinement,
497    children: Vec<AnyElement>,
498}
499
500impl CalendarItem {
501    fn new(id: impl Into<ElementId>, state: CalendarItemState) -> Self {
502        Self {
503            base: div().id(id.into()).test_support(),
504            state,
505            style: StyleRefinement::default(),
506            children: vec![],
507        }
508    }
509    // Keep the default visible label available to assistive technology even
510    // when a presentation assigns a role that does not derive a name from children.
511    fn with_label(self, label: SharedString) -> Self {
512        self.aria_label(label.clone()).child(label)
513    }
514
515    pub fn item_state(&self) -> CalendarItemState {
516        self.state
517    }
518
519    /// Remove the default label so a styled facade can provide custom content.
520    pub fn clear_children(mut self) -> Self {
521        self.children.clear();
522        self
523    }
524}
525impl ParentElement for CalendarItem {
526    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
527        self.children.extend(elements);
528    }
529}
530impl Styled for CalendarItem {
531    fn style(&mut self) -> &mut StyleRefinement {
532        &mut self.style
533    }
534}
535impl InteractiveElement for CalendarItem {
536    fn interactivity(&mut self) -> &mut gpui::Interactivity {
537        self.base.interactivity()
538    }
539
540    fn track_focus(mut self, handle: &gpui::FocusHandle) -> Self {
541        self.base = self.base.track_focus(handle);
542        self
543    }
544}
545impl StatefulInteractiveElement for CalendarItem {}
546impl RenderOnce for CalendarItem {
547    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
548        self.base.children(self.children).refine_style(&self.style)
549    }
550}
551
552type ItemRenderer =
553    Rc<dyn Fn(CalendarItem, CalendarItemState, &mut Window, &mut App) -> AnyElement>;
554type Labeler = Rc<dyn Fn(CalendarItemKind, i32) -> SharedString>;
555
556/// Complete unstyled calendar structure and behavior.
557///
558/// Base owns navigation, view switching, grids, disabled/selection state and
559/// click handling. The UI crate only decorates the pre-wired item slot.
560#[derive(IntoElement)]
561pub struct Calendar {
562    id: ElementId,
563    state: Entity<CalendarState>,
564    number_of_months: usize,
565    first_day_of_week: Weekday,
566    style: StyleRefinement,
567    item: ItemRenderer,
568    label: Labeler,
569}
570
571impl Calendar {
572    pub fn new(id: impl Into<ElementId>, state: &Entity<CalendarState>) -> Self {
573        Self {
574            id: id.into(),
575            state: state.clone(),
576            number_of_months: 1,
577            first_day_of_week: Weekday::Sun,
578            style: StyleRefinement::default(),
579            item: Rc::new(|item, _, _, _| item.into_any_element()),
580            label: Rc::new(|kind, value| match kind {
581                CalendarItemKind::Previous => "‹".into(),
582                CalendarItemKind::Next => "›".into(),
583                CalendarItemKind::Weekday => value.to_string().into(),
584                _ => value.to_string().into(),
585            }),
586        }
587    }
588    pub fn number_of_months(mut self, count: usize) -> Self {
589        self.number_of_months = count.max(1);
590        self
591    }
592    pub fn first_day_of_week(mut self, day: Weekday) -> Self {
593        self.first_day_of_week = day;
594        self
595    }
596    pub fn item(
597        mut self,
598        render: impl Fn(CalendarItem, CalendarItemState, &mut Window, &mut App) -> AnyElement + 'static,
599    ) -> Self {
600        self.item = Rc::new(render);
601        self
602    }
603    pub fn label(
604        mut self,
605        label: impl Fn(CalendarItemKind, i32) -> SharedString + 'static,
606    ) -> Self {
607        self.label = Rc::new(label);
608        self
609    }
610
611    fn render_item(
612        &self,
613        id: impl Into<ElementId>,
614        state: CalendarItemState,
615        value: i32,
616        window: &mut Window,
617        cx: &mut App,
618    ) -> AnyElement {
619        let label = (self.label)(state.kind(), value);
620        (self.item)(
621            CalendarItem::new(id, state).with_label(label),
622            state,
623            window,
624            cx,
625        )
626    }
627}
628impl Styled for Calendar {
629    fn style(&mut self) -> &mut StyleRefinement {
630        &mut self.style
631    }
632}
633
634impl RenderOnce for Calendar {
635    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
636        let count = self.number_of_months;
637        self.state
638            .update(cx, |s, cx| s.set_number_of_months(count, window, cx));
639        let view = self.state.read(cx).view();
640        let mut header = h_flex()
641            .flex_row_dir(cx)
642            .items_center()
643            .justify_between()
644            .child({
645                let st = CalendarItemState::new(CalendarItemKind::Previous).disabled(
646                    view.is_month()
647                        || (view.is_year() && !self.state.read(cx).has_prev_year_page()),
648                );
649                let mut item =
650                    CalendarItem::new("calendar-prev", st).with_label((self.label)(st.kind(), 0));
651                if !st.is_disabled() {
652                    let entity = self.state.clone();
653                    item = item.on_click(move |_, _window, cx| {
654                        entity.update(cx, |s, cx| {
655                            if s.view().is_day() {
656                                s.prev_month();
657                            } else {
658                                s.prev_year_page();
659                            }
660                            cx.notify();
661                        })
662                    });
663                }
664                (self.item)(item, st, window, cx)
665            });
666        if count == 1 {
667            let (month, year) = {
668                let s = self.state.read(cx);
669                (s.current_month() as i32, s.current_year())
670            };
671            for (kind, value, active) in [
672                (CalendarItemKind::MonthToggle, month, view.is_month()),
673                (CalendarItemKind::YearToggle, year, view.is_year()),
674            ] {
675                let st = CalendarItemState::new(kind).active(active);
676                let entity = self.state.clone();
677                let mut item = CalendarItem::new(format!("calendar-{kind:?}"), st)
678                    .with_label((self.label)(kind, value));
679                item = item.on_click(move |_, _, cx| {
680                    entity.update(cx, |s, cx| {
681                        s.set_view(
682                            if s.view()
683                                == match kind {
684                                    CalendarItemKind::MonthToggle => CalendarView::Month,
685                                    _ => CalendarView::Year,
686                                }
687                            {
688                                CalendarView::Day
689                            } else {
690                                match kind {
691                                    CalendarItemKind::MonthToggle => CalendarView::Month,
692                                    _ => CalendarView::Year,
693                                }
694                            },
695                        );
696                        cx.notify();
697                    })
698                });
699                header = header.child((self.item)(item, st, window, cx));
700            }
701        } else {
702            for offset in 0..count {
703                let (y, m) = self.state.read(cx).offset_year_month(offset);
704                header = header.child(
705                    div().text_sm().font_medium().child(
706                        v_flex()
707                            .items_center()
708                            .child((self.label)(CalendarItemKind::MonthToggle, m as i32))
709                            .child(y.to_string()),
710                    ),
711                );
712            }
713        }
714        header = header.child({
715            let st = CalendarItemState::new(CalendarItemKind::Next).disabled(
716                view.is_month() || (view.is_year() && !self.state.read(cx).has_next_year_page()),
717            );
718            let mut item =
719                CalendarItem::new("calendar-next", st).with_label((self.label)(st.kind(), 0));
720            if !st.is_disabled() {
721                let entity = self.state.clone();
722                item = item.on_click(move |_, _, cx| {
723                    entity.update(cx, |s, cx| {
724                        if s.view().is_day() {
725                            s.next_month()
726                        } else {
727                            s.next_year_page();
728                        }
729                        cx.notify();
730                    })
731                });
732            }
733            (self.item)(item, st, window, cx)
734        });
735
736        let mut body = match picker_grid_layout(view) {
737            None => h_flex().flex_row_dir(cx).justify_around(),
738            Some((columns, horizontal_gap)) => {
739                div().grid().grid_cols(columns).gap_x(px(horizontal_gap))
740            }
741        };
742        if view.is_day() {
743            for offset in 0..count {
744                let (year, month_number) = self.state.read(cx).offset_year_month(offset);
745                let weeks = days_in_month(year, month_number, self.first_day_of_week);
746                let mut month = v_flex();
747                let mut header_row = h_flex().flex_row_dir(cx);
748                for weekday in 0..7 {
749                    let st = CalendarItemState::new(CalendarItemKind::Weekday)
750                        .muted(true)
751                        .disabled(true);
752                    header_row = header_row.child(self.render_item(
753                        format!("weekday-{offset}-{weekday}"),
754                        st,
755                        (weekday + self.first_day_of_week.num_days_from_sunday() as i32) % 7,
756                        window,
757                        cx,
758                    ));
759                }
760                month = month.child(header_row);
761                for (week_index, week) in weeks.iter().enumerate() {
762                    let mut week_row = h_flex().flex_row_dir(cx);
763                    for date in week {
764                        let date = *date;
765                        let st = {
766                            let s = self.state.read(cx);
767                            let (_, m) = s.offset_year_month(offset);
768                            let disabled =
769                                s.disabled_matcher_ref().is_some_and(|x| x.matched(&date));
770                            CalendarItemState::new(CalendarItemKind::Day)
771                                .active(s.date().is_active(&date))
772                                .in_range(s.date().is_in_range(&date))
773                                .muted(date.month() != m || disabled)
774                                .disabled(disabled)
775                                .today(date == s.today())
776                        };
777                        let mut item =
778                            CalendarItem::new(format!("calendar-{date}-{offset}-{week_index}"), st)
779                                .with_label((self.label)(st.kind(), date.day() as i32))
780                                .aria_label(date.to_string());
781                        if !st.is_disabled() {
782                            let entity = self.state.clone();
783                            item = item.on_click(move |_, _, cx| {
784                                entity.update(cx, |s, cx| {
785                                    s.activate_date(date, cx);
786                                })
787                            });
788                        }
789                        week_row = week_row.child((self.item)(item, st, window, cx));
790                    }
791                    month = month.child(week_row);
792                }
793                body = body.child(month);
794            }
795        } else if view.is_month() {
796            let current = self.state.read(cx).current_month();
797            for month in 1..=12u8 {
798                let st = CalendarItemState::new(CalendarItemKind::Month).active(month == current);
799                let entity = self.state.clone();
800                let item = CalendarItem::new(format!("calendar-month-{month}"), st)
801                    .with_label((self.label)(st.kind(), month as i32))
802                    .on_click(move |_, _, cx| {
803                        entity.update(cx, |s, cx| {
804                            s.select_month(month);
805                            cx.notify();
806                        })
807                    });
808                body = body.child((self.item)(item, st, window, cx));
809            }
810        } else {
811            let current = self.state.read(cx).current_year();
812            let years = self.state.read(cx).years_on_page().to_vec();
813            for year in years {
814                let st = CalendarItemState::new(CalendarItemKind::Year).active(year == current);
815                let entity = self.state.clone();
816                let item = CalendarItem::new(format!("calendar-year-{year}"), st)
817                    .with_label((self.label)(st.kind(), year))
818                    .on_click(move |_, _, cx| {
819                        entity.update(cx, |s, cx| {
820                            s.select_year(year);
821                            cx.notify();
822                        })
823                    });
824                body = body.child((self.item)(item, st, window, cx));
825            }
826        }
827        v_flex()
828            .id(self.id)
829            .track_focus(&self.state.read(cx).focus_handle)
830            .child(header)
831            .child(body)
832            .refine_style(&self.style)
833    }
834}
835
836fn days_in_month(year: i32, month: u32, first_day: Weekday) -> Vec<Vec<NaiveDate>> {
837    let total = year as i64 * 12 + month as i64 - 1;
838    let year = total.div_euclid(12) as i32;
839    let month = total.rem_euclid(12) as u32 + 1;
840    let first = NaiveDate::from_ymd_opt(year, month, 1).unwrap();
841    let next = if month == 12 {
842        NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap()
843    } else {
844        NaiveDate::from_ymd_opt(year, month + 1, 1).unwrap()
845    };
846    let offset =
847        (first.weekday().num_days_from_sunday() + 7 - first_day.num_days_from_sunday()) % 7;
848    let start = first - chrono::Duration::days(offset as i64);
849    let count = ((next - start).num_days() as usize).div_ceil(7) * 7;
850    (0..count)
851        .map(|n| start + chrono::Duration::days(n as i64))
852        .collect::<Vec<_>>()
853        .chunks(7)
854        .map(<[_]>::to_vec)
855        .collect()
856}
857
858#[cfg(test)]
859mod tests {
860    use std::{cell::RefCell, rc::Rc};
861
862    use gpui::{AppContext as _, Context, Entity, IntoElement, Render, Subscription, Window};
863
864    use super::*;
865
866    #[gpui::test]
867    fn calendar_items_expose_names_in_every_view(cx: &mut gpui::TestAppContext) {
868        use gpui::{Element as _, accesskit};
869        struct Names {
870            calendar: Entity<CalendarState>,
871            labels: Rc<RefCell<Vec<(CalendarItemKind, String)>>>,
872        }
873        impl Render for Names {
874            fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
875                let labels = self.labels.clone();
876                labels.borrow_mut().clear();
877                Calendar::new("named-calendar", &self.calendar).item(move |item, state, _, _| {
878                    let mut node = accesskit::Node::new(accesskit::Role::Button);
879                    item.base.write_a11y_info(&mut node);
880                    labels
881                        .borrow_mut()
882                        .push((state.kind(), node.label().unwrap_or("").to_string()));
883                    item.into_any_element()
884                })
885            }
886        }
887        let (view, cx) = cx.add_window_view(|window, cx| Names {
888            calendar: cx.new(|cx| CalendarState::new(window, cx)),
889            labels: Rc::new(RefCell::new(Vec::new())),
890        });
891        for mode in [CalendarView::Day, CalendarView::Month, CalendarView::Year] {
892            cx.update(|window, cx| {
893                view.read(cx).calendar.clone().update(cx, |state, cx| {
894                    state.apply_date(Date::Single(Some(
895                        NaiveDate::from_ymd_opt(2026, 9, 7).unwrap(),
896                    )));
897                    state.set_view(mode);
898                    cx.notify();
899                });
900                window.draw(cx).clear(cx);
901                let labels = view.read(cx).labels.borrow();
902                assert!(!labels.is_empty());
903                for (kind, label) in labels.iter() {
904                    assert!(!label.is_empty(), "missing name for {kind:?}");
905                }
906                if mode.is_day() {
907                    assert!(labels.iter().any(
908                        |(kind, label)| *kind == CalendarItemKind::Day && label == "2026-09-07"
909                    ));
910                }
911            });
912        }
913    }
914
915    struct EventHarness {
916        calendar: Entity<CalendarState>,
917        events: Rc<RefCell<Vec<Date>>>,
918        _subscription: Option<Subscription>,
919    }
920    impl EventHarness {
921        fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
922            let calendar = cx.new(|cx| CalendarState::new(window, cx));
923            let events = Rc::new(RefCell::new(Vec::new()));
924            let mut this = Self {
925                calendar: calendar.clone(),
926                events: events.clone(),
927                _subscription: None,
928            };
929            this._subscription = Some(cx.subscribe(&calendar, move |_, _, event, _| {
930                let CalendarEvent::Selected(date) = event;
931                events.borrow_mut().push(*date);
932            }));
933            this
934        }
935    }
936    impl Render for EventHarness {
937        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
938            Empty
939        }
940    }
941    fn state(cx: &mut gpui::TestAppContext, date: Date) -> gpui::Entity<CalendarState> {
942        let (state, _) = cx.add_window_view(CalendarState::new);
943        state.update(cx, |state, _| {
944            state.date = date;
945        });
946        state
947    }
948    #[gpui::test]
949    fn range_selection_restarts_and_completes(cx: &mut gpui::TestAppContext) {
950        let s = state(cx, Date::Range(None, None));
951        let a = NaiveDate::from_ymd_opt(2025, 2, 10).unwrap();
952        let b = NaiveDate::from_ymd_opt(2025, 2, 12).unwrap();
953        s.update(cx, |s, _| {
954            assert!(!s.select_date(a));
955            assert!(s.select_date(b));
956        });
957        assert_eq!(
958            s.read_with(cx, |s, _| s.date()),
959            Date::Range(Some(a), Some(b))
960        );
961        s.update(cx, |s, _| assert!(!s.select_date(a)));
962        assert_eq!(s.read_with(cx, |s, _| s.date()), Date::Range(Some(a), None));
963    }
964    #[gpui::test]
965    fn disabled_date_is_rejected(cx: &mut gpui::TestAppContext) {
966        let s = state(cx, Date::Single(None));
967        s.update(cx, |s, _| {
968            s.disabled_matcher = Some(Rc::new(Matcher::range(
969                Some(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap()),
970                Some(NaiveDate::from_ymd_opt(2025, 1, 31).unwrap()),
971            )))
972        });
973        s.update(cx, |s, _| {
974            assert!(!s.select_date(NaiveDate::from_ymd_opt(2025, 1, 2).unwrap()))
975        });
976    }
977    #[gpui::test]
978    fn month_navigation_crosses_year(cx: &mut gpui::TestAppContext) {
979        let s = state(
980            cx,
981            Date::Single(Some(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap())),
982        );
983        s.update(cx, |s, _| {
984            s.apply_date(Date::Single(Some(
985                NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
986            )));
987            s.prev_month();
988        });
989        assert_eq!(
990            s.read_with(cx, |s, _| (s.current_year(), s.current_month())),
991            (2024, 12)
992        );
993        s.update(cx, |s, _| s.next_month());
994        assert_eq!(
995            s.read_with(cx, |s, _| (s.current_year(), s.current_month())),
996            (2025, 1)
997        );
998    }
999
1000    #[gpui::test]
1001    fn six_week_month_is_not_truncated(cx: &mut gpui::TestAppContext) {
1002        let s = state(
1003            cx,
1004            Date::Single(Some(NaiveDate::from_ymd_opt(2025, 8, 1).unwrap())),
1005        );
1006        s.update(cx, |s, _| {
1007            s.apply_date(Date::Single(Some(
1008                NaiveDate::from_ymd_opt(2025, 8, 1).unwrap(),
1009            )));
1010            assert_eq!(s.month_days().len(), 1);
1011            assert_eq!(s.month_days()[0].len(), 6);
1012            assert_eq!(s.days().len(), 6);
1013            assert_eq!(s.month_days()[0][5][0].day(), 31);
1014        });
1015    }
1016
1017    #[gpui::test]
1018    fn day_month_and_year_views_have_complete_transitions(cx: &mut gpui::TestAppContext) {
1019        let s = state(cx, Date::Single(None));
1020        s.update(cx, |s, _| {
1021            assert_eq!(s.view(), CalendarView::Day);
1022            s.set_view(CalendarView::Month);
1023            s.select_month(11);
1024            assert_eq!((s.view(), s.current_month()), (CalendarView::Day, 11));
1025            s.set_view(CalendarView::Year);
1026            s.select_year(2032);
1027            assert_eq!((s.view(), s.current_year()), (CalendarView::Day, 2032));
1028        });
1029    }
1030
1031    #[test]
1032    fn picker_views_use_stable_grid_layouts() {
1033        assert_eq!(picker_grid_layout(CalendarView::Month), Some((3, 4.)));
1034        assert_eq!(picker_grid_layout(CalendarView::Year), Some((5, 4.)));
1035        assert_eq!(picker_grid_layout(CalendarView::Day), None);
1036    }
1037
1038    #[gpui::test]
1039    fn year_page_navigation_respects_both_bounds(cx: &mut gpui::TestAppContext) {
1040        let s = state(cx, Date::Single(None));
1041        s.update(cx, |s, _| {
1042            s.apply_year_range((2000, 2041));
1043            while s.prev_year_page() {}
1044            assert!(!s.has_prev_year_page());
1045            assert!(!s.prev_year_page());
1046            assert!(s.next_year_page());
1047            while s.next_year_page() {}
1048            assert!(!s.has_next_year_page());
1049            assert!(!s.next_year_page());
1050        });
1051    }
1052
1053    #[gpui::test]
1054    fn activation_emits_only_for_complete_enabled_values(cx: &mut gpui::TestAppContext) {
1055        let (harness, _) = cx.add_window_view(EventHarness::new);
1056        let s = harness.read_with(cx, |h, _| h.calendar.clone());
1057        s.update(cx, |s, _| s.date = Date::Range(None, None));
1058        let start = NaiveDate::from_ymd_opt(2025, 4, 4).unwrap();
1059        let end = NaiveDate::from_ymd_opt(2025, 4, 8).unwrap();
1060        s.update(cx, |s, cx| {
1061            assert!(!s.activate_date(start, cx));
1062            assert!(s.activate_date(end, cx));
1063            assert_eq!(s.date(), Date::Range(Some(start), Some(end)));
1064            s.set_disabled_matcher_shared(Some(Rc::new(Matcher::custom(move |d| *d == start))));
1065            assert!(!s.activate_date(start, cx));
1066            assert_eq!(s.date(), Date::Range(Some(start), Some(end)));
1067        });
1068        assert_eq!(
1069            harness.read_with(cx, |h, _| h.events.borrow().clone()),
1070            vec![Date::Range(Some(start), Some(end))]
1071        );
1072
1073        s.update(cx, |s, cx| {
1074            s.date = Date::Single(None);
1075            assert!(s.activate_date(end, cx));
1076            assert_eq!(s.date(), Date::Single(Some(end)));
1077        });
1078        assert_eq!(
1079            harness.read_with(cx, |h, _| h.events.borrow().clone()),
1080            vec![Date::Range(Some(start), Some(end)), Date::Single(Some(end))]
1081        );
1082    }
1083}