From 69e84c0c48ab488830472ad5f08adab70c2455b1 Mon Sep 17 00:00:00 2001 From: Danilo Leal <67129314+danilo-leal@users.noreply.github.com> Date: Mon, 16 Jun 2025 19:07:29 -0300 Subject: [PATCH] agent: Scroll to bottom after submitting a new message (#32819) This is a follow up to my original attempt https://github.com/zed-industries/zed/pull/30878 and to the PR that eventually reverted parts of it because it broke stuff https://github.com/zed-industries/zed/pull/31295. This new approach attaches the `scroll_to_bottom` feature to the `chat` function, which is triggered when the `Chat` action is dispatched by the "send" icon buttons. With that, and from my testing, the thread doesn't forcefully scroll as new messages are added, which was the regression I had introduced. Release Notes: - agent: The panel nows scrolls to the bottom after submitting a new message, allowing to see it more easily. --- crates/agent/src/agent_panel.rs | 21 ++++++++++++++++++--- crates/agent/src/message_editor.rs | 6 ++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/agent/src/agent_panel.rs b/crates/agent/src/agent_panel.rs index d00c9791943abc12b81bc0144a40359e43c8aa0a..7d79ab22027c4c3623f40d6b1bf2e054b01eb5f7 100644 --- a/crates/agent/src/agent_panel.rs +++ b/crates/agent/src/agent_panel.rs @@ -520,10 +520,15 @@ impl AgentPanel { }); let message_editor_subscription = - cx.subscribe(&message_editor, |_, _, event, cx| match event { + cx.subscribe(&message_editor, |this, _, event, cx| match event { MessageEditorEvent::Changed | MessageEditorEvent::EstimatedTokenCount => { cx.notify(); } + MessageEditorEvent::ScrollThreadToBottom => { + this.thread.update(cx, |thread, cx| { + thread.scroll_to_bottom(cx); + }); + } }); let thread_id = thread.read(cx).id().clone(); @@ -803,10 +808,15 @@ impl AgentPanel { self.message_editor.focus_handle(cx).focus(window); let message_editor_subscription = - cx.subscribe(&self.message_editor, |_, _, event, cx| match event { + cx.subscribe(&self.message_editor, |this, _, event, cx| match event { MessageEditorEvent::Changed | MessageEditorEvent::EstimatedTokenCount => { cx.notify(); } + MessageEditorEvent::ScrollThreadToBottom => { + this.thread.update(cx, |thread, cx| { + thread.scroll_to_bottom(cx); + }); + } }); self._active_thread_subscriptions = vec![ @@ -1018,10 +1028,15 @@ impl AgentPanel { self.message_editor.focus_handle(cx).focus(window); let message_editor_subscription = - cx.subscribe(&self.message_editor, |_, _, event, cx| match event { + cx.subscribe(&self.message_editor, |this, _, event, cx| match event { MessageEditorEvent::Changed | MessageEditorEvent::EstimatedTokenCount => { cx.notify(); } + MessageEditorEvent::ScrollThreadToBottom => { + this.thread.update(cx, |thread, cx| { + thread.scroll_to_bottom(cx); + }); + } }); self._active_thread_subscriptions = vec![ diff --git a/crates/agent/src/message_editor.rs b/crates/agent/src/message_editor.rs index ee7ebd3092465f7b72cf24702786c4df4bea88aa..288b2b650bfcd52465b257083c102d52b91bf149 100644 --- a/crates/agent/src/message_editor.rs +++ b/crates/agent/src/message_editor.rs @@ -301,6 +301,7 @@ impl MessageEditor { self.set_editor_is_expanded(false, cx); self.send_to_model(window, cx); + cx.emit(MessageEditorEvent::ScrollThreadToBottom); cx.notify(); } @@ -906,7 +907,7 @@ impl MessageEditor { ) } - fn render_changed_buffers( + fn render_edits_bar( &self, changed_buffers: &BTreeMap, Entity>, window: &mut Window, @@ -1510,6 +1511,7 @@ impl EventEmitter for MessageEditor {} pub enum MessageEditorEvent { EstimatedTokenCount, Changed, + ScrollThreadToBottom, } impl Focusable for MessageEditor { @@ -1537,7 +1539,7 @@ impl Render for MessageEditor { v_flex() .size_full() .when(changed_buffers.len() > 0, |parent| { - parent.child(self.render_changed_buffers(&changed_buffers, window, cx)) + parent.child(self.render_edits_bar(&changed_buffers, window, cx)) }) .child(self.render_editor(window, cx)) .children({