pub struct ThemeDraft { /* private fields */ }Expand description
A theme being edited: one theme, or a light and dark pair.
Its methods read and change the variant edited: its base, its
overrides, its history and its seeds. A pair’s other variant keeps
its own (ThemeDraft::counterpart) until ThemeDraft::switch
makes it the one edited.
use egui_pop_tokens::{ThemeDraft, Themes};
use serde_json::json;
let themes = Themes::default();
let mut draft = ThemeDraft::for_theme(&themes, "pop-dark");
assert_eq!(draft.counterpart().map(ThemeDraft::base), Some("pop-light"));
let edit = draft.edit(&themes, "danger", Some(json!("#ff4040"))).unwrap();
draft.apply(edit.unwrap());
draft.switch();
assert_eq!(draft.base(), "pop-light");
assert!(draft.overrides().is_empty());
assert!(!draft.can_undo(), "each variant its own history");
let edit = draft.edit(&themes, "danger", Some(json!("#c00000"))).unwrap();
draft.apply(edit.unwrap());
// Installed as a pair, each variant with its own overrides.
let spec = draft.pair_spec(&Default::default());
assert_eq!(spec.base, "pop-dark");
assert_eq!(spec.overrides["danger"], json!("#ff4040"));
assert_eq!(spec.counterpart_overrides.unwrap()["danger"], json!("#c00000"));Implementations§
Source§impl ThemeDraft
impl ThemeDraft
Sourcepub fn new(base: impl Into<String>) -> ThemeDraft
pub fn new(base: impl Into<String>) -> ThemeDraft
A draft over a base theme, nothing changed yet.
Sourcepub fn from_spec(spec: &ThemeSpec) -> ThemeDraft
pub fn from_spec(spec: &ThemeSpec) -> ThemeDraft
A draft from a spec: its base and its overrides, no history; the
base alone (see ThemeDraft::from_pair_spec for its pair).
Sourcepub fn pair(first: impl Into<String>, second: impl Into<String>) -> ThemeDraft
pub fn pair(first: impl Into<String>, second: impl Into<String>) -> ThemeDraft
A draft over a light and dark pair, nothing changed in either:
first the theme named first (shown when nothing asks for the
other mode), edited first; second its counterpart.
Sourcepub fn for_theme(themes: &Themes, base: &str) -> ThemeDraft
pub fn for_theme(themes: &Themes, base: &str) -> ThemeDraft
A draft over a theme and, when it has one of the other mode, its
counterpart (Themes::counterpart_of), nothing changed: what
“start from a theme” gives.
Sourcepub fn from_pair_spec(themes: &Themes, spec: &ThemeSpec) -> ThemeDraft
pub fn from_pair_spec(themes: &Themes, spec: &ThemeSpec) -> ThemeDraft
A draft from a spec and its counterpart (Themes::counterpart),
no history: the base edited, with the spec’s overrides; the
counterpart with its own overrides, or the spec’s when it has
none of its own (ThemeSpec::counterpart_overrides). A
counterpart of the same mode as the base is not one: the draft is
the base alone.
Sourcepub fn counterpart(&self) -> Option<&ThemeDraft>
pub fn counterpart(&self) -> Option<&ThemeDraft>
The pair’s other variant, as it is: its base, overrides, history
and seeds. None for a theme alone.
Sourcepub fn editing_second(&self) -> bool
pub fn editing_second(&self) -> bool
Whether the variant edited is the pair’s second (the counterpart of the theme named first).
Sourcepub fn variants(&self) -> (&ThemeDraft, Option<&ThemeDraft>)
pub fn variants(&self) -> (&ThemeDraft, Option<&ThemeDraft>)
The pair’s variants: the theme named first, then its counterpart
(None for a theme alone). Read their base, overrides, history
and seeds.
Sourcepub fn generate_counterpart(&self, themes: &Themes) -> Option<GeneratedVariant>
pub fn generate_counterpart(&self, themes: &Themes) -> Option<GeneratedVariant>
The pair’s other variant as generated from the one edited, so
that it becomes its counterpart: its base with the edited
variant’s changes carried over, its own changes not among them
taken back. None for a theme alone. Deterministic, and nothing
is changed: record GeneratedVariant::edits on the other
variant (after ThemeDraft::switch) to apply it.
How each change is carried over:
- a colour value: mirrored (
crate::palette::mirror, the same OkHsl hue and saturation, the lightness1 - l); - an alias (
{blue.9}): as it is, so it names the other variant’s own token; - any other value (a dimension, a number, a duration, font families): as it is, shared by both;
- a scale generated from a seed (
ThemeDraft::set_seed, its step 9 still changed): the same seed for the other scheme, generated again (ThemeDraft::seeded_ramp) over the other variant with the changes above, its steps written; the seed is inGeneratedVariant::seeds.
use egui_pop_tokens::{ThemeDraft, Themes};
use serde_json::json;
let themes = Themes::default();
let mut draft = ThemeDraft::for_theme(&themes, "pop-dark");
let e = draft.edit(&themes, "surface.panel", Some(json!("#000000"))).unwrap();
draft.apply(e.unwrap());
let generated = draft.generate_counterpart(&themes).unwrap();
assert_eq!(generated.edits[0].path, "surface.panel");
assert_eq!(generated.edits[0].to, Some(json!("#ffffff")));Sourcepub fn switch(&mut self) -> bool
pub fn switch(&mut self) -> bool
Edits the pair’s other variant: what was edited keeps its changes
and history, and gets them back when switched to again. false
for a theme alone (nothing to switch to).
pub fn base(&self) -> &str
Sourcepub fn overrides(&self) -> &BTreeMap<String, Json>
pub fn overrides(&self) -> &BTreeMap<String, Json>
The tokens changed over the base theme, raw DTCG values by path.
Sourcepub fn value(&self, path: &str) -> Option<&Json>
pub fn value(&self, path: &str) -> Option<&Json>
A token’s own value in this draft, None when the base theme
gives it.
Sourcepub fn spec(&self, template: &ThemeSpec) -> ThemeSpec
pub fn spec(&self, template: &ThemeSpec) -> ThemeSpec
template with the variant edited’s base and overrides (its
density, accent and reduced motion kept): what the variant edited
resolves from.
Sourcepub fn pair_spec(&self, template: &ThemeSpec) -> ThemeSpec
pub fn pair_spec(&self, template: &ThemeSpec) -> ThemeSpec
template with the whole draft, as it is installed: the theme
named first, its overrides, and its counterpart with overrides of
its own (ThemeSpec::counterpart_overrides); a theme alone has
no counterpart (its overrides are not another theme’s).
Sourcepub fn edit(
&self,
themes: &Themes,
path: &str,
to: Option<Json>,
) -> Result<Option<TokenEdit>, String>
pub fn edit( &self, themes: &Themes, path: &str, to: Option<Json>, ) -> Result<Option<TokenEdit>, String>
Checks a new value for a token and gives the edit setting it, or
why it cannot be: the token must be one of the base theme’s, and
the value one of its type or an alias of a token of that type.
None goes back to the base theme’s value. Ok(None): nothing
would change.
Sourcepub fn preview(&mut self, path: &str, to: Option<Json>)
pub fn preview(&mut self, path: &str, to: Option<Json>)
Sets a token without recording it: a gesture in progress, shown
live, recorded once at its end with ThemeDraft::record.
Sourcepub fn record(&mut self, edit: TokenEdit)
pub fn record(&mut self, edit: TokenEdit)
Records an edit already shown (by ThemeDraft::preview): it
becomes the next to undo, and nothing is left to redo.
Sourcepub fn record_joined(&mut self, edit: TokenEdit)
pub fn record_joined(&mut self, edit: TokenEdit)
Records an edit as part of the change recorded just before it (a
palette generated at once): ThemeDraft::undo_group and
ThemeDraft::redo_group count them as one.
Sourcepub fn undo_group(&self) -> usize
pub fn undo_group(&self) -> usize
How many edits the next undo takes back as one change: 1, or the
edits recorded together (ThemeDraft::record_joined); 0 when
there is nothing to undo.
Sourcepub fn redo_group(&self) -> usize
pub fn redo_group(&self) -> usize
How many edits the next redo makes again as one change.
Sourcepub fn history_cap(&self) -> usize
pub fn history_cap(&self) -> usize
How many edits the history keeps (DRAFT_HISTORY_CAP by default).
Sourcepub fn set_history_cap(&mut self, cap: usize)
pub fn set_history_cap(&mut self, cap: usize)
Keeps at most cap edits to undo (and as many to redo), for this
variant and a pair’s other one, dropping the oldest now and
whenever a new edit goes over it. Edits recorded together
(ThemeDraft::record_joined) go as one: the oldest change goes
whole, and the newest stays whole even when it alone is over the
cap (a generated scale). 0 keeps none.
Sourcepub fn apply(&mut self, edit: TokenEdit)
pub fn apply(&mut self, edit: TokenEdit)
Makes an edit and records it (the same as ThemeDraft::record,
named for edits not previewed first).
Sourcepub fn scale_edits(
&self,
themes: &Themes,
scale: &str,
ramp: &Ramp,
) -> Result<Vec<TokenEdit>, String>
pub fn scale_edits( &self, themes: &Themes, scale: &str, ramp: &Ramp, ) -> Result<Vec<TokenEdit>, String>
The edits writing a generated scale over one of the base theme’s
scales (blue, gray): its steps 1 to 12 and its contrast,
those the base has, each checked as ThemeDraft::edit does,
those already at that value left out. Record them with
ThemeDraft::record then ThemeDraft::record_joined so they
undo as one change.
use egui_pop_tokens::palette::{Ramp, Scheme};
use egui_pop_tokens::{Color, ThemeDraft, Themes};
let themes = Themes::default();
let mut draft = ThemeDraft::new("pop-light");
let ramp = Ramp::new(Color::from_hex("#8e44ad").unwrap(), Scheme::Light);
let edits = draft.scale_edits(&themes, "blue", &ramp).unwrap();
assert_eq!(edits.len(), 13);
for (i, e) in edits.into_iter().enumerate() {
if i == 0 { draft.record(e) } else { draft.record_joined(e) }
}
assert_eq!(draft.undo_group(), 13);Sourcepub fn seeded_ramp(&self, themes: &Themes, scale: &str, seed: Seed) -> Ramp
pub fn seeded_ramp(&self, themes: &Themes, scale: &str, seed: Seed) -> Ramp
The ramp a seed gives for one of the base theme’s scales, as a
file giving that scale by its seed is resolved: fitted to the
draft’s theme without that scale’s own tokens
(crate::palette::SEEDS_EXTENSION). Write it with
ThemeDraft::scale_edits, then ThemeDraft::set_seed, so the
file saves the seed rather than twelve values.
Sourcepub fn set_seed(&mut self, scale: &str, seed: Option<Seed>)
pub fn set_seed(&mut self, scale: &str, seed: Option<Seed>)
Records that a scale was generated from a seed (None forgets
it). Not an edit: the scale’s values are (ThemeDraft::scale_edits).
The file saves the seed in place of the scale’s values as long as
they are the ones it gives; once changed by hand or undone, the
values are saved as they are.
Sourcepub fn seed(&self, scale: &str) -> Option<Seed>
pub fn seed(&self, scale: &str) -> Option<Seed>
The seed a scale was generated from, as recorded.
Sourcepub fn undo_changes(&self) -> Vec<Vec<TokenEdit>>
pub fn undo_changes(&self) -> Vec<Vec<TokenEdit>>
The changes an undo walks back, oldest first: each the edits
recorded together (ThemeDraft::record_joined), in the order
they were made.
Sourcepub fn redo_changes(&self) -> Vec<Vec<TokenEdit>>
pub fn redo_changes(&self) -> Vec<Vec<TokenEdit>>
The changes a redo makes again, the next one last, each in the order its edits were made.
Sourcepub fn undo_edit(&self) -> Option<TokenEdit>
pub fn undo_edit(&self) -> Option<TokenEdit>
The edit an undo would make (the last one’s inverse).
Sourcepub fn undo(&mut self) -> Option<TokenEdit>
pub fn undo(&mut self) -> Option<TokenEdit>
Takes the last edit back; returns the edit made (its inverse).
pub fn can_undo(&self) -> bool
pub fn can_redo(&self) -> bool
Sourcepub fn history(&self) -> &[TokenEdit]
pub fn history(&self) -> &[TokenEdit]
The edits made, oldest first (what an undo takes back last).
Sourcepub fn to_file(&self, themes: &Themes, name: &str) -> TokenFile
pub fn to_file(&self, themes: &Themes, name: &str) -> TokenFile
The draft as a token file named name: $extends its base, its
overrides with their types, stamped with the current format
(crate::FormatStamp). A scale generated from a seed
(ThemeDraft::set_seed) is saved as its seed
(crate::palette::SEEDS_EXTENSION): the steps whose value is the
one the seed gives are left out (regenerated when the file is
loaded), a step changed since is written as it is. A seed none of
whose steps still has its value (undone, or the theme changed
under it) is not saved. Loading the file gives the draft’s values.
A pair is saved as a new pair: the file is the theme named first’s,
named name, and carries its counterpart, named name and its
mode (ocean-light), each naming the other
(TokenFile::set_counterpart_file); loaded with
Themes::add_text, both are added.
Sourcepub fn export(&self, themes: &Themes, name: &str) -> String
pub fn export(&self, themes: &Themes, name: &str) -> String
The draft as the text of a .tokens.json file.
Sourcepub fn import(
themes: &Themes,
name: &str,
text: &str,
) -> Result<(ThemeDraft, Vec<Warning>), Error>
pub fn import( themes: &Themes, name: &str, text: &str, ) -> Result<(ThemeDraft, Vec<Warning>), Error>
Reads a token file as a draft: its $extends becomes the base
(the fallback theme when it has none), its tokens the overrides.
The file loads over its base as any theme file does
(Themes::check_file): brought to the current contract, a token
whose value does not check left out, the base’s value kept. A
draft changes its base’s tokens only, so a token the base does
not have is left out too. Each is a warning, as is a file stamped
newer than this egui-pop knows (read best-effort). Only an
unreadable file or an unknown base is an error.
A file carrying its counterpart (COUNTERPART_EXTENSION) is a
pair: the draft holds both variants, the file’s own edited first.
A counterpart that cannot be read, of an unknown base or of the
same mode is a warning, the file’s variant read alone.
Trait Implementations§
Source§impl Clone for ThemeDraft
impl Clone for ThemeDraft
Source§fn clone(&self) -> ThemeDraft
fn clone(&self) -> ThemeDraft
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more