toolbar.rs

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