toolbar.rs

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