diff --git a/docs/external-plugin-guide.md b/docs/external-plugin-guide.md index 84294b95..f218c388 100644 --- a/docs/external-plugin-guide.md +++ b/docs/external-plugin-guide.md @@ -247,6 +247,14 @@ interface TabDescriptor { * 抛错会被吞掉(不显示角标,不影响渲染)。 */ badge?: (ctx: Context, scope: SessionScope, state: SidebarState) => string | number | null | undefined + /** + * tab 栏右侧操作区(v0.25.0+):tab 激活时,返回的 ReactNode 渲染在 tab 栏 + * 右端(+ 按钮右侧、面板关闭按钮左侧,右对齐)。让 tab 把自己的工具栏 + * (如终端 tab 的新建 / 拆分 / 重启)直接放进 tab 栏,而不是在内容区顶部 + * 再单独渲染一行头部。返回 null/undefined 不渲染;抛错被吞掉(不渲染操作)。 + * 每次活跃 tab 渲染都会调用——保持廉价。 + */ + rightActions?: (ctx: Context, scope: SessionScope, state: SidebarState) => ReactNode /** * 生命周期回调(v0.12.0+),只由 SERVICE 路径触发: * - onOpen:openTab 真正**新建** tab 后(dedupe/id 安全网聚焦不算打开); diff --git a/src/client/Sidebar.tsx b/src/client/Sidebar.tsx index dd6a954c..2117351b 100644 --- a/src/client/Sidebar.tsx +++ b/src/client/Sidebar.tsx @@ -616,6 +616,24 @@ export function Sidebar(props: { ctx: Context; store: SidebarStore }) { return {text} } + /** + * The active tab's right-aligned action area from the tab-type registry: + * a descriptor that declares `rightActions` supplies its own toolbar + * (e.g. a terminal tab's new / split / restart buttons) rendered at the + * tab strip's right end. A throwing resolver is swallowed (no actions) — + * the tab strip must never break because a plugin's resolver failed. + */ + const tabRightActionsOf = (tab: SidebarTab): ReactNode => { + const descriptor = ctx.get('betterSidebar')?.getTab(tab.type) + if (descriptor?.rightActions === undefined) return null + try { + return descriptor.rightActions(ctx, { sessionId, cwd }, state) + } catch (error) { + console.error('[dsh-better-sidebar] tab rightActions error:', error) + return null + } + } + /** * Render one tab's content. `active` (from the workbench) tells whether * this tab is the active one in its pane; combined with the panel's @@ -733,6 +751,7 @@ export function Sidebar(props: { ctx: Context; store: SidebarStore }) { renderTab={renderTab} getTabIcon={tabIconOf} getTabBadge={tabBadgeOf} + getTabRightActions={tabRightActionsOf} /> diff --git a/src/client/TabBar.tsx b/src/client/TabBar.tsx index 70e2a4a0..689da228 100644 --- a/src/client/TabBar.tsx +++ b/src/client/TabBar.tsx @@ -68,9 +68,17 @@ export function TabBar(props: { /** Badge resolver for tab labels (reads the descriptor's `badge`; the * resolver returns the rendered pill or null). */ getTabBadge?: (tab: SidebarTab) => ReactNode + /** + * Right-aligned action area resolver for the active tab: returns a + * ReactNode rendered at the tab strip's right end (between the + button + * and the panel's close control), or null/undefined for none. Lets a + * descriptor (e.g. a terminal tab) inject its own toolbar (new / split / + * restart) directly into the strip instead of a separate header row. + */ + getTabRightActions?: (tab: SidebarTab) => ReactNode }) { const { - paneId, tabs, active, onActivate, onClose, onNewTab, newTabOptions, onDropTab, getTabIcon, getTabBadge, + paneId, tabs, active, onActivate, onClose, onNewTab, newTabOptions, onDropTab, getTabIcon, getTabBadge, getTabRightActions, } = props const [menuOpen, setMenuOpen] = useState(false) // The tab right-click context menu: the target tab plus the cursor @@ -302,6 +310,20 @@ export function TabBar(props: { anchor={} /> + {/* + The active tab's right-aligned action area: rendered at the tab + strip's right end (after the + menu, before the panel's close + control). A descriptor that declares `rightActions` supplies its own + toolbar here (e.g. a terminal tab's new / split / restart buttons), + so the page's controls live in the strip instead of a separate header + row below it. null/undefined renders nothing. + */} + {(() => { + if (getTabRightActions === undefined) return null + const activeTab = tabs.find(tab => tab.id === active) + if (activeTab === undefined) return null + return
{getTabRightActions(activeTab)}
+ })()} ) } diff --git a/src/client/service.ts b/src/client/service.ts index 182f1da5..fd8731ad 100644 --- a/src/client/service.ts +++ b/src/client/service.ts @@ -235,6 +235,15 @@ export interface TabDescriptor { * so keep it cheap; a throw is swallowed (no badge shown). */ badge?: (ctx: Context, scope: SessionScope, state: SidebarState) => string | number | null | undefined + /** + * Right-aligned action area for the tab strip (v0.25.0+). When the tab is + * active, the returned ReactNode renders at the tab strip's right end + * (between the + button and the panel's close control). Lets a descriptor + * (e.g. a terminal tab) put its toolbar (new / split / restart) directly + * in the strip instead of a separate header row below it. Returning + * null/undefined renders nothing; a throw is swallowed (no actions shown). + */ + rightActions?: (ctx: Context, scope: SessionScope, state: SidebarState) => ReactNode /** * Lifecycle callbacks (v0.12.0+). Fired by the SERVICE paths only: * `onOpen` when an open actually creates a tab (a dedupe/id-safety-net diff --git a/src/client/sidebar.module.css b/src/client/sidebar.module.css index c39f86d6..e38837d4 100644 --- a/src/client/sidebar.module.css +++ b/src/client/sidebar.module.css @@ -660,6 +660,21 @@ body[data-dsh-sidebar-dragging] .bottomPanel { color: var(--dsw-alias-label-primary); } +/* The active tab's right-aligned action area: sits at the tab strip's right + end (after the + button, before the panel's close control). Flexes to fill + the remaining width so its content can right-align; content is opaque to + the tab strip's own layout (the strip reserves the close control's width + via .bottomPanel .tabBar's padding-right, so this area never underlaps it). */ +.tabBarRightActions { + flex: 1; + min-width: 0; + display: flex; + align-items: center; + justify-content: flex-end; + gap: 2px; + padding-right: 4px; +} + /* ── Explorer ──────────────────────────────────────────────────────────── */ .explorer { diff --git a/src/client/split-pane.tsx b/src/client/split-pane.tsx index 7135bbaa..b0e25389 100644 --- a/src/client/split-pane.tsx +++ b/src/client/split-pane.tsx @@ -147,8 +147,9 @@ function LeafView(props: { renderTab: (tab: SidebarTab, active: boolean, paneId: string) => ReactNode getTabIcon?: (tab: SidebarTab) => ReactNode getTabBadge?: (tab: SidebarTab) => ReactNode + getTabRightActions?: (tab: SidebarTab) => ReactNode }) { - const { leaf, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge } = props + const { leaf, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge, getTabRightActions } = props const [dropZone, setDropZone] = useState(null) const activeTab = leaf.tabs.find(tab => tab.id === leaf.active) ?? leaf.tabs[leaf.tabs.length - 1] @@ -204,6 +205,7 @@ function LeafView(props: { newTabOptions={newTabOptions} getTabIcon={getTabIcon} getTabBadge={getTabBadge} + getTabRightActions={getTabRightActions} onDropTab={(payload, before) => { if (before === null) actions.moveTabToEdge(payload, leaf.id, 'center') else actions.moveTabBefore(payload, leaf.id, before) @@ -243,8 +245,9 @@ function NodeView(props: { renderTab: (tab: SidebarTab, active: boolean, paneId: string) => ReactNode getTabIcon?: (tab: SidebarTab) => ReactNode getTabBadge?: (tab: SidebarTab) => ReactNode + getTabRightActions?: (tab: SidebarTab) => ReactNode }) { - const { node, state, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge } = props + const { node, state, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge, getTabRightActions } = props if (node.kind === 'leaf') { return ( ) } @@ -303,8 +307,9 @@ export function Workbench(props: { renderTab: (tab: SidebarTab, active: boolean, paneId: string) => ReactNode getTabIcon?: (tab: SidebarTab) => ReactNode getTabBadge?: (tab: SidebarTab) => ReactNode + getTabRightActions?: (tab: SidebarTab) => ReactNode }) { - const { state, tree, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge } = props + const { state, tree, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge, getTabRightActions } = props return (
)