Skip to main content

material_ui_rs/design/
text.rs

1#![allow(dead_code)]
2use iced_widget::Text;
3use iced_widget::core::text as core_text;
4use iced_widget::text::{self as iced_text, Catalog, LineHeight, Style, StyleFn};
5
6use crate::{Theme, tokens};
7
8pub fn line_height(scale: tokens::typography::TypeScale) -> LineHeight {
9    LineHeight::Absolute(scale.line_height.into())
10}
11
12pub fn type_scale<'a, Renderer>(
13    content: impl iced_text::IntoFragment<'a>,
14    scale: tokens::typography::TypeScale,
15) -> Text<'a, Theme, Renderer>
16where
17    Renderer: core_text::Renderer + 'a,
18{
19    Text::new(content)
20        .size(scale.size)
21        .line_height(line_height(scale))
22}
23
24pub fn headline_large<'a, Renderer>(
25    content: impl iced_text::IntoFragment<'a>,
26) -> Text<'a, Theme, Renderer>
27where
28    Renderer: core_text::Renderer + 'a,
29{
30    type_scale(content, tokens::typography::HEADLINE_LARGE)
31}
32
33pub fn headline_medium<'a, Renderer>(
34    content: impl iced_text::IntoFragment<'a>,
35) -> Text<'a, Theme, Renderer>
36where
37    Renderer: core_text::Renderer + 'a,
38{
39    type_scale(content, tokens::typography::HEADLINE_MEDIUM)
40}
41
42pub fn title_medium<'a, Renderer>(
43    content: impl iced_text::IntoFragment<'a>,
44) -> Text<'a, Theme, Renderer>
45where
46    Renderer: core_text::Renderer + 'a,
47{
48    type_scale(content, tokens::typography::TITLE_MEDIUM)
49}
50
51pub fn body_large<'a, Renderer>(
52    content: impl iced_text::IntoFragment<'a>,
53) -> Text<'a, Theme, Renderer>
54where
55    Renderer: core_text::Renderer + 'a,
56{
57    type_scale(content, tokens::typography::BODY_LARGE)
58}
59
60pub fn body_medium<'a, Renderer>(
61    content: impl iced_text::IntoFragment<'a>,
62) -> Text<'a, Theme, Renderer>
63where
64    Renderer: core_text::Renderer + 'a,
65{
66    type_scale(content, tokens::typography::BODY_MEDIUM)
67}
68
69impl Catalog for Theme {
70    type Class<'a> = StyleFn<'a, Self>;
71
72    fn default<'a>() -> Self::Class<'a> {
73        Box::new(none)
74    }
75
76    fn style(&self, class: &Self::Class<'_>) -> Style {
77        class(self)
78    }
79}
80
81pub fn none(_: &Theme) -> Style {
82    Style { color: None }
83}
84
85pub fn primary(theme: &Theme) -> Style {
86    Style {
87        color: Some(theme.colors().primary.text),
88    }
89}
90
91pub fn primary_container(theme: &Theme) -> Style {
92    Style {
93        color: Some(theme.colors().primary.container_text),
94    }
95}
96
97pub fn secondary(theme: &Theme) -> Style {
98    Style {
99        color: Some(theme.colors().secondary.text),
100    }
101}
102
103pub fn secondary_container(theme: &Theme) -> Style {
104    Style {
105        color: Some(theme.colors().secondary.container_text),
106    }
107}
108
109pub fn tertiary(theme: &Theme) -> Style {
110    Style {
111        color: Some(theme.colors().tertiary.text),
112    }
113}
114
115pub fn tertiary_container(theme: &Theme) -> Style {
116    Style {
117        color: Some(theme.colors().tertiary.container_text),
118    }
119}
120
121pub fn error(theme: &Theme) -> Style {
122    Style {
123        color: Some(theme.colors().error.text),
124    }
125}
126
127pub fn error_container(theme: &Theme) -> Style {
128    Style {
129        color: Some(theme.colors().error.container_text),
130    }
131}
132
133pub fn surface(theme: &Theme) -> Style {
134    Style {
135        color: Some(theme.colors().surface.text),
136    }
137}
138
139pub fn surface_variant(theme: &Theme) -> Style {
140    Style {
141        color: Some(theme.colors().surface.text_variant),
142    }
143}
144
145pub fn inverse_surface(theme: &Theme) -> Style {
146    Style {
147        color: Some(theme.colors().inverse.inverse_surface_text),
148    }
149}