Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions crates/gpui/src/elements/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ struct ListItemSummary {
height: Pixels,
has_focus_handles: bool,
has_unknown_height: bool,
unknown_height_count: usize,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -685,6 +686,26 @@ impl ListState {
None
}

/// Top of item `ix` within the list content, and the content height.
/// Items with no known height count as the mean known height, so the
/// numbers stay proportional before every item has been laid out.
pub fn item_top_and_content_height(&self, ix: usize) -> (Pixels, Pixels) {
let state = &*self.0.borrow();
let total = state.items.summary();
let known = total.count - total.unknown_height_count;
let mean = if known > 0 {
total.height.0 / known as f32
} else {
0.
};
let mut cursor = state.items.cursor::<ListItemSummary>(());
let before: ListItemSummary = cursor.summary(&Count(ix), Bias::Right);
(
px(before.height.0 + mean * before.unknown_height_count as f32),
px(total.height.0 + mean * total.unknown_height_count as f32),
)
}
Comment on lines +694 to +707

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When the list has nonzero style padding, this method reports item 0 at 0 and omits both padding edges from content height. List places the first item at padding.top and includes both edges in scrollable content, so add the recorded padding to both returned values.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At crates/gpui/src/elements/list.rs, line 694:

<comment>When the list has nonzero style padding, this method reports item 0 at 0 and omits both padding edges from content height. `List` places the first item at `padding.top` and includes both edges in scrollable content, so add the recorded padding to both returned values.</comment>

<file context>
@@ -685,6 +686,26 @@ impl ListState {
+    /// numbers stay proportional before every item has been laid out.
+    pub fn item_top_and_content_height(&self, ix: usize) -> (Pixels, Pixels) {
+        let state = &*self.0.borrow();
+        let total = state.items.summary();
+        let known = total.count - total.unknown_height_count;
+        let mean = if known > 0 {
</file context>
Suggested change
let total = state.items.summary();
let known = total.count - total.unknown_height_count;
let mean = if known > 0 {
total.height.0 / known as f32
} else {
0.
};
let mut cursor = state.items.cursor::<ListItemSummary>(());
let before: ListItemSummary = cursor.summary(&Count(ix), Bias::Right);
(
px(before.height.0 + mean * before.unknown_height_count as f32),
px(total.height.0 + mean * total.unknown_height_count as f32),
)
}
let total = state.items.summary();
let padding = state.last_padding.unwrap_or_default();
let known = total.count - total.unknown_height_count;
let mean = if known > 0 {
total.height.0 / known as f32
} else {
0.
};
let mut cursor = state.items.cursor::<ListItemSummary>(());
let before: ListItemSummary = cursor.summary(&Count(ix), Bias::Right);
(
padding.top + px(before.height.0 + mean * before.unknown_height_count as f32),
total.height
+ padding.top
+ padding.bottom
+ px(mean * total.unknown_height_count as f32),
)


/// Call this method when the user starts dragging the scrollbar.
///
/// This will prevent the height reported to the scrollbar from changing during the drag
Expand Down Expand Up @@ -1597,6 +1618,7 @@ impl sum_tree::Item for ListItem {
},
has_focus_handles: focus_handle.is_some(),
has_unknown_height: size_hint.is_none(),
unknown_height_count: usize::from(size_hint.is_none()),
},
ListItem::Measured {
size, focus_handle, ..
Expand All @@ -1607,6 +1629,7 @@ impl sum_tree::Item for ListItem {
height: size.height,
has_focus_handles: focus_handle.is_some(),
has_unknown_height: false,
unknown_height_count: 0,
},
}
}
Expand All @@ -1624,6 +1647,7 @@ impl sum_tree::ContextLessSummary for ListItemSummary {
self.height += summary.height;
self.has_focus_handles |= summary.has_focus_handles;
self.has_unknown_height |= summary.has_unknown_height;
self.unknown_height_count += summary.unknown_height_count;
}
}

Expand Down Expand Up @@ -1951,6 +1975,45 @@ mod test {
assert_eq!(state.item_is_below_viewport(0), Some(false));
}

#[gpui::test]
fn test_item_top_tracks_measured_and_unmeasured_heights(cx: &mut TestAppContext) {
let cx = cx.add_empty_window();

struct TestView(ListState);
impl Render for TestView {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
list(self.0.clone(), |ix, _, _| {
div().h(px(10. * (ix + 1) as f32)).w_full().into_any()
})
.w_full()
.h_full()
}
}

let state = ListState::new(4, crate::ListAlignment::Top, px(0.)).measure_all();
let view = cx.update(|_, cx| cx.new(|_| TestView(state.clone())));
cx.draw(point(px(0.), px(0.)), size(px(100.), px(200.)), |_, _| {
view.into_any_element()
});
assert_eq!(state.item_top_and_content_height(2), (px(30.), px(100.)));

struct Uniform(ListState);
impl Render for Uniform {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
list(self.0.clone(), |_, _, _| div().h(px(50.)).w_full().into_any())
.w_full()
.h_full()
}
}
let state = ListState::new(10, crate::ListAlignment::Top, px(0.));
let view = cx.update(|_, cx| cx.new(|_| Uniform(state.clone())));
cx.draw(point(px(0.), px(0.)), size(px(100.), px(100.)), |_, _| {
view.into_any_element()
});
// Only the visible items are measured; the rest count as the mean.
assert_eq!(state.item_top_and_content_height(5), (px(250.), px(500.)));
}

#[gpui::test]
fn test_measure_all_after_width_change(cx: &mut TestAppContext) {
let cx = cx.add_empty_window();
Expand Down
Loading