Skip to main content

bexa_ui_core/widgets/
icon.rs

1use glyphon::Metrics;
2use glyphon::cosmic_text::Align;
3use taffy::prelude::*;
4
5use crate::framework::{DrawContext, Widget};
6use crate::icons::NERD_FONT_FAMILY;
7
8pub struct Icon {
9    glyph: &'static str,
10    metrics: Metrics,
11    color: [u8; 3],
12    padding: f32,
13}
14
15impl Icon {
16    pub fn new(glyph: &'static str, size: f32, color: [u8; 3]) -> Self {
17        Self {
18            glyph,
19            metrics: Metrics::new(size, size * 1.2),
20            color,
21            padding: 2.0,
22        }
23    }
24
25    pub fn with_padding(mut self, padding: f32) -> Self {
26        self.padding = padding;
27        self
28    }
29}
30
31impl Widget for Icon {
32    fn style(&self) -> Style {
33        let size = self.metrics.line_height + self.padding * 2.0;
34        Style {
35            size: Size {
36                width: Dimension::Length(size),
37                height: Dimension::Length(size),
38            },
39            ..Default::default()
40        }
41    }
42
43    fn draw(&self, ctx: &mut DrawContext) {
44        let layout = ctx.layout;
45        let left = layout.location.x + self.padding;
46        let top = layout.location.y + self.padding;
47        let bounds = (
48            layout.size.width - self.padding * 2.0,
49            layout.size.height - self.padding * 2.0,
50        );
51        ctx.renderer.draw_text_with_font(
52            self.glyph,
53            (left, top),
54            self.color,
55            bounds,
56            self.metrics,
57            Align::Center,
58            NERD_FONT_FAMILY,
59        );
60    }
61}