From d4b2ade24dae3ec0c13c2e85491798eaca61a35b Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 15 Nov 2026 16:35:49 +0800 Subject: [PATCH] fix(toolwindows): a window that loses its last tab takes its stripe button with it (#3504) Hierarchy, Run and Debug hid when their last tab closed but kept their stripe button; Find and Find Usages stayed open and empty. Like IDEA (hide on empty content; Hierarchy unavailable without content), they now hide and drop their button until new content opens them again. Terminal keeps its button. --- src/features/code-insight/hierarchy.test.ts | 2 +- src/features/code-insight/hierarchy.ts | 4 +-- .../code-insight/usageResults.test.ts | 27 ++++++++++++++++--- src/features/code-insight/usages.ts | 8 ++++++ .../debugger/__tests__/closeTab.test.ts | 2 +- src/features/debugger/state.ts | 4 +-- src/features/run/RunView.tsx | 3 ++- src/features/run/closeRunTab.test.ts | 2 +- .../search/__tests__/findWindow.test.ts | 14 ++++++++++ src/features/search/find/windowStore.ts | 2 ++ src/workbench/contributions.ts | 6 ++++- src/workbench/toolwindows/store.test.ts | 13 +++++++++ src/workbench/toolwindows/store.ts | 2 +- 13 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/features/code-insight/hierarchy.test.ts b/src/features/code-insight/hierarchy.test.ts index dd40c32fd..a9f7be98b 100644 --- a/src/features/code-insight/hierarchy.test.ts +++ b/src/features/code-insight/hierarchy.test.ts @@ -303,6 +303,6 @@ describe("closing the hierarchy tab", () => { useHierarchy.setState({ root: { name: "Number", path: "/p/Number.java", line: 0, col: 0 } as never }); closeHierarchy(); expect(useHierarchy.getState().root).toBeNull(); - expect(hide).toHaveBeenCalledWith("hierarchy"); + expect(hide).toHaveBeenCalledWith("hierarchy", { removeFromStripe: true }); }); }); diff --git a/src/features/code-insight/hierarchy.ts b/src/features/code-insight/hierarchy.ts index ff8f5dcde..cfe578ae1 100644 --- a/src/features/code-insight/hierarchy.ts +++ b/src/features/code-insight/hierarchy.ts @@ -92,10 +92,10 @@ export function resetHierarchy() { })); } -/** Closes the hierarchy's content tab; with no content left the tool window hides. */ +/** Closes the hierarchy's content tab; with no content left the tool window and its stripe button go. */ export function closeHierarchy() { resetHierarchy(); - toolWindowService().hide("hierarchy"); + toolWindowService().hide("hierarchy", { removeFromStripe: true }); } /** The empty-result line of the hierarchy tree. */ diff --git a/src/features/code-insight/usageResults.test.ts b/src/features/code-insight/usageResults.test.ts index f5283ce8c..f24725f15 100644 --- a/src/features/code-insight/usageResults.test.ts +++ b/src/features/code-insight/usageResults.test.ts @@ -1,9 +1,10 @@ import { describe, expect, it, vi } from "vitest"; const show = vi.fn(); +const hide = vi.fn(); vi.mock("@/workbench/contributions", async (orig) => ({ ...(await orig()), - toolWindowService: () => ({ show }), + toolWindowService: () => ({ show, hide }), })); /** The editor at the caret (`null`: a search from outside an editor). */ let caretView: import("@codemirror/view").EditorView | null = null; @@ -35,8 +36,16 @@ vi.mock("@tauri-apps/api/core", async (orig) => ({ const { registerDocumentIndexSync } = await import("@/workbench/contributions"); -const { activeUsageSearch, findUsages, openUsageTab, pinUsageTab, resetUsages, showUsageResults, useUsages } = - await import("./usages"); +const { + activeUsageSearch, + closeUsageTab, + findUsages, + openUsageTab, + pinUsageTab, + resetUsages, + showUsageResults, + useUsages, +} = await import("./usages"); describe("showUsageResults", () => { it("shows given usages in the Find Usages tool window", async () => { @@ -221,4 +230,16 @@ describe("usage tabs", () => { openUsageTab(search(104)); expect(useUsages.getState().tabs.map((t) => t.id)).toEqual([102, 104]); }); + + it("take the tool window and its stripe button away with the last one", () => { + resetUsages(); + hide.mockClear(); + openUsageTab(search(201)); + pinUsageTab(201, true); + openUsageTab(search(202)); + closeUsageTab(201); + expect(hide).not.toHaveBeenCalled(); + closeUsageTab(202); + expect(hide).toHaveBeenCalledWith("usages", { removeFromStripe: true }); + }); }); diff --git a/src/features/code-insight/usages.ts b/src/features/code-insight/usages.ts index b9bad7c7c..316315c62 100644 --- a/src/features/code-insight/usages.ts +++ b/src/features/code-insight/usages.ts @@ -168,6 +168,14 @@ export function closeUsageTab(id: number) { const active = s.active === id ? (tabs[Math.min(i, tabs.length - 1)]?.id ?? null) : s.active; return { tabs, active }; }); + // As in IDEA, the tool window and its stripe button go away with its last tab. + if (!useUsages.getState().tabs.length) { + try { + toolWindowService().hide("usages", { removeFromStripe: true }); + } catch { + // Workbench not ready (tests). + } + } } /** Rerun of the selected tab (⌘R in the Find Usages tool window). */ diff --git a/src/features/debugger/__tests__/closeTab.test.ts b/src/features/debugger/__tests__/closeTab.test.ts index c941420ca..9fa445a3d 100644 --- a/src/features/debugger/__tests__/closeTab.test.ts +++ b/src/features/debugger/__tests__/closeTab.test.ts @@ -15,5 +15,5 @@ it("hides the Debug tool window once its last session tab is closed", async () = expect(await closeSessionTab(1)).toBe(true); expect(hide).not.toHaveBeenCalled(); expect(await closeSessionTab(2)).toBe(true); - expect(hide).toHaveBeenCalledWith("debug"); + expect(hide).toHaveBeenCalledWith("debug", { removeFromStripe: true }); }); diff --git a/src/features/debugger/state.ts b/src/features/debugger/state.ts index c2a440071..b7c47bdfa 100644 --- a/src/features/debugger/state.ts +++ b/src/features/debugger/state.ts @@ -880,10 +880,10 @@ export async function closeSessionTab(id: DebugSessionId): Promise { await stopSession(s.id); } removeSession(s.id); - // As in IDEA, the tool window goes away with its last tab. + // As in IDEA, the tool window and its stripe button go away with its last tab. if (!Object.keys(useDebug.getState().sessions).length) { try { - toolWindowService().hide("debug"); + toolWindowService().hide("debug", { removeFromStripe: true }); } catch { // Workbench not ready (tests). } diff --git a/src/features/run/RunView.tsx b/src/features/run/RunView.tsx index eb14da9bc..5fa9dc381 100644 --- a/src/features/run/RunView.tsx +++ b/src/features/run/RunView.tsx @@ -303,7 +303,8 @@ function currentTab( export async function closeRunTab(id: string, traces: readonly StackTraceTab[]) { if (traces.some((t) => t.id === id)) closeStackTrace(id); else if (!(await closeSessionTab(id))) return; - if (!useRun.getState().sessions.length && !useStackTraces.getState().tabs.length) toolWindowService().hide("run"); + if (!useRun.getState().sessions.length && !useStackTraces.getState().tabs.length) + toolWindowService().hide("run", { removeFromStripe: true }); } /** Session (and stack trace) tabs in the tool window header. */ diff --git a/src/features/run/closeRunTab.test.ts b/src/features/run/closeRunTab.test.ts index 0c5c6ccc3..feb2c6060 100644 --- a/src/features/run/closeRunTab.test.ts +++ b/src/features/run/closeRunTab.test.ts @@ -24,5 +24,5 @@ it("hides the Run tool window once its last tab is closed", async () => { await closeRunTab("t", useStackTraces.getState().tabs); expect(hide).not.toHaveBeenCalled(); await closeRunTab("b", useStackTraces.getState().tabs); - expect(hide).toHaveBeenCalledWith("run"); + expect(hide).toHaveBeenCalledWith("run", { removeFromStripe: true }); }); diff --git a/src/features/search/__tests__/findWindow.test.ts b/src/features/search/__tests__/findWindow.test.ts index 16d3dc39d..96c77607f 100644 --- a/src/features/search/__tests__/findWindow.test.ts +++ b/src/features/search/__tests__/findWindow.test.ts @@ -1,8 +1,10 @@ import { afterEach, describe, expect, it, vi } from "vitest"; +import { provideToolWindowService, type ToolWindowService } from "@/workbench/contributions"; import type { FileMatches, SearchQuery } from "@/platform/ipc"; import { buildFindTree } from "../find/FindToolWindow"; import type { FindDeps } from "../find/session"; import { + closeFindTab, disposeFindWindow, openEditorResults, openInFindWindow, @@ -38,6 +40,18 @@ const file = (rel: string): FileMatches => ({ afterEach(disposeFindWindow); describe("Find tool window tabs", () => { + it("take the tool window and its stripe button away with the last one", () => { + const hide = vi.fn(); + provideToolWindowService({ show: vi.fn(), hide } as unknown as ToolWindowService); + const a = openInFindWindow(q("a"), "a", { deps }); + pinFindTab(a, true); + const b = openInFindWindow(q("b"), "b", { deps }); + closeFindTab(a); + expect(hide).not.toHaveBeenCalled(); + closeFindTab(b); + expect(hide).toHaveBeenCalledWith("find", { removeFromStripe: true }); + }); + it("reuses the selected tab unless it is pinned", () => { const a = openInFindWindow(q("a"), "a", { deps }); const b = openInFindWindow(q("b"), "b", { deps }); diff --git a/src/features/search/find/windowStore.ts b/src/features/search/find/windowStore.ts index a1b12cd50..3ff98ba2b 100644 --- a/src/features/search/find/windowStore.ts +++ b/src/features/search/find/windowStore.ts @@ -134,6 +134,8 @@ export function closeFindTab(id: number) { const active = s.active === id ? (tabs[Math.min(i, tabs.length - 1)]?.id ?? null) : s.active; return { tabs, active }; }); + // As in IDEA, the tool window and its stripe button go away with its last tab. + if (!useFindWindow.getState().tabs.length) maybeToolWindowService()?.hide("find", { removeFromStripe: true }); } export function disposeFindWindow() { diff --git a/src/workbench/contributions.ts b/src/workbench/contributions.ts index e4008ea36..b5cf26e9f 100644 --- a/src/workbench/contributions.ts +++ b/src/workbench/contributions.ts @@ -709,7 +709,11 @@ export interface EditorCloseSession { export interface ToolWindowService { show(id: string, focus?: boolean): void; - hide(id: string): void; + /** + * `removeFromStripe`: a window whose content is its tabs (Find, Run, Hierarchy…) lost its last + * one: its stripe button goes too, until new content opens it again (IDEA). + */ + hide(id: string, options?: { removeFromStripe?: boolean }): void; toggle(id: string): void; isVisible(id: string): boolean; } diff --git a/src/workbench/toolwindows/store.test.ts b/src/workbench/toolwindows/store.test.ts index 45185b8c6..441ecf569 100644 --- a/src/workbench/toolwindows/store.test.ts +++ b/src/workbench/toolwindows/store.test.ts @@ -476,6 +476,19 @@ describe("stripe buttons", () => { resetToolWindowPlacements(); expect(buttons()).toEqual({}); }); + + it("drops the button of a window that lost its last tab until new content opens it", () => { + disposers.push(registerToolWindow(find)); + showToolWindow("find"); + toolWindowServiceImpl.hide("find"); + expect(hasStripeButton(find, buttons())).toBe(true); + showToolWindow("find"); + toolWindowServiceImpl.hide("find", { removeFromStripe: true }); + expect(isToolWindowVisible("find")).toBe(false); + expect(hasStripeButton(find, buttons())).toBe(false); + showToolWindow("find"); + expect(hasStripeButton(find, buttons())).toBe(true); + }); }); describe("view modes", () => { diff --git a/src/workbench/toolwindows/store.ts b/src/workbench/toolwindows/store.ts index d55ef318f..db7e1feb9 100644 --- a/src/workbench/toolwindows/store.ts +++ b/src/workbench/toolwindows/store.ts @@ -742,7 +742,7 @@ function isFocusWithinToolWindow(id: string): boolean { export const toolWindowServiceImpl: ToolWindowService = { show: (id, focus) => showToolWindow(id, focus), - hide: hideToolWindow, + hide: (id, options) => (options?.removeFromStripe ? removeFromStripe(id) : hideToolWindow(id)), toggle: toggleToolWindow, isVisible: isToolWindowVisible, };