Simplify ContextMenu by not storing list components

This commit is contained in:
Marshall Bowers 2023-11-22 12:41:29 -05:00
parent 2c8d243d22
commit 031fca4105
5 changed files with 29 additions and 58 deletions

View file

@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::rc::Rc;
use crate::{prelude::*, v_stack, List};
use crate::{prelude::*, v_stack, Label, List};
use crate::{ListItem, ListSeparator, ListSubHeader};
use gpui::{
overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, ClickEvent, DispatchPhase,
@ -10,9 +10,9 @@ use gpui::{
};
pub enum ContextMenuItem {
Separator(ListSeparator),
Header(ListSubHeader),
Entry(ListItem, Rc<dyn Fn(&ClickEvent, &mut WindowContext)>),
Separator,
Header(SharedString),
Entry(SharedString, Rc<dyn Fn(&ClickEvent, &mut WindowContext)>),
}
pub struct ContextMenu {
@ -46,29 +46,30 @@ impl ContextMenu {
}
pub fn header(mut self, title: impl Into<SharedString>) -> Self {
self.items
.push(ContextMenuItem::Header(ListSubHeader::new(title)));
self.items.push(ContextMenuItem::Header(title.into()));
self
}
pub fn separator(mut self) -> Self {
self.items.push(ContextMenuItem::Separator(ListSeparator));
self.items.push(ContextMenuItem::Separator);
self
}
pub fn entry(
mut self,
view: ListItem,
label: impl Into<SharedString>,
on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> Self {
self.items
.push(ContextMenuItem::Entry(view, Rc::new(on_click)));
.push(ContextMenuItem::Entry(label.into(), Rc::new(on_click)));
self
}
pub fn action(self, view: ListItem, action: Box<dyn Action>) -> Self {
pub fn action(self, label: impl Into<SharedString>, action: Box<dyn Action>) -> Self {
// todo: add the keybindings to the list entry
self.entry(view, move |_, cx| cx.dispatch_action(action.boxed_clone()))
self.entry(label.into(), move |_, cx| {
cx.dispatch_action(action.boxed_clone())
})
}
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
@ -104,16 +105,15 @@ impl Render for ContextMenu {
// .border_color(cx.theme().colors().border)
.child(
List::new().children(self.items.iter().map(|item| match item {
ContextMenuItem::Separator(separator) => {
separator.clone().render_into_any()
ContextMenuItem::Separator => ListSeparator::new().render_into_any(),
ContextMenuItem::Header(header) => {
ListSubHeader::new(header.clone()).render_into_any()
}
ContextMenuItem::Header(header) => header.clone().render_into_any(),
ContextMenuItem::Entry(entry, callback) => {
let callback = callback.clone();
let dismiss = cx.listener(|_, _, cx| cx.emit(Manager::Dismiss));
entry
.clone()
ListItem::new(entry.clone(), Label::new(entry.clone()))
.on_click(move |event, cx| {
callback(event, cx);
dismiss(event, cx)