From e29418e9e4bfa93e509eb51b2edbf528a89d0796 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 20 Aug 2025 11:30:14 +0800 Subject: [PATCH] list: Fix List scroll_to_item position. (#1158) --- crates/story/src/list_story.rs | 17 ++++++++++++++--- crates/ui/src/list/list.rs | 9 ++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/story/src/list_story.rs b/crates/story/src/list_story.rs index 866eb184..8b5eab1e 100644 --- a/crates/story/src/list_story.rs +++ b/crates/story/src/list_story.rs @@ -478,14 +478,25 @@ impl Render for ListStory { })), ) .child( - Button::new("scroll-center") + Button::new("scroll-selected") .outline() - .child("Scroll to section 2") + .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( + Button::new("scroll-to-item") + .outline() + .child("Scroll to (5, 1)") .small() .on_click(cx.listener(|this, _, window, cx| { this.company_list.update(cx, |list, cx| { list.scroll_to_item( - IndexPath::default().section(1).row(0), + IndexPath::new(1).section(5), ScrollStrategy::Center, window, cx, diff --git a/crates/ui/src/list/list.rs b/crates/ui/src/list/list.rs index 03e6b294..16c6f776 100644 --- a/crates/ui/src/list/list.rs +++ b/crates/ui/src/list/list.rs @@ -215,7 +215,9 @@ where return; } - self.scroll_handle.scroll_to_item(ix.row, strategy); + if let Some(item_ix) = self.rows_cache.position_of(&ix) { + self.scroll_handle.scroll_to_item(item_ix, strategy); + } cx.notify(); } @@ -226,8 +228,9 @@ where pub fn scroll_to_selected_item(&mut self, _: &mut Window, cx: &mut Context) { if let Some(ix) = self.selected_index { - if let Some(ix) = self.rows_cache.position_of(&ix) { - self.scroll_handle.scroll_to_item(ix, ScrollStrategy::Top); + if let Some(item_ix) = self.rows_cache.position_of(&ix) { + self.scroll_handle + .scroll_to_item(item_ix, ScrollStrategy::Top); cx.notify(); } }