menu: Add label item to PopupMenu. (#733)

<img width="883" alt="image"
src="https://github.com/user-attachments/assets/06071f62-ddd3-4f9e-ae2f-82b583089792"
/>
This commit is contained in:
Jason Lee 2025-03-24 22:04:47 +08:00 committed by GitHub
parent f759cc5023
commit e0f4a7d3cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 56 additions and 32 deletions

View file

@ -180,6 +180,7 @@ impl Render for PopupStory {
.menu("Copy", Box::new(Copy))
.menu("Paste", Box::new(Paste))
.separator()
.label("This is a label")
.menu_with_check("Toggle Check", checked, Box::new(ToggleCheck))
.separator()
.submenu("Settings", window, cx, move |menu, _, _| {
@ -307,7 +308,10 @@ 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().max_h(px(300.));
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)),

View file

@ -279,37 +279,42 @@ impl Render for FontSizeSelector {
.ghost()
.icon(IconName::Settings2)
.popup_menu(move |this, _, _| {
this.menu_with_check(
"Font Large",
font_size == 18,
Box::new(SelectFont(18)),
)
.menu_with_check("Font Default", font_size == 16, Box::new(SelectFont(16)))
.menu_with_check("Font Small", font_size == 14, Box::new(SelectFont(14)))
.separator()
.menu_with_check("Radius 8px", radius == 8, Box::new(SelectRadius(8)))
.menu_with_check(
"Radius 4px (default)",
radius == 4,
Box::new(SelectRadius(4)),
)
.menu_with_check("Radius 0px", radius == 0, Box::new(SelectRadius(0)))
.separator()
.menu_with_check(
"Scrolling to show Scrollbar",
scroll_show == ScrollbarShow::Scrolling,
Box::new(SelectScrollbarShow(ScrollbarShow::Scrolling)),
)
.menu_with_check(
"Hover to show Scrollbar",
scroll_show == ScrollbarShow::Hover,
Box::new(SelectScrollbarShow(ScrollbarShow::Hover)),
)
.menu_with_check(
"Always show Scrollbar",
scroll_show == ScrollbarShow::Always,
Box::new(SelectScrollbarShow(ScrollbarShow::Always)),
)
this.scrollable()
.max_h(px(480.))
.label("Font Size")
.menu_with_check("Large", font_size == 18, Box::new(SelectFont(18)))
.menu_with_check(
"Medium (default)",
font_size == 16,
Box::new(SelectFont(16)),
)
.menu_with_check("Small", font_size == 14, Box::new(SelectFont(14)))
.separator()
.label("Border Radius")
.menu_with_check("8px", radius == 8, Box::new(SelectRadius(8)))
.menu_with_check(
"4px (default)",
radius == 4,
Box::new(SelectRadius(4)),
)
.menu_with_check("0px", radius == 0, Box::new(SelectRadius(0)))
.separator()
.label("Scrollbar")
.menu_with_check(
"Scrolling to show",
scroll_show == ScrollbarShow::Scrolling,
Box::new(SelectScrollbarShow(ScrollbarShow::Scrolling)),
)
.menu_with_check(
"Hover to show",
scroll_show == ScrollbarShow::Hover,
Box::new(SelectScrollbarShow(ScrollbarShow::Hover)),
)
.menu_with_check(
"Always show",
scroll_show == ScrollbarShow::Always,
Box::new(SelectScrollbarShow(ScrollbarShow::Always)),
)
})
.anchor(Corner::TopRight),
)

View file

@ -61,6 +61,7 @@ impl PopupMenuExt for Button {}
enum PopupMenuItem {
Separator,
Label(SharedString),
Item {
icon: Option<Icon>,
label: SharedString,
@ -191,6 +192,12 @@ impl PopupMenu {
self
}
/// Add label
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.menu_items.push(PopupMenuItem::Label(label.into()));
self
}
/// Add Menu to open link
pub fn link(self, label: impl Into<SharedString>, href: impl Into<String>) -> Self {
self.link_with_disabled(label, href, false)
@ -690,6 +697,14 @@ impl PopupMenu {
.my_0p5()
.bg(cx.theme().muted),
),
PopupMenuItem::Label(label) => this.disabled(true).cursor_default().child(
h_flex()
.cursor_default()
.items_center()
.gap_x_1()
.children(Self::render_icon(has_icon, None, window, cx))
.child(label.clone()),
),
PopupMenuItem::ElementItem {
render,
icon,