955 lines · 32.3 KB · Rust
RawDownload
use crate::persistence::model::DockData;use crate::{status_bar::StatusItemView, Workspace};use crate::{DraggedDock, Event, Pane};use client::proto;use gpui::{    deferred, div, px, Action, AnyView, AppContext, Axis, Corner, Entity, EntityId, EventEmitter,    FocusHandle, FocusableView, IntoElement, KeyContext, MouseButton, MouseDownEvent, MouseUpEvent,    ParentElement, Render, SharedString, StyleRefinement, Styled, Subscription, View, ViewContext,    VisualContext, WeakView, WindowContext,};use schemars::JsonSchema;use serde::{Deserialize, Serialize};use settings::SettingsStore;use std::sync::Arc;use ui::{h_flex, ContextMenu, IconButton, Tooltip};use ui::{prelude::*, right_click_menu}; pub(crate) const RESIZE_HANDLE_SIZE: Pixels = Pixels(6.); pub enum PanelEvent {    ZoomIn,    ZoomOut,    Activate,    Close,} pub use proto::PanelId; pub trait Panel: FocusableView + EventEmitter<PanelEvent> {    fn persistent_name() -> &'static str;    fn position(&self, cx: &WindowContext) -> DockPosition;    fn position_is_valid(&self, position: DockPosition) -> bool;    fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);    fn size(&self, cx: &WindowContext) -> Pixels;    fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>);    fn icon(&self, cx: &WindowContext) -> Option<ui::IconName>;    fn icon_tooltip(&self, cx: &WindowContext) -> Option<&'static str>;    fn toggle_action(&self) -> Box<dyn Action>;    fn icon_label(&self, _: &WindowContext) -> Option<String> {        None    }    fn is_zoomed(&self, _cx: &WindowContext) -> bool {        false    }    fn starts_open(&self, _cx: &WindowContext) -> bool {        false    }    fn set_zoomed(&mut self, _zoomed: bool, _cx: &mut ViewContext<Self>) {}    fn set_active(&mut self, _active: bool, _cx: &mut ViewContext<Self>) {}    fn pane(&self) -> Option<View<Pane>> {        None    }    fn remote_id() -> Option<proto::PanelId> {        None    }    fn activation_priority(&self) -> u32;} pub trait PanelHandle: Send + Sync {    fn panel_id(&self) -> EntityId;    fn persistent_name(&self) -> &'static str;    fn position(&self, cx: &WindowContext) -> DockPosition;    fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;    fn set_position(&self, position: DockPosition, cx: &mut WindowContext);    fn is_zoomed(&self, cx: &WindowContext) -> bool;    fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);    fn set_active(&self, active: bool, cx: &mut WindowContext);    fn remote_id(&self) -> Option<proto::PanelId>;    fn pane(&self, cx: &WindowContext) -> Option<View<Pane>>;    fn size(&self, cx: &WindowContext) -> Pixels;    fn set_size(&self, size: Option<Pixels>, cx: &mut WindowContext);    fn icon(&self, cx: &WindowContext) -> Option<ui::IconName>;    fn icon_tooltip(&self, cx: &WindowContext) -> Option<&'static str>;    fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action>;    fn icon_label(&self, cx: &WindowContext) -> Option<String>;    fn focus_handle(&self, cx: &AppContext) -> FocusHandle;    fn to_any(&self) -> AnyView;    fn activation_priority(&self, cx: &AppContext) -> u32;    fn move_to_next_position(&self, cx: &mut WindowContext) {        let current_position = self.position(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, cx);    }} impl<T> PanelHandle for View<T>where    T: Panel,{    fn panel_id(&self) -> EntityId {        Entity::entity_id(self)    }     fn persistent_name(&self) -> &'static str {        T::persistent_name()    }     fn position(&self, cx: &WindowContext) -> DockPosition {        self.read(cx).position(cx)    }     fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {        self.read(cx).position_is_valid(position)    }     fn set_position(&self, position: DockPosition, cx: &mut WindowContext) {        self.update(cx, |this, cx| this.set_position(position, cx))    }     fn is_zoomed(&self, cx: &WindowContext) -> bool {        self.read(cx).is_zoomed(cx)    }     fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext) {        self.update(cx, |this, cx| this.set_zoomed(zoomed, cx))    }     fn set_active(&self, active: bool, cx: &mut WindowContext) {        self.update(cx, |this, cx| this.set_active(active, cx))    }     fn pane(&self, cx: &WindowContext) -> Option<View<Pane>> {        self.read(cx).pane()    }     fn remote_id(&self) -> Option<PanelId> {        T::remote_id()    }     fn size(&self, cx: &WindowContext) -> Pixels {        self.read(cx).size(cx)    }     fn set_size(&self, size: Option<Pixels>, cx: &mut WindowContext) {        self.update(cx, |this, cx| this.set_size(size, cx))    }     fn icon(&self, cx: &WindowContext) -> Option<ui::IconName> {        self.read(cx).icon(cx)    }     fn icon_tooltip(&self, cx: &WindowContext) -> Option<&'static str> {        self.read(cx).icon_tooltip(cx)    }     fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action> {        self.read(cx).toggle_action()    }     fn icon_label(&self, cx: &WindowContext) -> Option<String> {        self.read(cx).icon_label(cx)    }     fn to_any(&self) -> AnyView {        self.clone().into()    }     fn focus_handle(&self, cx: &AppContext) -> FocusHandle {        self.read(cx).focus_handle(cx).clone()    }     fn activation_priority(&self, cx: &AppContext) -> u32 {        self.read(cx).activation_priority()    }} 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: WeakView<Workspace>,    is_open: bool,    active_panel_index: Option<usize>,    focus_handle: FocusHandle,    pub(crate) serialized_dock: Option<DockData>,    resizeable: bool,    _subscriptions: [Subscription; 2],} impl FocusableView for Dock {    fn focus_handle(&self, _: &AppContext) -> FocusHandle {        self.focus_handle.clone()    }} #[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]#[serde(rename_all = "lowercase")]pub enum DockPosition {    Left,    Bottom,    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: View<Dock>,} impl Dock {    pub fn new(position: DockPosition, cx: &mut ViewContext<Workspace>) -> View<Self> {        let focus_handle = cx.focus_handle();        let workspace = cx.view().clone();        let dock = cx.new_view(|cx: &mut ViewContext<Self>| {            let focus_subscription = cx.on_focus(&focus_handle, |dock, cx| {                if let Some(active_entry) = dock.active_panel_entry() {                    active_entry.panel.focus_handle(cx).focus(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.resizeable = !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,                resizeable: true,            }        });         cx.on_focus_in(&focus_handle, {            let dock = dock.downgrade();            move |workspace, cx| {                let Some(dock) = dock.upgrade() else {                    return;                };                let Some(panel) = dock.read(cx).active_panel() else {                    return;                };                if panel.is_zoomed(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), cx);                workspace.update_active_view_for_followers(cx)            }        })        .detach();         cx.observe(&dock, move |workspace, dock, cx| {            if dock.read(cx).is_open() {                if let Some(panel) = dock.read(cx).active_panel() {                    if panel.is_zoomed(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    }     pub fn panel<T: Panel>(&self) -> Option<View<T>> {        self.panel_entries            .iter()            .find_map(|entry| entry.panel.to_any().clone().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: &AppContext,    ) -> 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))    }     fn active_panel_entry(&self) -> Option<&PanelEntry> {        self.active_panel_index            .and_then(|index| self.panel_entries.get(index))    }     pub(crate) fn set_open(&mut self, open: bool, cx: &mut ViewContext<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, cx);            }             cx.notify();        }    }     pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) {        for entry in &mut self.panel_entries {            if entry.panel.panel_id() == panel.entity_id() {                if zoomed != entry.panel.is_zoomed(cx) {                    entry.panel.set_zoomed(zoomed, cx);                }            } else if entry.panel.is_zoomed(cx) {                entry.panel.set_zoomed(false, cx);            }        }         self.workspace            .update(cx, |workspace, cx| {                workspace.serialize_workspace(cx);            })            .ok();        cx.notify();    }     pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {        for entry in &mut self.panel_entries {            if entry.panel.is_zoomed(cx) {                entry.panel.set_zoomed(false, cx);            }        }    }     pub(crate) fn add_panel<T: Panel>(        &mut self,        panel: View<T>,        workspace: WeakView<Workspace>,        cx: &mut ViewContext<Self>,    ) -> usize {        let subscriptions = [            cx.observe(&panel, |_, _, cx| cx.notify()),            cx.observe_global::<SettingsStore>({                let workspace = workspace.clone();                let panel = panel.clone();                 move |this, cx| {                    let new_position = panel.read(cx).position(cx);                    if new_position == this.position {                        return;                    }                     let Ok(new_dock) = workspace.update(cx, |workspace, cx| {                        if panel.is_zoomed(cx) {                            workspace.zoomed_position = Some(new_position);                        }                        match new_position {                            DockPosition::Left => &workspace.left_dock,                            DockPosition::Bottom => &workspace.bottom_dock,                            DockPosition::Right => &workspace.right_dock,                        }                        .clone()                    }) else {                        return;                    };                     let was_visible = this.is_open()                        && this.visible_panel().map_or(false, |active_panel| {                            active_panel.panel_id() == Entity::entity_id(&panel)                        });                     this.remove_panel(&panel, cx);                     new_dock.update(cx, |new_dock, cx| {                        new_dock.remove_panel(&panel, cx);                        let index = new_dock.add_panel(panel.clone(), workspace.clone(), cx);                        if was_visible {                            new_dock.set_open(true, cx);                            new_dock.activate_panel(index, cx);                        }                    });                }            }),            cx.subscribe(&panel, move |this, panel, event, cx| match event {                PanelEvent::ZoomIn => {                    this.set_panel_zoomed(&panel.to_any(), true, cx);                    if !panel.focus_handle(cx).contains_focused(cx) {                        cx.focus_view(&panel);                    }                    workspace                        .update(cx, |workspace, cx| {                            workspace.zoomed = Some(panel.downgrade().into());                            workspace.zoomed_position = Some(panel.read(cx).position(cx));                            cx.emit(Event::ZoomChanged);                        })                        .ok();                }                PanelEvent::ZoomOut => {                    this.set_panel_zoomed(&panel.to_any(), false, cx);                    workspace                        .update(cx, |workspace, cx| {                            if workspace.zoomed_position == Some(this.position) {                                workspace.zoomed = None;                                workspace.zoomed_position = None;                                cx.emit(Event::ZoomChanged);                            }                            cx.notify();                        })                        .ok();                }                PanelEvent::Activate => {                    if let Some(ix) = this                        .panel_entries                        .iter()                        .position(|entry| entry.panel.panel_id() == Entity::entity_id(&panel))                    {                        this.set_open(true, cx);                        this.activate_panel(ix, cx);                        cx.focus_view(&panel);                    }                }                PanelEvent::Close => {                    if this                        .visible_panel()                        .map_or(false, |p| p.panel_id() == Entity::entity_id(&panel))                    {                        this.set_open(false, cx);                    }                }            }),        ];         let index = match self            .panel_entries            .binary_search_by_key(&panel.read(cx).activation_priority(), |entry| {                entry.panel.activation_priority(cx)            }) {            Ok(ix) => ix,            Err(ix) => ix,        };        if let Some(active_index) = self.active_panel_index.as_mut() {            if *active_index >= index {                *active_index += 1;            }        }

Lines 1–500 of 955.