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.

gpui: Fix uniform_list item layout far down long lists (#65288)

Pelotrio679ad7c30427
diff --git a/crates/gpui/src/elements/uniform_list.rs b/crates/gpui/src/elements/uniform_list.rs
index 0a449a20a3c..9ccad0bd1da 100644
--- a/crates/gpui/src/elements/uniform_list.rs
+++ b/crates/gpui/src/elements/uniform_list.rs
@@ -470,10 +470,13 @@ impl Element for UniformList {
scroll_offset = *updated_scroll_offset
}
+ let scroll_top = -scroll_offset.y.to_f64();
+ let item_height_f64 = item_height.to_f64();
let first_visible_element_ix =
- (-(scroll_offset.y + padding.top) / item_height).floor() as usize;
- let last_visible_element_ix = ((-scroll_offset.y + padded_bounds.size.height)
- / item_height)
+ ((scroll_top - padding.top.to_f64()) / item_height_f64).floor() as usize;
+ let last_visible_element_ix = ((scroll_top
+ + padded_bounds.size.height.to_f64())
+ / item_height_f64)
.ceil() as usize;
let visible_range = first_visible_element_ix
@@ -492,9 +495,9 @@ impl Element for UniformList {
let content_mask = ContentMask { bounds };
window.with_content_mask(Some(content_mask), |window| {
for (mut item, ix) in items.into_iter().zip(visible_range.clone()) {
+ let item_top = item_height_f64 * ix as f64 - scroll_top;
let item_origin = padded_bounds.origin
- + scroll_offset
- + point(Pixels::ZERO, item_height * ix);
+ + point(scroll_offset.x, Pixels::from(item_top));
let available_width = if can_scroll_horizontally {
padded_bounds.size.width + scroll_offset.x.abs()
@@ -862,4 +865,72 @@ mod test {
})
}
}
+
+ #[gpui::test]
+ fn test_item_positions_far_down_long_list(cx: &mut TestAppContext) {
+ use crate::{
+ Bounds, Context, Pixels, UniformListScrollHandle, Window, canvas, point, prelude::*,
+ px, uniform_list,
+ };
+ use std::{cell::RefCell, ops::Range, rc::Rc};
+
+ struct TestView {
+ scroll_handle: UniformListScrollHandle,
+ item_bounds: Rc<RefCell<Vec<(usize, Bounds<Pixels>)>>>,
+ }
+
+ impl Render for TestView {
+ fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
+ let item_bounds = self.item_bounds.clone();
+ uniform_list(
+ "entries",
+ 10_000_000,
+ cx.processor(move |_, range: Range<usize>, _window, _cx| {
+ range
+ .map(|ix| {
+ let item_bounds = item_bounds.clone();
+ canvas(
+ move |bounds, _, _| item_bounds.borrow_mut().push((ix, bounds)),
+ |_, _, _, _| {},
+ )
+ .w_full()
+ .h(px(24.0))
+ })
+ .collect()
+ }),
+ )
+ .track_scroll(&self.scroll_handle)
+ .h(px(200.0))
+ }
+ }
+
+ let item_bounds = Rc::new(RefCell::new(Vec::new()));
+ let (view, cx) = cx.add_window_view(|_, _| TestView {
+ scroll_handle: UniformListScrollHandle::new(),
+ item_bounds: item_bounds.clone(),
+ });
+
+ // Past 2^27px, f32 can only represent multiples of 16px. This offset is 16px into item
+ // 9_000_002, and the items must still be laid out 24px apart from that point.
+ item_bounds.borrow_mut().clear();
+ view.update(cx, |view, cx| {
+ view.scroll_handle
+ .0
+ .borrow()
+ .base_handle
+ .set_offset(point(px(0.0), px(-216_000_064.0)));
+ cx.notify();
+ });
+ cx.run_until_parked();
+
+ let item_tops = item_bounds
+ .borrow()
+ .iter()
+ .map(|(ix, bounds)| (*ix, bounds.origin.y))
+ .collect::<Vec<_>>();
+ let expected_tops = (9_000_002..9_000_011)
+ .map(|ix| (ix, px(-16.0 + 24.0 * (ix - 9_000_002) as f32)))
+ .collect::<Vec<_>>();
+ assert_eq!(item_tops, expected_tops);
+ }
}