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