diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 611220568b7601b6301889fbe48e8059230f21c2..34f5f378d931e5ddbedca9f4e2a98a0709da329c 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -1714,7 +1714,7 @@ mod tests { ) .detach(); client - .add_connection(user_id.to_proto(), client_conn, cx.to_async()) + .add_connection(user_id.to_proto(), client_conn, &cx.to_async()) .await .unwrap(); (user_id, client) diff --git a/zed/src/channel.rs b/zed/src/channel.rs index e3cb5d29d6635cc614075853dafef195b3dfa871..24997d49642648e5fc59c301270346cb75007a3b 100644 --- a/zed/src/channel.rs +++ b/zed/src/channel.rs @@ -671,7 +671,7 @@ mod tests { cx.background().spawn(io).detach(); client - .add_connection(user_id, client_conn, cx.to_async()) + .add_connection(user_id, client_conn, &cx.to_async()) .await .unwrap(); diff --git a/zed/src/chat_panel.rs b/zed/src/chat_panel.rs index fe1ac5ba8a9d3f346767606687fb6590a29f8143..c8aaac4f38845b4a52bf4261b76ad4f50d6d8c60 100644 --- a/zed/src/chat_panel.rs +++ b/zed/src/chat_panel.rs @@ -193,6 +193,19 @@ impl ChatPanel { cx.notify(); } + fn render_channel(&self) -> ElementBox { + let theme = &self.settings.borrow().theme; + Flex::column() + .with_child( + Container::new(ChildView::new(self.channel_select.id()).boxed()) + .with_style(&theme.chat_panel.channel_select.container) + .boxed(), + ) + .with_child(self.render_active_channel_messages()) + .with_child(self.render_input_box()) + .boxed() + } + fn render_active_channel_messages(&self) -> ElementBox { let messages = if self.active_channel.is_some() { List::new(self.message_list.clone()).boxed() @@ -279,6 +292,47 @@ impl ChatPanel { .boxed() } + fn render_sign_in_prompt(&self, cx: &mut RenderContext) -> ElementBox { + let theme = &self.settings.borrow().theme; + let rpc = self.rpc.clone(); + let this = cx.handle(); + + enum SignInPromptLabel {} + + Align::new( + MouseEventHandler::new::(0, cx, |mouse_state, _| { + Label::new( + "Sign in to use chat".to_string(), + if mouse_state.hovered { + theme.chat_panel.hovered_sign_in_prompt.clone() + } else { + theme.chat_panel.sign_in_prompt.clone() + }, + ) + .boxed() + }) + .with_cursor_style(CursorStyle::PointingHand) + .on_click(move |cx| { + let rpc = rpc.clone(); + let this = this.clone(); + cx.spawn(|mut cx| async move { + if rpc.authenticate_and_connect(&cx).log_err().await.is_some() { + cx.update(|cx| { + if let Some(this) = this.upgrade(cx) { + if this.is_focused(cx) { + this.update(cx, |this, cx| cx.focus(&this.input_editor)); + } + } + }) + } + }) + .detach(); + }) + .boxed(), + ) + .boxed() + } + fn send(&mut self, _: &Send, cx: &mut ViewContext) { if let Some((channel, _)) = self.active_channel.as_ref() { let body = self.input_editor.update(cx, |editor, cx| { @@ -303,6 +357,10 @@ impl ChatPanel { }) } } + + fn is_signed_in(&self) -> bool { + self.rpc.user_id().borrow().is_some() + } } impl Entity for ChatPanel { @@ -316,55 +374,24 @@ impl View for ChatPanel { fn render(&mut self, cx: &mut RenderContext) -> ElementBox { let theme = &self.settings.borrow().theme; - let element = if self.rpc.user_id().borrow().is_none() { - enum SignInPromptLabel {} - - Align::new( - MouseEventHandler::new::(0, cx, |mouse_state, _| { - Label::new( - "Sign in to use chat".to_string(), - if mouse_state.hovered { - theme.chat_panel.hovered_sign_in_prompt.clone() - } else { - theme.chat_panel.sign_in_prompt.clone() - }, - ) - .boxed() - }) - .with_cursor_style(CursorStyle::PointingHand) - .on_click({ - let rpc = self.rpc.clone(); - move |cx| { - let rpc = rpc.clone(); - cx.spawn(|cx| async move { - rpc.authenticate_and_connect(cx).log_err().await; - }) - .detach(); - } - }) - .boxed(), - ) - .boxed() + let element = if self.is_signed_in() { + self.render_channel() } else { - Container::new( - Flex::column() - .with_child( - Container::new(ChildView::new(self.channel_select.id()).boxed()) - .with_style(&theme.chat_panel.channel_select.container) - .boxed(), - ) - .with_child(self.render_active_channel_messages()) - .with_child(self.render_input_box()) - .boxed(), - ) - .with_style(&theme.chat_panel.container) - .boxed() + self.render_sign_in_prompt(cx) }; - ConstrainedBox::new(element).with_min_width(150.).boxed() + ConstrainedBox::new( + Container::new(element) + .with_style(&theme.chat_panel.container) + .boxed(), + ) + .with_min_width(150.) + .boxed() } fn on_focus(&mut self, cx: &mut ViewContext) { - cx.focus(&self.input_editor); + if self.is_signed_in() { + cx.focus(&self.input_editor); + } } } diff --git a/zed/src/lib.rs b/zed/src/lib.rs index d451c187d5cc982354d25ebf9575b4c7df7bbc1b..e7bb31b207b86a20fe80d091611cbc6ec57947d1 100644 --- a/zed/src/lib.rs +++ b/zed/src/lib.rs @@ -50,7 +50,7 @@ pub fn init(app_state: &Arc, cx: &mut gpui::MutableAppContext) { let rpc = app_state.rpc.clone(); move |_: &Authenticate, cx| { let rpc = rpc.clone(); - cx.spawn(|cx| async move { rpc.authenticate_and_connect(cx).log_err().await }) + cx.spawn(|cx| async move { rpc.authenticate_and_connect(&cx).log_err().await }) .detach(); } }); diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs index 6ec07c90f6470e772cff722146a1fcdc1ab95fa1..7c0302834db11954613b106fe7b8f4b9e07fc183 100644 --- a/zed/src/rpc.rs +++ b/zed/src/rpc.rs @@ -139,7 +139,7 @@ impl Client { pub async fn authenticate_and_connect( self: &Arc, - cx: AsyncAppContext, + cx: &AsyncAppContext, ) -> anyhow::Result<()> { if self.state.read().connection_id.is_some() { return Ok(()); @@ -176,7 +176,7 @@ impl Client { self: &Arc, user_id: u64, conn: Conn, - cx: AsyncAppContext, + cx: &AsyncAppContext, ) -> anyhow::Result<()> where Conn: 'static diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index ff87c4b42c4e6af76fbafa6766788085e31d9841..95050ca60791adee7019857bf4b0955a4b761eb3 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -798,7 +798,7 @@ impl Workspace { let platform = cx.platform(); let task = cx.spawn(|this, mut cx| async move { - rpc.authenticate_and_connect(cx.clone()).await?; + rpc.authenticate_and_connect(&cx).await?; let share_task = this.update(&mut cx, |this, cx| { let worktree = this.worktrees.iter().next()?; @@ -830,7 +830,7 @@ impl Workspace { let languages = self.languages.clone(); let task = cx.spawn(|this, mut cx| async move { - rpc.authenticate_and_connect(cx.clone()).await?; + rpc.authenticate_and_connect(&cx).await?; let worktree_url = cx .platform()