feat(ui): 自由窗口——tab 拖出侧边栏成为可移动/缩放/置顶的浮动窗口 (v0.16.0)

- 拖出:标签栏 tab 拖到主会话列(HTML5 DnD 检测 + 虚线提示浮层)或右键菜单
  「移动到自由窗口」(右/底面板共用 TabBar),tab 从 pane 移动进 SidebarState.floats
- 窗口(FreeWindow,渲染于 [data-dsh-panel-host] z-42):头部 pointer 拖动
  (rAF 直写 + 松手提交)、拖到 pane 高亮停靠(center 合并)、SE 角缩放、
  点击置顶、头部右键(回到侧边栏/关闭)、X = closeTab 走正常生命周期
- 服务语义:openTab dedupe/id 聚焦浮动 tab = 置顶窗口(不展开面板);
  closeTab/activateTab 兼顾 floats;agent 终端 reconcile 覆盖浮窗;
  features 单调新增 'floatWindows',版本 0.16.0
- 持久化:floats 随会话 localStorage,sanitizeState 宽容校验 + 几何钳制,
  uid 播种覆盖 float 前缀;i18n 三键 19 语言同步
- 关键修复(e2e 发现):portal 覆盖层(头部 Menu)的合成事件沿 React 树
  冒泡回拖拽表面,劫持菜单行点击——拖拽起点加 DOM 同源守卫(jsdom 不复现)
- e2e-mount.sh:缺省 tarball 改取 mtime 最新(旧字典序选择会把冒烟挂到
  过期产物);新增 tests/e2e/float-window.e2e.ts 双用例
This commit is contained in:
Menghuan1918 committed 2026-08-23 23:44:18 +08:00
1 parent d6d8db0416
commit f17a1b30ac
39 files changed
+2100 -96

No files matched your search

+289 -4
View File
@@ -1,10 +1,12 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import {
activateTab, allLeaves, BOTTOM_DEFAULT, BOTTOM_MIN, closeTab, createSidebarStore,
insertLeafAt, makeDefaultState, migrateBottomTabs, moveTab, moveTabToEdge, openDiffTab,
openTabInActivePane, patchTab, resizeSplit, resizeSplitIn, sanitizeState, setBottomHeight,
activateTab, allLeaves, BOTTOM_DEFAULT, BOTTOM_MIN, closeFloatByTab, closeTab, createSidebarStore,
dockFloat, FLOAT_MIN_H, FLOAT_MIN_W, floatTab, floatWithTab, insertLeafAt, makeDefaultState,
migrateBottomTabs, moveFloat, moveTab, moveTabToEdge, openDiffTab,
openTabInActivePane, patchTab, raiseFloat, reconcileAgentTerminals, resizeFloat, resizeSplit,
resizeSplitIn, sanitizeState, setBottomHeight,
splitPane, tabOpenIn, toggleBottomPanel, toggleExpanded, togglePanel,
type SidebarState, type SidebarTab, type SplitNode,
type SidebarLeaf, type SidebarState, type SidebarTab, type SplitNode,
} from '../src/client/state.ts'
describe('sidebar state', () => {
@@ -896,3 +898,286 @@ describe('v0.12.0 store additions', () => {
})
})
})
describe('free windows (v0.16.0)', () => {
// Node environment: no window → the viewport clamps are Infinity and only
// the size floors apply; the geometry tests stub a window where clamping
// is the point under test.
const state = (): SidebarState => makeDefaultState()
it('floatTab moves a docked tab into floats (pane keeps its other tabs)', () => {
let s = state()
const git = { id: 'git', type: 'git' as const, title: 'Git' }
const term = { id: 'term:1', type: 'terminal' as const, title: 'Term' }
s = openTabInActivePane(s, git)
s = openTabInActivePane(s, term)
const floated = floatTab(s, git.id, 300, 200)
const leaf = floated.splits as SidebarLeaf
expect(leaf.tabs).toHaveLength(2)
expect(leaf.tabs.some(tab => tab.id === 'git')).toBe(false)
// The pane's active pointer fell to the last remaining tab.
expect(leaf.active).toBe('term:1')
expect(floated.floats).toHaveLength(1)
expect(floated.floats[0]!.tab).toEqual(git)
// Default size centered on the drop point (no viewport → floors only).
expect(floated.floats[0]).toMatchObject({ x: 300 - 240, y: 200 - 180, w: 480, h: 360 })
expect(floated.activePane).toBe(leaf.id)
})
it('floatTab collapses a pane it empties and repoints the active pane', () => {
let s = state()
const paneId = (s.splits as SidebarLeaf).id
s = splitPane(s, 'row')
const fresh = allLeaves(s.splits).find(leaf => leaf.id !== paneId)!
s = { ...s, activePane: fresh.id }
s = openTabInActivePane(s, { id: 'term:1', type: 'terminal', title: 'T' })
expect(allLeaves(s.splits).find(leaf => leaf.id === fresh.id)!.tabs).toHaveLength(1)
const floated = floatTab(s, 'term:1', 100, 100)
expect(floated.floats).toHaveLength(1)
expect(allLeaves(floated.splits).some(leaf => leaf.id === fresh.id)).toBe(false)
expect(floated.activePane).toBe(paneId)
})
it('floatTab with an unknown or already-floating tab id is a strict no-op', () => {
let s = state()
const git = { id: 'git', type: 'git' as const, title: 'Git' }
s = openTabInActivePane(s, git)
expect(floatTab(s, 'nope', 0, 0)).toBe(s)
const floated = floatTab(s, 'git', 50, 50)
expect(floatTab(floated, 'git', 60, 60)).toBe(floated)
})
it('moveFloat / resizeFloat clamp to the viewport (floors always)', () => {
const g = globalThis as Record<string, unknown>
const previous = g.window
g.window = { innerWidth: 1024, innerHeight: 768 }
try {
let s = state()
const git = { id: 'git', type: 'git' as const, title: 'Git' }
s = floatTab(openTabInActivePane(s, git), 'git', 512, 384)
const id = s.floats[0]!.id
// Negative position clamps to 0; the window stays fully inside.
let moved = moveFloat(s, id, -50, -50)
expect(moved.floats[0]).toMatchObject({ x: 0, y: 0 })
// Beyond the right/bottom edge clamps the top-left so the window fits.
moved = moveFloat(moved, id, 5000, 5000)
expect(moved.floats[0]!.x).toBe(1024 - 480)
expect(moved.floats[0]!.y).toBe(768 - 360)
// No-op move returns the same reference (no persist churn).
expect(moveFloat(moved, id, moved.floats[0]!.x, moved.floats[0]!.y)).toBe(moved)
// Resize: floors, viewport ceiling, and the SE-corner anchor (x/y keep).
let resized = resizeFloat(moved, id, 10, 10)
expect(resized.floats[0]).toMatchObject({ w: FLOAT_MIN_W, h: FLOAT_MIN_H })
resized = resizeFloat(resized, id, 5000, 5000)
expect(resized.floats[0]!.w).toBe(1024 - resized.floats[0]!.x)
expect(resized.floats[0]!.h).toBe(768 - resized.floats[0]!.y)
// No-op resize and unknown ids return the same reference.
expect(resizeFloat(resized, id, resized.floats[0]!.w, resized.floats[0]!.h)).toBe(resized)
expect(resizeFloat(resized, 'nope', 400, 400)).toBe(resized)
} finally {
if (previous === undefined) delete g.window
else g.window = previous
}
})
it('raiseFloat moves a window to the top and is idempotent at the top', () => {
let s = state()
s = floatTab(openTabInActivePane(s, { id: 'a', type: 'db', title: 'a' }), 'a', 100, 100)
s = floatTab(openTabInActivePane(s, { id: 'b', type: 'db', title: 'b' }), 'b', 100, 100)
s = floatTab(openTabInActivePane(s, { id: 'c', type: 'db', title: 'c' }), 'c', 100, 100)
expect(s.floats.map(f => f.tab.id)).toEqual(['a', 'b', 'c'])
const raised = raiseFloat(s, s.floats[0]!.id)
expect(raised.floats.map(f => f.tab.id)).toEqual(['b', 'c', 'a'])
// Already topmost / unknown id → same reference (no notify churn).
expect(raiseFloat(raised, raised.floats[2]!.id)).toBe(raised)
expect(raiseFloat(raised, 'float:none')).toBe(raised)
// A single window has nothing to raise: same reference.
let single = state()
single = floatTab(single, (single.splits as SidebarLeaf).tabs[0]!.id, 0, 0)
expect(single.floats).toHaveLength(1)
expect(raiseFloat(single, single.floats[0]!.id)).toBe(single)
})
it('dockFloat lands the tab in the target pane (or the active pane) and activates it', () => {
let s = state()
const git = { id: 'git', type: 'git' as const, title: 'Git' }
s = floatTab(openTabInActivePane(s, git), 'git', 100, 100)
const floatId = s.floats[0]!.id
// Explicit target pane.
s = splitPane(s, 'row')
const target = allLeaves(s.splits).find(leaf => leaf.tabs.length === 0)!
const docked = dockFloat(s, floatId, target.id)
expect(docked.floats).toHaveLength(0)
expect(allLeaves(docked.splits).find(leaf => leaf.id === target.id)!.tabs.map(tab => tab.id)).toEqual(['git'])
expect(docked.activePane).toBe(target.id)
// Default target: the active pane.
let s2 = state()
const home = (s2.splits as SidebarLeaf).tabs[0]!
s2 = splitPane(s2, 'row')
s2 = floatTab(s2, home.id, 100, 100)
const active = s2.activePane!
const docked2 = dockFloat(s2, s2.floats[0]!.id)
expect(docked2.floats).toHaveLength(0)
expect(allLeaves(docked2.splits).find(leaf => leaf.id === active)!.tabs.some(tab => tab.id === home.id)).toBe(true)
expect(docked2.activePane).toBe(active)
// Stale target falls back to the right tree's first leaf.
let s3 = state()
const home3 = (s3.splits as SidebarLeaf).tabs[0]!
s3 = floatTab(s3, home3.id, 0, 0)
const docked3 = dockFloat(s3, s3.floats[0]!.id, 'pane:gone')
expect(docked3.floats).toHaveLength(0)
expect(allLeaves(docked3.splits).some(leaf => leaf.tabs.some(tab => tab.id === home3.id))).toBe(true)
// Unknown float id is a strict no-op.
expect(dockFloat(docked3, 'float:gone')).toBe(docked3)
})
it('tabOpenIn / patchTab / floatWithTab see floating tabs', () => {
let s = state()
const git = { id: 'git', type: 'git' as const, title: 'Git' }
s = floatTab(openTabInActivePane(s, git), 'git', 100, 100)
expect(tabOpenIn(s, 'git')).toBe(true)
expect(floatWithTab(s, 'git')!.id).toBe(s.floats[0]!.id)
const patched = patchTab(s, 'git', { title: 'Git 2', path: '/x' })
expect(patched.floats[0]!.tab).toMatchObject({ title: 'Git 2', path: '/x' })
expect(allLeaves(patched.splits).some(leaf => leaf.tabs.some(tab => tab.id === 'git'))).toBe(false)
})
it('closeFloatByTab removes exactly the window holding the tab', () => {
let s = state()
const git = { id: 'git', type: 'git' as const, title: 'Git' }
const term = { id: 'term:1', type: 'terminal' as const, title: 'T' }
s = openTabInActivePane(s, git)
s = openTabInActivePane(s, term)
s = floatTab(s, 'git', 50, 50)
s = floatTab(s, 'term:1', 60, 60)
expect(s.floats).toHaveLength(2)
const after = closeFloatByTab(s, 'git')
expect(after.floats.map(f => f.tab.id)).toEqual(['term:1'])
expect(closeFloatByTab(after, 'git')).toBe(after)
})
it('openTabInActivePane focuses a floating tab by raising (id safety net)', () => {
let s = state()
const git = { id: 'git', type: 'git' as const, title: 'Git' }
const term = { id: 'term:1', type: 'terminal' as const, title: 'T' }
s = floatTab(openTabInActivePane(s, git), 'git', 50, 50)
s = floatTab(openTabInActivePane(s, term), 'term:1', 60, 60)
// Reopening the floated id must NOT duplicate: it raises the window.
const after = openTabInActivePane(s, { id: 'git', type: 'git', title: 'Git' })
expect(after.floats.map(f => f.tab.id)).toEqual(['term:1', 'git'])
expect(allLeaves(after.splits).some(leaf => leaf.tabs.some(tab => tab.id === 'git'))).toBe(false)
})
it('reconcileAgentTerminals removes a vanished FLOATED agent terminal with its window', () => {
let s = state()
s = openTabInActivePane(s, { id: 'agent:u1', type: 'terminal', title: 'A1' })
s = floatTab(s, 'agent:u1', 50, 50)
s = openTabInActivePane(s, { id: 'agent:u2', type: 'terminal', title: 'A2' })
expect(s.floats).toHaveLength(1)
const after = reconcileAgentTerminals(s, [{ uuid: 'u2', title: 'A2' }])
expect(after.floats).toHaveLength(0)
expect(allLeaves(after.splits).flatMap(leaf => leaf.tabs).some(tab => tab.id === 'agent:u2')).toBe(true)
})
describe('sanitizeState (floats)', () => {
const g = globalThis as Record<string, unknown>
beforeEach(() => {
g.window = { clearTimeout: () => {}, setTimeout: () => 0, innerWidth: 1024, innerHeight: 768 }
g.localStorage = { getItem: () => null, setItem: () => {} }
})
afterEach(() => {
delete g.window
delete g.localStorage
})
const base = (): Record<string, unknown> => ({
panelOpen: true,
width: 400,
nextTerminal: 1,
activePane: 'pane:1',
expanded: [],
splits: { kind: 'leaf', id: 'pane:1', active: null, tabs: [] },
})
it('a missing floats field defaults to none (older persisted states load)', () => {
const restored = sanitizeState(base())!
expect(restored.floats).toEqual([])
})
it('drops malformed entries individually and keeps the layout (unlike tree corruption)', () => {
const parsed = base()
parsed.floats = [
'garbage',
{ id: 'float:1' }, // no tab
{ id: 'float:2', tab: { id: 't', type: 'git', title: 'G' }, x: 10, y: 10 }, // no w/h
{ id: 'float:3', tab: { id: 't3', type: 'git', title: 'G3' }, x: 10, y: 10, w: 500, h: 400 },
{ id: 'float:3', tab: { id: 't3b', type: 'git', title: 'G3b' }, x: 0, y: 0, w: 400, h: 300 }, // duplicate id
]
const restored = sanitizeState(parsed)!
expect(restored.floats).toHaveLength(1)
expect(restored.floats[0]).toMatchObject({ id: 'float:3', tab: { id: 't3' }, x: 10, y: 10 })
expect(restored.splits).toBeDefined()
})
it('drops ephemeral diff tabs and migrates explorer tabs inside floats', () => {
const parsed = base()
parsed.floats = [
{ id: 'float:1', tab: { id: 'd', type: 'diff', title: 'D' }, x: 0, y: 0, w: 400, h: 300 },
{ id: 'float:2', tab: { id: 'ex', type: 'explorer', title: 'Explorer' }, x: 0, y: 0, w: 400, h: 300 },
]
const restored = sanitizeState(parsed)!
expect(restored.floats.map(f => f.tab.type)).toEqual(['editor'])
expect(restored.floats[0]!.tab).toMatchObject({ title: 'Files', meta: { treeOpen: true } })
})
it('clamps stale off-screen geometry into the current viewport', () => {
const parsed = base()
parsed.floats = [
{ id: 'float:1', tab: { id: 't', type: 'git', title: 'G' }, x: -200, y: 900, w: 5000, h: 5000 },
]
const restored = sanitizeState(parsed)!
// Sizes cap at the viewport, and the capped window lands at 0,0 (the
// position clamp sees no room beyond it).
expect(restored.floats[0]).toMatchObject({ x: 0, y: 0, w: 1024, h: 768 })
})
it('the uid counter seeds past persisted float ids (no collision on re-float)', () => {
// Persisted: the pane max is 1, but floats reach float:2 — the counter
// must seed to 2 so a fresh float mints float:3+, never a duplicate
// float:2. (Intermediate mints during sanitize may raise it further;
// the guarantee under test is uniqueness, not an exact value.)
const parsed = base()
parsed.floats = [
{ id: 'float:2', tab: { id: 't2', type: 'git', title: 'G2' }, x: 0, y: 0, w: 400, h: 300 },
]
const saved = new Map<string, string>()
saved.set('dsh-sidebar:v1:seedtest', JSON.stringify(parsed))
g.localStorage = {
getItem: (key: string) => saved.get(key) ?? null,
setItem: (key: string, value: string) => { saved.set(key, value) },
}
const store = createSidebarStore()
store.setSession('seedtest')
const s = store.getSnapshot().state!
expect(s.floats[0]!.id).toBe('float:2')
store.reduce(cur => openTabInActivePane(cur, { id: 'tab:9', type: 'git', title: 'G9' }))
store.reduce(cur => floatTab(cur, 'tab:9', 30, 30))
const next = store.getSnapshot().state!
expect(next.floats).toHaveLength(2)
expect(new Set(next.floats.map(f => f.id)).size).toBe(2)
const freshId = next.floats.map(f => f.id).find(id => id !== 'float:2')!
expect(freshId).toMatch(/^float:\d+$/)
expect(Number(freshId.slice('float:'.length))).toBeGreaterThan(2)
})
it('floats survive a full persist round-trip (geometry verbatim when in-viewport)', () => {
const parsed = base()
parsed.floats = [
{ id: 'float:1', tab: { id: 't', type: 'git', title: 'G', meta: { k: 1 } }, x: 12, y: 34, w: 480, h: 360 },
]
const restored = sanitizeState(parsed)!
const again = sanitizeState(JSON.parse(JSON.stringify(restored)))!
expect(again.floats).toEqual(restored.floats)
})
})
})