Skip to main content

gpui_material_theme/
lib.rs

1//! Material 3 theme values for a host toolkit.
2//!
3//! The caller owns the theme and supplies current window metrics and system preferences.
4#![forbid(unsafe_code)]
5#![warn(missing_docs)]
6#![deny(rustdoc::broken_intra_doc_links)]
7
8mod color;
9#[cfg(feature = "dynamic-color")]
10mod dynamic_color;
11mod layout;
12mod motion;
13mod shape;
14mod typography;
15
16mod generated {
17    use crate::{
18        Argb, CornerShape, CubicBezier, PolygonSpec, Spring, TokenValue, TypeStyle, Typeface,
19    };
20
21    include!("generated.rs");
22}
23
24#[cfg(feature = "dynamic-color")]
25pub use color::contrast_ratio_of_tones;
26pub use color::{Appearance, Argb, ColorPair, ColorScheme, Elevation, StateLayer};
27#[cfg(feature = "dynamic-color")]
28pub use dynamic_color::{
29    CustomColorGroup, DynamicVariant, SpecVersion, ThemePreset, harmonize_color,
30};
31pub use generated::ColorRole;
32pub use layout::{Density, Direction, LayoutPolicy, WidthClass};
33pub use motion::{CubicBezier, MotionKind, MotionScheme, MotionSpec, MotionSpeed, Spring};
34pub use shape::{CornerRole, CornerShape, MaterialShape, PolygonRole, PolygonSpec};
35pub use typography::{FontFamilies, TypeRole, TypeStyle, Typeface, Typography, TypographyEmphasis};
36
37/// Application-owned configuration consumed by the Material rendering layer.
38#[derive(Clone, Debug, Default, PartialEq)]
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40pub struct MaterialTheme {
41    /// Semantic color roles and appearance for this theme.
42    pub colors: ColorScheme,
43    /// Font families and type scale configuration.
44    pub typography: Typography,
45    /// Spring motion scheme (`Standard` or `Expressive`).
46    pub motion: MotionScheme,
47    /// Desktop density and layout direction policy.
48    pub layout: LayoutPolicy,
49}
50
51impl MaterialTheme {
52    /// Creates the baseline theme for an explicit appearance.
53    pub fn baseline(appearance: Appearance) -> Self {
54        Self {
55            colors: ColorScheme::baseline(appearance),
56            ..Self::default()
57        }
58    }
59
60    /// Creates a Tonal Spot theme from an opaque sRGB seed. The seed's alpha is ignored.
61    #[cfg(feature = "dynamic-color")]
62    pub fn from_seed(seed: Argb, appearance: Appearance, contrast: f64) -> Self {
63        Self {
64            colors: ColorScheme::from_seed(seed, appearance, contrast),
65            ..Self::default()
66        }
67    }
68
69    /// Replaces the theme's [`ColorScheme`].
70    pub fn with_colors(mut self, colors: ColorScheme) -> Self {
71        self.colors = colors;
72        self
73    }
74
75    /// Replaces the theme's [`Typography`] configuration.
76    pub fn with_typography(mut self, typography: Typography) -> Self {
77        self.typography = typography;
78        self
79    }
80
81    /// Replaces the theme's [`MotionScheme`].
82    pub fn with_motion(mut self, motion: MotionScheme) -> Self {
83        self.motion = motion;
84        self
85    }
86
87    /// Replaces the theme's [`LayoutPolicy`].
88    pub fn with_layout(mut self, layout: LayoutPolicy) -> Self {
89        self.layout = layout;
90        self
91    }
92
93    /// Sets the desktop [`Density`] in [`Self::layout`].
94    pub fn with_density(mut self, density: Density) -> Self {
95        self.layout.density = density;
96        self
97    }
98
99    /// Sets the layout [`Direction`] in [`Self::layout`].
100    pub fn with_direction(mut self, direction: Direction) -> Self {
101        self.layout.direction = direction;
102        self
103    }
104}
105
106/// The typed value of a token from the bundled data. Semantic color roles
107/// resolve through the current [ColorScheme].
108#[derive(Clone, Copy, Debug, PartialEq)]
109pub enum TokenValue {
110    /// A literal packed sRGB color (`md.ref.palette.*` or resolved mode color).
111    Color(Argb),
112    /// A semantic color role resolved against the active [`ColorScheme`].
113    ColorRole(ColorRole),
114    /// A corner rounding specification (`CornerShape::Full` or four corner radii).
115    Corners(CornerShape),
116    /// A non-negative logical dimension in density-independent pixels (`dp`).
117    Dp(f32),
118    /// A signed logical offset in density-independent pixels (`dp`).
119    DpOffset(f32),
120    /// A complete Material typographic style.
121    TypeStyle(TypeStyle),
122    /// A cubic Bézier easing curve.
123    Easing(CubicBezier),
124    /// A motion duration in milliseconds.
125    Milliseconds(u16),
126    /// A normalized alpha or state-layer opacity in `0.0..=1.0`.
127    Opacity(f32),
128    /// A Material 3 Expressive polygon shape specification.
129    Polygon(PolygonSpec),
130    /// A typographic measurement in scale-independent pixels (`sp`).
131    Sp(f32),
132    /// A unit-mass spring physics specification.
133    Spring(Spring),
134    /// A logical typeface slot (`Brand` or `Plain`).
135    Typeface(Typeface),
136    /// A CSS-style numeric font weight (`1..=1000`).
137    Weight(u16),
138}
139
140/// Looks up an exact, case-sensitive token name from the bundled Material data.
141pub fn token(name: &str) -> Option<TokenValue> {
142    let index = generated::TOKENS
143        .binary_search_by_key(&name, |(name, _)| *name)
144        .ok()?;
145    Some(generated::TOKENS[index].1)
146}