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,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