1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: MIT
//! Icon font helpers and Button extension for rlvgl-ui.
//!
//! Maps human-readable icon names to LVGL symbol codepoints and extends the
//! [`rlvgl_widgets::button::Button`] widget with a fluent `icon` method.
use alloc::string::{String, ToString};
use core::fmt::Write;
use rlvgl_widgets::button::Button;
/// Resolve a human-friendly icon name to an LVGL symbol string.
pub fn lookup(name: &str) -> Option<&'static str> {
match name {
"save" => Some("\u{f0c7}"),
"edit" => Some("\u{f304}"),
"close" => Some("\u{f00d}"),
"gear" => Some("\u{f013}"),
_ => None,
}
}
/// Extension trait adding an `icon` method to buttons.
pub trait Icon {
/// Prefix the button label with the specified icon, if known.
fn icon(self, name: &str) -> Self;
}
impl Icon for Button {
fn icon(mut self, name: &str) -> Self {
if let Some(sym) = lookup(name) {
let text = self.text().to_string();
let mut buf = String::new();
// Write formatted text into the buffer; writing to a String cannot fail.
let _ = write!(&mut buf, "{sym} {text}");
self.set_text(&buf);
}
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use rlvgl_core::widget::Rect;
#[test]
fn icon_prefixes_label() {
let btn = Button::new(
"Save",
Rect {
x: 0,
y: 0,
width: 10,
height: 10,
},
)
.icon("save");
assert!(btn.text().starts_with(lookup("save").unwrap()));
}
}