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( .child(
Checkbox::new("loading") Checkbox::new("loading")
.label("Loading") .label("Loading")

View file

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