BlueGit

ict rusty-teams

Files

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

calls_page.rs

6f3e9db5 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!("calls-filter-{}", filter.label())))
188 .h(px(24.))
189 .px(px(10.))
190 .flex()
191 .flex_none()
192 .items_center()
193 .rounded_full()
194 .cursor_pointer()
195 .text_size(px(12.))
196 .when(selected, |pill| {
197 pill.bg(theme::accent()).text_color(theme::on_accent())
198 })
199 .when(!selected, |pill| {
200 pill.bg(theme::surface_raised())
201 .text_color(theme::text_soft())
202 .hover(|pill| pill.bg(theme::border_strong()))
203 })
204 .child(filter.label())
205 .on_click(move |_, _, cx| {
206 state.update(cx, |state, cx| state.set_call_history_filter(filter, cx))
207 })
208 });
209 h_flex().gap(px(6.)).items_center().children(pills).into_any_element()
210}
211
212fn empty_row(text: &'static str) -> AnyElement {
213 div()
214 .h(px(EMPTY_HEIGHT))
215 .flex()
216 .items_center()
217 .justify_center()
218 .text_size(px(13.))
219 .text_color(theme::text_muted())
220 .child(text)
221 .into_any_element()
222}
223
224fn speed_dial_row(
225 state: &Entity<AppState>,
226 entry: &SpeedDialEntry,
227 directory: &crate::data::Directory,
228) -> AnyElement {
229 let click_state = state.clone();
230 let user_id = entry.user_id.clone();
231 let menu_state = state.clone();
232 let menu_user_id = entry.user_id.clone();
233 h_flex()
234 .id(SharedString::from(format!("speed-dial-{}", entry.user_id)))
235 .h(px(CALL_ROW_HEIGHT))
236 .flex_none()
237 .px(px(10.))
238 .gap(px(12.))
239 .items_center()
240 .rounded(px(8.))
241 .cursor_pointer()
242 .hover(|row| row.bg(theme::row_hover()))
243 .child(person_avatar(
244 directory,
245 Some(&entry.user_id),
246 &entry.name,
247 AVATAR_SIZE,
248 ))
249 .child(
250 div()
251 .flex_1()
252 .min_w_0()
253 .truncate()
254 .text_size(px(14.))
255 .text_color(theme::text())
256 .child(entry.name.clone()),
257 )
258 .on_click(move |_, _, cx| {
259 click_state.update(cx, |state, cx| state.call_person(&user_id, false, cx));
260 })
261 .context_menu(move |popup, _, _| {
262 let video_state = menu_state.clone();
263 let video_user_id = menu_user_id.clone();
264 let remove_state = menu_state.clone();
265 let remove_user_id = menu_user_id.clone();
266 popup
267 .item(
268 PopupMenuItem::new("Video call")
269 .icon(menu_icon(IconName::Video))
270 .on_click(move |_, _, cx| {
271 video_state.update(cx, |state, cx| {
272 state.call_person(&video_user_id, true, cx)
273 });
274 }),
275 )
276 .item(
277 PopupMenuItem::new("Remove from speed dial")
278 .icon(menu_icon(IconName::Delete))
279 .on_click(move |_, _, cx| {
280 remove_state.update(cx, |state, cx| {
281 state.remove_from_speed_dial(&remove_user_id, cx)
282 });
283 }),
284 )
285 })
286 .into_any_element()
287}
288
289pub fn shows_speed_dial(window_width: f32) -> bool {
290 window_width >= SPEED_DIAL_MIN_WINDOW_WIDTH
291}
292
293pub struct CallsPage {
294 app: Entity<AppState>,
295 people: Entity<NewChatDraft>,
296 speed_dial_people: Entity<NewChatDraft>,
297 adding_speed_dial: bool,
298 scroll: ScrollHandle,
299 _observations: [Subscription; 3],
300}
301
302impl CallsPage {
303 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
304 let people_app = app.clone();
305 let people = cx.new(|cx| NewChatDraft::new(people_app, window, cx).compact());
306 let speed_dial_app = app.clone();
307 let speed_dial_people =
308 cx.new(|cx| NewChatDraft::new(speed_dial_app, window, cx).compact());
309 let observations = [
310 cx.observe(&app, |_, _, cx| cx.notify()),
311 cx.observe(&people, |_, _, cx| cx.notify()),
312 cx.observe(&speed_dial_people, |_, _, cx| cx.notify()),
313 ];
314 cx.spawn(async move |this, cx| {
315 loop {
316 cx.background_executor().timer(REFRESH_INTERVAL).await;
317 if this.update(cx, |_, cx| cx.notify()).is_err() {
318 break;
319 }
320 }
321 })
322 .detach();
323 CallsPage {
324 app,
325 people,
326 speed_dial_people,
327 adding_speed_dial: false,
328 scroll: ScrollHandle::new(),
329 _observations: observations,
330 }
331 }
332
333 fn call_picked(&mut self, window: &mut Window, cx: &mut Context<Self>) {
334 let picked = self.people.read(cx).chips().to_vec();
335 let [person] = picked.as_slice() else {
336 return;
337 };
338 let user_id = person.user_id.clone();
339 self.app
340 .update(cx, |state, cx| state.call_person(&user_id, false, cx));
341 self.people
342 .update(cx, |people, cx| people.reset(window, cx));
343 }
344
345 fn open_speed_dial_add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
346 self.adding_speed_dial = true;
347 self.speed_dial_people
348 .update(cx, |people, cx| people.focus_query(window, cx));
349 cx.notify();
350 }
351
352 fn close_speed_dial_add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
353 self.adding_speed_dial = false;
354 self.speed_dial_people
355 .update(cx, |people, cx| people.reset(window, cx));
356 cx.notify();
357 }
358
359 fn confirm_speed_dial_add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
360 let picked = self.speed_dial_people.read(cx).chips().to_vec();
361 self.app.update(cx, |state, cx| {
362 for person in &picked {
363 state.add_to_speed_dial(&person.user_id, &person.name, cx);
364 }
365 });
366 self.close_speed_dial_add(window, cx);
367 }
368
369 fn speed_dial_adder(&self, cx: &mut Context<Self>) -> Div {
370 let picked = !self.speed_dial_people.read(cx).chips().is_empty();
371 v_flex()
372 .gap(px(12.))
373 .child(self.speed_dial_people.clone())
374 .child(
375 h_flex()
376 .gap(px(8.))
377 .justify_end()
378 .child(
379 Button::new("speed-dial-cancel")
380 .label(CANCEL_LABEL)
381 .on_click(cx.listener(|this, _, window, cx| {
382 this.close_speed_dial_add(window, cx)
383 })),
384 )
385 .child(
386 Button::new("speed-dial-confirm")
387 .label(ADD_LABEL)
388 .when(picked, |button| button.primary())
389 .disabled(!picked)
390 .on_click(cx.listener(|this, _, window, cx| {
391 this.confirm_speed_dial_add(window, cx)
392 })),
393 ),
394 )
395 }
396
397 fn speed_dial_empty(&self, cx: &mut Context<Self>) -> Div {
398 v_flex()
399 .flex_1()
400 .items_center()
401 .justify_center()
402 .gap(px(12.))
403 .child(
404 div()
405 .text_size(px(13.))
406 .text_color(theme::text_muted())
407 .child(SPEED_DIAL_EMPTY),
408 )
409 .child(
410 Button::new("speed-dial-empty-add")
411 .label(ADD_LABEL)
412 .on_click(cx.listener(|this, _, window, cx| {
413 this.open_speed_dial_add(window, cx)
414 })),
415 )
416 }
417
418 fn speed_dial_column(&self, cx: &mut Context<Self>) -> Div {
419 let state = self.app.read(cx);
420 let rows = state
421 .speed_dial
422 .iter()
423 .map(|entry| speed_dial_row(&self.app, entry, &state.directory))
424 .collect::<Vec<_>>();
425 let body = if self.adding_speed_dial {
426 div()
427 .p(px(PAGE_PADDING))
428 .child(self.speed_dial_adder(cx))
429 } else if rows.is_empty() {
430 self.speed_dial_empty(cx)
431 } else {
432 div().child(
433 div()
434 .id("calls-speed-dial-scroll")
435 .size_full()
436 .overflow_y_scrollbar()
437 .child(
438 v_flex()
439 .px(px(PAGE_PADDING - 10.))
440 .py(px(8.))
441 .children(rows),
442 ),
443 )
444 };
445 v_flex()
446 .debug_selector(|| CALLS_SPEED_DIAL_TAG.into())
447 .w(px(SPEED_DIAL_WIDTH))
448 .h_full()
449 .flex_none()
450 .child(
451 h_flex()
452 .h(px(HISTORY_HEADER_HEIGHT))
453 .flex_none()
454 .pl(px(PAGE_PADDING))
455 .pr(px(PAGE_PADDING - 6.))
456 .items_center()
457 .justify_between()
458 .child(
459 div()
460 .text_size(px(14.))
461 .font_weight(FontWeight::SEMIBOLD)
462 .text_color(theme::text())
463 .child(SPEED_DIAL_TITLE),
464 )
465 .child(
466 div()
467 .id("speed-dial-add")
468 .size(px(28.))
469 .flex()
470 .items_center()
471 .justify_center()
472 .rounded(px(6.))
473 .cursor_pointer()
474 .hover(|button| button.bg(theme::row_hover()))
475 .tooltip(|window, cx| {
476 gpui_kit::component::tooltip::Tooltip::new(SPEED_DIAL_ADD_TITLE)
477 .build(window, cx)
478 })
479 .child(symbol("fluent_person_add", 18., theme::text_soft()))
480 .on_click(cx.listener(|this, _, window, cx| {
481 this.open_speed_dial_add(window, cx)
482 })),
483 ),
484 )
485 .child(body.flex_1().min_h_0())
486 }
487
488 fn dial_column(&self, cx: &mut Context<Self>) -> Div {
489 let picked = self.people.read(cx).chips().len();
490 let callable = picked == 1;
491 v_flex()
492 .debug_selector(|| CALLS_DIAL_TAG.into())
493 .w(px(DIAL_WIDTH))
494 .h_full()
495 .flex_none()
496 .p(px(PAGE_PADDING))
497 .gap(px(12.))
498 .child(self.people.clone())
499 .child(
500 Button::new("calls-dial-call")
501 .w_full()
502 .h(px(CALL_BUTTON_HEIGHT))
503 .icon(IconName::Phone)
504 .label(CALL_LABEL)
505 .when(callable, |button| button.primary())
506 .disabled(!callable)
507 .when(picked > 1, |button| button.tooltip(CALL_ONE_PERSON_HINT))
508 .on_click(cx.listener(|this, _, window, cx| this.call_picked(window, cx))),
509 )
510 }
511
512 fn history_list(&self, window_height: f32, cx: &mut Context<Self>) -> Div {
513 let now = Utc::now();
514 let offset = Local::now().offset().fix();
515 if self.app.read(cx).calls_list.needs_rebuild(now, offset) {
516 let app = self.app.clone();
517 cx.defer(move |cx| app.update(cx, |state, cx| state.rebuild_calls_list(cx)));
518 }
519 let top = -f32::from(scroll_offset(&self.scroll).y);
520 let measured = f32::from(self.scroll.bounds().size.height);
521 let height = if measured > 1. {
522 measured
523 } else {
524 window_height.max(FALLBACK_VIEWPORT)
525 };
526 let state = self.app.read(cx);
527 let visible = state
528 .calls_list
529 .window(top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
530 let mut list = v_flex().w_full().px(px(PAGE_PADDING - 10.)).py(px(8.));
531 if visible.before > 0. {
532 list = list.child(div().h(px(visible.before)).flex_none());
533 }
534 for entry in &state.calls_list.entries()[visible.range.clone()] {
535 list = list.child(match entry {
536 ListEntry::Empty(text) => empty_row(text),
537 ListEntry::Call(row) => call_row(
538 &self.app,
539 row,
540 state.can_open_chat_of_call_row(row),
541 row.user_id
542 .as_deref()
543 .is_some_and(|user_id| state.on_speed_dial(user_id)),
544 &state.directory,
545 ),
546 });
547 }
548 if visible.after > 0. {
549 list = list.child(div().h(px(visible.after)).flex_none());
550 }
551 list
552 }
553
554 fn history_column(&self, window_height: f32, cx: &mut Context<Self>) -> Div {
555 let filter = self.app.read(cx).calls_list.filter();
556 v_flex()
557 .debug_selector(|| CALLS_HISTORY_TAG.into())
558 .flex_1()
559 .min_w_0()
560 .h_full()
561 .child(
562 h_flex()
563 .h(px(HISTORY_HEADER_HEIGHT))
564 .flex_none()
565 .px(px(PAGE_PADDING))
566 .gap(px(16.))
567 .items_center()
568 .child(
569 div()
570 .text_size(px(14.))
571 .font_weight(FontWeight::SEMIBOLD)
572 .text_color(theme::text())
573 .child(HISTORY_TITLE),
574 )
575 .child(history_filters(&self.app, filter)),
576 )
577 .child(with_top_edge(
578 div()
579 .id("calls-history-scroll")
580 .flex_1()
581 .min_h_0()
582 .overflow_y_scrollbar()
583 .track_scroll(&self.scroll)
584 .child(self.history_list(window_height, cx)),
585 &self.scroll,
586 CALLS_SCROLL_DIVIDER,
587 ))
588 }
589}
590
591impl Render for CallsPage {
592 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
593 let window_height = f32::from(window.viewport_size().height);
594 let window_width = f32::from(window.viewport_size().width);
595 v_flex()
596 .debug_selector(|| "shell/islands/main".into())
597 .flex_1()
598 .w_full()
599 .min_w_0()
600 .h_full()
601 .rounded(px(10.))
602 .bg(theme::surface())
603 .overflow_hidden()
604 .child(
605 h_flex()
606 .h(px(HEADER_HEIGHT))
607 .flex_none()
608 .px(px(PAGE_PADDING))
609 .items_center()
610 .child(
611 div()
612 .text_size(px(16.))
613 .font_weight(FontWeight::BOLD)
614 .text_color(theme::text())
615 .child(CALLS_TITLE),
616 ),
617 )
618 .child(div().w_full().h(px(1.)).flex_none().bg(theme::border()))
619 .child(
620 h_flex()
621 .flex_1()
622 .min_h_0()
623 .w_full()
624 .child(self.dial_column(cx))
625 .child(div().w(px(1.)).h_full().flex_none().bg(theme::border()))
626 .child(self.history_column(window_height, cx))
627 .when(shows_speed_dial(window_width), |columns| {
628 columns
629 .child(div().w(px(1.)).h_full().flex_none().bg(theme::border()))
630 .child(self.speed_dial_column(cx))
631 }),
632 )
633 }
634}