feat(settings): reopen Settings on the page it was closed on (#4927)

Settings now records the page it shows (when it opens and on each navigation) and reopens there, in the separate Settings window and across restarts. The page is UI state and stays in localStorage; Settings for New Projects keeps its own pages out of it, and an id that no longer exists falls back to the first page.

Co-authored-by: weigram <23372053+weigram@users.noreply.github.com>
This commit is contained in:
weigram
2026-11-21 09:22:16 +08:00
committed by GitHub
co-authored by weigram
parent bccf1bd7d6
commit 5cfb0d3311
3 changed files with 87 additions and 3 deletions
@@ -4,10 +4,13 @@ import { DEFAULT_SSH_CONFIG } from "@/platform/remoteSettings";
import { SUPPORTED_LOCALES } from "@/platform/i18n";
import { useProject } from "@/platform/project";
import { DEFAULT_SETTINGS, getAt, setAt, validateSettings } from "../model/schema";
import { DEFAULT_PAGE } from "../model/staging";
import { lastSettingsPage, rememberSettingsPage } from "../model/lastPage";
import {
applySettings,
closeSettings,
openSettings,
selectPage,
pendingChanges,
persistChanges,
resetPage,
@@ -339,3 +342,49 @@ describe("literal map entry staging", () => {
expect(useSettingsDialog.getState().staged).toEqual({});
});
});
describe("reopening Settings", () => {
beforeEach(() => {
localStorage.clear();
useProject.setState({ settings: structuredClone(DEFAULT_SETTINGS) as Settings, project: null });
useSettingsDialog.setState({ pageId: DEFAULT_PAGE, newProjects: false });
});
it("reopens on the page the last session left", () => {
openSettings();
selectPage("editor.general");
closeSettings();
// A new session starts from the default page; the remembered one has to win.
useSettingsDialog.setState({ pageId: DEFAULT_PAGE });
openSettings();
expect(useSettingsDialog.getState().pageId).toBe("editor.general");
});
it("remembers the page of a deep link that opens Settings directly", () => {
openSettings("tools.sshConfigurations#add");
expect(lastSettingsPage()).toBe("tools.sshConfigurations");
expect(useSettingsDialog.getState().focus).toBe("add");
});
it("keeps Settings for New Projects out of the remembered page", () => {
selectPage("keymap");
openSettings("languages.java", { newProjects: true });
selectPage("languages.python");
expect(lastSettingsPage()).toBe("keymap");
openSettings();
expect(useSettingsDialog.getState().pageId).toBe("keymap");
});
it("falls back to the first page when nothing was recorded", () => {
openSettings();
expect(useSettingsDialog.getState().pageId).toBe(DEFAULT_PAGE);
});
it("ignores a recorded value that is not a page id", () => {
rememberSettingsPage("editor.general");
localStorage.setItem("openidea.settings.lastPage.v1", "not a page!");
expect(lastSettingsPage()).toBeNull();
openSettings();
expect(useSettingsDialog.getState().pageId).toBe(DEFAULT_PAGE);
});
});
+29
View File
@@ -0,0 +1,29 @@
/**
* The page Settings was last shown on (IDEA reopens Settings there). This is UI state, not a
* setting: it stays out of the settings JSON and lives in localStorage, which the separate
* Settings window shares with the workbench through the same origin.
*/
const KEY = "openidea.settings.lastPage.v1";
/** Page id characters of every built-in and plugin page (`editor.general`, `tools.sshConfigurations`). */
const PAGE_ID = /^[\w.-]{1,200}$/;
/** The page to reopen Settings on, or `null` when nothing usable was recorded. */
export function lastSettingsPage(): string | null {
try {
const value = localStorage.getItem(KEY);
return value && PAGE_ID.test(value) ? value : null;
} catch {
return null;
}
}
/** Records the page Settings shows; `null` forgets it. */
export function rememberSettingsPage(pageId: string | null): void {
try {
if (pageId) localStorage.setItem(KEY, pageId);
else localStorage.removeItem(KEY);
} catch {
// Without storage the dialog opens on the first page; nothing else depends on this.
}
}
+9 -3
View File
@@ -6,6 +6,7 @@
import { create } from "zustand";
import { call, commands, isTauri, type Settings } from "@/platform/ipc";
import { loadSettings, useProject } from "@/platform/project";
import { lastSettingsPage, rememberSettingsPage } from "./lastPage";
import { deepEqual, getAt, putAt, setAt, validateSettings, type ValidSettings } from "./schema";
export type Scope = "global" | "project";
@@ -112,7 +113,9 @@ export function splitPageTarget(target: string): { pageId: string; focus: string
export function openSettings(pageId?: string, opts: { newProjects?: boolean; globalBase?: ValidSettings } = {}) {
const s = useSettingsDialog.getState();
const base = validateSettings(useProject.getState().settings);
const target = pageId ? splitPageTarget(pageId) : { pageId: s.pageId, focus: null };
const newProjects = opts.newProjects ?? false;
// IDEA reopens Settings on the page it was left on; Settings for New Projects has its own pages.
const target = splitPageTarget(pageId ?? (newProjects ? s.pageId : (lastSettingsPage() ?? s.pageId)));
useSettingsDialog.setState({
open: true,
projectStructureOpen: false,
@@ -121,7 +124,7 @@ export function openSettings(pageId?: string, opts: { newProjects?: boolean; glo
query: "",
base,
globalBase: opts.globalBase ?? base,
newProjects: opts.newProjects ?? false,
newProjects,
staged: {},
stagedSecrets: {},
stagedExternal: {},
@@ -129,6 +132,7 @@ export function openSettings(pageId?: string, opts: { newProjects?: boolean; glo
saving: false,
error: null,
});
if (!newProjects) rememberSettingsPage(target.pageId);
if (!opts.globalBase) void loadGlobalBase();
}
@@ -214,7 +218,9 @@ export function closeSettings() {
/** Shows `pageId` (`page#focus` also selects `focus` on it). */
export function selectPage(pageId: string) {
useSettingsDialog.setState(splitPageTarget(pageId));
const target = splitPageTarget(pageId);
useSettingsDialog.setState(target);
if (!useSettingsDialog.getState().newProjects) rememberSettingsPage(target.pageId);
}
export function setQuery(query: string) {