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