BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / calendar_view.rs

calendar_view.rs

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