toolbar.rs

  1use crate::{ItemHandle, Pane};
  2use gpui::{
  3    elements::*, platform::CursorStyle, Action, AnyViewHandle, AppContext, ElementBox, Entity,
  4    MouseButton, 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        let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
116
117        Flex::column()
118            .with_child(
119                Flex::row()
120                    .with_child(nav_button(
121                        "icons/arrow_left_16.svg",
122                        button_style,
123                        tooltip_style.clone(),
124                        enable_go_backward,
125                        spacing,
126                        super::GoBack {
127                            pane: Some(pane.clone()),
128                        },
129                        super::GoBack { pane: None },
130                        "Go Back",
131                        cx,
132                    ))
133                    .with_child(nav_button(
134                        "icons/arrow_right_16.svg",
135                        button_style,
136                        tooltip_style.clone(),
137                        enable_go_forward,
138                        spacing,
139                        super::GoForward {
140                            pane: Some(pane.clone()),
141                        },
142                        super::GoForward { pane: None },
143                        "Go Forward",
144                        cx,
145                    ))
146                    .with_children(primary_left_items)
147                    .with_children(primary_right_items)
148                    .constrained()
149                    .with_height(height)
150                    .boxed(),
151            )
152            .with_children(secondary_item)
153            .contained()
154            .with_style(container_style)
155            .boxed()
156    }
157}
158
159fn nav_button<A: Action + Clone>(
160    svg_path: &'static str,
161    style: theme::Interactive<theme::IconButton>,
162    tooltip_style: TooltipStyle,
163    enabled: bool,
164    spacing: f32,
165    action: A,
166    tooltip_action: A,
167    action_name: &str,
168    cx: &mut RenderContext<Toolbar>,
169) -> ElementBox {
170    MouseEventHandler::new::<A, _, _>(0, cx, |state, _| {
171        let style = if enabled {
172            style.style_for(state, false)
173        } else {
174            style.disabled_style()
175        };
176        Svg::new(svg_path)
177            .with_color(style.color)
178            .constrained()
179            .with_width(style.icon_width)
180            .aligned()
181            .contained()
182            .with_style(style.container)
183            .constrained()
184            .with_width(style.button_width)
185            .with_height(style.button_width)
186            .aligned()
187            .boxed()
188    })
189    .with_cursor_style(if enabled {
190        CursorStyle::PointingHand
191    } else {
192        CursorStyle::default()
193    })
194    .on_click(MouseButton::Left, move |_, cx| {
195        cx.dispatch_action(action.clone())
196    })
197    .with_tooltip::<A, _>(
198        0,
199        action_name.to_string(),
200        Some(Box::new(tooltip_action)),
201        tooltip_style,
202        cx,
203    )
204    .contained()
205    .with_margin_right(spacing)
206    .boxed()
207}
208
209impl Toolbar {
210    pub fn new(pane: WeakViewHandle<Pane>) -> Self {
211        Self {
212            active_pane_item: None,
213            pane,
214            items: Default::default(),
215        }
216    }
217
218    pub fn add_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
219    where
220        T: 'static + ToolbarItemView,
221    {
222        let location = item.set_active_pane_item(self.active_pane_item.as_deref(), cx);
223        cx.subscribe(&item, |this, item, event, cx| {
224            if let Some((_, current_location)) =
225                this.items.iter_mut().find(|(i, _)| i.id() == item.id())
226            {
227                let new_location = item
228                    .read(cx)
229                    .location_for_event(event, *current_location, cx);
230                if new_location != *current_location {
231                    *current_location = new_location;
232                    cx.notify();
233                }
234            }
235        })
236        .detach();
237        self.items.push((Box::new(item), location));
238        cx.notify();
239    }
240
241    pub fn set_active_pane_item(
242        &mut self,
243        pane_item: Option<&dyn ItemHandle>,
244        cx: &mut ViewContext<Self>,
245    ) {
246        self.active_pane_item = pane_item.map(|item| item.boxed_clone());
247        for (toolbar_item, current_location) in self.items.iter_mut() {
248            let new_location = toolbar_item.set_active_pane_item(pane_item, cx);
249            if new_location != *current_location {
250                *current_location = new_location;
251                cx.notify();
252            }
253        }
254    }
255
256    pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<ViewHandle<T>> {
257        self.items
258            .iter()
259            .find_map(|(item, _)| item.to_any().downcast())
260    }
261}
262
263impl<T: ToolbarItemView> ToolbarItemViewHandle for ViewHandle<T> {
264    fn id(&self) -> usize {
265        self.id()
266    }
267
268    fn to_any(&self) -> AnyViewHandle {
269        self.into()
270    }
271
272    fn set_active_pane_item(
273        &self,
274        active_pane_item: Option<&dyn ItemHandle>,
275        cx: &mut MutableAppContext,
276    ) -> ToolbarItemLocation {
277        self.update(cx, |this, cx| {
278            this.set_active_pane_item(active_pane_item, cx)
279        })
280    }
281}
282
283impl Into<AnyViewHandle> for &dyn ToolbarItemViewHandle {
284    fn into(self) -> AnyViewHandle {
285        self.to_any()
286    }
287}