BlueGit

ict rusty-teams

Files

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

call_organizer.rs

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