gpui: Scroll to an item requested before a scroll handle's first frame

A div applied a pending `ScrollHandle::scroll_to_item` request using the
bounds and overflow the handle remembered from its previous frame. On
the first frame those are empty and non-scrolling, so the request was
consumed without moving anything. They are now taken from the frame
being drawn.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Nathan Sobo
2026-11-18 19:40:22 -07:00
parent fd95a5e59e
commit abf8dc2bb7
+55 -9
View File
@@ -2114,10 +2114,6 @@ impl Element for Div {
(child_max - child_min).into()
};
if let Some(scroll_handle) = self.interactivity.tracked_scroll_handle.as_ref() {
scroll_handle.scroll_to_active_item();
}
self.interactivity.prepaint(
global_id,
inspector_id,
@@ -2543,14 +2539,22 @@ impl Interactivity {
if let Some(scroll_offset) = self.scroll_offset.as_ref() {
let mut scroll_to_bottom = false;
let mut tracked_scroll_handle = self
if let Some(scroll_handle) = self.tracked_scroll_handle.as_ref() {
{
let mut scroll_handle_state = scroll_handle.0.borrow_mut();
scroll_handle_state.overflow = style.overflow;
// Scrolling to an item needs this frame's bounds and overflow.
// What the handle remembers is from the last frame, and is
// empty the first time the element is drawn.
scroll_handle_state.bounds = bounds;
scroll_to_bottom = mem::take(&mut scroll_handle_state.scroll_to_bottom);
}
scroll_handle.scroll_to_active_item();
}
let tracked_scroll_handle = self
.tracked_scroll_handle
.as_ref()
.map(|handle| handle.0.borrow_mut());
if let Some(scroll_handle_state) = tracked_scroll_handle.as_deref_mut() {
scroll_handle_state.overflow = style.overflow;
scroll_to_bottom = mem::take(&mut scroll_handle_state.scroll_to_bottom);
}
let rem_size = window.rem_size();
// Taffy lays the box out with the padding snapped to the device pixel
@@ -5778,6 +5782,48 @@ mod tests {
assert_eq!(scroll_handle.max_offset().y, px(0.));
}
#[gpui::test]
fn test_scrolls_to_an_item_requested_before_the_first_frame(cx: &mut TestAppContext) {
struct Strip {
scroll_handle: ScrollHandle,
}
impl Render for Strip {
fn render(
&mut self,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> impl IntoElement {
div().size_full().child(
div()
.id("strip")
.flex()
.h(px(20.))
.w(px(100.))
.overflow_x_scroll()
.track_scroll(&self.scroll_handle)
.children((0..5).map(|_| div().flex_none().w(px(40.)).h_full())),
)
}
}
// The handle has never been drawn, so it knows neither its element's
// bounds nor that the element scrolls.
let scroll_handle = ScrollHandle::new();
scroll_handle.scroll_to_item(4);
let window: AnyWindowHandle = cx
.add_window({
let scroll_handle = scroll_handle.clone();
move |_, _| Strip { scroll_handle }
})
.into();
cx.update_window(window, |_, window, cx| window.draw(cx).clear(cx))
.unwrap();
// Five 40px items in a 100px strip: the last one ends at 200px.
assert_eq!(scroll_handle.offset().x, px(-100.));
}
struct ContentSizedGrid;
impl Render for ContentSizedGrid {