toolbar.rs

  1use gpui2::AnyElement;
  2use smallvec::SmallVec;
  3
  4use crate::prelude::*;
  5
  6#[derive(Clone)]
  7pub struct ToolbarItem {}
  8
  9#[derive(Component)]
 10pub struct Toolbar<V: 'static> {
 11    left_items: SmallVec<[AnyElement<V>; 2]>,
 12    right_items: SmallVec<[AnyElement<V>; 2]>,
 13}
 14
 15impl<V: 'static> Toolbar<V> {
 16    pub fn new() -> Self {
 17        Self {
 18            left_items: SmallVec::new(),
 19            right_items: SmallVec::new(),
 20        }
 21    }
 22
 23    pub fn left_item(mut self, child: impl Component<V>) -> Self
 24    where
 25        Self: Sized,
 26    {
 27        self.left_items.push(child.render());
 28        self
 29    }
 30
 31    pub fn left_items(mut self, iter: impl IntoIterator<Item = impl Component<V>>) -> Self
 32    where
 33        Self: Sized,
 34    {
 35        self.left_items
 36            .extend(iter.into_iter().map(|item| item.render()));
 37        self
 38    }
 39
 40    pub fn right_item(mut self, child: impl Component<V>) -> Self
 41    where
 42        Self: Sized,
 43    {
 44        self.right_items.push(child.render());
 45        self
 46    }
 47
 48    pub fn right_items(mut self, iter: impl IntoIterator<Item = impl Component<V>>) -> Self
 49    where
 50        Self: Sized,
 51    {
 52        self.right_items
 53            .extend(iter.into_iter().map(|item| item.render()));
 54        self
 55    }
 56
 57    fn render(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
 58        let theme = theme(cx);
 59
 60        div()
 61            .bg(theme.toolbar)
 62            .p_2()
 63            .flex()
 64            .justify_between()
 65            .child(div().flex().children(self.left_items))
 66            .child(div().flex().children(self.right_items))
 67    }
 68}
 69
 70#[cfg(feature = "stories")]
 71pub use stories::*;
 72
 73#[cfg(feature = "stories")]
 74mod stories {
 75    use std::path::PathBuf;
 76    use std::str::FromStr;
 77
 78    use crate::{Breadcrumb, HighlightedText, Icon, IconButton, Story, Symbol};
 79
 80    use super::*;
 81
 82    #[derive(Component)]
 83    pub struct ToolbarStory;
 84
 85    impl ToolbarStory {
 86        pub fn new() -> Self {
 87            Self
 88        }
 89
 90        fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
 91            let theme = theme(cx);
 92
 93            Story::container(cx)
 94                .child(Story::title_for::<_, Toolbar<V>>(cx))
 95                .child(Story::label(cx, "Default"))
 96                .child(
 97                    Toolbar::new()
 98                        .left_item(Breadcrumb::new(
 99                            PathBuf::from_str("crates/ui/src/components/toolbar.rs").unwrap(),
100                            vec![
101                                Symbol(vec![
102                                    HighlightedText {
103                                        text: "impl ".to_string(),
104                                        color: theme.syntax.keyword,
105                                    },
106                                    HighlightedText {
107                                        text: "ToolbarStory".to_string(),
108                                        color: theme.syntax.function,
109                                    },
110                                ]),
111                                Symbol(vec![
112                                    HighlightedText {
113                                        text: "fn ".to_string(),
114                                        color: theme.syntax.keyword,
115                                    },
116                                    HighlightedText {
117                                        text: "render".to_string(),
118                                        color: theme.syntax.function,
119                                    },
120                                ]),
121                            ],
122                        ))
123                        .right_items(vec![
124                            IconButton::new("toggle_inlay_hints", Icon::InlayHint),
125                            IconButton::new("buffer_search", Icon::MagnifyingGlass),
126                            IconButton::new("inline_assist", Icon::MagicWand),
127                        ]),
128                )
129        }
130    }
131}