BlueGit

ict rusty-teams

Files

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

calls_page.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 3 weeks ago
a6592765 History
calls_page.rs 23.0 KiB Download Raw
1use std::time::Duration;
2
3use chrono::{Local, Offset, Utc};
4use gpui_kit::assets::IconName;
5use gpui_kit::component::button::{Button, ButtonVariants as _};
6use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenuItem};
7use gpui_kit::component::scroll::ScrollableElement as _;
8use gpui_kit::component::{Disableable as _, h_flex, v_flex};
9use gpui_kit::prelude::FluentBuilder as _;
10use gpui_kit::*;
11
12use super::avatar::person_avatar;
13use super::new_chat::NewChatDraft;
14use super::scroll_edge::{scroll_offset, with_top_edge};
15use super::widgets::{icon, menu_icon, symbol};
16use crate::app_state::AppState;
17use crate::call::{
18 CALL_ROW_HEIGHT, CallRow, EMPTY_HEIGHT, HistoryFilter, ListEntry, SpeedDialEntry,
19};
20use crate::theme;
21
22pub const CALLS_DIAL_TAG: &str = "shell/islands/main/calls-dial";
23pub const CALLS_HISTORY_TAG: &str = "shell/islands/main/calls-history";
24pub const CALLS_SPEED_DIAL_TAG: &str = "shell/islands/main/calls-speed-dial";
25const CALLS_SCROLL_DIVIDER: &str = "shell/islands/main/calls-history/divider-scroll";
26const CALLS_TITLE: &str = "Calls";
27const HISTORY_TITLE: &str = "History";
28const SPEED_DIAL_TITLE: &str = "Speed dial";
29const SPEED_DIAL_EMPTY: &str = "Add people to speed dial for quick access.";
30const SPEED_DIAL_ADD_TITLE: &str = "Add to speed dial";
31const ADD_LABEL: &str = "Add";
32const CANCEL_LABEL: &str = "Cancel";
33const SPEED_DIAL_WIDTH: f32 = 340.;
34const SPEED_DIAL_MIN_WINDOW_WIDTH: f32 = 1100.;
35const CALL_LABEL: &str = "Call";
36const CALL_ONE_PERSON_HINT: &str = "Pick one person to call";
37const HEADER_HEIGHT: f32 = 60.;
38const PAGE_PADDING: f32 = 20.;
39const DIAL_WIDTH: f32 = 340.;
40const HISTORY_HEADER_HEIGHT: f32 = 52.;
41const CALL_BUTTON_HEIGHT: f32 = 32.;
42const FALLBACK_VIEWPORT: f32 = 900.;
43const VIEWPORT_MARGIN: f32 = 300.;
44const AVATAR_SIZE: f32 = 36.;
45const REFRESH_INTERVAL: Duration = Duration::from_secs(30);
46
47fn meeting_avatar() -> Div {
48 div()
49 .size(px(AVATAR_SIZE))
50 .flex_none()
51 .flex()
52 .items_center()
53 .justify_center()
54 .rounded_full()
55 .bg(theme::border_strong())
56 .child(icon(IconName::Video, 16., theme::text()))
57}
58
59fn call_row(
60 state: &Entity<AppState>,
61 row: &CallRow,
62 can_open_chat: bool,
63 on_speed_dial: bool,
64 directory: &crate::data::Directory,
65) -> AnyElement {
66 let avatar = if row.meeting {
67 meeting_avatar().into_any_element()
68 } else {
69 person_avatar(directory, row.user_id.as_deref(), &row.title, AVATAR_SIZE)
70 };
71 let title_color = if row.missed {
72 theme::red_soft()
73 } else {
74 theme::text()
75 };
76 let click_state = state.clone();
77 let click_row = row.clone();
78 let menu_state = state.clone();
79 let menu_row = row.clone();
80 let can_call_back = !row.meeting && row.user_id.is_some();
81 let can_add_to_speed_dial = can_call_back && !on_speed_dial;
82 h_flex()
83 .id(SharedString::from(format!("call-{}", row.key)))
84 .h(px(CALL_ROW_HEIGHT))
85 .flex_none()
86 .px(px(10.))
87 .gap(px(12.))
88 .items_center()
89 .rounded(px(8.))
90 .cursor_pointer()
91 .hover(|row| row.bg(theme::row_hover()))
92 .child(avatar)
93 .child(
94 v_flex()
95 .flex_1()
96 .min_w_0()
97 .gap(px(2.))
98 .child(
99 h_flex()
100 .gap(px(8.))
101 .items_baseline()
102 .child(
103 div()
104 .flex_1()
105 .min_w_0()
106 .truncate()
107 .text_size(px(14.))
108 .text_color(title_color)
109 .child(row.title.clone()),
110 )
111 .child(
112 div()
113 .flex_none()
114 .text_size(px(11.))
115 .text_color(theme::text_muted())
116 .child(row.time_label.clone()),
117 ),
118 )
119 .child(
120 div()
121 .truncate()
122 .text_size(px(12.5))
123 .text_color(if row.missed {
124 theme::red_soft()
125 } else {
126 theme::text_muted()
127 })
128 .child(row.subline.clone()),
129 ),
130 )
131 .on_click(move |_, _, cx| {
132 click_state.update(cx, |state, cx| state.activate_call_row(&click_row, cx));
133 })
134 .context_menu(move |popup, _, _| {
135 let call_state = menu_state.clone();
136 let call_row = menu_row.clone();
137 let chat_state = menu_state.clone();
138 let chat_row = menu_row.clone();
139 let dial_state = menu_state.clone();
140 let dial_row = menu_row.clone();
141 popup
142 .when(can_call_back, |popup| {
143 popup.item(
144 PopupMenuItem::new("Call back")
145 .icon(menu_icon(IconName::Phone))
146 .on_click(move |_, _, cx| {
147 call_state.update(cx, |state, cx| {
148 state.call_back_from_history(&call_row, cx)
149 });
150 }),
151 )
152 })
153 .when(can_open_chat, |popup| {
154 popup.item(
155 PopupMenuItem::new("Open chat")
156 .icon(menu_icon(IconName::MessageSquare))
157 .on_click(move |_, _, cx| {
158 chat_state.update(cx, |state, cx| {
159 state.open_chat_of_call_row(&chat_row, cx)
160 });
161 }),
162 )
163 })
164 .when(can_add_to_speed_dial, |popup| {
165 popup.item(
166 PopupMenuItem::new(SPEED_DIAL_ADD_TITLE)
167 .icon(menu_icon(IconName::Star))
168 .on_click(move |_, _, cx| {
169 let Some(user_id) = dial_row.user_id.as_deref() else {
170 return;
171 };
172 dial_state.update(cx, |state, cx| {
173 state.add_to_speed_dial(user_id, &dial_row.title, cx)
174 });
175 }),
176 )
177 })
178 })
179 .into_any_element()
180}
181
182fn history_filters(state: &Entity<AppState>, current: HistoryFilter) -> AnyElement {
183 let pills = HistoryFilter::ALL.into_iter().map(|filter| {
184 let selected = filter == current;
185 let state = state.clone();
186 div()
187 .id(SharedString::from(format!(
188 "calls-filter-{}",
189 filter.label()
190 )))
191 .h(px(24.))
192 .px(px(10.))
193 .flex()
194 .flex_none()
195 .items_center()
196 .rounded_full()
197 .cursor_pointer()
198 .text_size(px(12.))
199 .when(selected, |pill| {
200 pill.bg(theme::accent()).text_color(theme::on_accent())
201 })
202 .when(!selected, |pill| {
203 pill.bg(theme::surface_raised())
204 .text_color(theme::text_soft())
205 .hover(|pill| pill.bg(theme::border_strong()))
206 })
207 .child(filter.label())
208 .on_click(move |_, _, cx| {
209 state.update(cx, |state, cx| state.set_call_history_filter(filter, cx))
210 })
211 });
212 h_flex()
213 .gap(px(6.))
214 .items_center()
215 .children(pills)
216 .into_any_element()
217}
218
219fn empty_row(text: &'static str) -> AnyElement {
220 div()
221 .h(px(EMPTY_HEIGHT))
222 .flex()
223 .items_center()
224 .justify_center()
225 .text_size(px(13.))
226 .text_color(theme::text_muted())
227 .child(text)
228 .into_any_element()
229}
230
231fn speed_dial_row(
232 state: &Entity<AppState>,
233 entry: &SpeedDialEntry,
234 directory: &crate::data::Directory,
235) -> AnyElement {
236 let click_state = state.clone();
237 let user_id = entry.user_id.clone();
238 let menu_state = state.clone();
239 let menu_user_id = entry.user_id.clone();
240 h_flex()
241 .id(SharedString::from(format!("speed-dial-{}", entry.user_id)))
242 .h(px(CALL_ROW_HEIGHT))
243 .flex_none()
244 .px(px(10.))
245 .gap(px(12.))
246 .items_center()
247 .rounded(px(8.))
248 .cursor_pointer()
249 .hover(|row| row.bg(theme::row_hover()))
250 .child(person_avatar(
251 directory,
252 Some(&entry.user_id),
253 &entry.name,
254 AVATAR_SIZE,
255 ))
256 .child(
257 div()
258 .flex_1()
259 .min_w_0()
260 .truncate()
261 .text_size(px(14.))
262 .text_color(theme::text())
263 .child(entry.name.clone()),
264 )
265 .on_click(move |_, _, cx| {
266 click_state.update(cx, |state, cx| state.call_person(&user_id, false, cx));
267 })
268 .context_menu(move |popup, _, _| {
269 let video_state = menu_state.clone();
270 let video_user_id = menu_user_id.clone();
271 let remove_state = menu_state.clone();
272 let remove_user_id = menu_user_id.clone();
273 popup
274 .item(
275 PopupMenuItem::new("Video call")
276 .icon(menu_icon(IconName::Video))
277 .on_click(move |_, _, cx| {
278 video_state.update(cx, |state, cx| {
279 state.call_person(&video_user_id, true, cx)
280 });
281 }),
282 )
283 .item(
284 PopupMenuItem::new("Remove from speed dial")
285 .icon(menu_icon(IconName::Delete))
286 .on_click(move |_, _, cx| {
287 remove_state.update(cx, |state, cx| {
288 state.remove_from_speed_dial(&remove_user_id, cx)
289 });
290 }),
291 )
292 })
293 .into_any_element()
294}
295
296pub fn shows_speed_dial(window_width: f32) -> bool {
297 window_width >= SPEED_DIAL_MIN_WINDOW_WIDTH
298}
299
300pub struct CallsPage {
301 app: Entity<AppState>,
302 people: Entity<NewChatDraft>,
303 speed_dial_people: Entity<NewChatDraft>,
304 adding_speed_dial: bool,
305 scroll: ScrollHandle,
306 _observations: [Subscription; 3],
307}
308
309impl CallsPage {
310 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
311 let people_app = app.clone();
312 let people = cx.new(|cx| NewChatDraft::new(people_app, window, cx).compact());
313 let speed_dial_app = app.clone();
314 let speed_dial_people =
315 cx.new(|cx| NewChatDraft::new(speed_dial_app, window, cx).compact());
316 let observations = [
317 cx.observe(&app, |_, _, cx| cx.notify()),
318 cx.observe(&people, |_, _, cx| cx.notify()),
319 cx.observe(&speed_dial_people, |_, _, cx| cx.notify()),
320 ];
321 cx.spawn(async move |this, cx| {
322 loop {
323 cx.background_executor().timer(REFRESH_INTERVAL).await;
324 if this.update(cx, |_, cx| cx.notify()).is_err() {
325 break;
326 }
327 }
328 })
329 .detach();
330 CallsPage {
331 app,
332 people,
333 speed_dial_people,
334 adding_speed_dial: false,
335 scroll: ScrollHandle::new(),
336 _observations: observations,
337 }
338 }
339
340 fn call_picked(&mut self, window: &mut Window, cx: &mut Context<Self>) {
341 let picked = self.people.read(cx).chips().to_vec();
342 let [person] = picked.as_slice() else {
343 return;
344 };
345 let user_id = person.user_id.clone();
346 self.app
347 .update(cx, |state, cx| state.call_person(&user_id, false, cx));
348 self.people
349 .update(cx, |people, cx| people.reset(window, cx));
350 }
351
352 fn open_speed_dial_add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
353 self.adding_speed_dial = true;
354 self.speed_dial_people
355 .update(cx, |people, cx| people.focus_query(window, cx));
356 cx.notify();
357 }
358
359 fn close_speed_dial_add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
360 self.adding_speed_dial = false;
361 self.speed_dial_people
362 .update(cx, |people, cx| people.reset(window, cx));
363 cx.notify();
364 }
365
366 fn confirm_speed_dial_add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
367 let picked = self.speed_dial_people.read(cx).chips().to_vec();
368 self.app.update(cx, |state, cx| {
369 for person in &picked {
370 state.add_to_speed_dial(&person.user_id, &person.name, cx);
371 }
372 });
373 self.close_speed_dial_add(window, cx);
374 }
375
376 fn speed_dial_adder(&self, cx: &mut Context<Self>) -> Div {
377 let picked = !self.speed_dial_people.read(cx).chips().is_empty();
378 v_flex()
379 .gap(px(12.))
380 .child(self.speed_dial_people.clone())
381 .child(
382 h_flex()
383 .gap(px(8.))
384 .justify_end()
385 .child(
386 Button::new("speed-dial-cancel")
387 .label(CANCEL_LABEL)
388 .on_click(cx.listener(|this, _, window, cx| {
389 this.close_speed_dial_add(window, cx)
390 })),
391 )
392 .child(
393 Button::new("speed-dial-confirm")
394 .label(ADD_LABEL)
395 .when(picked, |button| button.primary())
396 .disabled(!picked)
397 .on_click(cx.listener(|this, _, window, cx| {
398 this.confirm_speed_dial_add(window, cx)
399 })),
400 ),
401 )
402 }
403
404 fn speed_dial_empty(&self, cx: &mut Context<Self>) -> Div {
405 v_flex()
406 .flex_1()
407 .items_center()
408 .justify_center()
409 .gap(px(12.))
410 .child(
411 div()
412 .text_size(px(13.))
413 .text_color(theme::text_muted())
414 .child(SPEED_DIAL_EMPTY),
415 )
416 .child(
417 Button::new("speed-dial-empty-add")
418 .label(ADD_LABEL)
419 .on_click(
420 cx.listener(|this, _, window, cx| this.open_speed_dial_add(window, cx)),
421 ),
422 )
423 }
424
425 fn speed_dial_column(&self, cx: &mut Context<Self>) -> Div {
426 let state = self.app.read(cx);
427 let rows = state
428 .speed_dial
429 .iter()
430 .map(|entry| speed_dial_row(&self.app, entry, &state.directory))
431 .collect::<Vec<_>>();
432 let body = if self.adding_speed_dial {
433 div().p(px(PAGE_PADDING)).child(self.speed_dial_adder(cx))
434 } else if rows.is_empty() {
435 self.speed_dial_empty(cx)
436 } else {
437 div().child(
438 div()
439 .id("calls-speed-dial-scroll")
440 .size_full()
441 .overflow_y_scrollbar()
442 .child(
443 v_flex()
444 .px(px(PAGE_PADDING - 10.))
445 .py(px(8.))
446 .children(rows),
447 ),
448 )
449 };
450 v_flex()
451 .debug_selector(|| CALLS_SPEED_DIAL_TAG.into())
452 .w(px(SPEED_DIAL_WIDTH))
453 .h_full()
454 .flex_none()
455 .child(
456 h_flex()
457 .h(px(HISTORY_HEADER_HEIGHT))
458 .flex_none()
459 .pl(px(PAGE_PADDING))
460 .pr(px(PAGE_PADDING - 6.))
461 .items_center()
462 .justify_between()
463 .child(
464 div()
465 .text_size(px(14.))
466 .font_weight(FontWeight::SEMIBOLD)
467 .text_color(theme::text())
468 .child(SPEED_DIAL_TITLE),
469 )
470 .child(
471 div()
472 .id("speed-dial-add")
473 .size(px(28.))
474 .flex()
475 .items_center()
476 .justify_center()
477 .rounded(px(6.))
478 .cursor_pointer()
479 .hover(|button| button.bg(theme::row_hover()))
480 .tooltip(|window, cx| {
481 gpui_kit::component::tooltip::Tooltip::new(SPEED_DIAL_ADD_TITLE)
482 .build(window, cx)
483 })
484 .child(symbol("person_add", 18., theme::text_soft()))
485 .on_click(cx.listener(|this, _, window, cx| {
486 this.open_speed_dial_add(window, cx)
487 })),
488 ),
489 )
490 .child(body.flex_1().min_h_0())
491 }
492
493 fn dial_column(&self, cx: &mut Context<Self>) -> Div {
494 let picked = self.people.read(cx).chips().len();
495 let callable = picked == 1;
496 v_flex()
497 .debug_selector(|| CALLS_DIAL_TAG.into())
498 .w(px(DIAL_WIDTH))
499 .h_full()
500 .flex_none()
501 .p(px(PAGE_PADDING))
502 .gap(px(12.))
503 .child(self.people.clone())
504 .child(
505 Button::new("calls-dial-call")
506 .w_full()
507 .h(px(CALL_BUTTON_HEIGHT))
508 .icon(IconName::Phone)
509 .label(CALL_LABEL)
510 .when(callable, |button| button.primary())
511 .disabled(!callable)
512 .when(picked > 1, |button| button.tooltip(CALL_ONE_PERSON_HINT))
513 .on_click(cx.listener(|this, _, window, cx| this.call_picked(window, cx))),
514 )
515 }
516
517 fn history_list(&self, window_height: f32, cx: &mut Context<Self>) -> Div {
518 let now = Utc::now();
519 let offset = Local::now().offset().fix();
520 if self.app.read(cx).calls_list.needs_rebuild(now, offset) {
521 let app = self.app.clone();
522 cx.defer(move |cx| app.update(cx, |state, cx| state.rebuild_calls_list(cx)));
523 }
524 let top = -f32::from(scroll_offset(&self.scroll).y);
525 let measured = f32::from(self.scroll.bounds().size.height);
526 let height = if measured > 1. {
527 measured
528 } else {
529 window_height.max(FALLBACK_VIEWPORT)
530 };
531 let state = self.app.read(cx);
532 let visible = state
533 .calls_list
534 .window(top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
535 let mut list = v_flex().w_full().px(px(PAGE_PADDING - 10.)).py(px(8.));
536 if visible.before > 0. {
537 list = list.child(div().h(px(visible.before)).flex_none());
538 }
539 for entry in &state.calls_list.entries()[visible.range.clone()] {
540 list = list.child(match entry {
541 ListEntry::Empty(text) => empty_row(text),
542 ListEntry::Call(row) => call_row(
543 &self.app,
544 row,
545 state.can_open_chat_of_call_row(row),
546 row.user_id
547 .as_deref()
548 .is_some_and(|user_id| state.on_speed_dial(user_id)),
549 &state.directory,
550 ),
551 });
552 }
553 if visible.after > 0. {
554 list = list.child(div().h(px(visible.after)).flex_none());
555 }
556 list
557 }
558
559 fn history_column(&self, window_height: f32, cx: &mut Context<Self>) -> Div {
560 let filter = self.app.read(cx).calls_list.filter();
561 v_flex()
562 .debug_selector(|| CALLS_HISTORY_TAG.into())
563 .flex_1()
564 .min_w_0()
565 .h_full()
566 .child(
567 h_flex()
568 .h(px(HISTORY_HEADER_HEIGHT))
569 .flex_none()
570 .px(px(PAGE_PADDING))
571 .gap(px(16.))
572 .items_center()
573 .child(
574 div()
575 .text_size(px(14.))
576 .font_weight(FontWeight::SEMIBOLD)
577 .text_color(theme::text())
578 .child(HISTORY_TITLE),
579 )
580 .child(history_filters(&self.app, filter)),
581 )
582 .child(with_top_edge(
583 div()
584 .id("calls-history-scroll")
585 .flex_1()
586 .min_h_0()
587 .overflow_y_scrollbar()
588 .track_scroll(&self.scroll)
589 .child(self.history_list(window_height, cx)),
590 &self.scroll,
591 CALLS_SCROLL_DIVIDER,
592 ))
593 }
594}
595
596impl Render for CallsPage {
597 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
598 let window_height = f32::from(window.viewport_size().height);
599 let window_width = f32::from(window.viewport_size().width);
600 v_flex()
601 .debug_selector(|| "shell/islands/main".into())
602 .flex_1()
603 .w_full()
604 .min_w_0()
605 .h_full()
606 .rounded(px(10.))
607 .bg(theme::surface())
608 .overflow_hidden()
609 .child(
610 h_flex()
611 .h(px(HEADER_HEIGHT))
612 .flex_none()
613 .px(px(PAGE_PADDING))
614 .items_center()
615 .child(
616 div()
617 .text_size(px(16.))
618 .font_weight(FontWeight::BOLD)
619 .text_color(theme::text())
620 .child(CALLS_TITLE),
621 ),
622 )
623 .child(div().w_full().h(px(1.)).flex_none().bg(theme::border()))
624 .child(
625 h_flex()
626 .flex_1()
627 .min_h_0()
628 .w_full()
629 .child(self.dial_column(cx))
630 .child(div().w(px(1.)).h_full().flex_none().bg(theme::border()))
631 .child(self.history_column(window_height, cx))
632 .when(shows_speed_dial(window_width), |columns| {
633 columns
634 .child(div().w(px(1.)).h_full().flex_none().bg(theme::border()))
635 .child(self.speed_dial_column(cx))
636 }),
637 )
638 }
639}