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 gpui2::{Div, Render};
79
80 use crate::{Breadcrumb, HighlightedText, Icon, IconButton, Story, Symbol};
81
82 use super::*;
83
84 pub struct ToolbarStory;
85
86 impl Render for ToolbarStory {
87 type Element = Div<Self>;
88
89 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
90 let theme = theme(cx);
91
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 ".to_string(),
103 color: theme.syntax.color("keyword"),
104 },
105 HighlightedText {
106 text: "ToolbarStory".to_string(),
107 color: theme.syntax.color("function"),
108 },
109 ]),
110 Symbol(vec![
111 HighlightedText {
112 text: "fn ".to_string(),
113 color: theme.syntax.color("keyword"),
114 },
115 HighlightedText {
116 text: "render".to_string(),
117 color: 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}