Focus chat input editor after signing in

Antonio Scandurra created

Change summary

server/src/rpc.rs     |   2 
zed/src/channel.rs    |   2 
zed/src/chat_panel.rs | 115 +++++++++++++++++++++++++++-----------------
zed/src/lib.rs        |   2 
zed/src/rpc.rs        |   4 
zed/src/workspace.rs  |   4 
6 files changed, 78 insertions(+), 51 deletions(-)

Detailed changes

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)

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();
 

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<Self>) -> ElementBox {
+        let theme = &self.settings.borrow().theme;
+        let rpc = self.rpc.clone();
+        let this = cx.handle();
+
+        enum SignInPromptLabel {}
+
+        Align::new(
+            MouseEventHandler::new::<SignInPromptLabel, _, _, _>(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<Self>) {
         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<Self>) -> ElementBox {
         let theme = &self.settings.borrow().theme;
-        let element = if self.rpc.user_id().borrow().is_none() {
-            enum SignInPromptLabel {}
-
-            Align::new(
-                MouseEventHandler::new::<SignInPromptLabel, _, _, _>(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<Self>) {
-        cx.focus(&self.input_editor);
+        if self.is_signed_in() {
+            cx.focus(&self.input_editor);
+        }
     }
 }
 

zed/src/lib.rs 🔗

@@ -50,7 +50,7 @@ pub fn init(app_state: &Arc<AppState>, 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();
         }
     });

zed/src/rpc.rs 🔗

@@ -139,7 +139,7 @@ impl Client {
 
     pub async fn authenticate_and_connect(
         self: &Arc<Self>,
-        cx: AsyncAppContext,
+        cx: &AsyncAppContext,
     ) -> anyhow::Result<()> {
         if self.state.read().connection_id.is_some() {
             return Ok(());
@@ -176,7 +176,7 @@ impl Client {
         self: &Arc<Self>,
         user_id: u64,
         conn: Conn,
-        cx: AsyncAppContext,
+        cx: &AsyncAppContext,
     ) -> anyhow::Result<()>
     where
         Conn: 'static

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()