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