Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(file-browser): 把 #4436 的横向滚动契约移植进虚拟化文件树

Zhang Yunjin14237f1da3ef
diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx
index 4c2aa7b7ab..3e7ba6040c 100644
--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/FileTreeView.tsx
@@ -192,6 +192,31 @@ function isHtmlPath(filePath: string): boolean {
return /\.(html?|xhtml)$/i.test(filePath);
}
+/**
+ * 把 `data-relpath` 对应的行在横轴上对齐到容器可见区(#4436 契约:行宽由内容
+ * 决定,深层缩进 + 长名字会撑出横向滚动区)。
+ *
+ * 非虚拟化版靠 `scrollIntoView` 默认的 `inline: 'nearest'` 一并处理横轴;虚拟化下
+ * 目标行常常是 `scrollToIndex` 之后才被渲染出来,且行绝对定位在宽 100% 的层里、
+ * 自身 `min-w-max` —— 直接量行的矩形更干净。只动横轴、不动纵轴,不跟虚拟器的
+ * 纵向居中打架;行还没渲染(虚拟器还没把它带进视口)时静默跳过。
+ */
+function alignRowHorizontally(container: HTMLElement | null, relPath: string): void {
+ if (!container) return;
+ // 按 dataset.relpath 精确比对,不走属性选择器:relPath 自带 `.` / `/` / `[]`
+ // 等字符,选择器得先转义(且 jsdom 下没有 CSS.escape);渲染中的行数有界(视口 +
+ // overscan),逐行比对代价可忽略。
+ const el = Array.from(container.querySelectorAll<HTMLElement>('[data-relpath]')).find(
+ (node) => node.dataset.relpath === relPath,
+ );
+ if (!el) return;
+ const row = el.getBoundingClientRect();
+ const box = container.getBoundingClientRect();
+ const delta =
+ row.right > box.right ? row.right - box.right : row.left < box.left ? row.left - box.left : 0;
+ if (delta !== 0) container.scrollLeft += delta;
+}
+
export const FileTreeView = forwardRef<FileTreeViewHandle, FileTreeViewProps>(function FileTreeView(
{
tree,
@@ -352,6 +377,13 @@ export const FileTreeView = forwardRef<FileTreeViewHandle, FileTreeViewProps>(fu
);
if (index < 0) return; // 行不在当前树里(父目录未展开 / 文件不存在),静默 no-op
virtualizerRef.current?.scrollToIndex(index, { align: 'center', behavior: 'smooth' });
+ // 横轴:目标行可能仍停在视口右侧之外(深层缩进 + 长名字把行撑宽),
+ // 纵向到位后把该行横向也拉进可见区。行是由虚拟器按需渲染的,所以
+ // 现在、下一帧、smooth 纵向滚动结束后各试一次(见 helper 注释)。
+ const container = containerRef.current;
+ alignRowHorizontally(container, relPath);
+ requestAnimationFrame(() => alignRowHorizontally(container, relPath));
+ window.setTimeout(() => alignRowHorizontally(container, relPath), 320);
},
}),
[],
diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx
index a78e714e63..b24946e97d 100644
--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/__tests__/FileTreeView.horizontalScroll.test.tsx
@@ -8,6 +8,10 @@
* 修复前行 depth ≥9 名字宽 0、scrollWidth 只有 280;修复后行宽跟随内容,depth 15
* 名字恢复全宽、scrollWidth 549,横滚可读到全名。
*
+ * 本用例在 p3(PR #4398)的虚拟化 FileTreeView 上同样成立:#4436 落地的是「行宽
+ * 内容驱动 + 容器承接横滚 + 横条常显」契约,虚拟化只改行的定位方式,不改契约;
+ * 行视图靠树视口替身(jsdom 无布局,否则虚拟器产 0 行)。
+ *
* 背景:窄面板(RSB 文件浏览器 200px)里深层目录会一路缩进,旧行 `w-full` +
* 名字 `truncate` 只会把内容挤成 0 宽,永远撑不出滚动区 —— 深层目标"消失"且没有
* 横向滚动条可救。CSS 规则存在性靠静态断言(jsdom 不加载样式表),与
@@ -16,15 +20,20 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
+import { createRef } from 'react';
import { cleanup, render, screen } from '@testing-library/react';
-import { afterEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
vi.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));
-import { FileTreeView } from '../FileTreeView';
+import { FileTreeView, type FileTreeViewHandle } from '../FileTreeView';
import type { DirEntry, UseFileTreeReturn } from '../hooks/useFileTree';
+import { installTreeViewportStub, resetTestViewportSize } from './treeViewportStub';
+
+// jsdom 无布局:不装视口替身的话虚拟器产出 0 行(见 treeViewportStub 注释)。
+beforeAll(installTreeViewportStub);
const globalsSrc = readFileSync(
resolve(__dirname, '..', '..', '..', '..', 'styles', 'globals.css'),
@@ -53,6 +62,8 @@ function makeTree(): UseFileTreeReturn {
loadingPaths: new Set(),
initialLoading: false,
loadError: null,
+ showIgnoredDirsSupported: true,
+ storeKey: 'test-store',
toggleFolder: vi.fn(),
collapseAll: vi.fn(),
refresh: vi.fn(async () => undefined),
@@ -60,12 +71,20 @@ function makeTree(): UseFileTreeReturn {
};
}
-afterEach(() => cleanup());
+afterEach(() => {
+ cleanup();
+ resetTestViewportSize();
+});
describe('FileTreeView 横向滚动契约', () => {
it('滚动容器承接横向溢出,并挂常显横条样式钩子(tree-hscroll)', () => {
const { container } = render(
- <FileTreeView tree={makeTree()} selectedPath={null} onSelectFile={vi.fn()} />,
+ <FileTreeView
+ tree={makeTree()}
+ scrollScope="test-tab"
+ selectedPath={null}
+ onSelectFile={vi.fn()}
+ />,
);
// 根节点即滚动容器(scrollToPath querySelector 也依赖它)。
@@ -78,6 +97,7 @@ describe('FileTreeView 横向滚动契约', () => {
render(
<FileTreeView
tree={makeTree()}
+ scrollScope="test-tab"
selectedPath={null}
onSelectFile={vi.fn()}
renamingPath="cat.png"
@@ -101,6 +121,44 @@ describe('FileTreeView 横向滚动契约', () => {
const pendingRow = screen.getByPlaceholderText('untitled').parentElement;
expect(pendingRow?.className).toContain('min-w-max');
});
+
+ it('reveal 目标行时把横轴也拉进可见区(深层宽行不会停在视口右侧外面)', () => {
+ const ref = createRef<FileTreeViewHandle>();
+ const { container } = render(
+ <FileTreeView
+ ref={ref}
+ tree={makeTree()}
+ scrollScope="test-tab"
+ selectedPath={null}
+ onSelectFile={vi.fn()}
+ />,
+ );
+
+ const scroll = container.firstElementChild as HTMLElement;
+ const row = scroll.querySelector<HTMLElement>('[data-relpath="deep/l2/l3"]');
+ expect(row).toBeTruthy();
+
+ // jsdom 无布局:手工摆出「容器 200px,目标行 40→320」的几何(行比容器宽 120px)。
+ const rect = (left: number, right: number): DOMRect =>
+ ({
+ left,
+ right,
+ top: 0,
+ bottom: 28,
+ width: right - left,
+ height: 28,
+ x: left,
+ y: 0,
+ toJSON: () => ({}),
+ }) as DOMRect;
+ scroll.getBoundingClientRect = () => rect(0, 200);
+ row!.getBoundingClientRect = () => rect(40, 320);
+
+ ref.current?.scrollToPath('deep/l2/l3');
+
+ // 修复前(只滚纵向)这里恒为 0:行名一直停在视口右侧外面。
+ expect(scroll.scrollLeft).toBe(120);
+ });
});
describe('globals.css 里的 .tree-hscroll 规则', () => {