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