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