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(sidebar): 弹出窗口「+」菜单里已打开的插件页签切换、聚焦或收编

Dash6ba32d4e3a9a
diff --git a/apps/desktop/src/renderer/features/right-sidebar/AddTabDropdown.tsx b/apps/desktop/src/renderer/features/right-sidebar/AddTabDropdown.tsx
index a6e7a10a66..b7225e091c 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/AddTabDropdown.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/AddTabDropdown.tsx
@@ -61,8 +61,8 @@ interface AddTabDropdownProps {
sessionId?: string | null;
/** 本菜单所属的页签组:插件分组里新加的全局页签直接落在这一组。缺省右侧组。 */
groupId?: TabGroupId;
- /** 插件分组新加了全局页签(独立窗口据此收编)。已存在的不算。 */
- onGlobalTabAdded?: (tabId: string) => void;
+ /** 插件分组里选了某个全局页签(独立窗口据此收编 / 聚焦);existed = 选之前就已打开。 */
+ onGlobalTabSelected?: (tabId: string, existed: boolean) => void;
/** 锚点消失 / 面板收起时关闭(点外部与 Esc 由 DropdownMenu 根处理)。 */
onClose: () => void;
/** 选 kind。调用方负责真创建 tab + 关闭 dropdown。单例 kind 已存在时
@@ -141,7 +141,7 @@ export function AddTabDropdown({
anchorRef,
sessionId,
groupId,
- onGlobalTabAdded,
+ onGlobalTabSelected,
onClose,
onSelect,
existingKinds,
@@ -266,7 +266,7 @@ export function AddTabDropdown({
ghost: entry.ghost,
});
if (tab) {
- if (!entry.open) onGlobalTabAdded?.(tab.id);
+ onGlobalTabSelected?.(tab.id, entry.open);
setActiveGlobalTabId(tab.id);
}
onClose();
diff --git a/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx b/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx
index 4abdc0097d..9263154cd3 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx
@@ -67,6 +67,7 @@ import { TabBodyErrorBoundary } from './TabBodyErrorBoundary';
import { useRetainedBrowserTabHosts } from './hooks/useRetainedBrowserTabHosts';
import { useInstalledGhosts } from '@/cindy-brain/useInstalledGhosts';
import {
+ isTabPoppedOut,
summarizeSidebarTabWindows,
useSidebarTabWindowsState,
} from '@/lib/sidebarTabWindowState';
@@ -608,6 +609,26 @@ export function RightSidebarShell({
[groupId, isPrimaryGroup, onTabCreated, sessionId, soloMode, subagentsEnabled, t, tabs],
);
+ // 独立窗口里「+」菜单选了插件页签:新加的交给宿主收编;已打开的——本窗口承载就切过去,
+ // 在别的弹出窗口里就聚焦那扇窗口,在主窗侧栏里就收编进本窗口(主窗据弹出镜像把它隐藏)。
+ const handleGlobalTabSelected = useCallback(
+ (tabId: string, existed: boolean) => {
+ if (existed && tabs.some((tab) => tab.id === tabId)) {
+ setHostedActiveTabId(tabId);
+ return;
+ }
+ if (existed && isTabPoppedOut(tabId)) {
+ void window.electronAPI?.sidebarTabWindow
+ ?.focus(tabId)
+ .catch((err) => log.warn('focus plugin tab window failed', { tabId, err }));
+ return;
+ }
+ setHostedActiveTabId(tabId);
+ onTabCreated?.(tabId);
+ },
+ [onTabCreated, tabs],
+ );
+
const handleClose = useCallback(
(tabId: string) => {
// 全局页签的关闭是全局动作(所有任务里一起消失),不进任务桶。
@@ -868,7 +889,7 @@ export function RightSidebarShell({
popoutWindows={popoutWindows}
groupId={groupId}
onMoveTab={soloMode ? undefined : handleMoveTab}
- onGlobalTabAdded={soloMode ? onTabCreated : undefined}
+ onGlobalTabSelected={soloMode ? handleGlobalTabSelected : undefined}
pillVariant="chip"
addButtonWrapperClassName="h-[30px]"
subagentsAvailable={subagentsEnabled}
@@ -931,7 +952,7 @@ export function RightSidebarShell({
popoutWindows={popoutWindows}
groupId={groupId}
onMoveTab={soloMode ? undefined : handleMoveTab}
- onGlobalTabAdded={soloMode ? onTabCreated : undefined}
+ onGlobalTabSelected={soloMode ? handleGlobalTabSelected : undefined}
onCloseOthers={handleCloseOthers}
onCloseAll={handleCloseAll}
chromeWindowDrag={chromeWindowDrag}
diff --git a/apps/desktop/src/renderer/features/right-sidebar/TabBar.tsx b/apps/desktop/src/renderer/features/right-sidebar/TabBar.tsx
index 17b77626ed..8e229810c5 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/TabBar.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/TabBar.tsx
@@ -93,7 +93,7 @@ interface TabBarProps {
/** 页签右键菜单「移到 左侧 / 右侧栏 / 上方 / 下方」。不传 = 不显示。 */
onMoveTab?: (tabId: string, groupId: TabGroupId) => void;
/** 「+」菜单插件分组新加了全局页签(独立窗口据此收编)。 */
- onGlobalTabAdded?: (tabId: string) => void;
+ onGlobalTabSelected?: (tabId: string, existed: boolean) => void;
/** 本条横带是否作为**窗口**拖拽区(B3):主窗口内嵌形态传 false —— 空白处是
* "拖面板"手势面(窗口拖拽区收不到鼠标事件,二者物理互斥;拖窗走左栏顶行);
* detached 子窗口等其它宿主不传,默认 true 维持经典拖窗行为。 */
@@ -119,10 +119,10 @@ interface TabStripProps {
/** 见 TabBarProps.popoutWindows / onMergeBackTab。 */
popoutWindows?: PopoutWindowOption[];
onMergeBackTab?: (tabId: string) => void;
- /** 见 TabBarProps.groupId / onMoveTab / onGlobalTabAdded。 */
+ /** 见 TabBarProps.groupId / onMoveTab / onGlobalTabSelected。 */
groupId?: TabGroupId;
onMoveTab?: (tabId: string, groupId: TabGroupId) => void;
- onGlobalTabAdded?: (tabId: string) => void;
+ onGlobalTabSelected?: (tabId: string, existed: boolean) => void;
className?: string;
/**
* pill 视觉变体:
@@ -243,7 +243,7 @@ export function TabBar({
onMergeBackTab,
groupId,
onMoveTab,
- onGlobalTabAdded,
+ onGlobalTabSelected,
chromeWindowDrag = true,
subagentsAvailable = false,
}: TabBarProps) {
@@ -274,7 +274,7 @@ export function TabBar({
onMergeBackTab={onMergeBackTab}
groupId={groupId}
onMoveTab={onMoveTab}
- onGlobalTabAdded={onGlobalTabAdded}
+ onGlobalTabSelected={onGlobalTabSelected}
addButtonWrapperClassName="h-[36px]"
addButtonClassName="mt-[3px]"
subagentsAvailable={subagentsAvailable}
@@ -346,7 +346,7 @@ export function TabStrip({
onMergeBackTab,
groupId = 'right',
onMoveTab,
- onGlobalTabAdded,
+ onGlobalTabSelected,
className,
pillVariant = 'flush',
addButtonWrapperClassName,
@@ -545,7 +545,7 @@ export function TabStrip({
anchorRef={addButtonWrapperRef}
sessionId={sessionId}
groupId={groupId}
- onGlobalTabAdded={onGlobalTabAdded}
+ onGlobalTabSelected={onGlobalTabSelected}
onClose={closeDropdown}
onSelect={onAdd}
existingKinds={existingKinds}
diff --git a/apps/desktop/src/renderer/features/right-sidebar/__tests__/AddTabDropdown.test.tsx b/apps/desktop/src/renderer/features/right-sidebar/__tests__/AddTabDropdown.test.tsx
index 31656107ff..aed583aeef 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/__tests__/AddTabDropdown.test.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/__tests__/AddTabDropdown.test.tsx
@@ -2,9 +2,13 @@
import { createRef, type ReactNode } from 'react';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { setDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';
import { AddTabDropdown } from '../AddTabDropdown';
+import { __resetGlobalTabsForTest } from '../globalTabs';
+import { __resetTabGroupsForTest } from '../tabGroups';
const m = vi.hoisted(() => ({
+ ghosts: [] as unknown[],
open: vi.fn(async () => undefined),
rows: new Map<string, { remoteHostId?: string | null; deviceLinkDeviceId?: string | null }>(),
}));
@@ -35,6 +39,7 @@ vi.mock('@/features/bots/botStore', () => ({
}));
vi.mock('@/lib/sessionsStore', () => ({ sessionsStore: { findById: (id: string) => m.rows.get(id) ?? null } }));
vi.mock('../lib/openBotWorkbenchTab', () => ({ openBotWorkbenchTab: m.open }));
+vi.mock('@/cindy-brain/useInstalledGhosts', () => ({ useInstalledGhosts: () => m.ghosts }));
function renderMenu(sessionId: string, onSelect = vi.fn()) {
const anchorRef = createRef<HTMLElement>();
@@ -47,6 +52,11 @@ function renderMenu(sessionId: string, onSelect = vi.fn()) {
beforeEach(() => {
vi.clearAllMocks();
m.rows = new Map();
+ m.ghosts = [];
+ window.localStorage.clear();
+ __resetGlobalTabsForTest();
+ __resetTabGroupsForTest();
+ setDataOwnerGeneration('owner-a', 1);
});
afterEach(cleanup);
@@ -80,3 +90,44 @@ it.each([
renderMenu('bot-main');
expect(screen.queryByText('bots.workbench.title')).toBeNull();
});
+
+// 测试价值
+// - 生产入口与可观察结果:「+」菜单插件项;可观察的是选择时把 (tabId, 此前是否已打开) 交给宿主,
+// 独立窗口据此对「已打开」的页签切换 / 聚焦,而不是菜单关掉后毫无反应。
+// - Bug / 规则依据:review「弹出窗口『+』菜单里已在别处打开的插件项点了没反应」。
+// - 现有覆盖缺口:此前已打开的项不通知宿主。
+// - 最小失败输入:同一个插件项连点两次。
+it('reports whether a plugin tab already existed when its menu item is selected', () => {
+ m.ghosts = [
+ {
+ manifest: { id: 'alpha', name: 'Alpha', panel: { html: 'p.html' } },
+ enabled: true,
+ },
+ ];
+ const onGlobalTabSelected = vi.fn();
+ render(
+ <AddTabDropdown
+ open={false}
+ anchorRef={createRef<HTMLElement>()}
+ sessionId="task-1"
+ onClose={vi.fn()}
+ onSelect={vi.fn()}
+ onGlobalTabSelected={onGlobalTabSelected}
+ />,
+ );
+ fireEvent.click(screen.getByText('Alpha'));
+ expect(onGlobalTabSelected).toHaveBeenLastCalledWith('g_alpha', false);
+ cleanup();
+ render(
+ <AddTabDropdown
+ open={false}
+ anchorRef={createRef<HTMLElement>()}
+ sessionId="task-1"
+ onClose={vi.fn()}
+ onSelect={vi.fn()}
+ onGlobalTabSelected={onGlobalTabSelected}
+ />,
+ );
+ fireEvent.click(screen.getByText('Alpha'));
+ expect(onGlobalTabSelected).toHaveBeenLastCalledWith('g_alpha', true);
+});