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
+180 -113
View File
@@ -4,10 +4,10 @@ use std::time::Duration;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyView, App, AppContext as _, Axis, Bounds, Context, Entity, FontWeight,
InteractiveElement as _, IntoElement, KeyBinding, ListAlignment, ListState, ParentElement as _,
Pixels, Render, SharedString, Styled as _, UniformListScrollHandle, WeakEntity, Window,
WindowBounds, WindowOptions, actions, div, list, px, rems, size, uniform_list,
App, AppContext as _, Axis, Bounds, Context, Entity, FontWeight, InteractiveElement as _,
IntoElement, KeyBinding, ListAlignment, ListState, ParentElement as _, Pixels, Render,
SharedString, Styled as _, UniformListScrollHandle, WeakEntity, Window, WindowBounds,
WindowOptions, actions, div, list, px, rems, size, uniform_list,
};
use gpui_native_core::alert::{Alert, AlertSeverity, Alerts};
use gpui_native_core::avatar::Avatar;
@@ -50,7 +50,7 @@ use gpui_native_editor::diff::{DiffRowKind, DiffViewState};
use gpui_native_editor::highlight::Language;
use gpui_native_editor::markdown::{ListMarker, MarkdownBlock, MarkdownViewState, TextView};
use gpui_native_icons::{IconAssets, IconRole, icon};
use gpui_native_layout::panel::Panel;
use gpui_native_layout::panel::{Panel, PanelRow, PanelView};
use gpui_native_layout::resizable::{Resizable, ResizableState};
use gpui_native_layout::resize_handle::ResizeHandle;
use gpui_native_layout::shell::Shell;
@@ -226,9 +226,9 @@ struct Gallery {
tabs: Vec<SharedString>,
active: Option<SharedString>,
/// Created demo views, kept so state survives tab switches.
views: HashMap<SharedString, AnyView>,
views: HashMap<SharedString, Arc<dyn PanelView>>,
/// The panel the row is split with, if any.
split_panel: Option<AnyView>,
split_panel: Option<Arc<dyn PanelView>>,
palette: Option<Entity<CommandPaletteState>>,
palette_runs: Vec<SharedString>,
dialog_open: bool,
@@ -259,19 +259,19 @@ impl Gallery {
}
/// The panel row: the active demo, split with the opened sub panel.
fn render_panel(&self) -> Panel {
let panel = match self
fn render_panel(&self) -> PanelRow {
let row = match self
.active
.as_ref()
.and_then(|active| self.views.get(active).cloned())
{
Some(view) => Panel::new("gallery-panel").main(view),
None => Panel::new("gallery-panel"),
Some(panel) => PanelRow::new("gallery-panel").main(panel),
None => PanelRow::new("gallery-panel"),
};
match &self.split_panel {
Some(split) => panel.split(split.clone()),
None => panel,
Some(split) => row.split(split.clone()),
None => row,
}
}
@@ -433,7 +433,7 @@ impl Gallery {
return;
}
let view = cx.new(|_| column_details());
self.split_panel = Some(view.into());
self.split_panel = Some(Arc::new(view));
cx.notify();
}
@@ -453,111 +453,90 @@ impl Gallery {
gallery: WeakEntity<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Option<AnyView> {
let view: AnyView = match id {
"welcome" => cx.new(|_| WelcomePanel { gallery }).into(),
"button" => cx.new(|_| ButtonDemo { clicks: 0 }).into(),
"input" => cx.new(InputDemo::new).into(),
"field" => cx.new(|cx| FieldDemo::new(window, cx)).into(),
"combobox" => cx.new(|cx| ComboboxDemo::new(window, cx)).into(),
"status-dot" => cx.new(|_| StatusDotDemo).into(),
"checkbox" => cx.new(|_| CheckboxDemo { checked: false }).into(),
"radio" => cx
.new(|_| RadioDemo {
plan: SharedString::from("starter"),
})
.into(),
"switch" => cx.new(|_| SwitchDemo { checked: true }).into(),
"toggle" => cx.new(|_| ToggleDemo { pressed: false }).into(),
"slider" => cx.new(|_| SliderDemo { volume: 40.0 }).into(),
"segmented" => cx
.new(|_| SegmentedDemo {
filter: ExploreFilter::default(),
})
.into(),
"select" => cx
.new(|_| SelectDemo {
strategy: Some(Strategy::Curated),
})
.into(),
"link" => cx.new(|_| LinkDemo { clicks: 0 }).into(),
"clipboard" => cx.new(|_| ClipboardDemo).into(),
"badge" => cx.new(|_| BadgeDemo).into(),
"avatar" => cx.new(|_| AvatarDemo).into(),
"progress" => cx.new(|_| ProgressDemo).into(),
"spinner" => cx.new(|_| SpinnerDemo).into(),
"list" => cx.new(|_| ListDemo { selected: None }).into(),
"tree" => cx
.new(|_| TreeDemo {
nodes: TreeDemo::demo(),
selected: None,
})
.into(),
"tabs" => cx
.new(|_| TabsDemo {
selected: 0,
pill_tabs: vec![
SharedString::from("Explorer"),
SharedString::from("Syncing"),
SharedString::from("Blame"),
],
pill_selected: 0,
lead_selected: 0,
overflow_selected: 0,
overflow_open: false,
})
.into(),
"alert" => cx
.new(|_| AlertDemo {
alerts: self.alerts.clone(),
dismissed: false,
})
.into(),
"toast" => cx
.new(|_| ToastDemo {
toasts: self.toasts.clone(),
})
.into(),
"dialog" => cx.new(|_| DialogDemo { gallery }).into(),
) -> Option<Arc<dyn PanelView>> {
let panel: Arc<dyn PanelView> = match id {
"welcome" => Arc::new(cx.new(|_| WelcomePanel { gallery })),
"button" => Arc::new(cx.new(|_| ButtonDemo { clicks: 0 })),
"input" => Arc::new(cx.new(InputDemo::new)),
"field" => Arc::new(cx.new(|cx| FieldDemo::new(window, cx))),
"combobox" => Arc::new(cx.new(|cx| ComboboxDemo::new(window, cx))),
"status-dot" => Arc::new(cx.new(|_| StatusDotDemo)),
"checkbox" => Arc::new(cx.new(|_| CheckboxDemo { checked: false })),
"radio" => Arc::new(cx.new(|_| RadioDemo {
plan: SharedString::from("starter"),
})),
"switch" => Arc::new(cx.new(|_| SwitchDemo { checked: true })),
"toggle" => Arc::new(cx.new(|_| ToggleDemo { pressed: false })),
"slider" => Arc::new(cx.new(|_| SliderDemo { volume: 40.0 })),
"segmented" => Arc::new(cx.new(|_| SegmentedDemo {
filter: ExploreFilter::default(),
})),
"select" => Arc::new(cx.new(|_| SelectDemo {
strategy: Some(Strategy::Curated),
})),
"link" => Arc::new(cx.new(|_| LinkDemo { clicks: 0 })),
"clipboard" => Arc::new(cx.new(|_| ClipboardDemo)),
"badge" => Arc::new(cx.new(|_| BadgeDemo)),
"avatar" => Arc::new(cx.new(|_| AvatarDemo)),
"progress" => Arc::new(cx.new(|_| ProgressDemo)),
"spinner" => Arc::new(cx.new(|_| SpinnerDemo)),
"list" => Arc::new(cx.new(|_| ListDemo { selected: None })),
"tree" => Arc::new(cx.new(|_| TreeDemo {
nodes: TreeDemo::demo(),
selected: None,
})),
"tabs" => Arc::new(cx.new(|_| TabsDemo {
selected: 0,
pill_tabs: vec![
SharedString::from("Explorer"),
SharedString::from("Syncing"),
SharedString::from("Blame"),
],
pill_selected: 0,
lead_selected: 0,
overflow_selected: 0,
overflow_open: false,
})),
"alert" => Arc::new(cx.new(|_| AlertDemo {
alerts: self.alerts.clone(),
dismissed: false,
})),
"toast" => Arc::new(cx.new(|_| ToastDemo {
toasts: self.toasts.clone(),
})),
"dialog" => Arc::new(cx.new(|_| DialogDemo { gallery })),
"command-palette" => {
let palette = self.palette.clone()?;
cx.new(|cx| CommandPaletteDemo::new(gallery, palette, cx))
.into()
Arc::new(cx.new(|cx| CommandPaletteDemo::new(gallery, palette, cx)))
}
"menu" => cx.new(|_| MenuDemo { open: false }).into(),
"dropdown-menu" => cx
.new(|_| DropdownMenuDemo {
open_menu: None,
status: None,
})
.into(),
"context-menu" => cx
.new(|_| ContextMenuDemo {
action: SharedString::from("Right-click the card"),
})
.into(),
"popover" => cx.new(|_| PopoverDemo { open: false }).into(),
"tooltip" => cx.new(|_| TooltipDemo).into(),
"empty-state" => cx.new(|_| EmptyStateDemo).into(),
"scroll-area" => cx.new(|_| ScrollAreaDemo).into(),
"scrollbar" => cx.new(|_| ScrollbarDemo::new()).into(),
"code-view" => cx.new(CodeViewDemo::new).into(),
"diff-view" => cx.new(DiffViewDemo::new).into(),
"markdown-view" => cx.new(MarkdownViewDemo::new).into(),
"text-view" => cx.new(TextViewDemo::new).into(),
"sidebar" => cx.new(|_| SidebarDemo { selected: None }).into(),
"resize-handle" => cx
.new(|_| ResizeDemo {
width: rems(ResizeDemo::WIDTH_DEFAULT_REMS).to_pixels(window.rem_size()),
})
.into(),
"resizable" => cx.new(ResizableDemo::new).into(),
"separator" => cx.new(|_| SeparatorDemo).into(),
"theme" => cx.new(|_| ThemeDemo).into(),
"menu" => Arc::new(cx.new(|_| MenuDemo { open: false })),
"dropdown-menu" => Arc::new(cx.new(|_| DropdownMenuDemo {
open_menu: None,
status: None,
})),
"context-menu" => Arc::new(cx.new(|_| ContextMenuDemo {
action: SharedString::from("Right-click the card"),
})),
"popover" => Arc::new(cx.new(|_| PopoverDemo { open: false })),
"tooltip" => Arc::new(cx.new(|_| TooltipDemo)),
"empty-state" => Arc::new(cx.new(|_| EmptyStateDemo)),
"scroll-area" => Arc::new(cx.new(|_| ScrollAreaDemo)),
"scrollbar" => Arc::new(cx.new(|_| ScrollbarDemo::new())),
"code-view" => Arc::new(cx.new(CodeViewDemo::new)),
"diff-view" => Arc::new(cx.new(DiffViewDemo::new)),
"markdown-view" => Arc::new(cx.new(MarkdownViewDemo::new)),
"text-view" => Arc::new(cx.new(TextViewDemo::new)),
"sidebar" => Arc::new(cx.new(|_| SidebarDemo { selected: None })),
"resize-handle" => Arc::new(cx.new(|_| ResizeDemo {
width: rems(ResizeDemo::WIDTH_DEFAULT_REMS).to_pixels(window.rem_size()),
})),
"resizable" => Arc::new(cx.new(ResizableDemo::new)),
"separator" => Arc::new(cx.new(|_| SeparatorDemo)),
"theme" => Arc::new(cx.new(|_| ThemeDemo)),
_ => return None,
};
Some(view)
Some(panel)
}
}
@@ -698,6 +677,8 @@ struct WelcomePanel {
gallery: WeakEntity<Gallery>,
}
impl Panel for WelcomePanel {}
impl Render for WelcomePanel {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -806,6 +787,8 @@ struct RightDetails {
body: SharedString,
}
impl Panel for RightDetails {}
impl Render for RightDetails {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -841,6 +824,8 @@ impl ButtonDemo {
}
}
impl Panel for ButtonDemo {}
impl Render for ButtonDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1005,6 +990,8 @@ impl InputDemo {
}
}
impl Panel for InputDemo {}
impl Render for InputDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1089,6 +1076,8 @@ impl FieldDemo {
}
}
impl Panel for FieldDemo {}
impl Render for FieldDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1161,6 +1150,8 @@ impl Render for FieldDemo {
struct StatusDotDemo;
impl Panel for StatusDotDemo {}
impl Render for StatusDotDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1329,6 +1320,8 @@ impl ComboboxDemo {
}
}
impl Panel for ComboboxDemo {}
impl Render for ComboboxDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1398,6 +1391,8 @@ struct CheckboxDemo {
checked: bool,
}
impl Panel for CheckboxDemo {}
impl Render for CheckboxDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1440,6 +1435,8 @@ struct RadioDemo {
plan: SharedString,
}
impl Panel for RadioDemo {}
impl Render for RadioDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1476,6 +1473,8 @@ struct SwitchDemo {
checked: bool,
}
impl Panel for SwitchDemo {}
impl Render for SwitchDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1518,6 +1517,8 @@ struct ToggleDemo {
pressed: bool,
}
impl Panel for ToggleDemo {}
impl Render for ToggleDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1555,6 +1556,8 @@ struct SliderDemo {
volume: f32,
}
impl Panel for SliderDemo {}
impl Render for SliderDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1592,6 +1595,8 @@ struct SegmentedDemo {
filter: ExploreFilter,
}
impl Panel for SegmentedDemo {}
impl Render for SegmentedDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1674,6 +1679,8 @@ struct SelectDemo {
strategy: Option<Strategy>,
}
impl Panel for SelectDemo {}
impl Render for SelectDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1722,6 +1729,8 @@ struct LinkDemo {
clicks: usize,
}
impl Panel for LinkDemo {}
impl Render for LinkDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -1770,6 +1779,8 @@ impl Render for LinkDemo {
struct ClipboardDemo;
impl Panel for ClipboardDemo {}
impl Render for ClipboardDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1804,6 +1815,8 @@ impl Render for ClipboardDemo {
struct SeparatorDemo;
impl Panel for SeparatorDemo {}
impl Render for SeparatorDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1856,6 +1869,8 @@ impl ResizableDemo {
}
}
impl Panel for ResizableDemo {}
impl Render for ResizableDemo {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1895,6 +1910,8 @@ impl Render for ResizableDemo {
struct BadgeDemo;
impl Panel for BadgeDemo {}
impl Render for BadgeDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1932,6 +1949,8 @@ impl Render for BadgeDemo {
struct AvatarDemo;
impl Panel for AvatarDemo {}
impl Render for AvatarDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1967,6 +1986,8 @@ impl Render for AvatarDemo {
struct ProgressDemo;
impl Panel for ProgressDemo {}
impl Render for ProgressDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -1992,6 +2013,8 @@ impl Render for ProgressDemo {
struct SpinnerDemo;
impl Panel for SpinnerDemo {}
impl Render for SpinnerDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -2034,6 +2057,8 @@ struct ListDemo {
selected: Option<usize>,
}
impl Panel for ListDemo {}
impl Render for ListDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -2209,6 +2234,8 @@ impl TreeDemo {
}
}
impl Panel for TreeDemo {}
impl Render for TreeDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -2252,6 +2279,8 @@ struct TabsDemo {
overflow_open: bool,
}
impl Panel for TabsDemo {}
impl Render for TabsDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -2427,6 +2456,8 @@ struct AlertDemo {
dismissed: bool,
}
impl Panel for AlertDemo {}
impl Render for AlertDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -2519,6 +2550,8 @@ struct ToastDemo {
toasts: Entity<Toasts>,
}
impl Panel for ToastDemo {}
impl Render for ToastDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let toasts_undo = self.toasts.clone();
@@ -2589,6 +2622,8 @@ struct DialogDemo {
gallery: WeakEntity<Gallery>,
}
impl Panel for DialogDemo {}
impl Render for DialogDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let gallery_dialog = self.gallery.clone();
@@ -2655,6 +2690,8 @@ impl CommandPaletteDemo {
}
}
impl Panel for CommandPaletteDemo {}
impl Render for CommandPaletteDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -2721,6 +2758,8 @@ struct MenuDemo {
open: bool,
}
impl Panel for MenuDemo {}
impl Render for MenuDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -2792,6 +2831,8 @@ impl DropdownMenuDemo {
}
}
impl Panel for DropdownMenuDemo {}
impl Render for DropdownMenuDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -2943,6 +2984,8 @@ struct ContextMenuDemo {
action: SharedString,
}
impl Panel for ContextMenuDemo {}
impl Render for ContextMenuDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -2998,6 +3041,8 @@ struct PopoverDemo {
open: bool,
}
impl Panel for PopoverDemo {}
impl Render for PopoverDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -3039,6 +3084,8 @@ impl Render for PopoverDemo {
struct TooltipDemo;
impl Panel for TooltipDemo {}
impl Render for TooltipDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -3084,6 +3131,8 @@ impl Render for TooltipDemo {
struct EmptyStateDemo;
impl Panel for EmptyStateDemo {}
impl Render for EmptyStateDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
@@ -3130,6 +3179,8 @@ impl Render for EmptyStateDemo {
struct ScrollAreaDemo;
impl Panel for ScrollAreaDemo {}
impl Render for ScrollAreaDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
@@ -3212,6 +3263,8 @@ impl ScrollbarDemo {
}
}
impl Panel for ScrollbarDemo {}
impl Render for ScrollbarDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
@@ -3421,6 +3474,8 @@ impl CodeViewDemo {
}
}
impl Panel for CodeViewDemo {}
impl Render for CodeViewDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -3591,6 +3646,8 @@ impl DiffViewDemo {
}
}
impl Panel for DiffViewDemo {}
impl Render for DiffViewDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -3811,6 +3868,8 @@ impl MarkdownViewDemo {
}
}
impl Panel for MarkdownViewDemo {}
impl Render for MarkdownViewDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -3887,6 +3946,8 @@ impl TextViewDemo {
}
}
impl Panel for TextViewDemo {}
impl Render for TextViewDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.theme().colors;
@@ -3934,6 +3995,8 @@ struct SidebarDemo {
selected: Option<usize>,
}
impl Panel for SidebarDemo {}
impl Render for SidebarDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -4102,6 +4165,8 @@ impl ResizeDemo {
const WIDTH_MAX_REMS: f32 = 20.;
}
impl Panel for ResizeDemo {}
impl Render for ResizeDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.entity();
@@ -4180,6 +4245,8 @@ impl Render for ResizeDemo {
struct ThemeDemo;
impl Panel for ThemeDemo {}
impl Render for ThemeDemo {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();