BlueGit

ict rusty-teams

Files

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

calls_tab.rs

calls_tab.rs 17.3 KiB Download Raw
1use chatsvc::{Reply, ResponseType};
2use chrono::{Local, Offset, Utc};
3use gpui_kit::component::scroll::ScrollableElement as _;
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::avatar::person_avatar;
13use super::join_dialog::open_join_with_id;
14use super::new_meeting_dialog::open_new_meeting;
15use super::scroll_edge::{scroll_offset, with_top_edge};
16use super::sidebar::SIDEBAR_SCROLL_DIVIDER;
17use super::widgets::{icon, menu_icon};
18use crate::app_state::AppState;
19use crate::call::{
20 AGENDA_ROW_HEIGHT, AgendaRow, CALL_ROW_HEIGHT, CallRow, DAY_HEIGHT, EMPTY_HEIGHT,
21 FILTERS_HEIGHT, HistoryFilter, ListEntry, SECTION_HEIGHT, join_available, response_label,
22};
23use crate::theme;
24
25const FALLBACK_VIEWPORT: f32 = 900.;
26const VIEWPORT_MARGIN: f32 = 300.;
27const TIME_COLUMN_WIDTH: f32 = 96.;
28const AVATAR_SIZE: f32 = 36.;
29
30fn meeting_avatar() -> Div {
31 div()
32 .size(px(AVATAR_SIZE))
33 .flex_none()
34 .flex()
35 .items_center()
36 .justify_center()
37 .rounded_full()
38 .bg(theme::border_strong())
39 .child(icon(IconName::Video, 16., theme::text()))
40}
41
42fn call_row(
43 state: &Entity<AppState>,
44 row: &CallRow,
45 can_open_chat: bool,
46 directory: &crate::data::Directory,
47) -> AnyElement {
48 let avatar = if row.meeting {
49 meeting_avatar().into_any_element()
50 } else {
51 person_avatar(directory, row.user_id.as_deref(), &row.title, AVATAR_SIZE)
52 };
53 let title_color = if row.missed {
54 theme::red_soft()
55 } else {
56 theme::text()
57 };
58 let click_state = state.clone();
59 let click_row = row.clone();
60 let menu_state = state.clone();
61 let menu_row = row.clone();
62 let can_call_back = !row.meeting && row.user_id.is_some();
63 h_flex()
64 .id(SharedString::from(format!("call-{}", row.key)))
65 .mx(px(6.))
66 .h(px(CALL_ROW_HEIGHT))
67 .flex_none()
68 .px(px(10.))
69 .gap(px(12.))
70 .items_center()
71 .rounded(px(8.))
72 .cursor_pointer()
73 .hover(|row| row.bg(theme::row_hover()))
74 .child(avatar)
75 .child(
76 v_flex()
77 .flex_1()
78 .min_w_0()
79 .gap(px(2.))
80 .child(
81 h_flex()
82 .gap(px(8.))
83 .items_baseline()
84 .child(
85 div()
86 .flex_1()
87 .min_w_0()
88 .truncate()
89 .text_size(px(14.))
90 .text_color(title_color)
91 .child(row.title.clone()),
92 )
93 .child(
94 div()
95 .flex_none()
96 .text_size(px(11.))
97 .text_color(theme::text_muted())
98 .child(row.time_label.clone()),
99 ),
100 )
101 .child(
102 div()
103 .truncate()
104 .text_size(px(12.5))
105 .text_color(if row.missed {
106 theme::red_soft()
107 } else {
108 theme::text_muted()
109 })
110 .child(row.subline.clone()),
111 ),
112 )
113 .on_click(move |_, _, cx| {
114 click_state.update(cx, |state, cx| state.activate_call_row(&click_row, cx));
115 })
116 .context_menu(move |popup, _, _| {
117 let call_state = menu_state.clone();
118 let call_row = menu_row.clone();
119 let chat_state = menu_state.clone();
120 let chat_row = menu_row.clone();
121 popup
122 .when(can_call_back, |popup| {
123 popup.item(
124 PopupMenuItem::new("Call back")
125 .icon(menu_icon(IconName::Phone))
126 .on_click(move |_, _, cx| {
127 call_state.update(cx, |state, cx| {
128 state.call_back_from_history(&call_row, cx)
129 });
130 }),
131 )
132 })
133 .when(can_open_chat, |popup| {
134 popup.item(
135 PopupMenuItem::new("Open chat")
136 .icon(menu_icon(IconName::MessageSquare))
137 .on_click(move |_, _, cx| {
138 chat_state.update(cx, |state, cx| {
139 state.open_chat_of_call_row(&chat_row, cx)
140 });
141 }),
142 )
143 })
144 })
145 .into_any_element()
146}
147
148fn join_button(id: &'static str, label: &'static str, primary: bool) -> Stateful<Div> {
149 let (fill, hover_fill, text_color) = if primary {
150 (theme::accent(), theme::accent_text(), theme::on_accent())
151 } else {
152 (
153 theme::surface_raised(),
154 theme::popup_hover(),
155 theme::text_strong(),
156 )
157 };
158 div()
159 .id(id)
160 .h(px(28.))
161 .px(px(12.))
162 .flex()
163 .items_center()
164 .rounded(px(7.))
165 .bg(fill)
166 .cursor_pointer()
167 .hover(move |button| button.bg(hover_fill))
168 .text_size(px(12.5))
169 .font_weight(FontWeight::SEMIBOLD)
170 .text_color(text_color)
171 .child(label)
172}
173
174fn join_buttons(state: &Entity<AppState>, meet_now_pending: bool) -> impl IntoElement {
175 let meet_state = state.clone();
176 let new_state = state.clone();
177 let id_state = state.clone();
178 let paste_state = state.clone();
179 h_flex()
180 .debug_selector(|| "shell/islands/sidebar/calls-actions".into())
181 .w_full()
182 .px(px(16.))
183 .pb(px(8.))
184 .gap(px(8.))
185 .flex_wrap()
186 .child(
187 join_button("meet-now", "Meet now", true)
188 .debug_selector(|| "shell/islands/sidebar/calls-actions/meet-now".into())
189 .when(meet_now_pending, |button| button.opacity(0.5))
190 .when(!meet_now_pending, |button| {
191 button.on_click(move |_, _, cx| {
192 meet_state.update(cx, |state, cx| state.start_meet_now(cx));
193 })
194 }),
195 )
196 .child(
197 join_button("new-meeting", "New meeting", false)
198 .debug_selector(|| "shell/islands/sidebar/calls-actions/new-meeting".into())
199 .on_click(move |_, window, cx| open_new_meeting(new_state.clone(), window, cx)),
200 )
201 .child(
202 join_button("join-with-id", "Join with ID", false)
203 .on_click(move |_, window, cx| open_join_with_id(id_state.clone(), window, cx)),
204 )
205 .child(
206 join_button("paste-meeting-link", "Paste a meeting link", false).on_click(
207 move |_, _, cx| {
208 paste_state.update(cx, |state, cx| state.join_meeting_from_clipboard(cx));
209 },
210 ),
211 )
212}
213
214fn section_header(title: &'static str) -> AnyElement {
215 div()
216 .h(px(SECTION_HEIGHT))
217 .px(px(16.))
218 .flex()
219 .items_end()
220 .pb(px(4.))
221 .text_size(px(12.))
222 .font_weight(FontWeight::SEMIBOLD)
223 .text_color(theme::text_muted())
224 .child(title)
225 .into_any_element()
226}
227
228fn history_filters(state: &Entity<AppState>, current: HistoryFilter) -> AnyElement {
229 let pills = HistoryFilter::ALL.into_iter().map(|filter| {
230 let selected = filter == current;
231 let state = state.clone();
232 div()
233 .id(SharedString::from(format!("calls-filter-{}", filter.label())))
234 .h(px(24.))
235 .px(px(10.))
236 .flex()
237 .flex_none()
238 .items_center()
239 .rounded_full()
240 .cursor_pointer()
241 .text_size(px(12.))
242 .when(selected, |pill| {
243 pill.bg(theme::accent()).text_color(theme::on_accent())
244 })
245 .when(!selected, |pill| {
246 pill.bg(theme::surface_raised())
247 .text_color(theme::text_soft())
248 .hover(|pill| pill.bg(theme::border_strong()))
249 })
250 .child(filter.label())
251 .on_click(move |_, _, cx| {
252 state.update(cx, |state, cx| state.set_call_history_filter(filter, cx))
253 })
254 });
255 h_flex()
256 .h(px(FILTERS_HEIGHT))
257 .px(px(16.))
258 .gap(px(6.))
259 .items_center()
260 .children(pills)
261 .into_any_element()
262}
263
264fn day_header(label: &str) -> AnyElement {
265 div()
266 .h(px(DAY_HEIGHT))
267 .px(px(16.))
268 .flex()
269 .items_center()
270 .text_size(px(11.5))
271 .text_color(theme::text_faint())
272 .child(label.to_owned())
273 .into_any_element()
274}
275
276fn empty_row(text: &'static str) -> AnyElement {
277 div()
278 .h(px(EMPTY_HEIGHT))
279 .flex()
280 .items_center()
281 .justify_center()
282 .text_size(px(13.))
283 .text_color(theme::text_muted())
284 .child(text)
285 .into_any_element()
286}
287
288fn rsvp_menu_item(
289 state: &Entity<AppState>,
290 event_key: &str,
291 label: &'static str,
292 reply: Reply,
293 chosen: bool,
294) -> PopupMenuItem {
295 let state = state.clone();
296 let event_key = event_key.to_owned();
297 PopupMenuItem::new(label)
298 .checked(chosen)
299 .on_click(move |_, _, cx| {
300 state.update(cx, |state, cx| {
301 state.respond_to_event(&event_key, reply, cx)
302 });
303 })
304}
305
306pub(super) fn rsvp_menu(
307 menu: PopupMenu,
308 state: &Entity<AppState>,
309 key: &str,
310 response: ResponseType,
311 send_response: bool,
312) -> PopupMenu {
313 let toggle_state = state.clone();
314 menu.item(rsvp_menu_item(
315 state,
316 key,
317 "Accept",
318 Reply::Accept,
319 response == ResponseType::Accepted,
320 ))
321 .item(rsvp_menu_item(
322 state,
323 key,
324 "Tentative",
325 Reply::Tentative,
326 response == ResponseType::Tentative,
327 ))
328 .item(rsvp_menu_item(
329 state,
330 key,
331 "Decline",
332 Reply::Decline,
333 response == ResponseType::Declined,
334 ))
335 .separator()
336 .item(
337 PopupMenuItem::new("Send a response")
338 .checked(send_response)
339 .on_click(move |_, _, cx| {
340 toggle_state.update(cx, |state, cx| state.toggle_rsvp_send_response(cx));
341 }),
342 )
343}
344
345fn rsvp_control(state: &Entity<AppState>, row: &AgendaRow, send_response: bool) -> AnyElement {
346 let menu_state = state.clone();
347 let key = row.key.clone();
348 let response = row.response;
349 div()
350 .id(SharedString::from(format!("agenda-rsvp-{}", row.key)))
351 .flex_none()
352 .on_click(|_, _, cx| cx.stop_propagation())
353 .child(
354 Button::new(SharedString::from(format!(
355 "agenda-rsvp-button-{}",
356 row.key
357 )))
358 .xsmall()
359 .outline()
360 .label(response_label(response))
361 .icon(IconName::ChevronDown)
362 .dropdown_menu(move |menu, _, _| {
363 rsvp_menu(menu, &menu_state, &key, response, send_response)
364 }),
365 )
366 .into_any_element()
367}
368
369fn agenda_row(
370 state: &Entity<AppState>,
371 row: &AgendaRow,
372 joinable: bool,
373 send_response: bool,
374) -> AnyElement {
375 let click_state = state.clone();
376 let click_row = row.clone();
377 let join_state = state.clone();
378 let join_row = row.clone();
379 let menu_join_url = row.join_url.clone();
380 let line = h_flex()
381 .id(SharedString::from(format!("agenda-{}", row.key)))
382 .mx(px(6.))
383 .h(px(AGENDA_ROW_HEIGHT))
384 .flex_none()
385 .px(px(10.))
386 .gap(px(12.))
387 .items_center()
388 .rounded(px(8.))
389 .cursor_pointer()
390 .hover(|row| row.bg(theme::row_hover()))
391 .child(
392 v_flex()
393 .w(px(TIME_COLUMN_WIDTH))
394 .flex_none()
395 .text_size(px(12.))
396 .text_color(theme::text_muted())
397 .children(time_lines(&row.time_label)),
398 )
399 .child(
400 v_flex()
401 .flex_1()
402 .min_w_0()
403 .gap(px(2.))
404 .child(
405 div()
406 .truncate()
407 .text_size(px(14.))
408 .text_color(theme::text())
409 .child(row.subject.clone()),
410 )
411 .when(!row.detail.is_empty() || row.can_respond, |column| {
412 column.child(
413 h_flex()
414 .gap(px(6.))
415 .items_center()
416 .child(
417 div()
418 .flex_1()
419 .min_w_0()
420 .truncate()
421 .text_size(px(12.5))
422 .text_color(theme::text_muted())
423 .child(row.detail.clone()),
424 )
425 .when(row.can_respond, |line| {
426 line.child(rsvp_control(state, row, send_response))
427 }),
428 )
429 }),
430 )
431 .when(joinable, |line| {
432 line.child(
433 Button::new(SharedString::from(format!("agenda-join-{}", row.key)))
434 .small()
435 .primary()
436 .label("Join")
437 .on_click(move |_, _, cx| {
438 cx.stop_propagation();
439 join_state.update(cx, |state, cx| state.join_agenda_row(&join_row, cx));
440 }),
441 )
442 })
443 .on_click(move |_, _, cx| {
444 click_state.update(cx, |state, cx| state.open_agenda_chat(&click_row, cx));
445 });
446 let Some(join_url) = menu_join_url else {
447 return line.into_any_element();
448 };
449 line.context_menu(move |popup, _, _| {
450 let join_url = join_url.clone();
451 popup.item(
452 PopupMenuItem::new("Copy meeting link")
453 .icon(menu_icon(IconName::Link))
454 .on_click(move |_, _, cx| {
455 cx.write_to_clipboard(ClipboardItem::new_string(join_url.clone()));
456 }),
457 )
458 })
459 .into_any_element()
460}
461
462pub fn calls_body(
463 state: &Entity<AppState>,
464 scroll: &ScrollHandle,
465 window_height: f32,
466 cx: &mut App,
467) -> AnyElement {
468 let now = Utc::now();
469 let offset = Local::now().offset().fix();
470 if state.read(cx).calls_list.needs_rebuild(now, offset) {
471 let rebuild_state = state.clone();
472 cx.defer(move |cx| rebuild_state.update(cx, |state, cx| state.rebuild_calls_list(cx)));
473 }
474 let top = -f32::from(scroll_offset(scroll).y);
475 let measured = f32::from(scroll.bounds().size.height);
476 let height = if measured > 1. {
477 measured
478 } else {
479 window_height.max(FALLBACK_VIEWPORT)
480 };
481 let app = state.read(cx);
482 let window = app
483 .calls_list
484 .window(top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
485 let entries = &app.calls_list.entries()[window.range.clone()];
486 let mut list = v_flex().w_full().pb(px(12.));
487 if window.before > 0. {
488 list = list.child(div().h(px(window.before)).flex_none());
489 }
490 for entry in entries {
491 list = list.child(match entry {
492 ListEntry::Section(title) => section_header(title),
493 ListEntry::Filters(filter) => history_filters(state, *filter),
494 ListEntry::Day(label) => day_header(label),
495 ListEntry::Empty(text) => empty_row(text),
496 ListEntry::Agenda(row) => agenda_row(
497 state,
498 row,
499 join_available(row.start, row.end, now) && row.join_url.is_some(),
500 app.rsvp_send_response,
501 ),
502 ListEntry::Call(row) => call_row(
503 state,
504 row,
505 app.can_open_chat_of_call_row(row),
506 &app.directory,
507 ),
508 });
509 }
510 if window.after > 0. {
511 list = list.child(div().h(px(window.after)).flex_none());
512 }
513 v_flex()
514 .flex_1()
515 .min_h_0()
516 .child(join_buttons(state, app.meet_now_pending))
517 .child(with_top_edge(
518 div()
519 .id("calls-scroll")
520 .flex_1()
521 .min_h_0()
522 .overflow_y_scrollbar()
523 .track_scroll(scroll)
524 .child(list),
525 scroll,
526 SIDEBAR_SCROLL_DIVIDER,
527 ))
528 .into_any_element()
529}
530
531fn time_lines(label: &str) -> Vec<String> {
532 let twelve_hour = crate::format::clock_format().contains(['p', 'P']);
533 match label.split_once(" - ") {
534 Some((start, end)) if twelve_hour => vec![start.to_owned(), end.to_owned()],
535 _ => vec![label.to_owned()],
536 }
537}