toolbar.rs

  1use crate::{ItemHandle, Pane};
  2use gpui::{
  3    elements::*, platform::CursorStyle, Action, AnyViewHandle, AppContext, ElementBox, Entity,
  4    MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle,
  5};
  6use settings::Settings;
  7
  8pub trait ToolbarItemView: View {
  9    fn set_active_pane_item(
 10        &mut self,
 11        active_pane_item: Option<&dyn crate::ItemHandle>,
 12        cx: &mut ViewContext<Self>,
 13    ) -> ToolbarItemLocation;
 14
 15    fn location_for_event(
 16        &self,
 17        _event: &Self::Event,
 18        current_location: ToolbarItemLocation,
 19        _cx: &AppContext,
 20    ) -> ToolbarItemLocation {
 21        current_location
 22    }
 23}
 24
 25trait ToolbarItemViewHandle {
 26    fn id(&self) -> usize;
 27    fn to_any(&self) -> AnyViewHandle;
 28    fn set_active_pane_item(
 29        &self,
 30        active_pane_item: Option<&dyn ItemHandle>,
 31        cx: &mut MutableAppContext,
 32    ) -> ToolbarItemLocation;
 33}
 34
 35#[derive(Copy, Clone, Debug, PartialEq)]
 36pub enum ToolbarItemLocation {
 37    Hidden,
 38    PrimaryLeft { flex: Option<(f32, bool)> },
 39    PrimaryRight { flex: Option<(f32, bool)> },
 40    Secondary,
 41}
 42
 43pub struct Toolbar {
 44    active_pane_item: Option<Box<dyn ItemHandle>>,
 45    pane: WeakViewHandle<Pane>,
 46    items: Vec<(Box<dyn ToolbarItemViewHandle>, ToolbarItemLocation)>,
 47}
 48
 49impl Entity for Toolbar {
 50    type Event = ();
 51}
 52
 53impl View for Toolbar {
 54    fn ui_name() -> &'static str {
 55        "Toolbar"
 56    }
 57
 58    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
 59        let theme = &cx.global::<Settings>().theme.workspace.toolbar;
 60
 61        let mut primary_left_items = Vec::new();
 62        let mut primary_right_items = Vec::new();
 63        let mut secondary_item = None;
 64        let spacing = theme.item_spacing;
 65
 66        for (item, position) in &self.items {
 67            match *position {
 68                ToolbarItemLocation::Hidden => {}
 69                ToolbarItemLocation::PrimaryLeft { flex } => {
 70                    let left_item = ChildView::new(item.as_ref())
 71                        .aligned()
 72                        .contained()
 73                        .with_margin_right(spacing);
 74                    if let Some((flex, expanded)) = flex {
 75                        primary_left_items.push(left_item.flex(flex, expanded).boxed());
 76                    } else {
 77                        primary_left_items.push(left_item.boxed());
 78                    }
 79                }
 80                ToolbarItemLocation::PrimaryRight { flex } => {
 81                    let right_item = ChildView::new(item.as_ref())
 82                        .aligned()
 83                        .contained()
 84                        .with_margin_left(spacing)
 85                        .flex_float();
 86                    if let Some((flex, expanded)) = flex {
 87                        primary_right_items.push(right_item.flex(flex, expanded).boxed());
 88                    } else {
 89                        primary_right_items.push(right_item.boxed());
 90                    }
 91                }
 92                ToolbarItemLocation::Secondary => {
 93                    secondary_item = Some(
 94                        ChildView::new(item.as_ref())
 95                            .constrained()
 96                            .with_height(theme.height)
 97                            .boxed(),
 98                    );
 99                }
100            }
101        }
102
103        let pane = self.pane.clone();
104        let mut enable_go_backward = false;
105        let mut enable_go_forward = false;
106        if let Some(pane) = pane.upgrade(cx) {
107            let pane = pane.read(cx);
108            enable_go_backward = pane.can_navigate_backward();
109            enable_go_forward = pane.can_navigate_forward();
110        }
111
112        let container_style = theme.container;
113        let height = theme.height;
114        let button_style = theme.nav_button;
115
116        Flex::column()
117            .with_child(
118                Flex::row()
119                    .with_child(nav_button(
120                        "icons/arrow-left.svg",
121                        button_style,
122                        enable_go_backward,
123                        spacing,
124                        super::GoBack {
125                            pane: Some(pane.clone()),
126                        },
127                        cx,
128                    ))
129                    .with_child(nav_button(
130                        "icons/arrow-right.svg",
131                        button_style,
132                        enable_go_forward,
133                        spacing,
134                        super::GoForward {
135                            pane: Some(pane.clone()),
136                        },
137                        cx,
138                    ))
139                    .with_children(primary_left_items)
140                    .with_children(primary_right_items)
141                    .constrained()
142                    .with_height(height)
143                    .boxed(),
144            )
145            .with_children(secondary_item)
146            .contained()
147            .with_style(container_style)
148            .boxed()
149    }
150}
151
152fn nav_button<A: Action + Clone>(
153    svg_path: &'static str,
154    style: theme::Interactive<theme::IconButton>,
155    enabled: bool,
156    spacing: f32,
157    action: A,
158    cx: &mut RenderContext<Toolbar>,
159) -> ElementBox {
160    MouseEventHandler::new::<A, _, _>(0, cx, |state, _| {
161        let style = if enabled {
162            style.style_for(state, false)
163        } else {
164            style.disabled_style()
165        };
166        Svg::new(svg_path)
167            .with_color(style.color)
168            .constrained()
169            .with_width(style.icon_width)
170            .aligned()
171            .contained()
172            .with_style(style.container)
173            .constrained()
174            .with_width(style.button_width)
175            .with_height(style.button_width)
176            .aligned()
177            .boxed()
178    })
179    .with_cursor_style(if enabled {
180        CursorStyle::PointingHand
181    } else {
182        CursorStyle::default()
183    })
184    .on_mouse_down(move |_, cx| cx.dispatch_action(action.clone()))
185    .contained()
186    .with_margin_right(spacing)
187    .boxed()
188}
189
190impl Toolbar {
191    pub fn new(pane: WeakViewHandle<Pane>) -> Self {
192        Self {
193            active_pane_item: None,
194            pane,
195            items: Default::default(),
196        }
197    }
198
199    pub fn add_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
200    where
201        T: 'static + ToolbarItemView,
202    {
203        let location = item.set_active_pane_item(self.active_pane_item.as_deref(), cx);
204        cx.subscribe(&item, |this, item, event, cx| {
205            if let Some((_, current_location)) =
206                this.items.iter_mut().find(|(i, _)| i.id() == item.id())
207            {
208                let new_location = item
209                    .read(cx)
210                    .location_for_event(event, *current_location, cx);
211                if new_location != *current_location {
212                    *current_location = new_location;
213                    cx.notify();
214                }
215            }
216        })
217        .detach();
218        self.items.push((Box::new(item), location));
219        cx.notify();
220    }
221
222    pub fn set_active_pane_item(
223        &mut self,
224        pane_item: Option<&dyn ItemHandle>,
225        cx: &mut ViewContext<Self>,
226    ) {
227        self.active_pane_item = pane_item.map(|item| item.boxed_clone());
228        for (toolbar_item, current_location) in self.items.iter_mut() {
229            let new_location = toolbar_item.set_active_pane_item(pane_item, cx);
230            if new_location != *current_location {
231                *current_location = new_location;
232                cx.notify();
233            }
234        }
235    }
236
237    pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<ViewHandle<T>> {
238        self.items
239            .iter()
240            .find_map(|(item, _)| item.to_any().downcast())
241    }
242}
243
244impl<T: ToolbarItemView> ToolbarItemViewHandle for ViewHandle<T> {
245    fn id(&self) -> usize {
246        self.id()
247    }
248
249    fn to_any(&self) -> AnyViewHandle {
250        self.into()
251    }
252
253    fn set_active_pane_item(
254        &self,
255        active_pane_item: Option<&dyn ItemHandle>,
256        cx: &mut MutableAppContext,
257    ) -> ToolbarItemLocation {
258        self.update(cx, |this, cx| {
259            this.set_active_pane_item(active_pane_item, cx)
260        })
261    }
262}
263
264impl Into<AnyViewHandle> for &dyn ToolbarItemViewHandle {
265    fn into(self) -> AnyViewHandle {
266        self.to_any()
267    }
268}