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        div()
 59            .bg(cx.theme().colors().toolbar)
 60            .p_2()
 61            .flex()
 62            .justify_between()
 63            .child(div().flex().children(self.left_items))
 64            .child(div().flex().children(self.right_items))
 65    }
 66}
 67
 68#[cfg(feature = "stories")]
 69pub use stories::*;
 70
 71#[cfg(feature = "stories")]
 72mod stories {
 73    use std::path::PathBuf;
 74    use std::str::FromStr;
 75
 76    use gpui2::{Div, Render};
 77
 78    use crate::{Breadcrumb, HighlightedText, Icon, IconButton, Story, Symbol};
 79
 80    use super::*;
 81
 82    pub struct ToolbarStory;
 83
 84    impl Render for ToolbarStory {
 85        type Element = Div<Self>;
 86
 87        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
 88            Story::container(cx)
 89                .child(Story::title_for::<_, Toolbar<Self>>(cx))
 90                .child(Story::label(cx, "Default"))
 91                .child(
 92                    Toolbar::new()
 93                        .left_item(Breadcrumb::new(
 94                            PathBuf::from_str("crates/ui/src/components/toolbar.rs").unwrap(),
 95                            vec![
 96                                Symbol(vec![
 97                                    HighlightedText {
 98                                        text: "impl ".to_string(),
 99                                        color: cx.theme().syntax_color("keyword"),
100                                    },
101                                    HighlightedText {
102                                        text: "ToolbarStory".to_string(),
103                                        color: cx.theme().syntax_color("function"),
104                                    },
105                                ]),
106                                Symbol(vec![
107                                    HighlightedText {
108                                        text: "fn ".to_string(),
109                                        color: cx.theme().syntax_color("keyword"),
110                                    },
111                                    HighlightedText {
112                                        text: "render".to_string(),
113                                        color: cx.theme().syntax_color("function"),
114                                    },
115                                ]),
116                            ],
117                        ))
118                        .right_items(vec![
119                            IconButton::new("toggle_inlay_hints", Icon::InlayHint),
120                            IconButton::new("buffer_search", Icon::MagnifyingGlass),
121                            IconButton::new("inline_assist", Icon::MagicWand),
122                        ]),
123                )
124        }
125    }
126}