// Which theme each site tab gets. The global theme applies everywhere unless an account profile // has a theme of its own, and any pane can override both (the pane's “…” menu, the app's theme // settings). A pane or account theme is the theme document as it is (the global theme's // customizations and accent belong to the global theme). Native code keeps the same rules and // delivers each tab its theme; the shell uses them for pane chrome. Pure (tests, shell, panel). export type ThemeScopes = { /** wayBetter's window (tabs, address bar, titlebar, panel) follows the theme, or keeps its own style. */ chrome: 'theme' | 'own'; /** Account profile id -> theme id. */ profiles: Record; /** Pane (tab) id -> theme id. */ panes: Record; }; export const MAX_PROFILE_THEMES = 32; export const MAX_PANE_THEMES = 64; export const SCOPES_DEFAULT: ThemeScopes = { chrome: 'theme', profiles: {}, panes: {} }; const THEME_ID = /^[a-z0-9-]{1,40}$/; const PROFILE_ID = /^(default|p[a-z0-9]{1,32})$/; const TAB_ID = /^[A-Za-z0-9-]{1,40}$/; function entries(value: unknown, key: RegExp, max: number): Record { if (!value || typeof value !== 'object' || Array.isArray(value)) return {}; const out: Record = {}; for (const [id, theme] of Object.entries(value)) { if (Object.keys(out).length >= max) break; if (key.test(id) && typeof theme === 'string' && THEME_ID.test(theme)) out[id] = theme; } return out; } /** Any JSON to valid scopes (bounded; unknown values dropped). */ export function normalizeScopes(input: unknown): ThemeScopes { const source = input && typeof input === 'object' ? input as Record : {}; return { chrome: source.chrome === 'own' ? 'own' : 'theme', profiles: entries(source.profiles, PROFILE_ID, MAX_PROFILE_THEMES), panes: entries(source.panes, TAB_ID, MAX_PANE_THEMES), }; } export type ScopeSource = 'pane' | 'account' | 'global'; /** * The theme id a tab shows and where it comes from: the pane's own, else its account's, else the * global one. An id that no longer exists (a removed imported theme) falls through. */ export function themeIdFor(scopes: ThemeScopes, tab: { id?: string; profile?: string }, globalPreset: string, known: (id: string) => boolean): { id: string; source: ScopeSource } { const pane = tab.id ? scopes.panes[tab.id] : undefined; if (pane && known(pane)) return { id: pane, source: 'pane' }; const account = tab.profile ? scopes.profiles[tab.profile] : undefined; if (account && known(account)) return { id: account, source: 'account' }; return { id: globalPreset, source: 'global' }; } /** Drops pane entries whose pane is gone (the shell calls this with its open tab ids). */ export function prunePanes(scopes: ThemeScopes, open: Iterable): ThemeScopes { const keep = new Set(open); const panes = Object.fromEntries(Object.entries(scopes.panes).filter(([id]) => keep.has(id))); return Object.keys(panes).length === Object.keys(scopes.panes).length ? scopes : { ...scopes, panes }; } /** Sets (or with null clears) one pane's or account's theme. */ export function assign(scopes: ThemeScopes, kind: 'panes' | 'profiles', id: string, theme: string | null): ThemeScopes { const next = { ...scopes[kind] }; if (theme) next[id] = theme; else delete next[id]; return normalizeScopes({ ...scopes, [kind]: next }); }