BlueGit

ict rusty-teams

Files

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

calls_tab.rs

calls_tab.rs 17.5 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 _, 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
306fn rsvp_control(state: &Entity<AppState>, row: &AgendaRow, send_response: bool) -> AnyElement {
307 let menu_state = state.clone();
308 let key = row.key.clone();
309 let response = row.response;
310 div()
311 .id(SharedString::from(format!("agenda-rsvp-{}", row.key)))
312 .flex_none()
313 .on_click(|_, _, cx| cx.stop_propagation())
314 .child(
315 Button::new(SharedString::from(format!(
316 "agenda-rsvp-button-{}",
317 row.key
318 )))
319 .xsmall()
320 .outline()
321 .label(response_label(response))
322 .icon(IconName::ChevronDown)
323 .dropdown_menu(move |menu, _, _| {
324 let toggle_state = menu_state.clone();
325 menu.item(rsvp_menu_item(
326 &menu_state,
327 &key,
328 "Accept",
329 Reply::Accept,
330 response == ResponseType::Accepted,
331 ))
332 .item(rsvp_menu_item(
333 &menu_state,
334 &key,
335 "Tentative",
336 Reply::Tentative,
337 response == ResponseType::Tentative,
338 ))
339 .item(rsvp_menu_item(
340 &menu_state,
341 &key,
342 "Decline",
343 Reply::Decline,
344 response == ResponseType::Declined,
345 ))
346 .separator()
347 .item(
348 PopupMenuItem::new("Send a response")
349 .checked(send_response)
350 .on_click(move |_, _, cx| {
351 toggle_state
352 .update(cx, |state, cx| state.toggle_rsvp_send_response(cx));
353 }),
354 )
355 }),
356 )
357 .into_any_element()
358}
359
360fn agenda_row(
361 state: &Entity<AppState>,
362 row: &AgendaRow,
363 joinable: bool,
364 send_response: bool,
365) -> AnyElement {
366 let click_state = state.clone();
367 let click_row = row.clone();
368 let join_state = state.clone();
369 let join_row = row.clone();
370 let menu_join_url = row.join_url.clone();
371 let line = h_flex()
372 .id(SharedString::from(format!("agenda-{}", row.key)))
373 .mx(px(6.))
374 .h(px(AGENDA_ROW_HEIGHT))
375 .flex_none()
376 .px(px(10.))
377 .gap(px(12.))
378 .items_center()
379 .rounded(px(8.))
380 .cursor_pointer()
381 .hover(|row| row.bg(theme::row_hover()))
382 .child(
383 v_flex()
384 .w(px(TIME_COLUMN_WIDTH))
385 .flex_none()
386 .text_size(px(12.))
387 .text_color(theme::text_muted())
388 .children(time_lines(&row.time_label)),
389 )
390 .child(
391 v_flex()
392 .flex_1()
393 .min_w_0()
394 .gap(px(2.))
395 .child(
396 div()
397 .truncate()
398 .text_size(px(14.))
399 .text_color(theme::text())
400 .child(row.subject.clone()),
401 )
402 .when(!row.detail.is_empty() || row.can_respond, |column| {
403 column.child(
404 h_flex()
405 .gap(px(6.))
406 .items_center()
407 .child(
408 div()
409 .flex_1()
410 .min_w_0()
411 .truncate()
412 .text_size(px(12.5))
413 .text_color(theme::text_muted())
414 .child(row.detail.clone()),
415 )
416 .when(row.can_respond, |line| {
417 line.child(rsvp_control(state, row, send_response))
418 }),
419 )
420 }),
421 )
422 .when(joinable, |line| {
423 line.child(
424 Button::new(SharedString::from(format!("agenda-join-{}", row.key)))
425 .small()
426 .primary()
427 .label("Join")
428 .on_click(move |_, _, cx| {
429 cx.stop_propagation();
430 join_state.update(cx, |state, cx| state.join_agenda_row(&join_row, cx));
431 }),
432 )
433 })
434 .on_click(move |_, _, cx| {
435 click_state.update(cx, |state, cx| state.open_agenda_chat(&click_row, cx));
436 });
437 let Some(join_url) = menu_join_url else {
438 return line.into_any_element();
439 };
440 line.context_menu(move |popup, _, _| {
441 let join_url = join_url.clone();
442 popup.item(
443 PopupMenuItem::new("Copy meeting link")
444 .icon(menu_icon(IconName::Link))
445 .on_click(move |_, _, cx| {
446 cx.write_to_clipboard(ClipboardItem::new_string(join_url.clone()));
447 }),
448 )
449 })
450 .into_any_element()
451}
452
453pub fn calls_body(
454 state: &Entity<AppState>,
455 scroll: &ScrollHandle,
456 window_height: f32,
457 cx: &mut App,
458) -> AnyElement {
459 let now = Utc::now();
460 let offset = Local::now().offset().fix();
461 if state.read(cx).calls_list.needs_rebuild(now, offset) {
462 let rebuild_state = state.clone();
463 cx.defer(move |cx| rebuild_state.update(cx, |state, cx| state.rebuild_calls_list(cx)));
464 }
465 let top = -f32::from(scroll_offset(scroll).y);
466 let measured = f32::from(scroll.bounds().size.height);
467 let height = if measured > 1. {
468 measured
469 } else {
470 window_height.max(FALLBACK_VIEWPORT)
471 };
472 let app = state.read(cx);
473 let window = app
474 .calls_list
475 .window(top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
476 let entries = &app.calls_list.entries()[window.range.clone()];
477 let mut list = v_flex().w_full().pb(px(12.));
478 if window.before > 0. {
479 list = list.child(div().h(px(window.before)).flex_none());
480 }
481 for entry in entries {
482 list = list.child(match entry {
483 ListEntry::Section(title) => section_header(title),
484 ListEntry::Filters(filter) => history_filters(state, *filter),
485 ListEntry::Day(label) => day_header(label),
486 ListEntry::Empty(text) => empty_row(text),
487 ListEntry::Agenda(row) => agenda_row(
488 state,
489 row,
490 join_available(row.start, row.end, now) && row.join_url.is_some(),
491 app.rsvp_send_response,
492 ),
493 ListEntry::Call(row) => call_row(
494 state,
495 row,
496 app.can_open_chat_of_call_row(row),
497 &app.directory,
498 ),
499 });
500 }
501 if window.after > 0. {
502 list = list.child(div().h(px(window.after)).flex_none());
503 }
504 v_flex()
505 .flex_1()
506 .min_h_0()
507 .child(join_buttons(state, app.meet_now_pending))
508 .child(with_top_edge(
509 div()
510 .id("calls-scroll")
511 .flex_1()
512 .min_h_0()
513 .overflow_y_scrollbar()
514 .track_scroll(scroll)
515 .child(list),
516 scroll,
517 SIDEBAR_SCROLL_DIVIDER,
518 ))
519 .into_any_element()
520}
521
522fn time_lines(label: &str) -> Vec<String> {
523 let twelve_hour = crate::format::clock_format().contains(['p', 'P']);
524 match label.split_once(" - ") {
525 Some((start, end)) if twelve_hour => vec![start.to_owned(), end.to_owned()],
526 _ => vec![label.to_owned()],
527 }
528}