use iced::widget::{Svg, svg};
use iced::{Color, Length, Theme};
pub use lucide_static_svg::Icon;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Glyph {
name: &'static str,
svg: &'static str,
}
impl Glyph {
pub const fn new(icon: Icon) -> Self {
Self {
name: icon.name(),
svg: icon.svg_str(),
}
}
pub const fn name(self) -> &'static str {
self.name
}
pub const fn svg(self) -> &'static str {
self.svg
}
}
#[macro_export]
macro_rules! lucide {
($name:ident) => {{
const GLYPH: $crate::icon::Glyph = $crate::icon::Glyph::new($crate::icon::Icon::$name);
GLYPH
}};
}
pub fn icon<'a>(glyph: Glyph, size: f32) -> Svg<'a, Theme> {
tinted(glyph, size, None)
}
pub fn tinted<'a>(glyph: Glyph, size: f32, color: Option<Color>) -> Svg<'a, Theme> {
svg(svg::Handle::from_memory(glyph.svg().as_bytes()))
.width(Length::Fixed(size))
.height(Length::Fixed(size))
.style(move |theme: &Theme, _status| svg::Style {
color: Some(color.unwrap_or(theme.extended_palette().background.base.text)),
})
}
#[cfg(test)]
mod tests {
#[test]
fn macro_resolves_name_and_markup() {
let glyph = crate::lucide!(ChevronRight);
assert_eq!(glyph.name(), "chevron-right");
assert!(glyph.svg().starts_with("<svg"));
}
#[test]
fn lucide_svgs_use_current_colour_so_tinting_works() {
assert!(crate::lucide!(Save).svg().contains("currentColor"));
}
}