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): restore hidden grid layout state

davide14f651db969
diff --git a/apps/desktop/src/renderer/layout/LayoutRoot.tsx b/apps/desktop/src/renderer/layout/LayoutRoot.tsx
index 7d6e8b5376..d9eae2ca37 100644
--- a/apps/desktop/src/renderer/layout/LayoutRoot.tsx
+++ b/apps/desktop/src/renderer/layout/LayoutRoot.tsx
@@ -168,19 +168,19 @@ export function normalizeSubMinFractions(
(c) => c.node.type === 'pane' && c.node.panelKind === 'chat-main',
);
if (chatIndex < 0) return null;
+ const nodeIsRegistered = (node: LayoutNode): boolean =>
+ node.type === 'pane'
+ ? isRegistered(node.panelKind)
+ : node.children.some((child) => nodeIsRegistered(child.node));
// 在场份额的比例尺(share × scale = 树份额)。此处不能复用 activeSplitLedger:
// 自愈是纯函数,可见性判定由入参 isRegistered 提供(测试注入)。
const scale = children
- .filter((c) => c.node.type !== 'pane' || isRegistered(c.node.panelKind))
+ .filter((c) => nodeIsRegistered(c.node))
.reduce((sum, c) => sum + c.fraction, 0);
if (!(scale > 0)) return null;
let neededTotal = 0; // 在场份额口径的缺口总额
const bumps = new Map<number, number>(); // treeIndex → 目标在场份额
- const nodeIsRegistered = (node: LayoutNode): boolean =>
- node.type === 'pane'
- ? isRegistered(node.panelKind)
- : node.children.some((child) => nodeIsRegistered(child.node));
children.forEach((child, index) => {
const node = child.node;
if (node.type === 'pane' && node.panelKind === 'chat-main') return;
@@ -689,7 +689,7 @@ export function LayoutRoot({ suppressNonChatPanels = false }: LayoutRootProps =
// isPanelKindVisible 的隐式数据源(模块级镜像),必须进 deps 才能感知抽离变化。
const rootWidths = useMemo(
() => computeRootWidths(layout, liveFractions, avail),
- [layout, liveFractions, avail, ghostWindowsState, ghostBubbleState],
+ [layout, liveFractions, avail, ghostBubbleState, ghostSyncVersion, ghostWindowsState],
);
// chat 实际渲染宽 ≈ 可用宽 − 各在场非 chat 面板宽度之和(拖缝余量的兜底估值,
// 见 RootDividerPropsExtra;起拖优先实测元素矩形)。
diff --git a/apps/desktop/src/renderer/layout/__tests__/LayoutRoot.test.tsx b/apps/desktop/src/renderer/layout/__tests__/LayoutRoot.test.tsx
index c556efba03..5cfecf7e0f 100644
--- a/apps/desktop/src/renderer/layout/__tests__/LayoutRoot.test.tsx
+++ b/apps/desktop/src/renderer/layout/__tests__/LayoutRoot.test.tsx
@@ -1,7 +1,7 @@
// @vitest-environment jsdom
import { cleanup, render, screen } from '@testing-library/react';
import { act } from 'react';
-import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createDefaultLayout, type Layout } from '../../../shared/layoutTree';
import {
@@ -10,9 +10,19 @@ import {
} from '../../panels/BuiltinPanelBridge';
import { __resetBuiltinPanelsForTest } from '../../panels/builtinPanels';
import { __resetPanelRegistryForTest, registerPanelKind } from '../../panels/registry';
-import { LayoutRoot } from '../LayoutRoot';
+import { LayoutRoot, normalizeSubMinFractions } from '../LayoutRoot';
import { usePaneFill } from '../panePlacement';
+const ghostPanelSyncMock = vi.hoisted(() => ({ version: 0 }));
+
+vi.mock('../../cindy-brain/ghostPanels', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('../../cindy-brain/ghostPanels')>();
+ return {
+ ...actual,
+ useGhostPanelsSync: () => ghostPanelSyncMock.version,
+ };
+});
+
/** stub electronAPI.layout:同步返回给定树 + 可手动触发 onChanged。 */
let currentLayout: Layout;
let changedListeners: Array<(payload: { layout: Layout }) => void>;
@@ -61,6 +71,7 @@ function rowChildTestIds(): string[] {
}
beforeEach(() => {
+ ghostPanelSyncMock.version = 0;
currentLayout = createDefaultLayout();
stubElectronLayoutApi();
});
@@ -175,4 +186,99 @@ describe('LayoutRoot · 树驱动的顺序与在场', () => {
expect(document.querySelector('[data-layout-root-child-id="grid-alpha"]')).not.toBeNull();
expect(screen.getAllByTestId('layout-divider')).toHaveLength(3);
});
+
+ it('插件重新注册但布局树未变化时,恢复 grid 列的持久宽度', () => {
+ currentLayout = {
+ ...createDefaultLayout(),
+ content: {
+ type: 'split',
+ id: 'root',
+ direction: 'row',
+ children: [
+ {
+ fraction: 0.5,
+ node: { type: 'pane', id: 'chat', panelKind: 'chat-main', minWidth: 400 },
+ },
+ {
+ fraction: 0.3,
+ node: {
+ type: 'split',
+ id: 'grid-restored',
+ direction: 'column',
+ children: [
+ {
+ fraction: 1,
+ node: { type: 'pane', id: 'restored', panelKind: 'ghost:restored' },
+ },
+ ],
+ },
+ },
+ { fraction: 0.2, node: { type: 'pane', id: 'right', panelKind: 'right-tabs' } },
+ ],
+ },
+ };
+
+ const rendered = renderLayoutRoot();
+ expect(document.querySelector('[data-layout-root-child-id="grid-restored"]')).toBeNull();
+
+ registerPanelKind({
+ kind: 'ghost:restored',
+ Component: () => <div data-testid="restored-panel" />,
+ collapseMemory: 'global',
+ });
+ ghostPanelSyncMock.version += 1;
+ rendered.rerender(
+ <BuiltinPanelBridgeProvider value={bridge}>
+ <div data-testid="row">
+ <LayoutRoot />
+ </div>
+ </BuiltinPanelBridgeProvider>,
+ );
+
+ const grid = document.querySelector<HTMLElement>(
+ '[data-layout-root-child-id="grid-restored"]',
+ );
+ expect(grid).not.toBeNull();
+ expect(grid?.style.width).toBe('360px');
+ expect(screen.getByTestId('restored-panel')).not.toBeNull();
+ });
+});
+
+describe('normalizeSubMinFractions', () => {
+ it('全隐藏 grid 不进入在场比例尺,也不会触发布局自愈写回', () => {
+ const layout: Layout = {
+ ...createDefaultLayout(),
+ content: {
+ type: 'split',
+ id: 'root',
+ direction: 'row',
+ children: [
+ {
+ fraction: 0.35,
+ node: { type: 'pane', id: 'chat', panelKind: 'chat-main', minWidth: 400 },
+ },
+ {
+ fraction: 0.6,
+ node: {
+ type: 'split',
+ id: 'hidden-grid',
+ direction: 'column',
+ children: [
+ { fraction: 1, node: { type: 'pane', id: 'hidden', panelKind: 'ghost:hidden' } },
+ ],
+ },
+ },
+ { fraction: 0.05, node: { type: 'pane', id: 'right', panelKind: 'right-tabs' } },
+ ],
+ },
+ };
+
+ const fixed = normalizeSubMinFractions(
+ layout,
+ 2000,
+ (kind) => kind === 'chat-main' || kind === 'right-tabs',
+ );
+
+ expect(fixed).toBeNull();
+ });
});