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 .p(Spacing::Large.rems(cx))
100 .when(has_left_items || has_right_items, |this| {
101 this.gap(Spacing::Large.rems(cx))
102 })
103 .border_b_1()
104 .border_color(cx.theme().colors().border_variant)
105 .bg(cx.theme().colors().toolbar_background)
106 .child(
107 h_flex()
108 .min_h(rems_from_px(24.))
109 .justify_between()
110 .gap(Spacing::Large.rems(cx))
111 .when(has_left_items, |this| {
112 this.child(
113 h_flex()
114 .flex_auto()
115 .justify_start()
116 .overflow_x_hidden()
117 .children(self.left_items().map(|item| item.to_any())),
118 )
119 })
120 .when(has_right_items, |this| {
121 this.child(
122 h_flex()
123 .map(|el| {
124 if has_left_items {
125 // We're using `flex_none` here to prevent some flickering that can occur when the
126 // size of the left items container changes.
127 el.flex_none()
128 } else {
129 el.flex_auto()
130 }
131 })
132 .justify_end()
133 .children(self.right_items().map(|item| item.to_any())),
134 )
135 }),
136 )
137 .children(secondary_item)
138 }
139}
140
141impl Toolbar {
142 pub fn new() -> Self {
143 Self {
144 active_item: None,
145 items: Default::default(),
146 hidden: false,
147 can_navigate: true,
148 }
149 }
150
151 pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext<Self>) {
152 self.can_navigate = can_navigate;
153 cx.notify();
154 }
155
156 pub fn add_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
157 where
158 T: 'static + ToolbarItemView,
159 {
160 let location = item.set_active_pane_item(self.active_item.as_deref(), cx);
161 cx.subscribe(&item, |this, item, event, cx| {
162 if let Some((_, current_location)) = this
163 .items
164 .iter_mut()
165 .find(|(i, _)| i.id() == item.entity_id())
166 {
167 match event {
168 ToolbarItemEvent::ChangeLocation(new_location) => {
169 if new_location != current_location {
170 *current_location = *new_location;
171 cx.notify();
172 }
173 }
174 }
175 }
176 })
177 .detach();
178 self.items.push((Box::new(item), location));
179 cx.notify();
180 }
181
182 pub fn set_active_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
183 self.active_item = item.map(|item| item.boxed_clone());
184 self.hidden = self
185 .active_item
186 .as_ref()
187 .map(|item| !item.show_toolbar(cx))
188 .unwrap_or(false);
189
190 for (toolbar_item, current_location) in self.items.iter_mut() {
191 let new_location = toolbar_item.set_active_pane_item(item, cx);
192 if new_location != *current_location {
193 *current_location = new_location;
194 cx.notify();
195 }
196 }
197 }
198
199 pub fn focus_changed(&mut self, focused: bool, cx: &mut ViewContext<Self>) {
200 for (toolbar_item, _) in self.items.iter_mut() {
201 toolbar_item.focus_changed(focused, cx);
202 }
203 }
204
205 pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<View<T>> {
206 self.items
207 .iter()
208 .find_map(|(item, _)| item.to_any().downcast().ok())
209 }
210
211 pub fn hidden(&self) -> bool {
212 self.hidden
213 }
214}
215
216impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
217 fn id(&self) -> EntityId {
218 self.entity_id()
219 }
220
221 fn to_any(&self) -> AnyView {
222 self.clone().into()
223 }
224
225 fn set_active_pane_item(
226 &self,
227 active_pane_item: Option<&dyn ItemHandle>,
228 cx: &mut WindowContext,
229 ) -> ToolbarItemLocation {
230 self.update(cx, |this, cx| {
231 this.set_active_pane_item(active_pane_item, cx)
232 })
233 }
234
235 fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext) {
236 self.update(cx, |this, cx| {
237 this.pane_focus_update(pane_focused, cx);
238 cx.notify();
239 });
240 }
241}