Custom background picture for the dot matrix headers
Settings → Appearance: upload an image and every page header draws it as the dot matrix instead of The Creation of Adam, with a clickable focus point and a switch back to the built-in painting. The browser turns the image into a brightness grid; only that grid is saved as a setting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
422e46bf65
commit
02756ddf90
11 files changed
+309
-18
No files matched your search
@@ -0,0 +1,19 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { parseBackdrop } from "./backdrop";
|
||||
|
||||
const valid = { w: 10, h: 8, dots: "0123456789abcdef".repeat(5), focus: [0.3, 0.7] };
|
||||
|
||||
test("parseBackdrop keeps a valid grid", () => {
|
||||
assert.deepEqual(parseBackdrop(valid), valid);
|
||||
});
|
||||
|
||||
test("parseBackdrop rejects broken grids", () => {
|
||||
assert.equal(parseBackdrop(null), null);
|
||||
assert.equal(parseBackdrop({ ...valid, dots: valid.dots.slice(1) }), null);
|
||||
assert.equal(parseBackdrop({ ...valid, dots: valid.dots.replace("a", "g") }), null);
|
||||
assert.equal(parseBackdrop({ ...valid, w: 4, dots: "0".repeat(32) }), null);
|
||||
assert.equal(parseBackdrop({ ...valid, w: 241, dots: "0".repeat(241 * 8) }), null);
|
||||
assert.equal(parseBackdrop({ ...valid, focus: [0.5, 1.5] }), null);
|
||||
assert.equal(parseBackdrop({ ...valid, focus: [0.5] }), null);
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// A custom picture for the dot painting behind every page header (Settings → Appearance). Stored as
|
||||
// a brightness grid like the built-in painting; the uploaded photo itself is never sent or kept.
|
||||
|
||||
export const BACKDROP_KEY = "appearance.backdrop";
|
||||
/** Longest side of the grid, in cells. */
|
||||
export const BACKDROP_MAX_CELLS = 240;
|
||||
|
||||
export type Backdrop = {
|
||||
w: number;
|
||||
h: number;
|
||||
/** w×h brightness levels 0–f, row by row. */
|
||||
dots: string;
|
||||
/** The point (x, y as fractions of the picture) kept in view when the header crops it. */
|
||||
focus: [number, number];
|
||||
};
|
||||
|
||||
const fraction = (v: unknown) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v <= 1;
|
||||
const side = (v: unknown) => Number.isInteger(v) && (v as number) >= 8 && (v as number) <= BACKDROP_MAX_CELLS;
|
||||
|
||||
/** The backdrop if `v` is a valid one, else null. */
|
||||
export function parseBackdrop(v: unknown): Backdrop | null {
|
||||
if (!v || typeof v !== "object") return null;
|
||||
const { w, h, dots, focus } = v as Record<string, unknown>;
|
||||
if (!side(w) || !side(h)) return null;
|
||||
if (typeof dots !== "string" || dots.length !== (w as number) * (h as number) || !/^[0-9a-f]*$/.test(dots)) return null;
|
||||
if (!Array.isArray(focus) || focus.length !== 2 || !focus.every(fraction)) return null;
|
||||
return { w: w as number, h: h as number, dots, focus: [focus[0], focus[1]] };
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import "server-only";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { settings, workerState } from "@/lib/db/schema";
|
||||
import { type Backdrop, BACKDROP_KEY, parseBackdrop } from "@/lib/backdrop";
|
||||
import { isLabelColor } from "@/lib/colors";
|
||||
import { FILE_ROLE_ORDER, FILE_ROLES, type FileRole, type FileRoleStyles } from "@/lib/file-roles";
|
||||
import { MIN_SPEED_LIMIT, parseSpeedLimit, SPEED_LIMIT_KEY } from "@/lib/download-limit";
|
||||
@@ -140,3 +141,19 @@ export async function getScanStatus(): Promise<ScanStatus> {
|
||||
nextScanAt: next?.getTime() ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
/** The custom picture behind the page headers (Settings → Appearance), or null for the built-in painting. */
|
||||
export async function getBackdrop(): Promise<Backdrop | null> {
|
||||
return parseBackdrop(await read<unknown>(BACKDROP_KEY));
|
||||
}
|
||||
|
||||
export async function saveBackdrop(input: unknown): Promise<Backdrop | null> {
|
||||
if (input == null) {
|
||||
await db().delete(settings).where(eq(settings.key, BACKDROP_KEY));
|
||||
return null;
|
||||
}
|
||||
const backdrop = parseBackdrop(input);
|
||||
if (!backdrop) throw new SettingsError("That picture couldn't be read. Try another image.");
|
||||
await write(BACKDROP_KEY, backdrop);
|
||||
return backdrop;
|
||||
}
|
||||
Reference in new issue
Block a user