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): worker 菜单随裁剪容器缩放重算边界

Shinku82e9bb56cf3c
diff --git a/apps/desktop/src/renderer/features/cc-agent/RolePillDropdown.tsx b/apps/desktop/src/renderer/features/cc-agent/RolePillDropdown.tsx
index 095d48c561..5e8f283440 100644
--- a/apps/desktop/src/renderer/features/cc-agent/RolePillDropdown.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/RolePillDropdown.tsx
@@ -318,17 +318,23 @@ export interface WorkerListToolbarProps extends RolePillDropdownProps {
}
// 菜单经 absolute 定位且未走 portal 渲染,会被最近的 overflow 非 visible 祖先裁剪。
-// 找到该裁剪祖先的视口边界(top/bottom/left/right),用于钳制菜单可用高度/宽度;
-// 找不到裁剪祖先时返回 null,由调用方退回视口边界。
-function findClippingBounds(
- el: HTMLElement,
-): { top: number; bottom: number; left: number; right: number } | null {
+// 找到该裁剪祖先元素(overflow 非 visible 的最近祖先);找不到返回 null。
+function findClippingContainer(el: HTMLElement): HTMLElement | null {
let container: HTMLElement | null = el.parentElement;
while (container) {
const style = window.getComputedStyle(container);
if (style.overflowX !== 'visible' || style.overflowY !== 'visible') break;
container = container.parentElement;
}
+ return container;
+}
+
+// 找到裁剪祖先的视口边界(top/bottom/left/right),用于钳制菜单可用高度/宽度;
+// 找不到裁剪祖先时返回 null,由调用方退回视口边界。
+function findClippingBounds(
+ el: HTMLElement,
+): { top: number; bottom: number; left: number; right: number } | null {
+ const container = findClippingContainer(el);
if (!container) return null;
const r = container.getBoundingClientRect();
return { top: r.top, bottom: r.bottom, left: r.left, right: r.right };
@@ -370,7 +376,20 @@ function useAnchorMenuMaxHeight(
};
update();
window.addEventListener('resize', update);
- return () => window.removeEventListener('resize', update);
+ // 侧栏经拖拽分割线(pointermove)调整宽度时不会触发 window.resize;用 ResizeObserver
+ // 监听裁剪祖先容器尺寸变化,菜单打开期间及时重算边界,避免菜单超出当前侧栏被裁掉。
+ const clipContainer = anchorRef.current
+ ? findClippingContainer(anchorRef.current)
+ : null;
+ const resizeObserver =
+ typeof ResizeObserver !== 'undefined' && clipContainer
+ ? new ResizeObserver(update)
+ : null;
+ if (resizeObserver && clipContainer) resizeObserver.observe(clipContainer);
+ return () => {
+ window.removeEventListener('resize', update);
+ resizeObserver?.disconnect();
+ };
}, [anchorRef, open]);
return placement;
}
@@ -400,7 +419,20 @@ function useAnchorMenuMaxWidth(
};
update();
window.addEventListener('resize', update);
- return () => window.removeEventListener('resize', update);
+ // 侧栏经拖拽分割线(pointermove)调整宽度时不会触发 window.resize;用 ResizeObserver
+ // 监听裁剪祖先容器尺寸变化,菜单打开期间及时重算边界,避免菜单超出当前侧栏被裁掉。
+ const clipContainer = anchorRef.current
+ ? findClippingContainer(anchorRef.current)
+ : null;
+ const resizeObserver =
+ typeof ResizeObserver !== 'undefined' && clipContainer
+ ? new ResizeObserver(update)
+ : null;
+ if (resizeObserver && clipContainer) resizeObserver.observe(clipContainer);
+ return () => {
+ window.removeEventListener('resize', update);
+ resizeObserver?.disconnect();
+ };
}, [anchorRef, open, align]);
return maxWidth;
}
diff --git a/apps/desktop/src/renderer/features/cc-agent/__tests__/RolePillDropdown.anchorMenuResize.test.tsx b/apps/desktop/src/renderer/features/cc-agent/__tests__/RolePillDropdown.anchorMenuResize.test.tsx
new file mode 100644
index 0000000000..868c513715
--- /dev/null
+++ b/apps/desktop/src/renderer/features/cc-agent/__tests__/RolePillDropdown.anchorMenuResize.test.tsx
@@ -0,0 +1,132 @@
+// @vitest-environment jsdom
+
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import type { WorkerInfo } from '../hooks/useWorkers';
+import { WorkerListToolbar } from '../RolePillDropdown';
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+vi.mock('@/hooks/useAppShortcut', () => ({
+ useAppShortcutDisplay: () => '',
+}));
+
+vi.mock('@/components/ui/confirm-dialog-provider', () => ({
+ useConfirmDialog: () => ({ confirm: vi.fn(async () => false) }),
+}));
+
+function worker(overrides: Partial<WorkerInfo> = {}): WorkerInfo {
+ return {
+ workerId: 'worker-a',
+ sessionId: 'session-a',
+ role: 'developer',
+ agent: 'codex',
+ model: 'gpt-5.4',
+ effort: null,
+ label: null,
+ status: 'idle',
+ focused: true,
+ idleSince: null,
+ ...overrides,
+ };
+}
+
+function renderToolbar() {
+ const current = worker();
+ return render(
+ <WorkerListToolbar
+ worker={current}
+ workers={[current]}
+ selectedWorkerId={current.workerId}
+ activeWorkerCount={1}
+ softLimit={5}
+ hardLimit={8}
+ onSwitchFocus={vi.fn()}
+ onOpenCreate={vi.fn()}
+ onOpenSettings={vi.fn()}
+ onArchiveWorker={vi.fn()}
+ />,
+ );
+}
+
+function menuButton(): HTMLElement {
+ return screen.getByRole('button', { name: 'orca.rolePill.layoutMenuLabel' });
+}
+
+// 侧栏拖拽分割线(pointermove)不会触发 window.resize,菜单边界需靠 ResizeObserver
+// 监听裁剪祖先容器尺寸变化来重算。本组用例验证该接线。
+describe('worker 菜单随裁剪容器缩放重算边界', () => {
+ type ObserverRecord = { callback: ResizeObserverCallback; observed: Element | null };
+ let observers: ObserverRecord[];
+
+ beforeEach(() => {
+ observers = [];
+ vi.stubGlobal(
+ 'ResizeObserver',
+ class {
+ readonly callback: ResizeObserverCallback;
+ observed: Element | null = null;
+ constructor(callback: ResizeObserverCallback) {
+ this.callback = callback;
+ }
+ observe(el: Element) {
+ this.observed = el;
+ observers.push({ callback: this.callback, observed: el });
+ }
+ unobserve() {}
+ disconnect() {}
+ },
+ );
+ // 让最近祖先被识别为 overflow 非 visible 的裁剪容器,命中 ResizeObserver 分支。
+ // 其余属性/方法透传真实 CSSStyleDeclaration,避免破坏 dom-accessibility-api 的
+ // getPropertyValue 等调用。
+ const realGetComputedStyle = window.getComputedStyle.bind(window);
+ vi.spyOn(window, 'getComputedStyle').mockImplementation((el: Element) => {
+ const style = realGetComputedStyle(el);
+ return new Proxy(style, {
+ get(target, prop) {
+ if (prop === 'overflowX' || prop === 'overflowY') return 'hidden';
+ const value = Reflect.get(target, prop);
+ return typeof value === 'function' ? value.bind(target) : value;
+ },
+ });
+ });
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+ cleanup();
+ });
+
+ it('打开 ⋮ 菜单后,ResizeObserver 观察裁剪祖先容器', () => {
+ renderToolbar();
+ fireEvent.click(menuButton());
+ expect(screen.getByText('orca.rolePill.workersHeader')).toBeTruthy();
+
+ // 至少存在一个观察了非空裁剪容器的 ResizeObserver(⋮ 菜单 / dropdown 各一个)。
+ expect(observers.length).toBeGreaterThan(0);
+ expect(observers.some((o) => o.observed !== null)).toBe(true);
+ });
+
+ it('裁剪容器尺寸变化触发回调时菜单重新计算边界且不报错', () => {
+ renderToolbar();
+ fireEvent.click(menuButton());
+ expect(screen.getByText('orca.rolePill.workersHeader')).toBeTruthy();
+
+ const observed = observers.filter((o) => o.observed !== null);
+ expect(observed.length).toBeGreaterThan(0);
+
+ act(() => {
+ observed.forEach((o) =>
+ o.callback([{ contentRect: { width: 280 } } as ResizeObserverEntry], o as unknown as ResizeObserver),
+ );
+ });
+
+ // 重算后菜单仍正常渲染(未被裁剪祖先缩放逻辑抛错)。
+ expect(screen.getByText('orca.rolePill.workersHeader')).toBeTruthy();
+ });
+});