diff --git a/docs/plugin-authoring-guide.md b/docs/plugin-authoring-guide.md index 92770a3494..107c8268a6 100644 --- a/docs/plugin-authoring-guide.md +++ b/docs/plugin-authoring-guide.md @@ -77,6 +77,11 @@ Each line is the main call. Read the types for every field. | `ui` (optional) | The host's own controls | `Button`, `Input`, `Field`, `Switch`, `Dialog`. Fall back to plain elements when missing. | | `host` | Commands, HTTPS, processes, files | Needs `manifest.host`, below | +In a mounted panel, `ctx.panels.useTabTitle?.(label, icon, actions)` sets the +tab's live title and optional icon. Pass an optional stable array of +`{ label, run }` entries to add actions to that tab's context menu. Older hosts +that support the title hook may ignore the actions argument. + Agent tool names match `/^[a-z][a-z0-9_]{0,63}$/` and are unique across plugins; prefix them with your plugin. Changing a tool's name, description or schema restarts some agent sessions. `addInstructions` text is sent on every turn, so keep it short. In a Solo region, check `ui.headerMenuActions === true` before passing diff --git a/src/features/panels/PanelWorkspace.test.tsx b/src/features/panels/PanelWorkspace.test.tsx index 079a469c4e..5731054b6e 100644 --- a/src/features/panels/PanelWorkspace.test.tsx +++ b/src/features/panels/PanelWorkspace.test.tsx @@ -1,6 +1,6 @@ // @vitest-environment jsdom import "@testing-library/jest-dom/vitest"; -import { StrictMode, useRef, useState } from "react"; +import { StrictMode, useCallback, useMemo, useRef, useState } from "react"; import { act, cleanup, @@ -104,7 +104,8 @@ function Fixture({ const tabs = () => screen.getByRole("tablist", { name: "Panel tabs" }); const tab = (name: string) => within(tabs()).getByRole("tab", { name }); -test("a plugin panel names its tab with panels.useTabTitle, and renders without one outside a tab", () => { +test("a mounted plugin panel supplies its tab title and context-menu actions", async () => { + const user = userEvent.setup(); const ctx = new Context(); ctx.provide("pluginStatus", { isActive: () => true, @@ -112,30 +113,104 @@ test("a plugin panel names its tab with panels.useTabTitle, and renders without }); const { useTabTitle } = new PanelsService(ctx); function Build({ id }: { id: string }) { - useTabTitle?.(`#${id}`); - return

Build {id}

; + const [showLogs, setShowLogs] = useState(false); + const actions = useMemo( + () => [{ label: "Show logs", run: () => setShowLogs(true) }], + [], + ); + useTabTitle?.(`#${id}`, undefined, actions); + return

{showLogs ? `Logs for build ${id}` : `Build ${id}`}

; } - const { rerender } = render( + const workspace = (id?: string) => ( {}} - items={[{ id: "ci", label: "CI", content: }]} - />, + items={[ + { + id: "ci", + label: "CI", + actions: [{ label: "Open in browser", run() {} }], + content: id ? :

No build selected

, + }, + ]} + /> ); + const { rerender } = render(workspace("1")); expect(tab("#1")).toBeInTheDocument(); - rerender( - {}} - items={[{ id: "ci", label: "CI", content: }]} - />, - ); - expect(tab("#2")).toBeInTheDocument(); - expect(within(tabs()).queryByRole("tab", { name: "CI" })).toBeNull(); + rerender(workspace("2")); + + await user.pointer({ keys: "[MouseRight]", target: tab("#2") }); + expect( + await screen.findByRole("menuitem", { name: "Open in browser" }), + ).toBeVisible(); + await user.click(screen.getByRole("menuitem", { name: "Show logs" })); + expect(await screen.findByText("Logs for build 2")).toBeVisible(); + + rerender(workspace()); + await user.pointer({ keys: "[MouseRight]", target: tab("CI") }); + expect( + await screen.findByRole("menuitem", { name: "Open in browser" }), + ).toBeVisible(); + expect(screen.queryByRole("menuitem", { name: "Show logs" })).toBeNull(); cleanup(); render(); expect(screen.getByText("Build 3")).toBeInTheDocument(); }); + +test("a plugin-only tab menu preserves focus and closes when its action is withdrawn", async () => { + const user = userEvent.setup(); + function BuildTitle({ showLogs }: { showLogs: () => void }) { + const actions = useMemo( + () => [{ label: "Show logs", run: showLogs }], + [showLogs], + ); + usePanelTabTitle("Build", undefined, actions); + return null; + } + function Workspace({ hasBuild }: { hasBuild: boolean }) { + const [showLogs, setShowLogs] = useState(false); + const openLogs = useCallback(() => setShowLogs(true), []); + return ( + {}} + items={[ + { + id: "build", + label: "Build", + content: ( + <> + {hasBuild && } +

{showLogs ? "Build logs" : "Build summary"}

+ + ), + }, + ]} + /> + ); + } + + const { rerender } = render(); + tab("Build").focus(); + rerender(); + expect(tab("Build")).toHaveFocus(); + await user.keyboard("{Shift>}{F10}{/Shift}"); + await screen.findByRole("menuitem", { name: "Show logs" }); + await vi.waitFor(() => expect(screen.getByRole("menu")).toHaveFocus()); + + rerender(); + await vi.waitFor(() => expect(tab("Build")).toHaveFocus()); + await user.keyboard("{Shift>}{F10}{/Shift}"); + expect(screen.queryByRole("menu")).toBeNull(); + + rerender(); + expect(tab("Build")).toHaveFocus(); + expect(screen.queryByRole("menu")).toBeNull(); + await user.keyboard("{Shift>}{F10}{/Shift}"); + await user.click(await screen.findByRole("menuitem", { name: "Show logs" })); + expect(await screen.findByText("Build logs")).toBeVisible(); +}); + test("switches retained panels, associates their tabs, and closes inactive or active tabs with focus recovery", async () => { const user = userEvent.setup(); render( diff --git a/src/features/panels/PanelWorkspace.tsx b/src/features/panels/PanelWorkspace.tsx index c41b3c54ef..d9761e663c 100644 --- a/src/features/panels/PanelWorkspace.tsx +++ b/src/features/panels/PanelWorkspace.tsx @@ -45,7 +45,11 @@ export type PanelWorkspaceItem = { /** Header icon actions for this tab's content, shown while it is selected. */ toolbar?: ReactNode; }; -type Identity = { label: string; icon?: ReactNode }; +type Identity = { + label: string; + icon?: ReactNode; + actions?: readonly TabAction[] | undefined; +}; /** `keepOwner`: the owner stays open; only its detail tabs are cleared. */ type Close = (options?: { keepOwner?: boolean }) => void; type Local = { id: string; label: string; owner: string; close: Close }; @@ -60,15 +64,19 @@ export function usePanelTabHost() { return useContext(TabHost); } -export function usePanelTabTitle(label: string, icon?: ReactNode) { +export function usePanelTabTitle( + label: string, + icon?: ReactNode, + actions?: readonly TabAction[], +) { const host = usePanelTabHost(); const title = host?.title; const owner = host?.owner; useLayoutEffect(() => { if (!title || !owner) return; - title(owner, { label, icon }); + title(owner, { label, icon, actions }); return () => title(owner, undefined); - }, [title, owner, label, icon]); + }, [title, owner, label, icon, actions]); } // Keyboard opens and reduced motion skip the push. Deciding here, rather than @@ -214,16 +222,22 @@ export function PanelWorkspace({ (tab) => tab.id === localSelection && tab.owner === value, ); const selected = selectedLocal?.id ?? value; - const tabs = items.flatMap((item) => [ - { - ...item, - ...titles[item.id], - icon: titles[item.id]?.icon ?? item.icon ?? , - }, - ...owned - .filter((tab) => tab.owner === item.id) - .map((tab) => ({ ...tab, icon: })), - ]); + const tabs = items.flatMap((item) => { + const title = titles[item.id]; + return [ + { + ...item, + ...title, + icon: title?.icon ?? item.icon ?? , + actions: title?.actions + ? [...title.actions, ...(item.actions ?? [])] + : item.actions, + }, + ...owned + .filter((tab) => tab.owner === item.id) + .map((tab) => ({ ...tab, icon: })), + ]; + }); const active = tabs.find((tab) => tab.id === selected); const choose = (id: string) => { const local = owned.find((tab) => tab.id === id); diff --git a/src/features/panels/service.ts b/src/features/panels/service.ts index 09f050b445..baa213061c 100644 --- a/src/features/panels/service.ts +++ b/src/features/panels/service.ts @@ -153,10 +153,10 @@ export type Panels = { handleOpenRequests( handler: (request: PanelOpenRequest) => boolean, ): () => void; - /** React hook for a panel component: names its tab, in place of the static - * `title`, while mounted, with an optional icon. Also works in a host workspace. - * Does nothing outside a tab. Absent on hosts - * that predate it, so feature-detect before calling. */ + /** React hook for a panel component: sets its tab title, optional icon and + * context-menu actions while mounted. Keep the actions array stable between + * renders. Also works in a host workspace and does nothing outside a tab. + * Feature-detect the hook on older hosts. */ useTabTitle?: typeof usePanelTabTitle; }; declare module "@deepseek-ai/cordis" { diff --git a/src/shared/design-system/ui/Tabs.tsx b/src/shared/design-system/ui/Tabs.tsx index 09d8df410c..d735a60aa2 100644 --- a/src/shared/design-system/ui/Tabs.tsx +++ b/src/shared/design-system/ui/Tabs.tsx @@ -373,6 +373,7 @@ export function TabMenu({ closeOthers: (() => boolean) | undefined; children: ReactNode; }) { + const hasMenu = !!(actions?.length || onRename || closeAll || closeOthers); const [open, setOpen] = useState(false); const trigger = useRef(null); const [anchor, setAnchor] = useState(); @@ -380,6 +381,11 @@ export function TabMenu({ // A host action runs once the menu has closed, from its tab, so a // destination it opens can take focus from there. const afterClose = useRef<() => void>(undefined); + useLayoutEffect(() => { + if (hasMenu) return; + afterClose.current = undefined; + setOpen(false); + }, [hasMenu]); // Middle-click closes a closable tab, without the browser's own default. const item = { className: "buzz-tabs-item", @@ -393,12 +399,15 @@ export function TabMenu({ onClose(); }, }; - if (!actions?.length && !onRename && !closeAll) - return {children}; return ( { + disabled={!hasMenu} + open={hasMenu && open} + onOpenChange={(next, details) => { + if (next && !hasMenu) { + details.cancel(); + return; + } if (next) closingAll.current = false; setOpen(next); }} @@ -406,7 +415,7 @@ export function TabMenu({ if (next) return; const action = afterClose.current; afterClose.current = undefined; - if (!action) return; + if (!hasMenu || !action) return; trigger.current ?.querySelector('[role="tab"], button') ?.focus(); @@ -422,8 +431,9 @@ export function TabMenu({ onTouchStart={() => setAnchor(undefined)} onKeyDown={(event) => { if ( - event.key === "ContextMenu" || - (event.shiftKey && event.key === "F10") + hasMenu && + (event.key === "ContextMenu" || + (event.shiftKey && event.key === "F10")) ) { event.preventDefault(); closingAll.current = false;