menu: Improve Menu to avoid invalid or duplicate separators. (#276)

This commit is contained in:
Jason Lee 2024-09-26 21:28:42 +08:00 committed by GitHub
parent 7a8eaefd30
commit 709c706adf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 125 additions and 88 deletions

View file

@ -162,10 +162,12 @@ impl Render for PopupStory {
}))
.context_menu({
move |this, cx| {
this.menu("Cut", Box::new(Cut))
this.separator()
.menu("Cut", Box::new(Cut))
.menu("Copy", Box::new(Copy))
.menu("Paste", Box::new(Paste))
.separator()
.separator()
.submenu("Settings", cx, move |menu, _| {
menu.menu_with_check(
"Toggle Window Mode",
@ -179,6 +181,7 @@ impl Render for PopupStory {
})
.separator()
.menu("Search All", Box::new(SearchAll))
.separator()
}
})
.gap_6()

View file

@ -59,6 +59,10 @@ impl PopupMenuItem {
!matches!(self, PopupMenuItem::Separator)
}
fn is_separator(&self) -> bool {
matches!(self, PopupMenuItem::Separator)
}
fn has_icon(&self) -> bool {
matches!(self, PopupMenuItem::Item { icon: Some(_), .. })
}
@ -232,6 +236,14 @@ impl PopupMenu {
/// Add a separator Menu Item
pub fn separator(mut self) -> Self {
if self.menu_items.is_empty() {
return self;
}
if let Some(PopupMenuItem::Separator) = self.menu_items.last() {
return self;
}
self.menu_items.push(PopupMenuItem::Separator);
self
}
@ -411,6 +423,7 @@ impl Render for PopupMenu {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::IntoElement {
let view = cx.view().clone();
let has_icon = self.menu_items.iter().any(|item| item.has_icon());
let items_count = self.menu_items.len();
let max_width = self.max_width;
let bounds = self.bounds;
@ -438,8 +451,15 @@ impl Render for PopupMenu {
.absolute()
.size_full()
})
.children(self.menu_items.iter_mut().enumerate().map(|(ix, item)| {
.children(
self.menu_items
.iter_mut()
.enumerate()
// Skip last separator
.filter(|(ix, item)| !(*ix == items_count - 1 && item.is_separator()))
.map(|(ix, item)| {
let group_id = format!("item:{}", ix);
let this = ListItem::new(("menu-item", ix))
.group(group_id.clone())
.relative()
@ -453,6 +473,7 @@ impl Render for PopupMenu {
this.hovered_menu_ix = Some(ix);
cx.notify();
}));
match item {
PopupMenuItem::Separator => this.h_auto().p_0().disabled(true).child(
div()
@ -498,7 +519,11 @@ impl Render for PopupMenu {
.size_full()
.items_center()
.gap_x_1p5()
.children(Self::render_icon(has_icon, icon.clone(), cx))
.children(Self::render_icon(
has_icon,
icon.clone(),
cx,
))
.child(
h_flex()
.flex_1()
@ -510,8 +535,10 @@ impl Render for PopupMenu {
),
)
.when_some(self.hovered_menu_ix, |this, hovered_ix| {
let (anchor, left) =
if cx.bounds().size.width - bounds.origin.x < max_width {
let (anchor, left) = if cx.bounds().size.width
- bounds.origin.x
< max_width
{
(AnchorCorner::TopRight, -px(15.))
} else {
(AnchorCorner::TopLeft, bounds.size.width - px(10.))
@ -526,15 +553,22 @@ impl Render for PopupMenu {
};
if hovered_ix == ix {
this.child(anchored().anchor(anchor).child(
div().occlude().top(top).left(left).child(menu.clone()),
))
this.child(
anchored().anchor(anchor).child(
div()
.occlude()
.top(top)
.left(left)
.child(menu.clone()),
),
)
} else {
this
}
}),
),
}
}))
}),
)
}
}