feat: inline pinned tabs in TabBar (replaces PinnedRail)
M4 rewrite per user feedback: pinned terminals should display in the
Tabs bar itself, not a separate rail. Clicking activates in-place
(cross-session terminal rendering), no session jump.
Changes:
- Delete PinnedRail.tsx + tests/pinned-rail.spec.tsx
- pinned.ts: add createPinnedVirtualTab (virtual id = pinned:<sess>:<id>,
meta.__pinnedHome = { sessionId, cwd, tabId }), injectPinnedIntoTree
(appends virtual tabs to first leaf, overrides active), isPinnedVirtualId,
parsePinnedVirtualId, isPinnedVirtualTab, getPinnedHomeScope
- tab-content-memo.ts: add effectiveTabId to memo key — TabContent passes
the original tab id (not the virtual id) to the descriptor component so
TerminalView's WS URL resolves to the home PTY
- Sidebar.tsx: augmented tree (injectPinnedIntoTree), activePinnedTabId
state, wrappedActions (intercept virtual ids for activate/close/unpin/
drag/float), renderTab passes home scope + effectiveTabId for pinned tabs,
pinnedRevision replaces railRevision
- TabBar.tsx: pinned virtual tabs non-draggable, pin icon prefix, stripped
context menu (Unpin/Close only, no float/close-others/left/right)
- sidebar.module.css: remove rail styles, add .pinnedTab (italic + tertiary)
- Docs: README/README_EN/AGENTS.md/design doc updated for inline approach
157/157 client-side tests pass; typecheck + build clean
This commit is contained in:
1 parent
acf639471d
commit
e7c4578791
13 files changed
+416
-547
No files matched your search
@@ -1,200 +0,0 @@
|
||||
/**
|
||||
* PinnedRail component tests (v0.17.0+). The rail is a pure presentation
|
||||
* component: it renders the entries passed to it and fires callbacks. The
|
||||
* cross-session resolution (collectPinnedTabs) and the jump-back effect are
|
||||
* tested elsewhere (pinned.spec.ts, state.spec.ts). These tests cover the
|
||||
* rail's render conditions, tooltip, click/focus, right-click menu, and
|
||||
* middle-click close.
|
||||
*/
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createElement } from 'react'
|
||||
import { createRoot, type Root } from 'react-dom/client'
|
||||
import { act } from 'react-dom/test-utils'
|
||||
|
||||
;(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = true
|
||||
|
||||
import { PinnedRail } from '../src/client/PinnedRail.tsx'
|
||||
import type { PinnedTabEntry } from '../src/client/pinned.ts'
|
||||
|
||||
/** Point the browser-language fallback at Chinese so the labels assert. */
|
||||
function stubZh(): void {
|
||||
Object.defineProperty(globalThis, 'navigator', {
|
||||
value: { language: 'zh-CN' },
|
||||
configurable: true,
|
||||
})
|
||||
}
|
||||
|
||||
function makeEntry(overrides: Partial<PinnedTabEntry> & { tabId?: string } = {}): PinnedTabEntry {
|
||||
const tabId = overrides.tabId ?? 'terminal:1'
|
||||
return {
|
||||
tab: {
|
||||
id: tabId,
|
||||
type: 'terminal',
|
||||
title: 'My Terminal',
|
||||
pin: { scope: 'global' },
|
||||
...(overrides.tab?.pin ? { pin: overrides.tab.pin } : {}),
|
||||
},
|
||||
homeSessionId: 'home-session',
|
||||
...overrides,
|
||||
} as PinnedTabEntry
|
||||
}
|
||||
|
||||
function mountRail(entries: readonly PinnedTabEntry[]): {
|
||||
rail: HTMLElement | null
|
||||
entryEls: HTMLElement[]
|
||||
onFocus: ReturnType<typeof vi.fn>
|
||||
onUnpin: ReturnType<typeof vi.fn>
|
||||
onClose: ReturnType<typeof vi.fn>
|
||||
unmount: () => void
|
||||
} {
|
||||
const container = document.createElement('div')
|
||||
document.body.append(container)
|
||||
const onFocus = vi.fn()
|
||||
const onUnpin = vi.fn()
|
||||
const onClose = vi.fn()
|
||||
const root: Root = createRoot(container)
|
||||
act(() => {
|
||||
root.render(createElement(PinnedRail, { entries, onFocus, onUnpin, onClose }))
|
||||
})
|
||||
const rail = container.querySelector<HTMLElement>('[data-dsh-pinned-rail]')
|
||||
const entryEls = [...container.querySelectorAll<HTMLElement>('[data-dsh-pinned-tab]')]
|
||||
return { rail, entryEls, onFocus, onUnpin, onClose, unmount: () => { act(() => { root.unmount() }); container.remove() } }
|
||||
}
|
||||
|
||||
/** The portaled menu rows (empty when the menu is closed). */
|
||||
function menuItems(): HTMLElement[] {
|
||||
return [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')]
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = ''
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('PinnedRail', () => {
|
||||
it('renders nothing when there are no entries', () => {
|
||||
stubZh()
|
||||
const { rail } = mountRail([])
|
||||
expect(rail).toBeNull()
|
||||
})
|
||||
|
||||
it('renders the label and one entry per pinned tab with a tooltip', () => {
|
||||
stubZh()
|
||||
const entries = [
|
||||
makeEntry({ tabId: 'terminal:1' }),
|
||||
makeEntry({ tabId: 'terminal:2', tab: { id: 'terminal:2', type: 'terminal', title: 'T2', pin: { scope: 'workspace', homeCwd: '/proj' } } }),
|
||||
]
|
||||
const { rail, entryEls } = mountRail(entries)
|
||||
expect(rail).not.toBeNull()
|
||||
expect(entryEls).toHaveLength(2)
|
||||
// The label is present.
|
||||
expect(rail!.textContent).toContain('固定终端')
|
||||
// The tooltip carries the kind/scope/cwd info.
|
||||
expect(entryEls[0]!.title).toContain('UI 终端')
|
||||
expect(entryEls[0]!.title).toContain('固定到全局')
|
||||
expect(entryEls[1]!.title).toContain('固定到工作区')
|
||||
expect(entryEls[1]!.title).toContain('/proj')
|
||||
})
|
||||
|
||||
it('uses the Agent label for agent terminal tabs', () => {
|
||||
stubZh()
|
||||
const entries = [makeEntry({ tabId: 'agent:abc-123', tab: { id: 'agent:abc-123', type: 'terminal', title: 'Agent T', pin: { scope: 'global' } } })]
|
||||
const { entryEls } = mountRail(entries)
|
||||
expect(entryEls[0]!.title).toContain('Agent 终端')
|
||||
})
|
||||
|
||||
it('click fires onFocus with the home session id and tab id', () => {
|
||||
stubZh()
|
||||
const entries = [makeEntry({ tabId: 'terminal:1', homeSessionId: 'home-A' })]
|
||||
const { entryEls, onFocus, unmount } = mountRail(entries)
|
||||
try {
|
||||
act(() => { entryEls[0]!.click() })
|
||||
expect(onFocus).toHaveBeenCalledTimes(1)
|
||||
expect(onFocus).toHaveBeenCalledWith('home-A', 'terminal:1')
|
||||
} finally {
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
|
||||
it('right-click opens a menu with Unpin and Close, and clicking Unpin fires onUnpin', () => {
|
||||
stubZh()
|
||||
const entries = [makeEntry({ tabId: 'terminal:1', homeSessionId: 'home-A' })]
|
||||
const { entryEls, onUnpin, unmount } = mountRail(entries)
|
||||
try {
|
||||
act(() => {
|
||||
entryEls[0]!.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: 50, clientY: 30 }))
|
||||
})
|
||||
expect(menuItems().map(item => item.textContent)).toEqual(['取消固定', '关闭终端'])
|
||||
act(() => { menuItems()[0]!.click() })
|
||||
expect(onUnpin).toHaveBeenCalledWith('home-A', 'terminal:1')
|
||||
} finally {
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
|
||||
it('right-click Close fires onClose with the home session id and tab id', () => {
|
||||
stubZh()
|
||||
const entries = [makeEntry({ tabId: 'terminal:1', homeSessionId: 'home-A' })]
|
||||
const { entryEls, onClose, unmount } = mountRail(entries)
|
||||
try {
|
||||
act(() => {
|
||||
entryEls[0]!.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: 50, clientY: 30 }))
|
||||
})
|
||||
act(() => { menuItems()[1]!.click() })
|
||||
expect(onClose).toHaveBeenCalledWith('home-A', 'terminal:1')
|
||||
} finally {
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
|
||||
it('the per-entry X button fires onClose and stops propagation (no focus)', () => {
|
||||
stubZh()
|
||||
const entries = [makeEntry({ tabId: 'terminal:1', homeSessionId: 'home-A' })]
|
||||
const { entryEls, onFocus, onClose, unmount } = mountRail(entries)
|
||||
try {
|
||||
const closeBtn = entryEls[0]!.querySelector<HTMLButtonElement>('[class*="pinnedTabClose"]')!
|
||||
act(() => { closeBtn.click() })
|
||||
expect(onClose).toHaveBeenCalledWith('home-A', 'terminal:1')
|
||||
expect(onFocus).not.toHaveBeenCalled()
|
||||
} finally {
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
|
||||
it('middle-click close fires onClose (release on the same entry)', () => {
|
||||
stubZh()
|
||||
const entries = [makeEntry({ tabId: 'terminal:1', homeSessionId: 'home-A' })]
|
||||
const { entryEls, onClose, unmount } = mountRail(entries)
|
||||
try {
|
||||
act(() => {
|
||||
entryEls[0]!.dispatchEvent(new MouseEvent('mousedown', { button: 1, bubbles: true, cancelable: true }))
|
||||
})
|
||||
act(() => {
|
||||
// mouseup must land on the pressed entry's node.
|
||||
entryEls[0]!.dispatchEvent(new MouseEvent('mouseup', { button: 1, bubbles: true }))
|
||||
})
|
||||
expect(onClose).toHaveBeenCalledWith('home-A', 'terminal:1')
|
||||
} finally {
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
|
||||
it('middle-click press then release elsewhere cancels the close', () => {
|
||||
stubZh()
|
||||
const entries = [makeEntry({ tabId: 'terminal:1', homeSessionId: 'home-A' })]
|
||||
const { entryEls, onClose, unmount } = mountRail(entries)
|
||||
try {
|
||||
act(() => {
|
||||
entryEls[0]!.dispatchEvent(new MouseEvent('mousedown', { button: 1, bubbles: true, cancelable: true }))
|
||||
})
|
||||
act(() => {
|
||||
// Release on a different target (document.body, not the entry).
|
||||
document.body.dispatchEvent(new MouseEvent('mouseup', { button: 1, bubbles: true }))
|
||||
})
|
||||
expect(onClose).not.toHaveBeenCalled()
|
||||
} finally {
|
||||
unmount()
|
||||
}
|
||||
})
|
||||
})
|
||||
+125
-1
@@ -10,7 +10,10 @@ import {
|
||||
floatTab, makeDefaultState, openTabInActivePane, setTabPin, toggleBottomPanel,
|
||||
type SidebarState,
|
||||
} from '../src/client/state.ts'
|
||||
import { collectPinnedTabs, pinnedVisibleTo, type PinnedViewer } from '../src/client/pinned.ts'
|
||||
import { collectPinnedTabs, pinnedVisibleTo, type PinnedViewer,
|
||||
isPinnedVirtualId, parsePinnedVirtualId, isPinnedVirtualTab, getPinnedHomeScope,
|
||||
createPinnedVirtualTab, injectPinnedIntoTree,
|
||||
} from '../src/client/pinned.ts'
|
||||
|
||||
/** A pinned terminal tab in a fresh state, opened and pinned in one go. */
|
||||
function stateWithPinnedTerminal(
|
||||
@@ -140,3 +143,124 @@ describe('collectPinnedTabs', () => {
|
||||
expect(entries.map(e => e.tab.id)).toEqual(['terminal:pinned'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('isPinnedVirtualId / parsePinnedVirtualId', () => {
|
||||
it('detects a pinned virtual id by prefix', () => {
|
||||
expect(isPinnedVirtualId('pinned:home:terminal:3')).toBe(true)
|
||||
expect(isPinnedVirtualId('pinned:abc-123:agent:def-456')).toBe(true)
|
||||
expect(isPinnedVirtualId('terminal:3')).toBe(false)
|
||||
expect(isPinnedVirtualId('agent:uuid')).toBe(false)
|
||||
})
|
||||
|
||||
it('parses home session id and original tab id (terminal)', () => {
|
||||
const { homeSessionId, tabId } = parsePinnedVirtualId('pinned:home-sess:terminal:3')
|
||||
expect(homeSessionId).toBe('home-sess')
|
||||
expect(tabId).toBe('terminal:3')
|
||||
})
|
||||
|
||||
it('parses home session id and original tab id (agent)', () => {
|
||||
const { homeSessionId, tabId } = parsePinnedVirtualId('pinned:abc-123:agent:def-456')
|
||||
expect(homeSessionId).toBe('abc-123')
|
||||
expect(tabId).toBe('agent:def-456')
|
||||
})
|
||||
})
|
||||
|
||||
describe('createPinnedVirtualTab', () => {
|
||||
it('creates a virtual tab with a prefixed id and home scope in meta', () => {
|
||||
const tab = { id: 'terminal:3', type: 'terminal' as const, title: 'T3', pin: { scope: 'global' as const, homeCwd: '/p' } }
|
||||
const vtab = createPinnedVirtualTab({ tab, homeSessionId: 'home' })
|
||||
expect(vtab.id).toBe('pinned:home:terminal:3')
|
||||
expect(vtab.type).toBe('terminal')
|
||||
expect(vtab.title).toBe('T3')
|
||||
expect(vtab.pin).toEqual({ scope: 'global', homeCwd: '/p' })
|
||||
})
|
||||
|
||||
it('stores the home scope (sessionId, cwd, original tabId) in meta', () => {
|
||||
const tab = { id: 'terminal:3', type: 'terminal' as const, title: 'T3', pin: { scope: 'workspace' as const, homeCwd: '/proj' } }
|
||||
const vtab = createPinnedVirtualTab({ tab, homeSessionId: 'home-sess' })
|
||||
const home = getPinnedHomeScope(vtab)
|
||||
expect(home).toBeDefined()
|
||||
expect(home!.sessionId).toBe('home-sess')
|
||||
expect(home!.cwd).toBe('/proj')
|
||||
expect(home!.tabId).toBe('terminal:3')
|
||||
})
|
||||
|
||||
it('preserves existing meta fields alongside the pinned home scope', () => {
|
||||
const tab = { id: 'terminal:3', type: 'terminal' as const, title: 'T3', pin: { scope: 'global' as const }, meta: { custom: 42 } }
|
||||
const vtab = createPinnedVirtualTab({ tab, homeSessionId: 'home' })
|
||||
expect((vtab.meta as Record<string, unknown>).custom).toBe(42)
|
||||
expect(isPinnedVirtualTab(vtab)).toBe(true)
|
||||
})
|
||||
|
||||
it('cwd is undefined for a global pin without homeCwd', () => {
|
||||
const tab = { id: 'terminal:3', type: 'terminal' as const, title: 'T3', pin: { scope: 'global' as const } }
|
||||
const vtab = createPinnedVirtualTab({ tab, homeSessionId: 'home' })
|
||||
expect(getPinnedHomeScope(vtab)!.cwd).toBeUndefined()
|
||||
})
|
||||
|
||||
it('isPinnedVirtualTab returns false for a regular tab', () => {
|
||||
const tab = { id: 'terminal:3', type: 'terminal' as const, title: 'T3' }
|
||||
expect(isPinnedVirtualTab(tab)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('injectPinnedIntoTree', () => {
|
||||
it('returns the original tree when no pinned tabs and no active override', () => {
|
||||
const tree = makeDefaultState().splits
|
||||
expect(injectPinnedIntoTree(tree, [], null)).toBe(tree)
|
||||
})
|
||||
|
||||
it('appends pinned virtual tabs to the first leaf', () => {
|
||||
const s = makeDefaultState()
|
||||
const vtab = createPinnedVirtualTab({
|
||||
tab: { id: 'terminal:3', type: 'terminal', title: 'T3', pin: { scope: 'global' } },
|
||||
homeSessionId: 'home',
|
||||
})
|
||||
const result = injectPinnedIntoTree(s.splits, [vtab], null)
|
||||
expect(result.kind).toBe('leaf')
|
||||
if (result.kind === 'leaf') {
|
||||
expect(result.tabs[result.tabs.length - 1]!.id).toBe('pinned:home:terminal:3')
|
||||
}
|
||||
})
|
||||
|
||||
it('overrides the first leaf active when activePinnedId is set', () => {
|
||||
const s = openTabInActivePane(makeDefaultState(), { id: 'terminal:1', type: 'terminal', title: 'T1' })
|
||||
const vtab = createPinnedVirtualTab({
|
||||
tab: { id: 'terminal:2', type: 'terminal', title: 'T2', pin: { scope: 'global' } },
|
||||
homeSessionId: 'home',
|
||||
})
|
||||
const result = injectPinnedIntoTree(s.splits, [vtab], vtab.id)
|
||||
if (result.kind === 'leaf') {
|
||||
expect(result.active).toBe(vtab.id)
|
||||
expect(result.tabs.map(t => t.id)).toContain(vtab.id)
|
||||
expect(result.tabs.map(t => t.id)).toContain('terminal:1')
|
||||
}
|
||||
})
|
||||
|
||||
it('injects into the first child of a split tree', () => {
|
||||
let s = makeDefaultState()
|
||||
s = openTabInActivePane(s, { id: 'terminal:1', type: 'terminal', title: 'T1' })
|
||||
// Create a split (splits the active pane into two)
|
||||
const leafId = s.splits.kind === 'leaf' ? s.splits.id : ''
|
||||
s = { ...s, splits: { kind: 'split', id: 'split:1', dir: 'row', sizes: [0.5, 0.5], children: [
|
||||
{ kind: 'leaf', id: leafId, tabs: s.splits.kind === 'leaf' ? s.splits.tabs : [], active: 'terminal:1' },
|
||||
{ kind: 'leaf', id: 'pane:2', tabs: [], active: null },
|
||||
] } }
|
||||
const vtab = createPinnedVirtualTab({
|
||||
tab: { id: 'terminal:2', type: 'terminal', title: 'T2', pin: { scope: 'global' } },
|
||||
homeSessionId: 'home',
|
||||
})
|
||||
const result = injectPinnedIntoTree(s.splits, [vtab], null)
|
||||
expect(result.kind).toBe('split')
|
||||
if (result.kind === 'split') {
|
||||
const first = result.children[0]!
|
||||
if (first.kind === 'leaf') {
|
||||
expect(first.tabs.map(t => t.id)).toContain(vtab.id)
|
||||
}
|
||||
const second = result.children[1]!
|
||||
if (second.kind === 'leaf') {
|
||||
expect(second.tabs.map(t => t.id)).not.toContain(vtab.id)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -21,6 +21,7 @@ function makeKey(overrides: Partial<TabContentMemoKey> = {}): TabContentMemoKey
|
||||
revealed: [],
|
||||
localeRevision: 'en-US',
|
||||
tabsVersion: 0,
|
||||
effectiveTabId: undefined,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user