toolbar.rs

  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                        .items_start()
123                        .justify_between()
124                        .gap(DynamicSpacing::Base08.rems(cx))
125                        .when(has_left_items, |this| {
126                            this.child(
127                                h_flex()
128                                    .min_h_8()
129                                    .flex_auto()
130                                    .justify_start()
131                                    .overflow_x_hidden()
132                                    .children(self.left_items().map(|item| item.to_any())),
133                            )
134                        })
135                        .when(has_right_items, |this| {
136                            this.child(
137                                h_flex()
138                                    .h_8()
139                                    .flex_row_reverse()
140                                    .map(|el| {
141                                        if has_left_items {
142                                            // We're using `flex_none` here to prevent some flickering that can occur when the
143                                            // size of the left items container changes.
144                                            el.flex_none()
145                                        } else {
146                                            el.flex_auto()
147                                        }
148                                    })
149                                    .justify_end()
150                                    .children(self.right_items().map(|item| item.to_any())),
151                            )
152                        }),
153                )
154            })
155            .children(secondary_items)
156    }
157}
158
159impl Default for Toolbar {
160    fn default() -> Self {
161        Self::new()
162    }
163}
164
165impl Toolbar {
166    pub fn new() -> Self {
167        Self {
168            active_item: None,
169            items: Default::default(),
170            hidden: false,
171            can_navigate: true,
172        }
173    }
174
175    pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut Context<Self>) {
176        self.can_navigate = can_navigate;
177        cx.notify();
178    }
179
180    pub fn add_item<T>(&mut self, item: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
181    where
182        T: 'static + ToolbarItemView,
183    {
184        let location = item.set_active_pane_item(self.active_item.as_deref(), window, cx);
185        cx.subscribe(&item, |this, item, event, cx| {
186            if let Some((_, current_location)) = this
187                .items
188                .iter_mut()
189                .find(|(i, _)| i.id() == item.entity_id())
190            {
191                match event {
192                    ToolbarItemEvent::ChangeLocation(new_location) => {
193                        if new_location != current_location {
194                            *current_location = *new_location;
195                            cx.notify();
196                        }
197                    }
198                }
199            }
200        })
201        .detach();
202        self.items.push((Box::new(item), location));
203        cx.notify();
204    }
205
206    pub fn set_active_item(
207        &mut self,
208        item: Option<&dyn ItemHandle>,
209        window: &mut Window,
210        cx: &mut Context<Self>,
211    ) {
212        self.active_item = item.map(|item| item.boxed_clone());
213        self.hidden = self
214            .active_item
215            .as_ref()
216            .map(|item| !item.show_toolbar(cx))
217            .unwrap_or(false);
218
219        for (toolbar_item, current_location) in self.items.iter_mut() {
220            let new_location = toolbar_item.set_active_pane_item(item, window, cx);
221            if new_location != *current_location {
222                *current_location = new_location;
223                cx.notify();
224            }
225        }
226    }
227
228    pub fn focus_changed(&mut self, focused: bool, window: &mut Window, cx: &mut Context<Self>) {
229        for (toolbar_item, _) in self.items.iter_mut() {
230            toolbar_item.focus_changed(focused, window, cx);
231        }
232    }
233
234    pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<Entity<T>> {
235        self.items
236            .iter()
237            .find_map(|(item, _)| item.to_any().downcast().ok())
238    }
239
240    pub fn hidden(&self) -> bool {
241        self.hidden
242    }
243
244    pub fn contribute_context(&self, context: &mut KeyContext, cx: &App) {
245        for (item, location) in &self.items {
246            if *location != ToolbarItemLocation::Hidden {
247                item.contribute_context(context, cx);
248            }
249        }
250    }
251}
252
253impl<T: ToolbarItemView> ToolbarItemViewHandle for Entity<T> {
254    fn id(&self) -> EntityId {
255        self.entity_id()
256    }
257
258    fn to_any(&self) -> AnyView {
259        self.clone().into()
260    }
261
262    fn set_active_pane_item(
263        &self,
264        active_pane_item: Option<&dyn ItemHandle>,
265        window: &mut Window,
266        cx: &mut App,
267    ) -> ToolbarItemLocation {
268        self.update(cx, |this, cx| {
269            this.set_active_pane_item(active_pane_item, window, cx)
270        })
271    }
272
273    fn focus_changed(&mut self, pane_focused: bool, window: &mut Window, cx: &mut App) {
274        self.update(cx, |this, cx| {
275            this.pane_focus_update(pane_focused, window, cx);
276            cx.notify();
277        });
278    }
279
280    fn contribute_context(&self, context: &mut KeyContext, cx: &App) {
281        self.read(cx).contribute_context(context, cx)
282    }
283}