From d035836a511ca04fa4e8b8eceaddcd0b7f678f8e Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Sun, 11 Oct 2026 23:35:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(tasks):=20=E8=87=AA=E5=8A=A8=E9=80=82?= =?UTF-8?q?=E9=85=8D=E6=94=B9=E4=B8=BA=E6=BB=91=E8=A1=8C=E2=80=94=E2=80=94?= =?UTF-8?q?=E5=B1=95=E5=BC=80=E5=90=8E=E4=BB=8D=E4=BF=9D=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E9=80=82=E5=BA=94=E5=B1=85=E4=B8=AD=EF=BC=8C=E4=B8=94=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E7=9E=AC=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 维护者第三轮反馈:在 fit(自适应)档位下展开后,画面也应回到自适应位置, 避免节点溢出面板 / 不在中间。上一版让折叠「接管视图」正是拿这个换的平滑。 收口方式:把视图变换本身也做成补间。 - 新增 tasks-view-glide.ts:把 fit 的算术抽成纯函数 fitTransform(用 CONTENT 盒而 非含 padding 的整张画布定缩放、双轴居中、容器未测量时返回 undefined),外加 lerpTransform 与 GLIDE_MS。这两块在 jsdom 里可达,因此有了单测(居中、上下限、 未测量、滑行中点)。 - TasksGraph:fit 拆成 fitTarget(算术)+ applyFit(animate)。布局变化且视图仍属自动 适配时→**滑行**(rAF 220ms,与卡片补间同一条缓动);切会话→直接落位(内容整体 换掉);⌂ / 背景双击→滑回。任何读者手势(pointerdown / 滚轮 / 缩放按钮)立刻取消 滑行,并照旧把视图标记为「已接管」。 - 折叠不再接管视图(去掉上一版的 markTouched 包装):展开即滑到新取景,画面保持 居中且内容完整。 测试:tests/tasks-view-glide.spec.ts(4 条:居中与缩放上限/下限、未测量返回 undefined、 滑行插值)与 tests/tasks-motion.spec.ts 第 4 条改写(布局变化必须滑行、切会话必须落位、 手势必须能取消)。全套 2252 passed / 17 skipped(194 文件)、tsc 与 eslint 零告警、 构建通过;新构建已装进 sidebar-test,:3080 挂载正常(0 pageerror)。 --- CHANGELOG.md | 2 +- .../2026-10-11-tasks-status-and-group-fold.md | 7 +- src/client/TasksGraph.tsx | 153 +++++++++++------- src/client/tasks-view-glide.ts | 81 ++++++++++ tests/tasks-motion.spec.ts | 20 ++- tests/tasks-view-glide.spec.ts | 64 ++++++++ 6 files changed, 254 insertions(+), 73 deletions(-) create mode 100644 src/client/tasks-view-glide.ts create mode 100644 tests/tasks-view-glide.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e7b0df7..1f8e0e6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ - 📊 **节点详情多两项事实**:新增 **Token 用量**(宿主 `tokenUsage` projection 的四个互斥桶求和,悬停给出未缓存输入 / 缓存读取 / 缓存写入 / 输出明细)与 **耗时**(宿主 `subagentTiming`:已结算回合 + 正在跑的那一轮,按页面轮询刷新)。两项都按宿主投影的**存在性**渲染——没有测量就不显示这一行,绝不打印一个说不清的 0;`SidebarProjectionSnapshot.values` 的镜像类型同步补上这两个键。 - 🪟 **「查看转录」改为在原生右侧栏打开**(这是宿主自带能力):`dsh-resource://subagentchat/session/?parent=&mode=` + `ctx.sidebarRight.openResource(..., { kind: 'subagentchat', preferNewPane: true })`——与宿主自己的子代理列表(点行 = 主窗、点 chevron = 侧栏)同一套 tab 类型,主列保持不动;右侧新增小下拉按钮,菜单里可**在主窗中打开**(走原有的 `uiWorkspace.openSession` + 页签重挂)。宿主没有该 tab 类型 / 没有右栏 / 没有挂载会话时逐项降级回主窗并保持单按钮。 - ✨ **动效:画布在自己的坐标系里补间**(`tasks-layout-tween.ts`)。卡片层带着 fit 的 `transform: scale(k)`,所以任何「量 rect、播 transform」的 FLIP 库在这里都是错的——`@formkit/auto-animate` 更会把离场卡片钉上由 rect 换算的绝对 `left/top`(缩放层里根本表达不出来 → 跳位)。改为**直接补间布局坐标**(每张卡的 `x`/`y`/高度):`left`/`top` 就是画布自己的单位,缩放对它和对终点位置一样生效;而连线路径与相位框都是「盒子的纯函数」,于是**跟着卡片逐帧走**,不再在卡片还在飞的时候先瞬移到终点。入场另有 CSS 关键帧(300ms:上浮 + 0.88→1 缩放 + 淡入,带轻微过冲),并按节点顺序**错峰 26ms**(一次展开多张时是瀑布而不是一下全冒),`backwards` 填充让等待中的卡片先隐形而不是先冒再动。 -- 🧭 **读者一碰折叠,视图就归他**:点聚合 / 点卡片 chevron / 控制条折叠开关都会把视图标记为「已接管」(与拖拽、缩放同一条规则),此后画布不再随布局变化自动重框——折叠时不会再把整棵树从读者眼皮底下抽走;⌂ 按钮交还自动适配。切会话时重置并重新适配(新的树 = 新的画布),顺带修掉「拖过一次之后换会话就不再适配」的旧账。 +- 🧭 **自动适配改成「滑过去」而不是「跳过去」**:布局一变,视图变换(平移 + 缩放)与卡片一起补间 220ms,于是**展开后画面仍然是自适应的居中位置**(不会让节点溢出面板/偏到一角),同时也没有原来那种瞬跳。规则不变:读者拖过或缩放过就不再自动重框(那一刻视图归他),⌂ 按钮随时交还自动适配;切会话重置并重新适配(新的树 = 新的画布),顺带修掉「拖过一次之后换会话就不再适配」的旧账。 - 🪶 **FLIP 库只留给流式布局**:树模式行与后台任务行的离场是「原地淡出 + 兄弟滑位」,那正是 FLIP 的语义且不受缩放影响;画布则明确排除(`tests/tasks-motion.spec.ts` 钉死:画布不得 import 它、树与抽屉必须有)。 - 🧪 测试:新增 `tests/session-metrics.spec.ts`(5 条:四桶求和、空投影、运行中耗时按渲染时钟、0 不算工作、时钟回拨不显示负数)与 `tests/activity-icons.spec.ts`(3 条:13 个类别全覆盖、读 / 命令 / 搜索 / 网页互不同型、`undefined` 走思考图标);`tests/tasks-page.spec.tsx` 补 6 条(原生状态取代状态词、无数据回退、根卡真实状态、浮窗活动块位置、一次只开一组 + 判据极性的判别性用例、展开后成员仍可手工折回、Token / 耗时两行、侧栏打开地址 + 下拉在主窗打开 + 无宿主能力时单按钮、模式 chip 在徽标行);`tests/tasks-graph-layout.spec.ts` 的高度 / 行距断言跟随新的卡片预算。[设计文档](docs/plans/2026-10-11-tasks-status-and-group-fold.md)。 diff --git a/docs/plans/2026-10-11-tasks-status-and-group-fold.md b/docs/plans/2026-10-11-tasks-status-and-group-fold.md index 653595cb..e72cf443 100644 --- a/docs/plans/2026-10-11-tasks-status-and-group-fold.md +++ b/docs/plans/2026-10-11-tasks-status-and-group-fold.md @@ -137,8 +137,11 @@ state === 'running' → liveActivityLabel(summary) // 宿主 mess 目标盒子渲染(否则会有「先消失一帧再出现」的闪烁,首版实测就踩到了)。 - **入场用 CSS 关键帧**:300ms 上浮 + `scale(0.88→1)` + 淡入(轻微过冲),按节点页序**错峰 26ms**,`fill-mode: backwards` 让等待中的卡片先隐形。`prefers-reduced-motion` 下补间与入场都关闭。 - - **读者碰折叠即接管视图**:`markTouched` 挂在三个折叠入口上(聚合卡、卡片 chevron、控制条开关),此后不再随布局自动重框; - 切会话重置并重新适配(顺带修掉「拖过一次后换会话不再适配」)。 + - **自动适配改为滑行**(维护者第三轮反馈:「最开始在 fit 档位,展开后也要是自适应的位置,避免节点溢出屏幕 / 不在中间」)。 + 上一版让折叠「接管视图」,代价正是这一条。收口方式:**视图变换本身也补间**(`tasks-view-glide.ts`:把 fit 的算术抽成 + 纯函数并单测,`applyFit(animate)` 用 rAF 把 `x/y/k` 从当前位置滑到新目标,220ms 与卡片同一条缓动)。于是 + 「保持自适应」与「不许瞬跳」不再二选一:布局一变就滑到新取景,展开后画面仍是居中、内容仍完整;只有读者拖过 / 缩放过 + (`touchedRef`)才不再自动重框,任何读者手势(pointerdown / 滚轮 / 缩放按钮)都会立刻取消滑行。切会话仍旧直接落位(内容整体换掉)。 - **FLIP 库保留在流式布局**(树视图、任务抽屉);`tests/tasks-motion.spec.ts` 把「画布不得 import 它」写进守护, `tests/tasks-layout-tween.spec.tsx` 钉住插值 / 缓动 / 中途位置 / 画布尺寸不中途变化 / 入场盒子不缺席。 2. **真机只跑通到「插件挂载 + 零 pageerror」**:新构建已装进 `sidebar-test` profile 并在 http://127.0.0.1:3080 提供服务(`[data-dsh-better-sidebar]` 挂载、`/sidebar/api/*` 200、无 pageerror / console error)。但**动效与观感的逐帧复核没做成**:宿主右栏在无头上下文里以 `transform` 平移到视口外,我的合成点击无法稳定地把任务页调出来(多次尝试后放弃)。这一条由维护者自己在实例上看。 diff --git a/src/client/TasksGraph.tsx b/src/client/TasksGraph.tsx index 933a74c0..4f46d505 100644 --- a/src/client/TasksGraph.tsx +++ b/src/client/TasksGraph.tsx @@ -41,6 +41,15 @@ import { type GraphBox, } from './tasks-graph-layout.ts' import { useTweenedLayout } from './tasks-layout-tween.ts' +import { easeOut as glideEase } from './tasks-layout-tween.ts' +import { fitTransform, GLIDE_MS, lerpTransform, type ViewTransform } from './tasks-view-glide.ts' + +/** Whether the reader asked the platform for less motion. */ +function prefersReducedMotion(): boolean { + return typeof window !== 'undefined' + && typeof window.matchMedia === 'function' + && window.matchMedia('(prefers-reduced-motion: reduce)').matches +} import { activityLine, agentChip, agentMeta, foldErrorTally, foldPreviews, FoldGlyph, foldTally, TaskLine, WorkflowGlyph, workflowMeta, @@ -223,35 +232,69 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { const edges = useMemo(() => tasksEdges(nodes), [nodes]) const nodeById = useMemo(() => new Map(nodes.map(node => [node.id, node])), [nodes]) + /** A running view glide (see fit): cancelled by any reader gesture. */ + const glideRef = useRef(0) + + /** Stop a glide where it is (the reader took the wheel). */ + const stopGlide = useCallback((): void => { + if (glideRef.current !== 0) { + cancelAnimationFrame(glideRef.current) + glideRef.current = 0 + } + }, []) + /** - * Center the canvas and scale it to fill. The fit measures the CONTENT box - * (the layout's padding is breathing room, not content): centering the padded - * box leaves the graph visibly off-center in a narrow panel, which is exactly - * how "the graph looks small and sits in a corner" happens. + * The frame the auto-fit wants right now, or undefined before the container + * has been measured (the sidebar mounts hidden at zero size). The arithmetic + * lives in tasks-view-glide.ts, where a test can reach it. */ - const fit = useCallback((): void => { + const fitTarget = useCallback((): ViewTransform | undefined => { const container = containerRef.current - if (container === null) return - const cw = container.clientWidth - const ch = container.clientHeight - if (cw === 0 || ch === 0) return - const contentW = Math.max(GRAPH_NODE_W, layout.width - GRAPH_PAD * 2) - const contentH = Math.max(GRAPH_NODE_H, layout.height - GRAPH_PAD * 2) - const k = Math.max( - GRAPH_FIT_MIN_SCALE, - Math.min( - (cw - FIT_MARGIN * 2) / contentW, - (ch - FIT_MARGIN * 2) / contentH, - FIT_MAX_SCALE, - ), - ) - setTf({ - x: (cw - contentW * k) / 2 - GRAPH_PAD * k, - y: Math.max(FIT_MARGIN / 2, (ch - contentH * k) / 2) - GRAPH_PAD * k, - k, + if (container === null) return undefined + return fitTransform({ + containerW: container.clientWidth, + containerH: container.clientHeight, + layoutW: layout.width, + layoutH: layout.height, + pad: GRAPH_PAD, + minContentW: GRAPH_NODE_W, + minContentH: GRAPH_NODE_H, + margin: FIT_MARGIN, + minScale: GRAPH_FIT_MIN_SCALE, + maxScale: FIT_MAX_SCALE, }) }, [layout.width, layout.height]) + /** + * Put the frame where the fit wants it, either at once (a resize while the + * reader drags the panel edge, a fresh session) or as a GLIDE — the layout + * tween moves the cards, and the frame has to travel with them instead of + * jumping to the new framing a frame later. That glide is what makes "stay + * auto-fitted" and "never yank the view" the same behavior. + * + * @param animate - glide instead of snapping. + */ + const applyFit = useCallback((animate: boolean): void => { + const target = fitTarget() + if (target === undefined) return + stopGlide() + if (!animate || prefersReducedMotion()) { + setTf(target) + return + } + const from = tfRef.current + const started = performance.now() + const step = (): void => { + const progress = glideEase((performance.now() - started) / GLIDE_MS) + setTf(lerpTransform(from, target, progress)) + glideRef.current = progress < 1 ? requestAnimationFrame(step) : 0 + } + glideRef.current = requestAnimationFrame(step) + }, [fitTarget, stopGlide]) + + /** The reader's own re-fit (the ⌂ button and a background double-click). */ + const fit = useCallback((): void => { applyFit(true) }, [applyFit]) + /** * Keep the canvas reachable: a pan or a zoom-out may never push the whole * graph off screen (the classic "I dragged it away and cannot find it"). @@ -281,22 +324,24 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { fit() }, [fit]) - /** The newest `fit`, so the effects below can depend on WHAT changed. */ - const fitRef = useRef(fit) - fitRef.current = fit + /** The newest `applyFit`, so the effects below can depend on WHAT changed. */ + const fitRef = useRef(applyFit) + fitRef.current = applyFit // A different tree is a different canvas: re-fit AND hand the view back to the - // auto-fit, whatever the reader did to the previous one. + // auto-fit, whatever the reader did to the previous one. Snapped, because the + // content changes wholesale. useEffect(() => { touchedRef.current = false - fitRef.current() + fitRef.current(false) }, [rootId]) - // Auto-fit on layout growth — but never over a view the reader has taken over - // (a pan, a zoom, or a fold: see markTouched). The container's own resize - // path below fits under the same rule. + // Layout growth while the reader has left the view alone: glide to the new + // framing, so expanding a group KEEPS the graph centred and inside the panel + // without the frame jumping out from under the cards travelling to it. A + // reader who panned or zoomed owns the view, and nothing moves it. useEffect(() => { - if (!touchedRef.current) fitRef.current() + if (!touchedRef.current) fitRef.current(true) }, [layout.width, layout.height]) useEffect(() => { @@ -328,6 +373,7 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { const next = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, k * Math.exp(-event.deltaY * unit * 0.0014))) if (next === k) return touchedRef.current = true + stopGlide() setTf(clampTf({ x: mx - (mx - x) * (next / k), y: my - (my - y) * (next / k), @@ -336,7 +382,7 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { } container.addEventListener('wheel', onWheel, { passive: false }) return () => { container.removeEventListener('wheel', onWheel) } - }, [clampTf]) + }, [clampTf, stopGlide]) const zoomBy = useCallback((factor: number): void => { const container = containerRef.current @@ -344,10 +390,11 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { const next = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, k * factor)) if (next === k || container === null) return touchedRef.current = true + stopGlide() const mx = container.clientWidth / 2 const my = container.clientHeight / 2 setTf(clampTf({ x: mx - (mx - x) * (next / k), y: my - (my - y) * (next / k), k: next })) - }, [clampTf]) + }, [clampTf, stopGlide]) /** * Drag-pan. The gesture only ever starts on the BACKGROUND: a pointerdown @@ -370,6 +417,7 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { const onPointerDown = useCallback((event: ReactPointerEvent): void => { if (event.button !== 0) return + stopGlide() const target = event.target as HTMLElement if (target.closest('[data-graph-node]') !== null) return if (target.closest('[data-graph-controls]') !== null) return @@ -395,36 +443,17 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { window.addEventListener('pointermove', onMove) window.addEventListener('pointerup', onUp) window.addEventListener('pointercancel', onUp) - }, [clampTf]) + }, [clampTf, stopGlide]) /** Suppress node clicks that ended a drag gesture. */ const clickAllowed = useCallback((): boolean => travelRef.current <= CLICK_TOLERANCE_PX, []) - /** - * The reader driving the FOLD state takes the view over, exactly like a pan - * or a zoom does: from then on the canvas stops re-framing itself when the - * layout changes, so folding a group never yanks the tree out from under the - * cards the reader is watching. The ⌂ button hands the view back. + /* + * Folding does NOT claim the view. The frame stays auto-fitted for as long as + * the reader has not panned or zoomed, and the fit GLIDES (applyFit), so a + * group that expands larger than the panel stays centred and inside it + * without the jump that claiming the view used to trade for. */ - const markTouched = useCallback((): void => { touchedRef.current = true }, []) - - /** The aggregate's click: reveal its group, with the view under the reader. */ - const expandGroupTouched = useCallback((node: TasksFoldNode): void => { - markTouched() - onExpandGroup(node) - }, [markTouched, onExpandGroup]) - - /** The card's own chevron: fold that node, with the view under the reader. */ - const foldNodeTouched = useCallback((node: TasksAgentNode): void => { - markTouched() - onFoldNode?.(node) - }, [markTouched, onFoldNode]) - - /** The cluster's page-level switch: the same claim on the view. */ - const toggleFoldTouched = useCallback((): void => { - markTouched() - onToggleFold() - }, [markTouched, onToggleFold]) // Phase frames: group each run's members by phase and box them. const phaseFrames = useMemo((): PhaseFrame[] => { @@ -561,16 +590,16 @@ export function TasksGraph(props: TasksGraphProps): ReactNode { */ animationDelay: `${Math.min(index, 9) * 26}ms`, } - if (node.kind === 'fold') return renderFoldNode(node, style, expandGroupTouched, clickAllowed) + if (node.kind === 'fold') return renderFoldNode(node, style, onExpandGroup, clickAllowed) if (node.kind === 'workflow') { return renderWorkflowNode(node, style, onWorkflowInfo, clickAllowed) } - return renderAgentNode(node, style, onNodeInfo, onOpenTask, clickAllowed, foldNodeTouched) + return renderAgentNode(node, style, onNodeInfo, onOpenTask, clickAllowed, onFoldNode) })}
- +