Skip to main content

ThemeSpec

Struct ThemeSpec 

Source
#[non_exhaustive]
pub struct ThemeSpec { pub base: String, pub density: Density, pub accent: Option<String>, pub reduced_motion: bool, pub overrides: BTreeMap<String, Value>, pub counterpart: Counterpart, pub counterpart_overrides: Option<BTreeMap<String, Value>>, pub text_size: f32, pub font_ui: Option<String>, pub font_mono: Option<String>, pub scroll_bars: ScrollBars, }
Expand description

What to resolve: a base theme (mode and contrast), a density, an accent, reduced motion and the user’s own overrides, composed in that order. The base theme’s counterpart (Counterpart), when it has one, is resolved with the same density, accent, reduced motion and overrides (Themes::resolve_counterpart), or with overrides of its own (ThemeSpec::counterpart_overrides).

use egui_pop_tokens::{Density, ThemeSpec, Themes};

let spec = ThemeSpec::new("pop-light").density(Density::Compact).accent("blue");
let tokens = Themes::default().resolve(&spec);
assert!(tokens.problems().is_empty());
assert_eq!(tokens.dimension("space.m"), Some(4.0));

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§base: String

The base theme’s name (pop-dark, pop-light, …).

§density: Density§accent: Option<String>

A colour scale whose steps become the accent roles; None keeps the base theme’s accent.

§reduced_motion: bool

Sets motion.duration to zero.

§overrides: BTreeMap<String, Value>

Raw DTCG values by token path, applied last; values may be aliases.

§counterpart: Counterpart

The other variant of the base theme, light or dark.

§counterpart_overrides: Option<BTreeMap<String, Value>>

The counterpart’s own overrides, in place of overrides: a pair whose two variants are edited apart (the theme editor’s). None: overrides apply to both.

§text_size: f32

The user’s text size: a factor of the size scale (font.size.*), one of TEXT_SIZES; 1 keeps the theme’s sizes (ThemeSpec::text_size).

§font_ui: Option<String>

A family the user puts first in font.family.ui, before the theme’s own (which stay its fallbacks).

§font_mono: Option<String>

A family the user puts first in font.family.mono.

§scroll_bars: ScrollBars

The user’s choice over the theme’s scroll bars (ThemeSpec::scroll_bars).

Implementations§

Source§

impl ThemeSpec

Source

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

Source

pub fn pair(dark: impl Into<String>, light: impl Into<String>) -> ThemeSpec

A pair of themes, a dark one and a light one: dark is the base (shown when nothing asks for light), light its counterpart.

use egui_pop_tokens::{ThemeSpec, Themes};

let spec = ThemeSpec::pair("github-dark", "pop-light");
let light = Themes::default().resolve_counterpart(&spec).unwrap();
assert_eq!(light.spec().base, "pop-light");
assert!(!light.is_dark());
Source

pub fn counterpart(self, theme: impl Into<String>) -> ThemeSpec

Names the base theme’s counterpart, whatever the token files say.

Source

pub fn alone(self) -> ThemeSpec

The base theme alone, without its counterpart: its mode is forced.

Source

pub fn variant(&self, base: impl Into<String>) -> ThemeSpec

The same spec over another base theme, alone, with the same overrides.

Source

pub fn counterpart_variant(&self, other: impl Into<String>) -> ThemeSpec

What the counterpart other is resolved from: the same spec over it, alone, with the counterpart’s own overrides when the spec gives them (ThemeSpec::counterpart_overrides).

use egui_pop_tokens::{Color, ThemeSpec, Themes};
use serde_json::json;

let spec = ThemeSpec::new("pop-dark")
    .override_token("danger", json!("#ff0000"))
    .counterpart_override("danger", json!("#00ff00"));
let light = Themes::default().resolve_counterpart(&spec).unwrap();
assert_eq!(light.color("danger"), Color::from_hex("#00ff00"));
let dark = Themes::default().resolve(&spec);
assert_eq!(dark.color("danger"), Color::from_hex("#ff0000"));
Source

pub fn density(self, density: Density) -> ThemeSpec

Source

pub fn accent(self, scale: impl Into<String>) -> ThemeSpec

The accent: a colour scale of the theme by name ("blue"), or a seed colour ("#3a7bd5") the scale is generated from (ThemeSpec::accent_color).

Source

pub fn accent_color(self, seed: Color) -> ThemeSpec

An accent generated from a seed colour: the SEED_SCALE scale (palette::Ramp), for the theme’s light or dark scheme, the seed clamped to what gives a usable ramp. A counterpart gets the ramp of its own scheme from the same seed.

use egui_pop_tokens::{Color, ThemeSpec, Themes, contrast};

let spec = ThemeSpec::new("pop-light").accent_color(Color::from_hex("#c0392b").unwrap());
assert_eq!(spec.accent.as_deref(), Some("#c0392b"));
let tokens = Themes::default().resolve(&spec);
assert_eq!(tokens.get("accent.default").unwrap().alias.as_deref(), Some("custom.9"));
assert!(contrast::check(&tokens, contrast::Level::Normal).is_empty());
Source

pub fn accent_seed(&self) -> Option<Color>

The accent’s seed colour, when the accent is one (#rrggbb).

Source

pub fn reduced_motion(self, reduced: bool) -> ThemeSpec

Source

pub fn text_size(self, factor: f32) -> ThemeSpec

The user’s text size, a factor of the theme’s size scale, rounded to the nearest of TEXT_SIZES: each step of font.size.* is multiplied and rounded to half a point, and every text style and component token aliasing a step follows. Only the text grows: spacing does not (the interface zoom scales everything), and size.control.height grows to at least 1.6 times the body text, so a larger label is not clipped.

use egui_pop_tokens::{ThemeSpec, Themes};

let themes = Themes::default();
let base = themes.resolve(&ThemeSpec::new("pop-dark"));
let large = themes.resolve(&ThemeSpec::new("pop-dark").text_size(1.3));
assert_eq!(base.dimension("font.size.m"), Some(13.0));
assert_eq!(large.dimension("font.size.m"), Some(17.0));
assert_eq!(large.dimension("font.text-style.body"), Some(17.0));
assert_eq!(large.dimension("space.m"), base.dimension("space.m"));
Source

pub fn scroll_bars(self, choice: ScrollBars) -> ThemeSpec

The user’s choice over the theme’s scroll bars: scroll-bar.visibility and placement take it when the theme resolves, for the theme and its counterpart. Auto leaves the theme’s.

use egui_pop_tokens::{ScrollBars, ThemeSpec, Themes, Value};

let themes = Themes::default();
let full = themes.resolve(&ThemeSpec::new("pop-dark").scroll_bars(ScrollBars::Always));
let visibility = &full.get("scroll-bar.visibility").unwrap().value;
assert_eq!(visibility, &Value::Other("always".into()));
Source

pub fn font_ui(self, family: Option<String>) -> ThemeSpec

A family drawn first for interface text (font.family.ui), the theme’s own families after it; None keeps the theme’s.

Source

pub fn font_mono(self, family: Option<String>) -> ThemeSpec

A family drawn first for numbers and code (font.family.mono).

Source

pub fn override_token(self, path: impl Into<String>, value: Value) -> ThemeSpec

Overrides one token with a raw DTCG value (json!("#3080ff"), json!("{blue.9}")).

Source

pub fn counterpart_override( self, path: impl Into<String>, value: Value, ) -> ThemeSpec

Overrides one token of the counterpart only: from the first one, the counterpart has overrides of its own (ThemeSpec::counterpart_overrides), starting empty.

Source

pub fn label(&self) -> String

A short label: pop-dark · compact · blue.

Trait Implementations§

Source§

impl Clone for ThemeSpec

Source§

fn clone(&self) -> ThemeSpec

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 ThemeSpec

Source§

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

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

impl Default for ThemeSpec

Source§

fn default() -> ThemeSpec

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

impl PartialEq for ThemeSpec

Source§

fn eq(&self, other: &ThemeSpec) -> 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 ThemeSpec

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
§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

fn as_any(&self) -> &(dyn Any + 'static)

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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<T> Finish for T

§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

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
§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

§

fn to_owned_table(&self) -> U

§

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

§

fn try_clone(&self) -> Result<T, Error>

Clones self, possibly returning an error.
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,