feat(tabbar): 新增 TabDescriptor.rightActions,活跃 tab 可注入 tab 栏右侧操作区

TabBar 新增 getTabRightActions prop,活跃 tab 的 descriptor 声明 rightActions
时,返回的 ReactNode 渲染在 tab 栏右端(+ 按钮右侧、面板关闭按钮左侧,
右对齐)。让 tab 把自己的工具栏(如终端 tab 的新建 / 拆分 / 重启)直接放进
tab 栏,而不是在内容区顶部再单独渲染一行头部。

TabDescriptor.rightActions 与 badge 同签名 (ctx, scope, state) => ReactNode,
抛错被吞掉(不渲染操作),每次活跃 tab 渲染调用——保持廉价。

文档同步:docs/external-plugin-guide.md §4.1 TabDescriptor 字段。
This commit is contained in:
xlli67 committed 2026-09-30 13:00:28 +08:00
1 parent 492661a3ea
commit 679889d8a9
6 files changed
+83 -4

No files matched your search

+8
View File
@@ -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 安全网聚焦不算打开);
+19
View File
@@ -616,6 +616,24 @@ export function Sidebar(props: { ctx: Context; store: SidebarStore }) {
return <span className={css.tabBadge}>{text}</span>
}
/**
* 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}
/>
</div>
</div>
+23 -1
View File
@@ -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={<span />}
/>
</div>
{/*
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 <div className={css.tabBarRightActions}>{getTabRightActions(activeTab)}</div>
})()}
</div>
)
}
+9
View File
@@ -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
+15
View File
@@ -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 {
+9 -3
View File
@@ -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<DropZone | null>(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 (
<LeafView
@@ -255,6 +258,7 @@ function NodeView(props: {
renderTab={renderTab}
getTabIcon={getTabIcon}
getTabBadge={getTabBadge}
getTabRightActions={getTabRightActions}
/>
)
}
@@ -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 (
<div className={css.workbench}>
<NodeView
@@ -316,6 +321,7 @@ export function Workbench(props: {
renderTab={renderTab}
getTabIcon={getTabIcon}
getTabBadge={getTabBadge}
getTabRightActions={getTabRightActions}
/>
</div>
)