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        fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
 87            let theme = theme(cx);
 88
 89            Story::container(cx)
 90                .child(Story::title_for::<_, Toolbar<V>>(cx))
 91                .child(Story::label(cx, "Default"))
 92                .child(
 93                    Toolbar::new()
 94                        .left_item(Breadcrumb::new(
 95                            PathBuf::from_str("crates/ui/src/components/toolbar.rs").unwrap(),
 96                            vec![
 97                                Symbol(vec![
 98                                    HighlightedText {
 99                                        text: "impl ".to_string(),
100                                        color: theme.syntax.color("keyword"),
101                                    },
102                                    HighlightedText {
103                                        text: "ToolbarStory".to_string(),
104                                        color: theme.syntax.color("function"),
105                                    },
106                                ]),
107                                Symbol(vec![
108                                    HighlightedText {
109                                        text: "fn ".to_string(),
110                                        color: theme.syntax.color("keyword"),
111                                    },
112                                    HighlightedText {
113                                        text: "render".to_string(),
114                                        color: theme.syntax.color("function"),
115                                    },
116                                ]),
117                            ],
118                        ))
119                        .right_items(vec![
120                            IconButton::new("toggle_inlay_hints", Icon::InlayHint),
121                            IconButton::new("buffer_search", Icon::MagnifyingGlass),
122                            IconButton::new("inline_assist", Icon::MagicWand),
123                        ]),
124                )
125        }
126    }
127}