| 1 | use app_base::format::clock_format; |
| 2 | use chatsvc::CalendarEvent; |
| 3 | use chrono::{Datelike, FixedOffset, NaiveDate}; |
| 4 | use gpui_kit::component::{h_flex, v_flex}; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | |
| 8 | use crate::app_state::AppState; |
| 9 | use crate::call::week_grid::{WeekDay, month_shown}; |
| 10 | use crate::theme; |
| 11 | |
| 12 | pub const MONTH_GRID_TAG: &str = "shell/islands/calendar/month"; |
| 13 | const WEEKDAY_NAMES: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; |
| 14 | const WEEKDAY_HEADER_HEIGHT: f32 = 32.; |
| 15 | const VISIBLE_EVENTS: usize = 3; |
| 16 | const DAY_NUMBER_SIZE: f32 = 24.; |
| 17 | const CHIP_HEIGHT: f32 = 20.; |
| 18 | |
| 19 | fn day_events(day: &WeekDay) -> Vec<usize> { |
| 20 | let mut timed: Vec<(i64, usize)> = day |
| 21 | .blocks |
| 22 | .iter() |
| 23 | .map(|block| (block.start_minute, block.event_index)) |
| 24 | .collect(); |
| 25 | timed.sort(); |
| 26 | day.all_day |
| 27 | .iter() |
| 28 | .copied() |
| 29 | .chain(timed.into_iter().map(|(_, index)| index)) |
| 30 | .collect() |
| 31 | } |
| 32 | |
| 33 | fn more_label(hidden: usize) -> String { |
| 34 | format!("+{hidden} more") |
| 35 | } |
| 36 | |
| 37 | fn event_chip(event: &CalendarEvent, offset: FixedOffset) -> Div { |
| 38 | let time = (!event.all_day).then(|| { |
| 39 | event |
| 40 | .start |
| 41 | .with_timezone(&offset) |
| 42 | .format(clock_format()) |
| 43 | .to_string() |
| 44 | }); |
| 45 | h_flex() |
| 46 | .h(px(CHIP_HEIGHT)) |
| 47 | .flex_none() |
| 48 | .w_full() |
| 49 | .px(px(6.)) |
| 50 | .gap(px(6.)) |
| 51 | .items_center() |
| 52 | .rounded(px(4.)) |
| 53 | .bg(theme::accent().opacity(if event.all_day { 0.28 } else { 0.14 })) |
| 54 | .text_size(px(11.5)) |
| 55 | .text_color(theme::text()) |
| 56 | .children(time.map(|time| { |
| 57 | div() |
| 58 | .flex_none() |
| 59 | .text_color(theme::text_soft()) |
| 60 | .child(time) |
| 61 | })) |
| 62 | .child(div().flex_1().min_w_0().truncate().child(event.subject.clone())) |
| 63 | } |
| 64 | |
| 65 | fn day_cell( |
| 66 | state: &Entity<AppState>, |
| 67 | day: &WeekDay, |
| 68 | events: &[CalendarEvent], |
| 69 | shown_month: NaiveDate, |
| 70 | today: NaiveDate, |
| 71 | offset: FixedOffset, |
| 72 | ) -> Stateful<Div> { |
| 73 | let date = day.date; |
| 74 | let in_month = date.month() == shown_month.month(); |
| 75 | let is_today = date == today; |
| 76 | let indexes = day_events(day); |
| 77 | let hidden = indexes.len().saturating_sub(VISIBLE_EVENTS); |
| 78 | let number = if date.day() == 1 { |
| 79 | date.format("%b %-d").to_string() |
| 80 | } else { |
| 81 | date.day().to_string() |
| 82 | }; |
| 83 | let open_state = state.clone(); |
| 84 | v_flex() |
| 85 | .id(SharedString::from(format!("calendar-month-{date}"))) |
| 86 | .flex_1() |
| 87 | .min_w_0() |
| 88 | .h_full() |
| 89 | .p(px(4.)) |
| 90 | .gap(px(2.)) |
| 91 | .overflow_hidden() |
| 92 | .cursor_pointer() |
| 93 | .when(!in_month, |cell| cell.bg(theme::background().opacity(0.35))) |
| 94 | .hover(|cell| cell.bg(theme::row_hover())) |
| 95 | .child( |
| 96 | h_flex().child( |
| 97 | div() |
| 98 | .h(px(DAY_NUMBER_SIZE)) |
| 99 | .min_w(px(DAY_NUMBER_SIZE)) |
| 100 | .px(px(6.)) |
| 101 | .flex() |
| 102 | .items_center() |
| 103 | .justify_center() |
| 104 | .rounded_full() |
| 105 | .text_size(px(12.)) |
| 106 | .when(is_today, |number| { |
| 107 | number |
| 108 | .bg(theme::accent()) |
| 109 | .text_color(theme::on_accent()) |
| 110 | .font_weight(FontWeight::SEMIBOLD) |
| 111 | }) |
| 112 | .when(!is_today, |number| { |
| 113 | number.text_color(if in_month { |
| 114 | theme::text() |
| 115 | } else { |
| 116 | theme::text_faint() |
| 117 | }) |
| 118 | }) |
| 119 | .child(number), |
| 120 | ), |
| 121 | ) |
| 122 | .children( |
| 123 | indexes |
| 124 | .iter() |
| 125 | .take(VISIBLE_EVENTS) |
| 126 | .filter_map(|index| events.get(*index)) |
| 127 | .map(|event| event_chip(event, offset)), |
| 128 | ) |
| 129 | .when(hidden > 0, |cell| { |
| 130 | cell.child( |
| 131 | div() |
| 132 | .px(px(6.)) |
| 133 | .text_size(px(11.5)) |
| 134 | .text_color(theme::text_muted()) |
| 135 | .child(more_label(hidden)), |
| 136 | ) |
| 137 | }) |
| 138 | .on_click(move |_, _, cx| { |
| 139 | open_state.update(cx, |state, cx| state.show_calendar_day(date, cx)); |
| 140 | }) |
| 141 | } |
| 142 | |
| 143 | fn horizontal_line() -> Div { |
| 144 | div().w_full().h(px(1.)).flex_none().bg(theme::border()) |
| 145 | } |
| 146 | |
| 147 | fn vertical_line() -> Div { |
| 148 | div().w(px(1.)).h_full().flex_none().bg(theme::border()) |
| 149 | } |
| 150 | |
| 151 | pub fn month_grid( |
| 152 | state: &Entity<AppState>, |
| 153 | start: NaiveDate, |
| 154 | days: &[WeekDay], |
| 155 | events: &[CalendarEvent], |
| 156 | today: NaiveDate, |
| 157 | offset: FixedOffset, |
| 158 | ) -> Div { |
| 159 | let shown_month = month_shown(start); |
| 160 | let mut rows = v_flex().flex_1().min_h_0().w_full(); |
| 161 | for week in days.chunks(7) { |
| 162 | let mut row = h_flex().flex_1().min_h_0().w_full(); |
| 163 | for (position, day) in week.iter().enumerate() { |
| 164 | if position > 0 { |
| 165 | row = row.child(vertical_line()); |
| 166 | } |
| 167 | row = row.child(day_cell(state, day, events, shown_month, today, offset)); |
| 168 | } |
| 169 | rows = rows.child(horizontal_line()).child(row); |
| 170 | } |
| 171 | v_flex() |
| 172 | .debug_selector(|| MONTH_GRID_TAG.into()) |
| 173 | .flex_1() |
| 174 | .min_h_0() |
| 175 | .w_full() |
| 176 | .child( |
| 177 | h_flex() |
| 178 | .h(px(WEEKDAY_HEADER_HEIGHT)) |
| 179 | .flex_none() |
| 180 | .w_full() |
| 181 | .children(WEEKDAY_NAMES.iter().map(|name| { |
| 182 | div() |
| 183 | .flex_1() |
| 184 | .px(px(10.)) |
| 185 | .text_size(px(12.)) |
| 186 | .text_color(theme::text_muted()) |
| 187 | .child(*name) |
| 188 | })), |
| 189 | ) |
| 190 | .child(rows) |
| 191 | } |
| 192 | |
| 193 | #[cfg(test)] |
| 194 | mod tests { |
| 195 | use super::{WeekDay, day_events, more_label}; |
| 196 | use crate::call::week_grid::WeekBlock; |
| 197 | use chrono::NaiveDate; |
| 198 | |
| 199 | #[test] |
| 200 | fn a_day_lists_all_day_events_first_then_by_start() { |
| 201 | let block = |event_index, start_minute| WeekBlock { |
| 202 | event_index, |
| 203 | start_minute, |
| 204 | end_minute: start_minute + 30, |
| 205 | lane: 0, |
| 206 | lanes: 1, |
| 207 | }; |
| 208 | let day = WeekDay { |
| 209 | date: NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(), |
| 210 | all_day: vec![4], |
| 211 | blocks: vec![block(2, 600), block(1, 540)], |
| 212 | }; |
| 213 | assert_eq!(day_events(&day), [4, 1, 2]); |
| 214 | assert_eq!(more_label(2), "+2 more"); |
| 215 | } |
| 216 | } |