Keep preferences in one settings file and move tab transfer to the host

Keep the user preferences in settings.json in the app config folder.
WebView storage can be lost, and the host cannot read it. The file has
a schema number, a revision number, and one value for each section:
general, appearance, customThemes, customThemesLegacy, and language.
- settings_read gives all sections, the revision, and a one-time
  notice.
- settings_write replaces one section, writes the file atomically, and
  sends settings-changed to all windows. The limit is 8 MiB.
- settings_migrate writes the first file only when no file exists.
- The host moves a damaged file to settings.corrupt-<seconds>.json,
  starts from the defaults, and reports this one time.

Each window loads the sections before its first render. One store in
src/lib/settings/store.ts replaces the storage listeners. When no file
exists, the window normalizes the old localStorage values and migrates
them. The old keys stay in place for one release. The window layout
and the open tabs stay in localStorage on purpose. The browser preview
keeps all preferences in localStorage.

Keep tab transfer offers in host memory, not in localStorage, because
an offer can hold unsaved text. A window takes an offer once. The host
checks the workspace root and a 120 second expiry. When the receiver
accepts the tab, the host sends tab-transfer-accepted to the source
window only. A refused offer stays open, and the source tab stays open.
A new window and a drop on the tab ribbon use one check.

The smoke harness serves the new commands from one shared state for
each browser context. Tests read saved values from the mock settings
file. Rust, unit, and smoke tests cover the changes.
This commit is contained in:
Knotty Wyvern
2026-10-27 23:56:00 -07:00
parent a0eef01ed0
commit 5d5ba9e1f7
51 changed files with 1665 additions and 213 deletions
+14 -35
View File
@@ -1,3 +1,6 @@
import { readSection, writeSection } from '../settings/store.ts';
/** The old localStorage key. The customThemes section of the settings file replaces it. */
export const CUSTOM_THEMES_KEY = "runestone.custom-themes";
export const COLOR_TOKENS = ["bg", "bg-elevated", "bg-active", "border", "text", "text-dim", "text-strong", "accent", "accent-2", "selection", "error", "warn", "warn-hot", "ok", "special", "syntax-parameter", "syntax-keyword", "syntax-local", "syntax-static", "syntax-instance", "syntax-global", "syntax-string", "syntax-number", "syntax-comment", "syntax-type", "syntax-function"] as const;
export type ThemeColors = Partial<Record<typeof COLOR_TOKENS[number], string>>;
@@ -38,48 +41,24 @@ export function parseCustomTheme(input: unknown, legacyMode: "light" | "dark" =
return { schema: 2, id: value.id, name: value.name.trim(), colors: colors(value.schema === 1 ? legacyMode : "colors"), ...(fonts ? { fonts } : {}) };
}
const pendingLegacy = new Map<string, CustomTheme[]>();
/** The saved custom themes. Version 1 themes use the palette of `legacyMode`, by default the saved appearance mode. */
export function customThemes(legacyMode?: "light" | "dark"): CustomTheme[] {
try {
const raw = localStorage.getItem(CUSTOM_THEMES_KEY) || "[]";
const pending = pendingLegacy.get(raw);
if (pending) return pending.map(theme => ({ ...theme, colors: { ...theme.colors } }));
const stored = JSON.parse(raw);
const stored = readSection('customThemes');
if (!Array.isArray(stored)) return [];
if (!legacyMode) {
try { legacyMode = JSON.parse(localStorage.getItem('runestone.appearance') || 'null')?.mode === 'light' ? 'light' : 'dark'; }
catch { legacyMode = 'dark'; }
}
const parsed = stored.flatMap(value => { try { return [parseCustomTheme(value, legacyMode)]; } catch { return []; } });
if (stored.some(value => value?.schema === 1)) {
// Retain both original palettes before converting the saved selection to one palette.
try {
const backupKey = `${CUSTOM_THEMES_KEY}.legacy`;
const backup = JSON.parse(localStorage.getItem(backupKey) || '[]');
if (!Array.isArray(backup)) throw new Error('Invalid legacy backup');
const originals = backup;
for (const value of stored.filter(value => value?.schema === 1)) {
if (!originals.some(item => JSON.stringify(item) === JSON.stringify(value))) originals.push(value);
}
localStorage.setItem(backupKey, JSON.stringify(originals));
localStorage.setItem(CUSTOM_THEMES_KEY, JSON.stringify(stored.map(value => {
try { return parseCustomTheme(value, legacyMode); } catch { return value; }
})));
} catch {
// Storage failures must not hide readable themes or change their chosen palette.
pendingLegacy.set(raw, parsed);
}
}
return parsed;
if (!legacyMode) legacyMode = (readSection('appearance') as { mode?: unknown } | undefined)?.mode === 'light' ? 'light' : 'dark';
const mode = legacyMode;
return stored.flatMap(value => { try { return [parseCustomTheme(value, mode)]; } catch { return []; } });
} catch { return []; }
}
export function installTheme(theme: CustomTheme): void {
/** Throws at once if browser storage refuses the change. The promise reports a failed save. */
export function installTheme(theme: CustomTheme): Promise<void> {
const list = customThemes().filter(item => item.id !== theme.id);
localStorage.setItem(CUSTOM_THEMES_KEY, JSON.stringify([...list, parseCustomTheme(theme)]));
return writeSection('customThemes', [...list, parseCustomTheme(theme)]);
}
export function removeTheme(id: string): void {
localStorage.setItem(CUSTOM_THEMES_KEY, JSON.stringify(customThemes().filter(item => item.id !== id)));
/** Throws at once if browser storage refuses the change. The promise reports a failed save. */
export function removeTheme(id: string): Promise<void> {
return writeSection('customThemes', customThemes().filter(item => item.id !== id));
}