atagen

malice

public
Star0
Watch0
061a70b6a034a7ab04cd9227b253dfdfd3c71d58
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/appearance.tsTypescript108 lines4.9 KB
1
import { get, writable, type Writable } from 'svelte/store';
2
import { THEME_COLOURS, isCustomTheme, type CustomTheme, type ThemeDocument } from './custom-themes';
3
4
export const MESSAGE_SPACING_MIN = 0;
5
export const MESSAGE_SPACING_MAX = 16;
6
export const MESSAGE_SPACING_DEFAULT = 3;
7
8
function persisted<T>(key: string, fallback: T, valid: (v: unknown) => v is T): Writable<T> {
9
  let initial = fallback;
10
  try {
11
    const raw = localStorage.getItem(key);
12
    if (raw !== null) {
13
      const parsed: unknown = JSON.parse(raw);
14
      if (valid(parsed)) initial = parsed;
15
    }
16
  } catch {}
17
  const store = writable(initial);
18
  store.subscribe((value) => {
19
    try {
20
      localStorage.setItem(key, JSON.stringify(value));
21
    } catch {}
22
  });
23
  return store;
24
}
25
26
const isBool = (v: unknown): v is boolean => typeof v === 'boolean';
27
const isSpacing = (v: unknown): v is number =>
28
  typeof v === 'number' && Number.isFinite(v) && v >= MESSAGE_SPACING_MIN && v <= MESSAGE_SPACING_MAX;
29
30
export const navFolded = persisted('malice.navFolded', false, isBool);
31
export const railFolded = persisted('malice.railFolded', false, isBool);
32
33
export const SIDE_WIDTH = { min: 200, max: 480, default: 260 };
34
export const MEMBERS_WIDTH = { min: 200, max: 420, default: 240 };
35
export const THREAD_WIDTH = { min: 300, max: 720, default: 400 };
36
const widthIn = (range: { min: number; max: number }) => (v: unknown): v is number =>
37
  typeof v === 'number' && Number.isFinite(v) && v >= range.min && v <= range.max;
38
export const sideWidth = persisted('malice.sideWidth', SIDE_WIDTH.default, widthIn(SIDE_WIDTH));
39
export const membersWidth = persisted('malice.membersWidth', MEMBERS_WIDTH.default, widthIn(MEMBERS_WIDTH));
40
export const threadWidth = persisted('malice.threadWidth', THREAD_WIDTH.default, widthIn(THREAD_WIDTH));
41
export const messageSpacing = persisted('malice.messageSpacing', MESSAGE_SPACING_DEFAULT, isSpacing);
42
43
export const LINE_HEIGHT_MIN = 1.1;
44
export const LINE_HEIGHT_MAX = 2;
45
export const LINE_HEIGHT_DEFAULT = 1.5;
46
47
const isLineHeight = (v: unknown): v is number =>
48
  typeof v === 'number' && Number.isFinite(v) && v >= LINE_HEIGHT_MIN && v <= LINE_HEIGHT_MAX;
49
50
export const lineHeight = persisted('malice.lineHeight', LINE_HEIGHT_DEFAULT, isLineHeight);
51
52
const isStringRecord = (v: unknown): v is Record<string, string> =>
53
  typeof v === 'object' && v !== null && !Array.isArray(v) && Object.values(v).every((x) => typeof x === 'string');
54
55
export const lastSpaceRooms = persisted<Record<string, string>>('malice.lastSpaceRooms', {}, isStringRecord);
56
57
export const THEMES = [
58
  { id: 'system', label: 'Match system' },
59
  { id: 'dark', label: 'Dark' },
60
  { id: 'light', label: 'Light' },
61
  { id: 'black', label: 'Black' },
62
  { id: 'nord', label: 'Nord' },
63
  { id: 'malicious', label: 'Malicious' }
64
] as const;
65
66
export type ThemeId = (typeof THEMES)[number]['id'] | CustomTheme['id'];
67
68
const isCustomThemeList = (v: unknown): v is CustomTheme[] => Array.isArray(v) && v.every(isCustomTheme);
69
export const customThemes = persisted<CustomTheme[]>('malice.customThemes', [], isCustomThemeList);
70
export const themePreview = writable<ThemeDocument | null>(null);
71
72
const isTheme = (v: unknown): v is ThemeId => THEMES.some((t) => t.id === v) || get(customThemes).some((t) => t.id === v);
73
const isAccent = (v: unknown): v is string | null => v === null || (typeof v === 'string' && /^#[0-9a-f]{6}$/i.test(v));
74
75
export const theme = persisted<ThemeId>('malice.theme', 'dark', isTheme);
76
export const accent = persisted<string | null>('malice.accent', null, isAccent);
77
78
if (typeof document !== 'undefined') {
79
  const root = document.documentElement;
80
  const prefersLight = window.matchMedia('(prefers-color-scheme: light)');
81
  const themeColor = document.querySelector('meta[name="theme-color"]');
82
  const apply = () => {
83
    const current = get(theme);
84
    const custom = get(themePreview) ?? get(customThemes).find((t) => t.id === current);
85
    const base = current === 'system' ? (prefersLight.matches ? 'light' : 'dark') : current;
86
    root.dataset.theme = custom?.scheme ?? (current.startsWith('custom:') ? 'dark' : base);
87
    for (const [key] of THEME_COLOURS) {
88
      if (custom) root.style.setProperty(`--${key}`, custom.colours[key]);
89
      else root.style.removeProperty(`--${key}`);
90
    }
91
    if (custom) {
92
      root.style.setProperty('--name-lightness', `${custom.nameLightness}%`);
93
      root.style.colorScheme = custom.scheme;
94
    } else {
95
      root.style.removeProperty('--name-lightness');
96
      root.style.removeProperty('color-scheme');
97
    }
98
    const override = get(accent);
99
    if (override && !get(themePreview)) root.style.setProperty('--accent', override);
100
    const bar = getComputedStyle(root).getPropertyValue('--rail-bg').trim();
101
    if (bar) themeColor?.setAttribute('content', bar);
102
  };
103
  theme.subscribe(apply);
104
  customThemes.subscribe(apply);
105
  themePreview.subscribe(apply);
106
  prefersLight.addEventListener('change', apply);
107
  accent.subscribe(apply);
108
}