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
6mod color;
7#[cfg(feature = "dynamic-color")]
8mod dynamic_color;
9mod layout;
10mod motion;
11mod shape;
12mod typography;
13
14mod generated {
15    use crate::{
16        Argb, CornerShape, CubicBezier, PolygonSpec, Spring, TokenValue, TypeStyle, Typeface,
17    };
18
19    include!("generated.rs");
20}
21
22#[cfg(feature = "dynamic-color")]
23pub use color::contrast_ratio_of_tones;
24pub use color::{Appearance, Argb, ColorPair, ColorScheme, Elevation, StateLayer};
25#[cfg(feature = "dynamic-color")]
26pub use dynamic_color::{DynamicVariant, SpecVersion, ThemePreset};
27pub use generated::ColorRole;
28pub use layout::{Density, Direction, LayoutPolicy, WidthClass};
29pub use motion::{CubicBezier, MotionKind, MotionScheme, MotionSpec, MotionSpeed, Spring};
30pub use shape::{CornerRole, CornerShape, MaterialShape, PolygonRole, PolygonSpec};
31pub use typography::{FontFamilies, TypeRole, TypeStyle, Typeface, Typography, TypographyEmphasis};
32
33/// Application-owned configuration consumed by the Material rendering layer.
34#[derive(Clone, Debug, Default, PartialEq)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36pub struct MaterialTheme {
37    pub colors: ColorScheme,
38    pub typography: Typography,
39    pub motion: MotionScheme,
40    pub layout: LayoutPolicy,
41}
42
43impl MaterialTheme {
44    /// Creates the baseline theme for an explicit appearance.
45    pub fn baseline(appearance: Appearance) -> Self {
46        Self {
47            colors: ColorScheme::baseline(appearance),
48            ..Self::default()
49        }
50    }
51
52    /// Creates a Tonal Spot theme from an opaque sRGB seed. The seed's alpha is ignored.
53    #[cfg(feature = "dynamic-color")]
54    pub fn from_seed(seed: Argb, appearance: Appearance, contrast: f64) -> Self {
55        Self {
56            colors: ColorScheme::from_seed(seed, appearance, contrast),
57            ..Self::default()
58        }
59    }
60}
61
62/// The typed value of a token from the bundled data. Semantic color roles
63/// resolve through the current [ColorScheme].
64#[derive(Clone, Copy, Debug, PartialEq)]
65pub enum TokenValue {
66    Color(Argb),
67    ColorRole(ColorRole),
68    Corners(CornerShape),
69    Dp(f32),
70    DpOffset(f32),
71    TypeStyle(TypeStyle),
72    Easing(CubicBezier),
73    Milliseconds(u16),
74    Opacity(f32),
75    Polygon(PolygonSpec),
76    Sp(f32),
77    Spring(Spring),
78    Typeface(Typeface),
79    Weight(u16),
80}
81
82/// Looks up an exact, case-sensitive token name from the bundled Material data.
83pub fn token(name: &str) -> Option<TokenValue> {
84    let index = generated::TOKENS
85        .binary_search_by_key(&name, |(name, _)| *name)
86        .ok()?;
87    Some(generated::TOKENS[index].1)
88}