T1: four more themes, theme picker, theme file import, editor font settings

Plum Night, River, Paper and Reed (designed by Amy, approved by Daniel)
join the brand pair. theme.list/current/select/import, a picker with
live preview (view.selectTheme) and "Import Theme…" (view.importTheme);
imported themes are validated and stored in <data_dir>/themes. Editor
font family and line height settings apply live. Docs: docs/themes.md.

Gate: fmt, clippy and the rest pass up to cargo test, where only the
unrelated lutra-terminal PTY test a_full_buffer_stops_the_shell timed
out under load; to be re-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-11-02 19:47:58 +01:00
parent 872b5cb258
commit 086bc8a925
26 changed files with 2881 additions and 100 deletions

View file

@ -8,9 +8,13 @@
* ones.
* - **Errors:** a refused change keeps the error per key until the value
* changes or another change succeeds; the dialog shows it inline.
* - **Effects:** `editor.fontSize` drives `--lt-font-size-editor` (above
* the theme's value, see `setTokenOverride`). `appearance.theme` needs
* nothing here: the engine answers with `themeChanged`.
* - **Effects:** the editor font settings drive theme tokens above the
* theme's value (see `setTokenOverride`), live: `editor.fontSize` →
* `--lt-font-size-editor`, `editor.fontFamily` → `--lt-font-mono` (with
* {@link MONO_FALLBACK} behind it), `editor.lineHeight` →
* `--lt-line-height-editor`. `appearance.theme` needs nothing here: the
* engine answers with `themeChanged`; a change of it reloads the list,
* since an import adds a value (C9 T1).
* - **Text:** labels, descriptions, section names and enum value names are
* catalog keys (`settings.<key>.label`, `settings.<key>.description`,
* `settings.section.<id>`, `settings.<key>.value.<value>`); the engine
@ -38,6 +42,8 @@ export interface SettingDef {
default: SettingValue;
/** Accepted values of an enum; empty otherwise. */
enumValues: string[];
/** Display names of enum values without a catalog key (imported themes), by value. */
valueNames: Record<string, string>;
/** Smallest value of an int, or null. */
min: number | null;
/** Largest value of an int, or null. */
@ -68,6 +74,54 @@ export const FONT_SIZE_KEY = "editor.fontSize";
/** Theme token the editor font size drives. */
export const FONT_SIZE_TOKEN = "--lt-font-size-editor";
/** Key of the editor font family setting. */
export const FONT_FAMILY_KEY = "editor.fontFamily";
/** Theme token the editor font family drives. */
export const FONT_FAMILY_TOKEN = "--lt-font-mono";
/** Fonts behind a chosen family, as `font-mono` in `crates/engine/src/theme.rs`. */
export const MONO_FALLBACK = 'ui-monospace, "Cascadia Code", Menlo, Consolas, monospace';
/** Key of the editor line height setting, in percent of the font size. */
export const LINE_HEIGHT_KEY = "editor.lineHeight";
/** Theme token the editor line height drives. */
export const LINE_HEIGHT_TOKEN = "--lt-line-height-editor";
/** Key of the theme setting. */
export const THEME_KEY = "appearance.theme";
/**
* The token value of an editor font setting, or null for the theme's.
*
* @param key - the setting.
* @param value - its value; unchecked.
* @returns the CSS value, or null when the value is the default or unusable.
*/
export function fontTokenValue(key: string, value: SettingValue | undefined): string | null {
if (key === FONT_SIZE_KEY) {
return typeof value === "number" && Number.isInteger(value) && value > 0 ? `${String(value)}px` : null;
}
if (key === LINE_HEIGHT_KEY) {
return typeof value === "number" && Number.isInteger(value) && value >= 100 && value <= 300
? String(value / 100)
: null;
}
if (key === FONT_FAMILY_KEY) {
const family = typeof value === "string" ? value.trim() : "";
return family === "" || /[;{}<>\\]/.test(family) ? null : `${family}, ${MONO_FALLBACK}`;
}
return null;
}
/** Each editor font setting and the token it drives. */
const FONT_TOKENS: Readonly<Record<string, string>> = {
[FONT_SIZE_KEY]: FONT_SIZE_TOKEN,
[FONT_FAMILY_KEY]: FONT_FAMILY_TOKEN,
[LINE_HEIGHT_KEY]: LINE_HEIGHT_TOKEN,
};
/** Most settings read from `settings.list`; the rest are dropped. */
export const MAX_SETTINGS = 1000;
@ -94,7 +148,7 @@ function toSettingDef(raw: unknown): SettingDef | null {
return null;
}
const entry = raw as Record<string, unknown>;
const { key, type, labelKey, descriptionKey, section, enumValues, min, max } = entry;
const { key, type, labelKey, descriptionKey, section, enumValues, valueNames, min, max } = entry;
if (
typeof key !== "string" ||
typeof labelKey !== "string" ||
@ -110,11 +164,20 @@ function toSettingDef(raw: unknown): SettingDef | null {
if (type === "enum" && values.length === 0) {
return null;
}
const names: Record<string, string> = {};
if (typeof valueNames === "object" && valueNames !== null) {
for (const [value, name] of Object.entries(valueNames as Record<string, unknown>)) {
if (typeof name === "string" && values.includes(value)) {
names[value] = clip(name);
}
}
}
return {
key,
type,
default: toScalar(entry.default),
enumValues: values,
valueNames: names,
min: typeof min === "number" && Number.isFinite(min) ? min : null,
max: typeof max === "number" && Number.isFinite(max) ? max : null,
labelKey,
@ -179,14 +242,15 @@ export function sectionLabel(id: string): string {
/**
* The name of an enum value: `settings.<key>.value.<value>` with the value
* in camel case (`lutra-dark` → `lutraDark`), or the value itself.
* in camel case (`lutra-dark` → `lutraDark`), else the name the engine
* sent (an imported theme), else the value itself.
*
* @param def - the setting.
* @param value - one of its values.
*/
export function valueLabel(def: SettingDef, value: string): string {
const name = value.replace(/[-_.\s]+([A-Za-z0-9])/g, (_m, c: string) => c.toUpperCase());
return textOr(`settings.${def.key}.value.${name}`, value);
return textOr(`settings.${def.key}.value.${name}`, def.valueNames[value] ?? value);
}
/**
@ -259,19 +323,15 @@ export const useSettingsStore = defineStore("settings", () => {
/**
* Applies what a value change means outside the dialog: the editor font
* size token.
* tokens.
*
* @param key - the changed key.
*/
function applyEffect(key: string): void {
if (key !== FONT_SIZE_KEY) {
return;
const token = FONT_TOKENS[key];
if (token !== undefined) {
setTokenOverride(token, fontTokenValue(key, values[key]));
}
const size = values[key];
setTokenOverride(
FONT_SIZE_TOKEN,
typeof size === "number" && Number.isInteger(size) && size > 0 ? `${String(size)}px` : null,
);
}
/**
@ -408,6 +468,10 @@ export const useSettingsStore = defineStore("settings", () => {
for (const key of keys) {
Reflect.deleteProperty(errors, key);
}
if (keys.includes(THEME_KEY)) {
void load();
return;
}
void fetchValues(keys);
}