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