dock.rs

   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().clone();
 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                if let Some(panel) = dock.read(cx).active_panel() {
 310                    if 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                }
 317            }
 318            if workspace.zoomed_position == Some(position) {
 319                workspace.zoomed = None;
 320                workspace.zoomed_position = None;
 321                cx.emit(Event::ZoomChanged);
 322            }
 323        })
 324        .detach();
 325
 326        dock
 327    }
 328
 329    pub fn position(&self) -> DockPosition {
 330        self.position
 331    }
 332
 333    pub fn is_open(&self) -> bool {
 334        self.is_open
 335    }
 336
 337    fn resizable(&self, cx: &App) -> bool {
 338        !(self.zoom_layer_open || self.modal_layer.read(cx).has_active_modal())
 339    }
 340
 341    pub fn panel<T: Panel>(&self) -> Option<Entity<T>> {
 342        self.panel_entries
 343            .iter()
 344            .find_map(|entry| entry.panel.to_any().clone().downcast().ok())
 345    }
 346
 347    pub fn panel_index_for_type<T: Panel>(&self) -> Option<usize> {
 348        self.panel_entries
 349            .iter()
 350            .position(|entry| entry.panel.to_any().downcast::<T>().is_ok())
 351    }
 352
 353    pub fn panel_index_for_persistent_name(&self, ui_name: &str, _cx: &App) -> Option<usize> {
 354        self.panel_entries
 355            .iter()
 356            .position(|entry| entry.panel.persistent_name() == ui_name)
 357    }
 358
 359    pub fn panel_index_for_proto_id(&self, panel_id: PanelId) -> Option<usize> {
 360        self.panel_entries
 361            .iter()
 362            .position(|entry| entry.panel.remote_id() == Some(panel_id))
 363    }
 364
 365    pub fn first_enabled_panel_idx(&mut self, cx: &mut Context<Self>) -> anyhow::Result<usize> {
 366        self.panel_entries
 367            .iter()
 368            .position(|entry| entry.panel.enabled(cx))
 369            .with_context(|| {
 370                format!(
 371                    "Couldn't find any enabled panel for the {} dock.",
 372                    self.position.label()
 373                )
 374            })
 375    }
 376
 377    pub fn first_enabled_panel_idx_excluding(&self, exclude_name: &str, cx: &App) -> Option<usize> {
 378        self.panel_entries.iter().position(|entry| {
 379            entry.panel.persistent_name() != exclude_name && entry.panel.enabled(cx)
 380        })
 381    }
 382
 383    fn active_panel_entry(&self) -> Option<&PanelEntry> {
 384        self.active_panel_index
 385            .and_then(|index| self.panel_entries.get(index))
 386    }
 387
 388    pub fn active_panel_index(&self) -> Option<usize> {
 389        self.active_panel_index
 390    }
 391
 392    pub fn set_open(&mut self, open: bool, window: &mut Window, cx: &mut Context<Self>) {
 393        if open != self.is_open {
 394            self.is_open = open;
 395            if let Some(active_panel) = self.active_panel_entry() {
 396                active_panel.panel.set_active(open, window, cx);
 397            }
 398
 399            cx.notify();
 400        }
 401    }
 402
 403    pub fn set_panel_zoomed(
 404        &mut self,
 405        panel: &AnyView,
 406        zoomed: bool,
 407        window: &mut Window,
 408        cx: &mut Context<Self>,
 409    ) {
 410        for entry in &mut self.panel_entries {
 411            if entry.panel.panel_id() == panel.entity_id() {
 412                if zoomed != entry.panel.is_zoomed(window, cx) {
 413                    entry.panel.set_zoomed(zoomed, window, cx);
 414                }
 415            } else if entry.panel.is_zoomed(window, cx) {
 416                entry.panel.set_zoomed(false, window, cx);
 417            }
 418        }
 419
 420        self.workspace
 421            .update(cx, |workspace, cx| {
 422                workspace.serialize_workspace(window, cx);
 423            })
 424            .ok();
 425        cx.notify();
 426    }
 427
 428    pub fn zoom_out(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 429        for entry in &mut self.panel_entries {
 430            if entry.panel.is_zoomed(window, cx) {
 431                entry.panel.set_zoomed(false, window, cx);
 432            }
 433        }
 434    }
 435
 436    pub(crate) fn add_panel<T: Panel>(
 437        &mut self,
 438        panel: Entity<T>,
 439        workspace: WeakEntity<Workspace>,
 440        window: &mut Window,
 441        cx: &mut Context<Self>,
 442    ) -> usize {
 443        let subscriptions = [
 444            cx.observe(&panel, |_, _, cx| cx.notify()),
 445            cx.observe_global_in::<SettingsStore>(window, {
 446                let workspace = workspace.clone();
 447                let panel = panel.clone();
 448
 449                move |this, window, cx| {
 450                    let new_position = panel.read(cx).position(window, cx);
 451                    if new_position == this.position {
 452                        return;
 453                    }
 454
 455                    let Ok(new_dock) = workspace.update(cx, |workspace, cx| {
 456                        if panel.is_zoomed(window, cx) {
 457                            workspace.zoomed_position = Some(new_position);
 458                        }
 459                        match new_position {
 460                            DockPosition::Left => &workspace.left_dock,
 461                            DockPosition::Bottom => &workspace.bottom_dock,
 462                            DockPosition::Right => &workspace.right_dock,
 463                        }
 464                        .clone()
 465                    }) else {
 466                        return;
 467                    };
 468
 469                    let was_visible = this.is_open()
 470                        && this.visible_panel().map_or(false, |active_panel| {
 471                            active_panel.panel_id() == Entity::entity_id(&panel)
 472                        });
 473
 474                    this.remove_panel(&panel, window, cx);
 475
 476                    new_dock.update(cx, |new_dock, cx| {
 477                        new_dock.remove_panel(&panel, window, cx);
 478                        let index =
 479                            new_dock.add_panel(panel.clone(), workspace.clone(), window, cx);
 480                        if was_visible {
 481                            new_dock.set_open(true, window, cx);
 482                            new_dock.activate_panel(index, window, cx);
 483                        }
 484                    });
 485                }
 486            }),
 487            cx.subscribe_in(
 488                &panel,
 489                window,
 490                move |this, panel, event, window, cx| match event {
 491                    PanelEvent::ZoomIn => {
 492                        this.set_panel_zoomed(&panel.to_any(), true, window, cx);
 493                        if !PanelHandle::panel_focus_handle(panel, cx).contains_focused(window, cx)
 494                        {
 495                            window.focus(&panel.focus_handle(cx));
 496                        }
 497                        workspace
 498                            .update(cx, |workspace, cx| {
 499                                workspace.zoomed = Some(panel.downgrade().into());
 500                                workspace.zoomed_position =
 501                                    Some(panel.read(cx).position(window, cx));
 502                                cx.emit(Event::ZoomChanged);
 503                            })
 504                            .ok();
 505                    }
 506                    PanelEvent::ZoomOut => {
 507                        this.set_panel_zoomed(&panel.to_any(), false, window, cx);
 508                        workspace
 509                            .update(cx, |workspace, cx| {
 510                                if workspace.zoomed_position == Some(this.position) {
 511                                    workspace.zoomed = None;
 512                                    workspace.zoomed_position = None;
 513                                    cx.emit(Event::ZoomChanged);
 514                                }
 515                                cx.notify();
 516                            })
 517                            .ok();
 518                    }
 519                    PanelEvent::Activate => {
 520                        if let Some(ix) = this
 521                            .panel_entries
 522                            .iter()
 523                            .position(|entry| entry.panel.panel_id() == Entity::entity_id(panel))
 524                        {
 525                            this.set_open(true, window, cx);
 526                            this.activate_panel(ix, window, cx);
 527                            window.focus(&panel.read(cx).focus_handle(cx));
 528                        }
 529                    }
 530                    PanelEvent::Close => {
 531                        if this
 532                            .visible_panel()
 533                            .map_or(false, |p| p.panel_id() == Entity::entity_id(panel))
 534                        {
 535                            this.set_open(false, window, cx);
 536                        }
 537                    }
 538                },
 539            ),
 540        ];
 541
 542        let index = match self
 543            .panel_entries
 544            .binary_search_by_key(&panel.read(cx).activation_priority(), |entry| {
 545                entry.panel.activation_priority(cx)
 546            }) {
 547            Ok(ix) => ix,
 548            Err(ix) => ix,
 549        };
 550        if let Some(active_index) = self.active_panel_index.as_mut() {
 551            if *active_index >= index {
 552                *active_index += 1;
 553            }
 554        }
 555        self.panel_entries.insert(
 556            index,
 557            PanelEntry {
 558                panel: Arc::new(panel.clone()),
 559                _subscriptions: subscriptions,
 560            },
 561        );
 562
 563        self.restore_state(window, cx);
 564        if panel.read(cx).starts_open(window, cx) {
 565            self.activate_panel(index, window, cx);
 566            self.set_open(true, window, cx);
 567        }
 568
 569        cx.notify();
 570        index
 571    }
 572
 573    pub fn restore_state(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
 574        if let Some(serialized) = self.serialized_dock.clone() {
 575            if let Some(active_panel) = serialized.active_panel.filter(|_| serialized.visible) {
 576                if let Some(idx) = self.panel_index_for_persistent_name(active_panel.as_str(), cx) {
 577                    self.activate_panel(idx, window, cx);
 578                }
 579            }
 580
 581            if serialized.zoom {
 582                if let Some(panel) = self.active_panel() {
 583                    panel.set_zoomed(true, window, cx)
 584                }
 585            }
 586            self.set_open(serialized.visible, window, cx);
 587            return true;
 588        }
 589        false
 590    }
 591
 592    pub fn remove_panel<T: Panel>(
 593        &mut self,
 594        panel: &Entity<T>,
 595        window: &mut Window,
 596        cx: &mut Context<Self>,
 597    ) {
 598        if let Some(panel_ix) = self
 599            .panel_entries
 600            .iter()
 601            .position(|entry| entry.panel.panel_id() == Entity::entity_id(panel))
 602        {
 603            if let Some(active_panel_index) = self.active_panel_index.as_mut() {
 604                match panel_ix.cmp(active_panel_index) {
 605                    std::cmp::Ordering::Less => {
 606                        *active_panel_index -= 1;
 607                    }
 608                    std::cmp::Ordering::Equal => {
 609                        self.active_panel_index = None;
 610                        self.set_open(false, window, cx);
 611                    }
 612                    std::cmp::Ordering::Greater => {}
 613                }
 614            }
 615            self.panel_entries.remove(panel_ix);
 616            cx.notify();
 617        }
 618    }
 619
 620    pub fn panels_len(&self) -> usize {
 621        self.panel_entries.len()
 622    }
 623
 624    pub fn activate_panel(&mut self, panel_ix: usize, window: &mut Window, cx: &mut Context<Self>) {
 625        if Some(panel_ix) != self.active_panel_index {
 626            if let Some(active_panel) = self.active_panel_entry() {
 627                active_panel.panel.set_active(false, window, cx);
 628            }
 629
 630            self.active_panel_index = Some(panel_ix);
 631            if let Some(active_panel) = self.active_panel_entry() {
 632                active_panel.panel.set_active(true, window, cx);
 633            }
 634
 635            cx.notify();
 636        }
 637    }
 638
 639    pub fn visible_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
 640        let entry = self.visible_entry()?;
 641        Some(&entry.panel)
 642    }
 643
 644    pub fn active_panel(&self) -> Option<&Arc<dyn PanelHandle>> {
 645        let panel_entry = self.active_panel_entry()?;
 646        Some(&panel_entry.panel)
 647    }
 648
 649    fn visible_entry(&self) -> Option<&PanelEntry> {
 650        if self.is_open {
 651            self.active_panel_entry()
 652        } else {
 653            None
 654        }
 655    }
 656
 657    pub fn zoomed_panel(&self, window: &Window, cx: &App) -> Option<Arc<dyn PanelHandle>> {
 658        let entry = self.visible_entry()?;
 659        if entry.panel.is_zoomed(window, cx) {
 660            Some(entry.panel.clone())
 661        } else {
 662            None
 663        }
 664    }
 665
 666    pub fn panel_size(&self, panel: &dyn PanelHandle, window: &Window, cx: &App) -> Option<Pixels> {
 667        self.panel_entries
 668            .iter()
 669            .find(|entry| entry.panel.panel_id() == panel.panel_id())
 670            .map(|entry| entry.panel.size(window, cx))
 671    }
 672
 673    pub fn active_panel_size(&self, window: &Window, cx: &App) -> Option<Pixels> {
 674        if self.is_open {
 675            self.active_panel_entry()
 676                .map(|entry| entry.panel.size(window, cx))
 677        } else {
 678            None
 679        }
 680    }
 681
 682    pub fn resize_active_panel(
 683        &mut self,
 684        size: Option<Pixels>,
 685        window: &mut Window,
 686        cx: &mut Context<Self>,
 687    ) {
 688        if let Some(entry) = self.active_panel_entry() {
 689            let size = size.map(|size| size.max(RESIZE_HANDLE_SIZE).round());
 690
 691            entry.panel.set_size(size, window, cx);
 692            cx.notify();
 693        }
 694    }
 695
 696    pub fn resize_all_panels(
 697        &mut self,
 698        size: Option<Pixels>,
 699        window: &mut Window,
 700        cx: &mut Context<Self>,
 701    ) {
 702        for entry in &mut self.panel_entries {
 703            let size = size.map(|size| size.max(RESIZE_HANDLE_SIZE).round());
 704            entry.panel.set_size(size, window, cx);
 705        }
 706        cx.notify();
 707    }
 708
 709    pub fn toggle_action(&self) -> Box<dyn Action> {
 710        match self.position {
 711            DockPosition::Left => crate::ToggleLeftDock.boxed_clone(),
 712            DockPosition::Bottom => crate::ToggleBottomDock.boxed_clone(),
 713            DockPosition::Right => crate::ToggleRightDock.boxed_clone(),
 714        }
 715    }
 716
 717    fn dispatch_context() -> KeyContext {
 718        let mut dispatch_context = KeyContext::new_with_defaults();
 719        dispatch_context.add("Dock");
 720
 721        dispatch_context
 722    }
 723
 724    pub fn clamp_panel_size(&mut self, max_size: Pixels, window: &mut Window, cx: &mut App) {
 725        let max_size = px((max_size.0 - RESIZE_HANDLE_SIZE.0).abs());
 726        for panel in self.panel_entries.iter().map(|entry| &entry.panel) {
 727            if panel.size(window, cx) > max_size {
 728                panel.set_size(Some(max_size.max(RESIZE_HANDLE_SIZE)), window, cx);
 729            }
 730        }
 731    }
 732}
 733
 734impl Render for Dock {
 735    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 736        let dispatch_context = Self::dispatch_context();
 737        if let Some(entry) = self.visible_entry() {
 738            let size = entry.panel.size(window, cx);
 739
 740            let position = self.position;
 741            let create_resize_handle = || {
 742                let handle = div()
 743                    .id("resize-handle")
 744                    .on_drag(DraggedDock(position), |dock, _, _, cx| {
 745                        cx.stop_propagation();
 746                        cx.new(|_| dock.clone())
 747                    })
 748                    .on_mouse_down(
 749                        MouseButton::Left,
 750                        cx.listener(|_, _: &MouseDownEvent, _, cx| {
 751                            cx.stop_propagation();
 752                        }),
 753                    )
 754                    .on_mouse_up(
 755                        MouseButton::Left,
 756                        cx.listener(|dock, e: &MouseUpEvent, window, cx| {
 757                            if e.click_count == 2 {
 758                                dock.resize_active_panel(None, window, cx);
 759                                dock.workspace
 760                                    .update(cx, |workspace, cx| {
 761                                        workspace.serialize_workspace(window, cx);
 762                                    })
 763                                    .ok();
 764                                cx.stop_propagation();
 765                            }
 766                        }),
 767                    )
 768                    .occlude();
 769                match self.position() {
 770                    DockPosition::Left => deferred(
 771                        handle
 772                            .absolute()
 773                            .right(-RESIZE_HANDLE_SIZE / 2.)
 774                            .top(px(0.))
 775                            .h_full()
 776                            .w(RESIZE_HANDLE_SIZE)
 777                            .cursor_col_resize(),
 778                    ),
 779                    DockPosition::Bottom => deferred(
 780                        handle
 781                            .absolute()
 782                            .top(-RESIZE_HANDLE_SIZE / 2.)
 783                            .left(px(0.))
 784                            .w_full()
 785                            .h(RESIZE_HANDLE_SIZE)
 786                            .cursor_row_resize(),
 787                    ),
 788                    DockPosition::Right => deferred(
 789                        handle
 790                            .absolute()
 791                            .top(px(0.))
 792                            .left(-RESIZE_HANDLE_SIZE / 2.)
 793                            .h_full()
 794                            .w(RESIZE_HANDLE_SIZE)
 795                            .cursor_col_resize(),
 796                    ),
 797                }
 798            };
 799
 800            div()
 801                .key_context(dispatch_context)
 802                .track_focus(&self.focus_handle(cx))
 803                .flex()
 804                .bg(cx.theme().colors().panel_background)
 805                .border_color(cx.theme().colors().border)
 806                .overflow_hidden()
 807                .map(|this| match self.position().axis() {
 808                    Axis::Horizontal => this.w(size).h_full().flex_row(),
 809                    Axis::Vertical => this.h(size).w_full().flex_col(),
 810                })
 811                .map(|this| match self.position() {
 812                    DockPosition::Left => this.border_r_1(),
 813                    DockPosition::Right => this.border_l_1(),
 814                    DockPosition::Bottom => this.border_t_1(),
 815                })
 816                .child(
 817                    div()
 818                        .map(|this| match self.position().axis() {
 819                            Axis::Horizontal => this.min_w(size).h_full(),
 820                            Axis::Vertical => this.min_h(size).w_full(),
 821                        })
 822                        .child(
 823                            entry
 824                                .panel
 825                                .to_any()
 826                                .cached(StyleRefinement::default().v_flex().size_full()),
 827                        ),
 828                )
 829                .when(self.resizable(cx), |this| {
 830                    this.child(create_resize_handle())
 831                })
 832        } else {
 833            div()
 834                .key_context(dispatch_context)
 835                .track_focus(&self.focus_handle(cx))
 836        }
 837    }
 838}
 839
 840impl PanelButtons {
 841    pub fn new(dock: Entity<Dock>, cx: &mut Context<Self>) -> Self {
 842        cx.observe(&dock, |_, _, cx| cx.notify()).detach();
 843        let settings_subscription = cx.observe_global::<SettingsStore>(|_, cx| cx.notify());
 844        Self {
 845            dock,
 846            _settings_subscription: settings_subscription,
 847        }
 848    }
 849}
 850
 851impl Render for PanelButtons {
 852    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 853        let dock = self.dock.read(cx);
 854        let active_index = dock.active_panel_index;
 855        let is_open = dock.is_open;
 856        let dock_position = dock.position;
 857
 858        let (menu_anchor, menu_attach) = match dock.position {
 859            DockPosition::Left => (Corner::BottomLeft, Corner::TopLeft),
 860            DockPosition::Bottom | DockPosition::Right => (Corner::BottomRight, Corner::TopRight),
 861        };
 862
 863        let buttons: Vec<_> = dock
 864            .panel_entries
 865            .iter()
 866            .enumerate()
 867            .filter_map(|(i, entry)| {
 868                let icon = entry.panel.icon(window, cx)?;
 869                let icon_tooltip = entry.panel.icon_tooltip(window, cx)?;
 870                let name = entry.panel.persistent_name();
 871                let panel = entry.panel.clone();
 872
 873                let is_active_button = Some(i) == active_index && is_open;
 874                let (action, tooltip) = if is_active_button {
 875                    let action = dock.toggle_action();
 876
 877                    let tooltip: SharedString =
 878                        format!("Close {} Dock", dock.position.label()).into();
 879
 880                    (action, tooltip)
 881                } else {
 882                    let action = entry.panel.toggle_action(window, cx);
 883
 884                    (action, icon_tooltip.into())
 885                };
 886
 887                let focus_handle = dock.focus_handle(cx);
 888
 889                Some(
 890                    right_click_menu(name)
 891                        .menu(move |window, cx| {
 892                            const POSITIONS: [DockPosition; 3] = [
 893                                DockPosition::Left,
 894                                DockPosition::Right,
 895                                DockPosition::Bottom,
 896                            ];
 897
 898                            ContextMenu::build(window, cx, |mut menu, _, cx| {
 899                                for position in POSITIONS {
 900                                    if position != dock_position
 901                                        && panel.position_is_valid(position, cx)
 902                                    {
 903                                        let panel = panel.clone();
 904                                        menu = menu.entry(
 905                                            format!("Dock {}", position.label()),
 906                                            None,
 907                                            move |window, cx| {
 908                                                panel.set_position(position, window, cx);
 909                                            },
 910                                        )
 911                                    }
 912                                }
 913                                menu
 914                            })
 915                        })
 916                        .anchor(menu_anchor)
 917                        .attach(menu_attach)
 918                        .trigger(move |is_active, _window, _cx| {
 919                            IconButton::new(name, icon)
 920                                .icon_size(IconSize::Small)
 921                                .toggle_state(is_active_button)
 922                                .on_click({
 923                                    let action = action.boxed_clone();
 924                                    move |_, window, cx| {
 925                                        window.focus(&focus_handle);
 926                                        window.dispatch_action(action.boxed_clone(), cx)
 927                                    }
 928                                })
 929                                .when(!is_active, |this| {
 930                                    this.tooltip(move |window, cx| {
 931                                        Tooltip::for_action(tooltip.clone(), &*action, window, cx)
 932                                    })
 933                                })
 934                        }),
 935                )
 936            })
 937            .collect();
 938
 939        let has_buttons = !buttons.is_empty();
 940
 941        h_flex()
 942            .gap_1()
 943            .children(buttons)
 944            .when(has_buttons && dock.position == DockPosition::Left, |this| {
 945                this.child(Divider::vertical().color(DividerColor::Border))
 946            })
 947    }
 948}
 949
 950impl StatusItemView for PanelButtons {
 951    fn set_active_pane_item(
 952        &mut self,
 953        _active_pane_item: Option<&dyn crate::ItemHandle>,
 954        _window: &mut Window,
 955        _cx: &mut Context<Self>,
 956    ) {
 957        // Nothing to do, panel buttons don't depend on the active center item
 958    }
 959}
 960
 961#[cfg(any(test, feature = "test-support"))]
 962pub mod test {
 963    use super::*;
 964    use gpui::{App, Context, Window, actions, div};
 965
 966    pub struct TestPanel {
 967        pub position: DockPosition,
 968        pub zoomed: bool,
 969        pub active: bool,
 970        pub focus_handle: FocusHandle,
 971        pub size: Pixels,
 972    }
 973    actions!(test_only, [ToggleTestPanel]);
 974
 975    impl EventEmitter<PanelEvent> for TestPanel {}
 976
 977    impl TestPanel {
 978        pub fn new(position: DockPosition, cx: &mut App) -> Self {
 979            Self {
 980                position,
 981                zoomed: false,
 982                active: false,
 983                focus_handle: cx.focus_handle(),
 984                size: px(300.),
 985            }
 986        }
 987    }
 988
 989    impl Render for TestPanel {
 990        fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 991            div().id("test").track_focus(&self.focus_handle(cx))
 992        }
 993    }
 994
 995    impl Panel for TestPanel {
 996        fn persistent_name() -> &'static str {
 997            "TestPanel"
 998        }
 999
1000        fn position(&self, _window: &Window, _: &App) -> super::DockPosition {
1001            self.position
1002        }
1003
1004        fn position_is_valid(&self, _: super::DockPosition) -> bool {
1005            true
1006        }
1007
1008        fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
1009            self.position = position;
1010            cx.update_global::<SettingsStore, _>(|_, _| {});
1011        }
1012
1013        fn size(&self, _window: &Window, _: &App) -> Pixels {
1014            self.size
1015        }
1016
1017        fn set_size(&mut self, size: Option<Pixels>, _window: &mut Window, _: &mut Context<Self>) {
1018            self.size = size.unwrap_or(px(300.));
1019        }
1020
1021        fn icon(&self, _window: &Window, _: &App) -> Option<ui::IconName> {
1022            None
1023        }
1024
1025        fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
1026            None
1027        }
1028
1029        fn toggle_action(&self) -> Box<dyn Action> {
1030            ToggleTestPanel.boxed_clone()
1031        }
1032
1033        fn is_zoomed(&self, _window: &Window, _: &App) -> bool {
1034            self.zoomed
1035        }
1036
1037        fn set_zoomed(&mut self, zoomed: bool, _window: &mut Window, _cx: &mut Context<Self>) {
1038            self.zoomed = zoomed;
1039        }
1040
1041        fn set_active(&mut self, active: bool, _window: &mut Window, _cx: &mut Context<Self>) {
1042            self.active = active;
1043        }
1044
1045        fn activation_priority(&self) -> u32 {
1046            100
1047        }
1048    }
1049
1050    impl Focusable for TestPanel {
1051        fn focus_handle(&self, _cx: &App) -> FocusHandle {
1052            self.focus_handle.clone()
1053        }
1054    }
1055}