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 mut enable_go_backward = false;
105 let mut enable_go_forward = false;
106 if let Some(pane) = pane.upgrade(cx) {
107 let pane = pane.read(cx);
108 enable_go_backward = pane.can_navigate_backward();
109 enable_go_forward = pane.can_navigate_forward();
110 }
111
112 let container_style = theme.container;
113 let height = theme.height;
114 let button_style = theme.nav_button;
115
116 Flex::column()
117 .with_child(
118 Flex::row()
119 .with_child(nav_button(
120 "icons/arrow-left.svg",
121 button_style,
122 enable_go_backward,
123 spacing,
124 super::GoBack {
125 pane: Some(pane.clone()),
126 },
127 cx,
128 ))
129 .with_child(nav_button(
130 "icons/arrow-right.svg",
131 button_style,
132 enable_go_forward,
133 spacing,
134 super::GoForward {
135 pane: Some(pane.clone()),
136 },
137 cx,
138 ))
139 .with_children(primary_left_items)
140 .with_children(primary_right_items)
141 .constrained()
142 .with_height(height)
143 .boxed(),
144 )
145 .with_children(secondary_item)
146 .contained()
147 .with_style(container_style)
148 .boxed()
149 }
150}
151
152fn nav_button<A: Action + Clone>(
153 svg_path: &'static str,
154 style: theme::Interactive<theme::IconButton>,
155 enabled: bool,
156 spacing: f32,
157 action: A,
158 cx: &mut RenderContext<Toolbar>,
159) -> ElementBox {
160 MouseEventHandler::new::<A, _, _>(0, cx, |state, _| {
161 let style = if enabled {
162 style.style_for(state, false)
163 } else {
164 style.disabled_style()
165 };
166 Svg::new(svg_path)
167 .with_color(style.color)
168 .constrained()
169 .with_width(style.icon_width)
170 .aligned()
171 .contained()
172 .with_style(style.container)
173 .constrained()
174 .with_width(style.button_width)
175 .with_height(style.button_width)
176 .boxed()
177 })
178 .with_cursor_style(if enabled {
179 CursorStyle::PointingHand
180 } else {
181 CursorStyle::default()
182 })
183 .on_mouse_down(move |_, cx| cx.dispatch_action(action.clone()))
184 .contained()
185 .with_margin_right(spacing)
186 .boxed()
187}
188
189impl Toolbar {
190 pub fn new(pane: WeakViewHandle<Pane>) -> Self {
191 Self {
192 active_pane_item: None,
193 pane,
194 items: Default::default(),
195 }
196 }
197
198 pub fn add_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
199 where
200 T: 'static + ToolbarItemView,
201 {
202 let location = item.set_active_pane_item(self.active_pane_item.as_deref(), cx);
203 cx.subscribe(&item, |this, item, event, cx| {
204 if let Some((_, current_location)) =
205 this.items.iter_mut().find(|(i, _)| i.id() == item.id())
206 {
207 let new_location = item
208 .read(cx)
209 .location_for_event(event, *current_location, cx);
210 if new_location != *current_location {
211 *current_location = new_location;
212 cx.notify();
213 }
214 }
215 })
216 .detach();
217 self.items.push((Box::new(item), location));
218 cx.notify();
219 }
220
221 pub fn set_active_pane_item(
222 &mut self,
223 pane_item: Option<&dyn ItemHandle>,
224 cx: &mut ViewContext<Self>,
225 ) {
226 self.active_pane_item = pane_item.map(|item| item.boxed_clone());
227 for (toolbar_item, current_location) in self.items.iter_mut() {
228 let new_location = toolbar_item.set_active_pane_item(pane_item, cx);
229 if new_location != *current_location {
230 *current_location = new_location;
231 cx.notify();
232 }
233 }
234 }
235
236 pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<ViewHandle<T>> {
237 self.items
238 .iter()
239 .find_map(|(item, _)| item.to_any().downcast())
240 }
241}
242
243impl<T: ToolbarItemView> ToolbarItemViewHandle for ViewHandle<T> {
244 fn id(&self) -> usize {
245 self.id()
246 }
247
248 fn to_any(&self) -> AnyViewHandle {
249 self.into()
250 }
251
252 fn set_active_pane_item(
253 &self,
254 active_pane_item: Option<&dyn ItemHandle>,
255 cx: &mut MutableAppContext,
256 ) -> ToolbarItemLocation {
257 self.update(cx, |this, cx| {
258 this.set_active_pane_item(active_pane_item, cx)
259 })
260 }
261}
262
263impl Into<AnyViewHandle> for &dyn ToolbarItemViewHandle {
264 fn into(self) -> AnyViewHandle {
265 self.to_any()
266 }
267}