BlueGit

ict rusty-teams

Files

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

calls_page.rs

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