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_index_for_type<T: Panel>(&self) -> Option<usize> {
192 self.panel_entries
193 .iter()
194 .position(|entry| entry.panel.as_any().is::<T>())
195 }
196
197 pub fn panel_index_for_ui_name(&self, ui_name: &str, cx: &AppContext) -> Option<usize> {
198 self.panel_entries.iter().position(|entry| {
199 let panel = entry.panel.as_any();
200 cx.view_ui_name(panel.window_id(), panel.id()) == Some(ui_name)
201 })
202 }
203
204 pub fn active_panel_index(&self) -> usize {
205 self.active_panel_index
206 }
207
208 pub(crate) fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
209 if open != self.is_open {
210 self.is_open = open;
211 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
212 active_panel.panel.set_active(open, cx);
213 }
214
215 cx.notify();
216 }
217 }
218
219 pub fn set_panel_zoomed(
220 &mut self,
221 panel: &AnyViewHandle,
222 zoomed: bool,
223 cx: &mut ViewContext<Self>,
224 ) {
225 for entry in &mut self.panel_entries {
226 if entry.panel.as_any() == panel {
227 if zoomed != entry.panel.is_zoomed(cx) {
228 entry.panel.set_zoomed(zoomed, cx);
229 }
230 } else if entry.panel.is_zoomed(cx) {
231 entry.panel.set_zoomed(false, cx);
232 }
233 }
234
235 cx.notify();
236 }
237
238 pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
239 for entry in &mut self.panel_entries {
240 if entry.panel.is_zoomed(cx) {
241 entry.panel.set_zoomed(false, cx);
242 }
243 }
244 }
245
246 pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) {
247 let subscriptions = [
248 cx.observe(&panel, |_, _, cx| cx.notify()),
249 cx.subscribe(&panel, |this, panel, event, cx| {
250 if T::should_activate_on_event(event) {
251 if let Some(ix) = this
252 .panel_entries
253 .iter()
254 .position(|entry| entry.panel.id() == panel.id())
255 {
256 this.set_open(true, cx);
257 this.activate_panel(ix, cx);
258 cx.focus(&panel);
259 }
260 } else if T::should_close_on_event(event)
261 && this.visible_panel().map_or(false, |p| p.id() == panel.id())
262 {
263 this.set_open(false, cx);
264 }
265 }),
266 ];
267
268 let dock_view_id = cx.view_id();
269 self.panel_entries.push(PanelEntry {
270 panel: Rc::new(panel),
271 context_menu: cx.add_view(|cx| {
272 let mut menu = ContextMenu::new(dock_view_id, cx);
273 menu.set_position_mode(OverlayPositionMode::Local);
274 menu
275 }),
276 _subscriptions: subscriptions,
277 });
278 cx.notify()
279 }
280
281 pub fn remove_panel<T: Panel>(&mut self, panel: &ViewHandle<T>, cx: &mut ViewContext<Self>) {
282 if let Some(panel_ix) = self
283 .panel_entries
284 .iter()
285 .position(|entry| entry.panel.id() == panel.id())
286 {
287 if panel_ix == self.active_panel_index {
288 self.active_panel_index = 0;
289 self.set_open(false, cx);
290 } else if panel_ix < self.active_panel_index {
291 self.active_panel_index -= 1;
292 }
293 self.panel_entries.remove(panel_ix);
294 cx.notify();
295 }
296 }
297
298 pub fn panels_len(&self) -> usize {
299 self.panel_entries.len()
300 }
301
302 pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
303 if panel_ix != self.active_panel_index {
304 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
305 active_panel.panel.set_active(false, cx);
306 }
307
308 self.active_panel_index = panel_ix;
309 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
310 active_panel.panel.set_active(true, cx);
311 }
312
313 cx.notify();
314 }
315 }
316
317 pub fn visible_panel(&self) -> Option<&Rc<dyn PanelHandle>> {
318 let entry = self.visible_entry()?;
319 Some(&entry.panel)
320 }
321
322 pub fn active_panel(&self) -> Option<&Rc<dyn PanelHandle>> {
323 Some(&self.panel_entries.get(self.active_panel_index)?.panel)
324 }
325
326 fn visible_entry(&self) -> Option<&PanelEntry> {
327 if self.is_open {
328 self.panel_entries.get(self.active_panel_index)
329 } else {
330 None
331 }
332 }
333
334 pub fn zoomed_panel(&self, cx: &WindowContext) -> Option<Rc<dyn PanelHandle>> {
335 let entry = self.visible_entry()?;
336 if entry.panel.is_zoomed(cx) {
337 Some(entry.panel.clone())
338 } else {
339 None
340 }
341 }
342
343 pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> {
344 self.panel_entries
345 .iter()
346 .find(|entry| entry.panel.id() == panel.id())
347 .map(|entry| entry.panel.size(cx))
348 }
349
350 pub fn active_panel_size(&self, cx: &WindowContext) -> Option<f32> {
351 if self.is_open {
352 self.panel_entries
353 .get(self.active_panel_index)
354 .map(|entry| entry.panel.size(cx))
355 } else {
356 None
357 }
358 }
359
360 pub fn resize_active_panel(&mut self, size: f32, cx: &mut ViewContext<Self>) {
361 if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
362 entry.panel.set_size(size, cx);
363 cx.notify();
364 }
365 }
366
367 pub fn render_placeholder(&self, cx: &WindowContext) -> AnyElement<Workspace> {
368 if let Some(active_entry) = self.visible_entry() {
369 Empty::new()
370 .into_any()
371 .contained()
372 .with_style(self.style(cx))
373 .resizable(
374 self.position.to_resize_handle_side(),
375 active_entry.panel.size(cx),
376 |_, _, _| {},
377 )
378 .into_any()
379 } else {
380 Empty::new().into_any()
381 }
382 }
383
384 fn style(&self, cx: &WindowContext) -> ContainerStyle {
385 let theme = &settings::get::<ThemeSettings>(cx).theme;
386 let style = match self.position {
387 DockPosition::Left => theme.workspace.dock.left,
388 DockPosition::Bottom => theme.workspace.dock.bottom,
389 DockPosition::Right => theme.workspace.dock.right,
390 };
391 style
392 }
393}
394
395impl Entity for Dock {
396 type Event = ();
397}
398
399impl View for Dock {
400 fn ui_name() -> &'static str {
401 "Dock"
402 }
403
404 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
405 if let Some(active_entry) = self.visible_entry() {
406 let style = self.style(cx);
407 ChildView::new(active_entry.panel.as_any(), cx)
408 .contained()
409 .with_style(style)
410 .resizable(
411 self.position.to_resize_handle_side(),
412 active_entry.panel.size(cx),
413 |dock: &mut Self, size, cx| dock.resize_active_panel(size, cx),
414 )
415 .into_any()
416 } else {
417 Empty::new().into_any()
418 }
419 }
420
421 fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
422 if cx.is_self_focused() {
423 if let Some(active_entry) = self.visible_entry() {
424 cx.focus(active_entry.panel.as_any());
425 } else {
426 cx.focus_parent();
427 }
428 }
429 }
430}
431
432impl PanelButtons {
433 pub fn new(
434 dock: ViewHandle<Dock>,
435 workspace: WeakViewHandle<Workspace>,
436 cx: &mut ViewContext<Self>,
437 ) -> Self {
438 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
439 Self { dock, workspace }
440 }
441}
442
443impl Entity for PanelButtons {
444 type Event = ();
445}
446
447impl View for PanelButtons {
448 fn ui_name() -> &'static str {
449 "PanelButtons"
450 }
451
452 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
453 let theme = &settings::get::<ThemeSettings>(cx).theme;
454 let tooltip_style = theme.tooltip.clone();
455 let theme = &theme.workspace.status_bar.panel_buttons;
456 let button_style = theme.button.clone();
457 let dock = self.dock.read(cx);
458 let active_ix = dock.active_panel_index;
459 let is_open = dock.is_open;
460 let dock_position = dock.position;
461 let group_style = match dock_position {
462 DockPosition::Left => theme.group_left,
463 DockPosition::Bottom => theme.group_bottom,
464 DockPosition::Right => theme.group_right,
465 };
466 let menu_corner = match dock_position {
467 DockPosition::Left => AnchorCorner::BottomLeft,
468 DockPosition::Bottom | DockPosition::Right => AnchorCorner::BottomRight,
469 };
470
471 let panels = dock
472 .panel_entries
473 .iter()
474 .map(|item| (item.panel.clone(), item.context_menu.clone()))
475 .collect::<Vec<_>>();
476 Flex::row()
477 .with_children(panels.into_iter().enumerate().map(
478 |(panel_ix, (view, context_menu))| {
479 let is_active = is_open && panel_ix == active_ix;
480 let (tooltip, tooltip_action) = if is_active {
481 (
482 format!("Close {} dock", dock_position.to_label()),
483 Some(match dock_position {
484 DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
485 DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
486 DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
487 }),
488 )
489 } else {
490 view.icon_tooltip(cx)
491 };
492 Stack::new()
493 .with_child(
494 MouseEventHandler::<Self, _>::new(panel_ix, cx, |state, cx| {
495 let style = button_style.style_for(state, is_active);
496 Flex::row()
497 .with_child(
498 Svg::new(view.icon_path(cx))
499 .with_color(style.icon_color)
500 .constrained()
501 .with_width(style.icon_size)
502 .aligned(),
503 )
504 .with_children(if let Some(label) = view.icon_label(cx) {
505 Some(
506 Label::new(label, style.label.text.clone())
507 .contained()
508 .with_style(style.label.container)
509 .aligned(),
510 )
511 } else {
512 None
513 })
514 .constrained()
515 .with_height(style.icon_size)
516 .contained()
517 .with_style(style.container)
518 })
519 .with_cursor_style(CursorStyle::PointingHand)
520 .on_click(MouseButton::Left, {
521 let tooltip_action =
522 tooltip_action.as_ref().map(|action| action.boxed_clone());
523 move |_, this, cx| {
524 if let Some(tooltip_action) = &tooltip_action {
525 let window_id = cx.window_id();
526 let view_id = this.workspace.id();
527 let tooltip_action = tooltip_action.boxed_clone();
528 cx.spawn(|_, mut cx| async move {
529 cx.dispatch_action(
530 window_id,
531 view_id,
532 &*tooltip_action,
533 )
534 .ok();
535 })
536 .detach();
537 }
538 }
539 })
540 .on_click(MouseButton::Right, {
541 let view = view.clone();
542 let menu = context_menu.clone();
543 move |_, _, cx| {
544 const POSITIONS: [DockPosition; 3] = [
545 DockPosition::Left,
546 DockPosition::Right,
547 DockPosition::Bottom,
548 ];
549
550 menu.update(cx, |menu, cx| {
551 let items = POSITIONS
552 .into_iter()
553 .filter(|position| {
554 *position != dock_position
555 && view.position_is_valid(*position, cx)
556 })
557 .map(|position| {
558 let view = view.clone();
559 ContextMenuItem::handler(
560 format!("Dock {}", position.to_label()),
561 move |cx| view.set_position(position, cx),
562 )
563 })
564 .collect();
565 menu.show(Default::default(), menu_corner, items, cx);
566 })
567 }
568 })
569 .with_tooltip::<Self>(
570 panel_ix,
571 tooltip,
572 tooltip_action,
573 tooltip_style.clone(),
574 cx,
575 ),
576 )
577 .with_child(ChildView::new(&context_menu, cx))
578 },
579 ))
580 .contained()
581 .with_style(group_style)
582 .into_any()
583 }
584}
585
586impl StatusItemView for PanelButtons {
587 fn set_active_pane_item(
588 &mut self,
589 _: Option<&dyn crate::ItemHandle>,
590 _: &mut ViewContext<Self>,
591 ) {
592 }
593}
594
595#[cfg(test)]
596pub(crate) mod test {
597 use super::*;
598 use gpui::{ViewContext, WindowContext};
599
600 pub enum TestPanelEvent {
601 PositionChanged,
602 Activated,
603 Closed,
604 ZoomIn,
605 ZoomOut,
606 Focus,
607 }
608
609 pub struct TestPanel {
610 pub position: DockPosition,
611 pub zoomed: bool,
612 pub active: bool,
613 pub has_focus: bool,
614 pub size: f32,
615 }
616
617 impl TestPanel {
618 pub fn new(position: DockPosition) -> Self {
619 Self {
620 position,
621 zoomed: false,
622 active: false,
623 has_focus: false,
624 size: 300.,
625 }
626 }
627 }
628
629 impl Entity for TestPanel {
630 type Event = TestPanelEvent;
631 }
632
633 impl View for TestPanel {
634 fn ui_name() -> &'static str {
635 "TestPanel"
636 }
637
638 fn render(&mut self, _: &mut ViewContext<'_, '_, Self>) -> AnyElement<Self> {
639 Empty::new().into_any()
640 }
641
642 fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
643 self.has_focus = true;
644 cx.emit(TestPanelEvent::Focus);
645 }
646
647 fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {
648 self.has_focus = false;
649 }
650 }
651
652 impl Panel for TestPanel {
653 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
654 self.position
655 }
656
657 fn position_is_valid(&self, _: super::DockPosition) -> bool {
658 true
659 }
660
661 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
662 self.position = position;
663 cx.emit(TestPanelEvent::PositionChanged);
664 }
665
666 fn is_zoomed(&self, _: &WindowContext) -> bool {
667 self.zoomed
668 }
669
670 fn set_zoomed(&mut self, zoomed: bool, _cx: &mut ViewContext<Self>) {
671 self.zoomed = zoomed;
672 }
673
674 fn set_active(&mut self, active: bool, _cx: &mut ViewContext<Self>) {
675 self.active = active;
676 }
677
678 fn size(&self, _: &WindowContext) -> f32 {
679 self.size
680 }
681
682 fn set_size(&mut self, size: f32, _: &mut ViewContext<Self>) {
683 self.size = size;
684 }
685
686 fn icon_path(&self) -> &'static str {
687 "icons/test_panel.svg"
688 }
689
690 fn icon_tooltip(&self) -> (String, Option<Box<dyn Action>>) {
691 ("Test Panel".into(), None)
692 }
693
694 fn should_change_position_on_event(event: &Self::Event) -> bool {
695 matches!(event, TestPanelEvent::PositionChanged)
696 }
697
698 fn should_zoom_in_on_event(event: &Self::Event) -> bool {
699 matches!(event, TestPanelEvent::ZoomIn)
700 }
701
702 fn should_zoom_out_on_event(event: &Self::Event) -> bool {
703 matches!(event, TestPanelEvent::ZoomOut)
704 }
705
706 fn should_activate_on_event(event: &Self::Event) -> bool {
707 matches!(event, TestPanelEvent::Activated)
708 }
709
710 fn should_close_on_event(event: &Self::Event) -> bool {
711 matches!(event, TestPanelEvent::Closed)
712 }
713
714 fn has_focus(&self, _cx: &WindowContext) -> bool {
715 self.has_focus
716 }
717
718 fn is_focus_event(event: &Self::Event) -> bool {
719 matches!(event, TestPanelEvent::Focus)
720 }
721 }
722}