fix(theme): preserve wallpaper opacity and solid tooltip colors
Closes #10106
This commit is contained in:
1 parent
247eb2b0b0
commit
c059a78bb5
3 files changed
+498
-69
No files matched your search
@@ -1,7 +1,7 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { reactive } from "vue";
|
||||
import { BACKGROUND_IMAGE_ACTIVE_CLASS, useBackgroundImage } from "@/composables/useBackgroundImage";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { effectScope, reactive, type EffectScope } from "vue";
|
||||
import { BACKGROUND_IMAGE_ACTIVE_CLASS, useBackgroundImage as createBackgroundImage } from "@/composables/useBackgroundImage";
|
||||
import { useTheme } from "@/composables/useTheme";
|
||||
import { defaultBackgroundImageSettings, type BackgroundImageSettings } from "@/lib/app/appBackgroundImage";
|
||||
|
||||
@@ -13,6 +13,12 @@ vi.mock("@/lib/backend/api", () => ({
|
||||
|
||||
vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
|
||||
let scope: EffectScope;
|
||||
|
||||
function useBackgroundImage(settings: Parameters<typeof createBackgroundImage>[0]) {
|
||||
return scope.run(() => createBackgroundImage(settings))!;
|
||||
}
|
||||
|
||||
function settingsWith(overrides: Partial<BackgroundImageSettings>) {
|
||||
const base = defaultBackgroundImageSettings();
|
||||
return { editorSettings: reactive({ backgroundImage: { ...base, ...overrides } }) } as Parameters<typeof useBackgroundImage>[0];
|
||||
@@ -20,6 +26,7 @@ function settingsWith(overrides: Partial<BackgroundImageSettings>) {
|
||||
|
||||
describe("useBackgroundImage", () => {
|
||||
beforeEach(() => {
|
||||
scope = effectScope();
|
||||
vi.resetModules();
|
||||
readBackgroundImageMock.mockReset();
|
||||
(globalThis as Record<string, unknown>).__TAURI_INTERNALS__ = {};
|
||||
@@ -27,6 +34,8 @@ describe("useBackgroundImage", () => {
|
||||
document.documentElement.removeAttribute("style");
|
||||
});
|
||||
|
||||
afterEach(() => scope.stop());
|
||||
|
||||
it("reports no active background without a configured file", () => {
|
||||
const bg = useBackgroundImage(settingsWith({}));
|
||||
expect(bg.backgroundObjectUrl.value).toBeNull();
|
||||
|
||||
@@ -0,0 +1,432 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { effectScope, nextTick, reactive, type EffectScope } from "vue";
|
||||
import { BACKGROUND_IMAGE_SURFACE_VARS, defaultBackgroundImageSettings, type BackgroundImageSettings } from "@/lib/app/appBackgroundImage";
|
||||
import type { BackgroundImageComposable } from "@/composables/useBackgroundImage";
|
||||
|
||||
const readBackgroundImageMock = vi.hoisted(() => vi.fn());
|
||||
vi.mock("@/lib/backend/api", () => ({ readBackgroundImage: readBackgroundImageMock }));
|
||||
vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.mock("@/lib/app/appAppearance", () => ({ persistAppAppearancePatch: vi.fn() }));
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
let reject!: (error: Error) => void;
|
||||
const promise = new Promise<T>((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
async function settle() {
|
||||
await Promise.resolve();
|
||||
await nextTick();
|
||||
await Promise.resolve();
|
||||
await nextTick();
|
||||
}
|
||||
|
||||
describe("background image theme and scope lifecycle", () => {
|
||||
let useBackgroundImage: typeof import("@/composables/useBackgroundImage").useBackgroundImage;
|
||||
let theme: ReturnType<typeof import("@/composables/useTheme").useTheme>;
|
||||
let scope: EffectScope;
|
||||
let style: HTMLStyleElement;
|
||||
let systemThemeChanged: (event: { matches: boolean }) => void;
|
||||
const createObjectURL = vi.fn();
|
||||
const revokeObjectURL = vi.fn();
|
||||
|
||||
function mount(overrides: Partial<BackgroundImageSettings> = {}) {
|
||||
const settings = { editorSettings: reactive({ backgroundImage: { ...defaultBackgroundImageSettings(), ...overrides } }) } as Parameters<typeof useBackgroundImage>[0];
|
||||
scope = effectScope();
|
||||
const bg = scope.run(() => useBackgroundImage(settings))!;
|
||||
return { bg, settings };
|
||||
}
|
||||
|
||||
function token(name: string) {
|
||||
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
||||
}
|
||||
|
||||
function expectInactive(bg: BackgroundImageComposable) {
|
||||
expect(bg.active.value).toBe(false);
|
||||
expect(bg.backgroundObjectUrl.value).toBeNull();
|
||||
expect(bg.backgroundImageStyle.value).toEqual({});
|
||||
expect(document.documentElement.classList.contains("dbx-bg-active")).toBe(false);
|
||||
expect(document.documentElement.style.getPropertyValue("--background-solid")).toBe("");
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
readBackgroundImageMock.mockReset().mockResolvedValue("aGVsbG8=");
|
||||
createObjectURL.mockReset().mockImplementation(() => `blob:bg-${createObjectURL.mock.calls.length}`);
|
||||
revokeObjectURL.mockReset();
|
||||
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
|
||||
callback(0);
|
||||
return 0;
|
||||
});
|
||||
vi.spyOn(URL, "createObjectURL").mockImplementation(createObjectURL);
|
||||
vi.spyOn(URL, "revokeObjectURL").mockImplementation(revokeObjectURL);
|
||||
vi.spyOn(window, "matchMedia").mockReturnValue({
|
||||
matches: false,
|
||||
addEventListener: (_event: string, listener: typeof systemThemeChanged) => {
|
||||
systemThemeChanged = listener;
|
||||
},
|
||||
} as MediaQueryList);
|
||||
localStorage.clear();
|
||||
document.documentElement.className = "";
|
||||
document.documentElement.removeAttribute("style");
|
||||
style = document.createElement("style");
|
||||
style.textContent = `
|
||||
:root { --background: rgb(255 255 255); --sidebar: rgb(248 248 248); --dbx-content: var(--background); }
|
||||
:root.dark { --background: rgb(10 10 10); --sidebar: rgb(24 24 24); }
|
||||
:root.theme-cobalt { --background: rgb(240 246 255); }
|
||||
`;
|
||||
document.head.append(style);
|
||||
theme = (await import("@/composables/useTheme")).useTheme();
|
||||
theme.setThemeMode("light");
|
||||
theme.setThemePalette("pearl");
|
||||
({ useBackgroundImage } = await import("@/composables/useBackgroundImage"));
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
scope?.stop();
|
||||
await settle();
|
||||
style.remove();
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("leaves default surfaces opaque without reading an unconfigured file", async () => {
|
||||
const { bg } = mount();
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(token("--background")).toBe("rgb(255 255 255)");
|
||||
expect(readBackgroundImageMock).not.toHaveBeenCalled();
|
||||
expect(createObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["light", 0.05, "255 255 255"],
|
||||
["light", 0.8, "255 255 255"],
|
||||
["dark", 0.05, "10 10 10"],
|
||||
["dark", 0.8, "10 10 10"],
|
||||
] as const)("keeps built-in %s text opaque at surface opacity %s", async (mode, opacity, rgb) => {
|
||||
theme.setThemeMode(mode);
|
||||
const { bg } = mount({ filePath: "/wall.png", opacity });
|
||||
await settle();
|
||||
expect(bg.active.value).toBe(true);
|
||||
expect(token("--background")).toBe(`rgb(${rgb} / ${opacity})`);
|
||||
expect(token("--background-solid")).toBe(`rgb(${rgb})`);
|
||||
expect(token("--dbx-content")).toBe(`rgb(${rgb} / ${opacity})`);
|
||||
expect(token("--sidebar-solid")).toBe("");
|
||||
});
|
||||
|
||||
it.each(["same mode", "equivalent system", "same palette", "same corners", "apply theme"])("restores wallpaper opacity after %s", async (action) => {
|
||||
const { bg } = mount({ filePath: "/wall.png", opacity: 0.05 });
|
||||
await settle();
|
||||
const revision = theme.themeRevision.value;
|
||||
if (action === "same mode") theme.setThemeMode("light");
|
||||
if (action === "equivalent system") theme.setThemeMode("system");
|
||||
if (action === "same palette") theme.setThemePalette("pearl");
|
||||
if (action === "same corners") theme.setCornerStyle(theme.cornerStyle.value);
|
||||
if (action === "apply theme") theme.applyTheme();
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(255 255 255 / 0.05)");
|
||||
expect(token("--background-solid")).toBe("rgb(255 255 255)");
|
||||
expect(theme.themeRevision.value).toBe(revision + 1);
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
expect(readBackgroundImageMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("follows dark/light and system appearance changes without reloading the image", async () => {
|
||||
const { bg } = mount({ filePath: "/wall.png", opacity: 0.05 });
|
||||
await settle();
|
||||
for (const mode of ["dark", "light", "system"] as const) {
|
||||
theme.setThemeMode(mode);
|
||||
await settle();
|
||||
const rgb = mode === "dark" ? "10 10 10" : "255 255 255";
|
||||
expect(token("--background")).toBe(`rgb(${rgb} / 0.05)`);
|
||||
expect(token("--background-solid")).toBe(`rgb(${rgb})`);
|
||||
}
|
||||
systemThemeChanged({ matches: true });
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(10 10 10 / 0.05)");
|
||||
expect(token("--background-solid")).toBe("rgb(10 10 10)");
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
expect(createObjectURL).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("restores previews on cancellation and reapplies a committed preview", async () => {
|
||||
mount({ filePath: "/wall.png", opacity: 0.05 });
|
||||
await settle();
|
||||
theme.previewThemePalette("cobalt");
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(240 246 255 / 0.05)");
|
||||
expect(token("--background-solid")).toBe("rgb(240 246 255)");
|
||||
theme.clearThemePalettePreview();
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(255 255 255 / 0.05)");
|
||||
theme.previewThemePalette("cobalt");
|
||||
await settle();
|
||||
theme.setThemePalette("cobalt");
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(240 246 255 / 0.05)");
|
||||
expect(token("--background-solid")).toBe("rgb(240 246 255)");
|
||||
});
|
||||
|
||||
it("preserves custom colors through edits, preview cancellation and removal", async () => {
|
||||
theme.setCustomUiColors({ ...theme.activeCustomUiColors.value, background: "#123456", sidebar: "#654321" });
|
||||
theme.setThemePalette("custom");
|
||||
const { bg, settings } = mount({ filePath: "/wall.png", opacity: 0.8 });
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(18 52 86 / 0.8)");
|
||||
expect(token("--background-solid")).toBe("rgb(18 52 86)");
|
||||
expect(token("--sidebar")).toBe("rgb(101 67 33 / 0.8)");
|
||||
theme.previewThemePalette("pearl");
|
||||
await settle();
|
||||
theme.clearThemePalettePreview();
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(18 52 86 / 0.8)");
|
||||
theme.setThemeMode("dark");
|
||||
theme.setCustomUiColors({ ...theme.activeCustomUiColors.value, background: "#abcdef" });
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(171 205 239 / 0.8)");
|
||||
expect(token("--background-solid")).toBe("rgb(171 205 239)");
|
||||
theme.setThemeMode("light");
|
||||
await settle();
|
||||
settings.editorSettings.backgroundImage = defaultBackgroundImageSettings();
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(token("--background")).toBe("rgb(18 52 86)");
|
||||
expect(token("--sidebar")).toBe("rgb(101 67 33)");
|
||||
expect(revokeObjectURL).toHaveBeenCalledWith("blob:bg-1");
|
||||
});
|
||||
|
||||
it("restores wallpaper after a palette preview is cancelled in the same tick", async () => {
|
||||
mount({ filePath: "/wall.png", opacity: 0.05 });
|
||||
await settle();
|
||||
theme.previewThemePalette("cobalt");
|
||||
theme.clearThemePalettePreview();
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(255 255 255 / 0.05)");
|
||||
expect(token("--background-solid")).toBe("rgb(255 255 255)");
|
||||
});
|
||||
|
||||
it("restores defaults when all appearance and wallpaper settings reset together", async () => {
|
||||
theme.setThemePalette("custom");
|
||||
const { bg, settings } = mount({ filePath: "/wall.png", opacity: 0.05 });
|
||||
await settle();
|
||||
settings.editorSettings.backgroundImage = defaultBackgroundImageSettings();
|
||||
theme.resetCustomUiColors();
|
||||
theme.setThemePalette("pearl");
|
||||
theme.setThemeMode("system");
|
||||
theme.setCornerStyle("large");
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
for (const name of BACKGROUND_IMAGE_SURFACE_VARS) expect(document.documentElement.style.getPropertyValue(name)).toBe("");
|
||||
expect(token("--background")).toBe("rgb(255 255 255)");
|
||||
});
|
||||
|
||||
it("updates opacity and layout without reloading or tinting text", async () => {
|
||||
const { bg, settings } = mount({ filePath: "/wall.png" });
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(255 255 255 / 0.8)");
|
||||
settings.editorSettings.backgroundImage.opacity = 1;
|
||||
settings.editorSettings.backgroundImage.displayMode = "tile";
|
||||
settings.editorSettings.backgroundImage.blur = 4;
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(255 255 255 / 1)");
|
||||
expect(token("--background-solid")).toBe("rgb(255 255 255)");
|
||||
expect(bg.backgroundImageStyle.value).toMatchObject({ backgroundRepeat: "repeat", filter: "blur(4px)" });
|
||||
expect(readBackgroundImageMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it.each(["pearl", "custom"] as const)("keeps %s surfaces opaque when reading fails", async (palette) => {
|
||||
theme.setThemePalette(palette);
|
||||
const original = token("--background");
|
||||
readBackgroundImageMock.mockRejectedValue(new Error("gone"));
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const { bg } = mount({ filePath: "/missing.png" });
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(token("--background")).toBe(original);
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
expect(createObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("revokes a replaced URL and falls back after a later read failure", async () => {
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const { bg, settings } = mount({ filePath: "/first.png" });
|
||||
await settle();
|
||||
settings.editorSettings.backgroundImage.filePath = "/second.png";
|
||||
await settle();
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-2");
|
||||
expect(revokeObjectURL.mock.calls).toEqual([["blob:bg-1"]]);
|
||||
readBackgroundImageMock.mockRejectedValue(new Error("gone"));
|
||||
settings.editorSettings.backgroundImage.filePath = "/missing.png";
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(revokeObjectURL.mock.calls).toEqual([["blob:bg-1"], ["blob:bg-2"]]);
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("ignores an older read after a newer image loads", async () => {
|
||||
const first = deferred<string>();
|
||||
readBackgroundImageMock.mockReturnValueOnce(first.promise);
|
||||
const { bg, settings } = mount({ filePath: "/first.png" });
|
||||
settings.editorSettings.backgroundImage.filePath = "/second.png";
|
||||
await settle();
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
first.resolve("b2xk");
|
||||
await settle();
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
expect(createObjectURL).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("keeps the newer image when an older read rejects", async () => {
|
||||
const first = deferred<string>();
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
readBackgroundImageMock.mockReturnValueOnce(first.promise);
|
||||
const { bg, settings } = mount({ filePath: "/first.png" });
|
||||
settings.editorSettings.backgroundImage.filePath = "/second.png";
|
||||
await settle();
|
||||
first.reject(new Error("stale"));
|
||||
await settle();
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
expect(bg.active.value).toBe(true);
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
expect(revokeObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("cancels a same-tick path round trip and uses the final theme when loading finishes", async () => {
|
||||
const first = deferred<string>();
|
||||
const last = deferred<string>();
|
||||
readBackgroundImageMock.mockReturnValueOnce(first.promise).mockReturnValueOnce(last.promise);
|
||||
const { bg, settings } = mount({ filePath: "/wall.png" });
|
||||
settings.editorSettings.backgroundImage.filePath = null;
|
||||
settings.editorSettings.backgroundImage.filePath = "/wall.png";
|
||||
theme.setThemeMode("dark");
|
||||
theme.setThemeMode("dark");
|
||||
first.resolve("b2xk");
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
last.resolve("aGVsbG8=");
|
||||
await settle();
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
expect(token("--background")).toBe("rgb(10 10 10 / 0.8)");
|
||||
expect(token("--background-solid")).toBe("rgb(10 10 10)");
|
||||
expect(createObjectURL).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not reload an unchanged path when settings are replaced", async () => {
|
||||
const { bg, settings } = mount({ filePath: "/wall.png" });
|
||||
await settle();
|
||||
settings.editorSettings.backgroundImage = { ...settings.editorSettings.backgroundImage, opacity: 0.05 };
|
||||
theme.writeRootToken("--font-sans", "monospace");
|
||||
const revision = theme.themeRevision.value;
|
||||
await settle();
|
||||
expect(token("--background")).toBe("rgb(255 255 255 / 0.05)");
|
||||
expect(token("--background-solid")).toBe("rgb(255 255 255)");
|
||||
expect(theme.themeRevision.value).toBe(revision);
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
expect(readBackgroundImageMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("falls back to opaque surfaces when image data is malformed", async () => {
|
||||
readBackgroundImageMock.mockResolvedValue("not valid base64!");
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const { bg } = mount({ filePath: "/broken.png" });
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(token("--background")).toBe("rgb(255 255 255)");
|
||||
expect(createObjectURL).not.toHaveBeenCalled();
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it.each(["resolve", "reject"] as const)("ignores a read that settles with %s after removal", async (result) => {
|
||||
const pending = deferred<string>();
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
readBackgroundImageMock.mockReturnValueOnce(pending.promise);
|
||||
const { bg, settings } = mount({ filePath: "/wall.png" });
|
||||
settings.editorSettings.backgroundImage.filePath = null;
|
||||
await settle();
|
||||
if (result === "resolve") pending.resolve("aGVsbG8=");
|
||||
else pending.reject(new Error("cancelled"));
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(createObjectURL).not.toHaveBeenCalled();
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reloads the previous path after cancelling an intervening read", async () => {
|
||||
const pending = deferred<string>();
|
||||
const { bg, settings } = mount({ filePath: "/first.png" });
|
||||
await settle();
|
||||
readBackgroundImageMock.mockReturnValueOnce(pending.promise);
|
||||
settings.editorSettings.backgroundImage.filePath = "/second.png";
|
||||
await settle();
|
||||
settings.editorSettings.backgroundImage.filePath = "/first.png";
|
||||
await settle();
|
||||
pending.resolve("b2xk");
|
||||
await settle();
|
||||
expect(readBackgroundImageMock.mock.calls).toEqual([["/first.png"], ["/second.png"], ["/first.png"]]);
|
||||
expect(bg.backgroundObjectUrl.value).toBe("blob:bg-2");
|
||||
expect(createObjectURL).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("does not resurrect a URL after removal and rapid mode changes in one tick", async () => {
|
||||
const pending = deferred<string>();
|
||||
readBackgroundImageMock.mockReturnValueOnce(pending.promise);
|
||||
const { bg, settings } = mount({ filePath: "/wall.png" });
|
||||
settings.editorSettings.backgroundImage.filePath = null;
|
||||
pending.resolve("aGVsbG8=");
|
||||
theme.setThemeMode("dark");
|
||||
theme.setThemeMode("light");
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(createObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each(["pearl", "custom"] as const)("revokes the URL and restores %s surfaces on disposal", async (palette) => {
|
||||
theme.setThemePalette(palette);
|
||||
const original = token("--background");
|
||||
const { bg, settings } = mount({ filePath: "/wall.png" });
|
||||
await settle();
|
||||
scope.stop();
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(token("--background")).toBe(original);
|
||||
expect(revokeObjectURL.mock.calls).toEqual([["blob:bg-1"]]);
|
||||
settings.editorSettings.backgroundImage.filePath = "/ignored.png";
|
||||
await settle();
|
||||
expect(readBackgroundImageMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("ignores a pending load after its scope is disposed", async () => {
|
||||
const pending = deferred<string>();
|
||||
readBackgroundImageMock.mockReturnValueOnce(pending.promise);
|
||||
const { bg } = mount({ filePath: "/wall.png" });
|
||||
await settle();
|
||||
scope.stop();
|
||||
pending.resolve("aGVsbG8=");
|
||||
await settle();
|
||||
expectInactive(bg);
|
||||
expect(createObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps a remounted wallpaper independent of the disposed loader", async () => {
|
||||
const pending = deferred<string>();
|
||||
readBackgroundImageMock.mockReturnValueOnce(pending.promise);
|
||||
const previous = mount({ filePath: "/old.png" });
|
||||
await settle();
|
||||
scope.stop();
|
||||
const current = mount({ filePath: "/new.png" });
|
||||
await settle();
|
||||
pending.resolve("b2xk");
|
||||
await settle();
|
||||
expect(previous.bg.backgroundObjectUrl.value).toBeNull();
|
||||
expect(current.bg.backgroundObjectUrl.value).toBe("blob:bg-1");
|
||||
expect(token("--background")).toBe("rgb(255 255 255 / 0.8)");
|
||||
expect(createObjectURL).toHaveBeenCalledTimes(1);
|
||||
expect(revokeObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { computed, ref, watch, watchPostEffect, type ComputedRef, type Ref } from "vue";
|
||||
import { computed, onScopeDispose, ref, watch, watchPostEffect, type ComputedRef, type Ref } from "vue";
|
||||
import { BACKGROUND_IMAGE_INVERTED_TEXT_VARS, BACKGROUND_IMAGE_SURFACE_VARS, backgroundImageSolidVarName, backgroundImageStyle, backgroundImageSurfaceAlpha, surfaceColorWithAlpha, type BackgroundImageSettings } from "@/lib/app/appBackgroundImage";
|
||||
import { APP_CUSTOM_UI_COLOR_DEFS, appCustomUiColorValue, deriveCustomUiColors } from "@/lib/app/appTheme";
|
||||
import { readBackgroundImage } from "@/lib/backend/api";
|
||||
@@ -11,11 +11,6 @@ export const BACKGROUND_IMAGE_ACTIVE_CLASS = "dbx-bg-active";
|
||||
|
||||
const SURFACE_VAR_NAMES = new Set<string>(BACKGROUND_IMAGE_SURFACE_VARS);
|
||||
|
||||
const backgroundObjectUrl = ref<string | null>(null);
|
||||
let loadedFilePath: string | null = null;
|
||||
let loadEpoch = 0;
|
||||
let warnedOnce = false;
|
||||
|
||||
function base64ToBlob(base64: string): Blob {
|
||||
const binary = atob(base64);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
@@ -25,37 +20,6 @@ function base64ToBlob(base64: string): Blob {
|
||||
return new Blob([bytes]);
|
||||
}
|
||||
|
||||
async function loadBackgroundObjectUrl(settingsStore: SettingsStoreLike) {
|
||||
const filePath = settingsStore.editorSettings.backgroundImage.filePath;
|
||||
if (!filePath) {
|
||||
loadedFilePath = filePath;
|
||||
setAndRevokeObjectUrl(null);
|
||||
return;
|
||||
}
|
||||
const epoch = ++loadEpoch;
|
||||
try {
|
||||
const base64 = await readBackgroundImage(filePath);
|
||||
if (epoch !== loadEpoch) return;
|
||||
loadedFilePath = filePath;
|
||||
setAndRevokeObjectUrl(URL.createObjectURL(base64ToBlob(base64)));
|
||||
} catch (error) {
|
||||
if (epoch !== loadEpoch) return;
|
||||
loadedFilePath = filePath;
|
||||
setAndRevokeObjectUrl(null);
|
||||
if (!warnedOnce) {
|
||||
warnedOnce = true;
|
||||
console.warn("[dbx] failed to load background image; falling back to plain surfaces", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setAndRevokeObjectUrl(next: string | null) {
|
||||
if (backgroundObjectUrl.value && backgroundObjectUrl.value !== next) {
|
||||
URL.revokeObjectURL(backgroundObjectUrl.value);
|
||||
}
|
||||
backgroundObjectUrl.value = next;
|
||||
}
|
||||
|
||||
export interface BackgroundImageComposable {
|
||||
backgroundObjectUrl: Ref<string | null>;
|
||||
backgroundSettings: ComputedRef<BackgroundImageSettings>;
|
||||
@@ -64,29 +28,45 @@ export interface BackgroundImageComposable {
|
||||
}
|
||||
|
||||
export function useBackgroundImage(settingsStore: SettingsStoreLike): BackgroundImageComposable {
|
||||
const backgroundObjectUrl = ref<string | null>(null);
|
||||
let warnedOnce = false;
|
||||
const backgroundSettings = computed(() => settingsStore.editorSettings.backgroundImage);
|
||||
const { isDark, themePalette, activeCustomUiColors, cornerStyle } = useTheme();
|
||||
const { isDark, themePalette, activeCustomUiColors, cornerStyle, themeRevision } = useTheme();
|
||||
|
||||
function setAndRevokeObjectUrl(next: string | null) {
|
||||
if (backgroundObjectUrl.value && backgroundObjectUrl.value !== next) {
|
||||
URL.revokeObjectURL(backgroundObjectUrl.value);
|
||||
}
|
||||
backgroundObjectUrl.value = next;
|
||||
}
|
||||
|
||||
void loadBackgroundObjectUrl(settingsStore);
|
||||
watch(
|
||||
() => backgroundSettings.value.filePath,
|
||||
(filePath) => {
|
||||
if (filePath === loadedFilePath) return;
|
||||
void loadBackgroundObjectUrl(settingsStore);
|
||||
async (filePath, _previous, onCleanup) => {
|
||||
let cancelled = false;
|
||||
onCleanup(() => {
|
||||
cancelled = true;
|
||||
setAndRevokeObjectUrl(null);
|
||||
});
|
||||
if (!filePath) return;
|
||||
try {
|
||||
const base64 = await readBackgroundImage(filePath);
|
||||
if (cancelled) return;
|
||||
setAndRevokeObjectUrl(URL.createObjectURL(base64ToBlob(base64)));
|
||||
} catch (error) {
|
||||
if (cancelled) return;
|
||||
setAndRevokeObjectUrl(null);
|
||||
if (!warnedOnce) {
|
||||
warnedOnce = true;
|
||||
console.warn("[dbx] failed to load background image; falling back to plain surfaces", error);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ immediate: true, flush: "sync" },
|
||||
);
|
||||
|
||||
const active = computed(() => Boolean(backgroundObjectUrl.value));
|
||||
|
||||
// Keeps the palette surface colors in sync while the wallpaper is active:
|
||||
// clear the inline overrides first so the computed values below come from
|
||||
// the freshly applied palette classes, then re-emit them with alpha. The
|
||||
// theme refs read below are the reactive deps: any palette / mode / custom
|
||||
// color change re-runs this effect right after applyTheme rewrote the
|
||||
// class-based (and custom-palette inline) variables. Custom palettes own
|
||||
// every surface variable as an inline style, so clearing them would fall
|
||||
// back to the palette class colors; their base values are resolved from the
|
||||
// custom color settings instead of the computed style.
|
||||
const customSurfaceBaseColors = (): Map<string, string> => {
|
||||
const base = new Map<string, string>();
|
||||
if (themePalette.value !== "custom") return base;
|
||||
@@ -102,37 +82,45 @@ export function useBackgroundImage(settingsStore: SettingsStoreLike): Background
|
||||
return base;
|
||||
};
|
||||
|
||||
const resetSurfaceColors = () => {
|
||||
const doc = document.documentElement;
|
||||
const customBase = customSurfaceBaseColors();
|
||||
for (const varName of [...BACKGROUND_IMAGE_SURFACE_VARS, ...BACKGROUND_IMAGE_INVERTED_TEXT_VARS.map(backgroundImageSolidVarName)]) {
|
||||
doc.style.removeProperty(varName);
|
||||
}
|
||||
for (const [varName, color] of customBase) {
|
||||
doc.style.setProperty(varName, color);
|
||||
}
|
||||
doc.classList.remove(BACKGROUND_IMAGE_ACTIVE_CLASS);
|
||||
return customBase;
|
||||
};
|
||||
|
||||
onScopeDispose(() => {
|
||||
if (typeof document !== "undefined") resetSurfaceColors();
|
||||
});
|
||||
|
||||
watchPostEffect(() => {
|
||||
if (typeof document === "undefined") return;
|
||||
void isDark.value;
|
||||
void themePalette.value;
|
||||
void activeCustomUiColors.value;
|
||||
void cornerStyle.value;
|
||||
void themeRevision.value;
|
||||
const doc = document.documentElement;
|
||||
const alpha = backgroundImageSurfaceAlpha(backgroundSettings.value);
|
||||
const isActive = active.value;
|
||||
const customBase = customSurfaceBaseColors();
|
||||
for (const varName of [...BACKGROUND_IMAGE_SURFACE_VARS, ...BACKGROUND_IMAGE_INVERTED_TEXT_VARS.map(backgroundImageSolidVarName)]) {
|
||||
doc.style.removeProperty(varName);
|
||||
}
|
||||
const customBase = resetSurfaceColors();
|
||||
doc.classList.toggle(BACKGROUND_IMAGE_ACTIVE_CLASS, isActive);
|
||||
if (!isActive) {
|
||||
// Re-emit the custom palette values the clear above removed.
|
||||
for (const [varName, color] of customBase) {
|
||||
doc.style.setProperty(varName, color);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!isActive) return;
|
||||
const computedStyle = getComputedStyle(doc);
|
||||
const baseColors = new Map([...BACKGROUND_IMAGE_SURFACE_VARS, ...BACKGROUND_IMAGE_INVERTED_TEXT_VARS].map((varName) => [varName, (customBase.get(varName) ?? computedStyle.getPropertyValue(varName)).trim()]));
|
||||
for (const varName of BACKGROUND_IMAGE_SURFACE_VARS) {
|
||||
const base = (customBase.get(varName) ?? computedStyle.getPropertyValue(varName)).trim();
|
||||
const base = baseColors.get(varName)!;
|
||||
const tinted = surfaceColorWithAlpha(base, alpha);
|
||||
if (tinted) doc.style.setProperty(varName, tinted);
|
||||
}
|
||||
// Inverted-text surfaces (`bg-foreground text-background`) keep the opaque
|
||||
// palette color: their glyphs must not inherit the wallpaper alpha.
|
||||
for (const varName of BACKGROUND_IMAGE_INVERTED_TEXT_VARS) {
|
||||
const base = (customBase.get(varName) ?? computedStyle.getPropertyValue(varName)).trim();
|
||||
const base = baseColors.get(varName)!;
|
||||
if (base) doc.style.setProperty(backgroundImageSolidVarName(varName), base);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user