Files
gpui-native-kit/crates/layout/src/panel.rs
T
2026-11-15 12:50:57 +07:00

145 lines
3.9 KiB
Rust

//! The panel row: the window's main content, island-style over the app
//! background. It can split once into two half-width panels with a drag
//! 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, 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()
}
/// The island chrome every panel paints.
fn island(panel: Div, cx: &App) -> Div {
let theme = cx.theme();
let colors = theme.colors;
panel
.size_full()
.bg(colors.background)
.p_2()
.border_1()
.border_color(colors.border)
.rounded(theme.radius.overlay)
}
/// The panel row drawn under the titlebar.
#[derive(IntoElement)]
#[must_use]
pub struct PanelRow {
id: ElementId,
main: Option<AnyElement>,
split: Option<AnyElement>,
}
impl PanelRow {
/// Creates the panel row.
pub fn new<I>(id: I) -> Self
where
I: Into<ElementId>,
{
Self {
id: id.into(),
main: None,
split: None,
}
}
/// Sets the main panel's content.
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<P>(mut self, panel: P) -> Self
where
P: PanelView,
{
self.split = Some(panel.view().cached(content_style()).into_any_element());
self
}
}
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());
let main = island(
div()
.size_full()
.child(self.main.unwrap_or_else(|| div().into_any_element())),
cx,
);
let row = match self.split {
Some(split) => {
let sizes = window.with_element_namespace(id.clone(), |window| {
window
.use_keyed_state("panel-sizes", cx, |_, _| ResizableState::default())
.clone()
});
let extent = window.viewport_size().width - gutter * 2.0;
if sizes.read(cx).sizes().is_empty() {
sizes.update(cx, |state, cx| {
state.set_pane_width(0, (extent - gutter) / 2.0, cx)
});
}
div().w_full().h_full().child(
Resizable::new((id.clone(), "split"), sizes)
.axis(Axis::Horizontal)
.pane(main)
.pane(island(div().size_full().child(split), cx))
.h_full(),
)
}
None => div().w_full().h_full().child(main),
};
div()
.flex_1()
.min_w_0()
.h_full()
// the titlebar's centered tabs leave slack above the strip's
// bottom edge, so the top gutter is narrower to even out.
.p_2()
.pt_1()
.child(row)
}
}