Automatically share project when creating the room

Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-10-04 19:25:48 +02:00
parent 678b013da6
commit fceba6814f
12 changed files with 137 additions and 35 deletions

View file

@ -100,8 +100,9 @@ impl CollabTitlebarItem {
Some(_) => {}
None => {
if let Some(workspace) = self.workspace.upgrade(cx) {
let project = workspace.read(cx).project().clone();
let user_store = workspace.read(cx).user_store().clone();
let view = cx.add_view(|cx| ContactsPopover::new(user_store, cx));
let view = cx.add_view(|cx| ContactsPopover::new(project, user_store, cx));
cx.focus(&view);
cx.subscribe(&view, |this, _, event, cx| {
match event {

View file

@ -11,6 +11,6 @@ use workspace::AppState;
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
contacts_popover::init(cx);
collab_titlebar_item::init(cx);
incoming_call_notification::init(app_state.user_store.clone(), cx);
incoming_call_notification::init(app_state.clone(), cx);
project_shared_notification::init(app_state, cx);
}

View file

@ -10,6 +10,7 @@ use gpui::{
ViewHandle,
};
use menu::{Confirm, SelectNext, SelectPrev};
use project::Project;
use settings::Settings;
use theme::IconButton;
@ -30,6 +31,7 @@ struct ToggleExpanded(Section);
#[derive(Clone, PartialEq)]
struct Call {
recipient_user_id: u64,
initial_project: Option<ModelHandle<Project>>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, PartialOrd, Ord)]
@ -83,6 +85,7 @@ pub struct ContactsPopover {
entries: Vec<ContactEntry>,
match_candidates: Vec<StringMatchCandidate>,
list_state: ListState,
project: ModelHandle<Project>,
user_store: ModelHandle<UserStore>,
filter_editor: ViewHandle<Editor>,
collapsed_sections: Vec<Section>,
@ -91,7 +94,11 @@ pub struct ContactsPopover {
}
impl ContactsPopover {
pub fn new(user_store: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) -> Self {
pub fn new(
project: ModelHandle<Project>,
user_store: ModelHandle<UserStore>,
cx: &mut ViewContext<Self>,
) -> Self {
let filter_editor = cx.add_view(|cx| {
let mut editor = Editor::single_line(
Some(|theme| theme.contacts_panel.user_query_editor.clone()),
@ -149,9 +156,13 @@ impl ContactsPopover {
is_selected,
cx,
),
ContactEntry::Contact(contact) => {
Self::render_contact(contact, &theme.contacts_panel, is_selected, cx)
}
ContactEntry::Contact(contact) => Self::render_contact(
contact,
&this.project,
&theme.contacts_panel,
is_selected,
cx,
),
}
});
@ -168,6 +179,7 @@ impl ContactsPopover {
match_candidates: Default::default(),
filter_editor,
_subscriptions: subscriptions,
project,
user_store,
};
this.update_entries(cx);
@ -463,12 +475,18 @@ impl ContactsPopover {
fn render_contact(
contact: &Contact,
project: &ModelHandle<Project>,
theme: &theme::ContactsPanel,
is_selected: bool,
cx: &mut RenderContext<Self>,
) -> ElementBox {
let online = contact.online;
let user_id = contact.user.id;
let initial_project = if ActiveCall::global(cx).read(cx).room().is_none() {
Some(project.clone())
} else {
None
};
let mut element =
MouseEventHandler::<Contact>::new(contact.user.id as usize, cx, |_, _| {
Flex::row()
@ -501,6 +519,7 @@ impl ContactsPopover {
if online {
cx.dispatch_action(Call {
recipient_user_id: user_id,
initial_project: initial_project.clone(),
});
}
});
@ -629,7 +648,7 @@ impl ContactsPopover {
fn call(&mut self, action: &Call, cx: &mut ViewContext<Self>) {
ActiveCall::global(cx)
.update(cx, |active_call, cx| {
active_call.invite(action.recipient_user_id, cx)
active_call.invite(action.recipient_user_id, action.initial_project.clone(), cx)
})
.detach_and_log_err(cx);
}

View file

@ -1,21 +1,25 @@
use std::sync::Arc;
use call::ActiveCall;
use client::{incoming_call::IncomingCall, UserStore};
use client::incoming_call::IncomingCall;
use futures::StreamExt;
use gpui::{
elements::*,
geometry::{rect::RectF, vector::vec2f},
impl_internal_actions, Entity, ModelHandle, MouseButton, MutableAppContext, RenderContext,
View, ViewContext, WindowBounds, WindowKind, WindowOptions,
impl_internal_actions, Entity, MouseButton, MutableAppContext, RenderContext, View,
ViewContext, WindowBounds, WindowKind, WindowOptions,
};
use project::Project;
use settings::Settings;
use util::ResultExt;
use workspace::{AppState, Workspace};
impl_internal_actions!(incoming_call_notification, [RespondToCall]);
pub fn init(user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
cx.add_action(IncomingCallNotification::respond_to_call);
let mut incoming_call = user_store.read(cx).incoming_call();
let mut incoming_call = app_state.user_store.read(cx).incoming_call();
cx.spawn(|mut cx| async move {
let mut notification_window = None;
while let Some(incoming_call) = incoming_call.next().await {
@ -32,7 +36,7 @@ pub fn init(user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
kind: WindowKind::PopUp,
is_movable: false,
},
|_| IncomingCallNotification::new(incoming_call, user_store.clone()),
|_| IncomingCallNotification::new(incoming_call, app_state.clone()),
);
notification_window = Some(window_id);
}
@ -48,21 +52,47 @@ struct RespondToCall {
pub struct IncomingCallNotification {
call: IncomingCall,
user_store: ModelHandle<UserStore>,
app_state: Arc<AppState>,
}
impl IncomingCallNotification {
pub fn new(call: IncomingCall, user_store: ModelHandle<UserStore>) -> Self {
Self { call, user_store }
pub fn new(call: IncomingCall, app_state: Arc<AppState>) -> Self {
Self { call, app_state }
}
fn respond_to_call(&mut self, action: &RespondToCall, cx: &mut ViewContext<Self>) {
if action.accept {
ActiveCall::global(cx)
.update(cx, |active_call, cx| active_call.join(&self.call, cx))
.detach_and_log_err(cx);
let app_state = self.app_state.clone();
let join = ActiveCall::global(cx)
.update(cx, |active_call, cx| active_call.join(&self.call, cx));
let initial_project_id = self.call.initial_project_id;
cx.spawn_weak(|_, mut cx| async move {
join.await?;
if let Some(initial_project_id) = initial_project_id {
let project = Project::remote(
initial_project_id,
app_state.client.clone(),
app_state.user_store.clone(),
app_state.project_store.clone(),
app_state.languages.clone(),
app_state.fs.clone(),
cx.clone(),
)
.await?;
cx.add_window((app_state.build_window_options)(), |cx| {
let mut workspace =
Workspace::new(project, app_state.default_item_factory, cx);
(app_state.initialize_workspace)(&mut workspace, &app_state, cx);
workspace
});
}
anyhow::Ok(())
})
.detach_and_log_err(cx);
} else {
self.user_store
self.app_state
.user_store
.update(cx, |user_store, _| user_store.decline_call().log_err());
}