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.

Fix agent permission row flicker when scrolled away from a tall plan (#58689)

MartinYe12341cecd7d66a5f
diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rs
index 956c201780a..1134b6ded2f 100644
--- a/crates/agent_ui/src/conversation_view.rs
+++ b/crates/agent_ui/src/conversation_view.rs
@@ -8286,6 +8286,71 @@ pub(crate) mod tests {
});
}
+ #[gpui::test]
+ async fn test_permission_row_does_not_flicker_when_activity_bar_squeezes_list(
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx);
+
+ let (_view, thread_view, _entry_ix, cx) =
+ setup_pending_permission_thread("perm-flicker", cx).await;
+
+ // Give the pending tool call tall content (like a full plan awaiting
+ // approval), so the floating row embedding it dwarfs the panel.
+ let thread = thread_view.read_with(cx, |view, _cx| view.thread.clone());
+ thread.update(cx, |thread, cx| {
+ thread
+ .handle_session_update(
+ acp::SessionUpdate::ToolCallUpdate(acp::ToolCallUpdate::new(
+ acp::ToolCallId::new("perm-flicker"),
+ acp::ToolCallUpdateFields::new().content(vec![
+ acp::ToolCallContent::Content(acp::Content::new(
+ acp::ContentBlock::Text(acp::TextContent::new(
+ "Plan step\n\n".repeat(100),
+ )),
+ )),
+ ]),
+ )),
+ cx,
+ )
+ .expect("tool call content update should be accepted");
+ });
+ cx.run_until_parked();
+
+ // Park the inline prompt below the viewport so the floating row renders.
+ thread_view.read_with(cx, |view, _cx| {
+ view.list_state.scroll_to(ListOffset {
+ item_ix: 0,
+ offset_in_item: px(0.0),
+ });
+ });
+
+ // Drive several real window draws. Each draw lays out the activity bar
+ // (containing the floating row) and the conversation list together, so
+ // the row's height feeds back into the list viewport height that the
+ // next frame's visibility decision is based on. Since showing the row
+ // squeezes the list to zero height, a decision that treats a
+ // zero-height viewport as "unknown" makes the row's visibility
+ // oscillate from frame to frame, flickering between the conversation
+ // and the permission prompt.
+ let mut row_visibility = Vec::new();
+ for _ in 0..4 {
+ thread_view.update(cx, |_, cx| cx.notify());
+ cx.run_until_parked();
+ thread_view.update_in(cx, |view, window, cx| {
+ row_visibility.push(
+ view.render_main_agent_awaiting_permission(window, cx)
+ .is_some(),
+ );
+ });
+ }
+ assert_eq!(
+ row_visibility,
+ vec![true; 4],
+ "Floating row visibility must be stable across frames (false entries mean flicker)"
+ );
+ }
+
#[gpui::test]
async fn test_permission_row_shown_when_inline_prompt_is_above_viewport(
cx: &mut TestAppContext,
diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs
index 0fabed42000..f69b7a7fa3d 100644
--- a/crates/agent_ui/src/conversation_view/thread_view.rs
+++ b/crates/agent_ui/src/conversation_view/thread_view.rs
@@ -649,11 +649,15 @@ pub struct TurnFields {
///
/// `Standalone` draws its own border/margin/location header. `Embedded` is
/// hosted by a container that provides its own framing (e.g. the subagent
-/// card or the main-agent awaiting-permission row).
+/// card). `Floating` is like `Embedded`, but used for the floating
+/// awaiting-permission row above the message editor: the tool call's content
+/// is height-capped and scrollable so the row can never grow to consume the
+/// entire panel and squeeze the conversation list out of view.
#[derive(Copy, Clone, PartialEq, Eq)]
enum ToolCallLayout {
Standalone,
Embedded,
+ Floating,
}
fn full_path_for_empty_project_path(file: &dyn language::File, cx: &App) -> Option<String> {
@@ -3137,7 +3141,7 @@ impl ThreadView {
entry_ix,
tool_call,
&focus_handle,
- ToolCallLayout::Embedded,
+ ToolCallLayout::Floating,
window,
cx,
);
@@ -7113,14 +7117,11 @@ impl ThreadView {
let tool_output_display = if is_open {
match &tool_call.status {
- ToolCallStatus::WaitingForConfirmation { options, .. } => v_flex()
- .w_full()
- .children(
- tool_call
- .content
- .iter()
- .enumerate()
- .map(|(content_ix, content)| {
+ ToolCallStatus::WaitingForConfirmation { options, .. } => {
+ let confirmation_content = v_flex()
+ .w_full()
+ .children(tool_call.content.iter().enumerate().map(
+ |(content_ix, content)| {
div()
.child(self.render_tool_call_content(
active_session_id,
@@ -7135,94 +7136,119 @@ impl ThreadView {
cx,
))
.into_any_element()
- }),
- )
- .when_some(
- tool_call.sandbox_authorization_details.as_ref(),
- |this, details| {
- this.child(self.render_sandbox_authorization_details(
- entry_ix,
- &tool_call.id,
- details,
- cx,
- ))
- },
- )
- .when(should_show_raw_input, |this| {
- let is_raw_input_expanded =
- self.expanded_tool_call_raw_inputs.contains(&tool_call.id);
+ },
+ ))
+ .when_some(
+ tool_call.sandbox_authorization_details.as_ref(),
+ |this, details| {
+ this.child(self.render_sandbox_authorization_details(
+ entry_ix,
+ &tool_call.id,
+ details,
+ cx,
+ ))
+ },
+ )
+ .when(should_show_raw_input, |this| {
+ let is_raw_input_expanded =
+ self.expanded_tool_call_raw_inputs.contains(&tool_call.id);
- let input_header = if is_raw_input_expanded {
- "Raw Input:"
- } else {
- "View Raw Input"
- };
+ let input_header = if is_raw_input_expanded {
+ "Raw Input:"
+ } else {
+ "View Raw Input"
+ };
- this.child(
- v_flex()
- .p_2()
- .gap_1()
- .border_t_1()
- .border_color(self.tool_card_border_color(cx))
- .child(
- h_flex()
- .id("disclosure_container")
- .pl_0p5()
- .gap_1()
- .justify_between()
- .rounded_xs()
- .hover(|s| s.bg(cx.theme().colors().element_hover))
- .child(input_output_header(input_header.into()))
- .child(
- Disclosure::new(
- ("raw-input-disclosure", entry_ix),
- is_raw_input_expanded,
+ this.child(
+ v_flex()
+ .p_2()
+ .gap_1()
+ .border_t_1()
+ .border_color(self.tool_card_border_color(cx))
+ .child(
+ h_flex()
+ .id("disclosure_container")
+ .pl_0p5()
+ .gap_1()
+ .justify_between()
+ .rounded_xs()
+ .hover(|s| s.bg(cx.theme().colors().element_hover))
+ .child(input_output_header(input_header.into()))
+ .child(
+ Disclosure::new(
+ ("raw-input-disclosure", entry_ix),
+ is_raw_input_expanded,
+ )
+ .opened_icon(IconName::ChevronUp)
+ .closed_icon(IconName::ChevronDown),
)
- .opened_icon(IconName::ChevronUp)
- .closed_icon(IconName::ChevronDown),
- )
- .on_click(cx.listener({
- let id = tool_call.id.clone();
-
- move |this: &mut Self, _, _, cx| {
- if this.expanded_tool_call_raw_inputs.contains(&id)
- {
- this.expanded_tool_call_raw_inputs.remove(&id);
- } else {
- this.expanded_tool_call_raw_inputs
- .insert(id.clone());
+ .on_click(cx.listener({
+ let id = tool_call.id.clone();
+
+ move |this: &mut Self, _, _, cx| {
+ if this
+ .expanded_tool_call_raw_inputs
+ .contains(&id)
+ {
+ this.expanded_tool_call_raw_inputs
+ .remove(&id);
+ } else {
+ this.expanded_tool_call_raw_inputs
+ .insert(id.clone());
+ }
+ cx.notify();
}
- cx.notify();
- }
- })),
- )
- .when(is_raw_input_expanded, |this| {
- this.children(tool_call.raw_input_markdown.clone().map(
- |input| {
- self.render_markdown(
- input,
- MarkdownStyle::themed(
- MarkdownFont::Agent,
- window,
+ })),
+ )
+ .when(is_raw_input_expanded, |this| {
+ this.children(tool_call.raw_input_markdown.clone().map(
+ |input| {
+ self.render_markdown(
+ input,
+ MarkdownStyle::themed(
+ MarkdownFont::Agent,
+ window,
+ cx,
+ ),
cx,
- ),
- cx,
- )
- },
- ))
- }),
- )
- })
- .child(self.render_permission_buttons(
- self.thread.read(cx).session_id().clone(),
- self.is_first_tool_call(active_session_id, &tool_call.id, cx),
- options,
- entry_ix,
- tool_call.id.clone(),
- focus_handle,
- cx,
- ))
- .into_any(),
+ )
+ },
+ ))
+ }),
+ )
+ });
+
+ v_flex()
+ .w_full()
+ .map(|this| {
+ if layout == ToolCallLayout::Floating {
+ // Cap the content (e.g. a full plan awaiting
+ // approval) so the floating row can never
+ // consume the entire panel and squeeze the
+ // conversation list to zero height, while the
+ // permission buttons below stay visible.
+ this.child(
+ div()
+ .id(("floating-confirmation-content", entry_ix))
+ .max_h_40()
+ .overflow_y_scroll()
+ .child(confirmation_content),
+ )
+ } else {
+ this.child(confirmation_content)
+ }
+ })
+ .child(self.render_permission_buttons(
+ self.thread.read(cx).session_id().clone(),
+ self.is_first_tool_call(active_session_id, &tool_call.id, cx),
+ options,
+ entry_ix,
+ tool_call.id.clone(),
+ focus_handle,
+ cx,
+ ))
+ .into_any()
+ }
ToolCallStatus::Pending | ToolCallStatus::InProgress
if is_edit
&& tool_call.content.is_empty()
@@ -7315,7 +7341,10 @@ impl ThreadView {
v_flex()
.map(|this| {
- if layout == ToolCallLayout::Embedded {
+ if matches!(
+ layout,
+ ToolCallLayout::Embedded | ToolCallLayout::Floating
+ ) {
this
} else if use_card_layout {
this.my_1p5()
diff --git a/crates/gpui/src/elements/list.rs b/crates/gpui/src/elements/list.rs
index 5a729dcc5f5..725f517a122 100644
--- a/crates/gpui/src/elements/list.rs
+++ b/crates/gpui/src/elements/list.rs
@@ -744,11 +744,13 @@ impl ListState {
/// Returns whether the item is entirely above the viewport, or `None` if
/// the list has not measured enough layout to know.
+ ///
+ /// A zero-height viewport still yields a definitive answer: callers may
+ /// size sibling UI based on this query (potentially squeezing the list
+ /// itself to zero height), so returning `None` in that case would make
+ /// the answer oscillate from frame to frame.
pub fn item_is_above_viewport(&self, ix: usize) -> Option<bool> {
- let viewport_bounds = self.viewport_bounds();
- if viewport_bounds.size.height == px(0.0) {
- return None;
- }
+ let viewport_bounds = self.0.borrow().last_layout_bounds?;
let scroll_top = self.logical_scroll_top();
if ix < scroll_top.item_ix {
@@ -763,11 +765,11 @@ impl ListState {
/// Returns whether the item is entirely below the viewport, or `None` if
/// the list has not measured enough layout to know.
+ ///
+ /// See [`Self::item_is_above_viewport`] for why a zero-height viewport
+ /// still yields a definitive answer.
pub fn item_is_below_viewport(&self, ix: usize) -> Option<bool> {
- let viewport_bounds = self.viewport_bounds();
- if viewport_bounds.size.height == px(0.0) {
- return None;
- }
+ let viewport_bounds = self.0.borrow().last_layout_bounds?;
let scroll_top = self.logical_scroll_top();
if ix < scroll_top.item_ix {
@@ -1773,6 +1775,37 @@ mod test {
assert_eq!(state.item_is_below_viewport(3), Some(true));
}
+ #[gpui::test]
+ fn test_item_viewport_queries_remain_stable_with_zero_height_viewport(cx: &mut TestAppContext) {
+ let cx = cx.add_empty_window();
+
+ let state = ListState::new(5, crate::ListAlignment::Top, px(10.)).measure_all();
+
+ state.scroll_to(gpui::ListOffset {
+ item_ix: 2,
+ offset_in_item: px(0.),
+ });
+ cx.draw(point(px(0.), px(0.)), size(px(100.), px(20.)), |_, cx| {
+ cx.new(|_| TestListView(state.clone())).into_any_element()
+ });
+
+ assert_eq!(state.item_is_above_viewport(3), Some(false));
+ assert_eq!(state.item_is_below_viewport(3), Some(true));
+
+ // Squeeze the list to zero height, e.g. because a sibling element
+ // (sized based on the queries above) consumed all the space. The
+ // answers must remain definitive rather than becoming `None`,
+ // otherwise the sibling's size can oscillate between frames.
+ cx.draw(point(px(0.), px(0.)), size(px(100.), px(0.)), |_, cx| {
+ cx.new(|_| TestListView(state.clone())).into_any_element()
+ });
+
+ assert_eq!(state.item_is_above_viewport(1), Some(true));
+ assert_eq!(state.item_is_below_viewport(1), Some(false));
+ assert_eq!(state.item_is_above_viewport(3), Some(false));
+ assert_eq!(state.item_is_below_viewport(3), Some(true));
+ }
+
#[gpui::test]
fn test_item_viewport_queries_after_scroll_to_end_before_layout(cx: &mut TestAppContext) {
let cx = cx.add_empty_window();