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