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