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 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#[derive(Clone, Debug, Default, PartialEq)]
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44pub struct MaterialTheme {
45 pub colors: ColorScheme,
47 pub typography: Typography,
49 pub motion: MotionScheme,
51 pub layout: LayoutPolicy,
53}
54
55impl MaterialTheme {
56 pub fn baseline(appearance: Appearance) -> Self {
58 Self {
59 colors: ColorScheme::baseline(appearance),
60 ..Self::default()
61 }
62 }
63
64 #[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 pub fn colors(mut self, colors: ColorScheme) -> Self {
75 self.colors = colors;
76 self
77 }
78
79 pub fn typography(mut self, typography: Typography) -> Self {
81 self.typography = typography;
82 self
83 }
84
85 pub fn motion(mut self, motion: MotionScheme) -> Self {
87 self.motion = motion;
88 self
89 }
90
91 pub fn layout(mut self, layout: LayoutPolicy) -> Self {
93 self.layout = layout;
94 self
95 }
96
97 pub fn density(mut self, density: Density) -> Self {
99 self.layout.density = density;
100 self
101 }
102
103 pub fn direction(mut self, direction: Direction) -> Self {
105 self.layout.direction = direction;
106 self
107 }
108}
109
110#[derive(Clone, Copy, Debug, PartialEq)]
113pub enum TokenValue {
114 Color(Argb),
116 ColorRole(ColorRole),
118 Corners(CornerShape),
120 Dp(f32),
122 DpOffset(f32),
124 TypeStyle(TypeStyle),
126 Easing(CubicBezier),
128 Milliseconds(u16),
130 Opacity(f32),
132 Polygon(PolygonSpec),
134 Sp(f32),
136 Spring(Spring),
138 Typeface(Typeface),
140 Weight(u16),
142}
143
144pub 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}