1use crate::ItemHandle;
2use gpui::{
3 AnyView, Div, Entity, EntityId, EventEmitter, ParentElement as _, Render, Styled, View,
4 ViewContext, WindowContext,
5};
6use ui::prelude::*;
7use ui::{h_stack, v_stack};
8
9pub enum ToolbarItemEvent {
10 ChangeLocation(ToolbarItemLocation),
11}
12
13pub trait ToolbarItemView: Render + EventEmitter<ToolbarItemEvent> {
14 fn set_active_pane_item(
15 &mut self,
16 active_pane_item: Option<&dyn crate::ItemHandle>,
17 cx: &mut ViewContext<Self>,
18 ) -> ToolbarItemLocation;
19
20 fn pane_focus_update(&mut self, _pane_focused: bool, _cx: &mut ViewContext<Self>) {}
21
22 /// Number of times toolbar's height will be repeated to get the effective height.
23 /// Useful when multiple rows one under each other are needed.
24 /// The rows have the same width and act as a whole when reacting to resizes and similar events.
25 fn row_count(&self, _cx: &WindowContext) -> usize {
26 1
27 }
28}
29
30trait ToolbarItemViewHandle: Send {
31 fn id(&self) -> EntityId;
32 fn to_any(&self) -> AnyView;
33 fn set_active_pane_item(
34 &self,
35 active_pane_item: Option<&dyn ItemHandle>,
36 cx: &mut WindowContext,
37 ) -> ToolbarItemLocation;
38 fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext);
39 fn row_count(&self, cx: &WindowContext) -> usize;
40}
41
42#[derive(Copy, Clone, Debug, PartialEq)]
43pub enum ToolbarItemLocation {
44 Hidden,
45 PrimaryLeft,
46 PrimaryRight,
47 Secondary,
48}
49
50pub struct Toolbar {
51 active_item: Option<Box<dyn ItemHandle>>,
52 hidden: bool,
53 can_navigate: bool,
54 items: Vec<(Box<dyn ToolbarItemViewHandle>, ToolbarItemLocation)>,
55}
56
57impl Toolbar {
58 fn has_any_visible_items(&self) -> bool {
59 self.items
60 .iter()
61 .any(|(_item, location)| *location != ToolbarItemLocation::Hidden)
62 }
63
64 fn left_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
65 self.items.iter().filter_map(|(item, location)| {
66 if *location == ToolbarItemLocation::PrimaryLeft {
67 Some(item.as_ref())
68 } else {
69 None
70 }
71 })
72 }
73
74 fn right_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
75 self.items.iter().filter_map(|(item, location)| {
76 if *location == ToolbarItemLocation::PrimaryRight {
77 Some(item.as_ref())
78 } else {
79 None
80 }
81 })
82 }
83
84 fn secondary_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
85 self.items.iter().filter_map(|(item, location)| {
86 if *location == ToolbarItemLocation::Secondary {
87 Some(item.as_ref())
88 } else {
89 None
90 }
91 })
92 }
93}
94
95impl Render for Toolbar {
96 type Element = Div;
97
98 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
99 if !self.has_any_visible_items() {
100 return div();
101 }
102
103 let secondary_item = self.secondary_items().next().map(|item| item.to_any());
104
105 let has_left_items = self.left_items().count() > 0;
106 let has_right_items = self.right_items().count() > 0;
107
108 v_stack()
109 .p_2()
110 .when(has_left_items || has_right_items, |this| this.gap_2())
111 .border_b()
112 .border_color(cx.theme().colors().border_variant)
113 .bg(cx.theme().colors().toolbar_background)
114 .child(
115 h_stack()
116 .justify_between()
117 .when(has_left_items, |this| {
118 this.child(
119 h_stack()
120 .flex_1()
121 .justify_start()
122 .children(self.left_items().map(|item| item.to_any())),
123 )
124 })
125 .when(has_right_items, |this| {
126 this.child(
127 h_stack()
128 .flex_1()
129 .justify_end()
130 .children(self.right_items().map(|item| item.to_any())),
131 )
132 }),
133 )
134 .children(secondary_item)
135 }
136}
137
138// todo!()
139// impl View for Toolbar {
140// fn ui_name() -> &'static str {
141// "Toolbar"
142// }
143
144// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
145// let theme = &theme::current(cx).workspace.toolbar;
146
147// let mut primary_left_items = Vec::new();
148// let mut primary_right_items = Vec::new();
149// let mut secondary_item = None;
150// let spacing = theme.item_spacing;
151// let mut primary_items_row_count = 1;
152
153// for (item, position) in &self.items {
154// match *position {
155// ToolbarItemLocation::Hidden => {}
156
157// ToolbarItemLocation::PrimaryLeft { flex } => {
158// primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
159// let left_item = ChildView::new(item.as_any(), cx).aligned();
160// if let Some((flex, expanded)) = flex {
161// primary_left_items.push(left_item.flex(flex, expanded).into_any());
162// } else {
163// primary_left_items.push(left_item.into_any());
164// }
165// }
166
167// ToolbarItemLocation::PrimaryRight { flex } => {
168// primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
169// let right_item = ChildView::new(item.as_any(), cx).aligned().flex_float();
170// if let Some((flex, expanded)) = flex {
171// primary_right_items.push(right_item.flex(flex, expanded).into_any());
172// } else {
173// primary_right_items.push(right_item.into_any());
174// }
175// }
176
177// ToolbarItemLocation::Secondary => {
178// secondary_item = Some(
179// ChildView::new(item.as_any(), cx)
180// .constrained()
181// .with_height(theme.height * item.row_count(cx) as f32)
182// .into_any(),
183// );
184// }
185// }
186// }
187
188// let container_style = theme.container;
189// let height = theme.height * primary_items_row_count as f32;
190
191// let mut primary_items = Flex::row().with_spacing(spacing);
192// primary_items.extend(primary_left_items);
193// primary_items.extend(primary_right_items);
194
195// let mut toolbar = Flex::column();
196// if !primary_items.is_empty() {
197// toolbar.add_child(primary_items.constrained().with_height(height));
198// }
199// if let Some(secondary_item) = secondary_item {
200// toolbar.add_child(secondary_item);
201// }
202
203// if toolbar.is_empty() {
204// toolbar.into_any_named("toolbar")
205// } else {
206// toolbar
207// .contained()
208// .with_style(container_style)
209// .into_any_named("toolbar")
210// }
211// }
212// }
213
214impl Toolbar {
215 pub fn new() -> Self {
216 Self {
217 active_item: None,
218 items: Default::default(),
219 hidden: false,
220 can_navigate: true,
221 }
222 }
223
224 pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext<Self>) {
225 self.can_navigate = can_navigate;
226 cx.notify();
227 }
228
229 pub fn add_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
230 where
231 T: 'static + ToolbarItemView,
232 {
233 let location = item.set_active_pane_item(self.active_item.as_deref(), cx);
234 cx.subscribe(&item, |this, item, event, cx| {
235 if let Some((_, current_location)) =
236 this.items.iter_mut().find(|(i, _)| i.id() == item.id())
237 {
238 match event {
239 ToolbarItemEvent::ChangeLocation(new_location) => {
240 if new_location != current_location {
241 *current_location = *new_location;
242 cx.notify();
243 }
244 }
245 }
246 }
247 })
248 .detach();
249 self.items.push((Box::new(item), location));
250 cx.notify();
251 }
252
253 pub fn set_active_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
254 self.active_item = item.map(|item| item.boxed_clone());
255 self.hidden = self
256 .active_item
257 .as_ref()
258 .map(|item| !item.show_toolbar(cx))
259 .unwrap_or(false);
260
261 for (toolbar_item, current_location) in self.items.iter_mut() {
262 let new_location = toolbar_item.set_active_pane_item(item, cx);
263 if new_location != *current_location {
264 *current_location = new_location;
265 cx.notify();
266 }
267 }
268 }
269
270 pub fn focus_changed(&mut self, focused: bool, cx: &mut ViewContext<Self>) {
271 for (toolbar_item, _) in self.items.iter_mut() {
272 toolbar_item.focus_changed(focused, cx);
273 }
274 }
275
276 pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<View<T>> {
277 self.items
278 .iter()
279 .find_map(|(item, _)| item.to_any().downcast().ok())
280 }
281
282 pub fn hidden(&self) -> bool {
283 self.hidden
284 }
285}
286
287impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
288 fn id(&self) -> EntityId {
289 self.entity_id()
290 }
291
292 fn to_any(&self) -> AnyView {
293 self.clone().into()
294 }
295
296 fn set_active_pane_item(
297 &self,
298 active_pane_item: Option<&dyn ItemHandle>,
299 cx: &mut WindowContext,
300 ) -> ToolbarItemLocation {
301 self.update(cx, |this, cx| {
302 this.set_active_pane_item(active_pane_item, cx)
303 })
304 }
305
306 fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext) {
307 self.update(cx, |this, cx| {
308 this.pane_focus_update(pane_focused, cx);
309 cx.notify();
310 });
311 }
312
313 fn row_count(&self, cx: &WindowContext) -> usize {
314 self.read(cx).row_count(cx)
315 }
316}
317
318// todo!()
319// impl From<&dyn ToolbarItemViewHandle> for AnyViewHandle {
320// fn from(val: &dyn ToolbarItemViewHandle) -> Self {
321// val.as_any().clone()
322// }
323// }