1use crate::ItemHandle;
2use gpui::{
3 AnyView, App, Context, Entity, EntityId, EventEmitter, ParentElement as _, Render, Styled,
4 Window,
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 window: &mut Window,
18 cx: &mut Context<Self>,
19 ) -> ToolbarItemLocation;
20
21 fn pane_focus_update(
22 &mut self,
23 _pane_focused: bool,
24 _window: &mut Window,
25 _cx: &mut Context<Self>,
26 ) {
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 window: &mut Window,
37 cx: &mut App,
38 ) -> ToolbarItemLocation;
39 fn focus_changed(&mut self, pane_focused: bool, window: &mut Window, cx: &mut App);
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().rev().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 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
97 if !self.has_any_visible_items() {
98 return div();
99 }
100
101 let secondary_items = self.secondary_items().map(|item| item.to_any());
102
103 let has_left_items = self.left_items().count() > 0;
104 let has_right_items = self.right_items().count() > 0;
105
106 v_flex()
107 .group("toolbar")
108 .relative()
109 .p(DynamicSpacing::Base08.rems(cx))
110 .when(has_left_items || has_right_items, |this| {
111 this.gap(DynamicSpacing::Base08.rems(cx))
112 })
113 .border_b_1()
114 .border_color(cx.theme().colors().border_variant)
115 .bg(cx.theme().colors().toolbar_background)
116 .when(has_left_items || has_right_items, |this| {
117 this.child(
118 h_flex()
119 .min_h_6()
120 .justify_between()
121 .gap(DynamicSpacing::Base08.rems(cx))
122 .when(has_left_items, |this| {
123 this.child(
124 h_flex()
125 .flex_auto()
126 .justify_start()
127 .overflow_x_hidden()
128 .children(self.left_items().map(|item| item.to_any())),
129 )
130 })
131 .when(has_right_items, |this| {
132 this.child(
133 h_flex()
134 .h_full()
135 .flex_row_reverse()
136 .map(|el| {
137 if has_left_items {
138 // We're using `flex_none` here to prevent some flickering that can occur when the
139 // size of the left items container changes.
140 el.flex_none()
141 } else {
142 el.flex_auto()
143 }
144 })
145 .justify_end()
146 .children(self.right_items().map(|item| item.to_any())),
147 )
148 }),
149 )
150 })
151 .children(secondary_items)
152 }
153}
154
155impl Default for Toolbar {
156 fn default() -> Self {
157 Self::new()
158 }
159}
160
161impl Toolbar {
162 pub fn new() -> Self {
163 Self {
164 active_item: None,
165 items: Default::default(),
166 hidden: false,
167 can_navigate: true,
168 }
169 }
170
171 pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut Context<Self>) {
172 self.can_navigate = can_navigate;
173 cx.notify();
174 }
175
176 pub fn add_item<T>(&mut self, item: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
177 where
178 T: 'static + ToolbarItemView,
179 {
180 let location = item.set_active_pane_item(self.active_item.as_deref(), window, cx);
181 cx.subscribe(&item, |this, item, event, cx| {
182 if let Some((_, current_location)) = this
183 .items
184 .iter_mut()
185 .find(|(i, _)| i.id() == item.entity_id())
186 {
187 match event {
188 ToolbarItemEvent::ChangeLocation(new_location) => {
189 if new_location != current_location {
190 *current_location = *new_location;
191 cx.notify();
192 }
193 }
194 }
195 }
196 })
197 .detach();
198 self.items.push((Box::new(item), location));
199 cx.notify();
200 }
201
202 pub fn set_active_item(
203 &mut self,
204 item: Option<&dyn ItemHandle>,
205 window: &mut Window,
206 cx: &mut Context<Self>,
207 ) {
208 self.active_item = item.map(|item| item.boxed_clone());
209 self.hidden = self
210 .active_item
211 .as_ref()
212 .map(|item| !item.show_toolbar(cx))
213 .unwrap_or(false);
214
215 for (toolbar_item, current_location) in self.items.iter_mut() {
216 let new_location = toolbar_item.set_active_pane_item(item, window, cx);
217 if new_location != *current_location {
218 *current_location = new_location;
219 cx.notify();
220 }
221 }
222 }
223
224 pub fn focus_changed(&mut self, focused: bool, window: &mut Window, cx: &mut Context<Self>) {
225 for (toolbar_item, _) in self.items.iter_mut() {
226 toolbar_item.focus_changed(focused, window, cx);
227 }
228 }
229
230 pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<Entity<T>> {
231 self.items
232 .iter()
233 .find_map(|(item, _)| item.to_any().downcast().ok())
234 }
235
236 pub fn hidden(&self) -> bool {
237 self.hidden
238 }
239}
240
241impl<T: ToolbarItemView> ToolbarItemViewHandle for Entity<T> {
242 fn id(&self) -> EntityId {
243 self.entity_id()
244 }
245
246 fn to_any(&self) -> AnyView {
247 self.clone().into()
248 }
249
250 fn set_active_pane_item(
251 &self,
252 active_pane_item: Option<&dyn ItemHandle>,
253 window: &mut Window,
254 cx: &mut App,
255 ) -> ToolbarItemLocation {
256 self.update(cx, |this, cx| {
257 this.set_active_pane_item(active_pane_item, window, cx)
258 })
259 }
260
261 fn focus_changed(&mut self, pane_focused: bool, window: &mut Window, cx: &mut App) {
262 self.update(cx, |this, cx| {
263 this.pane_focus_update(pane_focused, window, cx);
264 cx.notify();
265 });
266 }
267}