From 7f64f8798eb4b4cf0cbf8145ceec23cf96c865d6 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 8 Aug 2025 19:13:18 +0800 Subject: [PATCH] virtual_list: Avoid scroll when list contents not overflow. (#1125) - Fix scrollbar position in popup menu. --- crates/story/src/menu_story.rs | 55 +++++++++++++++++++++----------- crates/ui/src/menu/popup_menu.rs | 4 +-- crates/ui/src/virtual_list.rs | 1 + 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/crates/story/src/menu_story.rs b/crates/story/src/menu_story.rs index d8451b75..44ce87b8 100644 --- a/crates/story/src/menu_story.rs +++ b/crates/story/src/menu_story.rs @@ -217,24 +217,43 @@ impl Render for MenuStory { }), ) .child( - section("Menu with scrollbar").child( - Button::new("popup-menu-11112") - .outline() - .label("Scrollable Menu") - .popup_menu_with_anchor(Corner::TopRight, move |this, _, _| { - let mut this = this - .scrollable() - .max_h(px(300.)) - .label(format!("Total {} items", 100)); - for i in 0..100 { - this = this.menu( - SharedString::from(format!("Item {}", i)), - Box::new(Info(i)), - ) - } - this.min_w(px(100.)) - }), - ), + section("Menu with scrollbar") + .child( + Button::new("popup-menu-scrollable-1") + .outline() + .label("Scrollable Menu (100 items)") + .popup_menu_with_anchor(Corner::TopRight, move |this, _, _| { + let mut this = this + .scrollable() + .max_h(px(300.)) + .label(format!("Total {} items", 100)); + for i in 0..100 { + this = this.menu( + SharedString::from(format!("Item {}", i)), + Box::new(Info(i)), + ) + } + this.min_w(px(100.)) + }), + ) + .child( + Button::new("popup-menu-scrollable-2") + .outline() + .label("Scrollable Menu (5 items)") + .popup_menu_with_anchor(Corner::TopRight, move |this, _, _| { + let mut this = this + .scrollable() + .max_h(px(300.)) + .label(format!("Total {} items", 100)); + for i in 0..5 { + this = this.menu( + SharedString::from(format!("Item {}", i)), + Box::new(Info(i)), + ) + } + this.min_w(px(100.)) + }), + ), ) } } diff --git a/crates/ui/src/menu/popup_menu.rs b/crates/ui/src/menu/popup_menu.rs index 1f7ebece..1ecb02f8 100644 --- a/crates/ui/src/menu/popup_menu.rs +++ b/crates/ui/src/menu/popup_menu.rs @@ -942,8 +942,8 @@ impl Render for PopupMenu { .absolute() .top_0() .left_0() - .right_0p5() - .bottom_0p5() + .right_0() + .bottom_0() .child(Scrollbar::vertical(&self.scroll_state, &self.scroll_handle)), ) }) diff --git a/crates/ui/src/virtual_list.rs b/crates/ui/src/virtual_list.rs index 50392aaf..a70ce33b 100644 --- a/crates/ui/src/virtual_list.rs +++ b/crates/ui/src/virtual_list.rs @@ -548,6 +548,7 @@ impl Element for VirtualList { scroll_to_item, ); } + scroll_offset = scroll_offset.min(&point(px(0.), px(0.))); self.base.interactivity().prepaint( global_id,