| 1 | //! Mini month grid shared by the date picker popover and the calendar list pane. |
| 2 | |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use chrono::{Datelike, Duration, NaiveDate, Weekday}; |
| 6 | use gpui_kit::component::{h_flex, v_flex}; |
| 7 | use gpui_kit::prelude::FluentBuilder as _; |
| 8 | use gpui_kit::*; |
| 9 | |
| 10 | use crate::call::calendar_list::mini_month_cell; |
| 11 | use crate::call::week_grid::{MONTH_GRID_DAYS, month_grid_start}; |
| 12 | use crate::theme; |
| 13 | |
| 14 | pub type PickDate = Rc<dyn Fn(NaiveDate, &mut Window, &mut App)>; |
| 15 | |
| 16 | /// Weekday initials in column order from the first day, in the app language. |
| 17 | pub fn weekday_letters(first_day: Weekday) -> [String; 7] { |
| 18 | let mut day = first_day; |
| 19 | std::array::from_fn(|_| { |
| 20 | let letter = app_base::format::weekday_name(day, app_base::format::WeekdayWidth::Letter); |
| 21 | day = day.succ(); |
| 22 | letter |
| 23 | }) |
| 24 | } |
| 25 | |
| 26 | /// Weekday initials and six weeks of day cells; the shown range is a band, today a circle. |
| 27 | #[allow(clippy::too_many_arguments)] |
| 28 | pub fn mini_month_grid( |
| 29 | id: &'static str, |
| 30 | month: NaiveDate, |
| 31 | shown: &[NaiveDate], |
| 32 | first_day: Weekday, |
| 33 | today: NaiveDate, |
| 34 | cell_size: f32, |
| 35 | on_pick: PickDate, |
| 36 | ) -> Div { |
| 37 | let start = month_grid_start(month, first_day); |
| 38 | let weeks = (0..MONTH_GRID_DAYS) |
| 39 | .map(|offset| start + Duration::days(offset)) |
| 40 | .collect::<Vec<_>>() |
| 41 | .chunks(7) |
| 42 | .map(|week| { |
| 43 | h_flex().children(week.iter().map(|date| { |
| 44 | let cell = mini_month_cell(*date, month, shown, today); |
| 45 | let pick = on_pick.clone(); |
| 46 | let date = *date; |
| 47 | div() |
| 48 | .id(SharedString::from(format!("{id}-{date}"))) |
| 49 | .size(px(cell_size)) |
| 50 | .flex() |
| 51 | .items_center() |
| 52 | .justify_center() |
| 53 | .when(cell.shown, |band| band.bg(theme::popup_hover())) |
| 54 | .child( |
| 55 | div() |
| 56 | .size(px(cell_size - 4.)) |
| 57 | .flex() |
| 58 | .items_center() |
| 59 | .justify_center() |
| 60 | .rounded_full() |
| 61 | .cursor_pointer() |
| 62 | .text_size(px(12.)) |
| 63 | .text_color(if cell.in_month { |
| 64 | theme::text() |
| 65 | } else { |
| 66 | theme::text_faint() |
| 67 | }) |
| 68 | .when(cell.today, |circle| { |
| 69 | circle |
| 70 | .bg(theme::accent()) |
| 71 | .text_color(theme::on_accent()) |
| 72 | .font_weight(FontWeight::SEMIBOLD) |
| 73 | }) |
| 74 | .when(!cell.today, |circle| { |
| 75 | circle.hover(|circle| circle.bg(theme::border_strong())) |
| 76 | }) |
| 77 | .child(date.day().to_string()), |
| 78 | ) |
| 79 | .on_click(move |_, window, cx| pick(date, window, cx)) |
| 80 | })) |
| 81 | }) |
| 82 | .collect::<Vec<_>>(); |
| 83 | v_flex() |
| 84 | .child(h_flex().children(weekday_letters(first_day).map(|letter| { |
| 85 | div() |
| 86 | .size(px(cell_size)) |
| 87 | .flex() |
| 88 | .items_center() |
| 89 | .justify_center() |
| 90 | .text_size(px(11.)) |
| 91 | .text_color(theme::text_muted()) |
| 92 | .child(letter) |
| 93 | }))) |
| 94 | .children(weeks) |
| 95 | } |