Skip to main content

ui/components/pane_group/
preview.rs

1//! Gallery catalog entry for [`PaneGroup`]. Kept separate from the
2//! interactive `ui_gallery` Layout-page demo (which owns a persistent
3//! `Entity<PaneGroup>` on `GalleryApp`, created once in `new()`) — this is a
4//! throwaway static example for the `Component` catalog, mirroring
5//! `ResizablePreview`'s equivalent split from `GalleryApp::resizable`.
6
7use gpui::AnyElement;
8
9use super::PaneGroup;
10use crate::{Pane, SplitDirection, TabContent, prelude::*};
11
12struct DemoTab(&'static str);
13
14impl TabContent for DemoTab {
15    fn render(&self, _focused: bool, _window: &mut Window, _cx: &mut App) -> AnyElement {
16        div()
17            .p_4()
18            .child(Label::new(self.0).color(Color::Muted))
19            .into_any_element()
20    }
21
22    fn title(&self) -> SharedString {
23        self.0.into()
24    }
25}
26
27/// Gallery catalog entry for [`PaneGroup`]: a two-pane horizontal split,
28/// each with one demo tab.
29#[derive(IntoElement, RegisterComponent)]
30pub struct PaneGroupPreview;
31
32impl RenderOnce for PaneGroupPreview {
33    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
34        div().h(px(280.)).child(cx.new(|cx| {
35            let first = cx.new(|cx| {
36                let mut pane = Pane::new().with_new_tab_factory(|| Box::new(DemoTab("New tab")));
37                pane.add_tab(Box::new(DemoTab("main.rs")), cx);
38                pane
39            });
40            let mut group = PaneGroup::new(cx, first).with_pane_factory(|cx| {
41                let mut pane = Pane::new().with_new_tab_factory(|| Box::new(DemoTab("New tab")));
42                pane.add_tab(Box::new(DemoTab("README.md")), cx);
43                pane
44            });
45            group.split(SplitDirection::Right, cx);
46            group
47        }))
48    }
49}
50
51impl Component for PaneGroupPreview {
52    fn scope() -> ComponentScope {
53        ComponentScope::Layout
54    }
55
56    fn description() -> Option<&'static str> {
57        Some(
58            "Recursive split-tree pane group: N-way horizontal/vertical splits, \
59             per-pane tabs, and both-axis drag-resize.",
60        )
61    }
62
63    fn preview(window: &mut Window, cx: &mut App) -> Option<AnyElement> {
64        PaneGroupPreview
65            .render(window, cx)
66            .into_any_element()
67            .into()
68    }
69}