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, AnchorCorner, AnyView, AppContext, Axis, Entity, EntityId,
7 EventEmitter, FocusHandle, FocusableView, IntoElement, KeyContext, MouseButton, MouseDownEvent,
8 MouseUpEvent, ParentElement, Render, SharedString, StyleRefinement, Styled, Subscription, View,
9 ViewContext, 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
18const 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 entry.panel.set_size(size, cx);
578 cx.notify();
579 }
580 }
581
582 pub fn toggle_action(&self) -> Box<dyn Action> {
583 match self.position {
584 DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
585 DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
586 DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
587 }
588 }
589
590 fn dispatch_context() -> KeyContext {
591 let mut dispatch_context = KeyContext::new_with_defaults();
592 dispatch_context.add("Dock");
593
594 dispatch_context
595 }
596}
597
598impl Render for Dock {
599 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
600 let dispatch_context = Self::dispatch_context();
601 if let Some(entry) = self.visible_entry() {
602 let size = entry.panel.size(cx);
603
604 let position = self.position;
605 let create_resize_handle = || {
606 let handle = div()
607 .id("resize-handle")
608 .on_drag(DraggedDock(position), |dock, cx| {
609 cx.stop_propagation();
610 cx.new_view(|_| dock.clone())
611 })
612 .on_mouse_down(
613 MouseButton::Left,
614 cx.listener(|_, _: &MouseDownEvent, cx| {
615 cx.stop_propagation();
616 }),
617 )
618 .on_mouse_up(
619 MouseButton::Left,
620 cx.listener(|v, e: &MouseUpEvent, cx| {
621 if e.click_count == 2 {
622 v.resize_active_panel(None, cx);
623 cx.stop_propagation();
624 }
625 }),
626 )
627 .occlude();
628 match self.position() {
629 DockPosition::Left => deferred(
630 handle
631 .absolute()
632 .right(-RESIZE_HANDLE_SIZE / 2.)
633 .top(px(0.))
634 .h_full()
635 .w(RESIZE_HANDLE_SIZE)
636 .cursor_col_resize(),
637 ),
638 DockPosition::Bottom => deferred(
639 handle
640 .absolute()
641 .top(-RESIZE_HANDLE_SIZE / 2.)
642 .left(px(0.))
643 .w_full()
644 .h(RESIZE_HANDLE_SIZE)
645 .cursor_row_resize(),
646 ),
647 DockPosition::Right => deferred(
648 handle
649 .absolute()
650 .top(px(0.))
651 .left(-RESIZE_HANDLE_SIZE / 2.)
652 .h_full()
653 .w(RESIZE_HANDLE_SIZE)
654 .cursor_col_resize(),
655 ),
656 }
657 };
658
659 div()
660 .key_context(dispatch_context)
661 .track_focus(&self.focus_handle(cx))
662 .flex()
663 .bg(cx.theme().colors().panel_background)
664 .border_color(cx.theme().colors().border)
665 .overflow_hidden()
666 .map(|this| match self.position().axis() {
667 Axis::Horizontal => this.w(size).h_full().flex_row(),
668 Axis::Vertical => this.h(size).w_full().flex_col(),
669 })
670 .map(|this| match self.position() {
671 DockPosition::Left => this.border_r_1(),
672 DockPosition::Right => this.border_l_1(),
673 DockPosition::Bottom => this.border_t_1(),
674 })
675 .child(
676 div()
677 .map(|this| match self.position().axis() {
678 Axis::Horizontal => this.min_w(size).h_full(),
679 Axis::Vertical => this.min_h(size).w_full(),
680 })
681 .child(
682 entry
683 .panel
684 .to_any()
685 .cached(StyleRefinement::default().v_flex().size_full()),
686 ),
687 )
688 .when(self.resizeable, |this| this.child(create_resize_handle()))
689 } else {
690 div()
691 .key_context(dispatch_context)
692 .track_focus(&self.focus_handle(cx))
693 }
694 }
695}
696
697impl PanelButtons {
698 pub fn new(dock: View<Dock>, cx: &mut ViewContext<Self>) -> Self {
699 cx.observe(&dock, |_, _, cx| cx.notify()).detach();
700 Self { dock }
701 }
702}
703
704impl Render for PanelButtons {
705 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
706 let dock = self.dock.read(cx);
707 let active_index = dock.active_panel_index;
708 let is_open = dock.is_open;
709 let dock_position = dock.position;
710
711 let (menu_anchor, menu_attach) = match dock.position {
712 DockPosition::Left => (AnchorCorner::BottomLeft, AnchorCorner::TopLeft),
713 DockPosition::Bottom | DockPosition::Right => {
714 (AnchorCorner::BottomRight, AnchorCorner::TopRight)
715 }
716 };
717
718 let buttons = dock
719 .panel_entries
720 .iter()
721 .enumerate()
722 .filter_map(|(i, entry)| {
723 let icon = entry.panel.icon(cx)?;
724 let icon_tooltip = entry.panel.icon_tooltip(cx)?;
725 let name = entry.panel.persistent_name();
726 let panel = entry.panel.clone();
727
728 let is_active_button = i == active_index && is_open;
729 let (action, tooltip) = if is_active_button {
730 let action = dock.toggle_action();
731
732 let tooltip: SharedString =
733 format!("Close {} dock", dock.position.label()).into();
734
735 (action, tooltip)
736 } else {
737 let action = entry.panel.toggle_action(cx);
738
739 (action, icon_tooltip.into())
740 };
741
742 Some(
743 right_click_menu(name)
744 .menu(move |cx| {
745 const POSITIONS: [DockPosition; 3] = [
746 DockPosition::Left,
747 DockPosition::Right,
748 DockPosition::Bottom,
749 ];
750
751 ContextMenu::build(cx, |mut menu, cx| {
752 for position in POSITIONS {
753 if position != dock_position
754 && panel.position_is_valid(position, cx)
755 {
756 let panel = panel.clone();
757 menu = menu.entry(
758 format!("Dock {}", position.label()),
759 None,
760 move |cx| {
761 panel.set_position(position, cx);
762 },
763 )
764 }
765 }
766 menu
767 })
768 })
769 .anchor(menu_anchor)
770 .attach(menu_attach)
771 .trigger(
772 IconButton::new(name, icon)
773 .icon_size(IconSize::Small)
774 .selected(is_active_button)
775 .on_click({
776 let action = action.boxed_clone();
777 move |_, cx| cx.dispatch_action(action.boxed_clone())
778 })
779 .tooltip(move |cx| {
780 Tooltip::for_action(tooltip.clone(), &*action, cx)
781 }),
782 ),
783 )
784 });
785
786 h_flex().gap_0p5().children(buttons)
787 }
788}
789
790impl StatusItemView for PanelButtons {
791 fn set_active_pane_item(
792 &mut self,
793 _active_pane_item: Option<&dyn crate::ItemHandle>,
794 _cx: &mut ViewContext<Self>,
795 ) {
796 // Nothing to do, panel buttons don't depend on the active center item
797 }
798}
799
800#[cfg(any(test, feature = "test-support"))]
801pub mod test {
802 use super::*;
803 use gpui::{actions, div, ViewContext, WindowContext};
804
805 pub struct TestPanel {
806 pub position: DockPosition,
807 pub zoomed: bool,
808 pub active: bool,
809 pub focus_handle: FocusHandle,
810 pub size: Pixels,
811 }
812 actions!(test, [ToggleTestPanel]);
813
814 impl EventEmitter<PanelEvent> for TestPanel {}
815
816 impl TestPanel {
817 pub fn new(position: DockPosition, cx: &mut WindowContext) -> Self {
818 Self {
819 position,
820 zoomed: false,
821 active: false,
822 focus_handle: cx.focus_handle(),
823 size: px(300.),
824 }
825 }
826 }
827
828 impl Render for TestPanel {
829 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
830 div().id("test").track_focus(&self.focus_handle(cx))
831 }
832 }
833
834 impl Panel for TestPanel {
835 fn persistent_name() -> &'static str {
836 "TestPanel"
837 }
838
839 fn position(&self, _: &gpui::WindowContext) -> super::DockPosition {
840 self.position
841 }
842
843 fn position_is_valid(&self, _: super::DockPosition) -> bool {
844 true
845 }
846
847 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
848 self.position = position;
849 cx.update_global::<SettingsStore, _>(|_, _| {});
850 }
851
852 fn size(&self, _: &WindowContext) -> Pixels {
853 self.size
854 }
855
856 fn set_size(&mut self, size: Option<Pixels>, _: &mut ViewContext<Self>) {
857 self.size = size.unwrap_or(px(300.));
858 }
859
860 fn icon(&self, _: &WindowContext) -> Option<ui::IconName> {
861 None
862 }
863
864 fn icon_tooltip(&self, _cx: &WindowContext) -> Option<&'static str> {
865 None
866 }
867
868 fn toggle_action(&self) -> Box<dyn Action> {
869 ToggleTestPanel.boxed_clone()
870 }
871
872 fn is_zoomed(&self, _: &WindowContext) -> bool {
873 self.zoomed
874 }
875
876 fn set_zoomed(&mut self, zoomed: bool, _cx: &mut ViewContext<Self>) {
877 self.zoomed = zoomed;
878 }
879
880 fn set_active(&mut self, active: bool, _cx: &mut ViewContext<Self>) {
881 self.active = active;
882 }
883 }
884
885 impl FocusableView for TestPanel {
886 fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
887 self.focus_handle.clone()
888 }
889 }
890}