1use rlvgl_core::{
8 event::Event,
9 renderer::Renderer,
10 widget::{Rect, Widget},
11};
12use rlvgl_widgets::label::Label;
13
14pub struct Text {
16 inner: Label,
17}
18
19impl Text {
20 pub fn new(text: &str, bounds: Rect) -> Self {
22 let inner = Label::new(text, bounds);
23 Self { inner }
24 }
25
26 pub fn style(&self) -> &rlvgl_core::style::Style {
28 &self.inner.style
29 }
30
31 pub fn style_mut(&mut self) -> &mut rlvgl_core::style::Style {
33 &mut self.inner.style
34 }
35
36 pub fn set_text(&mut self, text: &str) {
38 self.inner.set_text(text);
39 }
40
41 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
61pub struct Heading {
63 inner: Text,
64}
65
66impl Heading {
67 pub fn new(text: &str, bounds: Rect) -> Self {
69 let inner = Text::new(text, bounds);
70 Self { inner }
71 }
72
73 pub fn style(&self) -> &rlvgl_core::style::Style {
75 self.inner.style()
76 }
77
78 pub fn style_mut(&mut self) -> &mut rlvgl_core::style::Style {
80 self.inner.style_mut()
81 }
82
83 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}