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