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