From f7721d05231c81a36398a558854fd841caae9fe8 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Mon, 9 Oct 2023 11:20:10 -0400 Subject: [PATCH] Add `CommandPalette` component --- crates/storybook2/src/stories/components.rs | 1 + .../src/stories/components/command_palette.rs | 26 ++++++++ crates/storybook2/src/story_selector.rs | 4 ++ crates/ui2/src/components.rs | 2 + crates/ui2/src/components/command_palette.rs | 29 +++++++++ crates/ui2/src/static_data.rs | 62 ++++++++++++++++++- 6 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 crates/storybook2/src/stories/components/command_palette.rs create mode 100644 crates/ui2/src/components/command_palette.rs diff --git a/crates/storybook2/src/stories/components.rs b/crates/storybook2/src/stories/components.rs index 9b62c55921..d6750c3b84 100644 --- a/crates/storybook2/src/stories/components.rs +++ b/crates/storybook2/src/stories/components.rs @@ -3,6 +3,7 @@ pub mod breadcrumb; pub mod buffer; pub mod chat_panel; pub mod collab_panel; +pub mod command_palette; pub mod facepile; pub mod keybinding; pub mod palette; diff --git a/crates/storybook2/src/stories/components/command_palette.rs b/crates/storybook2/src/stories/components/command_palette.rs new file mode 100644 index 0000000000..d3575cdb50 --- /dev/null +++ b/crates/storybook2/src/stories/components/command_palette.rs @@ -0,0 +1,26 @@ +use std::marker::PhantomData; + +use ui::prelude::*; +use ui::CommandPalette; + +use crate::story::Story; + +#[derive(Element)] +pub struct CommandPaletteStory { + state_type: PhantomData, +} + +impl CommandPaletteStory { + pub fn new() -> Self { + Self { + state_type: PhantomData, + } + } + + fn render(&mut self, cx: &mut ViewContext) -> impl Element { + Story::container(cx) + .child(Story::title_for::<_, CommandPalette>(cx)) + .child(Story::label(cx, "Default")) + .child(CommandPalette::new(ScrollState::default())) + } +} diff --git a/crates/storybook2/src/story_selector.rs b/crates/storybook2/src/story_selector.rs index cb62fceeca..56387eee1d 100644 --- a/crates/storybook2/src/story_selector.rs +++ b/crates/storybook2/src/story_selector.rs @@ -41,6 +41,7 @@ pub enum ComponentStory { Buffer, ChatPanel, CollabPanel, + CommandPalette, Facepile, Keybinding, Palette, @@ -67,6 +68,9 @@ impl ComponentStory { Self::Breadcrumb => components::breadcrumb::BreadcrumbStory::new().into_any(), Self::ChatPanel => components::chat_panel::ChatPanelStory::new().into_any(), Self::CollabPanel => components::collab_panel::CollabPanelStory::new().into_any(), + Self::CommandPalette => { + components::command_palette::CommandPaletteStory::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.rs index 96abcb3838..968b7bee38 100644 --- a/crates/ui2/src/components.rs +++ b/crates/ui2/src/components.rs @@ -3,6 +3,7 @@ mod breadcrumb; mod buffer; mod chat_panel; mod collab_panel; +mod command_palette; mod editor_pane; mod facepile; mod icon_button; @@ -27,6 +28,7 @@ pub use breadcrumb::*; pub use buffer::*; pub use chat_panel::*; pub use collab_panel::*; +pub use command_palette::*; pub use editor_pane::*; pub use facepile::*; pub use icon_button::*; diff --git a/crates/ui2/src/components/command_palette.rs b/crates/ui2/src/components/command_palette.rs new file mode 100644 index 0000000000..61e20d8535 --- /dev/null +++ b/crates/ui2/src/components/command_palette.rs @@ -0,0 +1,29 @@ +use std::marker::PhantomData; + +use crate::prelude::*; +use crate::{example_editor_actions, OrderMethod, Palette}; + +#[derive(Element)] +pub struct CommandPalette { + state_type: PhantomData, + scroll_state: ScrollState, +} + +impl CommandPalette { + pub fn new(scroll_state: ScrollState) -> Self { + Self { + state_type: PhantomData, + scroll_state, + } + } + + fn render(&mut self, cx: &mut ViewContext) -> impl Element { + div().child( + Palette::new(self.scroll_state.clone()) + .items(example_editor_actions()) + .placeholder("Execute a command...") + .empty_string("No items found.") + .default_order(OrderMethod::Ascending), + ) + } +} diff --git a/crates/ui2/src/static_data.rs b/crates/ui2/src/static_data.rs index 8fddd9c517..4245f8344e 100644 --- a/crates/ui2/src/static_data.rs +++ b/crates/ui2/src/static_data.rs @@ -5,9 +5,10 @@ use rand::Rng; use crate::{ Buffer, BufferRow, BufferRows, Editor, FileSystemStatus, GitStatus, HighlightColor, - HighlightedLine, HighlightedText, Icon, Label, LabelColor, ListEntry, ListEntrySize, ListItem, - Livestream, MicStatus, Player, PlayerCallStatus, PlayerWithCallStatus, ScreenShareStatus, - Symbol, Tab, Theme, ToggleState, VideoStatus, + HighlightedLine, HighlightedText, Icon, Keybinding, Label, LabelColor, ListEntry, + ListEntrySize, ListItem, Livestream, MicStatus, ModifierKeys, PaletteItem, Player, + PlayerCallStatus, PlayerWithCallStatus, ScreenShareStatus, Symbol, Tab, Theme, ToggleState, + VideoStatus, }; pub fn static_tabs_example() -> Vec> { @@ -541,6 +542,61 @@ pub fn static_collab_panel_channels() -> Vec() -> Vec> { + vec![ + PaletteItem::new("New File").keybinding(Keybinding::new( + "N".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Open File").keybinding(Keybinding::new( + "O".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Save File").keybinding(Keybinding::new( + "S".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Cut").keybinding(Keybinding::new( + "X".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Copy").keybinding(Keybinding::new( + "C".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Paste").keybinding(Keybinding::new( + "V".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Undo").keybinding(Keybinding::new( + "Z".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Redo").keybinding(Keybinding::new( + "Z".to_string(), + ModifierKeys::new().control(true).shift(true), + )), + PaletteItem::new("Find").keybinding(Keybinding::new( + "F".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Replace").keybinding(Keybinding::new( + "R".to_string(), + ModifierKeys::new().control(true), + )), + PaletteItem::new("Jump to Line"), + PaletteItem::new("Select All"), + PaletteItem::new("Deselect All"), + PaletteItem::new("Switch Document"), + PaletteItem::new("Insert Line Below"), + PaletteItem::new("Insert Line Above"), + PaletteItem::new("Move Line Up"), + PaletteItem::new("Move Line Down"), + PaletteItem::new("Toggle Comment"), + PaletteItem::new("Delete Line"), + ] +} + pub fn empty_editor_example() -> Editor { Editor { tabs: static_tabs_example(),