list: Fix List scroll_to_item position. (#1158)

This commit is contained in:
Jason Lee 2025-08-20 11:30:14 +08:00 committed by GitHub
parent 35ba09a561
commit e29418e9e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 6 deletions

View file

@ -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,

View file

@ -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<Self>) {
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();
}
}