1use crate::{ItemHandle, Pane};
2use gpui::{
3 elements::*, platform::CursorStyle, Action, AnyViewHandle, AppContext, ElementBox, Entity,
4 MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle,
5};
6use settings::Settings;
7
8pub trait ToolbarItemView: View {
9 fn set_active_pane_item(
10 &mut self,
11 active_pane_item: Option<&dyn crate::ItemHandle>,
12 cx: &mut ViewContext<Self>,
13 ) -> ToolbarItemLocation;
14
15 fn location_for_event(
16 &self,
17 _event: &Self::Event,
18 current_location: ToolbarItemLocation,
19 _cx: &AppContext,
20 ) -> ToolbarItemLocation {
21 current_location
22 }
23}
24
25trait ToolbarItemViewHandle {
26 fn id(&self) -> usize;
27 fn to_any(&self) -> AnyViewHandle;
28 fn set_active_pane_item(
29 &self,
30 active_pane_item: Option<&dyn ItemHandle>,
31 cx: &mut MutableAppContext,
32 ) -> ToolbarItemLocation;
33}
34
35#[derive(Copy, Clone, Debug, PartialEq)]
36pub enum ToolbarItemLocation {
37 Hidden,
38 PrimaryLeft { flex: Option<(f32, bool)> },
39 PrimaryRight { flex: Option<(f32, bool)> },
40 Secondary,
41}
42
43pub struct Toolbar {
44 active_pane_item: Option<Box<dyn ItemHandle>>,
45 pane: WeakViewHandle<Pane>,
46 items: Vec<(Box<dyn ToolbarItemViewHandle>, ToolbarItemLocation)>,
47}
48
49impl Entity for Toolbar {
50 type Event = ();
51}
52
53impl View for Toolbar {
54 fn ui_name() -> &'static str {
55 "Toolbar"
56 }
57
58 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
59 let theme = &cx.global::<Settings>().theme.workspace.toolbar;
60
61 let mut primary_left_items = Vec::new();
62 let mut primary_right_items = Vec::new();
63 let mut secondary_item = None;
64 let spacing = theme.item_spacing;
65
66 for (item, position) in &self.items {
67 match *position {
68 ToolbarItemLocation::Hidden => {}
69 ToolbarItemLocation::PrimaryLeft { flex } => {
70 let left_item = ChildView::new(item.as_ref())
71 .aligned()
72 .contained()
73 .with_margin_right(spacing);
74 if let Some((flex, expanded)) = flex {
75 primary_left_items.push(left_item.flex(flex, expanded).boxed());
76 } else {
77 primary_left_items.push(left_item.boxed());
78 }
79 }
80 ToolbarItemLocation::PrimaryRight { flex } => {
81 let right_item = ChildView::new(item.as_ref())
82 .aligned()
83 .contained()
84 .with_margin_left(spacing)
85 .flex_float();
86 if let Some((flex, expanded)) = flex {
87 primary_right_items.push(right_item.flex(flex, expanded).boxed());
88 } else {
89 primary_right_items.push(right_item.boxed());
90 }
91 }
92 ToolbarItemLocation::Secondary => {
93 secondary_item = Some(
94 ChildView::new(item.as_ref())
95 .constrained()
96 .with_height(theme.height)
97 .boxed(),
98 );
99 }
100 }
101 }
102
103 let pane = self.pane.clone();
104 let container_style = theme.container;
105 let height = theme.height;
106 let button_style = theme.nav_button;
107
108 Flex::column()
109 .with_child(
110 Flex::row()
111 .with_child(nav_button(
112 "icons/arrow-left.svg",
113 button_style,
114 spacing,
115 super::GoBack {
116 pane: Some(pane.clone()),
117 },
118 cx,
119 ))
120 .with_child(nav_button(
121 "icons/arrow-right.svg",
122 button_style,
123 spacing,
124 super::GoForward {
125 pane: Some(pane.clone()),
126 },
127 cx,
128 ))
129 .with_children(primary_left_items)
130 .with_children(primary_right_items)
131 .constrained()
132 .with_height(height)
133 .boxed(),
134 )
135 .with_children(secondary_item)
136 .contained()
137 .with_style(container_style)
138 .boxed()
139 }
140}
141
142fn nav_button<A: Action + Clone>(
143 svg_path: &'static str,
144 style: theme::Interactive<theme::IconButton>,
145 spacing: f32,
146 action: A,
147 cx: &mut RenderContext<Toolbar>,
148) -> ElementBox {
149 MouseEventHandler::new::<A, _, _>(0, cx, |state, _| {
150 let style = style.style_for(state, false);
151 Svg::new(svg_path)
152 .with_color(style.color)
153 .constrained()
154 .with_width(style.icon_width)
155 .aligned()
156 .contained()
157 .with_style(style.container)
158 .constrained()
159 .with_width(style.button_width)
160 .with_height(style.button_width)
161 .boxed()
162 })
163 .with_cursor_style(CursorStyle::PointingHand)
164 .on_mouse_down(move |_, cx| cx.dispatch_action(action.clone()))
165 .contained()
166 .with_margin_right(spacing)
167 .boxed()
168}
169
170impl Toolbar {
171 pub fn new(pane: WeakViewHandle<Pane>) -> Self {
172 Self {
173 active_pane_item: None,
174 pane,
175 items: Default::default(),
176 }
177 }
178
179 pub fn add_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
180 where
181 T: 'static + ToolbarItemView,
182 {
183 let location = item.set_active_pane_item(self.active_pane_item.as_deref(), cx);
184 cx.subscribe(&item, |this, item, event, cx| {
185 if let Some((_, current_location)) =
186 this.items.iter_mut().find(|(i, _)| i.id() == item.id())
187 {
188 let new_location = item
189 .read(cx)
190 .location_for_event(event, *current_location, cx);
191 if new_location != *current_location {
192 *current_location = new_location;
193 cx.notify();
194 }
195 }
196 })
197 .detach();
198 self.items.push((Box::new(item), location));
199 cx.notify();
200 }
201
202 pub fn set_active_pane_item(
203 &mut self,
204 pane_item: Option<&dyn ItemHandle>,
205 cx: &mut ViewContext<Self>,
206 ) {
207 self.active_pane_item = pane_item.map(|item| item.boxed_clone());
208 for (toolbar_item, current_location) in self.items.iter_mut() {
209 let new_location = toolbar_item.set_active_pane_item(pane_item, cx);
210 if new_location != *current_location {
211 *current_location = new_location;
212 cx.notify();
213 }
214 }
215 }
216
217 pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<ViewHandle<T>> {
218 self.items
219 .iter()
220 .find_map(|(item, _)| item.to_any().downcast())
221 }
222}
223
224impl<T: ToolbarItemView> ToolbarItemViewHandle for ViewHandle<T> {
225 fn id(&self) -> usize {
226 self.id()
227 }
228
229 fn to_any(&self) -> AnyViewHandle {
230 self.into()
231 }
232
233 fn set_active_pane_item(
234 &self,
235 active_pane_item: Option<&dyn ItemHandle>,
236 cx: &mut MutableAppContext,
237 ) -> ToolbarItemLocation {
238 self.update(cx, |this, cx| {
239 this.set_active_pane_item(active_pane_item, cx)
240 })
241 }
242}
243
244impl Into<AnyViewHandle> for &dyn ToolbarItemViewHandle {
245 fn into(self) -> AnyViewHandle {
246 self.to_any()
247 }
248}