zen_rs/components/
text.rs

1//! Text component
2//!
3//! Provide
4//! - Text
5//! - fore/back-ground colors
6//! - font style
7//! - link (only html/leptos)
8
9pub mod h;
10
11use crate::aspects::{
12    BackgroundColor, DefaultFontFamily, FontStyle, ForegroundColor, Link, Size, Weight,
13};
14
15/// Return default [Text] instance
16#[inline]
17pub fn text() -> Text {
18    Text::default()
19}
20
21/// Text representation
22#[derive(Debug, Default, Clone, PartialEq, PartialOrd)]
23pub struct Text {
24    /// Content of the text component
25    content: String,
26    /// Foreground color of the text
27    foreground_color: ForegroundColor,
28    /// Background color of the text
29    background_color: BackgroundColor,
30    /// Font style of the text (size, weight, etc.)
31    font_style: FontStyle,
32    /// Optional link associated with the text
33    link: Link,
34}
35
36impl Text {
37    /// Set a link for the text component
38    #[inline]
39    pub fn link(mut self, link: impl ToString) -> Self {
40        self.link = Some(link.to_string());
41        self
42    }
43
44    /// Set the content of the text component
45    #[inline]
46    pub fn content(mut self, content: impl ToString) -> Self {
47        self.content = content.to_string();
48        self
49    }
50
51    /// Set the foreground color of the text
52    #[inline]
53    pub fn foreground_color(mut self, foreground_color: ForegroundColor) -> Self {
54        self.foreground_color = foreground_color;
55        self
56    }
57
58    /// Set the background color of the text
59    #[inline]
60    pub fn background_color(mut self, background_color: BackgroundColor) -> Self {
61        self.background_color = background_color;
62        self
63    }
64
65    /// Set the font size of the text
66    #[inline]
67    pub fn size(mut self, size: Size) -> Self {
68        self.font_style.0 = size;
69        self
70    }
71
72    /// Toggle the strikeout style for the text
73    #[inline]
74    pub fn is_strikeout(mut self) -> Self {
75        self.font_style.2 = !self.font_style.2;
76        self
77    }
78
79    /// Toggle the underline style for the text
80    #[inline]
81    pub fn is_underline(mut self) -> Self {
82        self.font_style.3 = !self.font_style.3;
83        self
84    }
85
86    /// Toggle the italic style for the text
87    #[inline]
88    pub fn is_italic(mut self) -> Self {
89        self.font_style.4 = !self.font_style.4;
90        self
91    }
92
93    /// Set the weight of the font
94    ///  - [Weight::Heavy] => 900
95    ///  - [Weight::ExtraBold] => 800
96    ///  - [Weight::Bold] => 700
97    ///  - [Weight::SemiBold] => 600
98    ///  - [Weight::Medium] => 500 | Default
99    ///  - [Weight::Normal] => 400
100    ///  - [Weight::Light] => 300
101    ///  - [Weight::ExtraLight] => 200
102    ///  - [Weight::Thin] => 100
103    #[inline]
104    pub fn font_weight(mut self, weight: Weight) -> Self {
105        self.font_style.1 = weight;
106        self
107    }
108
109    /// Set a custom font name
110    #[inline]
111    pub fn font_custom(mut self, name: impl ToString) -> Self {
112        self.font_style.5 .0 = name.to_string();
113        self
114    }
115
116    /// Set the default font family type
117    /// - Serif
118    /// - SansSerif | Default
119    /// - Monospace
120    /// - Cursive
121    /// - Fantasy
122    /// - SystemUi
123    #[inline]
124    pub fn font_default(mut self, def: DefaultFontFamily) -> Self {
125        self.font_style.5 .1 = def;
126        self
127    }
128
129    /// Set the complete [FontStyle] directly
130    #[inline]
131    pub fn font_style(mut self, font: FontStyle) -> Self {
132        self.font_style = font;
133        self
134    }
135
136    /// Get the text content
137    #[inline]
138    pub fn get_content(&self) -> &str {
139        self.content.as_str()
140    }
141
142    /// Get the foreground color
143    #[inline]
144    pub fn get_foreground_color(&self) -> ForegroundColor {
145        self.foreground_color
146    }
147
148    /// Get the background color
149    #[inline]
150    pub fn get_background_color(&self) -> BackgroundColor {
151        self.background_color
152    }
153
154    /// Get the font size
155    #[inline]
156    pub fn get_size(&self) -> Size {
157        self.font_style.0
158    }
159
160    /// Get the complete font style
161    #[inline]
162    pub fn get_font(&self) -> &FontStyle {
163        &self.font_style
164    }
165
166    /// Get the link (if any) associated with the text
167    #[inline]
168    pub fn get_link(&self) -> &Link {
169        &self.link
170    }
171}