1193 lines · 40.8 KB · Rust
RawDownload
use crate::persistence::model::DockData;use crate::{DraggedDock, Event, ModalLayer, Pane};use crate::{Workspace, status_bar::StatusItemView};use anyhow::Context as _;use client::proto; use gpui::{    Action, AnyView, App, Axis, Context, Corner, Entity, EntityId, EventEmitter, FocusHandle,    Focusable, IntoElement, KeyContext, MouseButton, MouseDownEvent, MouseUpEvent, ParentElement,    Render, SharedString, StyleRefinement, Styled, Subscription, WeakEntity, Window, deferred, div,    px,};use settings::SettingsStore;use std::sync::Arc;use ui::{ContextMenu, Divider, DividerColor, IconButton, Tooltip, h_flex};use ui::{prelude::*, right_click_menu};use util::ResultExt as _; pub(crate) const RESIZE_HANDLE_SIZE: Pixels = px(6.); pub enum PanelEvent {    ZoomIn,    ZoomOut,    Activate,    Close,} pub use proto::PanelId; pub struct MinimizePane;pub struct ClosePane; pub trait UtilityPane: EventEmitter<MinimizePane> + EventEmitter<ClosePane> + Render {    fn position(&self, window: &Window, cx: &App) -> UtilityPanePosition;    /// The icon to render in the adjacent pane's tab bar for toggling this utility pane    fn toggle_icon(&self, cx: &App) -> IconName;    fn expanded(&self, cx: &App) -> bool;    fn set_expanded(&mut self, expanded: bool, cx: &mut Context<Self>);    fn width(&self, cx: &App) -> Pixels;    fn set_width(&mut self, width: Option<Pixels>, cx: &mut Context<Self>);} pub trait UtilityPaneHandle: 'static + Send + Sync {    fn position(&self, window: &Window, cx: &App) -> UtilityPanePosition;    fn toggle_icon(&self, cx: &App) -> IconName;    fn expanded(&self, cx: &App) -> bool;    fn set_expanded(&self, expanded: bool, cx: &mut App);    fn width(&self, cx: &App) -> Pixels;    fn set_width(&self, width: Option<Pixels>, cx: &mut App);    fn to_any(&self) -> AnyView;    fn box_clone(&self) -> Box<dyn UtilityPaneHandle>;} impl<T> UtilityPaneHandle for Entity<T>where    T: UtilityPane,{    fn position(&self, window: &Window, cx: &App) -> UtilityPanePosition {        self.read(cx).position(window, cx)    }     fn toggle_icon(&self, cx: &App) -> IconName {        self.read(cx).toggle_icon(cx)    }     fn expanded(&self, cx: &App) -> bool {        self.read(cx).expanded(cx)    }     fn set_expanded(&self, expanded: bool, cx: &mut App) {        self.update(cx, |this, cx| this.set_expanded(expanded, cx))    }     fn width(&self, cx: &App) -> Pixels {        self.read(cx).width(cx)    }     fn set_width(&self, width: Option<Pixels>, cx: &mut App) {        self.update(cx, |this, cx| this.set_width(width, cx))    }     fn to_any(&self) -> AnyView {        self.clone().into()    }     fn box_clone(&self) -> Box<dyn UtilityPaneHandle> {        Box::new(self.clone())    }} pub enum UtilityPanePosition {    Left,    Right,} pub trait Panel: Focusable + EventEmitter<PanelEvent> + Render + Sized {    fn persistent_name() -> &'static str;    fn panel_key() -> &'static str;    fn position(&self, window: &Window, cx: &App) -> DockPosition;    fn position_is_valid(&self, position: DockPosition) -> bool;    fn set_position(&mut self, position: DockPosition, window: &mut Window, cx: &mut Context<Self>);    fn size(&self, window: &Window, cx: &App) -> Pixels;    fn set_size(&mut self, size: Option<Pixels>, window: &mut Window, cx: &mut Context<Self>);    fn icon(&self, window: &Window, cx: &App) -> Option<ui::IconName>;    fn icon_tooltip(&self, window: &Window, cx: &App) -> Option<&'static str>;    fn toggle_action(&self) -> Box<dyn Action>;    fn icon_label(&self, _window: &Window, _: &App) -> Option<String> {        None    }    fn is_zoomed(&self, _window: &Window, _cx: &App) -> bool {        false    }    fn starts_open(&self, _window: &Window, _cx: &App) -> bool {        false    }    fn set_zoomed(&mut self, _zoomed: bool, _window: &mut Window, _cx: &mut Context<Self>) {}    fn set_active(&mut self, _active: bool, _window: &mut Window, _cx: &mut Context<Self>) {}    fn pane(&self) -> Option<Entity<Pane>> {        None    }    fn remote_id() -> Option<proto::PanelId> {        None    }    fn activation_priority(&self) -> u32;    fn enabled(&self, _cx: &App) -> bool {        true    }} pub trait PanelHandle: Send + Sync {    fn panel_id(&self) -> EntityId;    fn persistent_name(&self) -> &'static str;    fn panel_key(&self) -> &'static str;    fn position(&self, window: &Window, cx: &App) -> DockPosition;    fn position_is_valid(&self, position: DockPosition, cx: &App) -> bool;    fn set_position(&self, position: DockPosition, window: &mut Window, cx: &mut App);    fn is_zoomed(&self, window: &Window, cx: &App) -> bool;    fn set_zoomed(&self, zoomed: bool, window: &mut Window, cx: &mut App);    fn set_active(&self, active: bool, window: &mut Window, cx: &mut App);    fn remote_id(&self) -> Option<proto::PanelId>;    fn pane(&self, cx: &App) -> Option<Entity<Pane>>;    fn size(&self, window: &Window, cx: &App) -> Pixels;    fn set_size(&self, size: Option<Pixels>, window: &mut Window, cx: &mut App);    fn icon(&self, window: &Window, cx: &App) -> Option<ui::IconName>;    fn icon_tooltip(&self, window: &Window, cx: &App) -> Option<&'static str>;    fn toggle_action(&self, window: &Window, cx: &App) -> Box<dyn Action>;    fn icon_label(&self, window: &Window, cx: &App) -> Option<String>;    fn panel_focus_handle(&self, cx: &App) -> FocusHandle;    fn to_any(&self) -> AnyView;    fn activation_priority(&self, cx: &App) -> u32;    fn enabled(&self, cx: &App) -> bool;    fn move_to_next_position(&self, window: &mut Window, cx: &mut App) {        let current_position = self.position(window, cx);        let next_position = [            DockPosition::Left,            DockPosition::Bottom,            DockPosition::Right,        ]        .into_iter()        .filter(|position| self.position_is_valid(*position, cx))        .skip_while(|valid_position| *valid_position != current_position)        .nth(1)        .unwrap_or(DockPosition::Left);         self.set_position(next_position, window, cx);    }} impl<T> PanelHandle for Entity<T>where    T: Panel,{    fn panel_id(&self) -> EntityId {        Entity::entity_id(self)    }     fn persistent_name(&self) -> &'static str {        T::persistent_name()    }     fn panel_key(&self) -> &'static str {        T::panel_key()    }     fn position(&self, window: &Window, cx: &App) -> DockPosition {        self.read(cx).position(window, cx)    }     fn position_is_valid(&self, position: DockPosition, cx: &App) -> bool {        self.read(cx).position_is_valid(position)    }     fn set_position(&self, position: DockPosition, window: &mut Window, cx: &mut App) {        self.update(cx, |this, cx| this.set_position(position, window, cx))    }     fn is_zoomed(&self, window: &Window, cx: &App) -> bool {        self.read(cx).is_zoomed(window, cx)    }     fn set_zoomed(&self, zoomed: bool, window: &mut Window, cx: &mut App) {        self.update(cx, |this, cx| this.set_zoomed(zoomed, window, cx))    }     fn set_active(&self, active: bool, window: &mut Window, cx: &mut App) {        self.update(cx, |this, cx| this.set_active(active, window, cx))    }     fn pane(&self, cx: &App) -> Option<Entity<Pane>> {        self.read(cx).pane()    }     fn remote_id(&self) -> Option<PanelId> {        T::remote_id()    }     fn size(&self, window: &Window, cx: &App) -> Pixels {        self.read(cx).size(window, cx)    }     fn set_size(&self, size: Option<Pixels>, window: &mut Window, cx: &mut App) {        self.update(cx, |this, cx| this.set_size(size, window, cx))    }     fn icon(&self, window: &Window, cx: &App) -> Option<ui::IconName> {        self.read(cx).icon(window, cx)    }     fn icon_tooltip(&self, window: &Window, cx: &App) -> Option<&'static str> {        self.read(cx).icon_tooltip(window, cx)    }     fn toggle_action(&self, _: &Window, cx: &App) -> Box<dyn Action> {        self.read(cx).toggle_action()    }     fn icon_label(&self, window: &Window, cx: &App) -> Option<String> {        self.read(cx).icon_label(window, cx)    }     fn to_any(&self) -> AnyView {        self.clone().into()    }     fn panel_focus_handle(&self, cx: &App) -> FocusHandle {        self.read(cx).focus_handle(cx)    }     fn activation_priority(&self, cx: &App) -> u32 {        self.read(cx).activation_priority()    }     fn enabled(&self, cx: &App) -> bool {        self.read(cx).enabled(cx)    }} impl From<&dyn PanelHandle> for AnyView {    fn from(val: &dyn PanelHandle) -> Self {        val.to_any()    }} /// A container with a fixed [`DockPosition`] adjacent to a certain widown edge./// Can contain multiple panels and show/hide itself with all contents.pub struct Dock {    position: DockPosition,    panel_entries: Vec<PanelEntry>,    workspace: WeakEntity<Workspace>,    is_open: bool,    active_panel_index: Option<usize>,    focus_handle: FocusHandle,    pub(crate) serialized_dock: Option<DockData>,    zoom_layer_open: bool,    modal_layer: Entity<ModalLayer>,    _subscriptions: [Subscription; 2],} impl Focusable for Dock {    fn focus_handle(&self, _: &App) -> FocusHandle {        self.focus_handle.clone()    }} #[derive(Copy, Clone, Debug, PartialEq, Eq)]pub enum DockPosition {    Left,    Bottom,    Right,} impl From<settings::DockPosition> for DockPosition {    fn from(value: settings::DockPosition) -> Self {        match value {            settings::DockPosition::Left => Self::Left,            settings::DockPosition::Bottom => Self::Bottom,            settings::DockPosition::Right => Self::Right,        }    }} impl Into<settings::DockPosition> for DockPosition {    fn into(self) -> settings::DockPosition {        match self {            Self::Left => settings::DockPosition::Left,            Self::Bottom => settings::DockPosition::Bottom,            Self::Right => settings::DockPosition::Right,        }    }} impl DockPosition {    fn label(&self) -> &'static str {        match self {            Self::Left => "Left",            Self::Bottom => "Bottom",            Self::Right => "Right",        }    }     pub fn axis(&self) -> Axis {        match self {            Self::Left | Self::Right => Axis::Horizontal,            Self::Bottom => Axis::Vertical,        }    }} struct PanelEntry {    panel: Arc<dyn PanelHandle>,    _subscriptions: [Subscription; 3],} pub struct PanelButtons {    dock: Entity<Dock>,    _settings_subscription: Subscription,} impl Dock {    pub fn new(        position: DockPosition,        modal_layer: Entity<ModalLayer>,        window: &mut Window,        cx: &mut Context<Workspace>,    ) -> Entity<Self> {        let focus_handle = cx.focus_handle();        let workspace = cx.entity();        let dock = cx.new(|cx| {            let focus_subscription =                cx.on_focus(&focus_handle, window, |dock: &mut Dock, window, cx| {                    if let Some(active_entry) = dock.active_panel_entry() {                        active_entry.panel.panel_focus_handle(cx).focus(window, cx)                    }                });            let zoom_subscription = cx.subscribe(&workspace, |dock, workspace, e: &Event, cx| {                if matches!(e, Event::ZoomChanged) {                    let is_zoomed = workspace.read(cx).zoomed.is_some();                    dock.zoom_layer_open = is_zoomed;                }            });            Self {                position,                workspace: workspace.downgrade(),                panel_entries: Default::default(),                active_panel_index: None,                is_open: false,                focus_handle: focus_handle.clone(),                _subscriptions: [focus_subscription, zoom_subscription],                serialized_dock: None,                zoom_layer_open: false,                modal_layer,            }        });         cx.on_focus_in(&focus_handle, window, {            let dock = dock.downgrade();            move |workspace, window, cx| {                let Some(dock) = dock.upgrade() else {                    return;                };                let Some(panel) = dock.read(cx).active_panel() else {                    return;                };                if panel.is_zoomed(window, cx) {                    workspace.zoomed = Some(panel.to_any().downgrade());                    workspace.zoomed_position = Some(position);                } else {                    workspace.zoomed = None;                    workspace.zoomed_position = None;                }                cx.emit(Event::ZoomChanged);                workspace.dismiss_zoomed_items_to_reveal(Some(position), window, cx);                workspace.update_active_view_for_followers(window, cx)            }        })        .detach();         cx.observe_in(&dock, window, move |workspace, dock, window, cx| {            if dock.read(cx).is_open()                && let Some(panel) = dock.read(cx).active_panel()                && panel.is_zoomed(window, cx)            {                workspace.zoomed = Some(panel.to_any().downgrade());                workspace.zoomed_position = Some(position);                cx.emit(Event::ZoomChanged);                return;            }            if workspace.zoomed_position == Some(position) {                workspace.zoomed = None;                workspace.zoomed_position = None;                cx.emit(Event::ZoomChanged);            }        })        .detach();         dock    }     pub fn position(&self) -> DockPosition {        self.position    }     pub fn is_open(&self) -> bool {        self.is_open    }     fn resizable(&self, cx: &App) -> bool {        !(self.zoom_layer_open || self.modal_layer.read(cx).has_active_modal())    }     pub fn panel<T: Panel>(&self) -> Option<Entity<T>> {        self.panel_entries            .iter()            .find_map(|entry| entry.panel.to_any().downcast().ok())    }     pub fn panel_index_for_type<T: Panel>(&self) -> Option<usize> {        self.panel_entries            .iter()            .position(|entry| entry.panel.to_any().downcast::<T>().is_ok())    }     pub fn panel_index_for_persistent_name(&self, ui_name: &str, _cx: &App) -> Option<usize> {        self.panel_entries            .iter()            .position(|entry| entry.panel.persistent_name() == ui_name)    }     pub fn panel_index_for_proto_id(&self, panel_id: PanelId) -> Option<usize> {        self.panel_entries            .iter()            .position(|entry| entry.panel.remote_id() == Some(panel_id))    }     pub fn panel_for_id(&self, panel_id: EntityId) -> Option<&Arc<dyn PanelHandle>> {        self.panel_entries            .iter()            .find(|entry| entry.panel.panel_id() == panel_id)            .map(|entry| &entry.panel)    }     pub fn first_enabled_panel_idx(&mut self, cx: &mut Context<Self>) -> anyhow::Result<usize> {        self.panel_entries            .iter()            .position(|entry| entry.panel.enabled(cx))            .with_context(|| {                format!(                    "Couldn't find any enabled panel for the {} dock.",                    self.position.label()                )            })    }     fn active_panel_entry(&self) -> Option<&PanelEntry> {        self.active_panel_index            .and_then(|index| self.panel_entries.get(index))    }     pub fn active_panel_index(&self) -> Option<usize> {        self.active_panel_index    }     pub fn set_open(&mut self, open: bool, window: &mut Window, cx: &mut Context<Self>) {        if open != self.is_open {            self.is_open = open;            if let Some(active_panel) = self.active_panel_entry() {                active_panel.panel.set_active(open, window, cx);            }             cx.notify();        }    }     pub fn set_panel_zoomed(        &mut self,        panel: &AnyView,        zoomed: bool,        window: &mut Window,        cx: &mut Context<Self>,    ) {

Lines 1–500 of 1193.