Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Fixed the zoomed agent panel closing unexpectedly on rewind (#62711)

Kirill Bulatovb2d9c2e122fb
diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rs
index 036482afe82..5b51d05b3c8 100644
--- a/crates/agent_ui/src/agent_panel.rs
+++ b/crates/agent_ui/src/agent_panel.rs
@@ -7142,6 +7142,89 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn test_resubmitting_edited_message_keeps_zoomed_agent_panel_open(
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx);
+ cx.update(|cx| {
+ agent::ThreadStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ let project = Project::test(fs, [], cx).await;
+ let multi_workspace =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = multi_workspace
+ .read_with(cx, |multi_workspace, _cx| {
+ multi_workspace.workspace().clone()
+ })
+ .unwrap();
+ let cx = &mut VisualTestContext::from_window(multi_workspace.into(), cx);
+ cx.simulate_resize(size(px(900.), px(700.)));
+
+ let connection = StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Response".into()),
+ )]);
+ let panel = workspace.update_in(cx, |workspace, window, cx| {
+ let panel = cx.new(|cx| AgentPanel::new(workspace, window, cx));
+ workspace.add_panel(panel.clone(), window, cx);
+ workspace.focus_panel::<AgentPanel>(window, cx);
+ panel
+ });
+ open_thread_with_connection(&panel, connection.clone(), cx);
+ send_message(&panel, cx);
+
+ panel.update_in(cx, |panel, window, cx| {
+ panel.toggle_zoom(&ToggleZoom, window, cx);
+ });
+ cx.run_until_parked();
+
+ let thread_view = panel.read_with(cx, |panel, cx| panel.active_thread_view(cx).unwrap());
+ let user_message_editor = thread_view.read_with(cx, |thread_view, cx| {
+ thread_view
+ .entry_view_state
+ .read(cx)
+ .entry(0)
+ .unwrap()
+ .message_editor()
+ .unwrap()
+ .clone()
+ });
+ cx.focus(&user_message_editor);
+ cx.run_until_parked();
+ user_message_editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("Edited message", window, cx);
+ });
+
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("New response".into()),
+ )]);
+ user_message_editor.update_in(cx, |_editor, window, cx| {
+ window.dispatch_action(Box::new(zed_actions::agent::Chat), cx);
+ });
+ cx.update(|window, _cx| window.blur());
+ cx.run_until_parked();
+
+ workspace.update_in(cx, |workspace, window, cx| {
+ let position = panel.read(cx).position(window, cx);
+ assert!(
+ workspace.dock_at_position(position).read(cx).is_open(),
+ "resubmitting an edited message should not hide the agent panel"
+ );
+ assert!(
+ panel.read(cx).is_zoomed(window, cx),
+ "resubmitting an edited message should not close Zen Mode"
+ );
+ assert!(
+ panel.read(cx).focus_handle(cx).contains_focused(window, cx),
+ "focus should stay within the agent panel after resubmitting"
+ );
+ });
+ }
+
#[gpui::test]
async fn test_active_thread_serialize_and_load_round_trip(cx: &mut TestAppContext) {
init_test(cx);
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index 3bb89308eb4..a92ae8f8efc 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -1148,6 +1148,7 @@ pub struct Window {
pub(crate) dirty_views: FxHashSet<EntityId>,
focus_listeners: SubscriberSet<(), AnyWindowFocusListener>,
pub(crate) focus_lost_listeners: SubscriberSet<(), AnyObserver>,
+ focus_lost_path: SmallVec<[FocusId; 8]>,
default_prevented: bool,
mouse_position: Point<Pixels>,
mouse_hit_test: HitTest,
@@ -1830,6 +1831,7 @@ impl Window {
dirty_views: FxHashSet::default(),
focus_listeners: SubscriberSet::new(),
focus_lost_listeners: SubscriberSet::new(),
+ focus_lost_path: SmallVec::new(),
default_prevented: true,
mouse_position,
mouse_hit_test: HitTest::default(),
@@ -2005,6 +2007,17 @@ impl Window {
.and_then(|id| FocusHandle::for_id(id, &cx.focus_handles))
}
+ /// While focus-lost listeners are being dispatched, returns the closest ancestor of the
+ /// previously focused element that can still receive focus, making it a suitable target
+ /// for focus restoration. Returns `None` at all other times, or when no such ancestor exists.
+ pub fn focus_lost_restore_target(&self, cx: &App) -> Option<FocusHandle> {
+ let (_leaf, ancestors) = self.focus_lost_path.split_last()?;
+ ancestors.iter().rev().find_map(|id| {
+ self.rendered_frame.dispatch_tree.focusable_node_id(*id)?;
+ FocusHandle::for_id(*id, &cx.focus_handles)
+ })
+ }
+
/// Move focus to the element associated with the given [`FocusHandle`].
pub fn focus(&mut self, handle: &FocusHandle, cx: &mut App) {
if !self.focus_enabled || self.focus == Some(handle.id) {
@@ -2870,9 +2883,11 @@ impl Window {
|| previous_window_active != current_window_active
{
if !previous_focus_path.is_empty() && current_focus_path.is_empty() {
+ self.focus_lost_path = previous_focus_path.clone();
self.focus_lost_listeners
.clone()
.retain(&(), |listener| listener(self, cx));
+ self.focus_lost_path = SmallVec::new();
// The focus-lost fallback (e.g. a workspace refocusing itself) may target
// an element that isn't part of the element tree, in which case scheduling
// a redraw below would dispatch focus-lost again, looping forever. Only
diff --git a/crates/workspace/src/multi_workspace.rs b/crates/workspace/src/multi_workspace.rs
index df49cf8fca1..78c46a59f2b 100644
--- a/crates/workspace/src/multi_workspace.rs
+++ b/crates/workspace/src/multi_workspace.rs
@@ -1492,28 +1492,7 @@ impl MultiWorkspace {
}
pub fn focus_active_workspace(&self, window: &mut Window, cx: &mut App) {
- // If a dock panel is zoomed, focus it instead of the center pane.
- // Otherwise, focusing the center pane triggers dismiss_zoomed_items_to_reveal
- // which closes the zoomed dock.
- let focus_handle = {
- let workspace = self.workspace().read(cx);
- let mut target = None;
- for dock in workspace.all_docks() {
- let dock = dock.read(cx);
- if dock.is_open() {
- if let Some(panel) = dock.active_panel() {
- if panel.is_zoomed(window, cx) {
- target = Some(panel.activation_focus_handle(cx));
- break;
- }
- }
- }
- }
- target.unwrap_or_else(|| {
- let pane = workspace.active_pane().clone();
- pane.read(cx).focus_handle(cx)
- })
- };
+ let focus_handle = self.workspace().read(cx).fallback_focus_handle(window, cx);
window.focus(&focus_handle, cx);
}
diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs
index 4c0e31e72ef..f8341a39a73 100644
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@ -1665,7 +1665,9 @@ impl Workspace {
}
cx.on_focus_lost(window, |this, window, cx| {
- let focus_handle = this.focus_handle(cx);
+ let focus_handle = window
+ .focus_lost_restore_target(cx)
+ .unwrap_or_else(|| this.fallback_focus_handle(window, cx));
window.focus(&focus_handle, cx);
})
.detach();
@@ -4473,6 +4475,27 @@ impl Workspace {
.find_map(|dock| dock.read(cx).panel::<T>())
}
+ // If a dock panel is zoomed, focus it instead of the center pane.
+ // Otherwise, focusing the center pane triggers dismiss_zoomed_items_to_reveal
+ // which closes the zoomed dock.
+ pub fn fallback_focus_handle(&self, window: &Window, cx: &App) -> FocusHandle {
+ self.all_docks()
+ .into_iter()
+ .find_map(|dock| {
+ let dock = dock.read(cx);
+ if !dock.is_open() {
+ return None;
+ }
+ let panel = dock.active_panel()?;
+ if panel.is_zoomed(window, cx) {
+ Some(panel.activation_focus_handle(cx))
+ } else {
+ None
+ }
+ })
+ .unwrap_or_else(|| self.active_pane.read(cx).focus_handle(cx))
+ }
+
fn dismiss_zoomed_items_to_reveal(
&mut self,
dock_to_reveal: Option<DockPosition>,
@@ -15270,6 +15293,98 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn test_zoomed_panel_stays_open_when_focus_is_lost(cx: &mut gpui::TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+
+ let project = Project::test(fs, [], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
+ let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
+
+ let panel = workspace.update_in(cx, |workspace, window, cx| {
+ let panel = cx.new(|cx| TestPanel::new(DockPosition::Right, 100, cx));
+ workspace.add_panel(panel.clone(), window, cx);
+ workspace.toggle_dock(DockPosition::Right, window, cx);
+ panel
+ });
+ cx.executor().run_until_parked();
+
+ panel.update(cx, |_, cx| cx.emit(PanelEvent::ZoomIn));
+ cx.executor().run_until_parked();
+
+ workspace.update_in(cx, |workspace, window, cx| {
+ assert!(workspace.right_dock().read(cx).is_open());
+ assert_eq!(workspace.zoomed_position, Some(DockPosition::Right));
+ assert!(panel.read(cx).focus_handle(cx).contains_focused(window, cx));
+ });
+
+ cx.update(|window, _| window.blur());
+ cx.executor().run_until_parked();
+ workspace.update(cx, |_, cx| cx.notify());
+ cx.executor().run_until_parked();
+
+ workspace.update_in(cx, |workspace, window, cx| {
+ assert!(workspace.right_dock().read(cx).is_open());
+ assert_eq!(workspace.zoomed_position, Some(DockPosition::Right));
+ assert!(panel.read(cx).focus_handle(cx).contains_focused(window, cx));
+ });
+ }
+
+ #[gpui::test]
+ async fn test_focus_restores_to_panel_when_focused_child_is_removed(
+ cx: &mut gpui::TestAppContext,
+ ) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+
+ let project = Project::test(fs, [], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
+ let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
+
+ let panel = workspace.update_in(cx, |workspace, window, cx| {
+ let panel =
+ cx.new(|cx| TestPanel::new_with_activation_child(DockPosition::Right, 100, cx));
+ workspace.add_panel(panel.clone(), window, cx);
+ workspace.toggle_dock(DockPosition::Right, window, cx);
+ panel
+ });
+ cx.executor().run_until_parked();
+
+ workspace.update_in(cx, |_workspace, window, cx| {
+ assert!(
+ panel
+ .read(cx)
+ .activation_focus_handle
+ .as_ref()
+ .unwrap()
+ .is_focused(window)
+ );
+ });
+
+ panel.update(cx, |panel, cx| {
+ panel.activation_focus_handle = None;
+ cx.notify();
+ });
+ cx.executor().run_until_parked();
+ workspace.update(cx, |_, cx| cx.notify());
+ cx.executor().run_until_parked();
+
+ workspace.update_in(cx, |workspace, window, cx| {
+ assert!(workspace.right_dock().read(cx).is_open());
+ assert!(panel.read(cx).focus_handle(cx).contains_focused(window, cx));
+ assert!(
+ !workspace
+ .active_pane()
+ .read(cx)
+ .focus_handle(cx)
+ .contains_focused(window, cx)
+ );
+ });
+ }
+
#[gpui::test]
async fn test_no_save_prompt_when_multi_buffer_dirty_items_closed(cx: &mut TestAppContext) {
init_test(cx);