collab_ui.rs

  1mod collab_titlebar_item;
  2mod collaborator_list_popover;
  3mod contact_finder;
  4mod contact_list;
  5mod contact_notification;
  6mod contacts_popover;
  7mod face_pile;
  8mod incoming_call_notification;
  9mod notifications;
 10mod project_shared_notification;
 11mod sharing_status_indicator;
 12
 13use anyhow::anyhow;
 14use call::ActiveCall;
 15pub use collab_titlebar_item::{CollabTitlebarItem, ToggleContactsMenu};
 16use gpui::{actions, AppContext, Task};
 17use std::sync::Arc;
 18use workspace::{AppState, JoinProject, ToggleFollow, Workspace};
 19
 20actions!(collab, [ToggleScreenSharing]);
 21
 22pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
 23    collab_titlebar_item::init(cx);
 24    contact_notification::init(cx);
 25    contact_list::init(cx);
 26    contact_finder::init(cx);
 27    contacts_popover::init(cx);
 28    incoming_call_notification::init(cx);
 29    project_shared_notification::init(cx);
 30    sharing_status_indicator::init(cx);
 31
 32    cx.add_global_action(toggle_screen_sharing);
 33    cx.add_global_action(move |action: &JoinProject, cx| {
 34        join_project(action, app_state.clone(), cx);
 35    });
 36}
 37
 38pub fn toggle_screen_sharing(_: &ToggleScreenSharing, cx: &mut AppContext) {
 39    if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
 40        let toggle_screen_sharing = room.update(cx, |room, cx| {
 41            if room.is_screen_sharing() {
 42                Task::ready(room.unshare_screen(cx))
 43            } else {
 44                room.share_screen(cx)
 45            }
 46        });
 47        toggle_screen_sharing.detach_and_log_err(cx);
 48    }
 49}
 50
 51fn join_project(action: &JoinProject, app_state: Arc<AppState>, cx: &mut AppContext) {
 52    let project_id = action.project_id;
 53    let follow_user_id = action.follow_user_id;
 54    cx.spawn(|mut cx| async move {
 55        let existing_workspace = cx.update(|cx| {
 56            cx.window_ids()
 57                .filter_map(|window_id| cx.root_view(window_id)?.clone().downcast::<Workspace>())
 58                .find(|workspace| {
 59                    workspace.read(cx).project().read(cx).remote_id() == Some(project_id)
 60                })
 61        });
 62
 63        let workspace = if let Some(existing_workspace) = existing_workspace {
 64            existing_workspace
 65        } else {
 66            let active_call = cx.read(ActiveCall::global);
 67            let room = active_call
 68                .read_with(&cx, |call, _| call.room().cloned())
 69                .ok_or_else(|| anyhow!("not in a call"))?;
 70            let project = room
 71                .update(&mut cx, |room, cx| {
 72                    room.join_project(
 73                        project_id,
 74                        app_state.languages.clone(),
 75                        app_state.fs.clone(),
 76                        cx,
 77                    )
 78                })
 79                .await?;
 80
 81            let (_, workspace) = cx.add_window(
 82                (app_state.build_window_options)(None, None, cx.platform().as_ref()),
 83                |cx| {
 84                    let mut workspace = Workspace::new(
 85                        Default::default(),
 86                        0,
 87                        project,
 88                        app_state.dock_default_item_factory,
 89                        app_state.background_actions,
 90                        cx,
 91                    );
 92                    (app_state.initialize_workspace)(&mut workspace, &app_state, cx);
 93                    workspace
 94                },
 95            );
 96            workspace
 97        };
 98
 99        cx.activate_window(workspace.window_id());
100        cx.platform().activate(true);
101
102        workspace.update(&mut cx, |workspace, cx| {
103            if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
104                let follow_peer_id = room
105                    .read(cx)
106                    .remote_participants()
107                    .iter()
108                    .find(|(_, participant)| participant.user.id == follow_user_id)
109                    .map(|(_, p)| p.peer_id)
110                    .or_else(|| {
111                        // If we couldn't follow the given user, follow the host instead.
112                        let collaborator = workspace
113                            .project()
114                            .read(cx)
115                            .collaborators()
116                            .values()
117                            .find(|collaborator| collaborator.replica_id == 0)?;
118                        Some(collaborator.peer_id)
119                    });
120
121                if let Some(follow_peer_id) = follow_peer_id {
122                    if !workspace.is_being_followed(follow_peer_id) {
123                        workspace
124                            .toggle_follow(&ToggleFollow(follow_peer_id), cx)
125                            .map(|follow| follow.detach_and_log_err(cx));
126                    }
127                }
128            }
129        });
130
131        anyhow::Ok(())
132    })
133    .detach_and_log_err(cx);
134}