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        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.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.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(move |_, _, cx| cx.dispatch_action(action.clone()))
195    .with_tooltip::<A, _>(
196        0,
197        action_name.to_string(),
198        Some(Box::new(tooltip_action)),
199        tooltip_style,
200        cx,
201    )
202    .contained()
203    .with_margin_right(spacing)
204    .boxed()
205}
206
207impl Toolbar {
208    pub fn new(pane: WeakViewHandle<Pane>) -> Self {
209        Self {
210            active_pane_item: None,
211            pane,
212            items: Default::default(),
213        }
214    }
215
216    pub fn add_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
217    where
218        T: 'static + ToolbarItemView,
219    {
220        let location = item.set_active_pane_item(self.active_pane_item.as_deref(), cx);
221        cx.subscribe(&item, |this, item, event, cx| {
222            if let Some((_, current_location)) =
223                this.items.iter_mut().find(|(i, _)| i.id() == item.id())
224            {
225                let new_location = item
226                    .read(cx)
227                    .location_for_event(event, *current_location, cx);
228                if new_location != *current_location {
229                    *current_location = new_location;
230                    cx.notify();
231                }
232            }
233        })
234        .detach();
235        self.items.push((Box::new(item), location));
236        cx.notify();
237    }
238
239    pub fn set_active_pane_item(
240        &mut self,
241        pane_item: Option<&dyn ItemHandle>,
242        cx: &mut ViewContext<Self>,
243    ) {
244        self.active_pane_item = pane_item.map(|item| item.boxed_clone());
245        for (toolbar_item, current_location) in self.items.iter_mut() {
246            let new_location = toolbar_item.set_active_pane_item(pane_item, cx);
247            if new_location != *current_location {
248                *current_location = new_location;
249                cx.notify();
250            }
251        }
252    }
253
254    pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<ViewHandle<T>> {
255        self.items
256            .iter()
257            .find_map(|(item, _)| item.to_any().downcast())
258    }
259}
260
261impl<T: ToolbarItemView> ToolbarItemViewHandle for ViewHandle<T> {
262    fn id(&self) -> usize {
263        self.id()
264    }
265
266    fn to_any(&self) -> AnyViewHandle {
267        self.into()
268    }
269
270    fn set_active_pane_item(
271        &self,
272        active_pane_item: Option<&dyn ItemHandle>,
273        cx: &mut MutableAppContext,
274    ) -> ToolbarItemLocation {
275        self.update(cx, |this, cx| {
276            this.set_active_pane_item(active_pane_item, cx)
277        })
278    }
279}
280
281impl Into<AnyViewHandle> for &dyn ToolbarItemViewHandle {
282    fn into(self) -> AnyViewHandle {
283        self.to_any()
284    }
285}