From 1bbc4b9f23c4dc56a7c20e2a298683ecd7d9c71b Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 20 Aug 2025 13:42:25 +0800 Subject: [PATCH] list: Fix List scroll to item by use deferred. (#1160) --- crates/story/src/list_story.rs | 11 ----------- crates/ui/src/list/list.rs | 19 ++++++------------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/crates/story/src/list_story.rs b/crates/story/src/list_story.rs index 8b5eab1e..d94d5caa 100644 --- a/crates/story/src/list_story.rs +++ b/crates/story/src/list_story.rs @@ -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") diff --git a/crates/ui/src/list/list.rs b/crates/ui/src/list/list.rs index 16c6f776..ef80c081 100644 --- a/crates/ui/src/list/list.rs +++ b/crates/ui/src/list/list.rs @@ -60,7 +60,7 @@ pub struct List { pub(crate) size: Size, rows_cache: RowsCache, selected_index: Option, - deferred_scroll_to_index: Option, + deferred_scroll_to_index: Option<(IndexPath, ScrollStrategy)>, mouse_right_clicked_index: Option, 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) { 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); } }