1use crate::{StatusItemView, Workspace};
2use context_menu::{ContextMenu, ContextMenuItem};
3use gpui::{
4 elements::*, impl_actions, platform::CursorStyle, platform::MouseButton, AnyViewHandle, Axis,
5 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 default_size(&self, cx: &WindowContext) -> f32;
16 fn icon_path(&self) -> &'static str;
17 fn icon_tooltip(&self) -> String;
18 fn icon_label(&self, _: &WindowContext) -> Option<String> {
19 None
20 }
21 fn should_change_position_on_event(_: &Self::Event) -> bool;
22 fn should_zoom_in_on_event(_: &Self::Event) -> bool;
23 fn should_zoom_out_on_event(_: &Self::Event) -> bool;
24 fn set_zoomed(&mut self, zoomed: bool, cx: &mut ViewContext<Self>);
25 fn should_activate_on_event(_: &Self::Event) -> bool;
26 fn should_close_on_event(_: &Self::Event) -> bool;
27 fn has_focus(&self, cx: &WindowContext) -> bool;
28 fn is_focus_event(_: &Self::Event) -> bool;
29}
30
31pub trait PanelHandle {
32 fn id(&self) -> usize;
33 fn position(&self, cx: &WindowContext) -> DockPosition;
34 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
35 fn set_position(&self, position: DockPosition, cx: &mut WindowContext);
36 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
37 fn default_size(&self, cx: &WindowContext) -> f32;
38 fn icon_path(&self, cx: &WindowContext) -> &'static str;
39 fn icon_tooltip(&self, cx: &WindowContext) -> String;
40 fn icon_label(&self, cx: &WindowContext) -> Option<String>;
41 fn has_focus(&self, cx: &WindowContext) -> bool;
42 fn as_any(&self) -> &AnyViewHandle;
43}
44
45impl<T> PanelHandle for ViewHandle<T>
46where
47 T: Panel,
48{
49 fn id(&self) -> usize {
50 self.id()
51 }
52
53 fn position(&self, cx: &WindowContext) -> DockPosition {
54 self.read(cx).position(cx)
55 }
56
57 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {
58 self.read(cx).position_is_valid(position)
59 }
60
61 fn set_position(&self, position: DockPosition, cx: &mut WindowContext) {
62 self.update(cx, |this, cx| this.set_position(position, cx))
63 }
64
65 fn default_size(&self, cx: &WindowContext) -> f32 {
66 self.read(cx).default_size(cx)
67 }
68
69 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext) {
70 self.update(cx, |this, cx| this.set_zoomed(zoomed, cx))
71 }
72
73 fn icon_path(&self, cx: &WindowContext) -> &'static str {
74 self.read(cx).icon_path()
75 }
76
77 fn icon_tooltip(&self, cx: &WindowContext) -> String {
78 self.read(cx).icon_tooltip()
79 }
80
81 fn icon_label(&self, cx: &WindowContext) -> Option<String> {
82 self.read(cx).icon_label(cx)
83 }
84
85 fn has_focus(&self, cx: &WindowContext) -> bool {
86 self.read(cx).has_focus(cx)
87 }
88
89 fn as_any(&self) -> &AnyViewHandle {
90 self
91 }
92}
93
94impl From<&dyn PanelHandle> for AnyViewHandle {
95 fn from(val: &dyn PanelHandle) -> Self {
96 val.as_any().clone()
97 }
98}
99
100pub struct Dock {
101 position: DockPosition,
102 panel_entries: Vec<PanelEntry>,
103 is_open: bool,
104 active_panel_index: usize,
105}
106
107#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
108pub enum DockPosition {
109 Left,
110 Bottom,
111 Right,
112}
113
114impl DockPosition {
115 fn to_label(&self) -> &'static str {
116 match self {
117 Self::Left => "left",
118 Self::Bottom => "bottom",
119 Self::Right => "right",
120 }
121 }
122
123 fn to_resize_handle_side(self) -> HandleSide {
124 match self {
125 Self::Left => HandleSide::Right,
126 Self::Bottom => HandleSide::Top,
127 Self::Right => HandleSide::Left,
128 }
129 }
130
131 pub fn axis(&self) -> Axis {
132 match self {
133 Self::Left | Self::Right => Axis::Horizontal,
134 Self::Bottom => Axis::Vertical,
135 }
136 }
137}
138
139struct PanelEntry {
140 panel: Rc<dyn PanelHandle>,
141 size: f32,
142 context_menu: ViewHandle<ContextMenu>,
143 zoomed: bool,
144 _subscriptions: [Subscription; 2],
145}
146
147pub struct PanelButtons {
148 dock: ViewHandle<Dock>,
149 workspace: WeakViewHandle<Workspace>,
150}
151
152#[derive(Clone, Debug, Deserialize, PartialEq)]
153pub struct TogglePanel {
154 pub dock_position: DockPosition,
155 pub panel_index: usize,
156}
157
158impl_actions!(workspace, [TogglePanel]);
159
160impl Dock {
161 pub fn new(position: DockPosition) -> Self {
162 Self {
163 position,
164 panel_entries: Default::default(),
165 active_panel_index: 0,
166 is_open: false,
167 }
168 }
169
170 pub fn is_open(&self) -> bool {
171 self.is_open
172 }
173
174 pub fn has_focus(&self, cx: &WindowContext) -> bool {
175 self.active_panel()
176 .map_or(false, |panel| panel.has_focus(cx))
177 }
178
179 pub fn panel_index<T: Panel>(&self) -> Option<usize> {
180 self.panel_entries
181 .iter()
182 .position(|entry| entry.panel.as_any().is::<T>())
183 }
184
185 pub fn active_panel_index(&self) -> usize {
186 self.active_panel_index
187 }
188
189 pub fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
190 if open != self.is_open {
191 self.is_open = open;
192 cx.notify();
193 }
194 }
195
196 pub fn toggle_open(&mut self, cx: &mut ViewContext<Self>) {
197 self.is_open = !self.is_open;
198 cx.notify();
199 }
200
201 pub fn set_panel_zoomed(
202 &mut self,
203 panel: &AnyViewHandle,
204 zoomed: bool,
205 cx: &mut ViewContext<Self>,
206 ) {
207 for entry in &mut self.panel_entries {
208 if entry.panel.as_any() == panel {
209 if zoomed != entry.zoomed {
210 entry.zoomed = zoomed;
211 entry.panel.set_zoomed(zoomed, cx);
212 }
213 } else if entry.zoomed {
214 entry.zoomed = false;
215 entry.panel.set_zoomed(false, cx);
216 }
217 }
218
219 cx.notify();
220 }
221
222 pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
223 for entry in &mut self.panel_entries {
224 if entry.zoomed {
225 entry.zoomed = false;
226 entry.panel.set_zoomed(false, cx);
227 }
228 }
229 }
230
231 pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) {
232 let subscriptions = [
233 cx.observe(&panel, |_, _, cx| cx.notify()),
234 cx.subscribe(&panel, |this, panel, event, cx| {
235 if T::should_activate_on_event(event) {
236 if let Some(ix) = this
237 .panel_entries
238 .iter()
239 .position(|entry| entry.panel.id() == panel.id())
240 {
241 this.set_open(true, cx);
242 this.activate_panel(ix, cx);
243 cx.focus(&panel);
244 }
245 } else if T::should_close_on_event(event)
246 && this.active_panel().map_or(false, |p| p.id() == panel.id())
247 {
248 this.set_open(false, cx);
249 }
250 }),
251 ];
252
253 let dock_view_id = cx.view_id();
254 let size = panel.default_size(cx);
255 self.panel_entries.push(PanelEntry {
256 panel: Rc::new(panel),
257 size,
258 zoomed: false,
259 context_menu: cx.add_view(|cx| {
260 let mut menu = ContextMenu::new(dock_view_id, cx);
261 menu.set_position_mode(OverlayPositionMode::Local);
262 menu
263 }),
264 _subscriptions: subscriptions,
265 });
266 cx.notify()
267 }
268
269 pub fn remove_panel<T: Panel>(&mut self, panel: &ViewHandle<T>, cx: &mut ViewContext<Self>) {
270 if let Some(panel_ix) = self
271 .panel_entries
272 .iter()
273 .position(|entry| entry.panel.id() == panel.id())
274 {
275 if panel_ix == self.active_panel_index {
276 self.active_panel_index = 0;
277 self.set_open(false, cx);
278 } else if panel_ix < self.active_panel_index {
279 self.active_panel_index -= 1;
280 }
281 self.panel_entries.remove(panel_ix);
282 cx.notify();
283 }
284 }
285
286 pub fn panels_len(&self) -> usize {
287 self.panel_entries.len()
288 }
289
290 pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
291 self.active_panel_index = panel_ix;
292 cx.notify();
293 }
294
295 pub fn toggle_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
296 if self.active_panel_index == panel_ix {
297 self.is_open = false;
298 } else {
299 self.active_panel_index = panel_ix;
300 }
301 cx.notify();
302 }
303
304 pub fn active_panel(&self) -> Option<&Rc<dyn PanelHandle>> {
305 let entry = self.active_entry()?;
306 Some(&entry.panel)
307 }
308
309 fn active_entry(&self) -> Option<&PanelEntry> {
310 if self.is_open {
311 self.panel_entries.get(self.active_panel_index)
312 } else {
313 None
314 }
315 }
316
317 pub fn zoomed_panel(&self) -> Option<Rc<dyn PanelHandle>> {
318 let entry = self.active_entry()?;
319 if entry.zoomed {
320 Some(entry.panel.clone())
321 } else {
322 None
323 }
324 }
325
326 pub fn panel_size(&self, panel: &dyn PanelHandle) -> Option<f32> {
327 self.panel_entries
328 .iter()
329 .find(|entry| entry.panel.id() == panel.id())
330 .map(|entry| entry.size)
331 }
332
333 pub fn resize_panel(&mut self, panel: &dyn PanelHandle, size: f32) {
334 let entry = self
335 .panel_entries
336 .iter_mut()
337 .find(|entry| entry.panel.id() == panel.id());
338 if let Some(entry) = entry {
339 entry.size = size;
340 }
341 }
342
343 pub fn active_panel_size(&self) -> Option<f32> {
344 if self.is_open {
345 self.panel_entries
346 .get(self.active_panel_index)
347 .map(|entry| entry.size)
348 } else {
349 None
350 }
351 }
352
353 pub fn resize_active_panel(&mut self, size: f32, cx: &mut ViewContext<Self>) {
354 if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
355 entry.size = size;
356 cx.notify();
357 }
358 }
359
360 pub fn render_placeholder(&self, cx: &WindowContext) -> AnyElement<Workspace> {
361 if let Some(active_entry) = self.active_entry() {
362 let style = &cx.global::<Settings>().theme.workspace.dock;
363 Empty::new()
364 .into_any()
365 .contained()
366 .with_style(style.container)
367 .resizable(
368 self.position.to_resize_handle_side(),
369 active_entry.size,
370 |_, _, _| {},
371 )
372 .into_any()
373 } else {
374 Empty::new().into_any()
375 }
376 }
377}
378
379impl Entity for Dock {
380 type Event = ();
381}
382
383impl View for Dock {
384 fn ui_name() -> &'static str {
385 "Dock"
386 }
387
388 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
389 if let Some(active_entry) = self.active_entry() {
390 let style = &cx.global::<Settings>().theme.workspace.dock;
391 ChildView::new(active_entry.panel.as_any(), cx)
392 .contained()
393 .with_style(style.container)
394 .resizable(
395 self.position.to_resize_handle_side(),
396 active_entry.size,
397 |dock: &mut Self, size, cx| dock.resize_active_panel(size, cx),
398 )
399 .into_any()
400 } else {
401 Empty::new().into_any()
402 }
403 }
404}
405
406impl PanelButtons {
407 pub fn new(
408 dock: ViewHandle<Dock>,
409 workspace: WeakViewHandle<Workspace>,
410 cx: &mut ViewContext<Self>,
411 ) -> Self {
412 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
413 Self { dock, workspace }
414 }
415}
416
417impl Entity for PanelButtons {
418 type Event = ();
419}
420
421impl View for PanelButtons {
422 fn ui_name() -> &'static str {
423 "PanelButtons"
424 }
425
426 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
427 let theme = &cx.global::<Settings>().theme;
428 let tooltip_style = theme.tooltip.clone();
429 let theme = &theme.workspace.status_bar.panel_buttons;
430 let button_style = theme.button.clone();
431 let dock = self.dock.read(cx);
432 let active_ix = dock.active_panel_index;
433 let is_open = dock.is_open;
434 let dock_position = dock.position;
435 let group_style = match dock_position {
436 DockPosition::Left => theme.group_left,
437 DockPosition::Bottom => theme.group_bottom,
438 DockPosition::Right => theme.group_right,
439 };
440 let menu_corner = match dock_position {
441 DockPosition::Left => AnchorCorner::BottomLeft,
442 DockPosition::Bottom | DockPosition::Right => AnchorCorner::BottomRight,
443 };
444
445 let panels = dock
446 .panel_entries
447 .iter()
448 .map(|item| (item.panel.clone(), item.context_menu.clone()))
449 .collect::<Vec<_>>();
450 Flex::row()
451 .with_children(
452 panels
453 .into_iter()
454 .enumerate()
455 .map(|(ix, (view, context_menu))| {
456 let action = TogglePanel {
457 dock_position,
458 panel_index: ix,
459 };
460
461 Stack::new()
462 .with_child(
463 MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| {
464 let is_active = is_open && ix == active_ix;
465 let style = button_style.style_for(state, is_active);
466 Flex::row()
467 .with_child(
468 Svg::new(view.icon_path(cx))
469 .with_color(style.icon_color)
470 .constrained()
471 .with_width(style.icon_size)
472 .aligned(),
473 )
474 .with_children(if let Some(label) = view.icon_label(cx) {
475 Some(
476 Label::new(label, style.label.text.clone())
477 .contained()
478 .with_style(style.label.container)
479 .aligned(),
480 )
481 } else {
482 None
483 })
484 .constrained()
485 .with_height(style.icon_size)
486 .contained()
487 .with_style(style.container)
488 })
489 .with_cursor_style(CursorStyle::PointingHand)
490 .on_click(MouseButton::Left, {
491 let action = action.clone();
492 move |_, this, cx| {
493 if let Some(workspace) = this.workspace.upgrade(cx) {
494 let action = action.clone();
495 cx.window_context().defer(move |cx| {
496 workspace.update(cx, |workspace, cx| {
497 workspace.toggle_panel(&action, cx)
498 });
499 });
500 }
501 }
502 })
503 .on_click(MouseButton::Right, {
504 let view = view.clone();
505 let menu = context_menu.clone();
506 move |_, _, cx| {
507 const POSITIONS: [DockPosition; 3] = [
508 DockPosition::Left,
509 DockPosition::Right,
510 DockPosition::Bottom,
511 ];
512
513 menu.update(cx, |menu, cx| {
514 let items = POSITIONS
515 .into_iter()
516 .filter(|position| {
517 *position != dock_position
518 && view.position_is_valid(*position, cx)
519 })
520 .map(|position| {
521 let view = view.clone();
522 ContextMenuItem::handler(
523 format!("Dock {}", position.to_label()),
524 move |cx| view.set_position(position, cx),
525 )
526 })
527 .collect();
528 menu.show(Default::default(), menu_corner, items, cx);
529 })
530 }
531 })
532 .with_tooltip::<Self>(
533 ix,
534 view.icon_tooltip(cx),
535 Some(Box::new(action)),
536 tooltip_style.clone(),
537 cx,
538 ),
539 )
540 .with_child(ChildView::new(&context_menu, cx))
541 }),
542 )
543 .contained()
544 .with_style(group_style)
545 .into_any()
546 }
547}
548
549impl StatusItemView for PanelButtons {
550 fn set_active_pane_item(
551 &mut self,
552 _: Option<&dyn crate::ItemHandle>,
553 _: &mut ViewContext<Self>,
554 ) {
555 }
556}
557
558#[cfg(test)]
559pub(crate) mod test {
560 use super::*;
561 use gpui::Entity;
562
563 pub enum TestPanelEvent {
564 PositionChanged,
565 Activated,
566 Closed,
567 }
568
569 pub struct TestPanel {
570 pub position: DockPosition,
571 }
572
573 impl Entity for TestPanel {
574 type Event = TestPanelEvent;
575 }
576
577 impl View for TestPanel {
578 fn ui_name() -> &'static str {
579 "TestPanel"
580 }
581
582 fn render(&mut self, _: &mut ViewContext<'_, '_, Self>) -> AnyElement<Self> {
583 Empty::new().into_any()
584 }
585 }
586
587 impl Panel for TestPanel {
588 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
589 self.position
590 }
591
592 fn position_is_valid(&self, _: super::DockPosition) -> bool {
593 true
594 }
595
596 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
597 self.position = position;
598 cx.emit(TestPanelEvent::PositionChanged);
599 }
600
601 fn set_zoomed(&mut self, _zoomed: bool, _cx: &mut ViewContext<Self>) {
602 unimplemented!()
603 }
604
605 fn default_size(&self, _: &WindowContext) -> f32 {
606 match self.position.axis() {
607 Axis::Horizontal => 300.,
608 Axis::Vertical => 200.,
609 }
610 }
611
612 fn icon_path(&self) -> &'static str {
613 "icons/test_panel.svg"
614 }
615
616 fn icon_tooltip(&self) -> String {
617 "Test Panel".into()
618 }
619
620 fn should_change_position_on_event(event: &Self::Event) -> bool {
621 matches!(event, TestPanelEvent::PositionChanged)
622 }
623
624 fn should_zoom_in_on_event(_: &Self::Event) -> bool {
625 false
626 }
627
628 fn should_zoom_out_on_event(_: &Self::Event) -> bool {
629 false
630 }
631
632 fn should_activate_on_event(event: &Self::Event) -> bool {
633 matches!(event, TestPanelEvent::Activated)
634 }
635
636 fn should_close_on_event(event: &Self::Event) -> bool {
637 matches!(event, TestPanelEvent::Closed)
638 }
639
640 fn has_focus(&self, _cx: &WindowContext) -> bool {
641 unimplemented!()
642 }
643
644 fn is_focus_event(_: &Self::Event) -> bool {
645 unimplemented!()
646 }
647 }
648}