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:
@@ -81,8 +81,16 @@ test('Opaque mode retains background ownership and the amount for re-enabling',
|
||||
|
||||
test('Saved legacy light themes migrate once and preserve both original palettes', async () => {
|
||||
const { customThemes, CUSTOM_THEMES_KEY } = await import('../../../src/lib/theme/customThemes.ts');
|
||||
const { migrateLegacyPreferences } = await import('../../../src/lib/settings/migration.ts');
|
||||
const { LEGACY_KEYS } = await import('../../../src/lib/settings/store.ts');
|
||||
const legacy = { schema: 1, id: 'saved-light', name: 'Saved light', light: { ...palette, bg: '#fff' }, dark: { ...palette, bg: '#111' } };
|
||||
const stored = new Map([[CUSTOM_THEMES_KEY, JSON.stringify([legacy])], ['runestone.appearance', JSON.stringify({ theme: 'custom:saved-light', mode: 'light' })]]);
|
||||
const old = new Map([[CUSTOM_THEMES_KEY, JSON.stringify([legacy])], ['runestone.appearance', JSON.stringify({ theme: 'custom:saved-light', mode: 'light' })]]);
|
||||
const sections = migrateLegacyPreferences(key => old.get(key) ?? null);
|
||||
assert.equal((sections.customThemes as { schema: number }[])[0].schema, 2);
|
||||
assert.deepEqual(sections.customThemesLegacy, [legacy]);
|
||||
assert.deepEqual(sections.appearance, { theme: 'custom:saved-light', mode: 'light' });
|
||||
// The browser preview store reads the sections from their old keys.
|
||||
const stored = new Map(Object.entries(sections).map(([section, value]) => [LEGACY_KEYS[section as keyof typeof LEGACY_KEYS], JSON.stringify(value)]));
|
||||
const previous = globalThis.localStorage;
|
||||
try {
|
||||
globalThis.localStorage = { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => stored.set(key, value) } as unknown as Storage;
|
||||
@@ -90,22 +98,18 @@ test('Saved legacy light themes migrate once and preserve both original palettes
|
||||
assert.deepEqual(themeModes('custom:saved-light'), ['light']);
|
||||
assert.equal(normalizeAppearance({ theme: 'custom:saved-light', mode: 'dark' }).mode, 'light');
|
||||
assert.deepEqual(exportAppearanceTheme('custom:saved-light').colors.bg, '#fff');
|
||||
assert.equal(JSON.parse(stored.get(CUSTOM_THEMES_KEY)!)[0].schema, 2);
|
||||
assert.deepEqual(JSON.parse(stored.get(`${CUSTOM_THEMES_KEY}.legacy`)!)[0], legacy);
|
||||
assert.deepEqual(customThemes('dark')[0].colors, legacy.light);
|
||||
assert.deepEqual(customThemes('dark')[0].colors, legacy.light, 'The converted theme keeps its palette in both modes');
|
||||
} finally { globalThis.localStorage = previous; }
|
||||
});
|
||||
|
||||
test('Legacy migration storage failure leaves the original palette available', async () => {
|
||||
test('Reading custom themes never writes, and an unconverted version 1 theme uses the requested palette', async () => {
|
||||
const { customThemes, CUSTOM_THEMES_KEY } = await import('../../../src/lib/theme/customThemes.ts');
|
||||
const legacy = { schema: 1, id: 'quota-light', name: 'Quota light', light: { ...palette, bg: '#fefefe' }, dark: palette };
|
||||
const raw = JSON.stringify([legacy]);
|
||||
const previous = globalThis.localStorage;
|
||||
let original = raw;
|
||||
try {
|
||||
globalThis.localStorage = { getItem: (key: string) => key === CUSTOM_THEMES_KEY ? original : null, setItem: () => { throw new Error('Quota exceeded'); } } as unknown as Storage;
|
||||
globalThis.localStorage = { getItem: (key: string) => key === CUSTOM_THEMES_KEY ? JSON.stringify([legacy]) : null, setItem: () => { throw new Error('Quota exceeded'); } } as unknown as Storage;
|
||||
assert.deepEqual(customThemes('light')[0].colors, legacy.light);
|
||||
assert.equal(original, raw);
|
||||
assert.deepEqual(customThemes('dark')[0].colors, legacy.light);
|
||||
assert.deepEqual(customThemes('dark')[0].colors, palette);
|
||||
assert.deepEqual(customThemes()[0].colors, palette, 'Without a saved appearance, the dark palette applies');
|
||||
} finally { globalThis.localStorage = previous; }
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user