list: Fix List scroll to item by use deferred. (#1160)

This commit is contained in:
Jason Lee 2025-08-20 13:42:25 +08:00
parent 8ae4ffbb0c
commit 1bbc4b9f23
2 changed files with 6 additions and 24 deletions

View file

@ -527,17 +527,6 @@ impl Render for ListStory {
})
})),
)
.child(
Button::new("scroll-to-selected")
.outline()
.child("Scroll to Selected")
.small()
.on_click(cx.listener(|this, _, window, cx| {
this.company_list.update(cx, |list, cx| {
list.scroll_to_selected_item(window, cx);
})
})),
)
.child(
Checkbox::new("loading")
.label("Loading")

View file

@ -60,7 +60,7 @@ pub struct List<D: ListDelegate> {
pub(crate) size: Size,
rows_cache: RowsCache,
selected_index: Option<IndexPath>,
deferred_scroll_to_index: Option<IndexPath>,
deferred_scroll_to_index: Option<(IndexPath, ScrollStrategy)>,
mouse_right_clicked_index: Option<IndexPath>,
reset_on_cancel: bool,
_search_task: Task<()>,
@ -214,10 +214,7 @@ where
cx.notify();
return;
}
if let Some(item_ix) = self.rows_cache.position_of(&ix) {
self.scroll_handle.scroll_to_item(item_ix, strategy);
}
self.deferred_scroll_to_index = Some((ix, strategy));
cx.notify();
}
@ -228,11 +225,8 @@ where
pub fn scroll_to_selected_item(&mut self, _: &mut Window, cx: &mut Context<Self>) {
if let Some(ix) = self.selected_index {
if let Some(item_ix) = self.rows_cache.position_of(&ix) {
self.scroll_handle
.scroll_to_item(item_ix, ScrollStrategy::Top);
cx.notify();
}
self.deferred_scroll_to_index = Some((ix, ScrollStrategy::Top));
cx.notify();
}
}
@ -575,10 +569,9 @@ where
self.prepare_items_if_needed(window, cx);
// Scroll to the selected item if it is set.
if let Some(ix) = self.deferred_scroll_to_index.take() {
if let Some((ix, strategy)) = self.deferred_scroll_to_index.take() {
if let Some(item_ix) = self.rows_cache.position_of(&ix) {
self.scroll_handle
.scroll_to_item(item_ix, ScrollStrategy::Top);
self.scroll_handle.scroll_to_item(item_ix, strategy);
}
}