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): swap built-in panes with grid columns

david1230872ab7c9
diff --git a/apps/desktop/src/renderer/layout/PanelDragController.tsx b/apps/desktop/src/renderer/layout/PanelDragController.tsx
index f28db8c955..ebdb367f9f 100644
--- a/apps/desktop/src/renderer/layout/PanelDragController.tsx
+++ b/apps/desktop/src/renderer/layout/PanelDragController.tsx
@@ -35,6 +35,7 @@ import {
moveGhostPaneToRootByKind,
stackGhostPaneByKind,
swapPanesByKind,
+ swapRootSplitChildrenByKind,
type LayoutNode,
} from '../../shared/layoutTree';
import { toast } from '@/lib/toast';
@@ -142,7 +143,7 @@ function pointInRect(x: number, y: number, rect: RectLike): boolean {
/**
* 二维停靠意图:插件之间上/下形成 grid,左/右回到根横排,中心交换槽位。
- * 内置面板保持原有根级交换,不允许被塞进插件纵向列。
+ * 内置面板保持根级交换;落在插件纵向列时交换整列,不把内置面板塞进列内。
*/
export function computePanelDropIntent(input: {
pointerX: number;
@@ -161,9 +162,7 @@ export function computePanelDropIntent(input: {
const sourceGhost = sourceKind.startsWith('ghost:');
const targetGhost = targetKind.startsWith('ghost:');
if (!sourceGhost) {
- return input.sourceIsRootPane &&
- input.targetIsRootPane &&
- input.sourceRootIndex !== input.targetRootIndex
+ return input.sourceIsRootPane && input.sourceRootIndex !== input.targetRootIndex
? 'swap'
: null;
}
@@ -208,8 +207,13 @@ export function computePanelDropZone(
intent: PanelDropIntent,
paneRect: RectLike,
rootRect: RectLike,
+ swapAtRoot = false,
): ZoneRect {
- const base = insetRect(intent === 'root-before' || intent === 'root-after' ? rootRect : paneRect);
+ const base = insetRect(
+ intent === 'root-before' || intent === 'root-after' || (intent === 'swap' && swapAtRoot)
+ ? rootRect
+ : paneRect,
+ );
switch (intent) {
case 'stack-before':
return { ...base, height: base.height / 2 };
@@ -386,7 +390,12 @@ export function PanelDragController({
targetKind: nextKind,
zone:
nextTarget && nextIntent
- ? computePanelDropZone(nextIntent, nextTarget.paneRect, nextTarget.rootRect)
+ ? computePanelDropZone(
+ nextIntent,
+ nextTarget.paneRect,
+ nextTarget.rootRect,
+ nextIntent === 'swap' && !sourceKind.startsWith('ghost:'),
+ )
: null,
});
}
@@ -423,7 +432,9 @@ export function PanelDragController({
targetKindNow,
targetIntentNow === 'root-before' ? 'before' : 'after',
)
- : swapPanesByKind(layout, sourceKind, targetKindNow);
+ : sourceKind.startsWith('ghost:')
+ ? swapPanesByKind(layout, sourceKind, targetKindNow)
+ : swapRootSplitChildrenByKind(layout, sourceKind, targetKindNow);
if (op.applied) {
void window.electronAPI.layout
.set(op.layout)
diff --git a/apps/desktop/src/renderer/layout/__tests__/panelDragController.test.ts b/apps/desktop/src/renderer/layout/__tests__/panelDragController.test.ts
index 8d92254e01..d1197318fb 100644
--- a/apps/desktop/src/renderer/layout/__tests__/panelDragController.test.ts
+++ b/apps/desktop/src/renderer/layout/__tests__/panelDragController.test.ts
@@ -99,7 +99,7 @@ describe('插件面板二维停靠落点', () => {
expect(computePanelDropIntent({ ...base, pointerX: 300, pointerY: 400 })).toBe('swap');
});
- it('内置面板只保留根级交换,不进入插件 column', () => {
+ it('内置面板与根 pane 或整个插件 column 做根级交换', () => {
expect(
computePanelDropIntent({
...base,
@@ -117,6 +117,17 @@ describe('插件面板二维停靠落点', () => {
sourceKind: 'chat-main',
targetIsRootPane: false,
}),
+ ).toBe('swap');
+ expect(
+ computePanelDropIntent({
+ ...base,
+ pointerX: 300,
+ pointerY: 400,
+ sourceKind: 'chat-main',
+ sourceRootIndex: 1,
+ targetRootIndex: 1,
+ targetIsRootPane: false,
+ }),
).toBeNull();
});
@@ -146,4 +157,14 @@ describe('插件面板二维停靠落点', () => {
height: 588,
});
});
+
+ it('内置面板交换插件 grid 时高亮整个根级列,而不是单个插件 pane', () => {
+ const columnRect = { left: 80, top: 80, width: 440, height: 640, right: 520, bottom: 720 };
+ expect(computePanelDropZone('swap', paneRect, columnRect, true)).toMatchObject({
+ left: 86,
+ top: 86,
+ width: 428,
+ height: 628,
+ });
+ });
});
diff --git a/apps/desktop/src/shared/__tests__/layoutTree.test.ts b/apps/desktop/src/shared/__tests__/layoutTree.test.ts
index ebd9ffbe64..64234b8f8e 100644
--- a/apps/desktop/src/shared/__tests__/layoutTree.test.ts
+++ b/apps/desktop/src/shared/__tests__/layoutTree.test.ts
@@ -559,17 +559,27 @@ describe('插件面板 grid 停靠', () => {
expect(validateLayout(result.layout)).toEqual({ ok: true });
});
- it('数据层拒绝把 chat/right-tabs 交换进插件纵向列', () => {
+ it('内置面板可与整个插件纵向列交换,但仍拒绝进入列内槽位', () => {
const stacked = stackGhostPaneByKind(
layoutWithTwoGhostPanes(),
'ghost:alpha',
'ghost:beta',
'before',
);
+ const before = stacked.layout.content as SplitNode;
+ const columnFraction = before.children[0].fraction;
+ const chatFraction = before.children[1].fraction;
expect(swapPanesByKind(stacked.layout, 'chat-main', 'ghost:alpha').applied).toBe(false);
- expect(countPanelKind(stacked.layout, 'chat-main')).toBe(1);
- expect(validateLayout(stacked.layout)).toEqual({ ok: true });
+ const result = swapRootSplitChildrenByKind(stacked.layout, 'chat-main', 'ghost:alpha');
+ expect(result.applied).toBe(true);
+ const root = result.layout.content as SplitNode;
+ expect(root.children[0].node).toMatchObject({ type: 'pane', panelKind: 'chat-main' });
+ expect(root.children[0].fraction).toBeCloseTo(chatFraction);
+ expect(root.children[1].node).toMatchObject({ type: 'split', direction: 'column' });
+ expect(root.children[1].fraction).toBeCloseTo(columnFraction);
+ expect(countPanelKind(result.layout, 'chat-main')).toBe(1);
+ expect(validateLayout(result.layout)).toEqual({ ok: true });
});
it('上下停靠拒绝内置面板或缺失目标,保持 chat-main 不变量', () => {
diff --git a/apps/desktop/src/shared/layoutTree.ts b/apps/desktop/src/shared/layoutTree.ts
index 5bffe8460d..53438a591a 100644
--- a/apps/desktop/src/shared/layoutTree.ts
+++ b/apps/desktop/src/shared/layoutTree.ts
@@ -510,9 +510,9 @@ export function removeRootSplitPaneByKind(layout: Layout, kind: PanelKind): Layo
}
/**
- * 交换 content 根分割中两个 panelKind 对应 child 的位置(N 面板拖拽换位的
- * 提交操作)。child 整体交换 —— fraction 随面板走(换位不改变各自宽度份额)。
- * 任一 kind 不在根分割里则拒绝。
+ * 交换 content 根分割中两个 panelKind 所在 child 的位置(N 面板拖拽换位的
+ * 提交操作)。kind 可以是根 pane,也可以位于根级插件 column 内;后者交换整列,
+ * 避免把内置面板塞进 ghost-only column。child 整体交换,fraction 随区域走。
*/
export function swapRootSplitChildrenByKind(
layout: Layout,
@@ -522,17 +522,11 @@ export function swapRootSplitChildrenByKind(
if (layout.content.type !== 'split') {
return { layout, applied: false, reason: 'content is not a split' };
}
- const indexOf = (kind: PanelKind) =>
- layout.content.type === 'split'
- ? layout.content.children.findIndex(
- (c) => c.node.type === 'pane' && c.node.panelKind === kind,
- )
- : -1;
- const a = indexOf(kindA);
- const b = indexOf(kindB);
+ const a = rootChildIndexContainingKind(layout, kindA);
+ const b = rootChildIndexContainingKind(layout, kindB);
if (a < 0 || b < 0)
return { layout, applied: false, reason: `pane kind not found: ${a < 0 ? kindA : kindB}` };
- if (a === b) return { layout, applied: false, reason: 'cannot swap a pane with itself' };
+ if (a === b) return { layout, applied: false, reason: 'cannot swap within the same root child' };
const next = structuredClone(layout);
const split = next.content as SplitNode;
[split.children[a], split.children[b]] = [split.children[b], split.children[a]];