diff --git a/src/workbench/toolwindows/store.test.ts b/src/workbench/toolwindows/store.test.ts index 035b954a90..1c8d8b93f1 100644 --- a/src/workbench/toolwindows/store.test.ts +++ b/src/workbench/toolwindows/store.test.ts @@ -693,6 +693,18 @@ describe("view modes", () => { expect(isToolWindowVisible("terminal")).toBe(false); }); + it("an undocked window activated from a menu survives the menu returning focus to the editor", () => { + setToolWindowViewMode("terminal", "undock"); + showToolWindow("terminal", true); + // The closing menu restores focus to the editor before the window takes it. + setFocusedToolWindow(null, true); + expect(isToolWindowVisible("terminal")).toBe(true); + + setFocusedToolWindow("terminal"); + setFocusedToolWindow(null, true); + expect(isToolWindowVisible("terminal")).toBe(false); + }); + it("stay when focus goes to a popup or dialog; pinned windows never hide on focus", () => { setToolWindowViewMode("project", "dockUnpinned"); showToolWindow("terminal"); diff --git a/src/workbench/toolwindows/store.ts b/src/workbench/toolwindows/store.ts index 2ff219d83d..15cd47ac18 100644 --- a/src/workbench/toolwindows/store.ts +++ b/src/workbench/toolwindows/store.ts @@ -548,6 +548,13 @@ export function isToolWindowVisible(id: string): boolean { /** Last shown or focused tool window, kept after it is hidden (Jump to Last Tool Window). */ let lastUsed: string | null = null; +/** + * `seq` of the last focus request that reached its window or was dropped with it. A window with a + * newer request does not hide as unpinned: a menu that ran the action returns focus to the editor + * before the window takes it. + */ +let servedFocusSeq = 0; + export function lastUsedToolWindow(): string | null { return lastUsed && anchorOf(lastUsed) ? lastUsed : null; } @@ -589,6 +596,7 @@ export function hideToolWindow(id: string) { const slot = slotOfId(id); const s = useToolWindowLayout.getState(); if (!slot || s.visible[slot] !== id) return; + if (s.focusRequest?.id === id) servedFocusSeq = s.focusRequest.seq; const hadFocus = s.focused === id || isFocusWithinToolWindow(id); const nextVisible = { ...s.visible, [slot]: null }; // The side stays maximized while its other area still shows a registered window. @@ -846,6 +854,8 @@ export function restoreMaximizedToolWindow() { */ export function setFocusedToolWindow(id: string | null, editor = false) { const s = useToolWindowLayout.getState(); + const request = s.focusRequest; + if (id && request?.id === id) servedFocusSeq = request.seq; if (s.focused !== id) { if (id) lastUsed = id; useToolWindowLayout.setState({ focused: id, lastActive: id ?? s.lastActive }); @@ -855,7 +865,8 @@ export function setFocusedToolWindow(id: string | null, editor = false) { if (!id && !editor) return; for (const slot of SLOTS) { const shown = useToolWindowLayout.getState().visible[slot]; - if (shown && shown !== id && viewModeOf(shown) !== "dockPinned") hideToolWindow(shown); + const pending = request?.id === shown && request.seq !== servedFocusSeq; + if (shown && shown !== id && !pending && viewModeOf(shown) !== "dockPinned") hideToolWindow(shown); } }