Send messages on enter

Nathan Sobo and Antonio Scandurra created

Co-Authored-By: Antonio Scandurra <me@as-cii.com>

Change summary

zed/src/chat_panel.rs | 24 +++++++++++++++++++++++-
zed/src/editor.rs     |  7 +++++++
2 files changed, 30 insertions(+), 1 deletion(-)

Detailed changes

zed/src/chat_panel.rs 🔗

@@ -1,10 +1,12 @@
 use crate::{
     channel::{Channel, ChannelEvent, ChannelList, ChannelMessage},
     editor::Editor,
+    util::ResultExt,
     Settings,
 };
 use gpui::{
-    elements::*, Entity, ModelHandle, RenderContext, Subscription, View, ViewContext, ViewHandle,
+    action, elements::*, Entity, ModelHandle, MutableAppContext, RenderContext, Subscription, View,
+    ViewContext, ViewHandle,
 };
 use postage::watch;
 
@@ -18,6 +20,12 @@ pub struct ChatPanel {
 
 pub enum Event {}
 
+action!(Send);
+
+pub fn init(cx: &mut MutableAppContext) {
+    cx.add_action(ChatPanel::send);
+}
+
 impl ChatPanel {
     pub fn new(
         channel_list: ModelHandle<ChannelList>,
@@ -109,6 +117,20 @@ impl ChatPanel {
             .with_max_height(100.)
             .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| {
+                let body = editor.text(cx);
+                editor.clear(cx);
+                body
+            });
+
+            channel
+                .update(cx, |channel, cx| channel.send_message(body, cx))
+                .log_err();
+        }
+    }
 }
 
 impl Entity for ChatPanel {

zed/src/editor.rs 🔗

@@ -719,6 +719,13 @@ impl Editor {
         self.end_transaction(cx);
     }
 
+    pub fn clear(&mut self, cx: &mut ViewContext<Self>) {
+        self.start_transaction(cx);
+        self.select_all(&SelectAll, cx);
+        self.insert(&Insert(String::new()), cx);
+        self.end_transaction(cx);
+    }
+
     fn newline(&mut self, Newline(insert_newline): &Newline, cx: &mut ViewContext<Self>) {
         match self.mode {
             EditorMode::SingleLine => cx.propagate_action(),