layout: replace panel descriptors with panel trait
This commit is contained in:
+75
-84
@@ -3,6 +3,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
@@ -27,65 +28,59 @@ const MIN_SPLIT_RATIO: f32 = 0.1;
|
||||
/// The fraction a split opens at, and what a double-click on the seam restores.
|
||||
const DEFAULT_SPLIT_RATIO: f32 = 0.5;
|
||||
|
||||
/// Builds the view a panel opens as, once per open.
|
||||
type PanelBuilder = Box<dyn Fn(&mut Window, &mut App) -> AnyView>;
|
||||
/// Builds a panel's view entity the first time its tab opens.
|
||||
type PanelBuilder = Box<dyn Fn(&mut Window, &mut App) -> Arc<dyn PanelView>>;
|
||||
/// Runs when a panel with a right sidebar activates, so the app can open it.
|
||||
type OpenRightSidebarHandler = Rc<dyn Fn(&mut App)>;
|
||||
|
||||
/// A registered panel: its identity, chrome, and how its view is built.
|
||||
#[must_use]
|
||||
pub struct PanelDescriptor {
|
||||
id: SharedString,
|
||||
title: Option<SharedString>,
|
||||
lead: Option<Rc<dyn Fn() -> AnyElement>>,
|
||||
build: PanelBuilder,
|
||||
right_sidebar: Option<PanelBuilder>,
|
||||
}
|
||||
|
||||
impl PanelDescriptor {
|
||||
/// Creates a panel descriptor that opens through `build`.
|
||||
pub fn new<I, F>(id: I, build: F) -> Self
|
||||
where
|
||||
I: Into<SharedString>,
|
||||
F: Fn(&mut Window, &mut App) -> AnyView + 'static,
|
||||
{
|
||||
Self {
|
||||
id: id.into(),
|
||||
title: None,
|
||||
lead: None,
|
||||
build: Box::new(build),
|
||||
right_sidebar: None,
|
||||
}
|
||||
/// A dockable panel.
|
||||
#[allow(unused_variables)]
|
||||
pub trait Panel: Render + 'static {
|
||||
/// The panel's tab label.
|
||||
fn title(&self, cx: &App) -> Option<SharedString> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Shows `title` as the panel's tab label.
|
||||
pub fn title<T>(mut self, title: T) -> Self
|
||||
where
|
||||
T: Into<SharedString>,
|
||||
{
|
||||
self.title = Some(title.into());
|
||||
self
|
||||
/// An element, e.g. an icon or avatar, leading the tab label.
|
||||
fn lead(&self, cx: &App) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Shows an element, e.g. an icon or avatar, leading the tab label.
|
||||
pub fn lead<F, E>(mut self, build: F) -> Self
|
||||
where
|
||||
F: Fn() -> E + 'static,
|
||||
E: IntoElement,
|
||||
{
|
||||
self.lead = Some(Rc::new(move || build().into_any_element()));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the view the right sidebar shows while this panel is active.
|
||||
/// The view the right sidebar shows while this panel is active.
|
||||
///
|
||||
/// Activating the panel opens the right sidebar.
|
||||
pub fn right_sidebar<F>(mut self, build: F) -> Self
|
||||
where
|
||||
F: Fn(&mut Window, &mut App) -> AnyView + 'static,
|
||||
{
|
||||
self.right_sidebar = Some(Box::new(build));
|
||||
self
|
||||
fn right_sidebar(&mut self, window: &mut Window, cx: &mut App) -> Option<AnyView> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Object-safe handle to an open panel entity.
|
||||
pub trait PanelView: 'static {
|
||||
/// The panel's view.
|
||||
fn view(&self) -> AnyView;
|
||||
/// The tab label, read live from the panel.
|
||||
fn title(&self, cx: &App) -> Option<SharedString>;
|
||||
/// The element leading the tab label, read live from the panel.
|
||||
fn lead(&self, cx: &App) -> Option<AnyElement>;
|
||||
/// The right sidebar view, built through the panel.
|
||||
fn right_sidebar(&self, window: &mut Window, cx: &mut App) -> Option<AnyView>;
|
||||
}
|
||||
|
||||
impl<T: Panel> PanelView for Entity<T> {
|
||||
fn view(&self) -> AnyView {
|
||||
self.clone().into()
|
||||
}
|
||||
|
||||
fn title(&self, cx: &App) -> Option<SharedString> {
|
||||
self.read(cx).title(cx)
|
||||
}
|
||||
|
||||
fn lead(&self, cx: &App) -> Option<AnyElement> {
|
||||
self.read(cx).lead(cx)
|
||||
}
|
||||
|
||||
fn right_sidebar(&self, window: &mut Window, cx: &mut App) -> Option<AnyView> {
|
||||
self.update(cx, |panel, cx| panel.right_sidebar(window, cx))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,12 +135,10 @@ impl Render for DragPreview {
|
||||
}
|
||||
}
|
||||
|
||||
/// One open panel view.
|
||||
/// One open panel: its dock id, live handle, and right sidebar view.
|
||||
struct PanelTab {
|
||||
panel: SharedString,
|
||||
title: Option<SharedString>,
|
||||
lead: Option<Rc<dyn Fn() -> AnyElement>>,
|
||||
view: AnyView,
|
||||
handle: Arc<dyn PanelView>,
|
||||
right_view: Option<AnyView>,
|
||||
}
|
||||
|
||||
@@ -173,7 +166,7 @@ enum Node {
|
||||
/// The dock's pane tree, owned by the application.
|
||||
pub struct DockState {
|
||||
root: Node,
|
||||
panels: HashMap<SharedString, PanelDescriptor>,
|
||||
panels: HashMap<SharedString, PanelBuilder>,
|
||||
/// The pane new panels open into, kept on the last activated pane.
|
||||
active_pane: u64,
|
||||
next_key: u64,
|
||||
@@ -205,9 +198,18 @@ impl DockState {
|
||||
self.on_open_right_sidebar = Some(Rc::new(handler));
|
||||
}
|
||||
|
||||
/// Registers a panel descriptor, replacing any earlier one with its id.
|
||||
pub fn register_panel(&mut self, panel: PanelDescriptor) {
|
||||
self.panels.insert(panel.id.clone(), panel);
|
||||
/// Registers a panel under `id`, replacing any earlier one with it.
|
||||
pub fn register_panel<I, F, P>(&mut self, id: I, build: F)
|
||||
where
|
||||
I: Into<SharedString>,
|
||||
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
|
||||
P: Panel,
|
||||
{
|
||||
let build: PanelBuilder = Box::new(move |window, cx| {
|
||||
let panel = build(window, cx);
|
||||
Arc::new(panel)
|
||||
});
|
||||
self.panels.insert(id.into(), build);
|
||||
}
|
||||
|
||||
/// Opens a panel into the active pane and activates its tab.
|
||||
@@ -217,20 +219,16 @@ impl DockState {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(panel) = self.panels.get(id) else {
|
||||
let Some(build) = self.panels.get(id) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let right_view = panel
|
||||
.right_sidebar
|
||||
.as_ref()
|
||||
.map(|build| (build)(window, cx));
|
||||
let handle = build(window, cx);
|
||||
let right_view = handle.right_sidebar(window, cx);
|
||||
|
||||
let tab = PanelTab {
|
||||
panel: SharedString::from(id),
|
||||
title: panel.title.clone(),
|
||||
lead: panel.lead.clone(),
|
||||
view: (panel.build)(window, cx),
|
||||
handle,
|
||||
right_view,
|
||||
};
|
||||
|
||||
@@ -692,9 +690,7 @@ impl DockState {
|
||||
.iter()
|
||||
.map(|tab| TabView {
|
||||
panel: tab.panel.clone(),
|
||||
title: tab.title.clone(),
|
||||
lead: tab.lead.clone(),
|
||||
view: tab.view.clone(),
|
||||
handle: tab.handle.clone(),
|
||||
})
|
||||
.collect(),
|
||||
active: pane.active.clone(),
|
||||
@@ -750,13 +746,11 @@ impl DockState {
|
||||
}
|
||||
}
|
||||
|
||||
/// One open panel view, per frame.
|
||||
/// One open panel's handle, per frame.
|
||||
#[derive(Clone)]
|
||||
struct TabView {
|
||||
panel: SharedString,
|
||||
title: Option<SharedString>,
|
||||
lead: Option<Rc<dyn Fn() -> AnyElement>>,
|
||||
view: AnyView,
|
||||
handle: Arc<dyn PanelView>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -1013,7 +1007,7 @@ fn render_pane(
|
||||
.tabs
|
||||
.iter()
|
||||
.find(|tab| Some(&tab.panel) == pane.active.as_ref())
|
||||
.map(|tab| tab.view.clone());
|
||||
.map(|tab| tab.handle.view());
|
||||
|
||||
// The cache keeps an un-notified panel from re-rendering on dock frames.
|
||||
let content_id = SharedString::from(format!("{dock_id}-content-{}", pane.key));
|
||||
@@ -1056,28 +1050,25 @@ fn render_pane(
|
||||
.selected(selected)
|
||||
.tabs({
|
||||
let tabs = pane.tabs.clone();
|
||||
move |_, _| {
|
||||
move |_, cx| {
|
||||
tabs.iter()
|
||||
.map(|tab| {
|
||||
Tab::new(tab.panel.clone(), tab.title.clone().unwrap_or_default())
|
||||
.when_some(tab.lead.as_ref().map(|build| build()), |entry, lead| {
|
||||
entry.lead(lead)
|
||||
})
|
||||
Tab::new(tab.panel.clone(), tab.handle.title(cx).unwrap_or_default())
|
||||
.when_some(tab.handle.lead(cx), |entry, lead| entry.lead(lead))
|
||||
.closeable(true)
|
||||
.wrap({
|
||||
let panel = tab.panel.clone();
|
||||
let title = tab.title.clone();
|
||||
let handle = tab.handle.clone();
|
||||
move |tab| {
|
||||
tab.on_drag(
|
||||
PanelDrag {
|
||||
panel: panel.clone(),
|
||||
},
|
||||
{
|
||||
let title = title.clone();
|
||||
let handle = handle.clone();
|
||||
move |_, _, _, cx| {
|
||||
cx.new(|_| DragPreview {
|
||||
title: title.clone(),
|
||||
})
|
||||
let title = handle.title(cx);
|
||||
cx.new(move |_| DragPreview { title })
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user