diff options
Diffstat (limited to 'deckit/theme.py')
| -rw-r--r-- | deckit/theme.py | 66 |
1 files changed, 60 insertions, 6 deletions
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)) |
