1use crate::persistence::model::DockData;
2use crate::{status_bar::StatusItemView, Workspace};
3use crate::{DraggedDock, Event, Pane};
4use client::proto;
5use gpui::{
6 deferred, div, px, Action, AnyView, AppContext, Axis, Corner, Entity, EntityId, EventEmitter,
7 FocusHandle, FocusableView, IntoElement, KeyContext, MouseButton, MouseDownEvent, MouseUpEvent,
8 ParentElement, Render, SharedString, StyleRefinement, Styled, Subscription, View, ViewContext,
9 VisualContext, WeakView, WindowContext,
10};
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use settings::SettingsStore;
14use std::sync::Arc;
15use ui::{h_flex, ContextMenu, IconButton, Tooltip};
16use ui::{prelude::*, right_click_menu};
17
18pub(crate) const RESIZE_HANDLE_SIZE: Pixels = Pixels(6.);
19
20pub enum PanelEvent {
21 ZoomIn,
22 ZoomOut,
23 Activate,
24 Close,
25}
26
27pub use proto::PanelId;
28
29pub trait Panel: FocusableView + EventEmitter<PanelEvent> {
30 fn persistent_name() -> &'static str;
31 fn position(&self, cx: &WindowContext) -> DockPosition;
32 fn position_is_valid(&self, position: DockPosition) -> bool;
33 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);
34 fn size(&self, cx: &WindowContext) -> Pixels;
35 fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>);
36 fn icon(&self, cx: &WindowContext) -> Option<ui::IconName>;
37 fn icon_tooltip(&self, cx: &WindowContext) -> Option<&'static str>;
38 fn toggle_action(&self) -> Box<dyn Action>;
39 fn icon_label(&self, _: &WindowContext) -> Option<String> {
40 None
41 }
42 fn is_zoomed(&self, _cx: &WindowContext) -> bool {
43 false
44 }
45 fn starts_open(&self, _cx: &WindowContext) -> bool {
46 false
47 }
48 fn set_zoomed(&mut self, _zoomed: bool, _cx: &mut ViewContext<Self>) {}
49 fn set_active(&mut self, _active: bool, _cx: &mut ViewContext<Self>) {}
50 fn pane(&self) -> Option<View<Pane>> {
51 None
52 }
53 fn remote_id() -> Option<proto::PanelId> {
54 None
55 }
56}
57
58pub trait PanelHandle: Send + Sync {
59 fn panel_id(&self) -> EntityId;
60 fn persistent_name(&self) -> &'static str;
61 fn position(&self, cx: &WindowContext) -> DockPosition;
62 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
63 fn set_position(&self, position: DockPosition, cx: &mut WindowContext);
64 fn is_zoomed(&self, cx: &WindowContext) -> bool;
65 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
66 fn set_active(&self, active: bool, cx: &mut WindowContext);
67 fn remote_id(&self) -> Option<proto::PanelId>;
68 fn pane(&self, cx: &WindowContext) -> Option<View<Pane>>;
69 fn size(&self, cx: &WindowContext) -> Pixels;
70 fn set_size(&self, size: Option<Pixels>, cx: &mut WindowContext);
71 fn icon(&self, cx: &WindowContext) -> Option<ui::IconName>;
72 fn icon_tooltip(&self, cx: &WindowContext) -> Option<&'static str>;
73 fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action>;
74 fn icon_label(&self, cx: &WindowContext) -> Option<String>;
75 fn focus_handle(&self, cx: &AppContext) -> FocusHandle;
76 fn to_any(&self) -> AnyView;
77}
78
79impl<T> PanelHandle for View<T>
80where
81 T: Panel,
82{
83 fn panel_id(&self) -> EntityId {
84 Entity::entity_id(self)
85 }
86
87 fn persistent_name(&self) -> &'static str {
88 T::persistent_name()
89 }
90
91 fn position(&self, cx: &WindowContext) -> DockPosition {
92 self.read(cx).position(cx)
93 }
94
95 fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {
96 self.read(cx).position_is_valid(position)
97 }
98
99 fn set_position(&self, position: DockPosition, cx: &mut WindowContext) {
100 self.update(cx, |this, cx| this.set_position(position, cx))
101 }
102
103 fn is_zoomed(&self, cx: &WindowContext) -> bool {
104 self.read(cx).is_zoomed(cx)
105 }
106
107 fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext) {
108 self.update(cx, |this, cx| this.set_zoomed(zoomed, cx))
109 }
110
111 fn set_active(&self, active: bool, cx: &mut WindowContext) {
112 self.update(cx, |this, cx| this.set_active(active, cx))
113 }
114
115 fn pane(&self, cx: &WindowContext) -> Option<View<Pane>> {
116 self.read(cx).pane()
117 }
118
119 fn remote_id(&self) -> Option<PanelId> {
120 T::remote_id()
121 }
122
123 fn size(&self, cx: &WindowContext) -> Pixels {
124 self.read(cx).size(cx)
125 }
126
127 fn set_size(&self, size: Option<Pixels>, cx: &mut WindowContext) {
128 self.update(cx, |this, cx| this.set_size(size, cx))
129 }
130
131 fn icon(&self, cx: &WindowContext) -> Option<ui::IconName> {
132 self.read(cx).icon(cx)
133 }
134
135 fn icon_tooltip(&self, cx: &WindowContext) -> Option<&'static str> {
136 self.read(cx).icon_tooltip(cx)
137 }
138
139 fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action> {
140 self.read(cx).toggle_action()
141 }
142
143 fn icon_label(&self, cx: &WindowContext) -> Option<String> {
144 self.read(cx).icon_label(cx)
145 }
146
147 fn to_any(&self) -> AnyView {
148 self.clone().into()
149 }
150
151 fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
152 self.read(cx).focus_handle(cx).clone()
153 }
154}
155
156impl From<&dyn PanelHandle> for AnyView {
157 fn from(val: &dyn PanelHandle) -> Self {
158 val.to_any()
159 }
160}
161
162/// A container with a fixed [`DockPosition`] adjacent to a certain widown edge.
163/// Can contain multiple panels and show/hide itself with all contents.
164pub struct Dock {
165 position: DockPosition,
166 panel_entries: Vec<PanelEntry>,
167 is_open: bool,
168 active_panel_index: usize,
169 focus_handle: FocusHandle,
170 pub(crate) serialized_dock: Option<DockData>,
171 resizeable: bool,
172 _subscriptions: [Subscription; 2],
173}
174
175impl FocusableView for Dock {
176 fn focus_handle(&self, _: &AppContext) -> FocusHandle {
177 self.focus_handle.clone()
178 }
179}
180
181#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
182#[serde(rename_all = "lowercase")]
183pub enum DockPosition {
184 Left,
185 Bottom,
186 Right,
187}
188
189impl DockPosition {
190 fn label(&self) -> &'static str {
191 match self {
192 Self::Left => "left",
193 Self::Bottom => "bottom",
194 Self::Right => "right",
195 }
196 }
197
198 pub fn axis(&self) -> Axis {
199 match self {
200 Self::Left | Self::Right => Axis::Horizontal,
201 Self::Bottom => Axis::Vertical,
202 }
203 }
204}
205
206struct PanelEntry {
207 panel: Arc<dyn PanelHandle>,
208 _subscriptions: [Subscription; 3],
209}
210
211pub struct PanelButtons {
212 dock: View<Dock>,
213}
214
215impl Dock {
216 pub fn new(position: DockPosition, cx: &mut ViewContext<Workspace>) -> View<Self> {
217 let focus_handle = cx.focus_handle();
218 let workspace = cx.view().clone();
219 let dock = cx.new_view(|cx: &mut ViewContext<Self>| {
220 let focus_subscription = cx.on_focus(&focus_handle, |dock, cx| {
221 if let Some(active_entry) = dock.panel_entries.get(dock.active_panel_index) {
222 active_entry.panel.focus_handle(cx).focus(cx)
223 }
224 });
225 let zoom_subscription = cx.subscribe(&workspace, |dock, workspace, e: &Event, cx| {
226 if matches!(e, Event::ZoomChanged) {
227 let is_zoomed = workspace.read(cx).zoomed.is_some();
228 dock.resizeable = !is_zoomed;
229 }
230 });
231 Self {
232 position,
233 panel_entries: Default::default(),
234 active_panel_index: 0,
235 is_open: false,
236 focus_handle: focus_handle.clone(),
237 _subscriptions: [focus_subscription, zoom_subscription],
238 serialized_dock: None,
239 resizeable: true,
240 }
241 });
242
243 cx.on_focus_in(&focus_handle, {
244 let dock = dock.downgrade();
245 move |workspace, cx| {
246 let Some(dock) = dock.upgrade() else {
247 return;
248 };
249 let Some(panel) = dock.read(cx).active_panel() else {
250 return;
251 };
252 if panel.is_zoomed(cx) {
253 workspace.zoomed = Some(panel.to_any().downgrade());
254 workspace.zoomed_position = Some(position);
255 } else {
256 workspace.zoomed = None;
257 workspace.zoomed_position = None;
258 }
259 cx.emit(Event::ZoomChanged);
260 workspace.dismiss_zoomed_items_to_reveal(Some(position), cx);
261 workspace.update_active_view_for_followers(cx)
262 }
263 })
264 .detach();
265
266 cx.observe(&dock, move |workspace, dock, cx| {
267 if dock.read(cx).is_open() {
268 if let Some(panel) = dock.read(cx).active_panel() {
269 if panel.is_zoomed(cx) {
270 workspace.zoomed = Some(panel.to_any().downgrade());
271 workspace.zoomed_position = Some(position);
272 cx.emit(Event::ZoomChanged);
273 return;
274 }
275 }
276 }
277 if workspace.zoomed_position == Some(position) {
278 workspace.zoomed = None;
279 workspace.zoomed_position = None;
280 cx.emit(Event::ZoomChanged);
281 }
282 })
283 .detach();
284
285 dock
286 }
287
288 pub fn position(&self) -> DockPosition {
289 self.position
290 }
291
292 pub fn is_open(&self) -> bool {
293 self.is_open
294 }
295
296 pub fn panel<T: Panel>(&self) -> Option<View<T>> {
297 self.panel_entries
298 .iter()
299 .find_map(|entry| entry.panel.to_any().clone().downcast().ok())
300 }
301
302 pub fn panel_index_for_type<T: Panel>(&self) -> Option<usize> {
303 self.panel_entries
304 .iter()
305 .position(|entry| entry.panel.to_any().downcast::<T>().is_ok())
306 }
307
308 pub fn panel_index_for_persistent_name(
309 &self,
310 ui_name: &str,
311 _cx: &AppContext,
312 ) -> Option<usize> {
313 self.panel_entries
314 .iter()
315 .position(|entry| entry.panel.persistent_name() == ui_name)
316 }
317
318 pub fn panel_index_for_proto_id(&self, panel_id: PanelId) -> Option<usize> {
319 self.panel_entries
320 .iter()
321 .position(|entry| entry.panel.remote_id() == Some(panel_id))
322 }
323
324 pub fn active_panel_index(&self) -> usize {
325 self.active_panel_index
326 }
327
328 pub(crate) fn set_open(&mut self, open: bool, cx: &mut ViewContext<Self>) {
329 if open != self.is_open {
330 self.is_open = open;
331 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
332 active_panel.panel.set_active(open, cx);
333 }
334
335 cx.notify();
336 }
337 }
338
339 pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) {
340 for entry in &mut self.panel_entries {
341 if entry.panel.panel_id() == panel.entity_id() {
342 if zoomed != entry.panel.is_zoomed(cx) {
343 entry.panel.set_zoomed(zoomed, cx);
344 }
345 } else if entry.panel.is_zoomed(cx) {
346 entry.panel.set_zoomed(false, cx);
347 }
348 }
349
350 cx.notify();
351 }
352
353 pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
354 for entry in &mut self.panel_entries {
355 if entry.panel.is_zoomed(cx) {
356 entry.panel.set_zoomed(false, cx);
357 }
358 }
359 }
360
361 pub(crate) fn add_panel<T: Panel>(
362 &mut self,
363 panel: View<T>,
364 workspace: WeakView<Workspace>,
365 cx: &mut ViewContext<Self>,
366 ) {
367 let subscriptions = [
368 cx.observe(&panel, |_, _, cx| cx.notify()),
369 cx.observe_global::<SettingsStore>({
370 let workspace = workspace.clone();
371 let panel = panel.clone();
372
373 move |this, cx| {
374 let new_position = panel.read(cx).position(cx);
375 if new_position == this.position {
376 return;
377 }
378
379 let Ok(new_dock) = workspace.update(cx, |workspace, cx| {
380 if panel.is_zoomed(cx) {
381 workspace.zoomed_position = Some(new_position);
382 }
383 match new_position {
384 DockPosition::Left => &workspace.left_dock,
385 DockPosition::Bottom => &workspace.bottom_dock,
386 DockPosition::Right => &workspace.right_dock,
387 }
388 .clone()
389 }) else {
390 return;
391 };
392
393 let was_visible = this.is_open()
394 && this.visible_panel().map_or(false, |active_panel| {
395 active_panel.panel_id() == Entity::entity_id(&panel)
396 });
397
398 this.remove_panel(&panel, cx);
399
400 new_dock.update(cx, |new_dock, cx| {
401 new_dock.remove_panel(&panel, cx);
402 new_dock.add_panel(panel.clone(), workspace.clone(), cx);
403 if was_visible {
404 new_dock.set_open(true, cx);
405 new_dock.activate_panel(new_dock.panels_len() - 1, cx);
406 }
407 });
408 }
409 }),
410 cx.subscribe(&panel, move |this, panel, event, cx| match event {
411 PanelEvent::ZoomIn => {
412 this.set_panel_zoomed(&panel.to_any(), true, cx);
413 if !panel.focus_handle(cx).contains_focused(cx) {
414 cx.focus_view(&panel);
415 }
416 workspace
417 .update(cx, |workspace, cx| {
418 workspace.zoomed = Some(panel.downgrade().into());
419 workspace.zoomed_position = Some(panel.read(cx).position(cx));
420 cx.emit(Event::ZoomChanged);
421 })
422 .ok();
423 }
424 PanelEvent::ZoomOut => {
425 this.set_panel_zoomed(&panel.to_any(), false, cx);
426 workspace
427 .update(cx, |workspace, cx| {
428 if workspace.zoomed_position == Some(this.position) {
429 workspace.zoomed = None;
430 workspace.zoomed_position = None;
431 cx.emit(Event::ZoomChanged);
432 }
433 cx.notify();
434 })
435 .ok();
436 }
437 PanelEvent::Activate => {
438 if let Some(ix) = this
439 .panel_entries
440 .iter()
441 .position(|entry| entry.panel.panel_id() == Entity::entity_id(&panel))
442 {
443 this.set_open(true, cx);
444 this.activate_panel(ix, cx);
445 cx.focus_view(&panel);
446 }
447 }
448 PanelEvent::Close => {
449 if this
450 .visible_panel()
451 .map_or(false, |p| p.panel_id() == Entity::entity_id(&panel))
452 {
453 this.set_open(false, cx);
454 }
455 }
456 }),
457 ];
458
459 self.panel_entries.push(PanelEntry {
460 panel: Arc::new(panel.clone()),
461 _subscriptions: subscriptions,
462 });
463
464 if !self.restore_state(cx) && panel.read(cx).starts_open(cx) {
465 self.activate_panel(self.panel_entries.len() - 1, cx);
466 self.set_open(true, cx);
467 }
468
469 cx.notify()
470 }
471
472 pub fn restore_state(&mut self, cx: &mut ViewContext<Self>) -> bool {
473 if let Some(serialized) = self.serialized_dock.clone() {
474 if let Some(active_panel) = serialized.active_panel {
475 if let Some(idx) = self.panel_index_for_persistent_name(active_panel.as_str(), cx) {
476 self.activate_panel(idx, cx);
477 }
478 }
479
480 if serialized.zoom {
481 if let Some(panel) = self.active_panel() {
482 panel.set_zoomed(true, cx)
483 }
484 }
485 self.set_open(serialized.visible, cx);
486 return true;
487 }
488 false
489 }
490
491 pub fn remove_panel<T: Panel>(&mut self, panel: &View<T>, cx: &mut ViewContext<Self>) {
492 if let Some(panel_ix) = self
493 .panel_entries
494 .iter()
495 .position(|entry| entry.panel.panel_id() == Entity::entity_id(panel))
496 {
497 match panel_ix.cmp(&self.active_panel_index) {
498 std::cmp::Ordering::Less => {
499 self.active_panel_index -= 1;
500 }
501 std::cmp::Ordering::Equal => {
502 self.active_panel_index = 0;
503 self.set_open(false, cx);
504 }
505 std::cmp::Ordering::Greater => {}
506 }
507 self.panel_entries.remove(panel_ix);
508 cx.notify();
509 }
510 }
511
512 pub fn panels_len(&self) -> usize {
513 self.panel_entries.len()
514 }
515
516 pub fn activate_panel(&mut self, panel_ix: usize, cx: &mut ViewContext<Self>) {
517 if panel_ix != self.active_panel_index {
518 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
519 active_panel.panel.set_active(false, cx);
520 }
521
522 self.active_panel_index = panel_ix;
523 if let Some(active_panel) = self.panel_entries.get(self.active_panel_index) {
524 active_panel.panel.set_active(true, cx);
525 }
526
527 cx.notify();
528 }
529 }
530
531 pub fn visible_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
532 let entry = self.visible_entry()?;
533 Some(&entry.panel)
534 }
535
536 pub fn active_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
537 Some(&self.panel_entries.get(self.active_panel_index)?.panel)
538 }
539
540 fn visible_entry(&self) -> Option<&PanelEntry> {
541 if self.is_open {
542 self.panel_entries.get(self.active_panel_index)
543 } else {
544 None
545 }
546 }
547
548 pub fn zoomed_panel(&self, cx: &WindowContext) -> Option<Arc<dyn PanelHandle>> {
549 let entry = self.visible_entry()?;
550 if entry.panel.is_zoomed(cx) {
551 Some(entry.panel.clone())
552 } else {
553 None
554 }
555 }
556
557 pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<Pixels> {
558 self.panel_entries
559 .iter()
560 .find(|entry| entry.panel.panel_id() == panel.panel_id())
561 .map(|entry| entry.panel.size(cx))
562 }
563
564 pub fn active_panel_size(&self, cx: &WindowContext) -> Option<Pixels> {
565 if self.is_open {
566 self.panel_entries
567 .get(self.active_panel_index)
568 .map(|entry| entry.panel.size(cx))
569 } else {
570 None
571 }
572 }
573
574 pub fn resize_active_panel(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
575 if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
576 let size = size.map(|size| size.max(RESIZE_HANDLE_SIZE).round());
577
578 entry.panel.set_size(size, cx);
579 cx.notify();
580 }
581 }
582
583 pub fn toggle_action(&self) -> Box<dyn Action> {
584 match self.position {
585 DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
586 DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
587 DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
588 }
589 }
590
591 fn dispatch_context() -> KeyContext {
592 let mut dispatch_context = KeyContext::new_with_defaults();
593 dispatch_context.add("Dock");
594
595 dispatch_context
596 }
597
598 pub fn clamp_panel_size(&mut self, max_size: Pixels, cx: &mut WindowContext) {
599 let max_size = px((max_size.0 - RESIZE_HANDLE_SIZE.0).abs());
600 for panel in self.panel_entries.iter().map(|entry| &entry.panel) {
601 if panel.size(cx) > max_size {
602 panel.set_size(Some(max_size.max(RESIZE_HANDLE_SIZE)), cx);
603 }
604 }
605 }
606}
607
608impl Render for Dock {
609 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
610 let dispatch_context = Self::dispatch_context();
611 if let Some(entry) = self.visible_entry() {
612 let size = entry.panel.size(cx);
613
614 let position = self.position;
615 let create_resize_handle = || {
616 let handle = div()
617 .id("resize-handle")
618 .on_drag(DraggedDock(position), |dock, _, cx| {
619 cx.stop_propagation();
620 cx.new_view(|_| dock.clone())
621 })
622 .on_mouse_down(
623 MouseButton::Left,
624 cx.listener(|_, _: &MouseDownEvent, cx| {
625 cx.stop_propagation();
626 }),
627 )
628 .on_mouse_up(
629 MouseButton::Left,
630 cx.listener(|v, e: &MouseUpEvent, cx| {
631 if e.click_count == 2 {
632 v.resize_active_panel(None, cx);
633 cx.stop_propagation();
634 }
635 }),
636 )
637 .occlude();
638 match self.position() {
639 DockPosition::Left => deferred(
640 handle
641 .absolute()
642 .right(-RESIZE_HANDLE_SIZE / 2.)
643 .top(px(0.))
644 .h_full()
645 .w(RESIZE_HANDLE_SIZE)
646 .cursor_col_resize(),
647 ),
648 DockPosition::Bottom => deferred(
649 handle
650 .absolute()
651 .top(-RESIZE_HANDLE_SIZE / 2.)
652 .left(px(0.))
653 .w_full()
654 .h(RESIZE_HANDLE_SIZE)
655 .cursor_row_resize(),
656 ),
657 DockPosition::Right => deferred(
658 handle
659 .absolute()
660 .top(px(0.))
661 .left(-RESIZE_HANDLE_SIZE / 2.)
662 .h_full()
663 .w(RESIZE_HANDLE_SIZE)
664 .cursor_col_resize(),
665 ),
666 }
667 };
668
669 div()
670 .key_context(dispatch_context)
671 .track_focus(&self.focus_handle(cx))
672 .flex()
673 .bg(cx.theme().colors().panel_background)
674 .border_color(cx.theme().colors().border)
675 .overflow_hidden()
676 .map(|this| match self.position().axis() {
677 Axis::Horizontal => this.w(size).h_full().flex_row(),
678 Axis::Vertical => this.h(size).w_full().flex_col(),
679 })
680 .map(|this| match self.position() {
681 DockPosition::Left => this.border_r_1(),
682 DockPosition::Right => this.border_l_1(),
683 DockPosition::Bottom => this.border_t_1(),
684 })
685 .child(
686 div()
687 .map(|this| match self.position().axis() {
688 Axis::Horizontal => this.min_w(size).h_full(),
689 Axis::Vertical => this.min_h(size).w_full(),
690 })
691 .child(
692 entry
693 .panel
694 .to_any()
695 .cached(StyleRefinement::default().v_flex().size_full()),
696 ),
697 )
698 .when(self.resizeable, |this| this.child(create_resize_handle()))
699 } else {
700 div()
701 .key_context(dispatch_context)
702 .track_focus(&self.focus_handle(cx))
703 }
704 }
705}
706
707impl PanelButtons {
708 pub fn new(dock: View<Dock>, cx: &mut ViewContext<Self>) -> Self {
709 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
710 Self { dock }
711 }
712}
713
714impl Render for PanelButtons {
715 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
716 let dock = self.dock.read(cx);
717 let active_index = dock.active_panel_index;
718 let is_open = dock.is_open;
719 let dock_position = dock.position;
720
721 let (menu_anchor, menu_attach) = match dock.position {
722 DockPosition::Left => (Corner::BottomLeft, Corner::TopLeft),
723 DockPosition::Bottom | DockPosition::Right => (Corner::BottomRight, Corner::TopRight),
724 };
725
726 let buttons = dock
727 .panel_entries
728 .iter()
729 .enumerate()
730 .filter_map(|(i, entry)| {
731 let icon = entry.panel.icon(cx)?;
732 let icon_tooltip = entry.panel.icon_tooltip(cx)?;
733 let name = entry.panel.persistent_name();
734 let panel = entry.panel.clone();
735
736 let is_active_button = i == active_index && is_open;
737 let (action, tooltip) = if is_active_button {
738 let action = dock.toggle_action();
739
740 let tooltip: SharedString =
741 format!("Close {} dock", dock.position.label()).into();
742
743 (action, tooltip)
744 } else {
745 let action = entry.panel.toggle_action(cx);
746
747 (action, icon_tooltip.into())
748 };
749
750 Some(
751 right_click_menu(name)
752 .menu(move |cx| {
753 const POSITIONS: [DockPosition; 3] = [
754 DockPosition::Left,
755 DockPosition::Right,
756 DockPosition::Bottom,
757 ];
758
759 ContextMenu::build(cx, |mut menu, cx| {
760 for position in POSITIONS {
761 if position != dock_position
762 && panel.position_is_valid(position, cx)
763 {
764 let panel = panel.clone();
765 menu = menu.entry(
766 format!("Dock {}", position.label()),
767 None,
768 move |cx| {
769 panel.set_position(position, cx);
770 },
771 )
772 }
773 }
774 menu
775 })
776 })
777 .anchor(menu_anchor)
778 .attach(menu_attach)
779 .trigger(
780 IconButton::new(name, icon)
781 .icon_size(IconSize::Small)
782 .toggle_state(is_active_button)
783 .on_click({
784 let action = action.boxed_clone();
785 move |_, cx| cx.dispatch_action(action.boxed_clone())
786 })
787 .tooltip(move |cx| {
788 Tooltip::for_action(tooltip.clone(), &*action, cx)
789 }),
790 ),
791 )
792 });
793
794 h_flex().gap_0p5().children(buttons)
795 }
796}
797
798impl StatusItemView for PanelButtons {
799 fn set_active_pane_item(
800 &mut self,
801 _active_pane_item: Option<&dyn crate::ItemHandle>,
802 _cx: &mut ViewContext<Self>,
803 ) {
804 // Nothing to do, panel buttons don't depend on the active center item
805 }
806}
807
808#[cfg(any(test, feature = "test-support"))]
809pub mod test {
810 use super::*;
811 use gpui::{actions, div, ViewContext, WindowContext};
812
813 pub struct TestPanel {
814 pub position: DockPosition,
815 pub zoomed: bool,
816 pub active: bool,
817 pub focus_handle: FocusHandle,
818 pub size: Pixels,
819 }
820 actions!(test, [ToggleTestPanel]);
821
822 impl EventEmitter<PanelEvent> for TestPanel {}
823
824 impl TestPanel {
825 pub fn new(position: DockPosition, cx: &mut WindowContext) -> Self {
826 Self {
827 position,
828 zoomed: false,
829 active: false,
830 focus_handle: cx.focus_handle(),
831 size: px(300.),
832 }
833 }
834 }
835
836 impl Render for TestPanel {
837 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
838 div().id("test").track_focus(&self.focus_handle(cx))
839 }
840 }
841
842 impl Panel for TestPanel {
843 fn persistent_name() -> &'static str {
844 "TestPanel"
845 }
846
847 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
848 self.position
849 }
850
851 fn position_is_valid(&self, _: super::DockPosition) -> bool {
852 true
853 }
854
855 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
856 self.position = position;
857 cx.update_global::<SettingsStore, _>(|_, _| {});
858 }
859
860 fn size(&self, _: &WindowContext) -> Pixels {
861 self.size
862 }
863
864 fn set_size(&mut self, size: Option<Pixels>, _: &mut ViewContext<Self>) {
865 self.size = size.unwrap_or(px(300.));
866 }
867
868 fn icon(&self, _: &WindowContext) -> Option<ui::IconName> {
869 None
870 }
871
872 fn icon_tooltip(&self, _cx: &WindowContext) -> Option<&'static str> {
873 None
874 }
875
876 fn toggle_action(&self) -> Box<dyn Action> {
877 ToggleTestPanel.boxed_clone()
878 }
879
880 fn is_zoomed(&self, _: &WindowContext) -> bool {
881 self.zoomed
882 }
883
884 fn set_zoomed(&mut self, zoomed: bool, _cx: &mut ViewContext<Self>) {
885 self.zoomed = zoomed;
886 }
887
888 fn set_active(&mut self, active: bool, _cx: &mut ViewContext<Self>) {
889 self.active = active;
890 }
891 }
892
893 impl FocusableView for TestPanel {
894 fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
895 self.focus_handle.clone()
896 }
897 }
898}