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