layout: replace panel icon with lead element and optional title

This commit is contained in:
2026-11-11 16:26:47 +07:00
parent a0ede5e15a
commit 9326fca63d
5 changed files with 282 additions and 214 deletions
+32 -26
View File
@@ -15,7 +15,7 @@ use gpui_native_core::common::{h_flex, v_flex};
use gpui_native_core::context_menu::ContextMenu;
use gpui_native_core::menu::MenuItem;
use gpui_native_data::tabs::{Tab, Tabs, TabsVariant};
use gpui_native_icons::{Icon, IconRole};
use gpui_native_icons::IconRole;
use gpui_native_theme::{ActiveTheme as _, Size};
use crate::resize_handle::ResizeHandle;
@@ -36,35 +36,44 @@ type OpenRightSidebarHandler = Rc<dyn Fn(&mut App)>;
#[must_use]
pub struct PanelDescriptor {
id: SharedString,
title: SharedString,
icon: Option<Icon>,
title: Option<SharedString>,
lead: Option<Rc<dyn Fn() -> AnyElement>>,
build: PanelBuilder,
right_sidebar: Option<PanelBuilder>,
}
impl PanelDescriptor {
/// Creates a panel descriptor that opens through `build`.
pub fn new<I, T, F>(id: I, title: T, build: F) -> Self
pub fn new<I, F>(id: I, build: F) -> Self
where
I: Into<SharedString>,
T: Into<SharedString>,
F: Fn(&mut Window, &mut App) -> AnyView + 'static,
{
Self {
id: id.into(),
title: title.into(),
icon: None,
title: None,
lead: None,
build: Box::new(build),
right_sidebar: None,
}
}
/// Shows an icon leading the tab label.
pub fn icon<I>(mut self, icon: I) -> Self
/// Shows `title` as the panel's tab label.
pub fn title<T>(mut self, title: T) -> Self
where
I: Into<Icon>,
T: Into<SharedString>,
{
self.icon = Some(icon.into());
self.title = Some(title.into());
self
}
/// Shows an element, e.g. an icon or avatar, leading the tab label.
pub fn lead<F, E>(mut self, build: F) -> Self
where
F: Fn() -> E + 'static,
E: IntoElement,
{
self.lead = Some(Rc::new(move || build().into_any_element()));
self
}
@@ -111,7 +120,7 @@ pub struct PanelDrag {
/// The card that follows the cursor while a tab is dragged.
struct DragPreview {
title: SharedString,
title: Option<SharedString>,
}
impl Render for DragPreview {
@@ -127,15 +136,15 @@ impl Render for DragPreview {
.text_size(theme.sizes.medium.text_size)
.text_color(theme.colors.foreground)
.opacity(0.85)
.child(self.title.clone())
.children(self.title.clone())
}
}
/// One open panel view.
struct PanelTab {
panel: SharedString,
title: SharedString,
icon: Option<Icon>,
title: Option<SharedString>,
lead: Option<Rc<dyn Fn() -> AnyElement>>,
view: AnyView,
right_view: Option<AnyView>,
}
@@ -220,7 +229,7 @@ impl DockState {
let tab = PanelTab {
panel: SharedString::from(id),
title: panel.title.clone(),
icon: panel.icon.clone(),
lead: panel.lead.clone(),
view: (panel.build)(window, cx),
right_view,
};
@@ -684,7 +693,7 @@ impl DockState {
.map(|tab| TabView {
panel: tab.panel.clone(),
title: tab.title.clone(),
icon: tab.icon.clone(),
lead: tab.lead.clone(),
view: tab.view.clone(),
})
.collect(),
@@ -745,8 +754,8 @@ impl DockState {
#[derive(Clone)]
struct TabView {
panel: SharedString,
title: SharedString,
icon: Option<Icon>,
title: Option<SharedString>,
lead: Option<Rc<dyn Fn() -> AnyElement>>,
view: AnyView,
}
@@ -1047,15 +1056,12 @@ fn render_pane(
.selected(selected)
.tabs({
let tabs = pane.tabs.clone();
move |_, cx| {
let theme = cx.theme();
let metrics = theme.sizes.resolve(Size::default());
move |_, _| {
tabs.iter()
.map(|tab| {
Tab::new(tab.panel.clone(), tab.title.clone())
.when_some(tab.icon.clone(), |entry, icon| {
entry.lead(icon.size(metrics.icon_size))
Tab::new(tab.panel.clone(), tab.title.clone().unwrap_or_default())
.when_some(tab.lead.as_ref().map(|build| build()), |entry, lead| {
entry.lead(lead)
})
.closeable(true)
.wrap({
+3 -2
View File
@@ -36,11 +36,12 @@ impl Render for SidePanel {
}
fn descriptor(tag: &'static str) -> PanelDescriptor {
PanelDescriptor::new(tag, tag, move |_, cx| cx.new(|_| Panel { tag }).into())
PanelDescriptor::new(tag, move |_, cx| cx.new(|_| Panel { tag }).into()).title(tag)
}
fn descriptor_with_right_sidebar(tag: &'static str) -> PanelDescriptor {
PanelDescriptor::new(tag, tag, move |_, cx| cx.new(|_| Panel { tag }).into())
PanelDescriptor::new(tag, move |_, cx| cx.new(|_| Panel { tag }).into())
.title(tag)
.right_sidebar(move |_, cx| cx.new(|_| SidePanel { tag }).into())
}
+6 -6
View File
@@ -392,15 +392,15 @@ mod tabbar_toggles {
let dock = cx.update(|cx| {
let dock = DockState::new(cx);
dock.update(cx, |dock, _| {
dock.register_panel(PanelDescriptor::new("one", "One", |_, cx| {
cx.new(|_| DockPanel).into()
}));
dock.register_panel(
PanelDescriptor::new("one", |_, cx| cx.new(|_| DockPanel).into()).title("One"),
);
for index in 0..14 {
let tag = format!("panel-{index}");
let id = tag.clone();
dock.register_panel(PanelDescriptor::new(id, tag, |_, cx| {
cx.new(|_| DockPanel).into()
}));
dock.register_panel(
PanelDescriptor::new(id, |_, cx| cx.new(|_| DockPanel).into()).title(tag),
);
}
});
dock
+1 -1
View File
@@ -22,7 +22,7 @@ impl Render for Panel {
}
fn descriptor(tag: &'static str) -> PanelDescriptor {
PanelDescriptor::new(tag, tag, move |_, cx| cx.new(|_| Panel { tag }).into())
PanelDescriptor::new(tag, move |_, cx| cx.new(|_| Panel { tag }).into()).title(tag)
}
struct Harness {