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

@ -50,7 +50,7 @@ export const lutraEditorTheme: Extension = EditorView.theme({
},
".cm-scroller": {
fontFamily: "var(--lt-font-mono)",
lineHeight: "1.5",
lineHeight: "var(--lt-line-height-editor)",
},
".cm-content": {
caretColor: "var(--lt-editor-cursor)",

View file

@ -0,0 +1,285 @@
<script setup lang="ts">
/**
* The theme picker (C9 T1): "Select Theme…" lists the built-in and imported
* themes with a swatch of each (window, editor, accent, keyword and string
* colour) and "Same as system". Up/Down, Home and End move the selection
* and preview the theme live; Enter or a click switches to it; Escape or a
* click outside puts the previous theme back. Also supplies "Import
* Theme…". An exclusive modal (`commands/modals.ts`).
*/
import { Monitor } from "@lucide/vue";
import { nextTick, onMounted, onUnmounted, ref, watch } from "vue";
import { useModalsStore } from "../../commands/modals";
import { useCommandsStore } from "../../commands/store";
import { t } from "../../i18n";
import { useThemesStore, type ThemeInfo } from "./themes";
const themes = useThemesStore();
const modals = useModalsStore();
const commands = useCommandsStore();
/** The list element, which holds the focus. */
const list = ref<HTMLElement | null>(null);
/** Element that had the focus before the picker opened. */
let returnFocus: HTMLElement | null = null;
/** Withdraws the UI commands; set while mounted. */
let withdraw: (() => void)[] = [];
/** Swatch tokens, in the order they are drawn. */
const SWATCH = ["--lt-bg", "--lt-editor-bg", "--lt-accent", "--lt-syntax-keyword", "--lt-syntax-string"] as const;
/**
* The swatch colours of a theme.
*
* @param info - the theme.
* @returns CSS colours; empty for "Same as system".
*/
function swatch(info: ThemeInfo): string[] {
const tokens = info.theme?.tokens ?? {};
return SWATCH.map((name) => tokens[name] ?? "transparent");
}
/**
* The second line of a row: dark or light, and whether it was imported.
*
* @param info - the theme.
*/
function kindOf(info: ThemeInfo): string {
if (info.dark === null) {
return t("theme.picker.followsSystem");
}
const mode = info.dark ? t("theme.picker.dark") : t("theme.picker.light");
return info.builtin ? mode : t("theme.picker.imported", { mode });
}
/**
* Keyboard handling of the list.
*
* @param event - the key press.
*/
function onKeydown(event: KeyboardEvent): void {
const count = themes.items.length;
const moves: Record<string, number> = { ArrowDown: 1, ArrowUp: -1, Home: -count, End: count };
const delta = moves[event.key];
if (delta !== undefined) {
themes.move(delta);
scrollSelected();
} else if (event.key === "Enter") {
void themes.choose();
} else if (event.key === "Escape") {
themes.cancel();
} else {
return;
}
event.preventDefault();
event.stopPropagation();
}
/** Keeps the selected row in view. */
function scrollSelected(): void {
void nextTick(() => {
const row = list.value?.querySelector<HTMLElement>(`#lt-theme-${String(themes.selected)}`);
if (row && typeof row.scrollIntoView === "function") {
row.scrollIntoView({ block: "nearest" });
}
});
}
watch(
() => themes.open,
(open) => {
if (open) {
const active = document.activeElement;
returnFocus = active instanceof HTMLElement ? active : null;
modals.opened("themes", () => {
themes.cancel();
});
void nextTick(() => {
list.value?.focus();
scrollSelected();
});
} else {
modals.closed("themes");
returnFocus?.focus();
returnFocus = null;
}
},
);
onMounted(() => {
withdraw = [
commands.provide("view.selectTheme", { run: () => themes.show() }),
commands.provide("view.importTheme", { run: () => themes.importFile() }),
];
});
onUnmounted(() => {
for (const stop of withdraw) {
stop();
}
withdraw = [];
});
</script>
<template>
<div
v-if="themes.open"
class="theme-picker"
data-test="theme-picker"
@mousedown.self.prevent="themes.cancel()"
>
<div
class="theme-picker__dialog"
role="dialog"
aria-modal="true"
:aria-label="t('theme.picker.title')"
>
<p class="theme-picker__title">
{{ t("theme.picker.title") }}
</p>
<ul
ref="list"
class="theme-picker__list"
role="listbox"
tabindex="0"
:aria-label="t('theme.picker.title')"
:aria-activedescendant="`lt-theme-${String(themes.selected)}`"
data-test="theme-picker-list"
@keydown="onKeydown"
>
<li
v-for="(item, index) in themes.items"
:id="`lt-theme-${String(index)}`"
:key="item.id"
class="theme-picker__row"
:class="{ 'theme-picker__row--selected': index === themes.selected }"
role="option"
:aria-selected="index === themes.selected"
:data-id="item.id"
data-test="theme-picker-row"
@mousemove="themes.hover(index)"
@mousedown.prevent
@click="themes.choose(index)"
>
<span
v-if="item.theme"
class="theme-picker__swatch"
aria-hidden="true"
>
<span
v-for="(colour, part) in swatch(item)"
:key="part"
class="theme-picker__chip"
:style="{ background: colour }"
/>
</span>
<Monitor
v-else
class="theme-picker__icon"
:size="14"
aria-hidden="true"
/>
<span class="theme-picker__name">{{ item.name }}</span>
<span class="theme-picker__kind">{{ kindOf(item) }}</span>
<span
v-if="item.id === themes.initialId"
class="theme-picker__current"
>{{ t("theme.picker.current") }}</span>
</li>
</ul>
</div>
</div>
</template>
<style scoped>
.theme-picker {
position: fixed;
inset: 0;
display: flex;
align-items: flex-start;
justify-content: center;
padding-top: 12vh;
background: color-mix(in srgb, var(--lt-bg) 40%, transparent);
z-index: 30;
}
.theme-picker__dialog {
width: min(480px, 90vw);
max-height: 60vh;
display: flex;
flex-direction: column;
background: var(--lt-bg);
color: var(--lt-fg);
border: 1px solid var(--lt-border);
border-radius: var(--lt-radius, 4px);
overflow: hidden;
}
.theme-picker__title {
margin: 0;
padding: var(--lt-space-s, 4px) var(--lt-space-m, 8px);
font-weight: 600;
border-bottom: 1px solid var(--lt-border);
}
.theme-picker__list {
margin: 0;
padding: var(--lt-space-s, 4px) 0;
list-style: none;
overflow-y: auto;
outline: none;
}
.theme-picker__row {
display: flex;
align-items: center;
gap: var(--lt-space-m, 8px);
padding: 5px var(--lt-space-m, 8px);
white-space: nowrap;
cursor: default;
}
.theme-picker__row--selected {
background: var(--lt-editor-selection-bg);
}
.theme-picker__swatch {
display: inline-flex;
flex-shrink: 0;
border: 1px solid var(--lt-border);
border-radius: 3px;
overflow: hidden;
}
.theme-picker__chip {
width: 10px;
height: 14px;
}
.theme-picker__icon {
flex-shrink: 0;
width: 52px;
color: var(--lt-fg-muted);
}
.theme-picker__name {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
}
.theme-picker__kind,
.theme-picker__current {
flex-shrink: 0;
color: var(--lt-fg-muted);
font-size: var(--lt-font-size-small);
}
.theme-picker__row--selected .theme-picker__kind,
.theme-picker__row--selected .theme-picker__current {
color: var(--lt-fg);
}
</style>

View file

@ -0,0 +1,232 @@
/**
* The theme picker and theme import (C9 T1).
*
* - **Picker:** "Select Theme…" (`view.selectTheme`) lists the built-in
* and imported themes from `theme.list`, plus "Same as system". Moving
* the selection previews the theme live (its tokens go onto `:root`
* through `applyTheme`); Enter or a click switches to it with
* `theme.select`; Escape puts the theme in use back. An exclusive modal
* (`commands/modals.ts`).
* - **Import:** "Import Theme…" (`view.importTheme`) asks for a theme file
* (format: `docs/themes.md`) and runs `theme.import`, which stores the
* theme in Lutra's data directory and switches to it. A broken file
* gives an error toast with the engine's reason.
*/
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { hasMessage, t } from "../../i18n";
import { pickFile, run } from "../../ipc";
import { useNotificationsStore } from "../../stores/notifications";
import { applyTheme, toResolvedTheme, type ResolvedTheme } from "../../theme";
/** The `appearance.theme` value that follows the OS. */
export const SYSTEM = "system";
/** Most themes read from `theme.list`. */
export const MAX_THEMES = 500;
/** A theme as the picker lists it. */
export interface ThemeInfo {
/** Theme id, or {@link SYSTEM}. */
id: string;
/** Display name: catalog text for built-in themes, the file's name for imported ones. */
name: string;
/** Whether the theme is dark; null for {@link SYSTEM}. */
dark: boolean | null;
/** Whether Lutra ships it. */
builtin: boolean;
/** Its tokens for the preview; null for {@link SYSTEM}. */
theme: ResolvedTheme | null;
}
/**
* Reads one `theme.list` entry from untrusted data.
*
* @param raw - the entry.
* @returns the theme, or null if it is malformed.
*/
export function toThemeInfo(raw: unknown): ThemeInfo | null {
const theme = toResolvedTheme(raw);
if (!theme) {
return null;
}
const { label, name, builtin } = raw as Record<string, unknown>;
const shown =
typeof label === "string" && hasMessage(label)
? t(label)
: typeof name === "string" && name.trim() !== ""
? name.trim()
: theme.id;
return { id: theme.id, name: shown, dark: theme.dark, builtin: builtin === true, theme };
}
/**
* Reads the `theme.list` result and appends "Same as system".
*
* @param raw - the result.
* @returns the themes, or null if the result is not a list.
*/
export function toThemeList(raw: unknown): ThemeInfo[] | null {
if (!Array.isArray(raw)) {
return null;
}
const themes = (raw as unknown[])
.slice(0, MAX_THEMES)
.map(toThemeInfo)
.filter((info): info is ThemeInfo => info !== null);
themes.push({
id: SYSTEM,
name: t("settings.appearance.theme.value.system"),
dark: null,
builtin: true,
theme: null,
});
return themes;
}
/** Store of the theme picker. */
export const useThemesStore = defineStore("themes", () => {
const notifications = useNotificationsStore();
/** Themes in picker order; empty while closed. */
const items = ref<ThemeInfo[]>([]);
/** Index of the selected row. */
const selected = ref(0);
/** Whether the picker is shown. */
const open = ref(false);
/** Id of the theme setting when the picker opened. */
const initialId = ref<string | null>(null);
/** The theme on screen when the picker opened, to restore on Escape. */
let initialTheme: ResolvedTheme | null = null;
/** The selected theme, or null. */
const current = computed(() => items.value[selected.value] ?? null);
/**
* Shows a theme's tokens without switching to it. "Same as system" shows
* the theme in use when the picker opened.
*
* @param info - the theme.
*/
function preview(info: ThemeInfo | null): void {
const theme = info?.theme ?? initialTheme;
if (theme) {
applyTheme(theme);
}
}
/** Opens the picker with the current theme selected. A failed load raises an error toast. */
async function show(): Promise<void> {
const [listed, setting, now] = await Promise.all([
run("theme.list"),
run("settings.get", { keys: ["appearance.theme"] }),
run("theme.current"),
]);
if (!listed.ok) {
notifications.notifyError("theme.list", listed.error);
return;
}
const list = toThemeList(listed.value);
if (!list) {
return;
}
const values = setting.ok ? (setting.value as { values?: Record<string, unknown> } | null)?.values : undefined;
const id = typeof values?.["appearance.theme"] === "string" ? values["appearance.theme"] : null;
initialId.value = id;
initialTheme = now.ok ? toResolvedTheme(now.value) : null;
items.value = list;
selected.value = Math.max(0, list.findIndex((info) => info.id === id));
open.value = true;
}
/**
* Moves the selection and previews the theme there.
*
* @param delta - rows to move; clamped to the list.
*/
function move(delta: number): void {
const count = items.value.length;
if (count === 0) {
return;
}
selected.value = Math.min(count - 1, Math.max(0, selected.value + delta));
preview(current.value);
}
/**
* Selects a row by index and previews it.
*
* @param index - the row.
*/
function hover(index: number): void {
if (index >= 0 && index < items.value.length && index !== selected.value) {
selected.value = index;
preview(current.value);
}
}
/** Closes the picker and puts the theme in use back. */
function cancel(): void {
if (!open.value) {
return;
}
open.value = false;
if (initialTheme) {
applyTheme(initialTheme);
}
}
/**
* Switches to a theme with `theme.select` and closes the picker. A
* failure puts the previous theme back and raises an error toast.
*
* @param index - the row; the selected one when absent.
*/
async function choose(index?: number): Promise<void> {
const info = items.value[index ?? selected.value];
if (!open.value || !info) {
return;
}
open.value = false;
const result = await run("theme.select", { id: info.id });
if (!result.ok) {
if (initialTheme) {
applyTheme(initialTheme);
}
notifications.notifyError("theme.select", result.error);
return;
}
const theme = toResolvedTheme(result.value);
if (theme) {
applyTheme(theme);
}
}
/**
* Asks for a theme file and imports it with `theme.import`. Cancelling
* the file dialog does nothing; success shows an info toast with the
* theme's name; a broken file shows the engine's reason.
*/
async function importFile(): Promise<void> {
const picked = await pickFile(t("theme.import.title"));
if (!picked.ok) {
notifications.notifyError("theme.import", picked.error);
return;
}
if (picked.value === null) {
return;
}
const result = await run("theme.import", { path: picked.value });
if (!result.ok) {
notifications.notifyError("theme.import", result.error);
return;
}
const name = (result.value as { name?: unknown } | null)?.name;
notifications.notify({
level: "info",
messageKey: "theme.import.done",
params: { name: typeof name === "string" ? name : picked.value },
});
}
return { items, selected, open, initialId, current, show, move, hover, cancel, choose, importFile };
});