Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
agent_ui: Fix agent terminal close behavior with keyboard shortcuts (#58217)
Hugh L104ccf4bb6ea
diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rsindex 11d0986603e..72f99f3d6b5 100644--- a/crates/agent_ui/src/agent_panel.rs+++ b/crates/agent_ui/src/agent_panel.rs@@ -2198,7 +2198,7 @@ impl AgentPanel {}TerminalEvent::Bell => this.mark_terminal_notification(terminal_id, window, cx),TerminalEvent::CloseTerminal => {- this.close_terminal_from_terminal_event(terminal_id, window, cx);+ this.request_close_terminal_from_terminal_event(terminal_id, cx);}TerminalEvent::BlinkChanged(_)| TerminalEvent::SelectionsChanged@@ -2269,7 +2269,7 @@ impl AgentPanel {window: &mut Window,cx: &mut Context<Self>,) {- self.close_terminal_internal(terminal_id, true, None, window, cx);+ self.close_terminal_internal(terminal_id, true, window, cx);}pub fn close_terminal_without_activating_draft(@@ -2278,14 +2278,13 @@ impl AgentPanel {window: &mut Window,cx: &mut Context<Self>,) {- self.close_terminal_internal(terminal_id, false, None, window, cx);+ self.close_terminal_internal(terminal_id, false, window, cx);}fn close_terminal_internal(&mut self,terminal_id: TerminalId,activate_draft_after_close: bool,- terminal_closed_metadata: Option<TerminalThreadMetadata>,window: &mut Window,cx: &mut Context<Self>,) {@@ -2311,21 +2310,18 @@ impl AgentPanel {}}- if let Some(metadata) = terminal_closed_metadata {- cx.emit(AgentPanelEvent::TerminalClosed { metadata });- }cx.emit(AgentPanelEvent::EntryChanged);cx.notify();}- fn close_terminal_from_terminal_event(+ fn request_close_terminal_from_terminal_event(&mut self,terminal_id: TerminalId,- window: &mut Window,cx: &mut Context<Self>,) {- let metadata = self.terminal_metadata(terminal_id, cx);- self.close_terminal_internal(terminal_id, false, metadata, window, cx);+ if let Some(metadata) = self.terminal_metadata(terminal_id, cx) {+ cx.emit(AgentPanelEvent::TerminalCloseRequested { metadata });+ }}fn emit_terminal_thread_started(@@ -2767,7 +2763,7 @@ impl AgentPanel {this.dismiss_terminal_pop_up_if_visible(terminal_id, &pop_up_weak, window, cx);}AgentPanelEvent::EntryChanged- | AgentPanelEvent::TerminalClosed { .. }+ | AgentPanelEvent::TerminalCloseRequested { .. }| AgentPanelEvent::ThreadInteracted { .. } => {}}});@@ -4944,7 +4940,7 @@ pub enum AgentPanelEvent {ActiveViewChanged,ActiveViewFocused,EntryChanged,- TerminalClosed { metadata: TerminalThreadMetadata },+ TerminalCloseRequested { metadata: TerminalThreadMetadata },ThreadInteracted { thread_id: ThreadId },}@@ -9409,39 +9405,6 @@ mod tests {);}- #[gpui::test]- async fn test_terminal_close_event_closes_without_sidebar(cx: &mut TestAppContext) {- let (panel, mut cx) = setup_panel(cx).await;- cx.update(|_, cx| {- TerminalThreadMetadataStore::init_global(cx);- });-- let terminal_id = panel- .update_in(&mut cx, |panel, window, cx| {- panel.insert_test_terminal("Dev Server", true, window, cx)- })- .expect("test terminal should be inserted");- cx.run_until_parked();-- panel.update(&mut cx, |panel, cx| {- panel.emit_test_terminal_close(terminal_id, cx);- });- cx.run_until_parked();-- panel.read_with(&cx, |panel, _cx| {- assert!(!panel.has_terminal(terminal_id));- });- cx.update(|_, cx| {- assert!(- TerminalThreadMetadataStore::global(cx)- .read(cx)- .entry(terminal_id)- .is_none(),- "terminal metadata should be deleted by the fallback close"- );- });- }-#[gpui::test]async fn test_terminal_title_omits_placeholder_title(cx: &mut TestAppContext) {let (panel, mut cx) = setup_panel(cx).await;diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rsindex 507c96c902c..d65e83e45a2 100644--- a/crates/agent_ui/src/conversation_view.rs+++ b/crates/agent_ui/src/conversation_view.rs@@ -3046,7 +3046,7 @@ impl ConversationView {dismiss_if_visible(this, window, cx);}AgentPanelEvent::EntryChanged- | AgentPanelEvent::TerminalClosed { .. }+ | AgentPanelEvent::TerminalCloseRequested { .. }| AgentPanelEvent::ThreadInteracted { .. } => {}},));diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rsindex b28ea7af1e7..f55618fc2b8 100644--- a/crates/sidebar/src/sidebar.rs+++ b/crates/sidebar/src/sidebar.rs@@ -1139,7 +1139,7 @@ impl Sidebar {this.sync_active_entry_from_panel(agent_panel, cx);this.schedule_update_entries(false, cx);}- AgentPanelEvent::TerminalClosed { metadata } => {+ AgentPanelEvent::TerminalCloseRequested { metadata } => {if let Some(workspace) = workspace.upgrade() {let workspace = ThreadEntryWorkspace::Open(workspace);this.close_terminal(metadata, &workspace, window, cx);@@ -5209,6 +5209,7 @@ impl Sidebar {cx: &mut Context<Self>,) {let terminal_id = metadata.terminal_id;+ let defer_draft_activation = activate_panel_draft && is_active && neighbor.is_some();// Closing from the sidebar must not steal focus, since the row's// workspace may not be the active workspace.@@ -5216,10 +5217,10 @@ impl Sidebar {workspace.update(cx, |workspace, cx| {if let Some(panel) = workspace.panel::<AgentPanel>(cx) {panel.update(cx, |panel, cx| {- if activate_panel_draft {- panel.close_terminal(terminal_id, window, cx);- } else {+ if defer_draft_activation || !activate_panel_draft {panel.close_terminal_without_activating_draft(terminal_id, window, cx);+ } else {+ panel.close_terminal(terminal_id, window, cx);}});}@@ -5241,6 +5242,15 @@ impl Sidebar {{return;}+ if defer_draft_activation && let ThreadEntryWorkspace::Open(workspace) = workspace {+ workspace.update(cx, |workspace, cx| {+ if let Some(panel) = workspace.panel::<AgentPanel>(cx) {+ panel.update(cx, |panel, cx| {+ panel.activate_draft(false, AgentThreadSource::AgentPanel, window, cx);+ });+ }+ });+ }self.sync_active_entry_from_active_workspace(cx);}self.update_entries(cx);diff --git a/crates/sidebar/src/sidebar_tests.rs b/crates/sidebar/src/sidebar_tests.rsindex e5c7cca5d67..864efd5ab82 100644--- a/crates/sidebar/src/sidebar_tests.rs+++ b/crates/sidebar/src/sidebar_tests.rs@@ -2930,6 +2930,46 @@ async fn test_terminal_close_event_closes_sidebar_terminal(cx: &mut TestAppConte});}+#[gpui::test]+async fn test_terminal_close_event_activates_neighbor(cx: &mut TestAppContext) {+ let project = init_test_project_with_agent_panel("/my-project", cx).await;+ let (multi_workspace, cx) =+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);+ let build_terminal_id = panel+ .update_in(cx, |panel, window, cx| {+ panel.insert_test_terminal("Build", true, window, cx)+ })+ .expect("build test terminal should be inserted");+ let server_terminal_id = panel+ .update_in(cx, |panel, window, cx| {+ panel.insert_test_terminal("Server", true, window, cx)+ })+ .expect("server test terminal should be inserted");+ cx.run_until_parked();++ panel.update(cx, |panel, cx| {+ panel.emit_test_terminal_close(server_terminal_id, cx);+ });+ cx.run_until_parked();++ panel.read_with(cx, |panel, _cx| {+ assert!(!panel.has_terminal(server_terminal_id));+ assert_eq!(panel.active_terminal_id(), Some(build_terminal_id));+ });+ sidebar.read_with(cx, |sidebar, _cx| {+ assert!(+ matches!(&sidebar.active_entry, Some(ActiveEntry::Terminal { terminal_id, .. }) if *terminal_id == build_terminal_id),+ "expected remaining terminal to become active, got {:?}",+ sidebar.active_entry,+ );+ });+ assert_eq!(+ visible_entries_as_strings(&sidebar, cx),+ vec!["v [my-project]", " Build"]+ );+}+#[gpui::test]async fn test_agent_panel_terminal_notifications_update_sidebar(cx: &mut TestAppContext) {let project = init_test_project_with_agent_panel("/my-project", cx).await;