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, MutableAppContext, 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 MutableAppContext) {
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 MutableAppContext) {
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 MutableAppContext) {
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::<Workspace>(window_id))
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 cx,
90 );
91 (app_state.initialize_workspace)(&mut workspace, &app_state, cx);
92 workspace
93 },
94 );
95 workspace
96 };
97
98 cx.activate_window(workspace.window_id());
99 cx.platform().activate(true);
100
101 workspace.update(&mut cx, |workspace, cx| {
102 if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
103 let follow_peer_id = room
104 .read(cx)
105 .remote_participants()
106 .iter()
107 .find(|(_, participant)| participant.user.id == follow_user_id)
108 .map(|(_, p)| p.peer_id)
109 .or_else(|| {
110 // If we couldn't follow the given user, follow the host instead.
111 let collaborator = workspace
112 .project()
113 .read(cx)
114 .collaborators()
115 .values()
116 .find(|collaborator| collaborator.replica_id == 0)?;
117 Some(collaborator.peer_id)
118 });
119
120 if let Some(follow_peer_id) = follow_peer_id {
121 if !workspace.is_being_followed(follow_peer_id) {
122 workspace
123 .toggle_follow(&ToggleFollow(follow_peer_id), cx)
124 .map(|follow| follow.detach_and_log_err(cx));
125 }
126 }
127 }
128 });
129
130 anyhow::Ok(())
131 })
132 .detach_and_log_err(cx);
133}