cosmic/widget/
text.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::Renderer;
pub use iced::widget::Text;
use iced_core::text::LineHeight;
use std::borrow::Cow;

/// Creates a new [`Text`] widget with the provided content.
///
/// [`Text`]: widget::Text
pub fn text<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    Text::new(text.into()).font(crate::font::default())
}

/// Available presets for text typography
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Typography {
    Body,
    Caption,
    CaptionHeading,
    Heading,
    Monotext,
    Title1,
    Title2,
    Title3,
    Title4,
}

/// [`Text`] widget with the Title 1 typography preset.
pub fn title1<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(35.0)
            .line_height(LineHeight::Absolute(52.0.into()))
            .font(crate::font::semibold())
    }

    inner(text.into())
}

/// [`Text`] widget with the Title 2 typography preset.
pub fn title2<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(29.0)
            .line_height(LineHeight::Absolute(43.0.into()))
            .font(crate::font::semibold())
    }

    inner(text.into())
}

/// [`Text`] widget with the Title 3 typography preset.
pub fn title3<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(24.0)
            .line_height(LineHeight::Absolute(36.0.into()))
            .font(crate::font::bold())
    }

    inner(text.into())
}

/// [`Text`] widget with the Title 4 typography preset.
pub fn title4<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(20.0)
            .line_height(LineHeight::Absolute(30.0.into()))
            .font(crate::font::bold())
    }

    inner(text.into())
}

/// [`Text`] widget with the Heading typography preset.
pub fn heading<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(14.0)
            .line_height(LineHeight::Absolute(iced::Pixels(21.0)))
            .font(crate::font::bold())
    }

    inner(text.into())
}

/// [`Text`] widget with the Caption Heading typography preset.
pub fn caption_heading<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(12.0)
            .line_height(LineHeight::Absolute(iced::Pixels(17.0)))
            .font(crate::font::semibold())
    }

    inner(text.into())
}

/// [`Text`] widget with the Body typography preset.
pub fn body<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(14.0)
            .line_height(LineHeight::Absolute(21.0.into()))
            .font(crate::font::default())
    }

    inner(text.into())
}

/// [`Text`] widget with the Caption typography preset.
pub fn caption<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(12.0)
            .line_height(LineHeight::Absolute(17.0.into()))
            .font(crate::font::default())
    }

    inner(text.into())
}

/// [`Text`] widget with the Monotext typography preset.
pub fn monotext<'a>(text: impl Into<Cow<'a, str>> + 'a) -> Text<'a, crate::Theme, Renderer> {
    #[inline(never)]
    fn inner(text: Cow<str>) -> Text<crate::Theme, Renderer> {
        Text::new(text)
            .size(14.0)
            .line_height(LineHeight::Absolute(20.0.into()))
            .font(crate::font::mono())
    }

    inner(text.into())
}