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