| 1 | use std::time::Duration; |
| 2 | |
| 3 | use app_base::format::{self, WeekdayWidth, clock_format, weekday_name}; |
| 4 | use chatsvc::{CalendarEvent, ResponseType, join_info_text}; |
| 5 | use chrono::{DateTime, Datelike, Local, NaiveDate, Offset, Timelike, Utc}; |
| 6 | use gpui_kit::assets::IconName; |
| 7 | use gpui_kit::component::Sizable as _; |
| 8 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 9 | use gpui_kit::component::menu::{ContextMenuExt as _, DropdownMenu as _, PopupMenu, PopupMenuItem}; |
| 10 | use gpui_kit::component::{h_flex, v_flex}; |
| 11 | use gpui_kit::prelude::FluentBuilder as _; |
| 12 | use gpui_kit::*; |
| 13 | |
| 14 | use super::calendar_date_picker::date_picker_button; |
| 15 | use super::calendar_event_dialog::open_calendar_event; |
| 16 | use super::calendar_month::month_grid; |
| 17 | use super::calendar_picker; |
| 18 | use super::join_dialog::open_join_with_id; |
| 19 | use super::meeting_proposal_card::meeting_proposal_cards; |
| 20 | use super::new_meeting_dialog::open_new_meeting; |
| 21 | use super::rsvp_menu::rsvp_menu; |
| 22 | use super::scroll_edge::scroll_offset; |
| 23 | use crate::app_state::AppState; |
| 24 | use crate::call::calendar_now::{ |
| 25 | DEFAULT_GRID_VIEWPORT, FollowNow, MANUAL_SCROLL_EPSILON, NOW_BIAS_FRACTION, hour_height, |
| 26 | now_scroll_offset_y, offset_differs, shows_hour_grid, work_start_scroll_offset_y, |
| 27 | }; |
| 28 | use crate::call::week::CalendarLoadFailure; |
| 29 | use crate::call::week_grid::{ |
| 30 | CalendarSpan, EventLook, WeekBlock, WeekDay, WeekLayout, calendar_index, event_look, |
| 31 | hour_label, month_shown, week_days, |
| 32 | }; |
| 33 | use crate::i18n::tr; |
| 34 | use crate::theme; |
| 35 | use crate::views::widgets::icon; |
| 36 | |
| 37 | const GUTTER_WIDTH: f32 = 50.; |
| 38 | const HEADER_HEIGHT: f32 = 52.; |
| 39 | const DAY_HEADER_HEIGHT: f32 = 56.; |
| 40 | const ALL_DAY_HEIGHT: f32 = 26.; |
| 41 | const BLOCK_GAP: f32 = 2.; |
| 42 | const MINOR_LINE_OPACITY: f32 = 0.45; |
| 43 | const OFF_HOURS_OPACITY: f32 = 0.85; |
| 44 | const NOW_ELSEWHERE_OPACITY: f32 = 0.4; |
| 45 | pub const CALENDAR_HEADER_TAG: &str = "shell/islands/calendar/header"; |
| 46 | pub const CALENDAR_DAY_HEADERS_TAG: &str = "shell/islands/calendar/day-headers"; |
| 47 | pub const CALENDAR_NEW_MEETING_TAG: &str = "shell/islands/calendar/header/new-meeting"; |
| 48 | pub const COMPACT_HEADER_WIDTH: f32 = 960.; |
| 49 | /// Header widths below which button labels give way to icons (German labels are longest). |
| 50 | const SECONDARY_LABELS_WIDTH: f32 = 1080.; |
| 51 | const PRIMARY_LABELS_WIDTH: f32 = 780.; |
| 52 | |
| 53 | /// Which header buttons keep their text at a given header width. |
| 54 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 55 | pub struct HeaderLabels { |
| 56 | /// Today, view and New meeting. |
| 57 | pub primary: bool, |
| 58 | /// Join with an ID and Meet now. |
| 59 | pub secondary: bool, |
| 60 | } |
| 61 | |
| 62 | pub fn header_labels(header_width: f32) -> HeaderLabels { |
| 63 | HeaderLabels { |
| 64 | primary: header_width >= PRIMARY_LABELS_WIDTH, |
| 65 | secondary: header_width >= SECONDARY_LABELS_WIDTH, |
| 66 | } |
| 67 | } |
| 68 | const BUSY_FILL_OPACITY: f32 = 0.28; |
| 69 | const FREE_FILL_OPACITY: f32 = 0.12; |
| 70 | const STATUS_BAR_WIDTH: f32 = 4.; |
| 71 | const HATCH_STRIPE: f32 = 1.5; |
| 72 | const HATCH_GAP: f32 = 2.5; |
| 73 | const SMALL_TITLE_HEIGHT: f32 = 20.; |
| 74 | const TODAY_MARK_HEIGHT: f32 = 3.; |
| 75 | const NOW_DOT_SIZE: f32 = 8.; |
| 76 | const CLOCK_TICK: Duration = Duration::from_secs(30); |
| 77 | const CHROME_HEIGHT: f32 = HEADER_HEIGHT + DAY_HEADER_HEIGHT + ALL_DAY_HEIGHT; |
| 78 | |
| 79 | pub struct CalendarView { |
| 80 | state: Entity<AppState>, |
| 81 | scroll: ScrollHandle, |
| 82 | follow_now: FollowNow, |
| 83 | seen_scroll_to_now: u64, |
| 84 | last_programmatic_offset: f32, |
| 85 | skip_manual_check: bool, |
| 86 | clock_running: bool, |
| 87 | /// Row height of one hour and minor lines per hour, from the time scale. |
| 88 | hour_height: f32, |
| 89 | steps_per_hour: usize, |
| 90 | _observation: Subscription, |
| 91 | _agent_observation: Option<Subscription>, |
| 92 | } |
| 93 | |
| 94 | impl CalendarView { |
| 95 | pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self { |
| 96 | let observation = cx.observe(&state, |_, _, cx| cx.notify()); |
| 97 | let agent_observation = |
| 98 | crate::agent_api::handle(cx).map(|api| cx.observe(&api, |_, _, cx| cx.notify())); |
| 99 | CalendarView { |
| 100 | state, |
| 101 | scroll: ScrollHandle::new(), |
| 102 | follow_now: FollowNow { active: true }, |
| 103 | seen_scroll_to_now: 0, |
| 104 | last_programmatic_offset: 0., |
| 105 | skip_manual_check: false, |
| 106 | clock_running: false, |
| 107 | hour_height: hour_height(Default::default()), |
| 108 | steps_per_hour: 2, |
| 109 | _observation: observation, |
| 110 | _agent_observation: agent_observation, |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn grid_viewport_height(window: &Window) -> f32 { |
| 115 | (f32::from(window.viewport_size().height) - CHROME_HEIGHT).max(DEFAULT_GRID_VIEWPORT * 0.5) |
| 116 | } |
| 117 | |
| 118 | fn align_scroll_to_now(&mut self, window: &Window) { |
| 119 | let local = Local::now(); |
| 120 | let offset = now_scroll_offset_y( |
| 121 | local.hour(), |
| 122 | local.minute(), |
| 123 | self.hour_height, |
| 124 | Self::grid_viewport_height(window), |
| 125 | NOW_BIAS_FRACTION, |
| 126 | ); |
| 127 | self.set_programmatic_offset(offset); |
| 128 | } |
| 129 | |
| 130 | /// A range without today opens at work start instead of midnight. |
| 131 | fn align_scroll_to_work_start(&mut self, work_start_minute: i64, window: &Window) { |
| 132 | let offset = work_start_scroll_offset_y( |
| 133 | work_start_minute, |
| 134 | self.hour_height, |
| 135 | Self::grid_viewport_height(window), |
| 136 | ); |
| 137 | self.set_programmatic_offset(offset); |
| 138 | } |
| 139 | |
| 140 | fn set_programmatic_offset(&mut self, offset: f32) { |
| 141 | self.scroll.set_offset(point(px(0.), px(offset))); |
| 142 | self.last_programmatic_offset = offset; |
| 143 | self.skip_manual_check = true; |
| 144 | } |
| 145 | |
| 146 | fn sync_follow_from_state(&mut self, window: &Window, cx: &mut Context<Self>) { |
| 147 | let (scroll_to_now, span, today_visible, work_start) = { |
| 148 | let state = self.state.read(cx); |
| 149 | let today_visible = state.calendar_shows_today(); |
| 150 | let prefs = &state.calendar_prefs; |
| 151 | self.hour_height = hour_height(prefs.time_scale); |
| 152 | self.steps_per_hour = prefs.time_scale.steps_per_hour() as usize; |
| 153 | ( |
| 154 | state.calendar_scroll_to_now, |
| 155 | state.calendar_week.span, |
| 156 | today_visible, |
| 157 | prefs.work_hours.minute_range().0, |
| 158 | ) |
| 159 | }; |
| 160 | if !shows_hour_grid(span) { |
| 161 | self.stop_clock(); |
| 162 | return; |
| 163 | } |
| 164 | self.ensure_clock(cx); |
| 165 | if scroll_to_now != self.seen_scroll_to_now { |
| 166 | self.seen_scroll_to_now = scroll_to_now; |
| 167 | if today_visible { |
| 168 | self.follow_now.request_align(); |
| 169 | } else { |
| 170 | self.align_scroll_to_work_start(work_start, window); |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | if self.skip_manual_check { |
| 175 | self.skip_manual_check = false; |
| 176 | } else if self.follow_now.active |
| 177 | && offset_differs( |
| 178 | f32::from(scroll_offset(&self.scroll).y), |
| 179 | self.last_programmatic_offset, |
| 180 | MANUAL_SCROLL_EPSILON, |
| 181 | ) |
| 182 | { |
| 183 | self.follow_now.note_manual_scroll(); |
| 184 | } |
| 185 | if self.follow_now.should_follow(today_visible) { |
| 186 | self.align_scroll_to_now(window); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | fn ensure_clock(&mut self, cx: &mut Context<Self>) { |
| 191 | if self.clock_running { |
| 192 | return; |
| 193 | } |
| 194 | self.clock_running = true; |
| 195 | cx.spawn(async move |this, cx| { |
| 196 | loop { |
| 197 | cx.background_executor().timer(CLOCK_TICK).await; |
| 198 | let keep = this |
| 199 | .update(cx, |view, cx| { |
| 200 | let span = view.state.read(cx).calendar_week.span; |
| 201 | if !shows_hour_grid(span) { |
| 202 | view.clock_running = false; |
| 203 | return false; |
| 204 | } |
| 205 | cx.notify(); |
| 206 | true |
| 207 | }) |
| 208 | .unwrap_or(false); |
| 209 | if !keep { |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | }) |
| 214 | .detach(); |
| 215 | } |
| 216 | |
| 217 | fn stop_clock(&mut self) { |
| 218 | self.clock_running = false; |
| 219 | } |
| 220 | |
| 221 | fn on_grid_scroll_wheel( |
| 222 | &mut self, |
| 223 | _event: &ScrollWheelEvent, |
| 224 | _window: &mut Window, |
| 225 | cx: &mut Context<Self>, |
| 226 | ) { |
| 227 | self.follow_now.note_manual_scroll(); |
| 228 | cx.notify(); |
| 229 | } |
| 230 | |
| 231 | fn header( |
| 232 | &self, |
| 233 | start: NaiveDate, |
| 234 | span: CalendarSpan, |
| 235 | dates: &[NaiveDate], |
| 236 | layout: &WeekLayout, |
| 237 | compact: bool, |
| 238 | labels: HeaderLabels, |
| 239 | ) -> impl IntoElement { |
| 240 | let span_state = self.state.clone(); |
| 241 | let label = |key: &'static str| labels.primary.then(|| tr(key)); |
| 242 | let secondary = |key: &'static str| labels.secondary.then(|| tr(key)); |
| 243 | let (today, back, forward) = (self.state.clone(), self.state.clone(), self.state.clone()); |
| 244 | let (join_state, meet_state, new_state) = |
| 245 | (self.state.clone(), self.state.clone(), self.state.clone()); |
| 246 | let pane_state = self.state.clone(); |
| 247 | let (back_tip, forward_tip) = match span { |
| 248 | CalendarSpan::Day => ("calendar.view.previous_day", "calendar.view.next_day"), |
| 249 | CalendarSpan::Month => ("calendar.view.previous_month", "calendar.view.next_month"), |
| 250 | _ => ("calendar.view.previous_week", "calendar.view.next_week"), |
| 251 | }; |
| 252 | h_flex() |
| 253 | .debug_selector(|| CALENDAR_HEADER_TAG.into()) |
| 254 | .h(px(HEADER_HEIGHT)) |
| 255 | .flex_none() |
| 256 | .items_center() |
| 257 | .gap(px(8.)) |
| 258 | .px(px(16.)) |
| 259 | .when(!compact, |header| { |
| 260 | header.child( |
| 261 | Button::new("calendar-pane-toggle") |
| 262 | .small() |
| 263 | .ghost() |
| 264 | .icon(IconName::PanelLeft) |
| 265 | .tooltip(tr("calendar.view.toggle_pane")) |
| 266 | .on_click(move |_, _, cx| { |
| 267 | pane_state.update(cx, |state, cx| { |
| 268 | state.update_calendar_prefs( |
| 269 | |prefs| prefs.pane_hidden = !prefs.pane_hidden, |
| 270 | cx, |
| 271 | ) |
| 272 | }) |
| 273 | }), |
| 274 | ) |
| 275 | }) |
| 276 | .child( |
| 277 | Button::new("calendar-today") |
| 278 | .small() |
| 279 | .outline() |
| 280 | .icon(IconName::Calendar) |
| 281 | .when_some(label("calendar.view.today"), |button, text| { |
| 282 | button.label(text) |
| 283 | }) |
| 284 | .tooltip(tr("calendar.view.today")) |
| 285 | .on_click(move |_, _, cx| { |
| 286 | today.update(cx, |state, cx| state.calendar_to_today(cx)) |
| 287 | }), |
| 288 | ) |
| 289 | .child( |
| 290 | Button::new("calendar-back") |
| 291 | .small() |
| 292 | .ghost() |
| 293 | .icon(IconName::ChevronLeft) |
| 294 | .tooltip(tr(back_tip)) |
| 295 | .on_click(move |_, _, cx| { |
| 296 | back.update(cx, |state, cx| state.shift_calendar_week(-1, cx)) |
| 297 | }), |
| 298 | ) |
| 299 | .child( |
| 300 | Button::new("calendar-forward") |
| 301 | .small() |
| 302 | .ghost() |
| 303 | .icon(IconName::ChevronRight) |
| 304 | .tooltip(tr(forward_tip)) |
| 305 | .on_click(move |_, _, cx| { |
| 306 | forward.update(cx, |state, cx| state.shift_calendar_week(1, cx)) |
| 307 | }), |
| 308 | ) |
| 309 | .child(h_flex().flex_1().min_w_0().child(date_picker_button( |
| 310 | &self.state, |
| 311 | span.title(start, layout), |
| 312 | dates.to_vec(), |
| 313 | layout.first_day, |
| 314 | if span == CalendarSpan::Month { |
| 315 | month_shown(start) |
| 316 | } else { |
| 317 | start |
| 318 | }, |
| 319 | ))) |
| 320 | .child( |
| 321 | Button::new("calendar-span") |
| 322 | .small() |
| 323 | .ghost() |
| 324 | .icon(IconName::LayoutGrid) |
| 325 | .when(labels.primary, |button| button.label(span.label())) |
| 326 | .dropdown_caret(true) |
| 327 | .dropdown_menu(move |menu, _, _| { |
| 328 | CalendarSpan::ALL.iter().fold(menu, |menu, &choice| { |
| 329 | let state = span_state.clone(); |
| 330 | menu.item( |
| 331 | PopupMenuItem::new(choice.label()) |
| 332 | .checked(choice == span) |
| 333 | .on_click(move |_, _, cx| { |
| 334 | state.update(cx, |state, cx| { |
| 335 | state.set_calendar_span(choice, cx) |
| 336 | }) |
| 337 | }), |
| 338 | ) |
| 339 | }) |
| 340 | }), |
| 341 | ) |
| 342 | .child( |
| 343 | Button::new("calendar-join-id") |
| 344 | .small() |
| 345 | .outline() |
| 346 | .icon(IconName::Hash) |
| 347 | .when_some(secondary("calendar.view.join_with_id"), |button, text| { |
| 348 | button.label(text) |
| 349 | }) |
| 350 | .tooltip(tr("calendar.view.join_with_id")) |
| 351 | .on_click(move |_, window, cx| { |
| 352 | open_join_with_id(join_state.clone(), window, cx) |
| 353 | }), |
| 354 | ) |
| 355 | .child( |
| 356 | Button::new("calendar-meet-now") |
| 357 | .small() |
| 358 | .outline() |
| 359 | .icon(IconName::Video) |
| 360 | .when_some(secondary("calendar.view.meet_now"), |button, text| { |
| 361 | button.label(text) |
| 362 | }) |
| 363 | .tooltip(tr("calendar.view.meet_now")) |
| 364 | .on_click(move |_, _, cx| { |
| 365 | meet_state.update(cx, |state, cx| state.start_meet_now(cx)) |
| 366 | }), |
| 367 | ) |
| 368 | .child( |
| 369 | Button::new("calendar-new-meeting") |
| 370 | .debug_selector(|| CALENDAR_NEW_MEETING_TAG.into()) |
| 371 | .small() |
| 372 | .primary() |
| 373 | .icon(IconName::Plus) |
| 374 | .when_some(label("calendar.view.new_meeting"), |button, text| { |
| 375 | button.label(text) |
| 376 | }) |
| 377 | .tooltip(tr("calendar.view.new_meeting")) |
| 378 | .on_click(move |_, window, cx| open_new_meeting(new_state.clone(), window, cx)), |
| 379 | ) |
| 380 | } |
| 381 | |
| 382 | /// Day number above the full weekday name; today in the accent colour with its bar. |
| 383 | fn day_headers(&self, days: &[WeekDay], today: NaiveDate) -> impl IntoElement { |
| 384 | h_flex() |
| 385 | .debug_selector(|| CALENDAR_DAY_HEADERS_TAG.into()) |
| 386 | .h(px(DAY_HEADER_HEIGHT)) |
| 387 | .flex_none() |
| 388 | .border_b_1() |
| 389 | .border_color(theme::border()) |
| 390 | .child(div().w(px(GUTTER_WIDTH)).flex_none()) |
| 391 | .children(days.iter().map(|day| { |
| 392 | let is_today = day.date == today; |
| 393 | let (number_color, name_color) = if is_today { |
| 394 | (theme::accent_text(), theme::accent_text()) |
| 395 | } else { |
| 396 | (theme::text(), theme::text_soft()) |
| 397 | }; |
| 398 | v_flex() |
| 399 | .relative() |
| 400 | .flex_1() |
| 401 | .min_w_0() |
| 402 | .h_full() |
| 403 | .justify_center() |
| 404 | .px(px(12.)) |
| 405 | .border_l_1() |
| 406 | .border_color(theme::border()) |
| 407 | .when(is_today, |header| { |
| 408 | header.child( |
| 409 | div() |
| 410 | .absolute() |
| 411 | .top_0() |
| 412 | .left_0() |
| 413 | .right_0() |
| 414 | .h(px(TODAY_MARK_HEIGHT)) |
| 415 | .bg(theme::accent()), |
| 416 | ) |
| 417 | }) |
| 418 | .child( |
| 419 | div() |
| 420 | .text_size(px(20.)) |
| 421 | .line_height(px(24.)) |
| 422 | .text_color(number_color) |
| 423 | .when(is_today, |number| number.font_weight(FontWeight::SEMIBOLD)) |
| 424 | .child(format::calendar_day_number(day.date)), |
| 425 | ) |
| 426 | .child( |
| 427 | div() |
| 428 | .text_size(px(12.)) |
| 429 | .text_color(name_color) |
| 430 | .truncate() |
| 431 | .child(weekday_name(day.date.weekday(), WeekdayWidth::Full)), |
| 432 | ) |
| 433 | })) |
| 434 | } |
| 435 | |
| 436 | fn all_day_row( |
| 437 | &self, |
| 438 | days: &[WeekDay], |
| 439 | events: &[CalendarEvent], |
| 440 | colors: &[Hsla], |
| 441 | send_response: bool, |
| 442 | ) -> Option<Div> { |
| 443 | if days.iter().all(|day| day.all_day.is_empty()) { |
| 444 | return None; |
| 445 | } |
| 446 | Some( |
| 447 | h_flex() |
| 448 | .flex_none() |
| 449 | .border_b_1() |
| 450 | .border_color(theme::border()) |
| 451 | .child(div().w(px(GUTTER_WIDTH)).flex_none()) |
| 452 | .children(days.iter().map(|day| { |
| 453 | v_flex() |
| 454 | .flex_1() |
| 455 | .min_w_0() |
| 456 | .gap(px(BLOCK_GAP)) |
| 457 | .p(px(BLOCK_GAP)) |
| 458 | .border_l_1() |
| 459 | .border_color(theme::border()) |
| 460 | .children(day.all_day.iter().map(|&index| { |
| 461 | let event = events[index].clone(); |
| 462 | let color = colors[index]; |
| 463 | let open_state = self.state.clone(); |
| 464 | let (menu_state, menu_event) = (self.state.clone(), event.clone()); |
| 465 | // Day-qualified id: multi-day all-day chips share object_id. |
| 466 | div() |
| 467 | .id(SharedString::from(format!( |
| 468 | "calendar-all-day-{}-{}", |
| 469 | event.object_id, day.date |
| 470 | ))) |
| 471 | .h(px(ALL_DAY_HEIGHT - 2. * BLOCK_GAP)) |
| 472 | .px(px(6.)) |
| 473 | .flex() |
| 474 | .items_center() |
| 475 | .rounded(px(6.)) |
| 476 | .bg(event_fill(event_look(&event), color)) |
| 477 | .text_size(px(12.)) |
| 478 | .truncate() |
| 479 | .cursor_pointer() |
| 480 | .child(event_title(&event)) |
| 481 | .on_click(move |_, window, cx| { |
| 482 | open_calendar_event( |
| 483 | open_state.clone(), |
| 484 | event.clone(), |
| 485 | window, |
| 486 | cx, |
| 487 | ); |
| 488 | }) |
| 489 | .context_menu(move |menu, _, _| { |
| 490 | event_menu(menu, &menu_state, &menu_event, send_response) |
| 491 | }) |
| 492 | })) |
| 493 | })), |
| 494 | ) |
| 495 | } |
| 496 | |
| 497 | fn event_block( |
| 498 | &self, |
| 499 | block: &WeekBlock, |
| 500 | event: &CalendarEvent, |
| 501 | color: Hsla, |
| 502 | day: NaiveDate, |
| 503 | send_response: bool, |
| 504 | ) -> impl IntoElement { |
| 505 | let top = block.start_minute as f32 / 60. * self.hour_height; |
| 506 | let height = ((block.end_minute - block.start_minute) as f32 / 60. * self.hour_height |
| 507 | - BLOCK_GAP) |
| 508 | .max(14.); |
| 509 | let width = 1. / block.lanes as f32; |
| 510 | let compact = height < 36.; |
| 511 | let small = height < SMALL_TITLE_HEIGHT; |
| 512 | let title_size = if small { 10. } else { 12. }; |
| 513 | let title_line = if small { 14. } else { 16. }; |
| 514 | let pad_y = if compact { 1. } else { 3. }; |
| 515 | // Title and organizer flow as one text; tall blocks wrap it over more lines. |
| 516 | let lines = (((height - pad_y * 2.) / title_line).floor() as usize).clamp(1, 8); |
| 517 | let look = event_look(event); |
| 518 | // Join on the block whenever a Teams link exists (same as detail dialog). |
| 519 | // Skip on tiny blocks — the button would crush the title. |
| 520 | let can_join = !compact && event.online && !event.cancelled && event.join_url.is_some(); |
| 521 | let open_state = self.state.clone(); |
| 522 | let open_event = event.clone(); |
| 523 | let (menu_state, menu_event) = (self.state.clone(), event.clone()); |
| 524 | let (join_state, join_url, subject) = ( |
| 525 | self.state.clone(), |
| 526 | event.join_url.clone(), |
| 527 | event.subject.clone(), |
| 528 | ); |
| 529 | let (title_color, detail_color) = match look { |
| 530 | EventLook::Busy => (theme::text(), theme::text_soft()), |
| 531 | EventLook::Free | EventLook::Tentative => (theme::text_soft(), theme::text_muted()), |
| 532 | }; |
| 533 | // Day-qualified id: overnight / multi-day spans reuse object_id per column. |
| 534 | div() |
| 535 | .id(SharedString::from(format!( |
| 536 | "calendar-event-{}-{}", |
| 537 | event.object_id, day |
| 538 | ))) |
| 539 | .absolute() |
| 540 | .top(px(top)) |
| 541 | .h(px(height)) |
| 542 | .left(relative(width * block.lane as f32)) |
| 543 | .w(relative(width)) |
| 544 | .px(px(BLOCK_GAP)) |
| 545 | .child( |
| 546 | h_flex() |
| 547 | .size_full() |
| 548 | .items_start() |
| 549 | .overflow_hidden() |
| 550 | .rounded(px(6.)) |
| 551 | .bg(event_fill(look, color)) |
| 552 | .when(event.cancelled, |block| block.opacity(0.6)) |
| 553 | .cursor_pointer() |
| 554 | .children(status_bar(look, color)) |
| 555 | .child( |
| 556 | div() |
| 557 | .flex_1() |
| 558 | .min_w_0() |
| 559 | .pl(px(5.)) |
| 560 | .pr(px(4.)) |
| 561 | .h_full() |
| 562 | .py(px(pad_y)) |
| 563 | .overflow_hidden() |
| 564 | .text_size(px(title_size)) |
| 565 | .line_height(px(title_line)) |
| 566 | .text_color(detail_color) |
| 567 | .line_clamp(lines) |
| 568 | .text_ellipsis() |
| 569 | .child(title_and_organizer(event, title_color)), |
| 570 | ) |
| 571 | .when(event.series, |block| { |
| 572 | block.child(div().flex_none().pt(px(pad_y + 1.)).pr(px(4.)).child(icon( |
| 573 | IconName::Repeat, |
| 574 | 12., |
| 575 | detail_color, |
| 576 | ))) |
| 577 | }) |
| 578 | .when(can_join, |block| { |
| 579 | block.child( |
| 580 | div().flex_none().pr(px(4.)).pt(px(3.)).child( |
| 581 | Button::new(SharedString::from(format!( |
| 582 | "calendar-join-{}-{}", |
| 583 | event.object_id, day |
| 584 | ))) |
| 585 | .xsmall() |
| 586 | .primary() |
| 587 | .label(tr("common.join")) |
| 588 | .on_click(move |_, _, cx| { |
| 589 | cx.stop_propagation(); |
| 590 | join_state.update(cx, |state, cx| { |
| 591 | state.join_calendar_meeting( |
| 592 | join_url.as_deref(), |
| 593 | &subject, |
| 594 | cx, |
| 595 | ) |
| 596 | }); |
| 597 | }), |
| 598 | ), |
| 599 | ) |
| 600 | }), |
| 601 | ) |
| 602 | .on_click(move |_, window, cx| { |
| 603 | open_calendar_event(open_state.clone(), open_event.clone(), window, cx); |
| 604 | }) |
| 605 | .context_menu(move |menu, _, _| { |
| 606 | event_menu(menu, &menu_state, &menu_event, send_response) |
| 607 | }) |
| 608 | } |
| 609 | |
| 610 | #[allow(clippy::too_many_arguments)] |
| 611 | fn grid( |
| 612 | &self, |
| 613 | days: &[WeekDay], |
| 614 | events: &[CalendarEvent], |
| 615 | colors: &[Hsla], |
| 616 | layout: &WeekLayout, |
| 617 | work_minutes: (i64, i64), |
| 618 | today: NaiveDate, |
| 619 | now: DateTime<Utc>, |
| 620 | send_response: bool, |
| 621 | cx: &mut Context<Self>, |
| 622 | ) -> impl IntoElement { |
| 623 | let row = self.hour_height; |
| 624 | let steps = self.steps_per_hour; |
| 625 | let total_height = 24. * row; |
| 626 | let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P"); |
| 627 | let now_minute = { |
| 628 | let local = now.with_timezone(&Local); |
| 629 | local.hour() * 60 + local.minute() |
| 630 | }; |
| 631 | let now_top = now_minute as f32 / 60. * row; |
| 632 | let today_shown = days.iter().any(|day| day.date == today); |
| 633 | let minute_y = |minute: i64| minute as f32 / 60. * row; |
| 634 | div() |
| 635 | .id("calendar-grid") |
| 636 | .flex_1() |
| 637 | .min_h_0() |
| 638 | .overflow_y_scroll() |
| 639 | .track_scroll(&self.scroll) |
| 640 | .on_scroll_wheel(cx.listener(Self::on_grid_scroll_wheel)) |
| 641 | .child( |
| 642 | h_flex() |
| 643 | .h(px(total_height)) |
| 644 | .w_full() |
| 645 | .child( |
| 646 | v_flex() |
| 647 | .w(px(GUTTER_WIDTH)) |
| 648 | .flex_none() |
| 649 | .children((0..24).map(|hour| { |
| 650 | div() |
| 651 | .h(px(row)) |
| 652 | .pt(px(2.)) |
| 653 | .pr(px(8.)) |
| 654 | .flex() |
| 655 | .justify_end() |
| 656 | .text_size(px(11.)) |
| 657 | .text_color(theme::text_muted()) |
| 658 | .when(hour > 0, |label| { |
| 659 | label.child(hour_label(hour, twelve_hour)) |
| 660 | }) |
| 661 | })), |
| 662 | ) |
| 663 | .children(days.iter().map(|day| { |
| 664 | let work_day = layout.is_work_day(day.date.weekday()); |
| 665 | let (work_start, work_end) = work_minutes; |
| 666 | let shade = |top: f32, height: f32| { |
| 667 | div() |
| 668 | .absolute() |
| 669 | .top(px(top)) |
| 670 | .left_0() |
| 671 | .right_0() |
| 672 | .h(px(height)) |
| 673 | .bg(theme::background().opacity(OFF_HOURS_OPACITY)) |
| 674 | }; |
| 675 | let shading = if work_day { |
| 676 | vec![ |
| 677 | shade(0., minute_y(work_start)), |
| 678 | shade(minute_y(work_end), total_height - minute_y(work_end)), |
| 679 | ] |
| 680 | } else { |
| 681 | vec![shade(0., total_height)] |
| 682 | }; |
| 683 | div() |
| 684 | .relative() |
| 685 | .flex_1() |
| 686 | .min_w_0() |
| 687 | .h_full() |
| 688 | .border_l_1() |
| 689 | .border_color(theme::border()) |
| 690 | .children(shading) |
| 691 | .children((1..24 * steps).map(|step| { |
| 692 | let line = div() |
| 693 | .absolute() |
| 694 | .top(px(step as f32 * row / steps as f32)) |
| 695 | .left_0() |
| 696 | .right_0() |
| 697 | .h(px(1.)); |
| 698 | if step % steps == 0 { |
| 699 | line.bg(theme::border()) |
| 700 | } else { |
| 701 | line.bg(theme::border().opacity(MINOR_LINE_OPACITY)) |
| 702 | } |
| 703 | })) |
| 704 | .children(day.blocks.iter().map(|block| { |
| 705 | self.event_block( |
| 706 | block, |
| 707 | &events[block.event_index], |
| 708 | colors[block.event_index], |
| 709 | day.date, |
| 710 | send_response, |
| 711 | ) |
| 712 | })) |
| 713 | .when(today_shown && day.date != today, |column| { |
| 714 | column.child( |
| 715 | div() |
| 716 | .absolute() |
| 717 | .top(px(now_top)) |
| 718 | .left_0() |
| 719 | .right_0() |
| 720 | .h(px(1.)) |
| 721 | .bg(theme::accent().opacity(NOW_ELSEWHERE_OPACITY)), |
| 722 | ) |
| 723 | }) |
| 724 | .when(day.date == today, |column| { |
| 725 | column |
| 726 | .child( |
| 727 | div() |
| 728 | .absolute() |
| 729 | .top(px(now_top - 1.)) |
| 730 | .left_0() |
| 731 | .right_0() |
| 732 | .h(px(2.)) |
| 733 | .bg(theme::accent()), |
| 734 | ) |
| 735 | .child( |
| 736 | div() |
| 737 | .absolute() |
| 738 | .top(px(now_top - NOW_DOT_SIZE / 2.)) |
| 739 | .left_0() |
| 740 | .size(px(NOW_DOT_SIZE)) |
| 741 | .rounded_full() |
| 742 | .bg(theme::accent()), |
| 743 | ) |
| 744 | }) |
| 745 | })), |
| 746 | ) |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /// Loading line and one notice per failed source, under the calendar header. |
| 751 | fn load_notices( |
| 752 | state: &Entity<AppState>, |
| 753 | failures: &[CalendarLoadFailure], |
| 754 | loading: bool, |
| 755 | ) -> Vec<Div> { |
| 756 | let mut rows = Vec::new(); |
| 757 | if loading { |
| 758 | rows.push( |
| 759 | h_flex() |
| 760 | .flex_none() |
| 761 | .px(px(16.)) |
| 762 | .pb(px(6.)) |
| 763 | .text_size(px(12.)) |
| 764 | .text_color(theme::text_muted()) |
| 765 | .child(tr("calendar.view.loading")), |
| 766 | ); |
| 767 | } |
| 768 | for failure in failures { |
| 769 | let action = state.clone(); |
| 770 | let sign_in = failure.sign_in; |
| 771 | let mut args = std::collections::HashMap::new(); |
| 772 | args.insert("source".to_owned(), failure.source.label().to_owned()); |
| 773 | let label = if sign_in { |
| 774 | tr("auth.sign_in") |
| 775 | } else { |
| 776 | tr("common.retry") |
| 777 | }; |
| 778 | rows.push( |
| 779 | h_flex() |
| 780 | .flex_none() |
| 781 | .mx(px(16.)) |
| 782 | .mb(px(6.)) |
| 783 | .px(px(12.)) |
| 784 | .py(px(6.)) |
| 785 | .gap(px(12.)) |
| 786 | .items_center() |
| 787 | .rounded(px(6.)) |
| 788 | .bg(theme::red_tint()) |
| 789 | .text_size(px(13.)) |
| 790 | .text_color(theme::text()) |
| 791 | .child( |
| 792 | div() |
| 793 | .flex_1() |
| 794 | .min_w_0() |
| 795 | .truncate() |
| 796 | .child(crate::i18n::tr_args("calendar.view.source_failed", &args)), |
| 797 | ) |
| 798 | .child( |
| 799 | Button::new(SharedString::from(format!( |
| 800 | "calendar-retry-{}", |
| 801 | failure.source.label() |
| 802 | ))) |
| 803 | .xsmall() |
| 804 | .outline() |
| 805 | .label(label) |
| 806 | .tooltip(failure.message.clone()) |
| 807 | .on_click(move |_, _, cx| { |
| 808 | action.update(cx, |state, cx| { |
| 809 | if sign_in { |
| 810 | state.connect_google_calendar(cx); |
| 811 | } else { |
| 812 | state.retry_calendar_week(cx); |
| 813 | } |
| 814 | }) |
| 815 | }), |
| 816 | ), |
| 817 | ); |
| 818 | } |
| 819 | rows |
| 820 | } |
| 821 | |
| 822 | fn event_menu( |
| 823 | menu: PopupMenu, |
| 824 | state: &Entity<AppState>, |
| 825 | event: &CalendarEvent, |
| 826 | send_response: bool, |
| 827 | ) -> PopupMenu { |
| 828 | let mut menu = menu; |
| 829 | if let Some(join_url) = event |
| 830 | .join_url |
| 831 | .clone() |
| 832 | .filter(|_| event.online && !event.cancelled) |
| 833 | { |
| 834 | let (join_state, subject) = (state.clone(), event.subject.clone()); |
| 835 | menu = menu.item( |
| 836 | PopupMenuItem::new(tr("common.join")) |
| 837 | .icon(IconName::Video) |
| 838 | .on_click(move |_, _, cx| { |
| 839 | join_state.update(cx, |state, cx| { |
| 840 | state.join_calendar_meeting(Some(&join_url), &subject, cx) |
| 841 | }) |
| 842 | }), |
| 843 | ); |
| 844 | } |
| 845 | if let Some(join_url) = event.join_url.as_deref().filter(|_| event.online) { |
| 846 | let info = join_info_text(join_url, event.dial_in.as_ref()); |
| 847 | menu = menu.item( |
| 848 | PopupMenuItem::new(tr("calendar.view.copy_join_info")) |
| 849 | .icon(IconName::Copy) |
| 850 | .on_click(move |_, _, cx| { |
| 851 | cx.write_to_clipboard(ClipboardItem::new_string(info.clone())) |
| 852 | }), |
| 853 | ); |
| 854 | } |
| 855 | if let Some(thread_id) = event.thread_id.clone() { |
| 856 | let chat_state = state.clone(); |
| 857 | menu = menu.item( |
| 858 | PopupMenuItem::new(tr("calendar.view.chat_with_participants")) |
| 859 | .icon(IconName::MessageSquare) |
| 860 | .on_click(move |_, _, cx| { |
| 861 | chat_state.update(cx, |state, cx| { |
| 862 | state.open_meeting_chat(Some(&thread_id), cx) |
| 863 | }) |
| 864 | }), |
| 865 | ); |
| 866 | } |
| 867 | if event.response == ResponseType::Organizer || event.organizer_name.is_none() { |
| 868 | return menu; |
| 869 | } |
| 870 | rsvp_menu( |
| 871 | menu.separator(), |
| 872 | state, |
| 873 | &event.object_id, |
| 874 | event.response, |
| 875 | send_response, |
| 876 | ) |
| 877 | } |
| 878 | |
| 879 | fn event_fill(look: EventLook, color: Hsla) -> Hsla { |
| 880 | match look { |
| 881 | EventLook::Busy => color.opacity(BUSY_FILL_OPACITY), |
| 882 | EventLook::Free | EventLook::Tentative => color.opacity(FREE_FILL_OPACITY), |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | fn status_bar(look: EventLook, color: Hsla) -> Option<Div> { |
| 887 | let bar = div().flex_none().w(px(STATUS_BAR_WIDTH)).h_full(); |
| 888 | match look { |
| 889 | EventLook::Busy => Some(bar.bg(color)), |
| 890 | EventLook::Tentative => Some( |
| 891 | bar.bg(color.opacity(BUSY_FILL_OPACITY)) |
| 892 | .child( |
| 893 | div() |
| 894 | .size_full() |
| 895 | .bg(pattern_slash(color, HATCH_STRIPE, HATCH_GAP)), |
| 896 | ), |
| 897 | ), |
| 898 | EventLook::Free => None, |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | /// "Daily Standup Zulal Fouque": bold title, organizer right after it on the same line. |
| 903 | fn title_and_organizer(event: &CalendarEvent, title_color: Hsla) -> StyledText { |
| 904 | let title = event_title(event); |
| 905 | let text = match event.organizer_name.as_deref() { |
| 906 | Some(organizer) if !organizer.is_empty() => format!("{title} {organizer}"), |
| 907 | _ => title.clone(), |
| 908 | }; |
| 909 | StyledText::new(text).with_highlights([( |
| 910 | 0..title.len(), |
| 911 | HighlightStyle { |
| 912 | color: Some(title_color), |
| 913 | font_weight: Some(FontWeight::SEMIBOLD), |
| 914 | ..Default::default() |
| 915 | }, |
| 916 | )]) |
| 917 | } |
| 918 | |
| 919 | fn event_title(event: &CalendarEvent) -> String { |
| 920 | let subject = if event.cancelled { |
| 921 | format!("{}{}", tr("calendar.peek.canceled_prefix"), event.subject) |
| 922 | } else { |
| 923 | event.subject.clone() |
| 924 | }; |
| 925 | let label = source_label(event); |
| 926 | if label.is_empty() { |
| 927 | subject |
| 928 | } else { |
| 929 | format!("{subject} · {label}") |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | fn source_label(event: &CalendarEvent) -> String { |
| 934 | let mut sources = event.sources.clone(); |
| 935 | if sources.is_empty() { |
| 936 | sources.push(event.source); |
| 937 | } |
| 938 | sources.sort_by_key(|source| source.label()); |
| 939 | sources.dedup(); |
| 940 | if sources.len() == 1 && sources[0] == chatsvc::CalendarSource::Outlook { |
| 941 | return String::new(); |
| 942 | } |
| 943 | sources |
| 944 | .iter() |
| 945 | .map(|source| source.label()) |
| 946 | .collect::<Vec<_>>() |
| 947 | .join("+") |
| 948 | } |
| 949 | |
| 950 | impl Render for CalendarView { |
| 951 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 952 | let window_width = f32::from(window.viewport_size().width); |
| 953 | let compact = window_width < COMPACT_HEADER_WIDTH; |
| 954 | self.sync_follow_from_state(window, cx); |
| 955 | let state = self.state.read(cx); |
| 956 | let start = state.calendar_start(); |
| 957 | let events = state.calendar_week.events.clone(); |
| 958 | let send_response = state.rsvp_send_response; |
| 959 | let failures = state.calendar_week.failures.clone(); |
| 960 | let loading = state.calendar_week.loading && events.is_empty(); |
| 961 | let offset = Local::now().offset().fix(); |
| 962 | let span = state.calendar_week.span; |
| 963 | let layout = state.calendar_layout(); |
| 964 | let dates = state.calendar_dates(); |
| 965 | let days = week_days(&events, &dates, offset); |
| 966 | let today = Local::now().date_naive(); |
| 967 | let now = Utc::now(); |
| 968 | let accounts = state.calendar_accounts(); |
| 969 | let colors: Vec<Hsla> = events |
| 970 | .iter() |
| 971 | .map(|event| theme::calendar_color(calendar_index(event, &accounts))) |
| 972 | .collect(); |
| 973 | let work_minutes = state.calendar_prefs.work_hours.minute_range(); |
| 974 | let show_picker = !compact && !state.calendar_prefs.pane_hidden; |
| 975 | // Width left for the header: window minus rail, island gaps and the pane. |
| 976 | let header_width = window_width |
| 977 | - app_ui_kit::layout::RAIL_WIDTH |
| 978 | - 2. * app_ui_kit::layout::ISLAND_GAP |
| 979 | - if show_picker { |
| 980 | calendar_picker::PICKER_WIDTH |
| 981 | } else { |
| 982 | 0. |
| 983 | }; |
| 984 | let labels = header_labels(header_width); |
| 985 | h_flex() |
| 986 | .debug_selector(|| "shell/islands/calendar".into()) |
| 987 | .flex_1() |
| 988 | .w_full() |
| 989 | .min_w_0() |
| 990 | .h_full() |
| 991 | .rounded(px(10.)) |
| 992 | .bg(theme::surface()) |
| 993 | .overflow_hidden() |
| 994 | .when(show_picker, |row| { |
| 995 | row.child(calendar_picker::render(&self.state, cx)) |
| 996 | }) |
| 997 | .child( |
| 998 | v_flex() |
| 999 | .flex_1() |
| 1000 | .min_w_0() |
| 1001 | .h_full() |
| 1002 | .child(self.header(start, span, &dates, &layout, compact, labels)) |
| 1003 | .children(load_notices(&self.state, &failures, loading)) |
| 1004 | .children(meeting_proposal_cards(cx)) |
| 1005 | .map(|calendar| { |
| 1006 | if span == CalendarSpan::Month { |
| 1007 | calendar.child(month_grid( |
| 1008 | &self.state, |
| 1009 | start, |
| 1010 | &days, |
| 1011 | &events, |
| 1012 | &colors, |
| 1013 | today, |
| 1014 | offset, |
| 1015 | layout.weekdays(), |
| 1016 | )) |
| 1017 | } else { |
| 1018 | calendar |
| 1019 | .child(self.day_headers(&days, today)) |
| 1020 | .children(self.all_day_row(&days, &events, &colors, send_response)) |
| 1021 | .child(self.grid( |
| 1022 | &days, |
| 1023 | &events, |
| 1024 | &colors, |
| 1025 | &layout, |
| 1026 | work_minutes, |
| 1027 | today, |
| 1028 | now, |
| 1029 | send_response, |
| 1030 | cx, |
| 1031 | )) |
| 1032 | } |
| 1033 | }), |
| 1034 | ) |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | #[cfg(test)] |
| 1039 | mod tests { |
| 1040 | use super::header_labels; |
| 1041 | |
| 1042 | #[test] |
| 1043 | fn header_buttons_drop_their_text_as_the_header_narrows() { |
| 1044 | let wide = header_labels(1300.); |
| 1045 | assert!(wide.primary && wide.secondary); |
| 1046 | let middle = header_labels(900.); |
| 1047 | assert!(middle.primary && !middle.secondary); |
| 1048 | let narrow = header_labels(600.); |
| 1049 | assert!(!narrow.primary && !narrow.secondary); |
| 1050 | } |
| 1051 | } |