Skip to main content

ravenrs_widgets/
icon.rs

1use ravenrs_style::Semantic;
2
3use crate::context::{BuildCx, NodeId};
4use crate::{Fill, IconName, View, util};
5
6/// A vector icon from the built-in Lucide set, drawn as a glyph so it stays
7/// crisp at any size and takes the text color.
8#[derive(Clone, Copy, Debug)]
9#[cfg_attr(feature = "live", ravenrs_live::expose(part = "fields"))]
10pub struct Icon {
11    name: IconName,
12    size: f32,
13    color: Fill,
14}
15
16#[cfg_attr(feature = "live", ravenrs_live::expose)]
17impl Icon {
18    /// A 16 px icon in the `Ink` color.
19    pub fn new(name: IconName) -> Self {
20        Self { name, size: 16.0, color: Semantic::Ink.into() }
21    }
22
23    /// Size in logical pixels (default 16).
24    pub fn size(mut self, size: f32) -> Self {
25        self.size = size;
26        self
27    }
28
29    /// Color, as a theme role (`Ink` by default) or a literal color.
30    pub fn color(mut self, color: impl Into<Fill>) -> Self {
31        self.color = color.into();
32        self
33    }
34}
35
36impl View for Icon {
37    fn build(&self, cx: &mut BuildCx<'_>) -> NodeId {
38        let color = self.color.resolve(cx.theme());
39        util::icon(cx, self.name, self.size, color)
40    }
41}