Add CommandPalette component

This commit is contained in:
Marshall Bowers 2023-10-09 11:20:10 -04:00
parent e5473fc51a
commit f7721d0523
6 changed files with 121 additions and 3 deletions

View file

@ -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::*;

View file

@ -0,0 +1,29 @@
use std::marker::PhantomData;
use crate::prelude::*;
use crate::{example_editor_actions, OrderMethod, Palette};
#[derive(Element)]
pub struct CommandPalette<S: 'static + Send + Sync + Clone> {
state_type: PhantomData<S>,
scroll_state: ScrollState,
}
impl<S: 'static + Send + Sync + Clone> CommandPalette<S> {
pub fn new(scroll_state: ScrollState) -> Self {
Self {
state_type: PhantomData,
scroll_state,
}
}
fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
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),
)
}
}

View file

@ -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<S: 'static + Send + Sync + Clone>() -> Vec<Tab<S>> {
@ -541,6 +542,61 @@ pub fn static_collab_panel_channels<S: 'static + Send + Sync + Clone>() -> Vec<L
.collect()
}
pub fn example_editor_actions<S: 'static + Send + Sync + Clone>() -> Vec<PaletteItem<S>> {
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<S: 'static + Send + Sync + Clone>() -> Editor<S> {
Editor {
tabs: static_tabs_example(),