zen_rs/components/
icon.rs

1//! Icon component
2
3pub mod github;
4
5use crate::aspects::{Height, Path, Size, StrokeLinecap, StrokeLinejoin, SvgColor, Width};
6
7/// Default attribute for the `xmlns` in SVG elements.
8pub static XMLNS: &str = r"http://www.w3.org/2000/svg";
9
10/// Returns a default [Icon] instance.
11#[inline]
12pub fn icon() -> Icon {
13    Icon::default()
14}
15
16#[derive(Debug, Default, Clone, PartialEq, PartialOrd)]
17/// Represents SVG settings, including content, colors, dimensions, and specific attributes.
18pub struct Icon {
19    /// Content paths of the SVG.
20    content: Vec<Path>,
21    /// Foreground color of the SVG.
22    foreground_color: SvgColor,
23    /// Background color of the SVG.
24    background_color: SvgColor,
25    /// Width of the SVG.
26    width: Width,
27    /// Height of the SVG.
28    height: Height,
29    /// Line cap style for strokes.
30    stroke_linecap: Option<StrokeLinecap>,
31    /// Line join style for strokes.
32    stroke_linejoin: Option<StrokeLinejoin>,
33    /// Width of the stroke lines.
34    stroke_width: Option<f64>,
35    /// Viewbox dimensions of the SVG.
36    view_box: (u8, u8, u8, u8),
37}
38
39impl Icon {
40    /// Retrieves the content paths of the SVG.
41    #[inline]
42    pub fn get_content(&self) -> &[String] {
43        &(self).content
44    }
45
46    /// Retrieves the foreground color of the SVG.
47    #[inline]
48    pub fn get_foreground_color(&self) -> &SvgColor {
49        &self.foreground_color
50    }
51
52    /// Retrieves the background color of the SVG.
53    #[inline]
54    pub fn get_background_color(&self) -> &SvgColor {
55        &self.background_color
56    }
57
58    /// Retrieves the width of the SVG.
59    #[inline]
60    pub fn get_width(&self) -> Width {
61        self.width
62    }
63
64    /// Retrieves the height of the SVG.
65    #[inline]
66    pub fn get_height(&self) -> Height {
67        self.height
68    }
69
70    /// Adds a new content path to the SVG.
71    #[inline]
72    pub fn content(mut self, content: impl ToString) -> Self {
73        self.content.push(content.to_string());
74        self
75    }
76
77    /// Adds multiple content paths to the SVG.
78    #[inline]
79    pub fn contents(mut self, content: Vec<impl ToString>) -> Self {
80        self.content
81            .append(&mut content.iter().map(|x| x.to_string()).collect());
82        self
83    }
84
85    /// Sets the foreground color of the SVG.
86    #[inline]
87    pub fn foreground_color(mut self, foreground_color: impl ToString) -> Self {
88        self.foreground_color = SvgColor::Color(foreground_color.to_string());
89        self
90    }
91
92    /// Removes the foreground color of the SVG.
93    #[inline]
94    pub fn foreground_color_none(mut self) -> Self {
95        self.foreground_color = SvgColor::None;
96        self
97    }
98
99    /// Sets the foreground color to `currentColor`.
100    #[inline]
101    pub fn foreground_color_current_color(mut self) -> Self {
102        self.foreground_color = SvgColor::CurrentColor;
103        self
104    }
105
106    /// Sets the background color of the SVG.
107    #[inline]
108    pub fn background_color(mut self, background_color: impl ToString) -> Self {
109        self.background_color = SvgColor::Color(background_color.to_string());
110        self
111    }
112
113    /// Removes the background color of the SVG.
114    #[inline]
115    pub fn background_color_none(mut self) -> Self {
116        self.background_color = SvgColor::None;
117        self
118    }
119
120    /// Sets the background color to `currentColor`.
121    #[inline]
122    pub fn background_color_current_color(mut self) -> Self {
123        self.background_color = SvgColor::CurrentColor;
124        self
125    }
126
127    /// Sets the width of the SVG.
128    #[inline]
129    pub fn width(mut self, width: Width) -> Self {
130        self.width = width;
131        self
132    }
133
134    /// Sets the height of the SVG.
135    #[inline]
136    pub fn height(mut self, height: Height) -> Self {
137        self.height = height;
138        self
139    }
140
141    /// Sets both width and height of the SVG.
142    #[inline]
143    pub fn size(mut self, size: Size) -> Self {
144        self.width = size;
145        self.height = size;
146        self
147    }
148
149    /// Retrieves the stroke line join style of the SVG.
150    #[inline]
151    pub fn get_stroke_linejoin(&self) -> Option<StrokeLinejoin> {
152        self.stroke_linejoin
153    }
154
155    /// Retrieves the stroke width of the SVG.
156    #[inline]
157    pub fn get_stroke_width(&self) -> Option<f64> {
158        self.stroke_width
159    }
160
161    /// Retrieves the viewbox dimensions of the SVG.
162    #[inline]
163    pub fn get_view_box(&self) -> (u8, u8, u8, u8) {
164        self.view_box
165    }
166
167    /// Retrieves the stroke line cap style of the SVG.
168    #[inline]
169    pub fn get_stroke_linecap(&self) -> Option<StrokeLinecap> {
170        self.stroke_linecap
171    }
172
173    /// Sets the stroke line join style of the SVG.
174    #[inline]
175    pub fn stroke_linejoin(mut self, stroke_linejoin: StrokeLinejoin) -> Self {
176        self.stroke_linejoin = Some(stroke_linejoin);
177        self
178    }
179
180    /// Sets the stroke width of the SVG.
181    #[inline]
182    pub fn stroke_width(mut self, stroke_width: f64) -> Self {
183        self.stroke_width = Some(stroke_width);
184        self
185    }
186
187    /// Sets the viewbox dimensions of the SVG.
188    #[inline]
189    pub fn view_box(mut self, view_box: (u8, u8, u8, u8)) -> Self {
190        self.view_box = view_box;
191        self
192    }
193
194    /// Sets the stroke line cap style of the SVG.
195    #[inline]
196    pub fn stroke_linecap(mut self, stroke_linecap: StrokeLinecap) -> Self {
197        self.stroke_linecap = Some(stroke_linecap);
198        self
199    }
200}