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::{
27    CustomColorGroup, DynamicVariant, SpecVersion, ThemePreset, harmonize_color,
28};
29pub use generated::ColorRole;
30pub use layout::{Density, Direction, LayoutPolicy, WidthClass};
31pub use motion::{CubicBezier, MotionKind, MotionScheme, MotionSpec, MotionSpeed, Spring};
32pub use shape::{CornerRole, CornerShape, MaterialShape, PolygonRole, PolygonSpec};
33pub use typography::{FontFamilies, TypeRole, TypeStyle, Typeface, Typography, TypographyEmphasis};
34
35/// Application-owned configuration consumed by the Material rendering layer.
36#[derive(Clone, Debug, Default, PartialEq)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
38pub struct MaterialTheme {
39    pub colors: ColorScheme,
40    pub typography: Typography,
41    pub motion: MotionScheme,
42    pub layout: LayoutPolicy,
43}
44
45impl MaterialTheme {
46    /// Creates the baseline theme for an explicit appearance.
47    pub fn baseline(appearance: Appearance) -> Self {
48        Self {
49            colors: ColorScheme::baseline(appearance),
50            ..Self::default()
51        }
52    }
53
54    /// Creates a Tonal Spot theme from an opaque sRGB seed. The seed's alpha is ignored.
55    #[cfg(feature = "dynamic-color")]
56    pub fn from_seed(seed: Argb, appearance: Appearance, contrast: f64) -> Self {
57        Self {
58            colors: ColorScheme::from_seed(seed, appearance, contrast),
59            ..Self::default()
60        }
61    }
62
63    pub fn with_colors(mut self, colors: ColorScheme) -> Self {
64        self.colors = colors;
65        self
66    }
67
68    pub fn with_typography(mut self, typography: Typography) -> Self {
69        self.typography = typography;
70        self
71    }
72
73    pub fn with_motion(mut self, motion: MotionScheme) -> Self {
74        self.motion = motion;
75        self
76    }
77
78    pub fn with_layout(mut self, layout: LayoutPolicy) -> Self {
79        self.layout = layout;
80        self
81    }
82
83    pub fn with_density(mut self, density: Density) -> Self {
84        self.layout.density = density;
85        self
86    }
87
88    pub fn with_direction(mut self, direction: Direction) -> Self {
89        self.layout.direction = direction;
90        self
91    }
92}
93
94/// The typed value of a token from the bundled data. Semantic color roles
95/// resolve through the current [ColorScheme].
96#[derive(Clone, Copy, Debug, PartialEq)]
97pub enum TokenValue {
98    Color(Argb),
99    ColorRole(ColorRole),
100    Corners(CornerShape),
101    Dp(f32),
102    DpOffset(f32),
103    TypeStyle(TypeStyle),
104    Easing(CubicBezier),
105    Milliseconds(u16),
106    Opacity(f32),
107    Polygon(PolygonSpec),
108    Sp(f32),
109    Spring(Spring),
110    Typeface(Typeface),
111    Weight(u16),
112}
113
114/// Looks up an exact, case-sensitive token name from the bundled Material data.
115pub fn token(name: &str) -> Option<TokenValue> {
116    let index = generated::TOKENS
117        .binary_search_by_key(&name, |(name, _)| *name)
118        .ok()?;
119    Some(generated::TOKENS[index].1)
120}