command_palette.rs

 1use std::marker::PhantomData;
 2
 3use crate::prelude::*;
 4use crate::{example_editor_actions, OrderMethod, Palette};
 5
 6#[derive(Element)]
 7pub struct CommandPalette<S: 'static + Send + Sync> {
 8    id: ElementId,
 9    state_type: PhantomData<S>,
10}
11
12impl<S: 'static + Send + Sync> CommandPalette<S> {
13    pub fn new(id: impl Into<ElementId>) -> Self {
14        Self {
15            id: id.into(),
16            state_type: PhantomData,
17        }
18    }
19
20    fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
21        div().id(self.id.clone()).child(
22            Palette::new("palette")
23                .items(example_editor_actions())
24                .placeholder("Execute a command...")
25                .empty_string("No items found.")
26                .default_order(OrderMethod::Ascending),
27        )
28    }
29}
30
31#[cfg(feature = "stories")]
32pub use stories::*;
33
34#[cfg(feature = "stories")]
35mod stories {
36    use crate::Story;
37
38    use super::*;
39
40    #[derive(Element)]
41    pub struct CommandPaletteStory<S: 'static + Send + Sync> {
42        state_type: PhantomData<S>,
43    }
44
45    impl<S: 'static + Send + Sync> CommandPaletteStory<S> {
46        pub fn new() -> Self {
47            Self {
48                state_type: PhantomData,
49            }
50        }
51
52        fn render(
53            &mut self,
54            _view: &mut S,
55            cx: &mut ViewContext<S>,
56        ) -> impl Element<ViewState = S> {
57            Story::container(cx)
58                .child(Story::title_for::<_, CommandPalette<S>>(cx))
59                .child(Story::label(cx, "Default"))
60                .child(CommandPalette::new("command-palette"))
61        }
62    }
63}