Fix context menu in tab bar

This commit is contained in:
Antonio Scandurra 2023-12-22 10:23:27 +01:00
parent 3d5b903f78
commit 3de72f8366
10 changed files with 100 additions and 30 deletions

View file

@ -76,13 +76,14 @@ impl ContextMenu {
pub fn entry(
mut self,
label: impl Into<SharedString>,
action: Option<Box<dyn Action>>,
handler: impl Fn(&mut WindowContext) + 'static,
) -> Self {
self.items.push(ContextMenuItem::Entry {
label: label.into(),
handler: Rc::new(handler),
icon: None,
action: None,
action,
});
self
}
@ -282,7 +283,10 @@ impl Render for ContextMenu {
ListItem::new(ix)
.inset(true)
.selected(Some(ix) == self.selected_index)
.on_click(move |_, cx| handler(cx))
.on_click(cx.listener(move |_, _, cx| {
handler(cx);
cx.emit(DismissEvent);
}))
.child(
h_stack()
.w_full()
@ -303,7 +307,10 @@ impl Render for ContextMenu {
ListItem::new(ix)
.inset(true)
.selected(Some(ix) == self.selected_index)
.on_click(move |_, cx| handler(cx))
.on_click(cx.listener(move |_, _, cx| {
handler(cx);
cx.emit(DismissEvent);
}))
.child(entry_render(cx))
.into_any_element()
}

View file

@ -1,4 +1,4 @@
use gpui::{actions, Action, AnchorCorner, Div, Render, View};
use gpui::{actions, AnchorCorner, Div, Render, View};
use story::Story;
use crate::prelude::*;
@ -10,12 +10,9 @@ fn build_menu(cx: &mut WindowContext, header: impl Into<SharedString>) -> View<C
ContextMenu::build(cx, |menu, _| {
menu.header(header)
.separator()
.entry("Print current time", |cx| {
println!("dispatching PrintCurrentTime action");
cx.dispatch_action(PrintCurrentDate.boxed_clone())
})
.entry("Print best foot", |cx| {
cx.dispatch_action(PrintBestFood.boxed_clone())
.action("Print current time", Box::new(PrintCurrentDate))
.entry("Print best food", Some(Box::new(PrintBestFood)), |cx| {
cx.dispatch_action(Box::new(PrintBestFood))
})
})
}