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