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