From ebeb6476ebbf3a4305b372fdf3c80a11f4950863 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 25 Nov 2026 15:38:41 -0300 Subject: [PATCH] fix(toolwindows): keep an undocked window opened from a menu on screen (#5946) An undocked or unpinned tool window shown with focus from a menu or popup hid again at once: the window takes focus a frame later, and in between the closing menu returned focus to the editor, which hides unpinned windows. A window whose focus request has not reached it yet no longer auto-hides; the request counts as served once the window gets focus or is hidden. Git > Show History with an undocked Git window now opens and stays. Closes #5941 Co-authored-by: formiat <29867335+formiat@users.noreply.github.com> --- src/workbench/toolwindows/store.test.ts | 12 ++++++++++++ src/workbench/toolwindows/store.ts | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) 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); } }