1use crate::StatusItemView;
2use gpui::{
3 elements::*, impl_actions, platform::CursorStyle, AnyViewHandle, AppContext, Entity,
4 RenderContext, Subscription, View, ViewContext, ViewHandle,
5};
6use serde::Deserialize;
7use settings::Settings;
8use std::{cell::RefCell, rc::Rc};
9use theme::Theme;
10
11pub trait SidebarItem: View {
12 fn should_activate_item_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
13 false
14 }
15 fn should_show_badge(&self, cx: &AppContext) -> bool;
16 fn contains_focused_view(&self, _: &AppContext) -> bool {
17 false
18 }
19}
20
21pub trait SidebarItemHandle {
22 fn id(&self) -> usize;
23 fn should_show_badge(&self, cx: &AppContext) -> bool;
24 fn is_focused(&self, cx: &AppContext) -> bool;
25 fn to_any(&self) -> AnyViewHandle;
26}
27
28impl<T> SidebarItemHandle for ViewHandle<T>
29where
30 T: SidebarItem,
31{
32 fn id(&self) -> usize {
33 self.id()
34 }
35
36 fn should_show_badge(&self, cx: &AppContext) -> bool {
37 self.read(cx).should_show_badge(cx)
38 }
39
40 fn is_focused(&self, cx: &AppContext) -> bool {
41 ViewHandle::is_focused(&self, cx) || self.read(cx).contains_focused_view(cx)
42 }
43
44 fn to_any(&self) -> AnyViewHandle {
45 self.into()
46 }
47}
48
49impl Into<AnyViewHandle> for &dyn SidebarItemHandle {
50 fn into(self) -> AnyViewHandle {
51 self.to_any()
52 }
53}
54
55pub struct Sidebar {
56 side: Side,
57 items: Vec<Item>,
58 active_item_ix: Option<usize>,
59 actual_width: Rc<RefCell<f32>>,
60 custom_width: Rc<RefCell<f32>>,
61}
62
63#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
64pub enum Side {
65 Left,
66 Right,
67}
68
69struct Item {
70 icon_path: &'static str,
71 view: Rc<dyn SidebarItemHandle>,
72 _subscriptions: [Subscription; 2],
73}
74
75pub struct SidebarButtons {
76 sidebar: ViewHandle<Sidebar>,
77}
78
79#[derive(Clone, Debug, Deserialize, PartialEq)]
80pub struct ToggleSidebarItem {
81 pub side: Side,
82 pub item_index: usize,
83}
84
85#[derive(Clone, Debug, Deserialize, PartialEq)]
86pub struct ToggleSidebarItemFocus {
87 pub side: Side,
88 pub item_index: usize,
89}
90
91impl_actions!(workspace, [ToggleSidebarItem, ToggleSidebarItemFocus]);
92
93impl Sidebar {
94 pub fn new(side: Side) -> Self {
95 Self {
96 side,
97 items: Default::default(),
98 active_item_ix: None,
99 actual_width: Rc::new(RefCell::new(260.)),
100 custom_width: Rc::new(RefCell::new(260.)),
101 }
102 }
103
104 pub fn add_item<T: SidebarItem>(
105 &mut self,
106 icon_path: &'static str,
107 view: ViewHandle<T>,
108 cx: &mut ViewContext<Self>,
109 ) {
110 let subscriptions = [
111 cx.observe(&view, |_, _, cx| cx.notify()),
112 cx.subscribe(&view, |this, view, event, cx| {
113 if view.read(cx).should_activate_item_on_event(event, cx) {
114 if let Some(ix) = this
115 .items
116 .iter()
117 .position(|item| item.view.id() == view.id())
118 {
119 this.activate_item(ix, cx);
120 }
121 }
122 }),
123 ];
124 self.items.push(Item {
125 icon_path,
126 view: Rc::new(view),
127 _subscriptions: subscriptions,
128 });
129 cx.notify()
130 }
131
132 pub fn activate_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
133 self.active_item_ix = Some(item_ix);
134 cx.notify();
135 }
136
137 pub fn toggle_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
138 if self.active_item_ix == Some(item_ix) {
139 self.active_item_ix = None;
140 } else {
141 self.active_item_ix = Some(item_ix);
142 }
143 cx.notify();
144 }
145
146 pub fn active_item(&self) -> Option<&Rc<dyn SidebarItemHandle>> {
147 self.active_item_ix
148 .and_then(|ix| self.items.get(ix))
149 .map(|item| &item.view)
150 }
151
152 fn render_resize_handle(&self, theme: &Theme, cx: &mut RenderContext<Self>) -> ElementBox {
153 let actual_width = self.actual_width.clone();
154 let custom_width = self.custom_width.clone();
155 let side = self.side;
156 MouseEventHandler::new::<Self, _, _>(side as usize, cx, |_, _| {
157 Empty::new()
158 .contained()
159 .with_style(theme.workspace.sidebar_resize_handle)
160 .boxed()
161 })
162 .with_padding(Padding {
163 left: 4.,
164 right: 4.,
165 ..Default::default()
166 })
167 .with_cursor_style(CursorStyle::ResizeLeftRight)
168 .on_mouse_down(|_, _| {}) // This prevents the mouse down event from being propagated elsewhere
169 .on_drag(move |delta, cx| {
170 let prev_width = *actual_width.borrow();
171 *custom_width.borrow_mut() = 0f32
172 .max(match side {
173 Side::Left => prev_width + delta.x(),
174 Side::Right => prev_width - delta.x(),
175 })
176 .round();
177
178 cx.notify();
179 })
180 .boxed()
181 }
182}
183
184impl Entity for Sidebar {
185 type Event = ();
186}
187
188impl View for Sidebar {
189 fn ui_name() -> &'static str {
190 "Sidebar"
191 }
192
193 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
194 let theme = cx.global::<Settings>().theme.clone();
195 if let Some(active_item) = self.active_item() {
196 let mut container = Flex::row();
197 if matches!(self.side, Side::Right) {
198 container.add_child(self.render_resize_handle(&theme, cx));
199 }
200
201 container.add_child(
202 Hook::new(
203 ChildView::new(active_item.to_any())
204 .constrained()
205 .with_max_width(*self.custom_width.borrow())
206 .boxed(),
207 )
208 .on_after_layout({
209 let actual_width = self.actual_width.clone();
210 move |size, _| *actual_width.borrow_mut() = size.x()
211 })
212 .flex(1., false)
213 .boxed(),
214 );
215 if matches!(self.side, Side::Left) {
216 container.add_child(self.render_resize_handle(&theme, cx));
217 }
218 container.boxed()
219 } else {
220 Empty::new().boxed()
221 }
222 }
223}
224
225impl SidebarButtons {
226 pub fn new(sidebar: ViewHandle<Sidebar>, cx: &mut ViewContext<Self>) -> Self {
227 cx.observe(&sidebar, |_, _, cx| cx.notify()).detach();
228 Self { sidebar }
229 }
230}
231
232impl Entity for SidebarButtons {
233 type Event = ();
234}
235
236impl View for SidebarButtons {
237 fn ui_name() -> &'static str {
238 "SidebarToggleButton"
239 }
240
241 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
242 let theme = &cx
243 .global::<Settings>()
244 .theme
245 .workspace
246 .status_bar
247 .sidebar_buttons;
248 let sidebar = self.sidebar.read(cx);
249 let item_style = theme.item;
250 let badge_style = theme.badge;
251 let active_ix = sidebar.active_item_ix;
252 let side = sidebar.side;
253 let group_style = match side {
254 Side::Left => theme.group_left,
255 Side::Right => theme.group_right,
256 };
257 let items = sidebar
258 .items
259 .iter()
260 .map(|item| (item.icon_path, item.view.clone()))
261 .collect::<Vec<_>>();
262 Flex::row()
263 .with_children(
264 items
265 .into_iter()
266 .enumerate()
267 .map(|(ix, (icon_path, item_view))| {
268 MouseEventHandler::new::<Self, _, _>(ix, cx, move |state, cx| {
269 let is_active = Some(ix) == active_ix;
270 let style = item_style.style_for(state, is_active);
271 Stack::new()
272 .with_child(
273 Svg::new(icon_path).with_color(style.icon_color).boxed(),
274 )
275 .with_children(if !is_active && item_view.should_show_badge(cx) {
276 Some(
277 Empty::new()
278 .collapsed()
279 .contained()
280 .with_style(badge_style)
281 .aligned()
282 .bottom()
283 .right()
284 .boxed(),
285 )
286 } else {
287 None
288 })
289 .constrained()
290 .with_width(style.icon_size)
291 .with_height(style.icon_size)
292 .contained()
293 .with_style(style.container)
294 .boxed()
295 })
296 .with_cursor_style(CursorStyle::PointingHand)
297 .on_click(move |_, _, cx| {
298 cx.dispatch_action(ToggleSidebarItem {
299 side,
300 item_index: ix,
301 })
302 })
303 .boxed()
304 }),
305 )
306 .contained()
307 .with_style(group_style)
308 .boxed()
309 }
310}
311
312impl StatusItemView for SidebarButtons {
313 fn set_active_pane_item(
314 &mut self,
315 _: Option<&dyn crate::ItemHandle>,
316 _: &mut ViewContext<Self>,
317 ) {
318 }
319}