| 1 | use std::cell::RefCell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use chrono::{Datelike, Duration, Local, Months, NaiveDate, Weekday}; |
| 5 | use gpui_kit::assets::IconName; |
| 6 | use gpui_kit::component::Sizable as _; |
| 7 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 8 | use gpui_kit::component::popover::{Popover, PopoverState}; |
| 9 | use gpui_kit::component::{h_flex, v_flex}; |
| 10 | use gpui_kit::prelude::FluentBuilder as _; |
| 11 | use gpui_kit::*; |
| 12 | |
| 13 | use crate::app_state::AppState; |
| 14 | use crate::call::week_grid::{MONTH_GRID_DAYS, first_of_month, month_grid_start}; |
| 15 | use crate::theme; |
| 16 | |
| 17 | pub const DATE_PICKER_TAG: &str = "shell/islands/calendar/date-picker"; |
| 18 | const CELL_SIZE: f32 = 32.; |
| 19 | const PICKER_PADDING: f32 = 12.; |
| 20 | |
| 21 | pub struct DatePicker { |
| 22 | app: Entity<AppState>, |
| 23 | month: NaiveDate, |
| 24 | shown: Vec<NaiveDate>, |
| 25 | first_day: Weekday, |
| 26 | popover: WeakEntity<PopoverState>, |
| 27 | } |
| 28 | |
| 29 | impl DatePicker { |
| 30 | fn new( |
| 31 | app: Entity<AppState>, |
| 32 | shown: Vec<NaiveDate>, |
| 33 | first_day: Weekday, |
| 34 | month: NaiveDate, |
| 35 | popover: WeakEntity<PopoverState>, |
| 36 | ) -> Self { |
| 37 | DatePicker { |
| 38 | app, |
| 39 | month: first_of_month(month), |
| 40 | shown, |
| 41 | first_day, |
| 42 | popover, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn move_month(&mut self, forward: bool, cx: &mut Context<Self>) { |
| 47 | let moved = if forward { |
| 48 | self.month.checked_add_months(Months::new(1)) |
| 49 | } else { |
| 50 | self.month.checked_sub_months(Months::new(1)) |
| 51 | }; |
| 52 | self.month = moved.unwrap_or(self.month); |
| 53 | cx.notify(); |
| 54 | } |
| 55 | |
| 56 | fn pick(&mut self, date: NaiveDate, window: &mut Window, cx: &mut Context<Self>) { |
| 57 | self.app |
| 58 | .update(cx, |state, cx| state.show_calendar_date(date, cx)); |
| 59 | self.popover |
| 60 | .update(cx, |popover, cx| popover.dismiss(window, cx)) |
| 61 | .ok(); |
| 62 | } |
| 63 | |
| 64 | fn day_cell(&self, date: NaiveDate, today: NaiveDate, cx: &mut Context<Self>) -> Stateful<Div> { |
| 65 | let in_month = date.month() == self.month.month(); |
| 66 | let shown = self.shown.contains(&date); |
| 67 | let is_today = date == today; |
| 68 | div() |
| 69 | .id(SharedString::from(format!("date-picker-{date}"))) |
| 70 | .size(px(CELL_SIZE)) |
| 71 | .flex() |
| 72 | .items_center() |
| 73 | .justify_center() |
| 74 | .rounded_full() |
| 75 | .cursor_pointer() |
| 76 | .text_size(px(12.)) |
| 77 | .text_color(if in_month { |
| 78 | theme::text() |
| 79 | } else { |
| 80 | theme::text_faint() |
| 81 | }) |
| 82 | .when(shown && !is_today, |cell| cell.bg(theme::popup_hover())) |
| 83 | .when(is_today, |cell| { |
| 84 | cell.bg(theme::accent()).text_color(theme::on_accent()) |
| 85 | }) |
| 86 | .when(!is_today, |cell| { |
| 87 | cell.hover(|cell| cell.bg(theme::border_strong())) |
| 88 | }) |
| 89 | .child(date.day().to_string()) |
| 90 | .on_click(cx.listener(move |this, _, window, cx| this.pick(date, window, cx))) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl Render for DatePicker { |
| 95 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 96 | let today = Local::now().date_naive(); |
| 97 | let start = month_grid_start(self.month, self.first_day); |
| 98 | let days: Vec<NaiveDate> = (0..MONTH_GRID_DAYS) |
| 99 | .map(|offset| start + Duration::days(offset)) |
| 100 | .collect(); |
| 101 | let weeks = days |
| 102 | .chunks(7) |
| 103 | .map(|week| { |
| 104 | h_flex().children( |
| 105 | week.iter() |
| 106 | .map(|date| self.day_cell(*date, today, cx)) |
| 107 | .collect::<Vec<_>>(), |
| 108 | ) |
| 109 | }) |
| 110 | .collect::<Vec<_>>(); |
| 111 | v_flex() |
| 112 | .debug_selector(|| DATE_PICKER_TAG.into()) |
| 113 | .p(px(PICKER_PADDING)) |
| 114 | .gap(px(4.)) |
| 115 | .child( |
| 116 | h_flex() |
| 117 | .items_center() |
| 118 | .justify_between() |
| 119 | .pb(px(4.)) |
| 120 | .child( |
| 121 | div() |
| 122 | .pl(px(6.)) |
| 123 | .text_size(px(14.)) |
| 124 | .font_weight(FontWeight::SEMIBOLD) |
| 125 | .text_color(theme::text()) |
| 126 | .child(app_base::format::month_year_label(self.month)), |
| 127 | ) |
| 128 | .child( |
| 129 | h_flex() |
| 130 | .child( |
| 131 | Button::new("date-picker-back") |
| 132 | .small() |
| 133 | .ghost() |
| 134 | .icon(IconName::ChevronLeft) |
| 135 | .tooltip(crate::i18n::tr("calendar.view.previous_month_short")) |
| 136 | .on_click( |
| 137 | cx.listener(|this, _, _, cx| this.move_month(false, cx)), |
| 138 | ), |
| 139 | ) |
| 140 | .child( |
| 141 | Button::new("date-picker-forward") |
| 142 | .small() |
| 143 | .ghost() |
| 144 | .icon(IconName::ChevronRight) |
| 145 | .tooltip(crate::i18n::tr("calendar.view.next_month_short")) |
| 146 | .on_click( |
| 147 | cx.listener(|this, _, _, cx| this.move_month(true, cx)), |
| 148 | ), |
| 149 | ), |
| 150 | ), |
| 151 | ) |
| 152 | .child( |
| 153 | h_flex().children(weekday_letters(self.first_day).map(|letter| { |
| 154 | div() |
| 155 | .size(px(CELL_SIZE)) |
| 156 | .flex() |
| 157 | .items_center() |
| 158 | .justify_center() |
| 159 | .text_size(px(11.)) |
| 160 | .text_color(theme::text_muted()) |
| 161 | .child(letter) |
| 162 | })), |
| 163 | ) |
| 164 | .children(weeks) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// Weekday initials in column order from the first day, in the app language. |
| 169 | fn weekday_letters(first_day: Weekday) -> [String; 7] { |
| 170 | let mut day = first_day; |
| 171 | std::array::from_fn(|_| { |
| 172 | let letter = app_base::format::weekday_name(day, app_base::format::WeekdayWidth::Letter); |
| 173 | day = day.succ(); |
| 174 | letter |
| 175 | }) |
| 176 | } |
| 177 | |
| 178 | pub fn date_picker_button( |
| 179 | app: &Entity<AppState>, |
| 180 | title: String, |
| 181 | shown: Vec<NaiveDate>, |
| 182 | first_day: Weekday, |
| 183 | month: NaiveDate, |
| 184 | ) -> AnyElement { |
| 185 | let app = app.clone(); |
| 186 | let picker: Rc<RefCell<Option<Entity<DatePicker>>>> = Rc::default(); |
| 187 | let reset = picker.clone(); |
| 188 | Popover::new("calendar-date-picker") |
| 189 | .anchor(Anchor::TopLeft) |
| 190 | .p(px(0.)) |
| 191 | .trigger( |
| 192 | Button::new("calendar-date-title") |
| 193 | .small() |
| 194 | .ghost() |
| 195 | .label(title) |
| 196 | .dropdown_caret(true), |
| 197 | ) |
| 198 | .on_open_change(move |open, _, _| { |
| 199 | if !*open { |
| 200 | reset.borrow_mut().take(); |
| 201 | } |
| 202 | }) |
| 203 | .content(move |_, _, cx| { |
| 204 | if let Some(existing) = picker.borrow().clone() { |
| 205 | return existing; |
| 206 | } |
| 207 | let popover = cx.entity().downgrade(); |
| 208 | let app = app.clone(); |
| 209 | let created = |
| 210 | cx.new(|_| DatePicker::new(app, shown.clone(), first_day, month, popover)); |
| 211 | *picker.borrow_mut() = Some(created.clone()); |
| 212 | created |
| 213 | }) |
| 214 | .into_any_element() |
| 215 | } |