Порівняти коміти

...
Автор SHA1 Повідомлення Дата
Deng Liu 8ac1ee2b3e feat(toolwindows): remember maximized state across hide and reopen
Copilot-Session: fddd7ed1-c260-45fb-9523-7117cdd5e772
2026-11-25 09:56:45 -05:00
2 змінених файлів з 145 додано та 10 видалено

No files matched your search

+78 -1
Переглянути файл
@@ -151,7 +151,7 @@ describe("tool window semantics", () => {
visible: { bottom: null, left: "project" },
});
showToolWindow("terminal");
toggleMaximizeToolWindow("terminal");
expect(useToolWindowLayout.getState().maximized).toBe("bottom");
// The left side is covered by the maximized bottom: its window is not the one shown.
useToolWindowLayout.setState({ focused: null, lastActive: "project" });
hideSideToolWindows();
@@ -252,6 +252,83 @@ describe("tool window persistence", () => {
expect(isToolWindowVisible("project")).toBe(true);
});
it("remembers a tool window's maximized state across hide, reopen, and reload until explicitly restored", () => {
const disposeK8s = registerToolWindow({
id: "plugin.k8s.view:kubernetes",
title: "Kubernetes",
icon: "box",
anchor: "right",
secondary: true,
component: Empty,
});
try {
showToolWindow("plugin.k8s.view:kubernetes", true);
toggleMaximizeToolWindow("plugin.k8s.view:kubernetes");
expect(useToolWindowLayout.getState().maximized).toBe("right");
// Hiding reveals the editor and other tool windows, then clicking the stripe button restores maximize.
toggleToolWindow("plugin.k8s.view:kubernetes");
expect(isToolWindowVisible("plugin.k8s.view:kubernetes")).toBe(false);
expect(useToolWindowLayout.getState().maximized).toBeNull();
expect(isToolWindowVisible("project")).toBe(true);
toggleToolWindow("plugin.k8s.view:kubernetes");
expect(useToolWindowLayout.getState().maximized).toBe("right");
expect(useToolWindowLayout.getState().visible["right-split"]).toBe("plugin.k8s.view:kubernetes");
// Opening a non-maximized window on another side unmaximizes; switching back restores maximize.
toggleToolWindow("plugin.k8s.view:kubernetes");
showToolWindow("terminal", true);
expect(useToolWindowLayout.getState().maximized).toBeNull();
showToolWindow("plugin.k8s.view:kubernetes", true);
expect(useToolWindowLayout.getState().maximized).toBe("right");
// Persisted per project across reload (starts unmaximized on reload, restores on hide/reopen).
hideToolWindow("plugin.k8s.view:kubernetes");
loadToolWindowLayout("/work/a");
expect(useToolWindowLayout.getState().maximized).toBeNull();
showToolWindow("plugin.k8s.view:kubernetes", true);
expect(useToolWindowLayout.getState().maximized).toBe("right");
// Explicit Restore Size clears the remembered state.
toggleMaximizeToolWindow("plugin.k8s.view:kubernetes");
expect(useToolWindowLayout.getState().maximized).toBeNull();
hideToolWindow("plugin.k8s.view:kubernetes");
showToolWindow("plugin.k8s.view:kubernetes", true);
expect(useToolWindowLayout.getState().maximized).toBeNull();
// Manual resize after navigation-restore also clears the remembered state.
toggleMaximizeToolWindow("plugin.k8s.view:kubernetes");
restoreMaximizedToolWindow();
setToolWindowSize("right", 420);
hideToolWindow("plugin.k8s.view:kubernetes");
showToolWindow("plugin.k8s.view:kubernetes", true);
expect(useToolWindowLayout.getState().maximized).toBeNull();
} finally {
disposeK8s();
}
});
it("does not leak remembered maximize to another window in the same slot and clears it on Restore Default Layout", () => {
showToolWindow("terminal");
toggleMaximizeToolWindow("terminal");
expect(useToolWindowLayout.getState().maximized).toBe("bottom");
// Replacing terminal in the bottom slot with a non-maximized window unmaximizes.
showToolWindow("problems", true);
expect(useToolWindowLayout.getState().maximized).toBeNull();
// Switching back to terminal restores its maximized state.
showToolWindow("terminal", true);
expect(useToolWindowLayout.getState().maximized).toBe("bottom");
// Restore Default Layout forgets remembered maximize.
resetToolWindowPlacements();
expect(useToolWindowLayout.getState().maximized).toBeNull();
hideToolWindow("terminal");
showToolWindow("terminal", true);
expect(useToolWindowLayout.getState().maximized).toBeNull();
});
it("persists visible windows and sizes per project root", () => {
showToolWindow("structure");
showToolWindow("terminal");
+67 -9
Переглянути файл
@@ -87,6 +87,8 @@ export interface ToolWindowLayoutState {
/** Per side: the primary area's share when both areas show. */
splits: Record<ToolWindowAnchor, number>;
maximized: ToolWindowAnchor | null;
/** Per-window maximized preference: remembered across hide/show and reloads until restored (persisted). */
maximizedWindows: Readonly<Record<string, boolean>>;
/** Tool window containing DOM focus. */
focused: string | null;
/** Most recently activated window (target of Shift+Escape). */
@@ -119,6 +121,7 @@ const initial = (): ToolWindowLayoutState => ({
windowSizes: {},
splits: { ...DEFAULT_SPLITS },
maximized: null,
maximizedWindows: {},
focused: null,
lastActive: null,
mounted: new Set(Object.values(DEFAULT_VISIBLE).filter((v): v is string => !!v)),
@@ -200,10 +203,18 @@ export function moveToolWindow(id: string, slot: ToolWindowSlot, before: string
visible[slot] = id;
}
const oldAnchor = slotAnchor(oldSlot);
const droppedMaximized = wasVisible && s.maximized === oldAnchor && oldAnchor !== slotAnchor(slot);
let maximizedWindows = s.maximizedWindows;
if (droppedMaximized && maximizedWindows[id]) {
const next = { ...maximizedWindows };
delete next[id];
maximizedWindows = next;
}
update({
placements,
visible,
maximized: wasVisible && s.maximized === oldAnchor && oldAnchor !== slotAnchor(slot) ? null : s.maximized,
maximized: droppedMaximized ? null : s.maximized,
maximizedWindows,
});
}
@@ -216,7 +227,7 @@ export function resetToolWindowPlacements() {
const tw = visible[slot] ? useContributions.getState().toolWindows.get(visible[slot]!) : undefined;
if (tw && slotOf(tw) !== slot) visible[slot] = null;
}
update({ placements: {}, stripeButtons: {}, viewModes: {}, visible });
update({ placements: {}, stripeButtons: {}, viewModes: {}, maximizedWindows: {}, maximized: null, visible });
}
export function viewModeOf(
@@ -261,6 +272,7 @@ function persist() {
placements,
stripeButtons,
viewModes,
maximizedWindows,
distractionFreeStash,
narrowStash,
} = useToolWindowLayout.getState();
@@ -269,7 +281,17 @@ function persist() {
try {
localStorage.setItem(
storageKey(scope),
JSON.stringify({ v: LAYOUT_VERSION, visible, sizes, windowSizes, splits, placements, stripeButtons, viewModes }),
JSON.stringify({
v: LAYOUT_VERSION,
visible,
sizes,
windowSizes,
splits,
placements,
stripeButtons,
viewModes,
maximizedWindows,
}),
);
} catch {
/* storage unavailable */
@@ -353,6 +375,13 @@ function sanitizeViewModes(raw: unknown): Record<string, ToolWindowViewMode> {
return out;
}
function sanitizeMaximizedWindows(raw: unknown): Record<string, boolean> {
const out: Record<string, boolean> = {};
if (raw && typeof raw === "object")
for (const [id, v] of Object.entries(raw as Record<string, unknown>)) if (v === true) out[id] = true;
return out;
}
function sanitizeVisible(raw: unknown): SlotRecord {
if (!raw || typeof raw !== "object") return { ...DEFAULT_VISIBLE };
const out = { ...NONE };
@@ -413,6 +442,7 @@ export function loadToolWindowLayout(scope: string) {
placements,
stripeButtons: sanitizeStripeButtons(saved?.stripeButtons),
viewModes: sanitizeViewModes(saved?.viewModes),
maximizedWindows: sanitizeMaximizedWindows(saved?.maximizedWindows),
mounted: new Set(Object.values(visible).filter((v): v is string => !!v)),
});
publishVisibility();
@@ -534,15 +564,23 @@ export function showToolWindow(id: string, focus = false) {
const a = slotAnchor(slot);
lastUsed = id;
const s = useToolWindowLayout.getState();
const wasVisible = isToolWindowVisible(id);
const nextVisible = { ...s.visible, [slot]: id };
const mounted = s.mounted.has(id) ? s.mounted : new Set(s.mounted).add(id);
const nextMaximized =
!wasVisible && s.maximizedWindows[id]
? a
: s.maximized === a && (wasVisible || windowsOn(a, nextVisible).some((wid) => s.maximizedWindows[wid]))
? a
: null;
update({
visible: { ...s.visible, [slot]: id },
visible: nextVisible,
// Opening a window puts its button on the stripe (IDEA).
stripeButtons: s.stripeButtons[id] ? s.stripeButtons : { ...s.stripeButtons, [id]: true },
mounted,
lastActive: id,
stashed: null,
maximized: s.maximized && s.maximized !== a ? null : s.maximized,
maximized: nextMaximized,
focusRequest: focus ? { id, seq: ++focusSeq } : s.focusRequest,
});
}
@@ -732,10 +770,17 @@ export function setToolWindowSize(anchor: ToolWindowAnchor, px: number) {
const s = useToolWindowLayout.getState();
const v = Math.round(px);
if (s.maximized === anchor || v < 80 || s.sizes[anchor] === v) return;
const shown = windowsOn(anchor, s.visible);
const windowSizes = rememberEachSize()
? { ...s.windowSizes, ...Object.fromEntries(windowsOn(anchor, s.visible).map((id) => [id, v])) }
? { ...s.windowSizes, ...Object.fromEntries(shown.map((id) => [id, v])) }
: s.windowSizes;
update({ sizes: { ...s.sizes, [anchor]: v }, windowSizes });
let maximizedWindows = s.maximizedWindows;
if (shown.some((id) => maximizedWindows[id])) {
const next = { ...maximizedWindows };
for (const id of shown) delete next[id];
maximizedWindows = next;
}
update({ sizes: { ...s.sizes, [anchor]: v }, windowSizes, maximizedWindows });
}
export type ResizeDirection = "left" | "right" | "up" | "down";
@@ -773,9 +818,18 @@ export function setToolWindowSplit(anchor: ToolWindowAnchor, ratio: number) {
export function toggleMaximizeToolWindow(id: string) {
const a = anchorOf(id);
if (!a) return;
if (!isToolWindowVisible(id)) showToolWindow(id, true);
const wasVisible = isToolWindowVisible(id);
if (!wasVisible) showToolWindow(id, true);
const s = useToolWindowLayout.getState();
update({ maximized: s.maximized === a ? null : a, focusRequest: { id, seq: ++focusSeq } }, false);
const maximizing = wasVisible ? s.maximized !== a : true;
const maximizedWindows = { ...s.maximizedWindows };
if (maximizing) {
maximizedWindows[id] = true;
} else {
for (const wid of windowsOn(a, s.visible)) delete maximizedWindows[wid];
delete maximizedWindows[id];
}
update({ maximized: maximizing ? a : null, maximizedWindows, focusRequest: { id, seq: ++focusSeq } });
}
/** Reveals the editor when navigation opens or focuses an editor tab. */
@@ -827,6 +881,7 @@ export interface ToolWindowLayoutSnapshot {
placements: Record<string, ToolWindowPlacement>;
stripeButtons: Record<string, boolean>;
viewModes: Record<string, ToolWindowViewMode>;
maximizedWindows?: Record<string, boolean>;
}
export function toolWindowLayoutSnapshot(): ToolWindowLayoutSnapshot {
@@ -839,6 +894,7 @@ export function toolWindowLayoutSnapshot(): ToolWindowLayoutSnapshot {
placements: { ...s.placements },
stripeButtons: { ...s.stripeButtons },
viewModes: { ...s.viewModes },
maximizedWindows: { ...s.maximizedWindows },
};
}
@@ -853,6 +909,7 @@ export function sanitizeLayoutSnapshot(raw: unknown): ToolWindowLayoutSnapshot {
placements: sanitizePlacements(r.placements),
stripeButtons: sanitizeStripeButtons(r.stripeButtons),
viewModes: sanitizeViewModes(r.viewModes),
maximizedWindows: sanitizeMaximizedWindows(r.maximizedWindows),
};
}
@@ -878,6 +935,7 @@ export function applyToolWindowLayout(snapshot: ToolWindowLayoutSnapshot) {
placements,
stripeButtons: { ...snapshot.stripeButtons },
viewModes: { ...snapshot.viewModes },
maximizedWindows: sanitizeMaximizedWindows(snapshot.maximizedWindows),
mounted,
maximized: null,
stashed: null,