gpui_material_theme/
lib.rs1#![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#[derive(Clone, Debug, Default, PartialEq)]
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40pub struct MaterialTheme {
41 pub colors: ColorScheme,
43 pub typography: Typography,
45 pub motion: MotionScheme,
47 pub layout: LayoutPolicy,
49}
50
51impl MaterialTheme {
52 pub fn baseline(appearance: Appearance) -> Self {
54 Self {
55 colors: ColorScheme::baseline(appearance),
56 ..Self::default()
57 }
58 }
59
60 #[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 pub fn with_colors(mut self, colors: ColorScheme) -> Self {
71 self.colors = colors;
72 self
73 }
74
75 pub fn with_typography(mut self, typography: Typography) -> Self {
77 self.typography = typography;
78 self
79 }
80
81 pub fn with_motion(mut self, motion: MotionScheme) -> Self {
83 self.motion = motion;
84 self
85 }
86
87 pub fn with_layout(mut self, layout: LayoutPolicy) -> Self {
89 self.layout = layout;
90 self
91 }
92
93 pub fn with_density(mut self, density: Density) -> Self {
95 self.layout.density = density;
96 self
97 }
98
99 pub fn with_direction(mut self, direction: Direction) -> Self {
101 self.layout.direction = direction;
102 self
103 }
104}
105
106#[derive(Clone, Copy, Debug, PartialEq)]
109pub enum TokenValue {
110 Color(Argb),
112 ColorRole(ColorRole),
114 Corners(CornerShape),
116 Dp(f32),
118 DpOffset(f32),
120 TypeStyle(TypeStyle),
122 Easing(CubicBezier),
124 Milliseconds(u16),
126 Opacity(f32),
128 Polygon(PolygonSpec),
130 Sp(f32),
132 Spring(Spring),
134 Typeface(Typeface),
136 Weight(u16),
138}
139
140pub 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}