Files
magpie/internal/gui/assets/fonts.js
T
yetone a1f06bd780 gui: a browser tab offers the fonts of the computer it runs on (Norcleeh)
Norcleeh (Discord #general): magpie in Docker, used through the web GUI,
Settings' font menu doesn't list the fonts he installed on his own
computer; the app does. Cause, confirmed in the code: a tab's list is
/api/fonts, the server's families (none in a nogui build such as Docker)
plus commonFamilies, filtered by what the browser draws. A family that
is only on the browser's computer was never offered, and a choice the
server doesn't list was dropped by desktopFonts.available.

Now (webfonts.js, which only a browser tab loads; the app's windows are
unchanged):
- Where the Local Font Access API is (Chromium on https or localhost),
  the press that opens a family menu calls queryLocalFonts() once a
  page, and at load when the local-fonts permission is already given.
  The families join the list with their real styles ("SemiBold
  Condensed Italic" -> weight 600, 75%, italic).
- Elsewhere (Safari, Firefox, a plain-http LAN address, a refused
  permission) the menu's search says "Search or type a font name…" and
  offers Use "name" for a family the list lacks, enabled when the canvas
  probe says this browser draws it, greyed with "This browser can't find
  this font" when not; Enter takes it.
- A chosen family the server doesn't list stays in the tab's list on
  later loads while the browser draws it, so it keeps being applied.
- The app's menu (openProtoMenu), never a <select>. Strings in zh,
  zh-TW, ja and de. docs/subsystems/gui-shell.md says so.
Budget: no raise. here() moved out of fonts.js into webfonts.js, which
isn't a core script: loadFonts import()s it in a tab only (the page has
no CSP). The core is 106 bytes smaller than origin/main's.

Run (mote-run). At this head on e8c7dddcb: gofmt, go build ./... and
-tags nogui, go test -run 'Font|Budget|Boot|Asset' ./internal/gui
./internal/fonts ok; fonts, i18n-keys, web-page, settings-general,
inner-scroll-contain 114/114. Earlier, on 6af83172d (with a <script>
loader in place of the import):
- fonts, font-cascade, win-fonts, i18n-keys, settings-* (10 files),
  text-size, web-page, web-app-standalone, mobile-web, gui-de, gui-ja,
  gui-zh-tw, menu-scroll, omarchy, proto-menu-overflow: 311 tests
  before the rebase, 300 ok. Red: mobile-web's 8 "desktop
  matches origin/main" subtests (they read origin/main through git, and
  a mote-run copy has no .git), and settings-lines-wrap webkit zh 440px
  (a Models tab press timing out at 8s under load). At the rebased
  head: fonts, i18n-keys, settings-general, web-page,
  settings-lines-wrap: fonts' WebKit half and a lines-wrap zh-TW press
  timed out at mote load ~210 (the box then rebooted); rerun at load
  ~14, fonts WebKit 21/21 and settings-lines-wrap 20/20, the rest of the
  first run ok. The 8s press is filed as #4026.
- New tests in fonts.test.cjs, Chromium and WebKit, en and zh, 1000px
  and 440px: "a browser tab lists the fonts of the computer it runs on"
  (queryLocalFonts stubbed: not asked before the press, asked once in
  it, families and real styles offered, applied, no settings POST) and
  "a browser tab takes a typed font when the API is missing/refused".
  All 16 red on origin/main's assets (MAGPIE_FONT_ASSETS): this
  computer's families not offered, the search not taking a name.
- Broken by hand: merge() without the tab's own chosen families: the
  typed tests go red at "kept across a reload, though the server lists
  no such font" in all 8 cases.
- go build ./... and -tags nogui, gofmt -l, GOOS=linux go vet -tags
  nogui ./internal/gui, GOOS=windows go vet ./internal/gui ok; go test
  -tags nogui -run 'Font|Budget|Boot|Asset' ./internal/gui
  ./internal/fonts ok.
- Real use on mote: a nogui `magpie web` in a sandbox HOME (server list
  57 common families). Chromium with local-fonts granted: 204 families,
  185 only on this Mac; Baskerville picked, drawn, kept across a reload.
  WebKit (no API): typed Didot, offered and taken, kept across a reload.

Not tried: Chromium's real permission prompt (Playwright grants it),
Firefox, a browser on another computer than magpie's, a Docker image
run, the full Go suite (no Go change).
2026-10-28 16:55:01 +08:00

43 lines
2.3 KiB
JavaScript

// Installed font choices reach both webviews before their first paint.
// Keep --font and --code as the platform/theme fallbacks: an explicit
// choice is a separate variable, including on Omarchy's body. A browser
// tab's choice is its own (it may be on another computer), from the
// families it draws (webfonts.js).
window.desktopFonts = (() => {
let catalogue = null, held = null;
const web = window.bootPrefs?.web, KEY = "magpie.fonts";
const own = () => { try { return JSON.parse(localStorage[KEY]); } catch { return held; } };
function keep(key, v) {
held = { ...own(), [key]: v };
try { localStorage[KEY] = JSON.stringify(held); } catch {}
apply();
}
const same = (a, b) => !!a && !!b && ["family", "name", "weight", "style", "stretch"].every((k) => a[k] === b[k]);
const available = (face) => !face || catalogue === null || catalogue.some((f) => f.styles.some((s) => same(s, face)));
const quoted = (s) => '"' + s.replace(/[\\"\x00-\x1f\x7f]/g, (c) => "\\" + c.charCodeAt(0).toString(16) + " ") + '"';
function apply(s) {
const root = document.documentElement;
let active = false;
for (const [key, prefix, fallback] of [["uiFont", "ui", "font"], ["codeFont", "code", "code"]]) {
const f = (web ? own() : s)?.[key];
const valid = f && typeof f.family === "string" && f.family && ["normal", "italic", "oblique"].includes(f.style)
&& Number.isFinite(f.weight) && f.weight >= 1 && f.weight <= 1000 && Number.isFinite(f.stretch) && f.stretch >= 0;
const values = valid && available(f) ? {
font: quoted(f.family) + ", var(--" + fallback + ")", weight: f.weight, style: f.style, stretch: f.stretch + "%",
} : {};
if (values.font) active = true;
for (const part of ["font", "weight", "style", "stretch"]) {
const name = "--" + prefix + "-" + part;
if (values[part] !== undefined) root.style.setProperty(name, values[part]);
else root.style.removeProperty(name);
}
}
if (active) root.setAttribute("data-fonts", "");
else root.removeAttribute("data-fonts");
// a wider font widens the tabs too: the header is fitted again (kiwiflysky on X)
window.fitTop?.();
}
return { apply, available, same, own, keep, quoted, catalogue: (list) => { catalogue = list; } };
})();
window.desktopFonts.apply(window.bootPrefs);