layout: add panel trait

This commit is contained in:
2026-11-15 12:50:57 +07:00
parent d2ac4c446d
commit f6ba9c0d47
4 changed files with 233 additions and 134 deletions
+38 -10
View File
@@ -3,14 +3,36 @@
//! divider between them; dragging the seam resizes the panels and a
//! double-click restores the half split.
use std::sync::Arc;
use gpui::{
AnyElement, AnyView, App, Axis, Div, ElementId, IntoElement, ParentElement as _, RenderOnce,
StyleRefinement, Styled as _, Window, div, rems,
AnyElement, AnyView, App, Axis, Div, ElementId, Entity, IntoElement, ParentElement as _,
Render, RenderOnce, StyleRefinement, Styled as _, Window, div, rems,
};
use gpui_native_theme::ActiveTheme as _;
use crate::resizable::{Resizable, ResizableState};
pub trait Panel: Render + 'static {}
/// An object-safe handle to a panel, so apps can hold panels by kind.
pub trait PanelView: 'static {
/// The panel's view.
fn view(&self) -> AnyView;
}
impl<T: Panel> PanelView for Entity<T> {
fn view(&self) -> AnyView {
self.clone().into()
}
}
impl<P: PanelView + ?Sized> PanelView for Arc<P> {
fn view(&self) -> AnyView {
(**self).view()
}
}
/// The layout the cached panel content lays out with.
fn content_style() -> StyleRefinement {
StyleRefinement::default().size_full()
@@ -33,13 +55,13 @@ fn island(panel: Div, cx: &App) -> Div {
/// The panel row drawn under the titlebar.
#[derive(IntoElement)]
#[must_use]
pub struct Panel {
pub struct PanelRow {
id: ElementId,
main: Option<AnyElement>,
split: Option<AnyElement>,
}
impl Panel {
impl PanelRow {
/// Creates the panel row.
pub fn new<I>(id: I) -> Self
where
@@ -53,19 +75,25 @@ impl Panel {
}
/// Sets the main panel's content.
pub fn main(mut self, main: AnyView) -> Self {
self.main = Some(main.cached(content_style()).into_any_element());
pub fn main<P>(mut self, main: P) -> Self
where
P: PanelView,
{
self.main = Some(main.view().cached(content_style()).into_any_element());
self
}
/// Splits the row into two half-width panels,
pub fn split(mut self, panel: AnyView) -> Self {
self.split = Some(panel.cached(content_style()).into_any_element());
/// Splits the row into two half-width panels.
pub fn split<P>(mut self, panel: P) -> Self
where
P: PanelView,
{
self.split = Some(panel.view().cached(content_style()).into_any_element());
self
}
}
impl RenderOnce for Panel {
impl RenderOnce for PanelRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let gutter = rems(0.5).to_pixels(window.rem_size());