Files
magpie/internal/gui/assets/appearance.js
T
yetone a65dade2e3 gui: an accent colour of your own and a picture behind the pages (CHENGJIAHEGOXIS on X)
CHENGJIAHEGOXIS asked for colours beyond the three themes and for a
local picture. Settings › General gains two rows under Appearance:

- Accent color: magpie's own, six quiet presets (Blue, Teal, Green,
  Orange, Pink, Graphite) and a swatch that opens the system's colour
  picker. Saved as settings.accent (#rrggbb only, checked in saveOwn;
  `magpie settings set accent "#0d9488"` works through settingsapi.Keys).
  The server draws it (appearance.go accentVars): darkened on the light
  theme and lightened on the dark one until it reads as text at 4.5:1
  on the background and cards, with white or near-black text on it,
  whichever reads better. Never under Omarchy, whose theme is the look.
- Background image: Choose… takes a PNG, JPEG, GIF or WebP of 16 MB at
  most, sent as base64 JSON to POST /api/appearance/wallpaper, saved as
  `wallpaper` in magpie's folder (edit.WriteAtomic); settings.Wallpaper
  is its stamp. A thumbnail, Change and Remove show; Remove deletes the
  file. It is drawn behind the window's pages (never the tray panel)
  under a veil of the theme's background at 84%, so text reads as on
  the plain page; the cards stay opaque, and body's colour stays the
  theme's, which Windows' title bar tint reads.

boot.js puts the look's stylesheet in before the first paint and says
which look it drew (bootPrefs.look); applyPrefs asks /api/appearance/css
only when the settings' look differs. The rows, with the Colorblind,
Reduce motion and Loading bird rows, are drawn by appearance.js,
imported when Settings draws, so the core scripts stay under their
budgets with no raise (app.js 2,393 under, the core 503 under).
A save of Settings that doesn't send accent keeps it (Merge guard),
and the wallpaper stamp is kept by every Settings save.

fonts.test.cjs: the two new rows push the font row to the foot of its
560×620 window, where Playwright's click scrolled it in by a pixel and
the test read that as the page moving; the reader now scrolls it in
first with the wheel. settings_keep_test sets Wallpaper; the three
/api/appearance routes are named in the CLI parity test's notYet.

Run (mote, at this change on origin/main 406ad72f1; after the rebase
on fa3ee5a7e, which touched only the Library plugin and the site here,
the core budget, TestEveryRoute*, TestSettings*, TestAccent*,
TestWallpaper*, appearance-look and i18n-keys again, the same result):
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on
  internal/gui, settings, settingsapi: clean.
- go test -count=1 ./internal/gui/... ./internal/settings/...
  ./internal/settingsapi/... ./internal/cli/...: ok.
  (go vet ./internal/cli too.)
- GUI, chromium and webkit: appearance-look (picking a preset, the
  reader's own and back; boot.js's look not fetched again; choosing
  and removing a picture; the rows keep their height while
  appearance.js loads; en, zh, zh-TW, ja, de at 420 and 1000px: rows
  fit, words translated), fonts, settings-general, settings-sections,
  settings-groups, reduce-motion, skeleton-bird, i18n-en-no-cjk: green.
  Red the same at origin/main 7693cf263 without this change, all
  filed: api-key-theme (#6444, filed now), i18n-keys "No folder"
  (#6422), settings-search and settings-lines-wrap de 440px (#6353).

Broken by hand, each red, then restored:
- Merge guard removed: TestAccentSetting red ("a save without the
  accent changed it").
- accentInk = 1.0: TestAccentReadable red.
- applyPrefs' lookPaint call removed: appearance-look's accent and
  picture tests red in chromium.
- the thumbnail made 66px tall: the row-height test red.

Not tried: the real app on Windows, Linux or Omarchy (title bar tint
with a picture, WebKitGTK drawing the layers), the system's real colour
picker dialog (the test sets the input's value), a 16 MB picture in the
real web views, and the whole GUI suite (queued on mote's suite lock
after the tag).
2026-11-13 13:52:41 +08:00

111 lines
5.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Settings › General's look (CHENGJIAHEGOXIS on X): an accent colour of
// the reader's own and a picture behind the window's pages, beside the
// Colorblind, Reduce motion and Loading bird rows. The stylesheet that
// draws the look is the server's (appearance.go): boot.js puts it in
// before the first paint, and lookPaint takes it again once it changed.
// app.js imports this when Settings draws or the look changed, so it
// weighs nothing on the core.
// the presets: magpie's own first, then a few quiet ones
const ACCENTS = () => [["", t("Default"), "#4f46e5"], ["#2563eb", t("Blue")], ["#0d9488", t("Teal")], ["#16a34a", t("Green")], ["#ea580c", t("Orange")], ["#db2777", t("Pink")], ["#52525b", t("Graphite")]];
window.lookPaint = async function () {
try {
const r = await (await fetch("/api/appearance/css")).json();
let st = document.getElementById("lookCSS");
if (!st) document.head.append((st = Object.assign(document.createElement("style"), { id: "lookCSS" })));
st.textContent = r.css || "";
// the accent is in the panel's tint and the title bar too
if (typeof tintPanel == "function") tintPanel(0);
} catch { /* the look stays as drawn */ }
};
window.drawLook = function (s, keep) {
$("#colorblindSegs").title = t("Colorblind-safe chart colors, and each allowance window's line in a dash of its own");
$("#colorblindSegs").replaceChildren(segsOO(s.colorblind, (v) => savePrefs({ ...keep, colorblind: v })));
$("#reduceMotionSegs").title = t("Animations stand still, as with the system's Reduce Motion, so magpie keeps the computer less busy");
$("#reduceMotionSegs").replaceChildren(segsOO(s.reduceMotion, (v) => savePrefs({ ...keep, reduceMotion: v })));
$("#birdSegs").title = t("magpie's bird hops on what is still loading; Off keeps it away, and magpie stops looking for it");
$("#birdSegs").replaceChildren(segsOO(!s.noBird, (v) => savePrefs({ ...keep, noBird: !v })));
drawAccent(s, keep);
drawWallpaper(s);
};
function drawAccent(s, keep) {
const box = $("#accentPick");
if (!box) return;
const now = (s.accent || "").toLowerCase();
const pick = (v) => savePrefs({ ...keep, accent: v });
const swatch = (v, name, color) => {
const b = el("button", "swatch" + (v === now ? " on" : ""));
b.type = "button";
b.setAttribute("role", "radio");
b.setAttribute("aria-checked", String(v === now));
b.title = name;
b.setAttribute("aria-label", name);
b.dataset.accent = v;
b.style.setProperty("--sw", color || v);
b.onclick = () => v !== now && pick(v);
return b;
};
const presets = ACCENTS();
const list = presets.map(([v, name, color]) => swatch(v, name, color));
// a colour of the reader's own, from the system's colour picker
const mine = now && !presets.some(([v]) => v === now);
const own = el("label", "swatch own" + (mine ? " on" : ""));
own.title = t("Custom color");
own.setAttribute("aria-label", own.title);
own.setAttribute("role", "radio");
own.setAttribute("aria-checked", String(!!mine));
if (mine) own.style.setProperty("--sw", now);
const input = el("input");
input.type = "color";
input.value = mine ? now : "#4f46e5";
input.tabIndex = -1;
// the picker reports as the reader drags; the choice is saved when it closes
input.onchange = () => input.value.toLowerCase() !== now && pick(input.value.toLowerCase());
own.tabIndex = 0;
own.onkeydown = (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); input.click(); } };
own.append(input);
box.replaceChildren(...list, own);
}
function drawWallpaper(s) {
const box = $("#wallpaperPick");
if (!box) return;
const file = el("input");
file.type = "file";
file.accept = "image/png,image/jpeg,image/gif,image/webp";
file.hidden = true;
const send = async (body) => {
try {
prefs = await api("appearance/wallpaper", body);
if (state) state.settings = prefs;
applyPrefs(prefs);
renderSettings();
} catch (e) { statusErr(e); }
};
file.onchange = async () => {
const f = file.files[0];
if (!f) return;
if (f.size > 16 << 20) { statusErr(new Error(t("A background image is 16 MB at most"))); return; }
// as base64 in JSON: the app's web view drops a File sent as the body
const bytes = new Uint8Array(await f.arrayBuffer());
let bin = "";
for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
send({ data: btoa(bin) });
};
const choose = el("button", "text", s.wallpaper ? t("Change") : t("Choose…"));
choose.type = "button";
choose.onclick = () => file.click();
if (!s.wallpaper) { box.replaceChildren(choose, file); return; }
const thumb = el("img", "wall-thumb");
thumb.alt = "";
thumb.src = "/api/appearance/wallpaper?v=" + s.wallpaper;
thumb.onerror = () => thumb.remove(); // a stamp whose file is gone (another computer's settings)
const remove = el("button", "text", t("Remove"));
remove.type = "button";
remove.onclick = () => send({ remove: true });
box.replaceChildren(thumb, choose, remove, file);
}