diff options
Diffstat (limited to 'deckit')
| -rw-r--r-- | deckit/modules/tablet.py | 3 | ||||
| -rw-r--r-- | deckit/server.py | 13 | ||||
| -rw-r--r-- | deckit/theme.py | 66 |
3 files changed, 68 insertions, 14 deletions
diff --git a/deckit/modules/tablet.py b/deckit/modules/tablet.py index 08f12fa..71f7a95 100644 --- a/deckit/modules/tablet.py +++ b/deckit/modules/tablet.py @@ -112,7 +112,6 @@ 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() @@ -155,5 +154,5 @@ def make(cfg: dict) -> Module: pass return Module(name="tablet", title="Tablet", - state=lambda: {**state(), "dim_after": dim_after, **clock}, + state=lambda: {**state(), "dim_after": dim_after}, actions={"dim": dim, "undim": undim}, stop=stop) diff --git a/deckit/server.py b/deckit/server.py index 2ced8dd..bd46446 100644 --- a/deckit/server.py +++ b/deckit/server.py @@ -142,10 +142,11 @@ 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", "") +def clock_font(key): + """The file of a clock font keystone's fonts.json names (theme.CLOCK_FONTS); + None when unset or not installed (fc-match falls back to any font, so the + family must match).""" + family = theme.families().get(key, "") if not family: return None out = _run(["fc-match", "-f", "%{family}\n%{file}", family]) @@ -219,8 +220,8 @@ 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() + if path.startswith("/fonts/") and path[len("/fonts/"):] in theme.CLOCK_FONTS: + f = clock_font(path[len("/fonts/"):]) 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(): diff --git a/deckit/theme.py b/deckit/theme.py index 142a79f..343e084 100644 --- a/deckit/theme.py +++ b/deckit/theme.py @@ -24,6 +24,7 @@ than restating the colours, so a scheme or wallpaper change reaches the tablet. import json import re from pathlib import Path +from urllib.parse import quote PALETTE_LINE = re.compile(r"^\s*([a-z0-9]+)\s*:\s*(#[0-9a-fA-F]{6,8});", re.M) ACCENT = re.compile(r"accent:\s*(#[0-9a-fA-F]{6,8})") @@ -32,7 +33,8 @@ BORDER = re.compile(r"rgb\(\s*([0-9a-fA-F]{6})\s*\)") PALETTE_PATH = Path.home() / ".config" / "rofi" / "udt" / "palette.rasi" ACCENT_PATH = Path.home() / ".cache" / "udt" / "udt-accent.rasi" BORDER_PATH = Path.home() / ".cache" / "udt" / "udt-border.lua" -# Keystone's fonts page owns text sizes: deck-it's own override, else the base. +# Keystone's fonts page owns text sizes (deck-it's own override, else the base) +# and the clock's font families. FONTS_PATH = Path.home() / ".config" / "keystone" / "fonts.json" FONT_SIZE = 14 # px, the size deck.css was designed at @@ -74,8 +76,59 @@ def parse_font_size(text: str) -> int | None: return None +# fonts.json's "family" keys for the dim cover's clock, and the selector each sets. +CLOCK_FONTS = {"deckit-clock": ".dim-time", "deckit-date": ".dim-date"} +# Its "override" keys for the clock's px sizes; unset keeps deck.css's rem. +CLOCK_SIZES = {"deckit:clock": ".dim-time", "deckit:date": ".dim-date"} + + +def parse_clock_sizes(text: str) -> dict[str, int]: + """The clock's own px sizes from keystone's fonts.json, by selector, range-checked.""" + try: + override = json.loads(text).get("override") or {} + except (ValueError, AttributeError): + return {} + if not isinstance(override, dict): + return {} + return {sel: round(px) for key, sel in CLOCK_SIZES.items() + if isinstance(px := override.get(key), (int, float)) and not isinstance(px, bool) and 8 <= px <= 400} + + +def parse_families(text: str) -> dict[str, str]: + """The clock font families from keystone's fonts.json, by key; empty ones left out.""" + try: + family = json.loads(text).get("family") or {} + except (ValueError, AttributeError): + return {} + if not isinstance(family, dict): + return {} + return {k: v.strip() for k, v in family.items() + if k in CLOCK_FONTS and isinstance(v, str) and v.strip()} + + +def families() -> dict[str, str]: + try: + return parse_families(FONTS_PATH.read_text()) + except OSError: + return {} + + +def font_rules(fams: dict[str, str], sizes: dict[str, int] | None = None) -> str: + """@font-face and selector rules for each set clock font. The family is in + the URL only, quoted, so a change reloads the file and no name reaches the CSS.""" + out = [] + for key, sel in CLOCK_FONTS.items(): + if key in fams: + out.append(f'@font-face {{ font-family: "{key}"; src: url("/fonts/{key}?f={quote(fams[key])}"); }}') + out.append(f'#dimmer {sel} {{ font-family: "{key}", "Inconsolata Nerd Font Mono", ' + 'ui-monospace, monospace; font-weight: normal; }') + out += [f"#dimmer {sel} {{ font-size: {px}px; }}" for sel, px in (sizes or {}).items()] + return "".join(line + "\n" for line in out) + + def css(colors: dict, accent: str | None, border: list[str] | None = None, - font_size: int | None = None) -> str: + font_size: int | None = None, fams: dict[str, str] | None = None, + clock_sizes: dict[str, int] | None = None) -> str: lines = [":root {"] lines.append(f" --udt-font-size: {font_size or FONT_SIZE}px;") lines += [f" --udt-{name}: {value};" for name, value in colors.items()] @@ -85,7 +138,7 @@ def css(colors: dict, accent: str | None, border: list[str] | None = None, for index, value in enumerate(stops[:3], 1): lines.append(f" --udt-border-{index}: {value};") lines.append("}") - return "\n".join(lines) + "\n" + return "\n".join(lines) + "\n" + font_rules(fams or {}, clock_sizes) def load() -> str: @@ -105,7 +158,8 @@ def load() -> str: except OSError: border = None try: - font_size = parse_font_size(FONTS_PATH.read_text()) + fonts = FONTS_PATH.read_text() except OSError: - font_size = None - return css(colors, accent, border, font_size) + fonts = "" + return css(colors, accent, border, parse_font_size(fonts), parse_families(fonts), + parse_clock_sizes(fonts)) |
