diff --git a/zed/src/chat_panel.rs b/zed/src/chat_panel.rs index 458215e72c..de4d183d5a 100644 --- a/zed/src/chat_panel.rs +++ b/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, @@ -109,6 +117,20 @@ impl ChatPanel { .with_max_height(100.) .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| { + 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 { diff --git a/zed/src/editor.rs b/zed/src/editor.rs index 6ac06cd370..4aa5b6ce3e 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -719,6 +719,13 @@ impl Editor { self.end_transaction(cx); } + pub fn clear(&mut self, cx: &mut ViewContext) { + 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) { match self.mode { EditorMode::SingleLine => cx.propagate_action(),