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());
+11 -9
View File
@@ -4,11 +4,11 @@
//! half-width islands with a drag divider that resizes them.
use gpui::{
AnyView, AppContext as _, Context, InteractiveElement as _, IntoElement, Modifiers,
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, Render,
SharedString, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px, size,
AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, Modifiers, MouseButton,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, Render, SharedString,
Styled as _, TestAppContext, VisualTestContext, Window, div, point, px, size,
};
use gpui_native_layout::panel::Panel;
use gpui_native_layout::panel::{Panel, PanelRow};
use gpui_native_theme::Theme;
const WINDOW_WIDTH: f32 = 1024.0;
@@ -36,15 +36,17 @@ impl Render for TagView {
}
}
impl Panel for TagView {}
struct PanelHarness {
split: bool,
main: AnyView,
split_view: AnyView,
main: Entity<TagView>,
split_view: Entity<TagView>,
}
impl Render for PanelHarness {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
let panel = Panel::new("panel-harness").main(self.main.clone());
let panel = PanelRow::new("panel-harness").main(self.main.clone());
let panel = if self.split {
panel.split(self.split_view.clone())
} else {
@@ -65,8 +67,8 @@ fn panel_window(cx: &mut TestAppContext, split: bool) -> VisualTestContext {
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
PanelHarness {
split,
main: main.into(),
split_view: split_view.into(),
main,
split_view,
}
});
let cx = VisualTestContext::from_window(window.into(), cx);