Merge PR #736: fix(native): focus through the controller on surface.activate; carry meta on close

# Conflicts:
#	docs/external-plugin-guide.md
#	src/client/native/surface.ts
#	src/client/service.ts
This commit is contained in:
HuanLinOTO committed 2026-10-01 17:12:53 +08:00
commit 86ec5ccc75
4 files changed
+121 -13

No files matched your search

+4 -4
View File
@@ -14,13 +14,13 @@
- `registerTab` / `registerFileViewer` 签名不变;
- `openTab` / `openFile` 默认落到原生右侧栏;新增可选 `OpenTabSeed.target`(`'right'` 默认 / `'bottom'` 落插件的底部工作台 / `'side'` 落**原生栏的第二个格**,见下表「在侧边打开」);
- `updateTab` / `closeTab` / `activateTab` 认识原生 tab id(插件为每个原生 tab 维护一条合成 `SidebarTab` 记录,`tab.meta` / `tab.path` 的写入照旧生效)。
- `updateTab` / `closeTab` / `activateTab` 认识原生 tab id(插件为每个原生 tab 维护一条合成 `SidebarTab` 记录,`tab.meta` / `tab.path` 的写入照旧生效);`activateTab` 对原生 tab 经宿主 `ISidebarRight.focus`(dsh ≥ 0.1.5)聚焦 tab 及其所在 pane——多实例外部插件 tab(无 `(kind, 地址)` 身份可重开)因此也能被程序化唤起到前台。
行为差异(写在这里以免踩坑):
| 事项 | 说明 |
|---|---|
| 生命周期回调 | 原生面只有「一次打开」,不区分新建/聚焦,因此只触发 `onOpen`(`onActivate` 仅在插件自己的底部工作台里触发) |
| 生命周期回调 | 原生面的「打开」不区分新建/聚焦,因此 `onOpen` 每次打开都触发(`onActivate` 仅在插件自己的底部工作台里触发)。程序化 `closeTab` 与 `activateTab` 在原生 tab 上正常工作:关闭走宿主 per-session 面,回调拿到完整 `tab`(**含 `meta`**);激活经 `ISidebarRight.focus` 聚焦 tab 与其 pane。**例外**:用户直接点原生 tab 上的 × 关闭不会触发 `onClose`(宿主不广播该事件,插件在 tab 体卸载时丢弃记录) |
| 去重 | 原生按 `(kind, 地址)` 去重:有 `createTab` 的类型每次新开一个 tab(sidechat / diff),其余聚焦已有 tab;`dedupeKey` 的自定义语义不参与原生面 |
| 布局持久化 | 原生栏的布局**只在内存**(刷新后回到折叠默认),插件自己的底部工作台仍然持久化 |
| 跨会话打开 | 目标会话的右侧栏 store 未挂载时,打开会排队到该会话上屏后重放 |
@@ -249,9 +249,9 @@ interface TabDescriptor {
badge?: (ctx: Context, scope: SessionScope, state: SidebarState) => string | number | null | undefined
/**
* 生命周期回调(v0.12.0+),只由 SERVICE 路径触发:
* - onOpen:openTab 真正**新建** tab 后(dedupe/id 安全网聚焦不算打开);
* - onOpen:openTab 真正**新建** tab 后(dedupe/id 安全网聚焦不算打开;原生面每次打开都触发);
* - onActivate:tab 被聚焦时(dedupe 聚焦、id 安全网聚焦、tab 栏点击激活);
* - onClose:closeTab 关闭 tab 后。
* - onClose:closeTab 关闭 tab 后(原生 tab 同样携带完整 tab,含 meta;用户点宿主 × 的关闭除外)。
* 内置专属流程(diff 拆分放置、sidechat 线程重开)直接改 state,不触发
* 回调——但它们只作用于内置类型(diff / sidechat),外部插件的 tab 永远走
* service 路径。回调抛错只 console.error,绝不打断打开/关闭流程。
+13 -5
View File
@@ -47,6 +47,8 @@ interface NativeController {
close(tabId: string): void
/** The mounted-seat observation (0.1.7 `ISidebarRight.mounted`). */
mounted?: MountedSessions
/** `ISidebarRight.focus` — focuses the tab and the pane holding it (dsh >= 0.1.5). */
focus?(tabId: string): void
/** Not part of `ISidebarRight`: the concrete controller's per-session writes. */
openTabIn?(sessionId: string, kind: string, options?: { params?: unknown; revealIfOpened?: boolean; preferNewPane?: boolean }): void
openResourceIn?(sessionId: string, address: string, options?: { params?: unknown; revealIfOpened?: boolean; preferNewPane?: boolean }): void
@@ -260,7 +262,11 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na
if (sessionId === mountedSessionId(ctx)) api.close(tabId)
else if (api.closeIn !== undefined) api.closeIn(sessionId, tabId)
}
return { type: record.tab.type, title: record.tab.title }
return {
type: record.tab.type,
title: record.tab.title,
...(record.tab.meta === undefined ? {} : { meta: record.tab.meta }),
}
},
update(tabId, patch, sessionId) {
const owner = sessionOf(tabId, sessionId)
@@ -269,10 +275,12 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na
return true
},
activate(tabId, sessionId) {
// The native surface has no cross-pane activation face the plugin needs:
// a tab is focused by opening its (kind, address) again, which the
// native open already de-duplicates.
return sessionOf(tabId, sessionId) !== undefined
// External plugin tabs (multi-instance kinds like side chats) carry no
// (kind, address) identity to re-open against, so "open again to focus"
// cannot work for them — focus through the controller's own face.
if (sessionOf(tabId, sessionId) === undefined) return false
controller()?.focus?.(tabId)
return true
},
has: (tabId, sessionId) => sessionOf(tabId, sessionId) !== undefined,
flushPending,
+9 -4
View File
@@ -460,8 +460,8 @@ export interface SidebarSurface {
openResource(input: { sessionId: string; address: string; line?: number; revealIfOpened: boolean; preferNewPane?: boolean }): void
/** The file address of one path (the native surface owns the grammar). */
fileAddress(sessionId: string, cwd: string | undefined, path: string): string
/** Close one native tab; the closed record's type/title, or undefined when the id is not native. */
close(sessionId: string, tabId: string): { type: string; title: string } | undefined
/** Close one native tab; the closed record's type/title/meta, or undefined when the id is not native. */
close(sessionId: string, tabId: string): { type: string; title: string; meta?: unknown } | undefined
/**
* Patch a native tab's plugin-side record; false when it is not native.
* `sessionId` (optional) names the seat session the tab lives in: native tab
@@ -470,7 +470,7 @@ export interface SidebarSurface {
* Omitted, the id resolves against the mounted seat.
*/
update(tabId: string, patch: { title?: string; path?: string; meta?: unknown }, sessionId?: string): boolean
/** Focus a native tab; false when it is not native (same `sessionId` rule as `update`). */
/** Focus a native tab through the host controller; false when it is not native (same `sessionId` rule as `update`). */
activate(tabId: string, sessionId?: string): boolean
/** Whether a tab id belongs to the native surface (same `sessionId` rule as `update`). */
has(tabId: string, sessionId?: string): boolean
@@ -1089,7 +1089,12 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
const descriptor = tabs.get(closedNative.type)
if (descriptor !== undefined) {
safeCall(() => descriptor.onClose?.(
{ id: tabId, type: closedNative.type as TabType, title: closedNative.title },
{
id: tabId,
type: closedNative.type as TabType,
title: closedNative.title,
...(closedNative.meta === undefined ? {} : { meta: closedNative.meta }),
},
scope ?? { sessionId },
))
}
+95
View File
@@ -9,6 +9,7 @@ import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { createNativeTabRecords, NativeTabBody, NativeTabTitle } from '../src/client/native/tab-adapter.tsx'
import { createNativeSurface } from '../src/client/native/surface.ts'
import { registerNativeSurface } from '../src/client/native/index.ts'
import { createNativeSurface } from '../src/client/native/surface.ts'
import { createBetterSidebarService, type SidebarSurface } from '../src/client/service.ts'
@@ -267,6 +268,30 @@ describe('service routing into the native surface', () => {
expect(() => service.closeTab('other', scope)).not.toThrow()
})
it('hands the native close meta to descriptor.onClose', () => {
// Regression #644: the native close path projected the record down to
// type/title, starving meta-driven lifecycle consumers (recently-closed
// records, reopen flows).
const surface: SidebarSurface = {
openTab: () => {},
openResource: () => {},
fileAddress: () => 'addr',
close: () => ({ type: 'my-plugin:term', title: 'T', meta: { threadId: 't-9' } }),
update: () => false,
activate: () => false,
has: () => true,
}
const store = createSidebarStore()
store.setSession('s1')
const service = createBetterSidebarService(store)
service.setSurface(surface)
const seen: SidebarTab[] = []
service.registerTab({ id: 'my-plugin:term', title: 'Terminal', component: () => null, onClose: tab => { seen.push(tab) } })
service.closeTab('native-1', scope)
expect(seen).toHaveLength(1)
expect(seen[0]).toMatchObject({ id: 'native-1', type: 'my-plugin:term', title: 'T', meta: { threadId: 't-9' } })
})
it('refuses a disabled type before touching the surface', () => {
const { service, calls } = mount()
service.setSurface(undefined)
@@ -286,6 +311,76 @@ describe('service routing into the native surface', () => {
})
})
describe('createNativeSurface activate/close (the real adapter)', () => {
const mountSurface = (): {
surface: ReturnType<typeof createNativeSurface>
records: ReturnType<typeof createNativeTabRecords>
focus: ReturnType<typeof vi.fn>
close: ReturnType<typeof vi.fn>
} => {
const focus = vi.fn()
const close = vi.fn()
const controller = {
openTab: () => {},
openResource: () => {},
close,
// `ISidebarRight.focus` (dsh >= 0.1.5): the face activate() rides.
focus,
mounted: { getSnapshot: () => 's1', subscribe: () => () => {} },
openTabIn: () => {},
openResourceIn: () => {},
closeIn: () => {},
}
const records = createNativeTabRecords()
const ctx = {
get: (name: string) => (name === 'sidebarRight' ? controller : undefined),
sessions: { list: { subscribe: () => () => {}, getSnapshot: () => ({}) } },
}
const surface = createNativeSurface(ctx as never, records)
return { surface, records, focus, close }
}
it('activate focuses an existing record through the controller focus face', () => {
// Regression #644: activate() used to return records.has(tabId) without
// focusing anything — external plugins with multi-instance native tabs
// (dsh-sidenote's side chats) could never bring a tab to the front.
const { surface, records, focus } = mountSurface()
records.ensure({ id: 'tab-9', kind: 'sidechat', title: 'Side Chat', params: { meta: { threadId: 't-1' } }, scope })
expect(surface.activate('tab-9')).toBe(true)
expect(focus).toHaveBeenCalledTimes(1)
expect(focus).toHaveBeenCalledWith('tab-9')
})
it('activate leaves unknown ids alone and reports them', () => {
const { surface, focus } = mountSurface()
expect(surface.activate('missing')).toBe(false)
expect(focus).not.toHaveBeenCalled()
})
it('close returns the record meta and rides the on-screen face for the mounted session', () => {
const { surface, records, close } = mountSurface()
records.ensure({ id: 'tab-10', kind: 'sidechat', title: 'hello thread', params: { meta: { threadId: 't-7' } }, scope })
const closed = surface.close('s1', 'tab-10')
expect(closed).toEqual({ type: 'sidechat', title: 'hello thread', meta: { threadId: 't-7' } })
// The mounted seat is s1's, so the host close rode the on-screen face.
expect(close).toHaveBeenCalledWith('tab-10')
expect(records.has('tab-10')).toBe(false)
})
it('close of a non-mounted session rides the per-session face', () => {
const { surface, records } = mountSurface()
records.ensure({ id: 'tab-11', kind: 'sidechat', title: 'bg', params: undefined, scope })
const closed = surface.close('s2', 'tab-11')
expect(closed).toEqual({ type: 'sidechat', title: 'bg' })
})
it('close of an unknown id is undefined (a strict no-op)', () => {
const { surface, close } = mountSurface()
expect(surface.close('s1', 'missing')).toBeUndefined()
expect(close).not.toHaveBeenCalled()
})
})
describe('registerNativeSurface lifecycle (service-driven registration)', () => {
it('keeps the files takeover registered across a Session switch pulse', () => {
// Regression: `sync()`'s cleanup loop compared the live registrations with