Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add `ContextMenu` component
Marshall Bowers333e3e4f01b4
diff --git a/crates/storybook2/src/stories/components.rs b/crates/storybook2/src/stories/components.rsindex d6750c3b84c..b7f008ac08f 100644--- a/crates/storybook2/src/stories/components.rs+++ b/crates/storybook2/src/stories/components.rs@@ -4,6 +4,7 @@ pub mod buffer;pub mod chat_panel;pub mod collab_panel;pub mod command_palette;+pub mod context_menu;pub mod facepile;pub mod keybinding;pub mod palette;diff --git a/crates/storybook2/src/stories/components/context_menu.rs b/crates/storybook2/src/stories/components/context_menu.rsnew file mode 100644index 00000000000..f93f97f1332--- /dev/null+++ b/crates/storybook2/src/stories/components/context_menu.rs@@ -0,0 +1,30 @@+use std::marker::PhantomData;++use ui::prelude::*;+use ui::{ContextMenu, ContextMenuItem, Label};++use crate::story::Story;++#[derive(Element)]+pub struct ContextMenuStory<S: 'static + Send + Sync + Clone> {+ state_type: PhantomData<S>,+}++impl<S: 'static + Send + Sync + Clone> ContextMenuStory<S> {+ pub fn new() -> Self {+ Self {+ state_type: PhantomData,+ }+ }++ fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {+ Story::container(cx)+ .child(Story::title_for::<_, ContextMenu<S>>(cx))+ .child(Story::label(cx, "Default"))+ .child(ContextMenu::new([+ ContextMenuItem::header("Section header"),+ ContextMenuItem::Separator,+ ContextMenuItem::entry(Label::new("Some entry")),+ ]))+ }+}diff --git a/crates/storybook2/src/story_selector.rs b/crates/storybook2/src/story_selector.rsindex 56387eee1d6..feacea235d8 100644--- a/crates/storybook2/src/story_selector.rs+++ b/crates/storybook2/src/story_selector.rs@@ -42,6 +42,7 @@ pub enum ComponentStory {ChatPanel,CollabPanel,CommandPalette,+ ContextMenu,Facepile,Keybinding,Palette,@@ -71,6 +72,7 @@ impl ComponentStory {Self::CommandPalette => {components::command_palette::CommandPaletteStory::new().into_any()}+ Self::ContextMenu => components::context_menu::ContextMenuStory::new().into_any(),Self::Facepile => components::facepile::FacepileStory::new().into_any(),Self::Keybinding => components::keybinding::KeybindingStory::new().into_any(),Self::Palette => components::palette::PaletteStory::new().into_any(),diff --git a/crates/ui2/src/components.rs b/crates/ui2/src/components.rsindex 968b7bee380..9e28d30662b 100644--- a/crates/ui2/src/components.rs+++ b/crates/ui2/src/components.rs@@ -4,6 +4,7 @@ mod buffer;mod chat_panel;mod collab_panel;mod command_palette;+mod context_menu;mod editor_pane;mod facepile;mod icon_button;@@ -29,6 +30,7 @@ pub use buffer::*;pub use chat_panel::*;pub use collab_panel::*;pub use command_palette::*;+pub use context_menu::*;pub use editor_pane::*;pub use facepile::*;pub use icon_button::*;diff --git a/crates/ui2/src/components/context_menu.rs b/crates/ui2/src/components/context_menu.rsnew file mode 100644index 00000000000..9fa7e7c6350--- /dev/null+++ b/crates/ui2/src/components/context_menu.rs@@ -0,0 +1,67 @@+use crate::prelude::*;+use crate::{+ theme, v_stack, Label, List, ListEntry, ListItem, ListItemVariant, ListSeparator, ListSubHeader,+};++#[derive(Clone)]+pub enum ContextMenuItem<S: 'static + Send + Sync + Clone> {+ Header(&'static str),+ Entry(Label<S>),+ Separator,+}++impl<S: 'static + Send + Sync + Clone> ContextMenuItem<S> {+ fn to_list_item(self) -> ListItem<S> {+ match self {+ ContextMenuItem::Header(label) => ListSubHeader::new(label).into(),+ ContextMenuItem::Entry(label) => {+ ListEntry::new(label).variant(ListItemVariant::Inset).into()+ }+ ContextMenuItem::Separator => ListSeparator::new().into(),+ }+ }++ pub fn header(label: &'static str) -> Self {+ Self::Header(label)+ }++ pub fn separator() -> Self {+ Self::Separator+ }++ pub fn entry(label: Label<S>) -> Self {+ Self::Entry(label)+ }+}++#[derive(Element)]+pub struct ContextMenu<S: 'static + Send + Sync + Clone> {+ items: Vec<ContextMenuItem<S>>,+}++impl<S: 'static + Send + Sync + Clone> ContextMenu<S> {+ pub fn new(items: impl IntoIterator<Item = ContextMenuItem<S>>) -> Self {+ Self {+ items: items.into_iter().collect(),+ }+ }+ fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {+ let theme = theme(cx);++ v_stack()+ .flex()+ .fill(theme.lowest.base.default.background)+ .border()+ .border_color(theme.lowest.base.default.border)+ .child(+ List::new(+ self.items+ .clone()+ .into_iter()+ .map(ContextMenuItem::to_list_item)+ .collect(),+ )+ .set_toggle(ToggleState::Toggled),+ )+ }+}