1 parent
d9b6d44d9d
commit
13c3f8205c
9 files changed
+416
-20
No files matched your search
@@ -27,6 +27,7 @@ import {
|
||||
} from "@/lib/app/appTheme";
|
||||
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage";
|
||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
import { persistAppAppearancePatch } from "@/lib/app/appAppearance";
|
||||
|
||||
function isLinuxTauriRuntime() {
|
||||
return isTauriRuntime() && typeof navigator !== "undefined" && /linux/i.test(navigator.userAgent);
|
||||
@@ -169,6 +170,7 @@ function applyTheme() {
|
||||
function setThemeMode(mode: AppThemeMode) {
|
||||
themeMode.value = mode;
|
||||
safeLocalStorageSet(APP_THEME_STORAGE_KEY, mode);
|
||||
persistAppAppearancePatch({ themeMode: mode });
|
||||
applyTheme();
|
||||
}
|
||||
|
||||
@@ -176,6 +178,7 @@ function setThemePalette(palette: AppThemePalette) {
|
||||
savedThemePaletteValue.value = palette;
|
||||
previewedThemePalette.value = null;
|
||||
safeLocalStorageSet(APP_THEME_PALETTE_STORAGE_KEY, palette);
|
||||
persistAppAppearancePatch({ themePalette: palette });
|
||||
applyTheme();
|
||||
}
|
||||
|
||||
@@ -225,6 +228,7 @@ function setCustomUiColors(colors: AppCustomUiColors) {
|
||||
customUiColors.value = next;
|
||||
safeLocalStorageSet(APP_CUSTOM_UI_STORAGE_KEY, JSON.stringify(next));
|
||||
}
|
||||
persistAppAppearancePatch({ [isDark.value ? "customUiColorsDark" : "customUiColors"]: next });
|
||||
applyCustomUiColors();
|
||||
}
|
||||
|
||||
@@ -235,6 +239,7 @@ function resetCustomUiColors() {
|
||||
function setCornerStyle(style: AppCornerStyle) {
|
||||
cornerStyle.value = normalizeAppCornerStyle(style);
|
||||
safeLocalStorageSet(APP_CORNER_STYLE_STORAGE_KEY, cornerStyle.value);
|
||||
persistAppAppearancePatch({ cornerStyle: cornerStyle.value });
|
||||
applyTheme();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createI18n } from "vue-i18n";
|
||||
import en from "./locales/en";
|
||||
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage";
|
||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
import { safeLocalStorageGet } from "@/lib/backend/safeStorage";
|
||||
import { persistAppLocale } from "@/lib/app/appAppearance";
|
||||
|
||||
export type Locale = "az" | "en" | "es" | "it" | "ja" | "ko" | "pt-BR" | "ru" | "tr" | "zh-CN" | "zh-TW";
|
||||
type LocaleMessages = Record<string, unknown>;
|
||||
@@ -98,19 +98,9 @@ export async function loadLocaleMessages(locale: Locale) {
|
||||
loadedLocales.add(locale);
|
||||
}
|
||||
|
||||
async function syncLocaleToBackend(locale: Locale) {
|
||||
if (!isTauriRuntime()) return;
|
||||
try {
|
||||
const { invoke } = await import("@tauri-apps/api/core");
|
||||
await invoke("set_app_locale", { locale });
|
||||
} catch (error) {
|
||||
console.warn("[DBX][i18n] failed to sync locale to backend", error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadSavedLocale() {
|
||||
await loadLocaleMessages(initialLocale);
|
||||
void syncLocaleToBackend(initialLocale);
|
||||
persistAppLocale(initialLocale);
|
||||
}
|
||||
|
||||
async function applyTransientLocale(locale: Locale) {
|
||||
@@ -138,8 +128,7 @@ export async function setLocale(locale: Locale) {
|
||||
++localeRequestId;
|
||||
// An explicit selection is durable immediately; only the visible locale
|
||||
// waits for its lazy message bundle. Preview paths never reach this branch.
|
||||
safeLocalStorageSet("dbx-locale", locale);
|
||||
void syncLocaleToBackend(locale);
|
||||
persistAppLocale(locale);
|
||||
await loadLocaleMessages(locale);
|
||||
// A later explicit selection wins; hover/restore requests must not undo it.
|
||||
if (persistedLocale !== locale) return;
|
||||
|
||||
@@ -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" });
|
||||
});
|
||||
});
|
||||
@@ -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");
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { installDebugLogCapture } from "@/lib/backend/debugLog";
|
||||
import { retryStartupAfterPreloadFailure } from "@/lib/startup/startupPreloadRecovery";
|
||||
import { markStartupPhase } from "@/lib/startup/startupTiming";
|
||||
import { applyLegacyWebViewClass, isBlockingCompatFailure } from "@/lib/ui/legacyWebView";
|
||||
import { hydrateAppAppearance } from "@/lib/app/appAppearance";
|
||||
|
||||
function startupErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
@@ -87,6 +88,10 @@ async function bootstrap() {
|
||||
window.dispatchEvent(new Event("dbx:startup-ready"));
|
||||
return;
|
||||
}
|
||||
// Tauri WebViews may not retain localStorage across macOS restarts. Load the
|
||||
// durable appearance record before importing i18n and the theme composable;
|
||||
// those modules synchronously read the compatibility keys during evaluation.
|
||||
await hydrateAppAppearance();
|
||||
const [{ default: i18n, loadSavedLocale }, { default: App }] = await Promise.all([import("./i18n"), import("./StartupGate.vue")]);
|
||||
console.log("[STARTUP] frontend modules loaded");
|
||||
const localeReady = loadSavedLocale();
|
||||
|
||||
Reference in new issue
Block a user