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
4 changes: 4 additions & 0 deletions crates/ui/src/list/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ where
let rows_cache = self.rows_cache.clone();
let scrollbar_visible = self.options.scrollbar_visible;
let scroll_handle = self.scroll_handle.clone();
let item_to_measure_index = rows_cache
.position_of(&self.item_to_measure_index)
.unwrap_or(0);

v_flex()
.flex_grow_1()
Expand Down Expand Up @@ -563,6 +566,7 @@ where
.collect::<Vec<_>>()
},
)
.with_item_to_measure_index(item_to_measure_index)
.paddings(self.options.paddings.clone())
.when(self.options.max_height.is_some(), |this| {
this.with_sizing_behavior(ListSizingBehavior::Infer)
Expand Down
15 changes: 12 additions & 3 deletions crates/ui/src/virtual_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ impl VirtualListScrollHandle {
///
/// This is like `uniform_list` in GPUI, but support two axis.
///
/// The `item_sizes` is the size of each column,
/// only the `height` is used, `width` is ignored and VirtualList will measure the first item width.
/// The `item_sizes` is the size of each row. Only the `height` is used; `width` is inferred
/// by measuring the item selected with [`VirtualList::with_item_to_measure_index`], defaulting
/// to the first item.
///
/// See also [`h_virtual_list`]
#[inline]
Expand Down Expand Up @@ -197,6 +198,7 @@ where
item_sizes,
render_items: Box::new(render_range),
sizing_behavior: ListSizingBehavior::default(),
item_to_measure_index: 0,
}
}

Expand All @@ -212,6 +214,7 @@ pub struct VirtualList {
dyn for<'a> Fn(Range<usize>, &'a mut Window, &'a mut App) -> SmallVec<[AnyElement; 64]>,
>,
sizing_behavior: ListSizingBehavior,
item_to_measure_index: usize,
}

impl Styled for VirtualList {
Expand All @@ -233,6 +236,12 @@ impl VirtualList {
self
}

/// Set the item index used to infer the list's cross-axis size.
pub fn with_item_to_measure_index(mut self, index: usize) -> Self {
self.item_to_measure_index = index;
self
}

/// Specify for table.
///
/// Table is special, because the `scroll_handle` is based on Table head (That is not a virtual list).
Expand Down Expand Up @@ -301,7 +310,7 @@ impl VirtualList {
return Size::default();
}

let item_ix = 0;
let item_ix = self.item_to_measure_index.min(self.items_count - 1);
let mut items = (self.render_items)(item_ix..item_ix + 1, window, cx);
let Some(mut item_to_measure) = items.pop() else {
return Size::default();
Expand Down
Loading