Skip to main content

ThemeDraft

Struct ThemeDraft 

Source
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

Source

pub fn new(base: impl Into<String>) -> ThemeDraft

A draft over a base theme, nothing changed yet.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn is_paired(&self) -> bool

Whether the draft is a light and dark pair.

Source

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.

Source

pub fn editing_second(&self) -> bool

Whether the variant edited is the pair’s second (the counterpart of the theme named first).

Source

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.

Source

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 lightness 1 - 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 in GeneratedVariant::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")));
Source

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).

Source

pub fn base(&self) -> &str

Source

pub fn overrides(&self) -> &BTreeMap<String, Json>

The tokens changed over the base theme, raw DTCG values by path.

Source

pub fn value(&self, path: &str) -> Option<&Json>

A token’s own value in this draft, None when the base theme gives it.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn redo_group(&self) -> usize

How many edits the next redo makes again as one change.

Source

pub fn history_cap(&self) -> usize

How many edits the history keeps (DRAFT_HISTORY_CAP by default).

Source

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.

Source

pub fn apply(&mut self, edit: TokenEdit)

Makes an edit and records it (the same as ThemeDraft::record, named for edits not previewed first).

Source

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);
Source

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.

Source

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.

Source

pub fn seed(&self, scale: &str) -> Option<Seed>

The seed a scale was generated from, as recorded.

Source

pub fn seeds(&self) -> &BTreeMap<String, Seed>

The seeds recorded, by scale.

Source

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.

Source

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.

Source

pub fn undo_edit(&self) -> Option<TokenEdit>

The edit an undo would make (the last one’s inverse).

Source

pub fn redo_edit(&self) -> Option<TokenEdit>

The edit a redo would make.

Source

pub fn undo(&mut self) -> Option<TokenEdit>

Takes the last edit back; returns the edit made (its inverse).

Source

pub fn redo(&mut self) -> Option<TokenEdit>

Makes the last undone edit again; returns it.

Source

pub fn can_undo(&self) -> bool

Source

pub fn can_redo(&self) -> bool

Source

pub fn history(&self) -> &[TokenEdit]

The edits made, oldest first (what an undo takes back last).

Source

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.

Source

pub fn export(&self, themes: &Themes, name: &str) -> String

The draft as the text of a .tokens.json file.

Source

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

Source§

fn clone(&self) -> ThemeDraft

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ThemeDraft

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ThemeDraft

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for ThemeDraft

Source§

fn eq(&self, other: &ThemeDraft) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ThemeDraft

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.