BlueGit

ict rusty-teams

Files

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

call_organizer.rs

call_organizer.rs 23.2 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 let popup = if model.is_one_to_one() {
378 one_to_one_items(popup, app, model)
379 } else {
380 meeting_items(popup, app, model)
381 };
382 incoming_video_item(popup, app, model)
383}
384
385fn incoming_video_item(popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
386 if model.health.incoming_video {
387 return popup;
388 }
389 let app = app.clone();
390 popup.item(
391 PopupMenuItem::new("Turn on incoming video")
392 .icon(IconName::Video)
393 .on_click(move |_, _, cx| {
394 app.update(cx, |state, cx| state.set_call_incoming_video(true, cx))
395 }),
396 )
397}
398
399fn one_to_one_items(mut popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
400 let (hold_app, transfer_app) = (app.clone(), app.clone());
401 let held = model.hold == HoldState::Local;
402 popup = popup.item(
403 PopupMenuItem::new(if held { "Resume" } else { "Hold" })
404 .icon(IconName::Timer)
405 .disabled(!held && !model.can_hold())
406 .on_click(move |_, _, cx| hold_app.update(cx, |state, cx| state.toggle_call_hold(cx))),
407 );
408 let can_transfer = model.can_transfer();
409 popup.item(
410 PopupMenuItem::new("Transfer...")
411 .icon(IconName::Forward)
412 .disabled(!can_transfer)
413 .on_click(move |_, window, cx| open_transfer_picker(&transfer_app, window, cx)),
414 )
415}
416
417fn meeting_items(mut popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
418 let (mute_app, captions_app, record_app, board_app) =
419 (app.clone(), app.clone(), app.clone(), app.clone());
420 let captions_on = model.captions_on();
421 if model.organizes_meeting() {
422 popup = popup.item(
423 PopupMenuItem::new("Mute all")
424 .icon(IconName::MicOff)
425 .on_click(move |_, _, cx| mute_app.update(cx, |state, cx| state.mute_all_call(cx))),
426 );
427 }
428 if model.can_record() {
429 let recording = model.recording;
430 popup = popup.item(
431 PopupMenuItem::new(if recording {
432 "Stop recording"
433 } else {
434 "Start recording"
435 })
436 .icon(IconName::Video)
437 .on_click(move |_, window, cx| {
438 if recording {
439 confirm_stop_recording(&record_app, window, cx);
440 } else {
441 record_app.update(cx, |state, cx| state.set_call_recording(true, cx));
442 }
443 }),
444 );
445 }
446 if model.can_open_whiteboard() {
447 popup = popup.item(
448 PopupMenuItem::new("Whiteboard")
449 .icon(IconName::Frame)
450 .on_click(move |_, _, cx| {
451 board_app.update(cx, |state, cx| state.open_call_whiteboard(cx))
452 }),
453 );
454 }
455 popup.item(
456 PopupMenuItem::new(if captions_on {
457 "Turn off live captions"
458 } else {
459 "Turn on live captions"
460 })
461 .icon(if captions_on {
462 IconName::CaptionsOff
463 } else {
464 IconName::Captions
465 })
466 .on_click(move |_, _, cx| {
467 captions_app.update(cx, |state, cx| state.toggle_call_captions(cx))
468 }),
469 )
470}
471
472pub fn chevron_button(button_id: &'static str, face_id: &'static str) -> Button {
473 Button::new(button_id)
474 .ghost()
475 .p_0()
476 .w(px(CAMERA_MENU_WIDTH))
477 .h(px(super::call_view::CONTROL_SIZE))
478 .rounded_full()
479 .child(
480 div()
481 .id(face_id)
482 .w(px(CAMERA_MENU_WIDTH))
483 .h(px(super::call_view::CONTROL_SIZE))
484 .flex()
485 .items_center()
486 .justify_center()
487 .child(icon(IconName::ChevronUp, 16., theme::text())),
488 )
489}
490
491pub fn camera_menu_button(
492 app: &Entity<AppState>,
493 model: &CallModel,
494 backgrounds: BackgroundMenu,
495) -> Option<AnyElement> {
496 if !model.can_use_camera() {
497 return None;
498 }
499 let app = app.clone();
500 let model = model.clone();
501 Some(
502 chevron_button("call-camera-menu", "call-camera-menu-face")
503 .dropdown_menu_with_anchor(Anchor::BottomLeft, move |popup, _, _| {
504 camera_menu(popup, &app, &model, &backgrounds)
505 })
506 .into_any_element(),
507 )
508}
509
510fn camera_menu(
511 popup: PopupMenu,
512 app: &Entity<AppState>,
513 model: &CallModel,
514 backgrounds: &BackgroundMenu,
515) -> PopupMenu {
516 let camera_app = app.clone();
517 let cameras = super::call_view::device_items(
518 calling::camera_entries(&model.cameras),
519 &model.camera,
520 move |choice, cx| {
521 camera_app.update(cx, |state, cx| state.select_call_camera(choice, cx));
522 },
523 );
524 let mut popup = popup
525 .min_w(px(MENU_WIDTH))
526 .item(PopupMenuItem::label("Camera"));
527 for item in cameras {
528 popup = popup.item(item);
529 }
530 background_items(popup, app, backgrounds)
531}
532
533pub fn captions_overlay(model: &CallModel, now: Instant) -> Option<AnyElement> {
534 let lines = model.visible_captions(now);
535 if lines.is_empty() {
536 return None;
537 }
538 let shown = CAPTION_SHOWN.as_secs_f32();
539 Some(
540 div()
541 .id("call-captions")
542 .absolute()
543 .left_0()
544 .right_0()
545 .bottom(px(CAPTION_BOTTOM))
546 .flex()
547 .justify_center()
548 .px(px(24.))
549 .child(
550 v_flex()
551 .w(px(CAPTION_WIDTH))
552 .max_w_full()
553 .gap(px(4.))
554 .px(px(16.))
555 .py(px(8.))
556 .rounded(px(10.))
557 .bg(hsla(0., 0., 0., CAPTION_BACKGROUND))
558 .text_color(theme::white())
559 .text_size(px(16.))
560 .children(lines.into_iter().map(|line| {
561 let age = now.saturating_duration_since(line.at).as_secs_f32();
562 let fade = ((shown - age) / CAPTION_FADE_SECONDS).clamp(0., 1.);
563 h_flex()
564 .opacity(fade)
565 .gap(px(8.))
566 .items_start()
567 .child(
568 div()
569 .flex_none()
570 .font_weight(FontWeight::BOLD)
571 .child(format!("{}:", line.speaker)),
572 )
573 .child(div().flex_1().min_w_0().child(line.text.clone()))
574 })),
575 )
576 .into_any_element(),
577 )
578}
579
580fn focus_chip(label: &'static str, glyph: IconName, background: Hsla, foreground: Hsla) -> Div {
581 h_flex()
582 .h(px(CHIP_HEIGHT))
583 .px(px(8.))
584 .gap(px(4.))
585 .items_center()
586 .rounded_full()
587 .bg(background)
588 .text_color(foreground)
589 .text_size(px(12.))
590 .font_weight(FontWeight::SEMIBOLD)
591 .child(icon(glyph, 13., foreground))
592 .child(label)
593}
594
595pub fn spotlight_chip() -> Div {
596 focus_chip("Spotlight", IconName::Star, theme::amber(), dark_text())
597}
598
599pub fn pin_chip() -> Div {
600 focus_chip(
601 "Pinned",
602 IconName::Frame,
603 hsla(0., 0., 0., 0.6),
604 theme::white(),
605 )
606}
607
608pub fn render_focus_stage(
609 app: &Entity<AppState>,
610 state: &AppState,
611 focus: &Focus,
612 now: Instant,
613) -> Option<AnyElement> {
614 let call = state.call.as_ref()?;
615 let model = &call.model;
616 let me = state.directory.me.as_ref();
617 let (name, user_id, speaking, muted, key) = if focus.own {
618 (
619 "You".to_owned(),
620 me.map(|person| person.user_id.clone()),
621 model.local_speaking,
622 model.muted,
623 VideoKey::LocalCamera,
624 )
625 } else {
626 let tile = model.tiles.iter().find(|tile| tile.mri == focus.mri)?;
627 (
628 tile.name.clone(),
629 tile.user_id.clone(),
630 model.tile_speaking(tile),
631 tile.muted,
632 VideoKey::Person(tile.mri.clone()),
633 )
634 };
635 let image: Option<Arc<RenderImage>> = call.pictures.live(&key, now);
636 let picture = match image {
637 Some(image) => img(ImageSource::Render(image))
638 .size_full()
639 .object_fit(ObjectFit::Contain)
640 .into_any_element(),
641 None => div()
642 .size_full()
643 .flex()
644 .items_center()
645 .justify_center()
646 .child(person_avatar(
647 &state.directory,
648 user_id.as_deref(),
649 &name,
650 FOCUS_AVATAR,
651 ))
652 .into_any_element(),
653 };
654 let stage = div()
655 .id("call-focus-stage")
656 .relative()
657 .flex_1()
658 .min_w_0()
659 .h_full()
660 .overflow_hidden()
661 .rounded(px(FOCUS_RADIUS))
662 .bg(hsla(0., 0., 0.04, 1.))
663 .border_2()
664 .border_color(if speaking {
665 theme::green()
666 } else {
667 transparent_black()
668 })
669 .child(picture)
670 .child(
671 h_flex()
672 .absolute()
673 .top(px(12.))
674 .left(px(12.))
675 .gap(px(6.))
676 .when(focus.spotlight, |chips| chips.child(spotlight_chip()))
677 .when(focus.pinned, |chips| chips.child(pin_chip())),
678 )
679 .child(
680 h_flex()
681 .absolute()
682 .left(px(12.))
683 .bottom(px(12.))
684 .gap(px(8.))
685 .px(px(8.))
686 .py(px(4.))
687 .rounded(px(6.))
688 .bg(hsla(0., 0., 0., 0.6))
689 .text_color(theme::white())
690 .text_size(px(13.))
691 .font_weight(FontWeight::SEMIBOLD)
692 .child(name.clone())
693 .when(muted, |plate| {
694 plate.child(icon(IconName::MicOff, 14., theme::red()))
695 }),
696 );
697 Some(attach_tile_menu(
698 stage, app, model, &focus.mri, &name, focus.own,
699 ))
700}
701
702pub fn captions_state_hint(model: &CallModel) -> Option<&'static str> {
703 match model.captions {
704 CaptionState::Starting => Some("Starting live captions..."),
705 CaptionState::Failed(_) => Some("Live captions could not start"),
706 CaptionState::On | CaptionState::Off => None,
707 }
708}
709
710pub fn captions_hint_chip(model: &CallModel) -> Option<AnyElement> {
711 let text = captions_state_hint(model)?;
712 Some(
713 div()
714 .id("call-captions-hint")
715 .absolute()
716 .left_0()
717 .right_0()
718 .bottom(px(CAPTION_BOTTOM))
719 .flex()
720 .justify_center()
721 .child(
722 div()
723 .px(px(12.))
724 .py(px(6.))
725 .rounded_full()
726 .bg(hsla(0., 0., 0., CAPTION_BACKGROUND))
727 .text_color(theme::white())
728 .text_size(px(13.))
729 .child(text),
730 )
731 .into_any_element(),
732 )
733}