/** * @file fontSize — the two text sizes Settings → Appearance offers (issue #79), * viewer preferences kept in `localStorage` and synced to the account like the * theme (`lib/userPrefs.ts` PREF_KEYS): * - `agenthub.ui.fontSize`: the interface's size, the ROOT font size in px. * Tailwind's sizes and spacing are rem, and `index.css` re-states every * `text-[Npx]` class the app uses in rem, so the whole UI follows it. * `index.html` applies it before first paint (same key, same range). * Capped at 18px: the diff view pins its rows at `ROW_PX` (16px). * - `agenthub.terminal.fontSize`: xterm's cell font size in px (default 13). * A write fires `FONT_SIZE_EVENT`, which open terminals listen for to re-fit. * @author Nguyễn Ngọc Trí Vĩ */ export const UI_FONT_SIZE_KEY = "agenthub.ui.fontSize"; export const TERMINAL_FONT_SIZE_KEY = "agenthub.terminal.fontSize"; /** Fired on `window` after either size changes (this tab or a hydrate). */ export const FONT_SIZE_EVENT = "agenthub-font-size"; /** The interface sizes, root px; 16 is the browser's own default. */ export const UI_FONT_SIZES = [14, 16, 17, 18] as const; export type UiFontSize = (typeof UI_FONT_SIZES)[number]; export const DEFAULT_UI_FONT_SIZE: UiFontSize = 16; export const TERMINAL_FONT_SIZES = [11, 12, 13, 14, 15, 16, 18, 20] as const; export const DEFAULT_TERMINAL_FONT_SIZE = 13; function read(key: string): string | null { try { return window.localStorage.getItem(key); } catch { return null; } } function write(key: string, value: number) { try { window.localStorage.setItem(key, String(value)); } catch { /* storage blocked — this page still applies it */ } window.dispatchEvent(new Event(FONT_SIZE_EVENT)); } export function readUiFontSize(): UiFontSize { const n = Number(read(UI_FONT_SIZE_KEY)); return (UI_FONT_SIZES as readonly number[]).includes(n) ? (n as UiFontSize) : DEFAULT_UI_FONT_SIZE; } /** Paint the stored interface size on `` (the default leaves it unset). */ export function applyUiFontSize(size: UiFontSize = readUiFontSize()) { const root = document.documentElement; if (size === DEFAULT_UI_FONT_SIZE) root.style.removeProperty("font-size"); else root.style.fontSize = `${size}px`; } export function writeUiFontSize(size: UiFontSize) { applyUiFontSize(size); write(UI_FONT_SIZE_KEY, size); } export function readTerminalFontSize(): number { const n = Number(read(TERMINAL_FONT_SIZE_KEY)); return (TERMINAL_FONT_SIZES as readonly number[]).includes(n) ? n : DEFAULT_TERMINAL_FONT_SIZE; } export function writeTerminalFontSize(size: number) { write(TERMINAL_FONT_SIZE_KEY, size); }