BlueGit

ict rusty-teams

Files

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

call_organizer.rs

call_organizer.rs 22.7 KiB Download Raw
1use std::sync::Arc;
2use std::time::Instant;
3
4use calling::{CaptionState, HoldState, VideoKey};
5use gpui_kit::assets::IconName;
6use gpui_kit::component::button::{Button, ButtonVariants as _};
7use gpui_kit::component::dialog::DialogFooter;
8use gpui_kit::component::menu::{ContextMenuExt as _, DropdownMenu as _, PopupMenu, PopupMenuItem};
9use gpui_kit::component::popover::Popover;
10use gpui_kit::component::{Sizable as _, WindowExt as _, h_flex, v_flex};
11use gpui_kit::prelude::FluentBuilder as _;
12use gpui_kit::*;
13
14use super::avatar::person_avatar;
15use super::call_extras::{
16 BackgroundMenu, background_items, confirm_stop_recording, open_transfer_picker,
17};
18use super::call_view::{BANNER_HEIGHT, CONTROL_SIZE, MENU_WIDTH, round_control};
19use super::widgets::icon;
20use crate::app_state::AppState;
21use crate::call::{CAPTION_SHOWN, CallModel, Focus};
22use crate::notice::truncated;
23use crate::theme;
24
25const BANNER_TEXT: f32 = 13.;
26const PILL_HEIGHT: f32 = 26.;
27const LOBBY_LIST_WIDTH: f32 = 340.;
28const LOBBY_NAME_LIMIT: usize = 28;
29const CAPTION_WIDTH: f32 = 720.;
30const CAPTION_BOTTOM: f32 = 2. * 16. + CONTROL_SIZE + 16.;
31const CAPTION_FADE_SECONDS: f32 = 1.;
32const CAPTION_BACKGROUND: f32 = 0.72;
33const FOCUS_RADIUS: f32 = 12.;
34const FOCUS_AVATAR: f32 = 140.;
35const CHIP_HEIGHT: f32 = 24.;
36const MORE_SIZE: f32 = 28.;
37const CAMERA_MENU_WIDTH: f32 = 28.;
38const NAME_LIMIT: usize = 32;
39
40fn dark_text() -> Hsla {
41 hsla(0., 0., 0.08, 1.)
42}
43
44pub(super) fn pill(id: impl Into<ElementId>, label: &'static str, filled: bool) -> Stateful<Div> {
45 let (background, foreground) = if filled {
46 (dark_text(), theme::white())
47 } else {
48 (dark_text().opacity(0.12), dark_text())
49 };
50 div()
51 .id(id)
52 .h(px(PILL_HEIGHT))
53 .px(px(12.))
54 .flex()
55 .flex_none()
56 .items_center()
57 .rounded_full()
58 .bg(background)
59 .text_color(foreground)
60 .text_size(px(BANNER_TEXT))
61 .font_weight(FontWeight::SEMIBOLD)
62 .cursor_pointer()
63 .hover(|button| button.opacity(0.85))
64 .child(label)
65}
66
67pub fn lobby_text(waiting: usize) -> String {
68 match waiting {
69 1 => "1 person is waiting in the lobby".to_owned(),
70 count => format!("{count} people are waiting in the lobby"),
71 }
72}
73
74fn lobby_list(app: &Entity<AppState>, guests: &[(String, String)]) -> Div {
75 v_flex()
76 .w(px(LOBBY_LIST_WIDTH))
77 .gap(px(6.))
78 .children(guests.iter().map(|(mri, name)| {
79 let (admit_app, deny_app) = (app.clone(), app.clone());
80 let (admit_mri, deny_mri) = (mri.clone(), mri.clone());
81 h_flex()
82 .w_full()
83 .gap(px(8.))
84 .items_center()
85 .child(
86 div()
87 .flex_1()
88 .min_w_0()
89 .truncate()
90 .text_size(px(14.))
91 .child(truncated(name, LOBBY_NAME_LIMIT)),
92 )
93 .child(
94 Button::new(SharedString::from(format!("lobby-admit-{mri}")))
95 .label("Admit")
96 .on_click(move |_, _, cx| {
97 admit_app.update(cx, |state, cx| {
98 state.admit_call_guest(admit_mri.clone(), cx)
99 })
100 }),
101 )
102 .child(
103 Button::new(SharedString::from(format!("lobby-deny-{mri}")))
104 .label("Deny")
105 .ghost()
106 .on_click(move |_, _, cx| {
107 deny_app
108 .update(cx, |state, cx| state.deny_call_guest(deny_mri.clone(), cx))
109 }),
110 )
111 }))
112}
113
114pub fn lobby_banner(app: &Entity<AppState>, model: &CallModel) -> Option<AnyElement> {
115 if !model.can_admit() {
116 return None;
117 }
118 let guests: Vec<(String, String)> = model
119 .lobby_guests()
120 .into_iter()
121 .map(|tile| (tile.mri.clone(), tile.name.clone()))
122 .collect();
123 if guests.is_empty() {
124 return None;
125 }
126 let list_app = app.clone();
127 let admit_app = app.clone();
128 Some(
129 h_flex()
130 .id("call-lobby-banner")
131 .w_full()
132 .h(px(BANNER_HEIGHT))
133 .flex_none()
134 .px(px(20.))
135 .gap(px(12.))
136 .items_center()
137 .justify_center()
138 .bg(theme::amber())
139 .text_color(dark_text())
140 .text_size(px(BANNER_TEXT))
141 .font_weight(FontWeight::SEMIBOLD)
142 .child(lobby_text(guests.len()))
143 .child(
144 Popover::new("call-lobby-popover")
145 .anchor(Anchor::TopLeft)
146 .offset(px(8.))
147 .trigger(
148 Button::new("call-lobby-view")
149 .ghost()
150 .p_0()
151 .h(px(PILL_HEIGHT))
152 .rounded_full()
153 .child(pill("call-lobby-view-face", "View", false)),
154 )
155 .content(move |_, _, _| lobby_list(&list_app, &guests)),
156 )
157 .child(
158 pill("call-lobby-admit-all", "Admit all", true).on_click(move |_, _, cx| {
159 admit_app.update(cx, |state, cx| state.admit_all_call_guests(cx))
160 }),
161 )
162 .into_any_element(),
163 )
164}
165
166fn confirm_remove(
167 app: &Entity<AppState>,
168 mri: &str,
169 name: &str,
170 window: &mut Window,
171 cx: &mut App,
172) {
173 let app = app.clone();
174 let mri = mri.to_owned();
175 let heading = format!("Remove {} from the meeting?", truncated(name, NAME_LIMIT));
176 window.open_alert_dialog(cx, move |alert, _, _| {
177 let app = app.clone();
178 let mri = mri.clone();
179 let cancel = Button::new("remove-cancel")
180 .label("Cancel")
181 .on_click(|_, window, cx| window.close_dialog(cx));
182 let remove = Button::new("remove-confirm")
183 .label("Remove")
184 .danger()
185 .on_click(move |_, window, cx| {
186 app.update(cx, |state, cx| {
187 state.remove_call_participant(mri.clone(), cx)
188 });
189 window.close_dialog(cx);
190 });
191 alert
192 .title(heading.clone())
193 .description("They leave the call and can only come back if they join again.")
194 .footer(DialogFooter::new().child(cancel).child(remove))
195 .on_ok(|_, _, _| false)
196 });
197}
198
199pub fn tile_menu(
200 popup: PopupMenu,
201 app: &Entity<AppState>,
202 model: &CallModel,
203 mri: &str,
204 name: &str,
205 own: bool,
206) -> PopupMenu {
207 let pinned = model.is_pinned(mri);
208 let spotlighted = model.is_spotlighted(mri);
209 let organizes = model.organizes_meeting();
210 let raised = !own
211 && model
212 .tiles
213 .iter()
214 .any(|tile| tile.mri == mri && tile.hand.is_some());
215 let muted = model.tiles.iter().any(|tile| tile.mri == mri && tile.muted);
216 let mut popup = popup.min_w(px(MENU_WIDTH));
217 let pin_app = app.clone();
218 let pin_mri = mri.to_owned();
219 popup = popup.item(
220 PopupMenuItem::new(if pinned { "Unpin" } else { "Pin for me" })
221 .icon(IconName::Frame)
222 .on_click(move |_, _, cx| {
223 pin_app.update(cx, |state, cx| state.toggle_call_pin(&pin_mri, cx))
224 }),
225 );
226 if organizes {
227 let (spotlight_app, spotlight_mri) = (app.clone(), mri.to_owned());
228 popup = popup.item(
229 PopupMenuItem::new(if spotlighted {
230 "Stop spotlight"
231 } else {
232 "Spotlight for everyone"
233 })
234 .icon(IconName::Star)
235 .on_click(move |_, _, cx| {
236 spotlight_app.update(cx, |state, cx| {
237 state.toggle_call_spotlight(spotlight_mri.clone(), cx)
238 })
239 }),
240 );
241 }
242 if organizes && !own {
243 let (mute_app, mute_mri) = (app.clone(), mri.to_owned());
244 popup = popup.item(
245 PopupMenuItem::new("Mute")
246 .icon(IconName::MicOff)
247 .disabled(muted)
248 .on_click(move |_, _, cx| {
249 mute_app.update(cx, |state, cx| {
250 state.mute_call_participant(mute_mri.clone(), cx)
251 })
252 }),
253 );
254 }
255 if organizes && raised {
256 let (hand_app, hand_mri) = (app.clone(), mri.to_owned());
257 popup = popup.item(PopupMenuItem::new("Lower hand").on_click(move |_, _, cx| {
258 hand_app.update(cx, |state, cx| state.lower_call_hand(hand_mri.clone(), cx))
259 }));
260 }
261 if organizes && !own {
262 let (remove_app, remove_mri, remove_name) = (app.clone(), mri.to_owned(), name.to_owned());
263 popup = popup.separator().item(
264 PopupMenuItem::new("Remove from meeting...")
265 .icon(IconName::Close)
266 .on_click(move |_, window, cx| {
267 confirm_remove(&remove_app, &remove_mri, &remove_name, window, cx)
268 }),
269 );
270 }
271 popup
272}
273
274fn more_overlay(
275 app: &Entity<AppState>,
276 model: &CallModel,
277 mri: &str,
278 name: &str,
279 own: bool,
280 group: SharedString,
281) -> Div {
282 let (app, model, mri_owned, name) =
283 (app.clone(), model.clone(), mri.to_owned(), name.to_owned());
284 div()
285 .absolute()
286 .top(px(6.))
287 .right(px(38.))
288 .opacity(0.)
289 .group_hover(group, |more| more.opacity(1.))
290 .child(
291 Button::new(SharedString::from(format!("call-tile-more-{mri}")))
292 .icon(IconName::Ellipsis)
293 .ghost()
294 .xsmall()
295 .size(px(MORE_SIZE))
296 .rounded_full()
297 .bg(black().opacity(0.55))
298 .text_color(theme::white())
299 .tooltip("More")
300 .dropdown_menu_with_anchor(Anchor::TopRight, move |popup, _, _| {
301 tile_menu(popup, &app, &model, &mri_owned, &name, own)
302 }),
303 )
304}
305
306/// Adds the right-click menu and a "..." button that shows while the pointer is over the tile.
307pub fn attach_tile_menu(
308 tile: Stateful<Div>,
309 app: &Entity<AppState>,
310 model: &CallModel,
311 mri: &str,
312 name: &str,
313 own: bool,
314) -> AnyElement {
315 let group = SharedString::from(format!("call-tile-group-{mri}"));
316 let (context_app, context_model, context_mri, context_name) =
317 (app.clone(), model.clone(), mri.to_owned(), name.to_owned());
318 tile.group(group.clone())
319 .relative()
320 .child(more_overlay(app, model, mri, name, own, group))
321 .context_menu(move |popup, _, _| {
322 tile_menu(
323 popup,
324 &context_app,
325 &context_model,
326 &context_mri,
327 &context_name,
328 own,
329 )
330 })
331 .into_any_element()
332}
333
334pub fn with_tile_menu(
335 app: &Entity<AppState>,
336 model: &CallModel,
337 mri: &str,
338 name: &str,
339 own: bool,
340 tile: AnyElement,
341) -> AnyElement {
342 let wrapper = div()
343 .id(SharedString::from(format!("call-tile-{mri}")))
344 .flex_none()
345 .child(tile);
346 attach_tile_menu(wrapper, app, model, mri, name, own)
347}
348
349pub fn more_button(app: &Entity<AppState>, model: &CallModel) -> Option<AnyElement> {
350 let in_meeting = model.kind == crate::call::CallKind::Meeting;
351 if !in_meeting && !model.is_one_to_one() {
352 return None;
353 }
354 let app = app.clone();
355 let model = model.clone();
356 Some(
357 Button::new("call-more")
358 .ghost()
359 .p_0()
360 .size(px(CONTROL_SIZE))
361 .rounded_full()
362 .child(round_control(
363 "call-more-face",
364 IconName::Ellipsis,
365 theme::surface_raised(),
366 theme::text(),
367 ))
368 .dropdown_menu_with_anchor(Anchor::BottomRight, move |popup, _, _| {
369 more_menu(popup, &app, &model)
370 })
371 .into_any_element(),
372 )
373}
374
375fn more_menu(popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
376 let popup = popup.min_w(px(MENU_WIDTH));
377 if model.is_one_to_one() {
378 return one_to_one_items(popup, app, model);
379 }
380 meeting_items(popup, app, model)
381}
382
383fn one_to_one_items(mut popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
384 let (hold_app, transfer_app) = (app.clone(), app.clone());
385 let held = model.hold == HoldState::Local;
386 popup = popup.item(
387 PopupMenuItem::new(if held { "Resume" } else { "Hold" })
388 .icon(IconName::Timer)
389 .disabled(!held && !model.can_hold())
390 .on_click(move |_, _, cx| hold_app.update(cx, |state, cx| state.toggle_call_hold(cx))),
391 );
392 let can_transfer = model.can_transfer();
393 popup.item(
394 PopupMenuItem::new("Transfer...")
395 .icon(IconName::Forward)
396 .disabled(!can_transfer)
397 .on_click(move |_, window, cx| open_transfer_picker(&transfer_app, window, cx)),
398 )
399}
400
401fn meeting_items(mut popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
402 let (mute_app, captions_app, record_app, board_app) =
403 (app.clone(), app.clone(), app.clone(), app.clone());
404 let captions_on = model.captions_on();
405 if model.organizes_meeting() {
406 popup = popup.item(
407 PopupMenuItem::new("Mute all")
408 .icon(IconName::MicOff)
409 .on_click(move |_, _, cx| mute_app.update(cx, |state, cx| state.mute_all_call(cx))),
410 );
411 }
412 if model.can_record() {
413 let recording = model.recording;
414 popup = popup.item(
415 PopupMenuItem::new(if recording {
416 "Stop recording"
417 } else {
418 "Start recording"
419 })
420 .icon(IconName::Video)
421 .on_click(move |_, window, cx| {
422 if recording {
423 confirm_stop_recording(&record_app, window, cx);
424 } else {
425 record_app.update(cx, |state, cx| state.set_call_recording(true, cx));
426 }
427 }),
428 );
429 }
430 if model.can_open_whiteboard() {
431 popup = popup.item(
432 PopupMenuItem::new("Whiteboard")
433 .icon(IconName::Frame)
434 .on_click(move |_, _, cx| {
435 board_app.update(cx, |state, cx| state.open_call_whiteboard(cx))
436 }),
437 );
438 }
439 popup.item(
440 PopupMenuItem::new(if captions_on {
441 "Turn off live captions"
442 } else {
443 "Turn on live captions"
444 })
445 .icon(if captions_on {
446 IconName::CaptionsOff
447 } else {
448 IconName::Captions
449 })
450 .on_click(move |_, _, cx| {
451 captions_app.update(cx, |state, cx| state.toggle_call_captions(cx))
452 }),
453 )
454}
455
456pub fn chevron_button(button_id: &'static str, face_id: &'static str) -> Button {
457 Button::new(button_id)
458 .ghost()
459 .p_0()
460 .w(px(CAMERA_MENU_WIDTH))
461 .h(px(super::call_view::CONTROL_SIZE))
462 .rounded_full()
463 .child(
464 div()
465 .id(face_id)
466 .w(px(CAMERA_MENU_WIDTH))
467 .h(px(super::call_view::CONTROL_SIZE))
468 .flex()
469 .items_center()
470 .justify_center()
471 .child(icon(IconName::ChevronUp, 16., theme::text())),
472 )
473}
474
475pub fn camera_menu_button(
476 app: &Entity<AppState>,
477 model: &CallModel,
478 backgrounds: BackgroundMenu,
479) -> Option<AnyElement> {
480 if !model.can_use_camera() {
481 return None;
482 }
483 let app = app.clone();
484 let model = model.clone();
485 Some(
486 chevron_button("call-camera-menu", "call-camera-menu-face")
487 .dropdown_menu_with_anchor(Anchor::BottomLeft, move |popup, _, _| {
488 camera_menu(popup, &app, &model, &backgrounds)
489 })
490 .into_any_element(),
491 )
492}
493
494fn camera_menu(
495 popup: PopupMenu,
496 app: &Entity<AppState>,
497 model: &CallModel,
498 backgrounds: &BackgroundMenu,
499) -> PopupMenu {
500 let camera_app = app.clone();
501 let cameras = super::call_view::device_items(
502 calling::camera_entries(&model.cameras),
503 &model.camera,
504 move |choice, cx| {
505 camera_app.update(cx, |state, cx| state.select_call_camera(choice, cx));
506 },
507 );
508 let mut popup = popup
509 .min_w(px(MENU_WIDTH))
510 .item(PopupMenuItem::label("Camera"));
511 for item in cameras {
512 popup = popup.item(item);
513 }
514 background_items(popup, app, backgrounds)
515}
516
517pub fn captions_overlay(model: &CallModel, now: Instant) -> Option<AnyElement> {
518 let lines = model.visible_captions(now);
519 if lines.is_empty() {
520 return None;
521 }
522 let shown = CAPTION_SHOWN.as_secs_f32();
523 Some(
524 div()
525 .id("call-captions")
526 .absolute()
527 .left_0()
528 .right_0()
529 .bottom(px(CAPTION_BOTTOM))
530 .flex()
531 .justify_center()
532 .px(px(24.))
533 .child(
534 v_flex()
535 .w(px(CAPTION_WIDTH))
536 .max_w_full()
537 .gap(px(4.))
538 .px(px(16.))
539 .py(px(8.))
540 .rounded(px(10.))
541 .bg(hsla(0., 0., 0., CAPTION_BACKGROUND))
542 .text_color(theme::white())
543 .text_size(px(16.))
544 .children(lines.into_iter().map(|line| {
545 let age = now.saturating_duration_since(line.at).as_secs_f32();
546 let fade = ((shown - age) / CAPTION_FADE_SECONDS).clamp(0., 1.);
547 h_flex()
548 .opacity(fade)
549 .gap(px(8.))
550 .items_start()
551 .child(
552 div()
553 .flex_none()
554 .font_weight(FontWeight::BOLD)
555 .child(format!("{}:", line.speaker)),
556 )
557 .child(div().flex_1().min_w_0().child(line.text.clone()))
558 })),
559 )
560 .into_any_element(),
561 )
562}
563
564fn focus_chip(label: &'static str, glyph: IconName, background: Hsla, foreground: Hsla) -> Div {
565 h_flex()
566 .h(px(CHIP_HEIGHT))
567 .px(px(8.))
568 .gap(px(4.))
569 .items_center()
570 .rounded_full()
571 .bg(background)
572 .text_color(foreground)
573 .text_size(px(12.))
574 .font_weight(FontWeight::SEMIBOLD)
575 .child(icon(glyph, 13., foreground))
576 .child(label)
577}
578
579pub fn spotlight_chip() -> Div {
580 focus_chip("Spotlight", IconName::Star, theme::amber(), dark_text())
581}
582
583pub fn pin_chip() -> Div {
584 focus_chip(
585 "Pinned",
586 IconName::Frame,
587 hsla(0., 0., 0., 0.6),
588 theme::white(),
589 )
590}
591
592pub fn render_focus_stage(
593 app: &Entity<AppState>,
594 state: &AppState,
595 focus: &Focus,
596 now: Instant,
597) -> Option<AnyElement> {
598 let call = state.call.as_ref()?;
599 let model = &call.model;
600 let me = state.directory.me.as_ref();
601 let (name, user_id, speaking, muted, key) = if focus.own {
602 (
603 "You".to_owned(),
604 me.map(|person| person.user_id.clone()),
605 model.local_speaking,
606 model.muted,
607 VideoKey::LocalCamera,
608 )
609 } else {
610 let tile = model.tiles.iter().find(|tile| tile.mri == focus.mri)?;
611 (
612 tile.name.clone(),
613 tile.user_id.clone(),
614 model.tile_speaking(tile),
615 tile.muted,
616 VideoKey::Person(tile.mri.clone()),
617 )
618 };
619 let image: Option<Arc<RenderImage>> = call.pictures.live(&key, now);
620 let picture = match image {
621 Some(image) => img(ImageSource::Render(image))
622 .size_full()
623 .object_fit(ObjectFit::Contain)
624 .into_any_element(),
625 None => div()
626 .size_full()
627 .flex()
628 .items_center()
629 .justify_center()
630 .child(person_avatar(
631 &state.directory,
632 user_id.as_deref(),
633 &name,
634 FOCUS_AVATAR,
635 ))
636 .into_any_element(),
637 };
638 let stage = div()
639 .id("call-focus-stage")
640 .relative()
641 .flex_1()
642 .min_w_0()
643 .h_full()
644 .overflow_hidden()
645 .rounded(px(FOCUS_RADIUS))
646 .bg(hsla(0., 0., 0.04, 1.))
647 .border_2()
648 .border_color(if speaking {
649 theme::green()
650 } else {
651 transparent_black()
652 })
653 .child(picture)
654 .child(
655 h_flex()
656 .absolute()
657 .top(px(12.))
658 .left(px(12.))
659 .gap(px(6.))
660 .when(focus.spotlight, |chips| chips.child(spotlight_chip()))
661 .when(focus.pinned, |chips| chips.child(pin_chip())),
662 )
663 .child(
664 h_flex()
665 .absolute()
666 .left(px(12.))
667 .bottom(px(12.))
668 .gap(px(8.))
669 .px(px(8.))
670 .py(px(4.))
671 .rounded(px(6.))
672 .bg(hsla(0., 0., 0., 0.6))
673 .text_color(theme::white())
674 .text_size(px(13.))
675 .font_weight(FontWeight::SEMIBOLD)
676 .child(name.clone())
677 .when(muted, |plate| {
678 plate.child(icon(IconName::MicOff, 14., theme::red()))
679 }),
680 );
681 Some(attach_tile_menu(
682 stage, app, model, &focus.mri, &name, focus.own,
683 ))
684}
685
686pub fn captions_state_hint(model: &CallModel) -> Option<&'static str> {
687 match model.captions {
688 CaptionState::Starting => Some("Starting live captions..."),
689 CaptionState::Failed(_) => Some("Live captions could not start"),
690 CaptionState::On | CaptionState::Off => None,
691 }
692}
693
694pub fn captions_hint_chip(model: &CallModel) -> Option<AnyElement> {
695 let text = captions_state_hint(model)?;
696 Some(
697 div()
698 .id("call-captions-hint")
699 .absolute()
700 .left_0()
701 .right_0()
702 .bottom(px(CAPTION_BOTTOM))
703 .flex()
704 .justify_center()
705 .child(
706 div()
707 .px(px(12.))
708 .py(px(6.))
709 .rounded_full()
710 .bg(hsla(0., 0., 0., CAPTION_BACKGROUND))
711 .text_color(theme::white())
712 .text_size(px(13.))
713 .child(text),
714 )
715 .into_any_element(),
716 )
717}