cosmic/widget/
text.rs

1use crate::Renderer;
2pub use iced::widget::Text;
3use iced_core::text::LineHeight;
4use std::borrow::Cow;
5
6/// Creates a new [`Text`] widget with the provided content.
7///
8/// [`Text`]: widget::Text
9pub fn text<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
10    Text::new(text.into()).font(crate::font::default())
11}
12
13/// Available presets for text typography
14#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
15pub enum Typography {
16    Body,
17    Caption,
18    CaptionHeading,
19    Heading,
20    Monotext,
21    Title1,
22    Title2,
23    Title3,
24    Title4,
25}
26
27/// [`Text`] widget with the Title 1 typography preset.
28pub fn title1<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
29    #[inline(never)]
30    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
31        Text::new(text)
32            .size(35.0)
33            .line_height(LineHeight::Absolute(52.0.into()))
34            .font(crate::font::semibold())
35    }
36
37    inner(text.into())
38}
39
40/// [`Text`] widget with the Title 2 typography preset.
41pub fn title2<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
42    #[inline(never)]
43    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
44        Text::new(text)
45            .size(29.0)
46            .line_height(LineHeight::Absolute(43.0.into()))
47            .font(crate::font::semibold())
48    }
49
50    inner(text.into())
51}
52
53/// [`Text`] widget with the Title 3 typography preset.
54pub fn title3<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
55    #[inline(never)]
56    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
57        Text::new(text)
58            .size(24.0)
59            .line_height(LineHeight::Absolute(36.0.into()))
60            .font(crate::font::bold())
61    }
62
63    inner(text.into())
64}
65
66/// [`Text`] widget with the Title 4 typography preset.
67pub fn title4<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
68    #[inline(never)]
69    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
70        Text::new(text)
71            .size(20.0)
72            .line_height(LineHeight::Absolute(30.0.into()))
73            .font(crate::font::bold())
74    }
75
76    inner(text.into())
77}
78
79/// [`Text`] widget with the Heading typography preset.
80pub fn heading<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
81    #[inline(never)]
82    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
83        Text::new(text)
84            .size(14.0)
85            .line_height(LineHeight::Absolute(iced::Pixels(21.0)))
86            .font(crate::font::bold())
87    }
88
89    inner(text.into())
90}
91
92/// [`Text`] widget with the Caption Heading typography preset.
93pub fn caption_heading<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
94    #[inline(never)]
95    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
96        Text::new(text)
97            .size(12.0)
98            .line_height(LineHeight::Absolute(iced::Pixels(17.0)))
99            .font(crate::font::semibold())
100    }
101
102    inner(text.into())
103}
104
105/// [`Text`] widget with the Body typography preset.
106pub fn body<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
107    #[inline(never)]
108    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
109        Text::new(text)
110            .size(14.0)
111            .line_height(LineHeight::Absolute(21.0.into()))
112            .font(crate::font::default())
113    }
114
115    inner(text.into())
116}
117
118/// [`Text`] widget with the Caption typography preset.
119pub fn caption<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
120    #[inline(never)]
121    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
122        Text::new(text)
123            .size(12.0)
124            .line_height(LineHeight::Absolute(17.0.into()))
125            .font(crate::font::default())
126    }
127
128    inner(text.into())
129}
130
131/// [`Text`] widget with the Monotext typography preset.
132pub fn monotext<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
133    #[inline(never)]
134    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
135        Text::new(text)
136            .size(14.0)
137            .line_height(LineHeight::Absolute(20.0.into()))
138            .font(crate::font::mono())
139    }
140
141    inner(text.into())
142}