Add theme settings, and remove the old theme system

This commit is contained in:
tyuXX 2026-11-15 20:48:49 +03:00
parent 567ac1a26d
commit 9f4a3197de
22 changed files with 2301 additions and 463 deletions

View file

@ -5,13 +5,212 @@ import type {
SavedRocketBlueprint,
} from "../shared/types.js";
export type ShapeSet = "chamfered" | "smooth" | "sharp";
export type ColorSet =
| "eyebleed"
| "subtle"
| "low-contrast"
| "catppuccin-mocha"
| "daylight"
| "gradient"
| "custom";
export interface GradientThemeConfig {
stops: string[];
angle: number;
mode: "linear" | "radial";
surfaceBase: "dark" | "light";
applyToPanels: boolean;
}
export interface CustomThemeTokens {
bgDeep: string;
bgPanelSolid: string;
bgElevated: string;
border: string;
text: string;
textDim: string;
accent: string;
glow: string;
green: string;
amber: string;
red: string;
violet: string;
panelOpacity: number;
}
export interface ThemeConfig {
shapeSet: ShapeSet;
colorSet: ColorSet;
uiEffects: boolean;
motionEffects: boolean;
dynamicAccent: boolean;
gradient: GradientThemeConfig;
custom: CustomThemeTokens;
}
export interface ClientSettings {
quality: "low" | "medium" | "high";
sound: boolean;
notifications: boolean;
dynamicTheme: boolean;
colorMode: "dark" | "light";
reducedEffects: boolean;
theme: ThemeConfig;
}
interface LegacyClientSettings {
quality?: "low" | "medium" | "high";
sound?: boolean;
notifications?: boolean;
dynamicTheme?: boolean;
colorMode?: "dark" | "light";
reducedEffects?: boolean;
theme?: Partial<ThemeConfig>;
}
export const DEFAULT_GRADIENT_CONFIG: GradientThemeConfig = {
stops: ["#0b1120", "#172554", "#0f172a"],
angle: 135,
mode: "linear",
surfaceBase: "dark",
applyToPanels: true,
};
export const DEFAULT_CUSTOM_TOKENS: CustomThemeTokens = {
bgDeep: "#0b0e14",
bgPanelSolid: "#12161f",
bgElevated: "#19202e",
border: "#2a3449",
text: "#e2e8f0",
textDim: "#94a3b8",
accent: "#60a5fa",
glow: "#60a5fa",
green: "#34d399",
amber: "#f59e0b",
red: "#f87171",
violet: "#a78bfa",
panelOpacity: 0.92,
};
export const DEFAULT_THEME_CONFIG: ThemeConfig = {
shapeSet: "chamfered",
colorSet: "catppuccin-mocha",
uiEffects: true,
motionEffects: true,
dynamicAccent: true,
gradient: { ...DEFAULT_GRADIENT_CONFIG, stops: [...DEFAULT_GRADIENT_CONFIG.stops] },
custom: { ...DEFAULT_CUSTOM_TOKENS },
};
const VALID_SHAPE_SETS = new Set<ShapeSet>(["chamfered", "smooth", "sharp"]);
const VALID_COLOR_SETS = new Set<ColorSet>([
"eyebleed",
"subtle",
"low-contrast",
"catppuccin-mocha",
"daylight",
"gradient",
"custom",
]);
function normalizeStoredHex(val: unknown, fallback: string): string {
if (typeof val !== "string") return fallback;
const trimmed = val.trim();
if (/^#[0-9a-fA-F]{6}$/.test(trimmed)) return trimmed.toLowerCase();
if (/^#[0-9a-fA-F]{3}$/.test(trimmed)) {
const [, r, g, b] = trimmed;
return `#${r}${r}${g}${g}${b}${b}`.toLowerCase();
}
return fallback;
}
export function normalizeThemeConfig(
rawTheme?: Partial<ThemeConfig> | null,
legacy?: LegacyClientSettings,
): ThemeConfig {
const legacyReduced = !!legacy?.reducedEffects;
const legacyLight = legacy?.colorMode === "light";
const legacyDyn = legacy?.dynamicTheme ?? true;
if (!rawTheme) {
return {
shapeSet: legacyReduced ? "smooth" : "chamfered",
colorSet: legacyLight
? "daylight"
: legacyReduced
? "subtle"
: "catppuccin-mocha",
uiEffects: !legacyReduced,
motionEffects: !legacyReduced,
dynamicAccent: legacyReduced ? false : legacyDyn,
gradient: {
...DEFAULT_GRADIENT_CONFIG,
stops: [...DEFAULT_GRADIENT_CONFIG.stops],
},
custom: { ...DEFAULT_CUSTOM_TOKENS },
};
}
const rawGrad = rawTheme.gradient;
const rawStops = Array.isArray(rawGrad?.stops)
? rawGrad.stops
.slice(0, 3)
.map((s, idx) =>
normalizeStoredHex(s, DEFAULT_GRADIENT_CONFIG.stops[idx] ?? "#0f172a"),
)
: [...DEFAULT_GRADIENT_CONFIG.stops];
while (rawStops.length < 2) {
rawStops.push(DEFAULT_GRADIENT_CONFIG.stops[rawStops.length] ?? "#172554");
}
const rawCustom = rawTheme.custom;
const angleNum = Number(rawGrad?.angle);
const opacityNum = Number(rawCustom?.panelOpacity);
return {
shapeSet: VALID_SHAPE_SETS.has(rawTheme.shapeSet as ShapeSet)
? (rawTheme.shapeSet as ShapeSet)
: "chamfered",
colorSet: VALID_COLOR_SETS.has(rawTheme.colorSet as ColorSet)
? (rawTheme.colorSet as ColorSet)
: "catppuccin-mocha",
uiEffects: rawTheme.uiEffects ?? true,
motionEffects: rawTheme.motionEffects ?? true,
dynamicAccent: rawTheme.dynamicAccent ?? true,
gradient: {
stops: rawStops,
angle: Number.isFinite(angleNum)
? Math.max(0, Math.min(360, Math.round(angleNum)))
: DEFAULT_GRADIENT_CONFIG.angle,
mode: rawGrad?.mode === "radial" ? "radial" : "linear",
surfaceBase: rawGrad?.surfaceBase === "light" ? "light" : "dark",
applyToPanels: rawGrad?.applyToPanels ?? true,
},
custom: {
bgDeep: normalizeStoredHex(rawCustom?.bgDeep, DEFAULT_CUSTOM_TOKENS.bgDeep),
bgPanelSolid: normalizeStoredHex(
rawCustom?.bgPanelSolid,
DEFAULT_CUSTOM_TOKENS.bgPanelSolid,
),
bgElevated: normalizeStoredHex(
rawCustom?.bgElevated,
DEFAULT_CUSTOM_TOKENS.bgElevated,
),
border: normalizeStoredHex(rawCustom?.border, DEFAULT_CUSTOM_TOKENS.border),
text: normalizeStoredHex(rawCustom?.text, DEFAULT_CUSTOM_TOKENS.text),
textDim: normalizeStoredHex(
rawCustom?.textDim,
DEFAULT_CUSTOM_TOKENS.textDim,
),
accent: normalizeStoredHex(rawCustom?.accent, DEFAULT_CUSTOM_TOKENS.accent),
glow: normalizeStoredHex(rawCustom?.glow, DEFAULT_CUSTOM_TOKENS.glow),
green: normalizeStoredHex(rawCustom?.green, DEFAULT_CUSTOM_TOKENS.green),
amber: normalizeStoredHex(rawCustom?.amber, DEFAULT_CUSTOM_TOKENS.amber),
red: normalizeStoredHex(rawCustom?.red, DEFAULT_CUSTOM_TOKENS.red),
violet: normalizeStoredHex(rawCustom?.violet, DEFAULT_CUSTOM_TOKENS.violet),
panelOpacity: Number.isFinite(opacityNum)
? Math.max(0.65, Math.min(1, opacityNum))
: DEFAULT_CUSTOM_TOKENS.panelOpacity,
},
};
}
const TOKEN_KEY = "spacegaem_proto_token";
@ -36,39 +235,35 @@ export function setToken(t: string | null): void {
else localStorage.removeItem(TOKEN_KEY);
}
let cachedSettings: ClientSettings | null = null;
export function getSettings(): ClientSettings {
if (cachedSettings) return cachedSettings;
try {
const raw = JSON.parse(
localStorage.getItem(SETTINGS_KEY) ?? "{}",
) as Partial<ClientSettings>;
return {
quality: raw.quality ?? "high",
) as LegacyClientSettings;
cachedSettings = {
quality:
raw.quality === "low" || raw.quality === "medium" ? raw.quality : "high",
sound: raw.sound ?? true,
notifications: raw.notifications ?? true,
dynamicTheme: raw.dynamicTheme ?? true,
colorMode: raw.colorMode === "light" ? "light" : "dark",
reducedEffects: raw.reducedEffects ?? false,
theme: normalizeThemeConfig(raw.theme, raw),
};
} catch {
return {
cachedSettings = {
quality: "high",
sound: true,
notifications: true,
dynamicTheme: true,
colorMode: "dark",
reducedEffects: false,
theme: normalizeThemeConfig(null),
};
}
return cachedSettings;
}
export function saveSettings(s: ClientSettings): void {
localStorage.setItem(SETTINGS_KEY, JSON.stringify(s));
document.body.dataset.dynamicTheme = s.dynamicTheme ? "on" : "off";
document.documentElement.dataset.colorMode = s.colorMode;
document.documentElement.classList.toggle(
"reduced-effects",
!!s.reducedEffects,
);
cachedSettings = s;
}
async function request<T = any>(