From 37de698a61e95304056cfc1bb3a54c6d3bc6d770 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Wed, 13 Aug 2025 15:12:54 +0800 Subject: [PATCH] list: Fix the `scroll_to_item` not working (#1129) --- crates/story/src/list_story.rs | 9 +-------- crates/ui/src/list/list.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/crates/story/src/list_story.rs b/crates/story/src/list_story.rs index 2f831783..465aa864 100644 --- a/crates/story/src/list_story.rs +++ b/crates/story/src/list_story.rs @@ -523,14 +523,7 @@ impl Render for ListStory { .small() .on_click(cx.listener(|this, _, window, cx| { this.company_list.update(cx, |list, cx| { - if let Some(selected) = list.selected_index() { - list.scroll_to_item( - selected, - ScrollStrategy::Top, - window, - cx, - ); - } + list.scroll_to_selected_item(window, cx); }) })), ) diff --git a/crates/ui/src/list/list.rs b/crates/ui/src/list/list.rs index aff811b7..03e6b294 100644 --- a/crates/ui/src/list/list.rs +++ b/crates/ui/src/list/list.rs @@ -202,7 +202,7 @@ where pub fn scroll_to_item( &mut self, ix: IndexPath, - _strategy: ScrollStrategy, + strategy: ScrollStrategy, _: &mut Window, cx: &mut Context, ) { @@ -215,7 +215,7 @@ where return; } - self.deferred_scroll_to_index = Some(ix); + self.scroll_handle.scroll_to_item(ix.row, strategy); cx.notify(); } @@ -224,10 +224,12 @@ where &self.scroll_handle } - pub fn scroll_to_selected_item(&mut self, _window: &mut Window, cx: &mut Context) { + pub fn scroll_to_selected_item(&mut self, _: &mut Window, cx: &mut Context) { if let Some(ix) = self.selected_index { - self.deferred_scroll_to_index = Some(ix); - cx.notify(); + if let Some(ix) = self.rows_cache.position_of(&ix) { + self.scroll_handle.scroll_to_item(ix, ScrollStrategy::Top); + cx.notify(); + } } }