1use std::rc::Rc;
2use std::sync::Arc;
3
4use call::{ActiveCall, Room};
5use channel::ChannelStore;
6use client::{User, proto::PeerId};
7use gpui::{
8 AnyElement, Hsla, IntoElement, MouseButton, Path, ScreenCaptureSource, Styled, WeakEntity,
9 canvas, point,
10};
11use gpui::{App, Task, Window};
12use icons::IconName;
13use livekit_client::ConnectionQuality;
14use project::WorktreeSettings;
15use remote_connection::RemoteConnectionModal;
16use rpc::proto::{self};
17use settings::{Settings as _, SettingsLocation};
18use theme::ActiveTheme;
19use ui::{
20 Avatar, AvatarAudioStatusIndicator, ContextMenu, ContextMenuItem, Divider, DividerColor,
21 Facepile, PopoverMenu, SplitButton, SplitButtonStyle, TintColor, Tooltip, prelude::*,
22};
23use util::rel_path::RelPath;
24use workspace::{ParticipantLocation, notifications::DetachAndPromptErr};
25use zed_actions::ShowCallStats;
26
27use crate::TitleBar;
28
29fn format_stat(value: Option<f64>, format: impl Fn(f64) -> String) -> String {
30 match value {
31 Some(v) => format(v),
32 None => "—".to_string(),
33 }
34}
35
36pub fn toggle_screen_sharing(
37 screen: anyhow::Result<Option<Rc<dyn ScreenCaptureSource>>>,
38 window: &mut Window,
39 cx: &mut App,
40) {
41 let call = ActiveCall::global(cx).read(cx);
42 let toggle_screen_sharing = match screen {
43 Ok(screen) => {
44 let Some(room) = call.room().cloned() else {
45 return;
46 };
47
48 room.update(cx, |room, cx| {
49 let clicked_on_currently_shared_screen =
50 room.shared_screen_id().is_some_and(|screen_id| {
51 Some(screen_id)
52 == screen
53 .as_deref()
54 .and_then(|s| s.metadata().ok().map(|meta| meta.id))
55 });
56 let should_unshare_current_screen = room.is_sharing_screen();
57 let unshared_current_screen = should_unshare_current_screen.then(|| {
58 telemetry::event!(
59 "Screen Share Disabled",
60 room_id = room.id(),
61 channel_id = room.channel_id(),
62 );
63 room.unshare_screen(clicked_on_currently_shared_screen || screen.is_none(), cx)
64 });
65 if let Some(screen) = screen {
66 if !should_unshare_current_screen {
67 telemetry::event!(
68 "Screen Share Enabled",
69 room_id = room.id(),
70 channel_id = room.channel_id(),
71 );
72 }
73 cx.spawn(async move |room, cx| {
74 unshared_current_screen.transpose()?;
75 if !clicked_on_currently_shared_screen {
76 room.update(cx, |room, cx| room.share_screen(screen, cx))?
77 .await
78 } else {
79 Ok(())
80 }
81 })
82 } else {
83 Task::ready(Ok(()))
84 }
85 })
86 }
87 Err(e) => Task::ready(Err(e)),
88 };
89 toggle_screen_sharing.detach_and_prompt_err("Sharing Screen Failed", window, cx, |e, _, _| Some(format!("{:?}\n\nPlease check that you have given Zed permissions to record your screen in Settings.", e)));
90}
91
92pub fn toggle_mute(cx: &mut App) {
93 let call = ActiveCall::global(cx).read(cx);
94 if let Some(room) = call.room().cloned() {
95 room.update(cx, |room, cx| {
96 let operation = if room.is_muted() {
97 "Microphone Enabled"
98 } else {
99 "Microphone Disabled"
100 };
101 telemetry::event!(
102 operation,
103 room_id = room.id(),
104 channel_id = room.channel_id(),
105 );
106
107 room.toggle_mute(cx)
108 });
109 }
110}
111
112pub fn toggle_deafen(cx: &mut App) {
113 if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
114 room.update(cx, |room, cx| room.toggle_deafen(cx));
115 }
116}
117
118fn render_color_ribbon(color: Hsla) -> impl Element {
119 canvas(
120 move |_, _, _| {},
121 move |bounds, _, window, _| {
122 let height = bounds.size.height;
123 let horizontal_offset = height;
124 let vertical_offset = height / 2.0;
125 let mut path = Path::new(bounds.bottom_left());
126 path.curve_to(
127 bounds.origin + point(horizontal_offset, vertical_offset),
128 bounds.origin + point(px(0.0), vertical_offset),
129 );
130 path.line_to(bounds.top_right() + point(-horizontal_offset, vertical_offset));
131 path.curve_to(
132 bounds.bottom_right(),
133 bounds.top_right() + point(px(0.0), vertical_offset),
134 );
135 path.line_to(bounds.bottom_left());
136 window.paint_path(path, color);
137 },
138 )
139 .h_1()
140 .w_full()
141}
142
143impl TitleBar {
144 pub(crate) fn render_collaborator_list(
145 &self,
146 _: &mut Window,
147 cx: &mut Context<Self>,
148 ) -> impl IntoElement {
149 let room = ActiveCall::global(cx).read(cx).room().cloned();
150 let current_user = self.user_store.read(cx).current_user();
151 let client = self.client.clone();
152 let project_id = self.project.read(cx).remote_id();
153 let workspace = self.workspace.upgrade();
154
155 h_flex()
156 .id("collaborator-list")
157 .w_full()
158 .gap_1()
159 .overflow_x_scroll()
160 .when_some(
161 current_user.zip(client.peer_id()).zip(room),
162 |this, ((current_user, peer_id), room)| {
163 let player_colors = cx.theme().players();
164 let room = room.read(cx);
165 let mut remote_participants =
166 room.remote_participants().values().collect::<Vec<_>>();
167 remote_participants.sort_by_key(|p| p.participant_index.0);
168
169 let current_user_face_pile = self.render_collaborator(
170 ¤t_user,
171 peer_id,
172 true,
173 room.is_speaking(),
174 room.is_muted(),
175 None,
176 room,
177 project_id,
178 ¤t_user,
179 cx,
180 );
181
182 this.children(current_user_face_pile.map(|face_pile| {
183 v_flex()
184 .on_mouse_down(MouseButton::Left, |_, window, _| {
185 window.prevent_default()
186 })
187 .child(face_pile)
188 .child(render_color_ribbon(player_colors.local().cursor))
189 }))
190 .children(remote_participants.iter().filter_map(|collaborator| {
191 let player_color =
192 player_colors.color_for_participant(collaborator.participant_index.0);
193 let is_following = workspace
194 .as_ref()?
195 .read(cx)
196 .is_being_followed(collaborator.peer_id);
197 let is_present = project_id.is_some_and(|project_id| {
198 collaborator.location
199 == ParticipantLocation::SharedProject { project_id }
200 });
201
202 let facepile = self.render_collaborator(
203 &collaborator.user,
204 collaborator.peer_id,
205 is_present,
206 collaborator.speaking,
207 collaborator.muted,
208 is_following.then_some(player_color.selection),
209 room,
210 project_id,
211 ¤t_user,
212 cx,
213 )?;
214
215 Some(
216 v_flex()
217 .id(("collaborator", collaborator.user.id))
218 .child(facepile)
219 .child(render_color_ribbon(player_color.cursor))
220 .cursor_pointer()
221 .on_mouse_down(MouseButton::Left, |_, window, _| {
222 window.prevent_default()
223 })
224 .on_click({
225 let peer_id = collaborator.peer_id;
226 cx.listener(move |this, _, window, cx| {
227 cx.stop_propagation();
228
229 this.workspace
230 .update(cx, |workspace, cx| {
231 if is_following {
232 workspace.unfollow(peer_id, window, cx);
233 } else {
234 workspace.follow(peer_id, window, cx);
235 }
236 })
237 .ok();
238 })
239 })
240 .occlude()
241 .tooltip({
242 let login = collaborator.user.github_login.clone();
243 Tooltip::text(format!("Follow {login}"))
244 }),
245 )
246 }))
247 },
248 )
249 }
250
251 fn render_collaborator(
252 &self,
253 user: &Arc<User>,
254 peer_id: PeerId,
255 is_present: bool,
256 is_speaking: bool,
257 is_muted: bool,
258 leader_selection_color: Option<Hsla>,
259 room: &Room,
260 project_id: Option<u64>,
261 current_user: &Arc<User>,
262 cx: &App,
263 ) -> Option<Div> {
264 if room.role_for_user(user.id) == Some(proto::ChannelRole::Guest) {
265 return None;
266 }
267
268 const FACEPILE_LIMIT: usize = 3;
269 let followers = project_id.map_or(&[] as &[_], |id| room.followers_for(peer_id, id));
270 let extra_count = followers.len().saturating_sub(FACEPILE_LIMIT);
271
272 Some(
273 div()
274 .m_0p5()
275 .p_0p5()
276 // When the collaborator is not followed, still draw this wrapper div, but leave
277 // it transparent, so that it does not shift the layout when following.
278 .when_some(leader_selection_color, |div, color| {
279 div.rounded_sm().bg(color)
280 })
281 .child(
282 Facepile::empty()
283 .child(
284 Avatar::new(user.avatar_uri.clone())
285 .grayscale(!is_present)
286 .border_color(if is_speaking {
287 cx.theme().status().info
288 } else {
289 // We draw the border in a transparent color rather to avoid
290 // the layout shift that would come with adding/removing the border.
291 gpui::transparent_black()
292 })
293 .when(is_muted, |avatar| {
294 avatar.indicator(
295 AvatarAudioStatusIndicator::new(ui::AudioStatus::Muted)
296 .tooltip({
297 let github_login = user.github_login.clone();
298 Tooltip::text(format!("{} is muted", github_login))
299 }),
300 )
301 }),
302 )
303 .children(followers.iter().take(FACEPILE_LIMIT).filter_map(
304 |follower_peer_id| {
305 let follower = room
306 .remote_participants()
307 .values()
308 .find_map(|p| {
309 (p.peer_id == *follower_peer_id).then_some(&p.user)
310 })
311 .or_else(|| {
312 (self.client.peer_id() == Some(*follower_peer_id))
313 .then_some(current_user)
314 })?
315 .clone();
316
317 Some(div().mt(-px(4.)).child(
318 Avatar::new(follower.avatar_uri.clone()).size(rems(0.75)),
319 ))
320 },
321 ))
322 .children(if extra_count > 0 {
323 Some(
324 Label::new(format!("+{extra_count}"))
325 .ml_1()
326 .into_any_element(),
327 )
328 } else {
329 None
330 }),
331 ),
332 )
333 }
334
335 pub(crate) fn render_call_controls(
336 &self,
337 window: &mut Window,
338 cx: &mut Context<Self>,
339 ) -> Vec<AnyElement> {
340 let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() else {
341 return Vec::new();
342 };
343
344 let is_connecting_to_project = self
345 .workspace
346 .update(cx, |workspace, cx| {
347 workspace
348 .active_modal::<RemoteConnectionModal>(cx)
349 .is_some()
350 })
351 .unwrap_or(false);
352
353 let room = room.read(cx);
354 let project = self.project.read(cx);
355 let is_local = project.is_local() || project.is_via_remote_server();
356 let is_shared = is_local && project.is_shared();
357 let is_muted = room.is_muted();
358 let muted_by_user = room.muted_by_user();
359 let is_deafened = room.is_deafened().unwrap_or(false);
360 let is_screen_sharing = room.is_sharing_screen();
361 let can_use_microphone = room.can_use_microphone();
362 let can_share_projects = room.can_share_projects();
363 let screen_sharing_supported = cx.is_screen_capture_supported();
364
365 let stats = room
366 .diagnostics()
367 .map(|d| d.read(cx).stats().clone())
368 .unwrap_or_default();
369
370 let channel_store = ChannelStore::global(cx);
371 let channel = room
372 .channel_id()
373 .and_then(|channel_id| channel_store.read(cx).channel_for_id(channel_id).cloned());
374
375 let mut children = Vec::new();
376
377 let effective_quality = stats.effective_quality.unwrap_or(ConnectionQuality::Lost);
378 let (signal_icon, signal_color, quality_label) = match effective_quality {
379 ConnectionQuality::Excellent => {
380 (IconName::SignalHigh, Some(Color::Success), "Excellent")
381 }
382 ConnectionQuality::Good => (IconName::SignalHigh, None, "Good"),
383 ConnectionQuality::Poor => (IconName::SignalMedium, Some(Color::Warning), "Poor"),
384 ConnectionQuality::Lost => (IconName::SignalLow, Some(Color::Error), "Lost"),
385 };
386
387 let quality_label: SharedString = quality_label.into();
388
389 children.push(
390 h_flex()
391 .gap_1()
392 .child(
393 IconButton::new("leave-call", IconName::Exit)
394 .style(ButtonStyle::Subtle)
395 .tooltip(Tooltip::text("Leave Call"))
396 .icon_size(IconSize::Small)
397 .on_click(move |_, _window, cx| {
398 ActiveCall::global(cx)
399 .update(cx, |call, cx| call.hang_up(cx))
400 .detach_and_log_err(cx);
401 }),
402 )
403 .child(Divider::vertical().color(DividerColor::Border))
404 .into_any_element(),
405 );
406
407 children.push(
408 IconButton::new("call-quality", signal_icon)
409 .icon_size(IconSize::Small)
410 .when_some(signal_color, |button, color| button.icon_color(color))
411 .tooltip(move |_window, cx| {
412 let quality_label = quality_label.clone();
413 let latency = format_stat(stats.latency_ms, |v| format!("{:.0}ms", v));
414 let jitter = format_stat(stats.jitter_ms, |v| format!("{:.0}ms", v));
415 let packet_loss = format_stat(stats.packet_loss_pct, |v| format!("{:.1}%", v));
416 let input_lag =
417 format_stat(stats.input_lag.map(|d| d.as_secs_f64() * 1000.0), |v| {
418 format!("{:.1}ms", v)
419 });
420
421 Tooltip::with_meta(
422 format!("Connection: {quality_label}"),
423 Some(&ShowCallStats),
424 format!(
425 "Latency: {latency} · Jitter: {jitter} · Loss: {packet_loss} · Input lag: {input_lag}",
426 ),
427 cx,
428 )
429 })
430 .on_click(move |_, window, cx| {
431 window.dispatch_action(Box::new(ShowCallStats), cx);
432 })
433 .into_any_element(),
434 );
435
436 if is_local && can_share_projects && !is_connecting_to_project {
437 let is_sharing_disabled = channel.is_some_and(|channel| match channel.visibility {
438 proto::ChannelVisibility::Public => project.visible_worktrees(cx).any(|worktree| {
439 let worktree_id = worktree.read(cx).id();
440
441 let settings_location = Some(SettingsLocation {
442 worktree_id,
443 path: RelPath::empty(),
444 });
445
446 WorktreeSettings::get(settings_location, cx).prevent_sharing_in_public_channels
447 }),
448 proto::ChannelVisibility::Members => false,
449 });
450
451 children.push(
452 Button::new(
453 "toggle_sharing",
454 if is_shared { "Unshare" } else { "Share" },
455 )
456 .tooltip(Tooltip::text(if is_shared {
457 "Stop sharing project with call participants"
458 } else {
459 "Share project with call participants"
460 }))
461 .style(ButtonStyle::Subtle)
462 .selected_style(ButtonStyle::Tinted(TintColor::Accent))
463 .toggle_state(is_shared)
464 .label_size(LabelSize::Small)
465 .when(is_sharing_disabled, |parent| {
466 parent.disabled(true).tooltip(Tooltip::text(
467 "This project may not be shared in a public channel.",
468 ))
469 })
470 .on_click(cx.listener(move |this, _, window, cx| {
471 if is_shared {
472 this.unshare_project(window, cx);
473 } else {
474 this.share_project(cx);
475 }
476 }))
477 .into_any_element(),
478 );
479 }
480
481 if can_use_microphone {
482 children.push(
483 IconButton::new(
484 "mute-microphone",
485 if is_muted {
486 IconName::MicMute
487 } else {
488 IconName::Mic
489 },
490 )
491 .tooltip(move |_window, cx| {
492 if is_muted {
493 if is_deafened {
494 Tooltip::with_meta(
495 "Unmute Microphone",
496 None,
497 "Audio will be unmuted",
498 cx,
499 )
500 } else {
501 Tooltip::simple("Unmute Microphone", cx)
502 }
503 } else {
504 Tooltip::simple("Mute Microphone", cx)
505 }
506 })
507 .style(ButtonStyle::Subtle)
508 .icon_size(IconSize::Small)
509 .toggle_state(is_muted)
510 .selected_style(ButtonStyle::Tinted(TintColor::Error))
511 .on_click(move |_, _window, cx| toggle_mute(cx))
512 .into_any_element(),
513 );
514 }
515
516 children.push(
517 IconButton::new(
518 "mute-sound",
519 if is_deafened {
520 IconName::AudioOff
521 } else {
522 IconName::AudioOn
523 },
524 )
525 .style(ButtonStyle::Subtle)
526 .selected_style(ButtonStyle::Tinted(TintColor::Error))
527 .icon_size(IconSize::Small)
528 .toggle_state(is_deafened)
529 .tooltip(move |_window, cx| {
530 if is_deafened {
531 let label = "Unmute Audio";
532
533 if !muted_by_user {
534 Tooltip::with_meta(label, None, "Microphone will be unmuted", cx)
535 } else {
536 Tooltip::simple(label, cx)
537 }
538 } else {
539 let label = "Mute Audio";
540
541 if !muted_by_user {
542 Tooltip::with_meta(label, None, "Microphone will be muted", cx)
543 } else {
544 Tooltip::simple(label, cx)
545 }
546 }
547 })
548 .on_click(move |_, _, cx| toggle_deafen(cx))
549 .into_any_element(),
550 );
551
552 if can_use_microphone && screen_sharing_supported {
553 #[cfg(target_os = "linux")]
554 let is_wayland = gpui::guess_compositor() == "Wayland";
555 #[cfg(not(target_os = "linux"))]
556 let is_wayland = false;
557
558 let trigger = IconButton::new("screen-share", IconName::Screen)
559 .style(ButtonStyle::Subtle)
560 .icon_size(IconSize::Small)
561 .toggle_state(is_screen_sharing)
562 .selected_style(ButtonStyle::Tinted(TintColor::Accent))
563 .tooltip(Tooltip::text(if is_screen_sharing {
564 "Stop Sharing Screen"
565 } else {
566 "Share Screen"
567 }))
568 .on_click(move |_, window, cx| {
569 let should_share = ActiveCall::global(cx)
570 .read(cx)
571 .room()
572 .is_some_and(|room| !room.read(cx).is_sharing_screen());
573
574 #[cfg(target_os = "linux")]
575 {
576 if is_wayland
577 && let Some(room) = ActiveCall::global(cx).read(cx).room().cloned()
578 {
579 let task = room.update(cx, |room, cx| {
580 if should_share {
581 room.share_screen_wayland(cx)
582 } else {
583 room.unshare_screen(true, cx)
584 .map(|()| Task::ready(Ok(())))
585 .unwrap_or_else(|e| Task::ready(Err(e)))
586 }
587 });
588 task.detach_and_prompt_err(
589 "Sharing Screen Failed",
590 window,
591 cx,
592 |e, _, _| Some(format!("{e:?}")),
593 );
594 }
595 }
596 if !is_wayland {
597 window
598 .spawn(cx, async move |cx| {
599 let screen = if should_share {
600 cx.update(|_, cx| pick_default_screen(cx))?.await
601 } else {
602 Ok(None)
603 };
604 cx.update(|window, cx| toggle_screen_sharing(screen, window, cx))?;
605
606 Result::<_, anyhow::Error>::Ok(())
607 })
608 .detach();
609 }
610 });
611
612 if is_wayland {
613 children.push(trigger.into_any_element());
614 } else {
615 children.push(
616 SplitButton::new(
617 trigger.render(window, cx),
618 self.render_screen_list().into_any_element(),
619 )
620 .style(SplitButtonStyle::Transparent)
621 .into_any_element(),
622 );
623 }
624 }
625
626 children.push(div().pr_2().into_any_element());
627
628 children
629 }
630
631 fn render_screen_list(&self) -> impl IntoElement {
632 PopoverMenu::new("screen-share-screen-list")
633 .with_handle(self.screen_share_popover_handle.clone())
634 .trigger(
635 ui::ButtonLike::new_rounded_right("screen-share-screen-list-trigger")
636 .child(
637 h_flex()
638 .mx_neg_0p5()
639 .h_full()
640 .justify_center()
641 .child(Icon::new(IconName::ChevronDown).size(IconSize::XSmall)),
642 )
643 .toggle_state(self.screen_share_popover_handle.is_deployed()),
644 )
645 .menu(|window, cx| {
646 let screens = cx.screen_capture_sources();
647 Some(ContextMenu::build(window, cx, |context_menu, _, cx| {
648 cx.spawn(async move |this: WeakEntity<ContextMenu>, cx| {
649 let screens = screens.await??;
650 this.update(cx, |this, cx| {
651 let active_screenshare_id = ActiveCall::global(cx)
652 .read(cx)
653 .room()
654 .and_then(|room| room.read(cx).shared_screen_id());
655 for screen in screens {
656 let Ok(meta) = screen.metadata() else {
657 continue;
658 };
659
660 let label = meta
661 .label
662 .clone()
663 .unwrap_or_else(|| SharedString::from("Unknown screen"));
664 let resolution = SharedString::from(format!(
665 "{} × {}",
666 meta.resolution.width.0, meta.resolution.height.0
667 ));
668 this.push_item(ContextMenuItem::CustomEntry {
669 entry_render: Box::new(move |_, _| {
670 h_flex()
671 .gap_2()
672 .child(
673 Icon::new(IconName::Screen)
674 .size(IconSize::XSmall)
675 .map(|this| {
676 if active_screenshare_id == Some(meta.id) {
677 this.color(Color::Accent)
678 } else {
679 this.color(Color::Muted)
680 }
681 }),
682 )
683 .child(Label::new(label.clone()))
684 .child(
685 Label::new(resolution.clone())
686 .color(Color::Muted)
687 .size(LabelSize::Small),
688 )
689 .into_any()
690 }),
691 selectable: true,
692 documentation_aside: None,
693 handler: Rc::new(move |_, window, cx| {
694 toggle_screen_sharing(Ok(Some(screen.clone())), window, cx);
695 }),
696 });
697 }
698 })
699 })
700 .detach_and_log_err(cx);
701 context_menu
702 }))
703 })
704 }
705}
706
707/// Picks the screen to share when clicking on the main screen sharing button.
708fn pick_default_screen(cx: &App) -> Task<anyhow::Result<Option<Rc<dyn ScreenCaptureSource>>>> {
709 let source = cx.screen_capture_sources();
710 cx.spawn(async move |_| {
711 let available_sources = source.await??;
712 Ok(available_sources
713 .iter()
714 .find(|it| {
715 it.as_ref()
716 .metadata()
717 .is_ok_and(|meta| meta.is_main.unwrap_or_default())
718 })
719 .or_else(|| available_sources.first())
720 .cloned())
721 })
722}