Give themes named color roles, theme data files, and file format 3
Phase 5a-1 and 5a-2. - roles.ts names 51 color roles. One derivation rule fills the roles that a theme does not give, for all themes. tokens.css keeps only the root defaults, without theme blocks. - Each built-in theme is one data file in src/lib/theme/themes/. No code outside the theme data names a theme ID. characterThemes.ts is gone. - New roles: link, focus, on-accent, highlight, scrim, the syntax roles, and 16 terminal (ANSI) roles. Stray colors in components use the roles. - contrast.ts checks each theme against the readability rules of docs/ui-color.md. The tests mark the known palette failures as todo, and a second test fails when a known failure starts to pass. - Theme files use schema 3 with a mode and an optional built-in base. A short file gets the missing colors and fonts from the base. Schema 1 and 2 files still import. Old syntax names convert on import. - Docs: ui-color.md, plugin-development.md (theme tokens), ui-design.md, guides/themes.md, and UI-041 and UI-042 in design-decisions.md. The applied colors stay the same for every built-in theme, except three small changes: the conflict line uses the warn color, the Write highlight uses the highlight role, and two dialog backdrops use the scrim role. pnpm check passes. pnpm test: 447 pass, 31 todo, 0 fail. Full smoke suite: 93 passed, 2 known. appearance/palettes read the old syntax names and passed 3 of 3 times after the fix. workspace/manageTemplates failed one time and passed on retry.
This commit is contained in:
+44
-95
@@ -1,21 +1,24 @@
|
||||
import { customThemes, FONT_ROLES, type ThemeFonts, type ThemeColors, type CustomTheme } from './customThemes.ts';
|
||||
import { customThemes, FONT_ROLES, type ThemeFonts, type CustomTheme } from './customThemes.ts';
|
||||
import { readBackgroundImage } from './backgroundImages.ts';
|
||||
import { characterTheme, characterSyntax, SYNTAX_ROLES } from './characterThemes.ts';
|
||||
import { readSection, writeSection } from '../settings/store.ts';
|
||||
import { BUILTIN_THEMES as THEME_DATA, DEFAULT_FONTS, DEFAULT_THEME, LEGACY_THEME_IDS, builtinTheme, type BuiltinThemeId } from './themes/index.ts';
|
||||
import { ROLES, resolveColors, type Mode, type ResolvedColors } from './roles.ts';
|
||||
import { appliedColors } from './contrast.ts';
|
||||
/** The old localStorage key. The appearance section of the settings file replaces it. */
|
||||
export const APPEARANCE_KEY = 'runestone.appearance';
|
||||
export const THEMES = ['default', 'claude', 'sakura', 'harvest-moon', 'crimson-pilot', 'silver-serpent'] as const;
|
||||
export const BUILTIN_THEMES = [
|
||||
{ id: 'default', name: 'RuneStone' }, { id: 'claude', name: 'Claude' }, { id: 'sakura', name: 'Sakura' },
|
||||
{ id: 'harvest-moon', name: 'Harvest Moon' }, { id: 'crimson-pilot', name: 'Crimson Pilot' }, { id: 'silver-serpent', name: 'Silver Serpent' },
|
||||
] as const;
|
||||
// Preserve selections saved before the public palette IDs changed.
|
||||
const LEGACY_THEME_IDS: Record<string, typeof THEMES[number]> = { holo: 'harvest-moon', asuka: 'crimson-pilot', malfoid: 'silver-serpent' };
|
||||
export function themeModes(theme: string): readonly ('light' | 'dark')[] {
|
||||
const character = characterTheme(theme);
|
||||
return theme.startsWith('custom:') ? [customThemeMode(customThemes().find(item => `custom:${item.id}` === theme)?.colors)] : character ? [character.mode] : theme === 'sakura' ? ['light'] : ['light', 'dark'];
|
||||
export const THEMES: readonly BuiltinThemeId[] = THEME_DATA.map(theme => theme.id);
|
||||
export const BUILTIN_THEMES: readonly { id: BuiltinThemeId; name: string }[] = THEME_DATA.map(({ id, name }) => ({ id, name }));
|
||||
const MODES = ['light', 'dark'] as const;
|
||||
export function themeModes(theme: string): readonly Mode[] {
|
||||
if (theme.startsWith('custom:')) return [customThemes().find(item => `custom:${item.id}` === theme)?.mode ?? 'dark'];
|
||||
const builtin = builtinTheme(theme);
|
||||
return builtin ? MODES.filter(mode => builtin.variants[mode]) : MODES;
|
||||
}
|
||||
export type ThemeName = typeof THEMES[number] | `custom:${string}`;
|
||||
/** The catalog key of a notice that the Appearance settings show for the theme, if any. */
|
||||
export function themeNotice(theme: string): string | undefined {
|
||||
return builtinTheme(theme)?.notice;
|
||||
}
|
||||
export type ThemeName = BuiltinThemeId | `custom:${string}`;
|
||||
export const BACKGROUND_POSITIONS = ['center', 'top left', 'top', 'top right', 'left', 'right', 'bottom left', 'bottom', 'bottom right'] as const;
|
||||
export const BACKGROUND_SIZES = ['cover', 'contain', 'auto'] as const;
|
||||
export type Appearance = { transparency?: { enabled: boolean; amount: number }; theme: ThemeName; mode: 'light' | 'dark'; newTabBackground?: { image?: string; imageId?: string; overlay?: number; position?: typeof BACKGROUND_POSITIONS[number]; size?: typeof BACKGROUND_SIZES[number] } };
|
||||
@@ -25,7 +28,8 @@ export function normalizeAppearance(value: { theme?: unknown; mode?: unknown; ne
|
||||
const savedTheme = typeof value?.theme === 'string' && Object.hasOwn(LEGACY_THEME_IDS, value.theme) ? LEGACY_THEME_IDS[value.theme] : value?.theme;
|
||||
const theme: ThemeName = typeof savedTheme === 'string' && (/^custom:[a-z][a-z0-9-]{0,63}$/.test(savedTheme) || (THEMES as readonly string[]).includes(savedTheme)) ? savedTheme as ThemeName : 'default';
|
||||
const selectedCustom = theme.startsWith('custom:') ? (themes ?? customThemes(value?.mode === 'light' ? 'light' : 'dark')).find(item => `custom:${item.id}` === theme) : undefined;
|
||||
const result: Appearance = { theme, mode: selectedCustom ? customThemeMode(selectedCustom.colors) : characterTheme(theme)?.mode ?? (theme === 'sakura' || value?.mode === 'light' ? 'light' : 'dark') };
|
||||
const builtinModes = builtinTheme(theme) ? themeModes(theme) : undefined;
|
||||
const result: Appearance = { theme, mode: selectedCustom ? selectedCustom.mode : builtinModes?.length === 1 ? builtinModes[0] : value?.mode === 'light' ? 'light' : 'dark' };
|
||||
if (value?.transparency && typeof value.transparency === 'object') {
|
||||
const source = value.transparency as { enabled?: unknown; amount?: unknown };
|
||||
result.transparency = { enabled: source.enabled !== false, amount: typeof source.amount === 'number' && Number.isFinite(source.amount) ? Math.max(0, Math.min(100, Math.round(source.amount))) : 18 };
|
||||
@@ -51,62 +55,31 @@ export function readAppearance(): Appearance {
|
||||
const value = readSection('appearance');
|
||||
return normalizeAppearance(value && typeof value === 'object' && !Array.isArray(value) ? value : null);
|
||||
}
|
||||
const keys = ['bg', 'bg-elevated', 'bg-active', 'border', 'text-dim', 'text', 'text-strong', 'accent', 'selection'] as const;
|
||||
const palettes = {
|
||||
default: {
|
||||
dark: ['#141414','#1c1c1c','#2a2a2a','#2e2e2e','#9a9a9a','#e6e6e6','#f3f3f3','#82aaff','#264f78'],
|
||||
light: ['#f3f3f3','#ffffff','#e8e8e8','#d4d4d4','#626262','#1e1e1e','#111111','#315fb3','#add6ff'],
|
||||
},
|
||||
// Anthropic brand guidelines: https://github.com/anthropics/skills/blob/main/skills/brand-guidelines/SKILL.md
|
||||
claude: {
|
||||
dark: ['#141413','#20201e','#32322e','#5a5952','#b0aea5','#faf9f5','#faf9f5','#d97757','#674435'],
|
||||
light: ['#faf9f5','#faf9f5','#e8e6dc','#b0aea5','#67665f','#141413','#141413','#d97757','#e8e6dc'],
|
||||
},
|
||||
// Palette from pm-image:7968b79c-9d80-40a5-9e9a-696c26aacfb9. Sakura supports light mode only.
|
||||
sakura: {
|
||||
light: ['#fefcfc','#fefcfc','#dbeacb','#f8dbe6','#687788','#513346','#321c2a','#98556c','#ffc9dc'],
|
||||
},
|
||||
|
||||
};
|
||||
function builtinPalette(theme: ThemeName) {
|
||||
if (theme === 'sakura') return { light: palettes.sakura.light, dark: palettes.sakura.light };
|
||||
const character = characterTheme(theme);
|
||||
if (character) return { light: character.surfaces, dark: character.surfaces };
|
||||
return palettes[theme === 'claude' ? 'claude' : 'default'];
|
||||
/**
|
||||
* Every color role and the font stacks of a theme. A custom theme uses its own mode, and gets missing roles from its base theme.
|
||||
* `legacyMode` selects the palette of a schema 1 custom theme.
|
||||
*/
|
||||
export function resolveTheme(theme: ThemeName, mode: Mode, legacyMode: Mode = mode): { mode: Mode; colors: ResolvedColors; fonts: Required<ThemeFonts> } {
|
||||
const custom = theme.startsWith('custom:') ? customThemes(legacyMode).find(item => `custom:${item.id}` === theme) : undefined;
|
||||
if (custom) {
|
||||
const base = builtinTheme(custom.base ?? DEFAULT_THEME.id)?.variants[custom.mode] ?? DEFAULT_THEME.variants[custom.mode];
|
||||
return { mode: custom.mode, colors: resolveColors(custom.colors, custom.mode, base), fonts: { ...themeFonts(custom.base ?? DEFAULT_THEME.id), ...custom.fonts } };
|
||||
}
|
||||
const builtin = builtinTheme(theme);
|
||||
if (builtin && !builtin.variants[mode]) mode = themeModes(theme)[0];
|
||||
return { mode, colors: resolveColors(builtin?.variants[mode] ?? DEFAULT_THEME.variants[mode], mode), fonts: themeFonts(theme) };
|
||||
}
|
||||
export function customThemeMode(colors?: ThemeColors): 'light' | 'dark' {
|
||||
const hex = colors?.bg?.slice(1) ?? '141414';
|
||||
const full = hex.length <= 4 ? [...hex.slice(0, 3)].map(value => value + value).join('') : hex.slice(0, 6);
|
||||
const rgb = [0, 2, 4].map(index => parseInt(full.slice(index, index + 2), 16) / 255)
|
||||
.map(value => value <= .04045 ? value / 12.92 : ((value + .055) / 1.055) ** 2.4);
|
||||
return rgb[0] * .2126 + rgb[1] * .7152 + rgb[2] * .0722 > .179 ? 'light' : 'dark';
|
||||
}
|
||||
export function exportAppearanceTheme(theme: ThemeName, mode: 'light' | 'dark' = 'dark'): CustomTheme {
|
||||
/** A complete theme file of schema 3, with every role and font stack. */
|
||||
export function exportAppearanceTheme(theme: ThemeName, mode: Mode = 'dark'): CustomTheme {
|
||||
const custom = customThemes(mode).find(item => `custom:${item.id}` === theme);
|
||||
if (custom) mode = customThemeMode(custom.colors);
|
||||
mode = characterTheme(theme)?.mode ?? (theme === 'sakura' ? 'light' : mode);
|
||||
const palette = builtinPalette(theme)[mode];
|
||||
const colors: ThemeColors = Object.fromEntries(keys.map((key, index) => [key, palette[index]]));
|
||||
colors['accent-2'] = characterTheme(theme)?.secondary ?? (theme === 'claude' ? '#6a9bcc' : theme === 'sakura' ? '#607fa9' : colors.accent);
|
||||
const semantic = mode === 'light' ? ['#b42323', '#8a5700', '#806600', '#287b29', '#7740a5'] : ['#f44747', '#d68a00', '#cca700', '#89d185', '#c792ea'];
|
||||
if (theme === 'claude') { semantic[3] = '#788c5d'; semantic[4] = '#6a9bcc'; }
|
||||
if (theme === 'sakura') { semantic[3] = '#618c71'; semantic[4] = '#cb6b91'; }
|
||||
(['error', 'warn-hot', 'warn', 'ok', 'special'] as const).forEach((key, index) => colors[key] = semantic[index]);
|
||||
const syntax = theme === 'sakura' ? ['#cb6b91', '#98556c', '#618c71', '#69a2bd', '#607fa9', '#687788'] : [colors.special, colors.accent, colors.text, colors['accent-2'], colors['accent-2'], colors['text-dim']];
|
||||
(['parameter', 'keyword', 'local', 'static', 'instance', 'global'] as const).forEach((key, index) => colors[`syntax-${key}`] = syntax[index]);
|
||||
Object.assign(colors, { 'syntax-string': colors.ok, 'syntax-number': colors.special, 'syntax-comment': colors['text-dim'], 'syntax-type': colors['accent-2'], 'syntax-function': colors['accent-2'] }, characterSyntax(theme));
|
||||
return { schema: 2, id: custom?.id ?? 'my-theme', name: custom?.name ?? 'My theme', colors: { ...colors, ...custom?.colors }, fonts: { ...themeFonts(theme), ...custom?.fonts } };
|
||||
const resolved = resolveTheme(theme, mode);
|
||||
return {
|
||||
schema: 3, id: custom?.id ?? 'my-theme', name: custom?.name ?? 'My theme', mode: resolved.mode,
|
||||
colors: Object.fromEntries(ROLES.map(role => [role, resolved.colors[role]])), fonts: resolved.fonts,
|
||||
};
|
||||
}
|
||||
export function themeFonts(theme: ThemeName): Required<ThemeFonts> {
|
||||
const ui = '"Segoe UI Variable Text", "Segoe UI", ui-sans-serif, system-ui, sans-serif';
|
||||
const prose = '"Segoe UI Variable Display", "Segoe UI", "Helvetica Neue", ui-sans-serif, system-ui, sans-serif';
|
||||
const mono = '"Cascadia Mono", "Cascadia Code", "JetBrains Mono", "Fira Code", Consolas, ui-monospace, monospace';
|
||||
const fonts = { ui, prose, heading: prose, mono };
|
||||
if (theme === 'claude') return { ...fonts, ui: '"Lora", Georgia, serif', prose: '"Lora", Georgia, serif', heading: '"Poppins", Arial, sans-serif' };
|
||||
if (theme === 'harvest-moon') return { ...fonts, ui: 'Candara, "Trebuchet MS", "Segoe UI", sans-serif', prose: 'Constantia, Georgia, "Times New Roman", serif', heading: 'Constantia, Georgia, "Times New Roman", serif' };
|
||||
if (theme === 'crimson-pilot') return { ...fonts, ui: 'Bahnschrift, "Trebuchet MS", Arial, sans-serif', prose: '"Segoe UI", "Helvetica Neue", Arial, sans-serif', heading: 'Bahnschrift, "Trebuchet MS", Arial, sans-serif' };
|
||||
if (theme === 'silver-serpent') return { ...fonts, ui: 'Corbel, "Segoe UI", "Helvetica Neue", Arial, sans-serif', prose: '"Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif', heading: '"Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif' };
|
||||
return fonts;
|
||||
export function themeFonts(theme: string): Required<ThemeFonts> {
|
||||
return { ...DEFAULT_FONTS, ...builtinTheme(theme)?.fonts };
|
||||
}
|
||||
export function applyTransparency(value: Appearance['transparency'], root: HTMLElement) {
|
||||
const supplied = value?.amount ?? 18;
|
||||
@@ -168,8 +141,7 @@ function applyBackgroundImage(root: HTMLElement, image?: string) {
|
||||
export function applyAppearance(value: Appearance, root = document.documentElement) {
|
||||
const normalized = normalizeAppearance(value);
|
||||
const { theme, newTabBackground } = normalized;
|
||||
const custom = theme.startsWith('custom:') ? customThemes(normalized.mode).find(item => item.id === theme.slice(7)) : undefined;
|
||||
const mode = custom ? customThemeMode(custom.colors) : normalized.mode;
|
||||
const { mode, colors, fonts } = resolveTheme(theme, normalized.mode);
|
||||
applyTransparency(normalized.transparency, root);
|
||||
root.style.setProperty('--rs-new-tab-bg', 'var(--rs-bg)');
|
||||
const overlay = newTabBackground?.overlay ?? 0;
|
||||
@@ -182,33 +154,10 @@ export function applyAppearance(value: Appearance, root = document.documentEleme
|
||||
root.dataset.theme = mode;
|
||||
root.dataset.palette = theme;
|
||||
root.style.colorScheme = mode;
|
||||
const fonts = { ...themeFonts(theme), ...custom?.fonts };
|
||||
FONT_ROLES.forEach(role => root.style.setProperty(`--rs-font-${role}`, fonts[role]));
|
||||
const palette = builtinPalette(theme)[mode];
|
||||
keys.forEach((key, index) => root.style.setProperty(`--rs-${key}`, palette[index]));
|
||||
root.style.setProperty('--rs-accent-2', characterTheme(theme)?.secondary ?? (theme === 'claude' ? '#6a9bcc' : theme === 'sakura' ? '#607fa9' : 'var(--rs-accent)'));
|
||||
root.style.setProperty('--rs-accent-dim', 'color-mix(in srgb, var(--rs-accent) 80%, var(--rs-bg))');
|
||||
root.style.setProperty('--rs-accent-deep', 'color-mix(in srgb, var(--rs-accent) 60%, var(--rs-bg))');
|
||||
const semantic = mode === 'light' ? ['#b42323', '#8a5700', '#806600', '#287b29', '#7740a5']
|
||||
: ['#f44747', '#d68a00', '#cca700', '#89d185', '#c792ea'];
|
||||
if (theme === 'claude') { semantic[3] = '#788c5d'; semantic[4] = '#6a9bcc'; }
|
||||
if (theme === 'sakura') { semantic[3] = '#618c71'; semantic[4] = '#cb6b91'; }
|
||||
['error', 'warn-hot', 'warn', 'ok', 'special'].forEach((key, index) => root.style.setProperty(`--rs-${key}`, semantic[index]));
|
||||
SYNTAX_ROLES.forEach(role => root.style.removeProperty(`--rs-syntax-${role}`));
|
||||
const syntax = theme === 'sakura'
|
||||
? ['#cb6b91', '#98556c', '#618c71', '#69a2bd', '#607fa9', '#687788']
|
||||
: ['var(--rs-special)', 'var(--rs-accent)', 'var(--rs-text)', 'var(--rs-accent-2)', 'var(--rs-accent-2)', 'var(--rs-text-dim)'];
|
||||
['parameter', 'keyword', 'local', 'static', 'instance', 'global'].forEach((key, index) => root.style.setProperty(`--rs-syntax-${key}`, syntax[index]));
|
||||
for (const [key, color] of Object.entries(characterSyntax(theme))) root.style.setProperty(`--rs-${key}`, color);
|
||||
root.style.setProperty('--rs-danger', 'var(--rs-error)');
|
||||
if (custom) for (const [key, color] of Object.entries(custom.colors)) root.style.setProperty(`--rs-${key}`, color);
|
||||
// Keep the theme hue while exposing the background beneath selected text.
|
||||
const selection = root.style.getPropertyValue('--rs-selection').trim().slice(1);
|
||||
const fullSelection = selection.length <= 4 ? [...selection].map(part => part + part).join('') : selection;
|
||||
if (/^[0-9a-f]{6}([0-9a-f]{2})?$/i.test(fullSelection)) {
|
||||
const alpha = Math.min(166, fullSelection.length === 8 ? parseInt(fullSelection.slice(6), 16) : 255);
|
||||
root.style.setProperty('--rs-selection', `#${fullSelection.slice(0, 6)}${alpha.toString(16).padStart(2, '0')}`);
|
||||
}
|
||||
// Text selection keeps the theme hue and shows the background beneath it.
|
||||
const applied = appliedColors(colors);
|
||||
for (const role of ROLES) root.style.setProperty(`--rs-${role}`, applied[role]);
|
||||
window.dispatchEvent(new CustomEvent('runestone-theme-changed'));
|
||||
}
|
||||
/** Save and apply an appearance. Throws at once if browser storage refuses it. The promise reports a failed save. */
|
||||
|
||||
Reference in New Issue
Block a user