1use crate::ItemHandle;
2use gpui::{
3 AnyView, Entity, EntityId, EventEmitter, ParentElement as _, Render, Styled, View, ViewContext,
4 WindowContext,
5};
6use ui::prelude::*;
7use ui::{h_flex, v_flex};
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
23trait ToolbarItemViewHandle: Send {
24 fn id(&self) -> EntityId;
25 fn to_any(&self) -> AnyView;
26 fn set_active_pane_item(
27 &self,
28 active_pane_item: Option<&dyn ItemHandle>,
29 cx: &mut WindowContext,
30 ) -> ToolbarItemLocation;
31 fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext);
32}
33
34#[derive(Copy, Clone, Debug, PartialEq)]
35pub enum ToolbarItemLocation {
36 Hidden,
37 PrimaryLeft,
38 PrimaryRight,
39 Secondary,
40}
41
42pub struct Toolbar {
43 active_item: Option<Box<dyn ItemHandle>>,
44 hidden: bool,
45 can_navigate: bool,
46 items: Vec<(Box<dyn ToolbarItemViewHandle>, ToolbarItemLocation)>,
47}
48
49impl Toolbar {
50 fn has_any_visible_items(&self) -> bool {
51 self.items
52 .iter()
53 .any(|(_item, location)| *location != ToolbarItemLocation::Hidden)
54 }
55
56 fn left_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
57 self.items.iter().filter_map(|(item, location)| {
58 if *location == ToolbarItemLocation::PrimaryLeft {
59 Some(item.as_ref())
60 } else {
61 None
62 }
63 })
64 }
65
66 fn right_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
67 self.items.iter().filter_map(|(item, location)| {
68 if *location == ToolbarItemLocation::PrimaryRight {
69 Some(item.as_ref())
70 } else {
71 None
72 }
73 })
74 }
75
76 fn secondary_items(&self) -> impl Iterator<Item = &dyn ToolbarItemViewHandle> {
77 self.items.iter().filter_map(|(item, location)| {
78 if *location == ToolbarItemLocation::Secondary {
79 Some(item.as_ref())
80 } else {
81 None
82 }
83 })
84 }
85}
86
87impl Render for Toolbar {
88 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
89 if !self.has_any_visible_items() {
90 return div();
91 }
92
93 let secondary_item = self.secondary_items().next().map(|item| item.to_any());
94
95 let has_left_items = self.left_items().count() > 0;
96 let has_right_items = self.right_items().count() > 0;
97
98 v_flex()
99 .group("toolbar")
100 .p(Spacing::Large.rems(cx))
101 .when(has_left_items || has_right_items, |this| {
102 this.gap(Spacing::Large.rems(cx))
103 })
104 .border_b_1()
105 .border_color(cx.theme().colors().border_variant)
106 .bg(cx.theme().colors().toolbar_background)
107 .child(
108 h_flex()
109 .min_h(rems_from_px(24.))
110 .justify_between()
111 .gap(Spacing::Large.rems(cx))
112 .when(has_left_items, |this| {
113 this.child(
114 h_flex()
115 .flex_auto()
116 .justify_start()
117 .overflow_x_hidden()
118 .children(self.left_items().map(|item| item.to_any())),
119 )
120 })
121 .when(has_right_items, |this| {
122 this.child(
123 h_flex()
124 .map(|el| {
125 if has_left_items {
126 // We're using `flex_none` here to prevent some flickering that can occur when the
127 // size of the left items container changes.
128 el.flex_none()
129 } else {
130 el.flex_auto()
131 }
132 })
133 .justify_end()
134 .children(self.right_items().map(|item| item.to_any())),
135 )
136 }),
137 )
138 .children(secondary_item)
139 }
140}
141
142impl Default for Toolbar {
143 fn default() -> Self {
144 Self::new()
145 }
146}
147
148impl Toolbar {
149 pub fn new() -> Self {
150 Self {
151 active_item: None,
152 items: Default::default(),
153 hidden: false,
154 can_navigate: true,
155 }
156 }
157
158 pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext<Self>) {
159 self.can_navigate = can_navigate;
160 cx.notify();
161 }
162
163 pub fn add_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
164 where
165 T: 'static + ToolbarItemView,
166 {
167 let location = item.set_active_pane_item(self.active_item.as_deref(), cx);
168 cx.subscribe(&item, |this, item, event, cx| {
169 if let Some((_, current_location)) = this
170 .items
171 .iter_mut()
172 .find(|(i, _)| i.id() == item.entity_id())
173 {
174 match event {
175 ToolbarItemEvent::ChangeLocation(new_location) => {
176 if new_location != current_location {
177 *current_location = *new_location;
178 cx.notify();
179 }
180 }
181 }
182 }
183 })
184 .detach();
185 self.items.push((Box::new(item), location));
186 cx.notify();
187 }
188
189 pub fn set_active_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
190 self.active_item = item.map(|item| item.boxed_clone());
191 self.hidden = self
192 .active_item
193 .as_ref()
194 .map(|item| !item.show_toolbar(cx))
195 .unwrap_or(false);
196
197 for (toolbar_item, current_location) in self.items.iter_mut() {
198 let new_location = toolbar_item.set_active_pane_item(item, cx);
199 if new_location != *current_location {
200 *current_location = new_location;
201 cx.notify();
202 }
203 }
204 }
205
206 pub fn focus_changed(&mut self, focused: bool, cx: &mut ViewContext<Self>) {
207 for (toolbar_item, _) in self.items.iter_mut() {
208 toolbar_item.focus_changed(focused, cx);
209 }
210 }
211
212 pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<View<T>> {
213 self.items
214 .iter()
215 .find_map(|(item, _)| item.to_any().downcast().ok())
216 }
217
218 pub fn hidden(&self) -> bool {
219 self.hidden
220 }
221}
222
223impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
224 fn id(&self) -> EntityId {
225 self.entity_id()
226 }
227
228 fn to_any(&self) -> AnyView {
229 self.clone().into()
230 }
231
232 fn set_active_pane_item(
233 &self,
234 active_pane_item: Option<&dyn ItemHandle>,
235 cx: &mut WindowContext,
236 ) -> ToolbarItemLocation {
237 self.update(cx, |this, cx| {
238 this.set_active_pane_item(active_pane_item, cx)
239 })
240 }
241
242 fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext) {
243 self.update(cx, |this, cx| {
244 this.pane_focus_update(pane_focused, cx);
245 cx.notify();
246 });
247 }
248}