BlueGit

ict rusty-teams

Files

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

calendar_view.rs

calendar_view.rs 23.9 KiB Download Raw
1use app_base::format::clock_format;
2use chatsvc::{CalendarEvent, ResponseType, join_info_text};
3use chrono::{DateTime, Datelike, Local, NaiveDate, Offset, Timelike, Utc};
4use gpui_kit::assets::IconName;
5use gpui_kit::component::Sizable as _;
6use gpui_kit::component::button::{Button, ButtonVariants as _};
7use gpui_kit::component::menu::{ContextMenuExt as _, DropdownMenu as _, PopupMenu, PopupMenuItem};
8use gpui_kit::component::{h_flex, v_flex};
9use gpui_kit::prelude::FluentBuilder as _;
10use gpui_kit::*;
11
12use super::calendar_date_picker::date_picker_button;
13use super::calendar_month::month_grid;
14use super::rsvp_menu::rsvp_menu;
15use super::join_dialog::open_join_with_id;
16use super::new_meeting_dialog::open_new_meeting;
17use crate::app_state::AppState;
18use crate::call::join_available;
19use crate::call::week_grid::{
20 CalendarSpan, EventLook, WeekBlock, WeekDay, event_look, hour_label, month_shown, week_days,
21};
22use crate::theme;
23
24const HOUR_HEIGHT: f32 = 120.;
25const QUARTERS_PER_HOUR: usize = 4;
26const QUARTER_LINE_OPACITY: f32 = 0.45;
27const GUTTER_WIDTH: f32 = 50.;
28const HEADER_HEIGHT: f32 = 52.;
29const DAY_HEADER_HEIGHT: f32 = 56.;
30const ALL_DAY_HEIGHT: f32 = 26.;
31const BLOCK_GAP: f32 = 2.;
32const FIRST_VISIBLE_HOUR: f32 = 8.;
33const CANCELED_PREFIX: &str = "Canceled: ";
34const COPY_JOIN_INFO: &str = "Copy join info";
35const COMPACT_HEADER_WIDTH: f32 = 960.;
36const BUSY_FILL_OPACITY: f32 = 0.28;
37const FREE_FILL_OPACITY: f32 = 0.12;
38const STATUS_BAR_WIDTH: f32 = 4.;
39const HATCH_STRIPE: f32 = 1.5;
40const HATCH_GAP: f32 = 2.5;
41const SMALL_TITLE_HEIGHT: f32 = 20.;
42const TODAY_MARK_HEIGHT: f32 = 3.;
43const NOW_DOT_SIZE: f32 = 8.;
44
45pub struct CalendarView {
46 state: Entity<AppState>,
47 scroll: ScrollHandle,
48 scrolled_to_morning: bool,
49 _observation: Subscription,
50}
51
52impl CalendarView {
53 pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self {
54 let observation = cx.observe(&state, |_, _, cx| cx.notify());
55 CalendarView {
56 state,
57 scroll: ScrollHandle::new(),
58 scrolled_to_morning: false,
59 _observation: observation,
60 }
61 }
62
63 fn header(&self, start: NaiveDate, span: CalendarSpan, compact: bool) -> impl IntoElement {
64 let span_state = self.state.clone();
65 let label = |text: &'static str| (!compact).then_some(text);
66 let (today, back, forward) = (self.state.clone(), self.state.clone(), self.state.clone());
67 let (join_state, meet_state, new_state) =
68 (self.state.clone(), self.state.clone(), self.state.clone());
69 h_flex()
70 .h(px(HEADER_HEIGHT))
71 .flex_none()
72 .items_center()
73 .gap(px(8.))
74 .px(px(16.))
75 .child(
76 div()
77 .flex_none()
78 .text_size(px(16.))
79 .font_weight(FontWeight::BOLD)
80 .mr(px(8.))
81 .child("Calendar"),
82 )
83 .child(
84 Button::new("calendar-today")
85 .small()
86 .outline()
87 .icon(IconName::Calendar)
88 .when_some(label("Today"), |button, text| button.label(text))
89 .tooltip("Today")
90 .on_click(move |_, _, cx| {
91 today.update(cx, |state, cx| state.calendar_to_today(cx))
92 }),
93 )
94 .child(
95 Button::new("calendar-back")
96 .small()
97 .ghost()
98 .icon(IconName::ChevronLeft)
99 .tooltip(match span {
100 CalendarSpan::Day => "Go to previous day",
101 CalendarSpan::Month => "Go to previous month",
102 _ => "Go to previous week",
103 })
104 .on_click(move |_, _, cx| {
105 back.update(cx, |state, cx| state.shift_calendar_week(-1, cx))
106 }),
107 )
108 .child(
109 Button::new("calendar-forward")
110 .small()
111 .ghost()
112 .icon(IconName::ChevronRight)
113 .tooltip(match span {
114 CalendarSpan::Day => "Go to next day",
115 CalendarSpan::Month => "Go to next month",
116 _ => "Go to next week",
117 })
118 .on_click(move |_, _, cx| {
119 forward.update(cx, |state, cx| state.shift_calendar_week(1, cx))
120 }),
121 )
122 .child(
123 h_flex().flex_1().min_w_0().child(date_picker_button(
124 &self.state,
125 span.title(start),
126 start,
127 span.days(),
128 if span == CalendarSpan::Month {
129 month_shown(start)
130 } else {
131 start
132 },
133 )),
134 )
135 .child(
136 Button::new("calendar-span")
137 .small()
138 .ghost()
139 .label(span.label())
140 .dropdown_caret(true)
141 .dropdown_menu(move |menu, _, _| {
142 CalendarSpan::ALL.iter().fold(menu, |menu, &choice| {
143 let state = span_state.clone();
144 menu.item(
145 PopupMenuItem::new(choice.label())
146 .checked(choice == span)
147 .on_click(move |_, _, cx| {
148 state.update(cx, |state, cx| {
149 state.set_calendar_span(choice, cx)
150 })
151 }),
152 )
153 })
154 }),
155 )
156 .child(
157 Button::new("calendar-join-id")
158 .small()
159 .outline()
160 .icon(IconName::Hash)
161 .when_some(label("Join with an ID"), |button, text| button.label(text))
162 .tooltip("Join with an ID")
163 .on_click(move |_, window, cx| {
164 open_join_with_id(join_state.clone(), window, cx)
165 }),
166 )
167 .child(
168 Button::new("calendar-meet-now")
169 .small()
170 .outline()
171 .icon(IconName::Video)
172 .when_some(label("Meet now"), |button, text| button.label(text))
173 .tooltip("Meet now")
174 .on_click(move |_, _, cx| {
175 meet_state.update(cx, |state, cx| state.start_meet_now(cx))
176 }),
177 )
178 .child(
179 Button::new("calendar-new-meeting")
180 .small()
181 .primary()
182 .icon(IconName::Plus)
183 .when_some(label("New meeting"), |button, text| button.label(text))
184 .tooltip("New meeting")
185 .on_click(move |_, window, cx| open_new_meeting(new_state.clone(), window, cx)),
186 )
187 }
188
189 fn day_headers(&self, days: &[WeekDay], today: NaiveDate) -> impl IntoElement {
190 h_flex()
191 .h(px(DAY_HEADER_HEIGHT))
192 .flex_none()
193 .border_b_1()
194 .border_color(theme::border())
195 .child(div().w(px(GUTTER_WIDTH)).flex_none())
196 .children(days.iter().map(|day| {
197 let is_today = day.date == today;
198 let color = if is_today {
199 theme::accent_text()
200 } else {
201 theme::text_soft()
202 };
203 v_flex()
204 .relative()
205 .flex_1()
206 .min_w_0()
207 .h_full()
208 .justify_center()
209 .px(px(12.))
210 .border_l_1()
211 .border_color(theme::border())
212 .when(is_today, |header| {
213 header.child(
214 div()
215 .absolute()
216 .top_0()
217 .left_0()
218 .right_0()
219 .h(px(TODAY_MARK_HEIGHT))
220 .bg(theme::accent()),
221 )
222 })
223 .child(
224 div()
225 .text_size(px(20.))
226 .line_height(px(24.))
227 .text_color(color)
228 .child(day.date.day().to_string()),
229 )
230 .child(
231 div()
232 .text_size(px(12.))
233 .text_color(color)
234 .child(day.date.format("%a").to_string()),
235 )
236 }))
237 }
238
239 fn all_day_row(&self, days: &[WeekDay], events: &[CalendarEvent]) -> Option<Div> {
240 if days.iter().all(|day| day.all_day.is_empty()) {
241 return None;
242 }
243 Some(
244 h_flex()
245 .flex_none()
246 .border_b_1()
247 .border_color(theme::border())
248 .child(div().w(px(GUTTER_WIDTH)).flex_none())
249 .children(days.iter().map(|day| {
250 v_flex()
251 .flex_1()
252 .min_w_0()
253 .gap(px(BLOCK_GAP))
254 .p(px(BLOCK_GAP))
255 .border_l_1()
256 .border_color(theme::border())
257 .children(day.all_day.iter().map(|&index| {
258 div()
259 .h(px(ALL_DAY_HEIGHT - 2. * BLOCK_GAP))
260 .px(px(6.))
261 .flex()
262 .items_center()
263 .rounded(px(4.))
264 .bg(event_fill(event_look(&events[index])))
265 .text_size(px(12.))
266 .truncate()
267 .child(event_title(&events[index]))
268 }))
269 })),
270 )
271 }
272
273 fn event_block(
274 &self,
275 block: &WeekBlock,
276 event: &CalendarEvent,
277 now: DateTime<Utc>,
278 send_response: bool,
279 ) -> impl IntoElement {
280 let top = block.start_minute as f32 / 60. * HOUR_HEIGHT;
281 let height = ((block.end_minute - block.start_minute) as f32 / 60. * HOUR_HEIGHT
282 - BLOCK_GAP)
283 .max(14.);
284 let width = 1. / block.lanes as f32;
285 let compact = height < 36.;
286 let small = height < SMALL_TITLE_HEIGHT;
287 let look = event_look(event);
288 let can_join = event.online
289 && !event.cancelled
290 && event.join_url.is_some()
291 && join_available(event.start, event.end, now);
292 let (open_state, thread_id) = (self.state.clone(), event.thread_id.clone());
293 let (menu_state, menu_event) = (self.state.clone(), event.clone());
294 let (join_state, join_url, subject) = (
295 self.state.clone(),
296 event.join_url.clone(),
297 event.subject.clone(),
298 );
299 let (title_color, detail_color) = match look {
300 EventLook::Busy => (theme::text(), theme::text_soft()),
301 EventLook::Free | EventLook::Tentative => (theme::text_soft(), theme::text_muted()),
302 };
303 div()
304 .id(SharedString::from(format!(
305 "calendar-event-{}",
306 event.object_id
307 )))
308 .absolute()
309 .top(px(top))
310 .h(px(height))
311 .left(relative(width * block.lane as f32))
312 .w(relative(width))
313 .px(px(BLOCK_GAP))
314 .child(
315 h_flex()
316 .size_full()
317 .items_start()
318 .overflow_hidden()
319 .rounded(px(4.))
320 .bg(event_fill(look))
321 .when(event.cancelled, |block| block.opacity(0.6))
322 .cursor_pointer()
323 .children(status_bar(look))
324 .child(
325 v_flex()
326 .flex_1()
327 .min_w_0()
328 .pl(px(5.))
329 .pr(px(4.))
330 .h_full()
331 .py(px(if compact { 1. } else { 4. }))
332 .when(compact, |body| body.justify_center())
333 .child(
334 div()
335 .text_size(px(if small { 10. } else { 12. }))
336 .line_height(px(if small { 13. } else { 16. }))
337 .font_weight(FontWeight::SEMIBOLD)
338 .text_color(title_color)
339 .when(compact, |title| title.truncate())
340 .when(!compact, |title| title.line_clamp(2))
341 .child(event_title(event)),
342 )
343 .when(!compact, |body| {
344 body.children(event.organizer_name.clone().map(|organizer| {
345 div()
346 .text_size(px(12.))
347 .line_height(px(16.))
348 .text_color(detail_color)
349 .truncate()
350 .child(organizer)
351 }))
352 }),
353 )
354 .when(can_join, |block| {
355 block.child(
356 div().flex_none().pr(px(4.)).pt(px(3.)).h_full().child(
357 Button::new(SharedString::from(format!(
358 "calendar-join-{}",
359 event.object_id
360 )))
361 .xsmall()
362 .primary()
363 .label("Join")
364 .on_click(move |_, _, cx| {
365 cx.stop_propagation();
366 join_state.update(cx, |state, cx| {
367 state.join_calendar_meeting(
368 join_url.as_deref(),
369 &subject,
370 cx,
371 )
372 });
373 }),
374 ),
375 )
376 }),
377 )
378 .on_click(move |_, _, cx| {
379 open_state.update(cx, |state, cx| {
380 state.open_meeting_chat(thread_id.as_deref(), cx)
381 });
382 })
383 .context_menu(move |menu, _, _| {
384 event_menu(menu, &menu_state, &menu_event, send_response)
385 })
386 }
387
388 fn grid(
389 &self,
390 days: &[WeekDay],
391 events: &[CalendarEvent],
392 today: NaiveDate,
393 now: DateTime<Utc>,
394 send_response: bool,
395 ) -> impl IntoElement {
396 let total_height = 24. * HOUR_HEIGHT;
397 let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P");
398 let now_minute = {
399 let local = now.with_timezone(&Local);
400 local.hour() * 60 + local.minute()
401 };
402 div()
403 .id("calendar-grid")
404 .flex_1()
405 .min_h_0()
406 .overflow_y_scroll()
407 .track_scroll(&self.scroll)
408 .child(
409 h_flex()
410 .h(px(total_height))
411 .w_full()
412 .child(
413 v_flex()
414 .w(px(GUTTER_WIDTH))
415 .flex_none()
416 .children((0..24).map(|hour| {
417 div()
418 .h(px(HOUR_HEIGHT))
419 .pt(px(4.))
420 .flex()
421 .justify_center()
422 .text_size(px(12.))
423 .text_color(theme::text_soft())
424 .when(hour > 0, |label| {
425 label.child(hour_label(hour, twelve_hour))
426 })
427 })),
428 )
429 .children(days.iter().map(|day| {
430 div()
431 .relative()
432 .flex_1()
433 .min_w_0()
434 .h_full()
435 .border_l_1()
436 .border_color(theme::border())
437 .children((1..24 * QUARTERS_PER_HOUR).map(|quarter| {
438 let line = div()
439 .absolute()
440 .top(
441 px(quarter as f32 * HOUR_HEIGHT / QUARTERS_PER_HOUR as f32),
442 )
443 .left_0()
444 .right_0();
445 if quarter % QUARTERS_PER_HOUR == 0 {
446 line.h(px(1.)).bg(theme::border())
447 } else {
448 line.h(px(1.))
449 .bg(theme::border().opacity(QUARTER_LINE_OPACITY))
450 }
451 }))
452 .children(day.blocks.iter().map(|block| {
453 self.event_block(
454 block,
455 &events[block.event_index],
456 now,
457 send_response,
458 )
459 }))
460 .when(day.date == today, |column| {
461 let now_top = now_minute as f32 / 60. * HOUR_HEIGHT;
462 column
463 .child(
464 div()
465 .absolute()
466 .top(px(now_top - 1.))
467 .left_0()
468 .right_0()
469 .h(px(2.))
470 .bg(theme::accent()),
471 )
472 .child(
473 div()
474 .absolute()
475 .top(px(now_top - NOW_DOT_SIZE / 2.))
476 .left_0()
477 .size(px(NOW_DOT_SIZE))
478 .rounded_full()
479 .bg(theme::accent()),
480 )
481 })
482 })),
483 )
484 }
485}
486
487fn event_menu(
488 menu: PopupMenu,
489 state: &Entity<AppState>,
490 event: &CalendarEvent,
491 send_response: bool,
492) -> PopupMenu {
493 let mut menu = menu;
494 if let Some(join_url) = event
495 .join_url
496 .clone()
497 .filter(|_| event.online && !event.cancelled)
498 {
499 let (join_state, subject) = (state.clone(), event.subject.clone());
500 menu = menu.item(PopupMenuItem::new("Join").icon(IconName::Video).on_click(
501 move |_, _, cx| {
502 join_state.update(cx, |state, cx| {
503 state.join_calendar_meeting(Some(&join_url), &subject, cx)
504 })
505 },
506 ));
507 }
508 if let Some(join_url) = event.join_url.as_deref().filter(|_| event.online) {
509 let info = join_info_text(join_url, event.dial_in.as_ref());
510 menu = menu.item(
511 PopupMenuItem::new(COPY_JOIN_INFO)
512 .icon(IconName::Copy)
513 .on_click(move |_, _, cx| {
514 cx.write_to_clipboard(ClipboardItem::new_string(info.clone()))
515 }),
516 );
517 }
518 if let Some(thread_id) = event.thread_id.clone() {
519 let chat_state = state.clone();
520 menu = menu.item(
521 PopupMenuItem::new("Chat with participants")
522 .icon(IconName::MessageSquare)
523 .on_click(move |_, _, cx| {
524 chat_state.update(cx, |state, cx| {
525 state.open_meeting_chat(Some(&thread_id), cx)
526 })
527 }),
528 );
529 }
530 if event.response == ResponseType::Organizer || event.organizer_name.is_none() {
531 return menu;
532 }
533 rsvp_menu(
534 menu.separator(),
535 state,
536 &event.object_id,
537 event.response,
538 send_response,
539 )
540}
541
542fn event_fill(look: EventLook) -> Hsla {
543 match look {
544 EventLook::Busy => theme::accent().opacity(BUSY_FILL_OPACITY),
545 EventLook::Free | EventLook::Tentative => theme::accent().opacity(FREE_FILL_OPACITY),
546 }
547}
548
549fn status_bar(look: EventLook) -> Option<Div> {
550 let bar = div().flex_none().w(px(STATUS_BAR_WIDTH)).h_full();
551 match look {
552 EventLook::Busy => Some(bar.bg(theme::accent())),
553 EventLook::Tentative => Some(
554 bar.bg(theme::accent().opacity(BUSY_FILL_OPACITY)).child(
555 div()
556 .size_full()
557 .bg(pattern_slash(theme::accent(), HATCH_STRIPE, HATCH_GAP)),
558 ),
559 ),
560 EventLook::Free => None,
561 }
562}
563
564fn event_title(event: &CalendarEvent) -> String {
565 if event.cancelled {
566 format!("{CANCELED_PREFIX}{}", event.subject)
567 } else {
568 event.subject.clone()
569 }
570}
571
572impl Render for CalendarView {
573 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
574 let compact = f32::from(window.viewport_size().width) < COMPACT_HEADER_WIDTH;
575 if !self.scrolled_to_morning {
576 self.scrolled_to_morning = true;
577 self.scroll
578 .set_offset(point(px(0.), px(-FIRST_VISIBLE_HOUR * HOUR_HEIGHT)));
579 }
580 let state = self.state.read(cx);
581 let start = state.calendar_start();
582 let events = state.calendar_week.events.clone();
583 let send_response = state.rsvp_send_response;
584 let offset = Local::now().offset().fix();
585 let span = state.calendar_week.span;
586 let days = week_days(&events, start, span.days(), offset);
587 let today = Local::now().date_naive();
588 let now = Utc::now();
589 v_flex()
590 .debug_selector(|| "shell/islands/calendar".into())
591 .flex_1()
592 .w_full()
593 .min_w_0()
594 .h_full()
595 .rounded(px(10.))
596 .bg(theme::surface())
597 .overflow_hidden()
598 .child(self.header(start, span, compact))
599 .map(|calendar| {
600 if span == CalendarSpan::Month {
601 calendar.child(month_grid(&self.state, start, &days, &events, today, offset))
602 } else {
603 calendar
604 .child(self.day_headers(&days, today))
605 .children(self.all_day_row(&days, &events))
606 .child(self.grid(&days, &events, today, now, send_response))
607 }
608 })
609 }
610}