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)]
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)]
80pub struct ToggleSidebarItem {
81 pub side: Side,
82 pub item_index: usize,
83}
84
85#[derive(Clone, Debug, Deserialize)]
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_drag(move |delta, cx| {
169 let prev_width = *actual_width.borrow();
170 *custom_width.borrow_mut() = 0f32
171 .max(match side {
172 Side::Left => prev_width + delta.x(),
173 Side::Right => prev_width - delta.x(),
174 })
175 .round();
176
177 cx.notify();
178 })
179 .boxed()
180 }
181}
182
183impl Entity for Sidebar {
184 type Event = ();
185}
186
187impl View for Sidebar {
188 fn ui_name() -> &'static str {
189 "Sidebar"
190 }
191
192 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
193 let theme = cx.global::<Settings>().theme.clone();
194 if let Some(active_item) = self.active_item() {
195 let mut container = Flex::row();
196 if matches!(self.side, Side::Right) {
197 container.add_child(self.render_resize_handle(&theme, cx));
198 }
199
200 container.add_child(
201 Hook::new(
202 ChildView::new(active_item.to_any())
203 .constrained()
204 .with_max_width(*self.custom_width.borrow())
205 .boxed(),
206 )
207 .on_after_layout({
208 let actual_width = self.actual_width.clone();
209 move |size, _| *actual_width.borrow_mut() = size.x()
210 })
211 .flex(1., false)
212 .boxed(),
213 );
214 if matches!(self.side, Side::Left) {
215 container.add_child(self.render_resize_handle(&theme, cx));
216 }
217 container.boxed()
218 } else {
219 Empty::new().boxed()
220 }
221 }
222}
223
224impl SidebarButtons {
225 pub fn new(sidebar: ViewHandle<Sidebar>, cx: &mut ViewContext<Self>) -> Self {
226 cx.observe(&sidebar, |_, _, cx| cx.notify()).detach();
227 Self { sidebar }
228 }
229}
230
231impl Entity for SidebarButtons {
232 type Event = ();
233}
234
235impl View for SidebarButtons {
236 fn ui_name() -> &'static str {
237 "SidebarToggleButton"
238 }
239
240 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
241 let theme = &cx
242 .global::<Settings>()
243 .theme
244 .workspace
245 .status_bar
246 .sidebar_buttons;
247 let sidebar = self.sidebar.read(cx);
248 let item_style = theme.item;
249 let badge_style = theme.badge;
250 let active_ix = sidebar.active_item_ix;
251 let side = sidebar.side;
252 let group_style = match side {
253 Side::Left => theme.group_left,
254 Side::Right => theme.group_right,
255 };
256 let items = sidebar
257 .items
258 .iter()
259 .map(|item| (item.icon_path, item.view.clone()))
260 .collect::<Vec<_>>();
261 Flex::row()
262 .with_children(
263 items
264 .into_iter()
265 .enumerate()
266 .map(|(ix, (icon_path, item_view))| {
267 MouseEventHandler::new::<Self, _, _>(ix, cx, move |state, cx| {
268 let is_active = Some(ix) == active_ix;
269 let style = item_style.style_for(state, is_active);
270 Stack::new()
271 .with_child(
272 Svg::new(icon_path).with_color(style.icon_color).boxed(),
273 )
274 .with_children(if !is_active && item_view.should_show_badge(cx) {
275 Some(
276 Empty::new()
277 .collapsed()
278 .contained()
279 .with_style(badge_style)
280 .aligned()
281 .bottom()
282 .right()
283 .boxed(),
284 )
285 } else {
286 None
287 })
288 .constrained()
289 .with_width(style.icon_size)
290 .with_height(style.icon_size)
291 .contained()
292 .with_style(style.container)
293 .boxed()
294 })
295 .with_cursor_style(CursorStyle::PointingHand)
296 .on_click(move |_, _, cx| {
297 cx.dispatch_action(ToggleSidebarItem {
298 side,
299 item_index: ix,
300 })
301 })
302 .boxed()
303 }),
304 )
305 .contained()
306 .with_style(group_style)
307 .boxed()
308 }
309}
310
311impl StatusItemView for SidebarButtons {
312 fn set_active_pane_item(
313 &mut self,
314 _: Option<&dyn crate::ItemHandle>,
315 _: &mut ViewContext<Self>,
316 ) {
317 }
318}