Phase 5a-5. - Settings → Appearance shows a preview card for each theme. The cards are one radio group. The arrow keys move and select, and the current card has the selection outline and a check mark (UI-047). - Edit theme opens the theme editor as a workbench tab in the window that opened Settings. The window shows each change at once. - The editor shows all 51 color roles in seven groups. Each role has a color picker, a hex field, its lowest readability result, and a reset button. A failure shows a warning icon and its ratio, or "Too close". - Save writes a short schema 3 theme with the built-in theme as its base. A built-in theme does not change. Discard returns to the saved colors, and Duplicate saves a copy. - The editor is a built-in package, like the browser, so it uses the editor tab code of plugins. - 23 new catalog rows with French, Japanese, and Simplified Chinese. - The smoke harness sends emit to every page and emit_to to the pages with the target window label. - Docs: UI-047, ui-design.md, and "Edit a theme" in guides/themes.md. New tests: appearance/themeEditor and tests/unit/theme/themeEditor.spec.ts. Six smoke tests use the cards instead of the theme menu.
80 lines
4.7 KiB
TypeScript
80 lines
4.7 KiB
TypeScript
// The model of the theme editor: role groups, the draft of one theme, and the readability results of each role.
|
|
import { message } from '../i18n/index.ts';
|
|
import { ANSI_COLORS, SYNTAX_ROLES, resolveColors, type Mode, type ResolvedColors, type Role, type ThemeColors } from './roles.ts';
|
|
import { checkTheme, type Finding, type Surface } from './contrast.ts';
|
|
import { builtinTheme, type BuiltinThemeId } from './themes/index.ts';
|
|
import type { CustomTheme, ThemeFonts } from './customThemes.ts';
|
|
|
|
/** The editor shows every role once, in these groups. */
|
|
export const ROLE_GROUPS: readonly { label: string; roles: readonly Role[] }[] = [
|
|
{ label: message('Surfaces'), roles: ['bg', 'bg-elevated', 'bg-active', 'border', 'line-highlight', 'scrim'] },
|
|
{ label: message('Text'), roles: ['text', 'text-dim', 'text-strong'] },
|
|
{ label: message('Accents'), roles: ['accent', 'accent-2', 'accent-dim', 'accent-deep', 'link', 'focus', 'on-accent'] },
|
|
{ label: message('Selection and highlight'), roles: ['selection', 'item-selection-bg', 'highlight'] },
|
|
{ label: message('Status'), roles: ['error', 'warn', 'warn-hot', 'ok', 'special'] },
|
|
{ label: message('Code'), roles: SYNTAX_ROLES.map(role => `syntax-${role}` as const) },
|
|
{ label: message('Terminal'), roles: ANSI_COLORS.flatMap(color => [`ansi-${color}`, `ansi-bright-${color}`] as const) },
|
|
];
|
|
|
|
/** One theme while the editor changes it. `id` is null for a built-in theme, which Save copies to a custom theme. */
|
|
export type ThemeDraft = { id: string | null; name: string; mode: Mode; base: BuiltinThemeId; overrides: ThemeColors; fonts?: ThemeFonts };
|
|
|
|
export function builtinDraft(id: BuiltinThemeId, mode: Mode, name: string): ThemeDraft {
|
|
return { id: null, name, mode, base: id, overrides: {} };
|
|
}
|
|
|
|
export function customDraft(theme: CustomTheme): ThemeDraft {
|
|
return { id: theme.id, name: theme.name, mode: theme.mode, base: (theme.base ?? 'default') as BuiltinThemeId, overrides: { ...theme.colors }, ...(theme.fonts ? { fonts: { ...theme.fonts } } : {}) };
|
|
}
|
|
|
|
/** Every role of the draft: its own colors, then the base theme, then the derivation rule. */
|
|
export function draftColors(draft: ThemeDraft): ResolvedColors {
|
|
return resolveColors(draft.overrides, draft.mode, builtinTheme(draft.base)?.variants[draft.mode] ?? {});
|
|
}
|
|
|
|
/** The schema 3 theme file of a draft. It keeps only the changed colors, and the base gives the others. */
|
|
export function draftTheme(draft: ThemeDraft, id: string, name: string): CustomTheme {
|
|
return { schema: 3, id, name, mode: draft.mode, base: draft.base, colors: { ...draft.overrides }, ...(draft.fonts ? { fonts: draft.fonts } : {}) };
|
|
}
|
|
|
|
/** A theme ID from a name, different from every ID in `taken`. */
|
|
export function uniqueThemeId(name: string, taken: Iterable<string>): string {
|
|
const used = new Set(taken);
|
|
const slug = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
|
// An ID starts with a letter.
|
|
const stem = (!slug ? 'theme' : /^[a-z]/.test(slug) ? slug : `t-${slug}`).slice(0, 56).replace(/-+$/, '');
|
|
for (let index = 1; ; index++) {
|
|
const id = index === 1 ? stem : `${stem}-${index}`;
|
|
if (!used.has(id)) return id;
|
|
}
|
|
}
|
|
|
|
/** The surfaces that a role paints. A rule that measures text on one of them counts for the role. */
|
|
const SURFACE_ROLES: Partial<Record<Role, readonly Surface[]>> = {
|
|
bg: ['bg'], 'bg-elevated': ['bg-elevated'], 'bg-active': ['bg-active'], selection: ['selection'], 'line-highlight': ['line-highlight'],
|
|
highlight: ['highlight'], 'item-selection-bg': ['item-selection', 'match'],
|
|
};
|
|
|
|
/** The readability results that a role takes part in, as a foreground, a surface, or a pair. */
|
|
export function roleFindings(role: Role, findings: readonly Finding[]): Finding[] {
|
|
const surfaces = SURFACE_ROLES[role] ?? [];
|
|
return findings.filter(finding => {
|
|
const subject = finding.subject;
|
|
if (subject.startsWith(`${role} on `) || subject.startsWith(`${role} and `) || subject.endsWith(` and ${role}`)) return true;
|
|
if (subject.startsWith(`${role} lighter than `) || subject.endsWith(` lighter than ${role}`)) return true;
|
|
return surfaces.some(surface => finding.subject.endsWith(` on ${surface}`));
|
|
});
|
|
}
|
|
|
|
/** The lowest contrast result of a role, and every result that fails. */
|
|
export function roleStatus(role: Role, findings: readonly Finding[]) {
|
|
const own = roleFindings(role, findings);
|
|
const ratios = own.filter(finding => finding.min > 1);
|
|
const lowest = ratios.length ? ratios.reduce((low, finding) => finding.value / finding.min < low.value / low.min ? finding : low) : undefined;
|
|
return { lowest, failures: own.filter(finding => !finding.pass) };
|
|
}
|
|
|
|
export function draftFindings(draft: ThemeDraft): Finding[] {
|
|
return checkTheme(draftColors(draft), draft.mode);
|
|
}
|