| 1 | use std::cell::RefCell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use chrono::{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::*; |
| 11 | |
| 12 | use super::calendar_mini_month::mini_month_grid; |
| 13 | use crate::app_state::AppState; |
| 14 | use crate::call::week_grid::first_of_month; |
| 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 | |
| 65 | impl Render for DatePicker { |
| 66 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 67 | let today = Local::now().date_naive(); |
| 68 | let picker = cx.entity().downgrade(); |
| 69 | let grid = mini_month_grid( |
| 70 | "date-picker", |
| 71 | self.month, |
| 72 | &self.shown, |
| 73 | self.first_day, |
| 74 | today, |
| 75 | CELL_SIZE, |
| 76 | Rc::new(move |date, window, cx| { |
| 77 | picker |
| 78 | .update(cx, |picker, cx| picker.pick(date, window, cx)) |
| 79 | .ok(); |
| 80 | }), |
| 81 | ); |
| 82 | v_flex() |
| 83 | .debug_selector(|| DATE_PICKER_TAG.into()) |
| 84 | .p(px(PICKER_PADDING)) |
| 85 | .gap(px(4.)) |
| 86 | .child( |
| 87 | h_flex() |
| 88 | .items_center() |
| 89 | .justify_between() |
| 90 | .pb(px(4.)) |
| 91 | .child( |
| 92 | div() |
| 93 | .pl(px(6.)) |
| 94 | .text_size(px(14.)) |
| 95 | .font_weight(FontWeight::SEMIBOLD) |
| 96 | .text_color(theme::text()) |
| 97 | .child(app_base::format::month_year_label(self.month)), |
| 98 | ) |
| 99 | .child( |
| 100 | h_flex() |
| 101 | .child( |
| 102 | Button::new("date-picker-back") |
| 103 | .small() |
| 104 | .ghost() |
| 105 | .icon(IconName::ChevronLeft) |
| 106 | .tooltip(crate::i18n::tr("calendar.view.previous_month_short")) |
| 107 | .on_click( |
| 108 | cx.listener(|this, _, _, cx| this.move_month(false, cx)), |
| 109 | ), |
| 110 | ) |
| 111 | .child( |
| 112 | Button::new("date-picker-forward") |
| 113 | .small() |
| 114 | .ghost() |
| 115 | .icon(IconName::ChevronRight) |
| 116 | .tooltip(crate::i18n::tr("calendar.view.next_month_short")) |
| 117 | .on_click( |
| 118 | cx.listener(|this, _, _, cx| this.move_month(true, cx)), |
| 119 | ), |
| 120 | ), |
| 121 | ), |
| 122 | ) |
| 123 | .child(grid) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub fn date_picker_button( |
| 128 | app: &Entity<AppState>, |
| 129 | title: String, |
| 130 | shown: Vec<NaiveDate>, |
| 131 | first_day: Weekday, |
| 132 | month: NaiveDate, |
| 133 | ) -> AnyElement { |
| 134 | let app = app.clone(); |
| 135 | let picker: Rc<RefCell<Option<Entity<DatePicker>>>> = Rc::default(); |
| 136 | let reset = picker.clone(); |
| 137 | Popover::new("calendar-date-picker") |
| 138 | .anchor(Anchor::TopLeft) |
| 139 | .p(px(0.)) |
| 140 | .trigger( |
| 141 | Button::new("calendar-date-title") |
| 142 | .small() |
| 143 | .ghost() |
| 144 | .label(title) |
| 145 | .dropdown_caret(true), |
| 146 | ) |
| 147 | .on_open_change(move |open, _, _| { |
| 148 | if !*open { |
| 149 | reset.borrow_mut().take(); |
| 150 | } |
| 151 | }) |
| 152 | .content(move |_, _, cx| { |
| 153 | if let Some(existing) = picker.borrow().clone() { |
| 154 | return existing; |
| 155 | } |
| 156 | let popover = cx.entity().downgrade(); |
| 157 | let app = app.clone(); |
| 158 | let created = |
| 159 | cx.new(|_| DatePicker::new(app, shown.clone(), first_day, month, popover)); |
| 160 | *picker.borrow_mut() = Some(created.clone()); |
| 161 | created |
| 162 | }) |
| 163 | .into_any_element() |
| 164 | } |