1use iced::widget::{Svg, svg};
7use iced::{Color, Length, Theme};
8
9pub use lucide_static_svg::Icon;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub struct Glyph {
14 name: &'static str,
15 svg: &'static str,
16}
17
18impl Glyph {
19 pub const fn new(icon: Icon) -> Self {
21 Self {
22 name: icon.name(),
23 svg: icon.svg_str(),
24 }
25 }
26
27 pub const fn name(self) -> &'static str {
29 self.name
30 }
31
32 pub const fn svg(self) -> &'static str {
34 self.svg
35 }
36}
37
38#[macro_export]
45macro_rules! lucide {
46 ($name:ident) => {{
47 const GLYPH: $crate::icon::Glyph = $crate::icon::Glyph::new($crate::icon::Icon::$name);
48 GLYPH
49 }};
50}
51
52pub fn icon<'a>(glyph: Glyph, size: f32) -> Svg<'a, Theme> {
54 tinted(glyph, size, None)
55}
56
57pub fn tinted<'a>(glyph: Glyph, size: f32, color: Option<Color>) -> Svg<'a, Theme> {
60 svg(svg::Handle::from_memory(glyph.svg().as_bytes()))
61 .width(Length::Fixed(size))
62 .height(Length::Fixed(size))
63 .style(move |theme: &Theme, _status| svg::Style {
64 color: Some(color.unwrap_or(theme.extended_palette().background.base.text)),
65 })
66}
67
68#[cfg(test)]
69mod tests {
70 #[test]
71 fn macro_resolves_name_and_markup() {
72 let glyph = crate::lucide!(ChevronRight);
73 assert_eq!(glyph.name(), "chevron-right");
74 assert!(glyph.svg().starts_with("<svg"));
75 }
76
77 #[test]
78 fn lucide_svgs_use_current_colour_so_tinting_works() {
79 assert!(crate::lucide!(Save).svg().contains("currentColor"));
80 }
81}