1use alloc::string::String;
3use rlvgl_core::draw::draw_widget_bg;
4use rlvgl_core::event::Event;
5use rlvgl_core::font::{FontMetrics, WidgetFont, shape_text_ltr};
6use rlvgl_core::renderer::{ClipRenderer, Renderer};
7use rlvgl_core::style::Style;
8use rlvgl_core::widget::{Color, Rect, Widget};
9
10pub struct Label {
12 bounds: Rect,
13 text: String,
14 pub style: Style,
16 #[deprecated(note = "use the resolved TextStyle text_color cascade when drawing labels")]
18 pub text_color: Color,
19 font: WidgetFont,
22}
23
24impl Label {
25 #[allow(deprecated)]
27 pub fn new(text: impl Into<String>, bounds: Rect) -> Self {
28 Self {
29 bounds,
30 text: text.into(),
31 style: Style::default(),
32 text_color: Color(0, 0, 0, 255),
33 font: WidgetFont::new(),
34 }
35 }
36
37 pub fn set_font(&mut self, font: &'static dyn FontMetrics) {
43 self.font.set(font);
44 }
45
46 pub fn resolved_font(&self) -> &'static dyn FontMetrics {
51 self.font.resolve()
52 }
53
54 pub fn set_text(&mut self, text: impl Into<String>) {
56 self.text = text.into();
57 }
58
59 pub fn text(&self) -> &str {
61 &self.text
62 }
63
64 #[allow(deprecated)]
69 pub fn set_text_color(&mut self, color: Color) {
70 self.text_color = color;
71 }
72
73 #[allow(deprecated)]
78 pub fn text_color_with_alpha(&self, alpha: u8) -> Color {
79 self.text_color.with_alpha(alpha)
80 }
81
82 #[allow(deprecated)]
89 pub fn draw_with_font(&self, renderer: &mut dyn Renderer, font: &dyn FontMetrics) {
90 draw_widget_bg(renderer, self.bounds, &self.style);
91 let metrics = font.line_metrics();
92 let baseline = self.bounds.y + metrics.ascent as i32;
93 let shaped = shape_text_ltr(font, &self.text, (self.bounds.x, baseline), 0);
94 let mut clipped = ClipRenderer::new(renderer, self.bounds);
95 clipped.draw_text_shaped(
96 &shaped,
97 (0, 0),
98 self.text_color.with_alpha(self.style.alpha),
99 );
100 }
101}
102
103impl Widget for Label {
104 fn bounds(&self) -> Rect {
105 self.bounds
106 }
107
108 fn widget_font_mut(&mut self) -> Option<&mut WidgetFont> {
109 Some(&mut self.font)
110 }
111
112 fn draw(&self, renderer: &mut dyn Renderer) {
113 self.draw_with_font(renderer, self.font.resolve());
114 }
115
116 fn handle_event(&mut self, _event: &Event) -> bool {
117 false
118 }
119}