Skip to main content

Module theme_editor

Module theme_editor 

Source
Expand description

The theme editor, as a plugin (ThemeEditorPlugin): a theme’s tokens edited live, grouped by role, each colour with its picker and the contrast of the pairs it takes part in, started from any built-in theme, exported and imported as a token file, every change undoable.

Experimental: may change in any release (docs/stability.md).

show draws the editor in any Ui (a panel, a window, a settings page); the state lives in the plugin’s service (THEME_EDITOR), over the theme service: each change is written to the application’s theme (its base and overrides) and installed before the next frame. When the theme changes elsewhere (a theme switcher), the editor starts again from it.

A theme with a light and a dark variant is edited as a pair: the draft holds both, each with its own changes and history, and the variant edited is the one shown. Switching between light and dark (the editor’s own switch, ThemeEditor::switch_variant, the application’s toggle, or the system’s mode when the preference follows it) edits the other one. Both stay installed, and a pair is saved as a new pair, one file carrying both. “Generate the other variant” (ThemeEditor::propose_counterpart, ThemeEditor::apply_counterpart) computes the other variant from the one edited, shown before it is applied.

The model is ThemeDraft (no egui): a base theme, the tokens changed over it and their history. A file is an ordinary .tokens.json that $extends the base.

StepArgumentsDefault
theme.tokenpath, from, to (raw DTCG values, null for the base theme’s), cause (edit, reset, undo, redo, agent)writes to
theme.loadbase, overrides, counterpart ({base, overrides}, or null for a theme alone), cause (start, import)loads them (the history starts again)

A before-hook vetoes a change (the token keeps from) or redirects it (another to, checked again); a run-hook’s result is the value written. A colour dragged in its picker is shown live and becomes one change when the pointer is released; a value typed in becomes one when its field loses the focus.

Commands, for agents (hidden from the palette): theme.read (the draft, or one token with its contrast readouts), theme.set (path, value, null for the base theme’s), theme.undo, theme.redo (the draft’s history is also a local scope, SCOPE: while the editor has the focus, Edit > Undo and Ctrl+Z undo its changes), theme.start (base), theme.export (name), theme.import (text), theme.generate (scale, seed: a colour scale generated from a seed, ThemeEditor::generate), theme.switch (the pair’s other variant edited) and theme.counterpart (the other variant generated from the one edited; proposed, or written with apply).

Owned: egui_thematic (MIT) edits egui’s Visuals, not tokens with aliases and contrast pairs; egui’s colour picker (MIT OR Apache-2.0) is adopted for colours; egui_colors (MIT) generates palettes, and its approach is re-derived in egui_pop_tokens::palette (see docs/theme-editor.md).

use egui_pop_api::theme_editor::{THEME_EDITOR, ThemeEditorPlugin, Cause};
use egui_pop_api::App;

let app = App::builder()
    .plugin(ThemeEditorPlugin)
    .build(&egui::Context::default())
    .unwrap();
let handle = app.handle();
let editor = handle.service(&THEME_EDITOR).unwrap();
editor
    .set(handle, "accent.default", Some(serde_json::json!("#2060c0")), Cause::Agent)
    .unwrap();
assert_eq!(handle.theme().spec().overrides.len(), 1);
editor.undo(handle, Cause::Agent).unwrap();
assert!(handle.theme().spec().overrides.is_empty());

Structs§

Readout
One contrast pair a token takes part in, measured.
ThemeEditor
The theme editor’s state, shared by its view and its commands.
ThemeEditorPlugin
Declares the editor’s service, steps and agent commands.

Enums§

Cause
Why a token changes, as the theme.token step says it.
Tab
What the editor shows: the tokens, or the fonts.

Constants§

CMD_THEME_COUNTERPART
CMD_THEME_EXPORT
CMD_THEME_GENERATE
CMD_THEME_IMPORT
CMD_THEME_READ
CMD_THEME_REDO
CMD_THEME_SET
CMD_THEME_START
CMD_THEME_SWITCH
CMD_THEME_UNDO
SCOPE
The editor’s history, a local scope (ADR 0014).
STEP_THEME_LOAD
The editor starts from a theme, or from a file.
STEP_THEME_TOKEN
A token changes.
SURFACE
The focus surface the editor is drawn in (themeEditorFocus): its keys listen there, and its draft is the local scope Ctrl+Z undoes in while it has the focus.
THEME_EDITOR
The editor’s state, as a service.
THEME_EDITOR_PLUGIN
The plugin’s id.

Functions§

contrast_of
The contrast pairs a token takes part in, itself or through the tokens aliasing it, measured in tokens.
dependents
The tokens whose value comes from path: itself, and every token aliasing it, directly or not.
font_sample
A sample line per role, interface then monospace, at the body size, in the fonts installed or, in egui_pop_core::fonts::with_preview, in the preview families: the fonts tab’s sample, also the settings window’s Fonts page’s.
level_of
The level a theme is checked at: AAA text (7:1) for a high-contrast theme, AA otherwise.
sample
The preview pane’s default content: text by role, the status colours, buttons in their tones and states, a checkbox, options, a slider, a field, a selected row and a progress bar, drawn in the theme of the Ui (a ScopedTheme’s in the editor).
scale_of
The colour scale a reference token is a step of (blue for blue.3): a scale with a step 9.
show
Draws the theme editor (it needs ThemeEditorPlugin).

Type Aliases§

FileDialog
A file chooser the host gives the editor (ThemeEditor::set_file_dialog): it starts a choice and answers later, as files::Files::choose does ({ "paths": [...] }, or null when cancelled).
Sampler
What the preview pane draws, in the draft’s theme (ThemeEditor::set_sampler).