From 863f7453f9e136cadc93db9f3f0b8f257f798ce8 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Fri, 13 Nov 2026 14:06:41 +0100 Subject: feat(tablet): a font of the PC's for the dim cover's clock [tablet] clock_font names a font family installed on the PC; the server finds its file with fc-match and serves it at /fonts/clock, and the cover's clock uses it, the date too while clock_font_date is on (default). fc-match falls back to any font, so the route answers only when the returned family is the one named; otherwise the page font stays. The font file never enters the repo, so a personal-use font can be used on one's own tablet without redistributing it. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 5 ++++- config.toml | 5 +++++ deckit/modules/tablet.py | 3 ++- deckit/server.py | 16 ++++++++++++++++ static/app.js | 8 +++++++- static/deck.css | 4 ++++ tests/test_server.py | 15 +++++++++++++++ 7 files changed, 53 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 4c34dee..c577643 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -87,7 +87,10 @@ Keep the server dependency-free apart from that one package. covers the page with `#dimmer` and `tablet`'s `dim` action sets the backlight to `dim_brightness` over adb (auto brightness off meanwhile); the touch on the cover calls `undim`, which puts both back, as does the - module's `stop`. + module's `stop`. The cover's clock (and, with `clock_font_date`, its + date) uses `[tablet] clock_font`, a family installed on the PC that the + server finds with `fc-match` and serves at `/fonts/clock`; the font file + never enters the repo, so a personal-use font is fine. Before a suspend, poweroff or reboot `deckit/sleep.py` (an elogind delay inhibitor plus `dbus-monitor` on PrepareForSleep/PrepareForShutdown) marks answers with `X-Deckit-Sleep: asleep|shutting down` for 2s, and `app.js` diff --git a/config.toml b/config.toml index cbaf6ee..d268031 100644 --- a/config.toml +++ b/config.toml @@ -100,6 +100,11 @@ peek = 8 # dim_brightness (0-255); a touch puts it back. 0 turns dimming off. dim_after = 120 dim_brightness = 8 +# A font family installed on the PC (fc-list) for the clock on the dim +# cover, served to the tablet at /fonts/clock; empty keeps the page font. +# clock_font_date sets the date in it too. +clock_font = "" +clock_font_date = true [icons] # Launcher icons follow the icon theme Keystone sets (gsettings diff --git a/deckit/modules/tablet.py b/deckit/modules/tablet.py index 71f7a95..08f12fa 100644 --- a/deckit/modules/tablet.py +++ b/deckit/modules/tablet.py @@ -112,6 +112,7 @@ def make(cfg: dict) -> Module: tcfg = cfg.get("tablet", {}) dim_after = max(0, int(tcfg.get("dim_after", 120))) level = min(255, max(0, int(tcfg.get("dim_brightness", 8)))) + clock = {"clock_font": bool(tcfg.get("clock_font")), "clock_font_date": bool(tcfg.get("clock_font_date", True))} saved = {} # mode and brightness from before a dim; empty while bright try: saved["mode"], saved["level"] = DIM_FILE.read_text().split() @@ -154,5 +155,5 @@ def make(cfg: dict) -> Module: pass return Module(name="tablet", title="Tablet", - state=lambda: {**state(), "dim_after": dim_after}, + state=lambda: {**state(), "dim_after": dim_after, **clock}, actions={"dim": dim, "undim": undim}, stop=stop) diff --git a/deckit/server.py b/deckit/server.py index 875a19d..2ced8dd 100644 --- a/deckit/server.py +++ b/deckit/server.py @@ -142,6 +142,19 @@ def nerd_font(): return _font["p"] +def clock_font(): + """[tablet] clock_font's file, for the dim cover's clock; None when unset + or not installed (fc-match falls back to any font, so the family must match).""" + family = CFG.get("tablet", {}).get("clock_font", "") + if not family: + return None + out = _run(["fc-match", "-f", "%{family}\n%{file}", family]) + names, _, file = out.partition("\n") + if family.lower() not in (n.strip().lower() for n in names.split(",")) or not Path(file).is_file(): + return None + return Path(file) + + def load_token() -> str: """A per-machine secret the tablet's browser holds as a cookie.""" path = token_path() @@ -206,6 +219,9 @@ class Handler(BaseHTTPRequestHandler): if path == "/fonts/nerd.ttf": f = nerd_font() return self._send(200, f.read_bytes(), "font/ttf", cache=True) if f else self._send(404) + if path == "/fonts/clock": + f = clock_font() + return self._send(200, f.read_bytes(), mimetypes.guess_type(f)[0] or "font/ttf") if f else self._send(404) if path.startswith("/api/"): if not self._authed(): return self._send(401) diff --git a/static/app.js b/static/app.js index d16bd80..1296805 100644 --- a/static/app.js +++ b/static/app.js @@ -293,7 +293,13 @@ function tick() { setInterval(tick, 1000); document.addEventListener("pointerdown", touched, true); // A fresh page is not dimmed, so a dim left by an earlier server goes too. -fetch("/api/tablet").then((r) => r.json()).then((t) => { dimAfter = t.dim_after ?? 0; tablet("undim"); touched(); }).catch(() => {}); +fetch("/api/tablet").then((r) => r.json()).then((t) => { + dimAfter = t.dim_after ?? 0; + // [tablet] clock_font, served at /fonts/clock (deck.css). + dimmer.classList.toggle("font-time", !!t.clock_font); + dimmer.classList.toggle("font-date", !!t.clock_font && !!t.clock_font_date); + tablet("undim"); touched(); +}).catch(() => {}); document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") { diff --git a/static/deck.css b/static/deck.css index b6d2202..fd0b813 100644 --- a/static/deck.css +++ b/static/deck.css @@ -611,6 +611,10 @@ select { #dimmer[hidden] { display: none; } .dim-time { font-size: 8rem; font-weight: 300; line-height: 1; color: var(--udt-text); font-variant-numeric: tabular-nums; } .dim-date { font-size: 1.6rem; color: var(--udt-subtext0); } +/* [tablet] clock_font; a missing font falls back to the page font. */ +@font-face { font-family: "deckit-clock"; src: url("/fonts/clock"); } +#dimmer.font-time .dim-time { font-family: "deckit-clock", "Inconsolata Nerd Font Mono", ui-monospace, monospace; font-weight: normal; } +#dimmer.font-date .dim-date { font-family: "deckit-clock", "Inconsolata Nerd Font Mono", ui-monospace, monospace; letter-spacing: .08em; } /* The PC asleep: over everything, and the wake lock is dropped (app.js). */ #asleep { diff --git a/tests/test_server.py b/tests/test_server.py index 522b781..8f82e9f 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -137,6 +137,21 @@ class Server(unittest.TestCase): self.assertEqual(self.req("GET", "/fonts/nerd.ttf", HOST)[0], 404) srv._font.clear() + def test_clock_font(self): + from unittest import mock + import tempfile + from pathlib import Path + f = Path(tempfile.mkdtemp()) / "Graffiti.ttf" + f.write_bytes(b"\x00\x01\x00\x00clock") + with mock.patch.dict(srv.CFG, {"tablet": {"clock_font": "a Graffiti"}}): + with mock.patch.object(srv, "_run", return_value=f"a Graffiti\n{f}"): + self.assertEqual(self.req("GET", "/fonts/clock", HOST)[0:2], (200, b"\x00\x01\x00\x00clock")) + # fc-match's fallback for a font that is not installed + with mock.patch.object(srv, "_run", return_value="DejaVu Sans\n/usr/share/fonts/DejaVuSans.ttf"): + self.assertEqual(self.req("GET", "/fonts/clock", HOST)[0], 404) + with mock.patch.dict(srv.CFG, {"tablet": {}}): + self.assertEqual(self.req("GET", "/fonts/clock", HOST)[0], 404) + def test_assets_and_api(self): self.assertEqual(self.req("GET", "/", HOST)[0], 200) self.assertEqual(self.req("GET", "/theme.css", HOST)[0], 200) -- cgit v1.2.3