fix(settings): persist appearance preferences

Closes #10524
This commit is contained in:
t8y2 committed 2026-09-29 18:31:25 +00:00
1 parent d9b6d44d9d
commit 13c3f8205c
9 files changed
+416 -20

No files matched your search

@@ -0,0 +1,72 @@
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from "vitest";
const invoke = vi.hoisted(() => vi.fn());
vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => true }));
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
async function loadAppearance() {
vi.resetModules();
return import("@/lib/app/appAppearance");
}
beforeEach(() => {
invoke.mockReset();
window.localStorage.clear();
});
describe("Tauri appearance persistence", () => {
it("gives durable backend values precedence over stale WebView storage", async () => {
window.localStorage.setItem("dbx-locale", "en");
window.localStorage.setItem("dbx-theme", "light");
window.localStorage.setItem("dbx-theme-palette", "pearl");
invoke.mockResolvedValueOnce({ locale: "zh-CN", themeMode: "dark", themePalette: "cobalt", cornerStyle: "small" });
const { hydrateAppAppearance } = await loadAppearance();
await hydrateAppAppearance();
expect(window.localStorage.getItem("dbx-locale")).toBe("zh-CN");
expect(window.localStorage.getItem("dbx-theme")).toBe("dark");
expect(window.localStorage.getItem("dbx-theme-palette")).toBe("cobalt");
expect(window.localStorage.getItem("dbx-corner-style")).toBe("small");
expect(invoke).toHaveBeenCalledWith("load_app_appearance_settings", undefined);
expect(invoke).toHaveBeenCalledTimes(1);
});
it("migrates legacy WebView values when the durable record is empty", async () => {
window.localStorage.setItem("dbx-locale", "ja");
window.localStorage.setItem("dbx-theme", "system");
window.localStorage.setItem("dbx-theme-palette", "sage");
invoke.mockResolvedValueOnce({});
invoke.mockResolvedValueOnce(undefined);
const { hydrateAppAppearance } = await loadAppearance();
await hydrateAppAppearance();
expect(invoke).toHaveBeenNthCalledWith(1, "load_app_appearance_settings", undefined);
expect(invoke).toHaveBeenNthCalledWith(2, "update_app_appearance_settings", {
patch: { locale: "ja", themeMode: "system", themePalette: "sage" },
});
expect(window.localStorage.getItem("dbx-theme-palette")).toBe("sage");
});
it("persists explicit choices while leaving the caller synchronous", async () => {
invoke.mockResolvedValue(undefined);
const { persistAppAppearancePatch, persistAppLocale } = await loadAppearance();
persistAppAppearancePatch({ themeMode: "dark", themePalette: "graphite" });
persistAppLocale("ko");
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
expect(window.localStorage.getItem("dbx-theme")).toBe("dark");
expect(window.localStorage.getItem("dbx-theme-palette")).toBe("graphite");
expect(window.localStorage.getItem("dbx-locale")).toBe("ko");
expect(invoke).toHaveBeenNthCalledWith(1, "update_app_appearance_settings", {
patch: { themeMode: "dark", themePalette: "graphite" },
});
expect(invoke).toHaveBeenNthCalledWith(2, "set_app_locale", { locale: "ko" });
});
});
+175
View File
@@ -0,0 +1,175 @@
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { APP_CORNER_STYLE_STORAGE_KEY, APP_CUSTOM_UI_DARK_STORAGE_KEY, APP_CUSTOM_UI_STORAGE_KEY, APP_THEME_PALETTE_STORAGE_KEY, APP_THEME_STORAGE_KEY, type AppCustomUiColors, type AppCornerStyle, type AppThemeMode, type AppThemePalette } from "@/lib/app/appTheme";
export interface AppAppearanceSettings {
locale?: string | null;
themeMode?: AppThemeMode | string | null;
themePalette?: AppThemePalette | string | null;
customUiColors?: AppCustomUiColors | null;
customUiColorsDark?: AppCustomUiColors | null;
cornerStyle?: AppCornerStyle | string | null;
}
export type AppAppearanceSettingsPatch = Partial<AppAppearanceSettings>;
type TauriInvoke = <T>(command: string, args?: Record<string, unknown>) => Promise<T>;
let hydrationPromise: Promise<void> | null = null;
let persistenceQueue: Promise<void> = Promise.resolve();
function warn(action: string, error: unknown) {
console.warn(`[DBX][appearance:${action}]`, error);
}
async function invokeTauri<T>(command: string, args?: Record<string, unknown>): Promise<T> {
const { invoke } = (await import("@tauri-apps/api/core")) as { invoke: TauriInvoke };
return invoke<T>(command, args);
}
function nonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.trim().length > 0;
}
function validThemeMode(value: unknown): value is AppThemeMode {
return value === "light" || value === "dark" || value === "system" || value === "soft-light" || value === "soft-dark" || value === "soft-system";
}
function validThemePalette(value: unknown): value is AppThemePalette {
return (
value === "pearl" ||
value === "mist" ||
value === "graphite" ||
value === "cobalt" ||
value === "sage" ||
value === "amber" ||
value === "blush" ||
value === "vscode" ||
value === "idea" ||
value === "xcode" ||
value === "jetbrains" ||
value === "cursor" ||
value === "claude" ||
value === "custom"
);
}
function validCornerStyle(value: unknown): value is AppCornerStyle {
return value === "none" || value === "small" || value === "large";
}
function parseColorObject(raw: string | null): AppCustomUiColors | undefined {
if (!raw) return undefined;
try {
const parsed = JSON.parse(raw);
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as AppCustomUiColors) : undefined;
} catch {
return undefined;
}
}
function localAppearance(): AppAppearanceSettings {
return {
locale: safeLocalStorageGet("dbx-locale"),
themeMode: safeLocalStorageGet(APP_THEME_STORAGE_KEY),
themePalette: safeLocalStorageGet(APP_THEME_PALETTE_STORAGE_KEY),
customUiColors: parseColorObject(safeLocalStorageGet(APP_CUSTOM_UI_STORAGE_KEY)),
customUiColorsDark: parseColorObject(safeLocalStorageGet(APP_CUSTOM_UI_DARK_STORAGE_KEY)),
cornerStyle: safeLocalStorageGet(APP_CORNER_STYLE_STORAGE_KEY),
};
}
function backendField<T>(settings: Record<string, unknown>, camelCase: string, snakeCase: string): T | undefined {
const value = settings[camelCase] ?? settings[snakeCase];
return value as T | undefined;
}
function setLocalAppearance(patch: AppAppearanceSettingsPatch) {
if (nonEmptyString(patch.locale)) safeLocalStorageSet("dbx-locale", patch.locale);
if (validThemeMode(patch.themeMode)) safeLocalStorageSet(APP_THEME_STORAGE_KEY, patch.themeMode);
if (validThemePalette(patch.themePalette)) safeLocalStorageSet(APP_THEME_PALETTE_STORAGE_KEY, patch.themePalette);
if (patch.customUiColors && typeof patch.customUiColors === "object") {
safeLocalStorageSet(APP_CUSTOM_UI_STORAGE_KEY, JSON.stringify(patch.customUiColors));
}
if (patch.customUiColorsDark && typeof patch.customUiColorsDark === "object") {
safeLocalStorageSet(APP_CUSTOM_UI_DARK_STORAGE_KEY, JSON.stringify(patch.customUiColorsDark));
}
if (validCornerStyle(patch.cornerStyle)) safeLocalStorageSet(APP_CORNER_STYLE_STORAGE_KEY, patch.cornerStyle);
}
function enqueuePersistence(operation: () => Promise<void>, action: string) {
persistenceQueue = persistenceQueue
.catch(() => {})
.then(operation)
.catch((error) => warn(action, error));
}
/**
* Hydrate the renderer's legacy storage keys from the durable Tauri app
* settings record before i18n and the theme composable are imported. Backend
* values win; existing localStorage values are migrated when a field is new.
*/
export async function hydrateAppAppearance(): Promise<void> {
if (!isTauriRuntime()) return;
if (!hydrationPromise) {
hydrationPromise = (async () => {
let raw: unknown;
try {
raw = await invokeTauri("load_app_appearance_settings");
} catch (error) {
warn("load", error);
return;
}
const backend = raw && typeof raw === "object" && !Array.isArray(raw) ? (raw as Record<string, unknown>) : {};
const local = localAppearance();
const migrate: AppAppearanceSettingsPatch = {};
const locale = backendField<string>(backend, "locale", "locale");
if (nonEmptyString(locale)) setLocalAppearance({ locale });
else if (nonEmptyString(local.locale)) migrate.locale = local.locale;
const themeMode = backendField<string>(backend, "themeMode", "theme_mode");
if (validThemeMode(themeMode)) setLocalAppearance({ themeMode });
else if (validThemeMode(local.themeMode)) migrate.themeMode = local.themeMode;
const themePalette = backendField<string>(backend, "themePalette", "theme_palette");
if (validThemePalette(themePalette)) setLocalAppearance({ themePalette });
else if (validThemePalette(local.themePalette)) migrate.themePalette = local.themePalette;
const customUiColors = backendField<AppCustomUiColors>(backend, "customUiColors", "custom_ui_colors");
if (customUiColors && typeof customUiColors === "object" && !Array.isArray(customUiColors)) setLocalAppearance({ customUiColors });
else if (local.customUiColors) migrate.customUiColors = local.customUiColors;
const customUiColorsDark = backendField<AppCustomUiColors>(backend, "customUiColorsDark", "custom_ui_colors_dark");
if (customUiColorsDark && typeof customUiColorsDark === "object" && !Array.isArray(customUiColorsDark)) setLocalAppearance({ customUiColorsDark });
else if (local.customUiColorsDark) migrate.customUiColorsDark = local.customUiColorsDark;
const cornerStyle = backendField<string>(backend, "cornerStyle", "corner_style");
if (validCornerStyle(cornerStyle)) setLocalAppearance({ cornerStyle });
else if (validCornerStyle(local.cornerStyle)) migrate.cornerStyle = local.cornerStyle;
if (Object.keys(migrate).length > 0) {
try {
await invokeTauri("update_app_appearance_settings", { patch: migrate });
} catch (error) {
warn("migrate", error);
}
}
})().finally(() => {
hydrationPromise = null;
});
}
await hydrationPromise;
}
export function persistAppAppearancePatch(patch: AppAppearanceSettingsPatch) {
setLocalAppearance(patch);
if (!isTauriRuntime()) return;
enqueuePersistence(() => invokeTauri("update_app_appearance_settings", { patch }), "save");
}
export function persistAppLocale(locale: string) {
setLocalAppearance({ locale });
if (!isTauriRuntime()) return;
enqueuePersistence(() => invokeTauri("set_app_locale", { locale }), "locale");
}