Skip to main content

rlvgl_ui/
text.rs

1// SPDX-License-Identifier: MIT
2//! Text and heading helpers for rlvgl-ui.
3//!
4//! Provides simple wrappers around the [`rlvgl_widgets::label::Label`] widget
5//! for body text and semantic headings.
6
7use rlvgl_core::{
8    event::Event,
9    renderer::Renderer,
10    widget::{Rect, Widget},
11};
12use rlvgl_widgets::label::Label;
13
14/// Text wrapper around the [`Label`] widget.
15pub struct Text {
16    inner: Label,
17}
18
19impl Text {
20    /// Create a new text element with the provided content and bounds.
21    pub fn new(text: &str, bounds: Rect) -> Self {
22        let inner = Label::new(text, bounds);
23        Self { inner }
24    }
25
26    /// Immutable access to the text style.
27    pub fn style(&self) -> &rlvgl_core::style::Style {
28        &self.inner.style
29    }
30
31    /// Mutable access to the text style.
32    pub fn style_mut(&mut self) -> &mut rlvgl_core::style::Style {
33        &mut self.inner.style
34    }
35
36    /// Update the displayed text.
37    pub fn set_text(&mut self, text: &str) {
38        self.inner.set_text(text);
39    }
40
41    /// Retrieve the current text content.
42    pub fn text(&self) -> &str {
43        self.inner.text()
44    }
45}
46
47impl Widget for Text {
48    fn bounds(&self) -> Rect {
49        self.inner.bounds()
50    }
51
52    fn draw(&self, renderer: &mut dyn Renderer) {
53        self.inner.draw(renderer);
54    }
55
56    fn handle_event(&mut self, event: &Event) -> bool {
57        self.inner.handle_event(event)
58    }
59}
60
61/// Heading wrapper applying semantic emphasis.
62pub struct Heading {
63    inner: Text,
64}
65
66impl Heading {
67    /// Create a new heading element with the provided text and bounds.
68    pub fn new(text: &str, bounds: Rect) -> Self {
69        let inner = Text::new(text, bounds);
70        Self { inner }
71    }
72
73    /// Immutable access to the heading style.
74    pub fn style(&self) -> &rlvgl_core::style::Style {
75        self.inner.style()
76    }
77
78    /// Mutable access to the heading style.
79    pub fn style_mut(&mut self) -> &mut rlvgl_core::style::Style {
80        self.inner.style_mut()
81    }
82
83    /// Retrieve the heading text.
84    pub fn text(&self) -> &str {
85        self.inner.text()
86    }
87}
88
89impl Widget for Heading {
90    fn bounds(&self) -> Rect {
91        self.inner.bounds()
92    }
93
94    fn draw(&self, renderer: &mut dyn Renderer) {
95        self.inner.draw(renderer);
96    }
97
98    fn handle_event(&mut self, event: &Event) -> bool {
99        self.inner.handle_event(event)
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    use rlvgl_core::widget::Rect;
107
108    #[test]
109    fn text_updates_content() {
110        let mut txt = Text::new(
111            "hello",
112            Rect {
113                x: 0,
114                y: 0,
115                width: 10,
116                height: 10,
117            },
118        );
119        txt.set_text("world");
120        assert_eq!(txt.text(), "world");
121    }
122
123    #[test]
124    fn heading_uses_text() {
125        let heading = Heading::new(
126            "title",
127            Rect {
128                x: 0,
129                y: 0,
130                width: 10,
131                height: 10,
132            },
133        );
134        assert_eq!(heading.text(), "title");
135    }
136}