sidebar.rs

  1use gpui::{
  2    elements::*, impl_actions, platform::CursorStyle, AnyViewHandle, Entity, RenderContext, View,
  3    ViewContext, ViewHandle,
  4};
  5use serde::Deserialize;
  6use settings::Settings;
  7use std::{cell::RefCell, rc::Rc};
  8use theme::Theme;
  9
 10use crate::StatusItemView;
 11
 12pub struct Sidebar {
 13    side: Side,
 14    items: Vec<Item>,
 15    active_item_ix: Option<usize>,
 16    actual_width: Rc<RefCell<f32>>,
 17    custom_width: Rc<RefCell<f32>>,
 18}
 19
 20#[derive(Clone, Copy, Debug, Deserialize)]
 21pub enum Side {
 22    Left,
 23    Right,
 24}
 25
 26#[derive(Clone)]
 27struct Item {
 28    icon_path: &'static str,
 29    view: AnyViewHandle,
 30}
 31
 32pub struct SidebarButtons {
 33    sidebar: ViewHandle<Sidebar>,
 34}
 35
 36#[derive(Clone, Debug, Deserialize)]
 37pub struct ToggleSidebarItem {
 38    pub side: Side,
 39    pub item_index: usize,
 40}
 41
 42#[derive(Clone, Debug, Deserialize)]
 43pub struct ToggleSidebarItemFocus {
 44    pub side: Side,
 45    pub item_index: usize,
 46}
 47
 48impl_actions!(workspace, [ToggleSidebarItem, ToggleSidebarItemFocus]);
 49
 50impl Sidebar {
 51    pub fn new(side: Side) -> Self {
 52        Self {
 53            side,
 54            items: Default::default(),
 55            active_item_ix: None,
 56            actual_width: Rc::new(RefCell::new(260.)),
 57            custom_width: Rc::new(RefCell::new(260.)),
 58        }
 59    }
 60
 61    pub fn add_item(
 62        &mut self,
 63        icon_path: &'static str,
 64        view: AnyViewHandle,
 65        cx: &mut ViewContext<Self>,
 66    ) {
 67        self.items.push(Item { icon_path, view });
 68        cx.notify()
 69    }
 70
 71    pub fn activate_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
 72        self.active_item_ix = Some(item_ix);
 73        cx.notify();
 74    }
 75
 76    pub fn toggle_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
 77        if self.active_item_ix == Some(item_ix) {
 78            self.active_item_ix = None;
 79        } else {
 80            self.active_item_ix = Some(item_ix);
 81        }
 82        cx.notify();
 83    }
 84
 85    pub fn active_item(&self) -> Option<&AnyViewHandle> {
 86        self.active_item_ix
 87            .and_then(|ix| self.items.get(ix))
 88            .map(|item| &item.view)
 89    }
 90
 91    fn render_resize_handle(&self, theme: &Theme, cx: &mut RenderContext<Self>) -> ElementBox {
 92        let actual_width = self.actual_width.clone();
 93        let custom_width = self.custom_width.clone();
 94        let side = self.side;
 95        MouseEventHandler::new::<Self, _, _>(side as usize, cx, |_, _| {
 96            Empty::new()
 97                .contained()
 98                .with_style(theme.workspace.sidebar_resize_handle)
 99                .boxed()
100        })
101        .with_padding(Padding {
102            left: 4.,
103            right: 4.,
104            ..Default::default()
105        })
106        .with_cursor_style(CursorStyle::ResizeLeftRight)
107        .on_drag(move |delta, cx| {
108            let prev_width = *actual_width.borrow();
109            *custom_width.borrow_mut() = 0f32
110                .max(match side {
111                    Side::Left => prev_width + delta.x(),
112                    Side::Right => prev_width - delta.x(),
113                })
114                .round();
115
116            cx.notify();
117        })
118        .boxed()
119    }
120}
121
122impl Entity for Sidebar {
123    type Event = ();
124}
125
126impl View for Sidebar {
127    fn ui_name() -> &'static str {
128        "Sidebar"
129    }
130
131    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
132        let theme = cx.global::<Settings>().theme.clone();
133        if let Some(active_item) = self.active_item() {
134            let mut container = Flex::row();
135            if matches!(self.side, Side::Right) {
136                container.add_child(self.render_resize_handle(&theme, cx));
137            }
138
139            container.add_child(
140                Hook::new(
141                    ChildView::new(active_item)
142                        .constrained()
143                        .with_max_width(*self.custom_width.borrow())
144                        .boxed(),
145                )
146                .on_after_layout({
147                    let actual_width = self.actual_width.clone();
148                    move |size, _| *actual_width.borrow_mut() = size.x()
149                })
150                .flex(1., false)
151                .boxed(),
152            );
153            if matches!(self.side, Side::Left) {
154                container.add_child(self.render_resize_handle(&theme, cx));
155            }
156            container.boxed()
157        } else {
158            Empty::new().boxed()
159        }
160    }
161}
162
163impl SidebarButtons {
164    pub fn new(sidebar: ViewHandle<Sidebar>, cx: &mut ViewContext<Self>) -> Self {
165        cx.observe(&sidebar, |_, _, cx| cx.notify()).detach();
166        Self { sidebar }
167    }
168}
169
170impl Entity for SidebarButtons {
171    type Event = ();
172}
173
174impl View for SidebarButtons {
175    fn ui_name() -> &'static str {
176        "SidebarToggleButton"
177    }
178
179    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
180        let theme = &cx
181            .global::<Settings>()
182            .theme
183            .workspace
184            .status_bar
185            .sidebar_buttons;
186        let sidebar = self.sidebar.read(cx);
187        let item_style = theme.item;
188        let active_ix = sidebar.active_item_ix;
189        let side = sidebar.side;
190        let group_style = match side {
191            Side::Left => theme.group_left,
192            Side::Right => theme.group_right,
193        };
194        let items = sidebar.items.clone();
195        Flex::row()
196            .with_children(items.iter().enumerate().map(|(ix, item)| {
197                MouseEventHandler::new::<Self, _, _>(ix, cx, move |state, _| {
198                    let style = item_style.style_for(state, Some(ix) == active_ix);
199                    Svg::new(item.icon_path)
200                        .with_color(style.icon_color)
201                        .constrained()
202                        .with_height(style.icon_size)
203                        .contained()
204                        .with_style(style.container)
205                        .boxed()
206                })
207                .with_cursor_style(CursorStyle::PointingHand)
208                .on_click(move |_, cx| {
209                    cx.dispatch_action(ToggleSidebarItem {
210                        side,
211                        item_index: ix,
212                    })
213                })
214                .boxed()
215            }))
216            .contained()
217            .with_style(group_style)
218            .boxed()
219    }
220}
221
222impl StatusItemView for SidebarButtons {
223    fn set_active_pane_item(
224        &mut self,
225        _: Option<&dyn crate::ItemHandle>,
226        _: &mut ViewContext<Self>,
227    ) {
228    }
229}