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 + Clone> {
 8    state_type: PhantomData<S>,
 9    scroll_state: ScrollState,
10}
11
12impl<S: 'static + Send + Sync + Clone> CommandPalette<S> {
13    pub fn new(scroll_state: ScrollState) -> Self {
14        Self {
15            state_type: PhantomData,
16            scroll_state,
17        }
18    }
19
20    fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
21        div().child(
22            Palette::new(self.scroll_state.clone())
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 + Clone> {
42        state_type: PhantomData<S>,
43    }
44
45    impl<S: 'static + Send + Sync + Clone> CommandPaletteStory<S> {
46        pub fn new() -> Self {
47            Self {
48                state_type: PhantomData,
49            }
50        }
51
52        fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
53            Story::container(cx)
54                .child(Story::title_for::<_, CommandPalette<S>>(cx))
55                .child(Story::label(cx, "Default"))
56                .child(CommandPalette::new(ScrollState::default()))
57        }
58    }
59}