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