popup_menu: Add max_h to set the height of menu (#570)

This commit is contained in:
Floyd Wang 2025-01-23 18:43:34 +08:00 committed by GitHub
parent e03a0d4f4e
commit 1ff84a9943
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View file

@ -302,7 +302,7 @@ impl Render for PopupStory {
Button::new("popup-menu-11112")
.label("Scrollable Menu")
.popup_menu_with_anchor(Corner::TopRight, move |this, _| {
let mut this = this.scrollable();
let mut this = this.scrollable().max_h(px(300.));
for i in 0..100 {
this = this.menu(
SharedString::from(format!("Item {}", i)),

View file

@ -101,6 +101,7 @@ pub struct PopupMenu {
selected_index: Option<usize>,
min_width: Pixels,
max_width: Pixels,
max_height: Option<Pixels>,
hovered_menu_ix: Option<usize>,
bounds: Bounds<Pixels>,
@ -131,6 +132,7 @@ impl PopupMenu {
selected_index: None,
min_width: px(120.),
max_width: px(500.),
max_height: None,
has_icon: false,
hovered_menu_ix: None,
bounds: Bounds::default(),
@ -162,6 +164,12 @@ impl PopupMenu {
self
}
/// Set max height of the popup menu, default is half of the window height
pub fn max_h(mut self, height: impl Into<Pixels>) -> Self {
self.max_height = Some(height.into());
self
}
/// Set the menu to be scrollable to show vertical scrollbar.
///
/// NOTE: If this is true, the sub-menus will cannot be support.
@ -498,9 +506,13 @@ impl Render for PopupMenu {
let items_count = self.menu_items.len();
let max_width = self.max_width;
let bounds = self.bounds;
let window_haft_height = cx.window_bounds().get_bounds().size.height * 0.5;
let max_height = window_haft_height.min(px(450.));
let max_height = self.max_height.map_or_else(
|| {
let window_half_height = cx.window_bounds().get_bounds().size.height * 0.5;
window_half_height.min(px(450.))
},
|height| height,
);
const ITEM_HEIGHT: Pixels = px(26.);