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