collab.rs

  1use std::sync::Arc;
  2
  3use call::{ActiveCall, ParticipantLocation, Room};
  4use client::{proto::PeerId, User};
  5use gpui::{actions, AppContext, Task, WindowContext};
  6use gpui::{canvas, point, AnyElement, Hsla, IntoElement, MouseButton, Path, Styled};
  7use rpc::proto::{self};
  8use theme::ActiveTheme;
  9use ui::{prelude::*, Avatar, AvatarAudioStatusIndicator, Facepile, TintColor, Tooltip};
 10use workspace::notifications::DetachAndPromptErr;
 11
 12use crate::TitleBar;
 13
 14actions!(
 15    collab,
 16    [ToggleScreenSharing, ToggleMute, ToggleDeafen, LeaveCall]
 17);
 18
 19fn toggle_screen_sharing(_: &ToggleScreenSharing, cx: &mut WindowContext) {
 20    let call = ActiveCall::global(cx).read(cx);
 21    if let Some(room) = call.room().cloned() {
 22        let toggle_screen_sharing = room.update(cx, |room, cx| {
 23            if room.is_screen_sharing() {
 24                telemetry::event!(
 25                    "Screen Share Disabled",
 26                    room_id = room.id(),
 27                    channel_id = room.channel_id(),
 28                );
 29                Task::ready(room.unshare_screen(cx))
 30            } else {
 31                telemetry::event!(
 32                    "Screen Share Enabled",
 33                    room_id = room.id(),
 34                    channel_id = room.channel_id(),
 35                );
 36                room.share_screen(cx)
 37            }
 38        });
 39        toggle_screen_sharing.detach_and_prompt_err("Sharing Screen Failed", cx, |e, _| Some(format!("{:?}\n\nPlease check that you have given Zed permissions to record your screen in Settings.", e)));
 40    }
 41}
 42
 43fn toggle_mute(_: &ToggleMute, cx: &mut AppContext) {
 44    let call = ActiveCall::global(cx).read(cx);
 45    if let Some(room) = call.room().cloned() {
 46        room.update(cx, |room, cx| {
 47            let operation = if room.is_muted() {
 48                "Microphone Enabled"
 49            } else {
 50                "Microphone Disabled"
 51            };
 52            telemetry::event!(
 53                operation,
 54                room_id = room.id(),
 55                channel_id = room.channel_id(),
 56            );
 57
 58            room.toggle_mute(cx)
 59        });
 60    }
 61}
 62
 63fn toggle_deafen(_: &ToggleDeafen, cx: &mut AppContext) {
 64    if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
 65        room.update(cx, |room, cx| room.toggle_deafen(cx));
 66    }
 67}
 68
 69fn render_color_ribbon(color: Hsla) -> impl Element {
 70    canvas(
 71        move |_, _| {},
 72        move |bounds, _, cx| {
 73            let height = bounds.size.height;
 74            let horizontal_offset = height;
 75            let vertical_offset = px(height.0 / 2.0);
 76            let mut path = Path::new(bounds.bottom_left());
 77            path.curve_to(
 78                bounds.origin + point(horizontal_offset, vertical_offset),
 79                bounds.origin + point(px(0.0), vertical_offset),
 80            );
 81            path.line_to(bounds.top_right() + point(-horizontal_offset, vertical_offset));
 82            path.curve_to(
 83                bounds.bottom_right(),
 84                bounds.top_right() + point(px(0.0), vertical_offset),
 85            );
 86            path.line_to(bounds.bottom_left());
 87            cx.paint_path(path, color);
 88        },
 89    )
 90    .h_1()
 91    .w_full()
 92}
 93
 94impl TitleBar {
 95    pub(crate) fn render_collaborator_list(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
 96        let room = ActiveCall::global(cx).read(cx).room().cloned();
 97        let current_user = self.user_store.read(cx).current_user();
 98        let client = self.client.clone();
 99        let project_id = self.project.read(cx).remote_id();
100        let workspace = self.workspace.upgrade();
101
102        h_flex()
103            .id("collaborator-list")
104            .w_full()
105            .gap_1()
106            .overflow_x_scroll()
107            .when_some(
108                current_user.clone().zip(client.peer_id()).zip(room.clone()),
109                |this, ((current_user, peer_id), room)| {
110                    let player_colors = cx.theme().players();
111                    let room = room.read(cx);
112                    let mut remote_participants =
113                        room.remote_participants().values().collect::<Vec<_>>();
114                    remote_participants.sort_by_key(|p| p.participant_index.0);
115
116                    let current_user_face_pile = self.render_collaborator(
117                        &current_user,
118                        peer_id,
119                        true,
120                        room.is_speaking(),
121                        room.is_muted(),
122                        None,
123                        room,
124                        project_id,
125                        &current_user,
126                        cx,
127                    );
128
129                    this.children(current_user_face_pile.map(|face_pile| {
130                        v_flex()
131                            .on_mouse_down(MouseButton::Left, |_, cx| cx.stop_propagation())
132                            .child(face_pile)
133                            .child(render_color_ribbon(player_colors.local().cursor))
134                    }))
135                    .children(remote_participants.iter().filter_map(|collaborator| {
136                        let player_color =
137                            player_colors.color_for_participant(collaborator.participant_index.0);
138                        let is_following = workspace
139                            .as_ref()?
140                            .read(cx)
141                            .is_being_followed(collaborator.peer_id);
142                        let is_present = project_id.map_or(false, |project_id| {
143                            collaborator.location
144                                == ParticipantLocation::SharedProject { project_id }
145                        });
146
147                        let facepile = self.render_collaborator(
148                            &collaborator.user,
149                            collaborator.peer_id,
150                            is_present,
151                            collaborator.speaking,
152                            collaborator.muted,
153                            is_following.then_some(player_color.selection),
154                            room,
155                            project_id,
156                            &current_user,
157                            cx,
158                        )?;
159
160                        Some(
161                            v_flex()
162                                .id(("collaborator", collaborator.user.id))
163                                .child(facepile)
164                                .child(render_color_ribbon(player_color.cursor))
165                                .cursor_pointer()
166                                .on_click({
167                                    let peer_id = collaborator.peer_id;
168                                    cx.listener(move |this, _, cx| {
169                                        this.workspace
170                                            .update(cx, |workspace, cx| {
171                                                if is_following {
172                                                    workspace.unfollow(peer_id, cx);
173                                                } else {
174                                                    workspace.follow(peer_id, cx);
175                                                }
176                                            })
177                                            .ok();
178                                    })
179                                })
180                                .tooltip({
181                                    let login = collaborator.user.github_login.clone();
182                                    move |cx| Tooltip::text(format!("Follow {login}"), cx)
183                                }),
184                        )
185                    }))
186                },
187            )
188    }
189
190    #[allow(clippy::too_many_arguments)]
191    fn render_collaborator(
192        &self,
193        user: &Arc<User>,
194        peer_id: PeerId,
195        is_present: bool,
196        is_speaking: bool,
197        is_muted: bool,
198        leader_selection_color: Option<Hsla>,
199        room: &Room,
200        project_id: Option<u64>,
201        current_user: &Arc<User>,
202        cx: &ViewContext<Self>,
203    ) -> Option<Div> {
204        if room.role_for_user(user.id) == Some(proto::ChannelRole::Guest) {
205            return None;
206        }
207
208        const FACEPILE_LIMIT: usize = 3;
209        let followers = project_id.map_or(&[] as &[_], |id| room.followers_for(peer_id, id));
210        let extra_count = followers.len().saturating_sub(FACEPILE_LIMIT);
211
212        Some(
213            div()
214                .m_0p5()
215                .p_0p5()
216                // When the collaborator is not followed, still draw this wrapper div, but leave
217                // it transparent, so that it does not shift the layout when following.
218                .when_some(leader_selection_color, |div, color| {
219                    div.rounded_md().bg(color)
220                })
221                .child(
222                    Facepile::empty()
223                        .child(
224                            Avatar::new(user.avatar_uri.clone())
225                                .grayscale(!is_present)
226                                .border_color(if is_speaking {
227                                    cx.theme().status().info
228                                } else {
229                                    // We draw the border in a transparent color rather to avoid
230                                    // the layout shift that would come with adding/removing the border.
231                                    gpui::transparent_black()
232                                })
233                                .when(is_muted, |avatar| {
234                                    avatar.indicator(
235                                        AvatarAudioStatusIndicator::new(ui::AudioStatus::Muted)
236                                            .tooltip({
237                                                let github_login = user.github_login.clone();
238                                                move |cx| {
239                                                    Tooltip::text(
240                                                        format!("{} is muted", github_login),
241                                                        cx,
242                                                    )
243                                                }
244                                            }),
245                                    )
246                                }),
247                        )
248                        .children(followers.iter().take(FACEPILE_LIMIT).filter_map(
249                            |follower_peer_id| {
250                                let follower = room
251                                    .remote_participants()
252                                    .values()
253                                    .find_map(|p| {
254                                        (p.peer_id == *follower_peer_id).then_some(&p.user)
255                                    })
256                                    .or_else(|| {
257                                        (self.client.peer_id() == Some(*follower_peer_id))
258                                            .then_some(current_user)
259                                    })?
260                                    .clone();
261
262                                Some(div().mt(-px(4.)).child(
263                                    Avatar::new(follower.avatar_uri.clone()).size(rems(0.75)),
264                                ))
265                            },
266                        ))
267                        .children(if extra_count > 0 {
268                            Some(
269                                Label::new(format!("+{extra_count}"))
270                                    .ml_1()
271                                    .into_any_element(),
272                            )
273                        } else {
274                            None
275                        }),
276                ),
277        )
278    }
279
280    pub(crate) fn render_call_controls(&self, cx: &mut ViewContext<Self>) -> Vec<AnyElement> {
281        let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() else {
282            return Vec::new();
283        };
284
285        let is_connecting_to_project = self
286            .workspace
287            .update(cx, |workspace, cx| workspace.has_active_modal(cx))
288            .unwrap_or(false);
289
290        let room = room.read(cx);
291        let project = self.project.read(cx);
292        let is_local = project.is_local() || project.is_via_ssh();
293        let is_shared = is_local && project.is_shared();
294        let is_muted = room.is_muted();
295        let muted_by_user = room.muted_by_user();
296        let is_deafened = room.is_deafened().unwrap_or(false);
297        let is_screen_sharing = room.is_screen_sharing();
298        let can_use_microphone = room.can_use_microphone();
299        let can_share_projects = room.can_share_projects();
300        let screen_sharing_supported = match self.platform_style {
301            PlatformStyle::Mac => true,
302            PlatformStyle::Linux | PlatformStyle::Windows => false,
303        };
304
305        let mut children = Vec::new();
306
307        if is_local && can_share_projects && !is_connecting_to_project {
308            children.push(
309                Button::new(
310                    "toggle_sharing",
311                    if is_shared { "Unshare" } else { "Share" },
312                )
313                .tooltip(move |cx| {
314                    Tooltip::text(
315                        if is_shared {
316                            "Stop sharing project with call participants"
317                        } else {
318                            "Share project with call participants"
319                        },
320                        cx,
321                    )
322                })
323                .style(ButtonStyle::Subtle)
324                .selected_style(ButtonStyle::Tinted(TintColor::Accent))
325                .toggle_state(is_shared)
326                .label_size(LabelSize::Small)
327                .on_click(cx.listener(move |this, _, cx| {
328                    if is_shared {
329                        this.unshare_project(&Default::default(), cx);
330                    } else {
331                        this.share_project(&Default::default(), cx);
332                    }
333                }))
334                .into_any_element(),
335            );
336        }
337
338        children.push(
339            div()
340                .pr_2()
341                .child(
342                    IconButton::new("leave-call", ui::IconName::Exit)
343                        .style(ButtonStyle::Subtle)
344                        .tooltip(|cx| Tooltip::text("Leave call", cx))
345                        .icon_size(IconSize::Small)
346                        .on_click(move |_, cx| {
347                            ActiveCall::global(cx)
348                                .update(cx, |call, cx| call.hang_up(cx))
349                                .detach_and_log_err(cx);
350                        }),
351                )
352                .into_any_element(),
353        );
354
355        if can_use_microphone {
356            children.push(
357                IconButton::new(
358                    "mute-microphone",
359                    if is_muted {
360                        ui::IconName::MicMute
361                    } else {
362                        ui::IconName::Mic
363                    },
364                )
365                .tooltip(move |cx| {
366                    if is_muted {
367                        if is_deafened {
368                            Tooltip::with_meta(
369                                "Unmute Microphone",
370                                None,
371                                "Audio will be unmuted",
372                                cx,
373                            )
374                        } else {
375                            Tooltip::text("Unmute Microphone", cx)
376                        }
377                    } else {
378                        Tooltip::text("Mute Microphone", cx)
379                    }
380                })
381                .style(ButtonStyle::Subtle)
382                .icon_size(IconSize::Small)
383                .toggle_state(is_muted)
384                .selected_style(ButtonStyle::Tinted(TintColor::Error))
385                .on_click(move |_, cx| {
386                    toggle_mute(&Default::default(), cx);
387                })
388                .into_any_element(),
389            );
390
391            children.push(
392                IconButton::new(
393                    "mute-sound",
394                    if is_deafened {
395                        ui::IconName::AudioOff
396                    } else {
397                        ui::IconName::AudioOn
398                    },
399                )
400                .style(ButtonStyle::Subtle)
401                .selected_style(ButtonStyle::Tinted(TintColor::Error))
402                .icon_size(IconSize::Small)
403                .toggle_state(is_deafened)
404                .tooltip(move |cx| {
405                    if is_deafened {
406                        let label = "Unmute Audio";
407
408                        if !muted_by_user {
409                            Tooltip::with_meta(label, None, "Microphone will be unmuted", cx)
410                        } else {
411                            Tooltip::text(label, cx)
412                        }
413                    } else {
414                        let label = "Mute Audio";
415
416                        if !muted_by_user {
417                            Tooltip::with_meta(label, None, "Microphone will be muted", cx)
418                        } else {
419                            Tooltip::text(label, cx)
420                        }
421                    }
422                })
423                .on_click(move |_, cx| toggle_deafen(&Default::default(), cx))
424                .into_any_element(),
425            );
426        }
427
428        if screen_sharing_supported {
429            children.push(
430                IconButton::new("screen-share", ui::IconName::Screen)
431                    .style(ButtonStyle::Subtle)
432                    .icon_size(IconSize::Small)
433                    .toggle_state(is_screen_sharing)
434                    .selected_style(ButtonStyle::Tinted(TintColor::Accent))
435                    .tooltip(move |cx| {
436                        Tooltip::text(
437                            if is_screen_sharing {
438                                "Stop Sharing Screen"
439                            } else {
440                                "Share Screen"
441                            },
442                            cx,
443                        )
444                    })
445                    .on_click(move |_, cx| toggle_screen_sharing(&Default::default(), cx))
446                    .into_any_element(),
447            );
448        }
449
450        children.push(div().pr_2().into_any_element());
451
452        children
453    }
454}