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 v_stack()
106 .border_b()
107 .border_color(cx.theme().colors().border_variant)
108 .bg(cx.theme().colors().toolbar_background)
109 .child(
110 h_stack()
111 .justify_between()
112 .child(h_stack().children(self.left_items().map(|item| item.to_any())))
113 .child(h_stack().children(self.right_items().map(|item| item.to_any()))),
114 )
115 .children(secondary_item)
116 }
117}
118
119// todo!()
120// impl View for Toolbar {
121// fn ui_name() -> &'static str {
122// "Toolbar"
123// }
124
125// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
126// let theme = &theme::current(cx).workspace.toolbar;
127
128// let mut primary_left_items = Vec::new();
129// let mut primary_right_items = Vec::new();
130// let mut secondary_item = None;
131// let spacing = theme.item_spacing;
132// let mut primary_items_row_count = 1;
133
134// for (item, position) in &self.items {
135// match *position {
136// ToolbarItemLocation::Hidden => {}
137
138// ToolbarItemLocation::PrimaryLeft { flex } => {
139// primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
140// let left_item = ChildView::new(item.as_any(), cx).aligned();
141// if let Some((flex, expanded)) = flex {
142// primary_left_items.push(left_item.flex(flex, expanded).into_any());
143// } else {
144// primary_left_items.push(left_item.into_any());
145// }
146// }
147
148// ToolbarItemLocation::PrimaryRight { flex } => {
149// primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
150// let right_item = ChildView::new(item.as_any(), cx).aligned().flex_float();
151// if let Some((flex, expanded)) = flex {
152// primary_right_items.push(right_item.flex(flex, expanded).into_any());
153// } else {
154// primary_right_items.push(right_item.into_any());
155// }
156// }
157
158// ToolbarItemLocation::Secondary => {
159// secondary_item = Some(
160// ChildView::new(item.as_any(), cx)
161// .constrained()
162// .with_height(theme.height * item.row_count(cx) as f32)
163// .into_any(),
164// );
165// }
166// }
167// }
168
169// let container_style = theme.container;
170// let height = theme.height * primary_items_row_count as f32;
171
172// let mut primary_items = Flex::row().with_spacing(spacing);
173// primary_items.extend(primary_left_items);
174// primary_items.extend(primary_right_items);
175
176// let mut toolbar = Flex::column();
177// if !primary_items.is_empty() {
178// toolbar.add_child(primary_items.constrained().with_height(height));
179// }
180// if let Some(secondary_item) = secondary_item {
181// toolbar.add_child(secondary_item);
182// }
183
184// if toolbar.is_empty() {
185// toolbar.into_any_named("toolbar")
186// } else {
187// toolbar
188// .contained()
189// .with_style(container_style)
190// .into_any_named("toolbar")
191// }
192// }
193// }
194
195impl Toolbar {
196 pub fn new() -> Self {
197 Self {
198 active_item: None,
199 items: Default::default(),
200 hidden: false,
201 can_navigate: true,
202 }
203 }
204
205 pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext<Self>) {
206 self.can_navigate = can_navigate;
207 cx.notify();
208 }
209
210 pub fn add_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
211 where
212 T: 'static + ToolbarItemView,
213 {
214 let location = item.set_active_pane_item(self.active_item.as_deref(), cx);
215 cx.subscribe(&item, |this, item, event, cx| {
216 if let Some((_, current_location)) =
217 this.items.iter_mut().find(|(i, _)| i.id() == item.id())
218 {
219 match event {
220 ToolbarItemEvent::ChangeLocation(new_location) => {
221 if new_location != current_location {
222 *current_location = *new_location;
223 cx.notify();
224 }
225 }
226 }
227 }
228 })
229 .detach();
230 self.items.push((Box::new(item), location));
231 cx.notify();
232 }
233
234 pub fn set_active_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
235 self.active_item = item.map(|item| item.boxed_clone());
236 self.hidden = self
237 .active_item
238 .as_ref()
239 .map(|item| !item.show_toolbar(cx))
240 .unwrap_or(false);
241
242 for (toolbar_item, current_location) in self.items.iter_mut() {
243 let new_location = toolbar_item.set_active_pane_item(item, cx);
244 if new_location != *current_location {
245 *current_location = new_location;
246 cx.notify();
247 }
248 }
249 }
250
251 pub fn focus_changed(&mut self, focused: bool, cx: &mut ViewContext<Self>) {
252 for (toolbar_item, _) in self.items.iter_mut() {
253 toolbar_item.focus_changed(focused, cx);
254 }
255 }
256
257 pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<View<T>> {
258 self.items
259 .iter()
260 .find_map(|(item, _)| item.to_any().downcast().ok())
261 }
262
263 pub fn hidden(&self) -> bool {
264 self.hidden
265 }
266}
267
268impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
269 fn id(&self) -> EntityId {
270 self.entity_id()
271 }
272
273 fn to_any(&self) -> AnyView {
274 self.clone().into()
275 }
276
277 fn set_active_pane_item(
278 &self,
279 active_pane_item: Option<&dyn ItemHandle>,
280 cx: &mut WindowContext,
281 ) -> ToolbarItemLocation {
282 self.update(cx, |this, cx| {
283 this.set_active_pane_item(active_pane_item, cx)
284 })
285 }
286
287 fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext) {
288 self.update(cx, |this, cx| {
289 this.pane_focus_update(pane_focused, cx);
290 cx.notify();
291 });
292 }
293
294 fn row_count(&self, cx: &WindowContext) -> usize {
295 self.read(cx).row_count(cx)
296 }
297}
298
299// todo!()
300// impl From<&dyn ToolbarItemViewHandle> for AnyViewHandle {
301// fn from(val: &dyn ToolbarItemViewHandle) -> Self {
302// val.as_any().clone()
303// }
304// }