Skip to main content

armas_basic/icon/
mod.rs

1//! Icon system for Armas
2//!
3//! Re-exports the generic icon infrastructure from [`armas_icon`] and provides
4//! procedural drawing functions for the small set of icons used by built-in
5//! components (close, chevrons, info, error).
6//!
7//! For custom icons, use [`armas_icon::Icon`] with your own [`armas_icon::IconData`]
8//! or parse SVGs at runtime with the `runtime` feature of `armas_icon`.
9
10// Re-export the generic icon infrastructure
11pub use armas_icon::{render_icon, render_icon_data, Icon, IconData, OwnedIconData};
12
13use egui::{Color32, Painter, Pos2, Rect, Stroke};
14
15/// Draw a close icon (X) within the given rect.
16pub fn draw_close(painter: &Painter, rect: Rect, color: Color32) {
17    let stroke = Stroke::new(1.5, color);
18    let m = rect.shrink(rect.width() * 0.25);
19    painter.line_segment([m.left_top(), m.right_bottom()], stroke);
20    painter.line_segment([m.right_top(), m.left_bottom()], stroke);
21}
22
23/// Draw a chevron-left icon (<) within the given rect.
24pub fn draw_chevron_left(painter: &Painter, rect: Rect, color: Color32) {
25    let stroke = Stroke::new(1.5, color);
26    let cx = rect.center().x;
27    let cy = rect.center().y;
28    let half = rect.width() * 0.2;
29    painter.line_segment(
30        [
31            Pos2::new(cx + half, cy - half * 2.0),
32            Pos2::new(cx - half, cy),
33        ],
34        stroke,
35    );
36    painter.line_segment(
37        [
38            Pos2::new(cx - half, cy),
39            Pos2::new(cx + half, cy + half * 2.0),
40        ],
41        stroke,
42    );
43}
44
45/// Draw a chevron-right icon (>) within the given rect.
46pub fn draw_chevron_right(painter: &Painter, rect: Rect, color: Color32) {
47    let stroke = Stroke::new(1.5, color);
48    let cx = rect.center().x;
49    let cy = rect.center().y;
50    let half = rect.width() * 0.2;
51    painter.line_segment(
52        [
53            Pos2::new(cx - half, cy - half * 2.0),
54            Pos2::new(cx + half, cy),
55        ],
56        stroke,
57    );
58    painter.line_segment(
59        [
60            Pos2::new(cx + half, cy),
61            Pos2::new(cx - half, cy + half * 2.0),
62        ],
63        stroke,
64    );
65}
66
67/// Draw a chevron-down icon (v) within the given rect.
68pub fn draw_chevron_down(painter: &Painter, rect: Rect, color: Color32) {
69    let stroke = Stroke::new(1.5, color);
70    let cx = rect.center().x;
71    let cy = rect.center().y;
72    let half = rect.height() * 0.2;
73    painter.line_segment(
74        [
75            Pos2::new(cx - half * 2.0, cy - half),
76            Pos2::new(cx, cy + half),
77        ],
78        stroke,
79    );
80    painter.line_segment(
81        [
82            Pos2::new(cx, cy + half),
83            Pos2::new(cx + half * 2.0, cy - half),
84        ],
85        stroke,
86    );
87}
88
89/// Draw an info icon (circle with "i") within the given rect.
90pub fn draw_info(painter: &Painter, rect: Rect, color: Color32) {
91    let center = rect.center();
92    let r = rect.width().min(rect.height()) * 0.45;
93    let stroke = Stroke::new(1.5, color);
94
95    // Circle
96    painter.circle_stroke(center, r, stroke);
97
98    // Dot above the line
99    let dot_y = center.y - r * 0.3;
100    painter.circle_filled(Pos2::new(center.x, dot_y), 1.2, color);
101
102    // Vertical line of "i"
103    let line_top = center.y;
104    let line_bottom = center.y + r * 0.45;
105    painter.line_segment(
106        [
107            Pos2::new(center.x, line_top),
108            Pos2::new(center.x, line_bottom),
109        ],
110        stroke,
111    );
112}
113
114/// Draw an error icon (circle with "!") within the given rect.
115pub fn draw_error(painter: &Painter, rect: Rect, color: Color32) {
116    let center = rect.center();
117    let r = rect.width().min(rect.height()) * 0.45;
118    let stroke = Stroke::new(1.5, color);
119
120    // Circle
121    painter.circle_stroke(center, r, stroke);
122
123    // Vertical line of "!"
124    let line_top = center.y - r * 0.45;
125    let line_bottom = center.y + r * 0.1;
126    painter.line_segment(
127        [
128            Pos2::new(center.x, line_top),
129            Pos2::new(center.x, line_bottom),
130        ],
131        stroke,
132    );
133
134    // Dot below the line
135    let dot_y = center.y + r * 0.4;
136    painter.circle_filled(Pos2::new(center.x, dot_y), 1.2, color);
137}