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(desktop): diff 卡片横向滚动条常显,行底色延伸到整个滚动宽度 (#1044)

zhanghengxd81609d0eef6f
diff --git a/apps/desktop/src/renderer/__tests__/diffHorizontalScroll.test.tsx b/apps/desktop/src/renderer/__tests__/diffHorizontalScroll.test.tsx
new file mode 100644
index 0000000000..bccd295a71
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/diffHorizontalScroll.test.tsx
@@ -0,0 +1,105 @@
+// @vitest-environment jsdom
+/**
+ * diffHorizontalScroll.test.tsx
+ * ---------------------------------------------------------------------------
+ * Regression tests for 2026-07-30 用户反馈的聊天区 diff 横滚两个毛病:
+ * 1. 长行时横向滚动条看不见 —— 全局 thumb 默认透明(`.is-scrolling` 才显形),
+ * 而横向"内容还没完"没有任何其它视觉线索,用户不知道能滚。
+ * 2. 滚过去以后行背景(红/绿)只铺到一屏宽就断了 —— 行是 flex 容器,pre 宽度
+ * 锁在容器 100%,长行内容溢出到行边界之外,行背景不跟着长。
+ *
+ * DiffView 与 MarkdownDiffBlock 共用同一套契约:滚动容器带 `.diff-hscroll`
+ * (横向条常显)+ `overflow-x-auto`,其中的 pre 取 `w-max min-w-full`(行宽 =
+ * 最长行,底色跟着滚动区延伸)。
+ *
+ * 这里走真实渲染而不是源码扫描:两个类必须落在**同一个滚动容器**上、pre 必须
+ * 在该容器**里面**,契约才算成立 —— 正则扫源码会被组件里的注释文字命中,
+ * className 被删了也照过(#1044 review 指出的假阳性)。
+ * 只有 globals.css 的规则存在性仍靠静态断言:jsdom 不加载样式表。
+ */
+
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { cleanup, render } from '@testing-library/react';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { DiffView } from '@/components/chat/DiffView';
+import { MarkdownDiffBlock } from '@/components/chat/MarkdownDiffBlock';
+
+const globalsSrc = readFileSync(
+ resolve(__dirname, '..', 'styles', 'globals.css'),
+ 'utf8',
+);
+
+/** 长到必然触发横滚的一行,两个组件共用。 */
+const LONG = `const label = "${'x'.repeat(200)}";`;
+
+afterEach(() => cleanup());
+
+const cases: ReadonlyArray<[string, () => HTMLElement]> = [
+ [
+ 'DiffView',
+ () =>
+ render(<DiffView oldString={`${LONG}\nkeep\n`} newString={`${LONG}!\nkeep\n`} />)
+ .container,
+ ],
+ [
+ 'MarkdownDiffBlock',
+ () => render(<MarkdownDiffBlock raw={`- ${LONG}\n+ ${LONG}!\n keep`} />).container,
+ ],
+];
+
+describe('滚动容器契约', () => {
+ for (const [name, renderCase] of cases) {
+ it(`${name} 的横滚容器同时带 diff-hscroll 与 overflow-x-auto`, () => {
+ const container = renderCase();
+ const scrollers = container.querySelectorAll('.diff-hscroll');
+ // 只允许一个横滚容器:多一个说明结构被拆开,底色/滚动条契约要重新审
+ expect(scrollers).toHaveLength(1);
+ expect(scrollers[0].className).toContain('overflow-x-auto');
+ });
+
+ it(`${name} 的 pre 在该容器内,且取 w-max min-w-full`, () => {
+ const container = renderCase();
+ const scroller = container.querySelector('.diff-hscroll');
+ expect(scroller).toBeTruthy();
+ const pre = scroller?.querySelector('pre');
+ expect(pre, 'pre 必须在滚动容器内(横滚才作用于内容)').toBeTruthy();
+ // w-max = width:max-content(行宽 = 最长行,底色跟着滚动区延伸)
+ // min-w-full = min-width:100%(短 diff 不缩成窄条)
+ expect(pre?.className).toContain('w-max');
+ expect(pre?.className).toContain('min-w-full');
+ });
+
+ it(`${name} 的行仍带 --diff-*-bg 底色(整行填充没被改掉)`, () => {
+ const container = renderCase();
+ const html = container.innerHTML;
+ expect(html).toContain('bg-[var(--diff-del-bg)]');
+ expect(html).toContain('bg-[var(--diff-add-bg)]');
+ });
+ }
+});
+
+describe('globals.css 里的 .diff-hscroll 规则', () => {
+ it('横向 thumb 用 --msg-scrollbar(不是透明)', () => {
+ expect(globalsSrc).toMatch(
+ /\.diff-hscroll::-webkit-scrollbar-thumb:horizontal\s*\{\s*background-color:\s*var\(--msg-scrollbar\);\s*\}/,
+ );
+ });
+
+ it('横向槽厚 12px,与全局纵向槽宽对齐', () => {
+ expect(globalsSrc).toMatch(
+ /\.diff-hscroll::-webkit-scrollbar:horizontal\s*\{\s*height:\s*12px;\s*\}/,
+ );
+ expect(globalsSrc).toMatch(/::-webkit-scrollbar\s*\{\s*width:\s*12px;\s*\}/);
+ });
+
+ it('全局 auto-hide 体系没被改坏:默认 thumb 仍透明,.is-scrolling 才显形', () => {
+ expect(globalsSrc).toMatch(
+ /::-webkit-scrollbar-thumb\s*\{\s*background-color:\s*transparent;/,
+ );
+ expect(globalsSrc).toMatch(
+ /\.is-scrolling::-webkit-scrollbar-thumb\s*\{\s*background-color:\s*var\(--msg-scrollbar\);\s*\}/,
+ );
+ });
+});
diff --git a/apps/desktop/src/renderer/components/chat/DiffView.tsx b/apps/desktop/src/renderer/components/chat/DiffView.tsx
index de0823da1d..e1e880f71c 100644
--- a/apps/desktop/src/renderer/components/chat/DiffView.tsx
+++ b/apps/desktop/src/renderer/components/chat/DiffView.tsx
@@ -127,9 +127,14 @@ export function DiffView({ oldString, newString, contextLines }: DiffViewProps)
// select-text:globals.css 全局禁用了文本选中(Electron 原生 app 风格),
// diff 内容必须可读可复制,所以这里显式开。gutter / 符号列已通过 select-none
// 排除,只有内容文本会被选中。
+ //
+ // diff-hscroll:横向滚动条常显(globals.css),否则长行只能靠 trackpad 盲滚。
+ // pre 的 `w-max min-w-full`:行是 flex 容器,pre 宽度若锁在 100% 容器宽,
+ // 长行内容会溢出到行的边界之外 —— 滚过去以后行背景(红/绿)在一屏宽处就断了。
+ // 让 pre 取 max-content(至少铺满容器),行宽 = 最长行宽,底色跟着滚动区一起延伸。
return (
- <div className="select-text overflow-x-auto rounded-[12px] border border-[var(--msg-tool-card-border)]">
- <pre className="m-0 p-0 text-[length:calc(var(--app-code-font-size)_-_1px)] leading-[1.5] font-mono">
+ <div className="diff-hscroll select-text overflow-x-auto rounded-[12px] border border-[var(--msg-tool-card-border)]">
+ <pre className="m-0 w-max min-w-full p-0 text-[length:calc(var(--app-code-font-size)_-_1px)] leading-[1.5] font-mono">
{rows.map((row, i) => {
if (row.type === 'skip') {
// 折叠占位行 — 不带行号,只显示 "··· N lines unchanged ···"
diff --git a/apps/desktop/src/renderer/components/chat/MarkdownDiffBlock.tsx b/apps/desktop/src/renderer/components/chat/MarkdownDiffBlock.tsx
index 331afa5a8c..73668c6c69 100644
--- a/apps/desktop/src/renderer/components/chat/MarkdownDiffBlock.tsx
+++ b/apps/desktop/src/renderer/components/chat/MarkdownDiffBlock.tsx
@@ -66,14 +66,16 @@ export function MarkdownDiffBlock({ raw }: MarkdownDiffBlockProps) {
const gutterWidth = String(maxLineNum).length;
return (
+ // diff-hscroll + pre 的 `w-max min-w-full` 与 DiffView 同一套横滚契约:
+ // 滚动条常显 + 行宽取最长行,长行滚出去以后红/绿底色不断。
<div
className={cn(
- 'my-3 overflow-x-auto rounded-[12px]',
+ 'diff-hscroll my-3 overflow-x-auto rounded-[12px]',
'border border-[var(--msg-code-block-border)]',
'bg-[var(--msg-code-block-bg)]',
)}
>
- <pre className="m-0 p-0 text-[length:calc(var(--app-code-font-size)_-_1px)] leading-[1.5] font-mono">
+ <pre className="m-0 w-max min-w-full p-0 text-[length:calc(var(--app-code-font-size)_-_1px)] leading-[1.5] font-mono">
{lines.map((line, i) => (
<div
key={`${line.type}-${line.lineNum}-${i}`}
diff --git a/apps/desktop/src/renderer/styles/globals.css b/apps/desktop/src/renderer/styles/globals.css
index efb5cbce60..bd36a39b4d 100644
--- a/apps/desktop/src/renderer/styles/globals.css
+++ b/apps/desktop/src/renderer/styles/globals.css
@@ -1330,6 +1330,22 @@ body.resizing-pane [data-ghost-webview] {
background-color: var(--msg-scrollbar-hover);
}
+/* Diff 卡片(chat/DiffView + chat/MarkdownDiffBlock)的横向滚动条 —— 常显,
+ 不参与 `.is-scrolling` 自动隐藏体系。
+ why: 纵向"还有内容"有视觉线索(卡片被容器切断),横向没有 —— 长行到卡片右缘
+ 就没了,thumb 默认透明时用户根本不知道这里能横滚(2026-07-30 用户反馈)。
+ 槽厚 12px 与全局纵向槽宽对齐,thumb 同样靠 3px 透明 border 收成可见 6px;
+ `:horizontal` 让规则只吃横向条,纵向条继续走 auto-hide。 */
+.diff-hscroll::-webkit-scrollbar:horizontal {
+ height: 12px;
+}
+.diff-hscroll::-webkit-scrollbar-thumb:horizontal {
+ background-color: var(--msg-scrollbar);
+}
+.diff-hscroll::-webkit-scrollbar-thumb:horizontal:hover {
+ background-color: var(--msg-scrollbar-hover);
+}
+
/* F-QUEUE-DEFER · PendingQueuePanel — slim 4px scrollbar so the
`scrollbar-gutter: stable both-edges` reserved slot is small
(4px each side instead of 12px). Without this, the chip-bg rows