Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions apps/web/src/components/settings/ThemeEditorPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,23 @@ export function ThemeEditorPanel({
// comes back when the editor closes, including on cancel.
useEffect(() => {
if (!open || !isDraftSeeded) return;
applyThemeColorPreview(colorsByAppearance[activeAppearance], activeAppearance, sidebarArtwork);
}, [activeAppearance, colorsByAppearance, isDraftSeeded, open, sidebarArtwork]);
applyThemeColorPreview(
colorsByAppearance[activeAppearance],
activeAppearance,
sidebarArtwork,
// Match what saving will keep: merges retain the target's wallpaper,
// edits the edited theme's, and new drafts none.
mergeTarget ? mergeTarget.wallpaper : editingTheme?.wallpaper,
);
}, [
activeAppearance,
colorsByAppearance,
editingTheme,
isDraftSeeded,
mergeTarget,
open,
sidebarArtwork,
]);

useEffect(() => {
if (!open) return;
Expand Down Expand Up @@ -674,6 +689,7 @@ export function ThemeEditorPanel({
...mergeTarget.variants,
...Object.fromEntries(editedModes.map((mode) => [mode, colorsForSave[mode]])),
},
...(mergeTarget.wallpaper ? { wallpaper: mergeTarget.wallpaper } : {}),
...(sidebarArtwork ? { sidebarArtwork: true } : {}),
...(mergeTarget.managed === true && !isAdvanced ? { managed: true } : {}),
}),
Expand Down Expand Up @@ -705,6 +721,9 @@ export function ThemeEditorPanel({
...(getThemeModes(editingTheme).length > 1
? { variants: { [variantAppearance]: colorsForSave[variantAppearance] } }
: {}),
// The editor has no wallpaper controls; an imported wallpaper
// survives edits untouched.
...(editingTheme.wallpaper ? { wallpaper: editingTheme.wallpaper } : {}),
...(sidebarArtwork ? { sidebarArtwork: true } : {}),
...(isAdvanced ? {} : { managed: true }),
}),
Expand Down Expand Up @@ -732,6 +751,7 @@ export function ThemeEditorPanel({
...mergeTarget.variants,
[activeAppearance]: colorsForSave[activeAppearance],
},
...(mergeTarget.wallpaper ? { wallpaper: mergeTarget.wallpaper } : {}),
...(sidebarArtwork ? { sidebarArtwork: true } : {}),
...(mergeTarget.managed === true && !isAdvanced ? { managed: true } : {}),
}),
Expand Down
31 changes: 31 additions & 0 deletions apps/web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,37 @@ html[data-theme-id] body {
color: var(--foreground);
}

/* Theme wallpaper (ThemeWallpaper in themePalette.ts): the image rides the
body's chrome paint as a background-image stack. A scrim of the chrome color
layered on top ghosts the image down to the theme's chosen opacity, so no
extra element or stacking-context games are needed. */
html[data-wallpaper] body {
--theme-wallpaper-scrim: color-mix(
in srgb,
var(--app-chrome-background) calc(100% - var(--theme-wallpaper-opacity) * 100%),
transparent
);

background-image:
linear-gradient(var(--theme-wallpaper-scrim), var(--theme-wallpaper-scrim)),
var(--theme-wallpaper-image);
background-position: center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}

/* The main canvas normally repaints the same --background the body already
carries; while a wallpaper is active those duplicate coats would cover it
(even translucent ones compound), so they stand down and the body paint
shows through. Component surfaces — cards, popovers, glass — keep their own
paints; theme authors decide how much the wallpaper reads through them (and
the sidebar) with 8-digit hex color roles. */
html[data-wallpaper] [data-slot="sidebar-inset"],
html[data-wallpaper] [data-slot="sidebar-inset"] > .bg-background {
background-color: transparent;
}

/* Theme-token dependency probes are restored synchronously, before paint. Keep
transitions from observing the temporary sentinel color in between. */
html[data-theme-token-probe],
Expand Down
121 changes: 121 additions & 0 deletions apps/web/src/themePalette.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
createManagedThemeColors,
createVividThemeColors,
getDefaultThemeColors,
DEFAULT_THEME_WALLPAPER_OPACITY,
MAX_THEME_WALLPAPER_IMAGE_LENGTH,
THEME_FILE_VERSION,
} from "./themePalette";

Expand Down Expand Up @@ -224,6 +226,125 @@ describe("theme files", () => {
expect(JSON.parse(serializeThemeFile(withArtwork)).sidebarArtwork).toBe(true);
});

it("keeps wallpapers opt-in through theme files", () => {
const wallpaper = { image: "data:image/png;base64,aGVsbG8=", opacity: 0.2 };
const plain = parseThemeFile({
version: THEME_FILE_VERSION,
name: "Plain chrome",
appearance: "light",
colors: { accent: "#5b6cff" },
});
const wallpapered = parseThemeFile({
version: THEME_FILE_VERSION,
name: "Misty chrome",
appearance: "light",
colors: { accent: "#5b6cff" },
wallpaper,
});

expect(plain.wallpaper).toBeUndefined();
expect(wallpapered.wallpaper).toEqual(wallpaper);
expect(JSON.parse(serializeThemeFile(wallpapered)).wallpaper).toEqual(wallpaper);
});

it("rejects wallpapers that are not small embedded images", () => {
const base = {
version: THEME_FILE_VERSION,
name: "Wallpapered",
appearance: "light",
colors: { accent: "#5b6cff" },
} as const;

expect(() =>
parseThemeFile({ ...base, wallpaper: { image: "https://example.com/bg.png" } }),
).toThrow(/data: URIs/);
// Undecodable base64: wrong length, and over-padded.
expect(() =>
parseThemeFile({ ...base, wallpaper: { image: "data:image/png;base64,a" } }),
).toThrow(/data: URIs/);
expect(() =>
parseThemeFile({ ...base, wallpaper: { image: "data:image/png;base64,aGVsbG8===" } }),
).toThrow(/data: URIs/);
expect(() =>
parseThemeFile({
...base,
wallpaper: { image: `data:image/png;base64,${"a".repeat(MAX_THEME_WALLPAPER_IMAGE_LENGTH)}` },
}),
).toThrow(/192 KB/);
expect(() =>
parseThemeFile({
...base,
wallpaper: { image: "data:image/png;base64,aGVsbG8=", opacity: 2 },
}),
).toThrow(/between 0 and 1/);
});

it("paints and clears the wallpaper hook with the active theme", () => {
const stored = new Map<string, string>();
vi.stubGlobal("window", {
localStorage: {
getItem: (key: string) => stored.get(key) ?? null,
setItem: (key: string, value: string) => stored.set(key, value),
},
});
const documentElement = {
classList: { toggle: vi.fn() },
dataset: {} as Record<string, string>,
style: { removeProperty: vi.fn(), setProperty: vi.fn() },
};
vi.stubGlobal("document", { documentElement });

invalidateCustomThemes();
installCustomTheme(
parseThemeFile({
version: THEME_FILE_VERSION,
id: "misty",
name: "Misty",
appearance: "light",
colors: { accent: "#5b6cff" },
wallpaper: { image: "data:image/png;base64,aGVsbG8=" },
}),
);

// Drop the in-memory snapshot so the apply re-reads the stored JSON,
// covering the tolerant localStorage parse of the wallpaper.
invalidateCustomThemes();
applyThemePalette("misty");
expect(documentElement.dataset.wallpaper).toBe("");
expect(documentElement.style.setProperty).toHaveBeenCalledWith(
"--theme-wallpaper-image",
'url("data:image/png;base64,aGVsbG8=")',
);
expect(documentElement.style.setProperty).toHaveBeenCalledWith(
"--theme-wallpaper-opacity",
String(DEFAULT_THEME_WALLPAPER_OPACITY),
);

applyThemePalette(T3_CHAT_THEME.id);
expect(documentElement.dataset.wallpaper).toBeUndefined();
expect(documentElement.style.removeProperty).toHaveBeenCalledWith("--theme-wallpaper-image");
expect(documentElement.style.removeProperty).toHaveBeenCalledWith("--theme-wallpaper-opacity");

// Previews carry the wallpaper the save would keep — and clear it for
// drafts that would save without one, even over a wallpapered theme.
applyThemePalette("misty");
expect(documentElement.dataset.wallpaper).toBe("");
applyThemeColorPreview(T3_CHAT_THEME.colors, "light");
expect(documentElement.dataset.wallpaper).toBeUndefined();
applyThemeColorPreview(T3_CHAT_THEME.colors, "light", false, {
image: "data:image/png;base64,aGVsbG8=",
opacity: 0.4,
});
expect(documentElement.dataset.wallpaper).toBe("");
expect(documentElement.style.setProperty).toHaveBeenCalledWith(
"--theme-wallpaper-opacity",
"0.4",
);

vi.unstubAllGlobals();
invalidateCustomThemes();
});

it("publishes sidebar artwork changes from the live theme preview", () => {
const listener = vi.fn();
const unsubscribe = subscribeToThemePreview(listener);
Expand Down
Loading
Loading