Official
themes
The stock themes: presets of every colour, type size, corner, density token and background, chosen per light and dark mode and edited in Settings, beside the themes other plugins ship.
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/themes@0.3.5
Permissions in 0.3.5
Files
backdrops.ts4.8 KB
// A preset's backdrops (`ThemeTokens.backdrop`): what it draws behind the
// chat plugin's two places, the new chat page and a chat, as the editor
// shows them: one kind per place and the few values of that kind. The
// kernel draws them (`crates/kernel/src/theme_backdrop.rs`).
import type { Api, Color, ThemeBackdrop, ThemeTokens } from "divergence";
export const PLACES = [
{ key: "newChat", title: "New chat page" },
{ key: "chat", title: "Chats" },
] as const;
export type Place = (typeof PLACES)[number]["key"];
export type Kind = "none" | "caustics" | "fog" | "contours" | "color" | "gradient" | "image";
export const KINDS: readonly { readonly value: Kind; readonly label: string }[] = [
{ value: "none", label: "App default" },
{ value: "caustics", label: "Caustics" },
{ value: "fog", label: "Fog" },
{ value: "contours", label: "Contours" },
{ value: "color", label: "Colour" },
{ value: "gradient", label: "Gradient" },
{ value: "image", label: "Picture" },
];
/** A picture starts under a veil, so the words over it stay readable. */
export const PICTURE_VEIL = { opacity: 0.5 } as const;
export function kindOf(backdrop: ThemeBackdrop | undefined): Kind {
if (!backdrop) return "none";
if ("scene" in backdrop) return backdrop.scene;
if ("image" in backdrop) return "image";
return typeof backdrop.fill === "string" ? "color" : "gradient";
}
/** The backdrop a choice of `kind` starts with, keeping the veil, and a scene's hue and speed, of `was`. A picture
* has no start: it waits for the user to pick one (`pickPicture`). */
export function startOf(kind: Exclude<Kind, "none" | "image">, was: ThemeBackdrop | undefined, ground: Color): ThemeBackdrop {
const glass = was?.glass ? { glass: was.glass } : {};
switch (kind) {
case "caustics":
case "fog":
case "contours": {
const scene = was && "scene" in was ? { color: was.color, speed: was.speed } : {};
return { scene: kind, ...scene, ...glass };
}
case "color":
return { fill: ground, ...glass };
case "gradient":
return { fill: { linearGradient: { angle: 180, stops: [[ground, 0], ["#000000", 1]] } }, ...glass };
}
}
/** The tokens with `place`'s backdrop set, or gone for none. */
export function withBackdrop(tokens: ThemeTokens, place: Place, backdrop: ThemeBackdrop | undefined): ThemeTokens {
const { [place]: _old, ...rest } = tokens.backdrop ?? {};
const next = backdrop ? { ...rest, [place]: backdrop } : rest;
const { backdrop: _all, ...others } = tokens;
return Object.keys(next).length ? { ...others, backdrop: next } : others;
}
/** The backdrop with its veil changed; a veil with no tint and no blur is gone. */
export function withGlass(backdrop: ThemeBackdrop, change: { opacity?: number; blur?: number }): ThemeBackdrop {
const glass = { ...backdrop.glass, ...change };
const { glass: _old, ...ground } = backdrop;
return (glass.opacity ?? 0.6) > 0 || (glass.blur ?? 0) > 0 ? { ...ground, glass } : (ground as ThemeBackdrop);
}
/** The folder of the plugin's data where picked pictures are kept. */
const folder = (api: Api) => `${api.paths.data}/backdrops`;
/**
* Asks the user for a picture and keeps a copy in the plugin's data folder, so the preset still draws it when the
* original moves; answers the copy's path, or null when the user picks none.
*/
export async function pickPicture(api: Api): Promise<string | null> {
const { paths } = await api.pickImages();
const path = paths[0];
if (!path) return null;
const image = await api.attachImage(path);
try {
const { data, mimeType } = await api.host.kernel("image_data", { id: image.id });
const extension = mimeType.split("/")[1]!.replace("jpeg", "jpg").replace("svg+xml", "svg");
await api.fs.mkdir(folder(api));
const copy = `${folder(api)}/${crypto.randomUUID()}.${extension}`;
await api.fs.write(copy, data, { encoding: "base64" });
return copy;
} finally {
await api.host.kernel("forget_image", { id: image.id });
}
}
/** Every picture a preset of the user's draws. */
export function picturesIn(presets: readonly { readonly tokens: ThemeTokens }[]): Set<string> {
const pictures = new Set<string>();
for (const { tokens } of presets)
for (const backdrop of Object.values(tokens.backdrop ?? {})) if ("image" in backdrop) pictures.add(backdrop.image);
return pictures;
}
/** Removes the kept copies the presets no longer draw, once a save of `next` dropped one of `before`'s. */
export async function prune(api: Api, before: Set<string>, next: Set<string>): Promise<void> {
const ours = (path: string) => path.startsWith(`${folder(api)}/`);
if (![...before].some((path) => ours(path) && !next.has(path))) return;
for (const entry of await api.fs.list(folder(api))) {
const path = `${folder(api)}/${entry.name}`;
if (!next.has(path)) await api.fs.remove(path);
}
}
No comments yet.