BlueGit

ict rusty-teams

Files

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

calendar_view.rs

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