1use crate::{StatusItemView, Workspace};
2use context_menu::{ContextMenu, ContextMenuItem};
3use gpui::{
4 elements::*, impl_actions, platform::CursorStyle, platform::MouseButton, AnyViewHandle,
5 AppContext, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext,
6};
7use serde::Deserialize;
8use settings::Settings;
9use std::rc::Rc;
10
11pub trait Panel: View {
12 fn position(&self, cx: &WindowContext) -> DockPosition;
13 fn position_is_valid(&self, position: DockPosition) -> bool;
14 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);
15 fn icon_path(&self) -> &'static str;
16 fn icon_tooltip(&self) -> String;
17 fn icon_label(&self, _: &AppContext) -> Option<String> {
18 None
19 }
20 fn should_change_position_on_event(_: &Self::Event) -> bool;
21 fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool;
22 fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool;
23}
24
25pub trait PanelHandle {
26 fn id(&self) -> usize;
27 fn position(&self, cx: &WindowContext) -> DockPosition;
28 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
29 fn set_position(&self, position: DockPosition, cx: &mut WindowContext);
30 fn icon_path(&self, cx: &WindowContext) -> &'static str;
31 fn icon_tooltip(&self, cx: &WindowContext) -> String;
32 fn icon_label(&self, cx: &WindowContext) -> Option<String>;
33 fn is_focused(&self, cx: &WindowContext) -> bool;
34 fn as_any(&self) -> &AnyViewHandle;
35}
36
37impl<T> PanelHandle for ViewHandle<T>
38where
39 T: Panel,
40{
41 fn id(&self) -> usize {
42 self.id()
43 }
44
45 fn position(&self, cx: &WindowContext) -> DockPosition {
46 self.read(cx).position(cx)
47 }
48
49 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {
50 self.read(cx).position_is_valid(position)
51 }
52
53 fn set_position(&self, position: DockPosition, cx: &mut WindowContext) {
54 self.update(cx, |this, cx| this.set_position(position, cx))
55 }
56
57 fn icon_path(&self, cx: &WindowContext) -> &'static str {
58 self.read(cx).icon_path()
59 }
60
61 fn icon_tooltip(&self, cx: &WindowContext) -> String {
62 self.read(cx).icon_tooltip()
63 }
64
65 fn icon_label(&self, cx: &WindowContext) -> Option<String> {
66 self.read(cx).icon_label(cx)
67 }
68
69 fn is_focused(&self, cx: &WindowContext) -> bool {
70 ViewHandle::is_focused(self, cx)
71 }
72
73 fn as_any(&self) -> &AnyViewHandle {
74 self
75 }
76}
77
78impl From<&dyn PanelHandle> for AnyViewHandle {
79 fn from(val: &dyn PanelHandle) -> Self {
80 val.as_any().clone()
81 }
82}
83
84pub struct Dock {
85 position: DockPosition,
86 panels: Vec<PanelEntry>,
87 is_open: bool,
88 active_item_ix: usize,
89}
90
91#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
92pub enum DockPosition {
93 Left,
94 Bottom,
95 Right,
96}
97
98impl DockPosition {
99 fn to_label(&self) -> &'static str {
100 match self {
101 Self::Left => "left",
102 Self::Bottom => "bottom",
103 Self::Right => "right",
104 }
105 }
106
107 fn to_resizable_side(self) -> Side {
108 match self {
109 Self::Left => Side::Right,
110 Self::Bottom => Side::Top,
111 Self::Right => Side::Left,
112 }
113 }
114}
115
116struct PanelEntry {
117 panel: Rc<dyn PanelHandle>,
118 context_menu: ViewHandle<ContextMenu>,
119 _subscriptions: [Subscription; 2],
120}
121
122pub struct PanelButtons {
123 dock: ViewHandle<Dock>,
124 workspace: WeakViewHandle<Workspace>,
125}
126
127#[derive(Clone, Debug, Deserialize, PartialEq)]
128pub struct TogglePanel {
129 pub dock_position: DockPosition,
130 pub item_index: usize,
131}
132
133impl_actions!(workspace, [TogglePanel]);
134
135impl Dock {
136 pub fn new(position: DockPosition) -> Self {
137 Self {
138 position,
139 panels: Default::default(),
140 active_item_ix: 0,
141 is_open: false,
142 }
143 }
144
145 pub fn is_open(&self) -> bool {
146 self.is_open
147 }
148
149 pub fn active_item_ix(&self) -> usize {
150 self.active_item_ix
151 }
152
153 pub fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
154 if open != self.is_open {
155 self.is_open = open;
156 cx.notify();
157 }
158 }
159
160 pub fn toggle_open(&mut self, cx: &mut ViewContext<Self>) {
161 self.is_open = !self.is_open;
162 cx.notify();
163 }
164
165 pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) {
166 let subscriptions = [
167 cx.observe(&panel, |_, _, cx| cx.notify()),
168 cx.subscribe(&panel, |this, view, event, cx| {
169 if view.read(cx).should_activate_on_event(event, cx) {
170 if let Some(ix) = this
171 .panels
172 .iter()
173 .position(|item| item.panel.id() == view.id())
174 {
175 this.activate_item(ix, cx);
176 }
177 } else if view.read(cx).should_close_on_event(event, cx) {
178 this.set_open(false, cx);
179 }
180 }),
181 ];
182
183 let dock_view_id = cx.view_id();
184 self.panels.push(PanelEntry {
185 panel: Rc::new(panel),
186 context_menu: cx.add_view(|cx| {
187 let mut menu = ContextMenu::new(dock_view_id, cx);
188 menu.set_position_mode(OverlayPositionMode::Local);
189 menu
190 }),
191 _subscriptions: subscriptions,
192 });
193 cx.notify()
194 }
195
196 pub fn remove_panel<T: Panel>(&mut self, panel: &ViewHandle<T>, cx: &mut ViewContext<Self>) {
197 if let Some(panel_ix) = self
198 .panels
199 .iter()
200 .position(|item| item.panel.id() == panel.id())
201 {
202 if panel_ix == self.active_item_ix {
203 self.active_item_ix = 0;
204 self.set_open(false, cx);
205 }
206 self.panels.remove(panel_ix);
207 cx.notify();
208 }
209 }
210
211 pub fn panels_len(&self) -> usize {
212 self.panels.len()
213 }
214
215 pub fn activate_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
216 self.active_item_ix = item_ix;
217 cx.notify();
218 }
219
220 pub fn toggle_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
221 if self.active_item_ix == item_ix {
222 self.is_open = false;
223 } else {
224 self.active_item_ix = item_ix;
225 }
226 cx.notify();
227 }
228
229 pub fn active_item(&self) -> Option<&Rc<dyn PanelHandle>> {
230 if self.is_open {
231 self.panels.get(self.active_item_ix).map(|item| &item.panel)
232 } else {
233 None
234 }
235 }
236}
237
238impl Entity for Dock {
239 type Event = ();
240}
241
242impl View for Dock {
243 fn ui_name() -> &'static str {
244 "Dock"
245 }
246
247 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
248 if let Some(active_item) = self.active_item() {
249 enum ResizeHandleTag {}
250 let style = &cx.global::<Settings>().theme.workspace.dock;
251 ChildView::new(active_item.as_any(), cx)
252 .contained()
253 .with_style(style.container)
254 .with_resize_handle::<ResizeHandleTag>(
255 self.position as usize,
256 self.position.to_resizable_side(),
257 4.,
258 style.initial_size,
259 cx,
260 )
261 .into_any()
262 } else {
263 Empty::new().into_any()
264 }
265 }
266}
267
268impl PanelButtons {
269 pub fn new(
270 dock: ViewHandle<Dock>,
271 workspace: WeakViewHandle<Workspace>,
272 cx: &mut ViewContext<Self>,
273 ) -> Self {
274 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
275 Self { dock, workspace }
276 }
277}
278
279impl Entity for PanelButtons {
280 type Event = ();
281}
282
283impl View for PanelButtons {
284 fn ui_name() -> &'static str {
285 "PanelButtons"
286 }
287
288 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
289 let theme = &cx.global::<Settings>().theme;
290 let tooltip_style = theme.tooltip.clone();
291 let theme = &theme.workspace.status_bar.panel_buttons;
292 let item_style = theme.button.clone();
293 let dock = self.dock.read(cx);
294 let active_ix = dock.active_item_ix;
295 let is_open = dock.is_open;
296 let dock_position = dock.position;
297 let group_style = match dock_position {
298 DockPosition::Left => theme.group_left,
299 DockPosition::Bottom => theme.group_bottom,
300 DockPosition::Right => theme.group_right,
301 };
302 let menu_corner = match dock_position {
303 DockPosition::Left => AnchorCorner::BottomLeft,
304 DockPosition::Bottom | DockPosition::Right => AnchorCorner::BottomRight,
305 };
306
307 let items = dock
308 .panels
309 .iter()
310 .map(|item| (item.panel.clone(), item.context_menu.clone()))
311 .collect::<Vec<_>>();
312 Flex::row()
313 .with_children(
314 items
315 .into_iter()
316 .enumerate()
317 .map(|(ix, (view, context_menu))| {
318 let action = TogglePanel {
319 dock_position,
320 item_index: ix,
321 };
322
323 Stack::new()
324 .with_child(
325 MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| {
326 let is_active = is_open && ix == active_ix;
327 let style = item_style.style_for(state, is_active);
328 Flex::row()
329 .with_child(
330 Svg::new(view.icon_path(cx))
331 .with_color(style.icon_color)
332 .constrained()
333 .with_width(style.icon_size)
334 .aligned(),
335 )
336 .with_children(if let Some(label) = view.icon_label(cx) {
337 Some(
338 Label::new(label, style.label.text.clone())
339 .contained()
340 .with_style(style.label.container)
341 .aligned(),
342 )
343 } else {
344 None
345 })
346 .constrained()
347 .with_height(style.icon_size)
348 .contained()
349 .with_style(style.container)
350 })
351 .with_cursor_style(CursorStyle::PointingHand)
352 .on_click(MouseButton::Left, {
353 let action = action.clone();
354 move |_, this, cx| {
355 if let Some(workspace) = this.workspace.upgrade(cx) {
356 let action = action.clone();
357 cx.window_context().defer(move |cx| {
358 workspace.update(cx, |workspace, cx| {
359 workspace.toggle_panel(&action, cx)
360 });
361 });
362 }
363 }
364 })
365 .on_click(MouseButton::Right, {
366 let view = view.clone();
367 let menu = context_menu.clone();
368 move |_, _, cx| {
369 const POSITIONS: [DockPosition; 3] = [
370 DockPosition::Left,
371 DockPosition::Right,
372 DockPosition::Bottom,
373 ];
374
375 menu.update(cx, |menu, cx| {
376 let items = POSITIONS
377 .into_iter()
378 .filter(|position| {
379 *position != dock_position
380 && view.position_is_valid(*position, cx)
381 })
382 .map(|position| {
383 let view = view.clone();
384 ContextMenuItem::handler(
385 format!("Dock {}", position.to_label()),
386 move |cx| view.set_position(position, cx),
387 )
388 })
389 .collect();
390 menu.show(Default::default(), menu_corner, items, cx);
391 })
392 }
393 })
394 .with_tooltip::<Self>(
395 ix,
396 view.icon_tooltip(cx),
397 Some(Box::new(action)),
398 tooltip_style.clone(),
399 cx,
400 ),
401 )
402 .with_child(ChildView::new(&context_menu, cx))
403 }),
404 )
405 .contained()
406 .with_style(group_style)
407 .into_any()
408 }
409}
410
411impl StatusItemView for PanelButtons {
412 fn set_active_pane_item(
413 &mut self,
414 _: Option<&dyn crate::ItemHandle>,
415 _: &mut ViewContext<Self>,
416 ) {
417 }
418}