rlvgl 0.1.6

A modular, idiomatic Rust reimplementation of the LVGL graphics library for embedded and simulator use.
Documentation
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Helpers for rlvgl simulator examples.
//!
//! This crate provides a small widget tree and renderer used by the
//! simulator example. The demos showcase core rlvgl widgets and plugin
//! features using placeholder graphics.
#![no_std]

extern crate alloc;

use alloc::{boxed::Box, format, rc::Rc, vec::Vec};
use core::cell::RefCell;

use rlvgl::core::{
    WidgetNode, jpeg, png, qrcode,
    widget::{Color, Rect, Widget},
};
use rlvgl::widgets::{button::Button, container::Container, image::Image, label::Label};

type WidgetHandle = Rc<RefCell<dyn Widget>>;
type WidgetSlot = Rc<RefCell<Option<WidgetHandle>>>;

/// State returned by [`build_demo`] containing the root widget tree and related
/// bookkeeping used by the simulator.
pub struct Demo {
    /// Root of the demo widget tree.
    pub root: Rc<RefCell<WidgetNode>>,
    /// Counter incremented when the main button is clicked.
    pub counter: Rc<RefCell<u32>>,
    /// Widgets scheduled to be appended after event dispatch.
    pub pending: Rc<RefCell<Vec<WidgetNode>>>,
    /// Widget handles scheduled for removal after event dispatch.
    pub to_remove: Rc<RefCell<Vec<WidgetHandle>>>,
}

/// Build a simple widget tree demonstrating basic rlvgl widgets.
///
/// Returns a [`Demo`] struct containing the root [`WidgetNode`], a counter
/// incremented whenever the button is clicked, and a queue of widgets that
/// should be appended to the root after event dispatch. A `Plugins` button is
/// included to showcase optional features.
pub fn build_demo() -> Demo {
    let click_count = Rc::new(RefCell::new(0));
    let pending = Rc::new(RefCell::new(Vec::new()));
    let to_remove = Rc::new(RefCell::new(Vec::new()));

    let button = Rc::new(RefCell::new(Button::new(
        "Clicks: 0",
        Rect {
            x: 10,
            y: 40,
            width: 80,
            height: 20,
        },
    )));

    {
        let counter = click_count.clone();
        button.borrow_mut().set_on_click(move |btn: &mut Button| {
            let mut count = counter.borrow_mut();
            *count += 1;
            btn.set_text(format!("Clicks: {}", *count));
        });
    }

    let root = Rc::new(RefCell::new(WidgetNode {
        widget: Rc::new(RefCell::new(Container::new(Rect {
            x: 0,
            y: 0,
            width: 320,
            height: 240,
        }))),
        children: Vec::new(),
    }));

    let label = Label::new(
        "rlvgl Demo",
        Rect {
            x: 10,
            y: 10,
            width: 120,
            height: 20,
        },
    );
    root.borrow_mut().children.push(WidgetNode {
        widget: Rc::new(RefCell::new(label)),
        children: Vec::new(),
    });
    root.borrow_mut().children.push(WidgetNode {
        widget: button.clone(),
        children: Vec::new(),
    });

    let plugins = Rc::new(RefCell::new(Button::new(
        "Plugins",
        Rect {
            x: 100,
            y: 40,
            width: 80,
            height: 20,
        },
    )));
    let menu_widget: WidgetSlot = Rc::new(RefCell::new(None));
    let qr_demo: WidgetSlot = Rc::new(RefCell::new(None));
    let png_demo: WidgetSlot = Rc::new(RefCell::new(None));
    let jpeg_demo: WidgetSlot = Rc::new(RefCell::new(None));
    {
        let pending_add = pending.clone();
        let pending_rm = to_remove.clone();
        let menu_ref = menu_widget.clone();
        let qr_ref = qr_demo.clone();
        let png_ref = png_demo.clone();
        let jpeg_ref = jpeg_demo.clone();
        plugins.borrow_mut().set_on_click(move |_btn: &mut Button| {
            if let Some(menu_w) = menu_ref.borrow_mut().take() {
                pending_rm.borrow_mut().push(menu_w);
            } else {
                let menu_w: WidgetHandle = Rc::new(RefCell::new(Container::new(Rect {
                    x: 10,
                    y: 70,
                    width: 100,
                    height: 110,
                })));
                let mut menu = WidgetNode {
                    widget: menu_w.clone(),
                    children: Vec::new(),
                };

                let qr_button = Rc::new(RefCell::new(Button::new(
                    "QR Code",
                    Rect {
                        x: 20,
                        y: 80,
                        width: 80,
                        height: 20,
                    },
                )));
                {
                    let pending_add = pending_add.clone();
                    let pending_rm = pending_rm.clone();
                    let qr_demo = qr_ref.clone();
                    qr_button.borrow_mut().set_on_click(move |_b: &mut Button| {
                        if let Some(qr_w) = qr_demo.borrow_mut().take() {
                            pending_rm.borrow_mut().push(qr_w);
                        } else {
                            let demo = build_plugin_demo();
                            let handle = demo.widget.clone();
                            qr_demo.borrow_mut().replace(handle.clone());
                            pending_add.borrow_mut().push(demo);
                        }
                    });
                }
                menu.children.push(WidgetNode {
                    widget: qr_button,
                    children: Vec::new(),
                });

                let png_button = Rc::new(RefCell::new(Button::new(
                    "PNG",
                    Rect {
                        x: 20,
                        y: 110,
                        width: 80,
                        height: 20,
                    },
                )));
                {
                    let pending_add = pending_add.clone();
                    let pending_rm = pending_rm.clone();
                    let png_demo = png_ref.clone();
                    png_button
                        .borrow_mut()
                        .set_on_click(move |_b: &mut Button| {
                            if let Some(png_w) = png_demo.borrow_mut().take() {
                                pending_rm.borrow_mut().push(png_w);
                            } else {
                                let demo = build_png_demo();
                                let handle = demo.widget.clone();
                                png_demo.borrow_mut().replace(handle.clone());
                                pending_add.borrow_mut().push(demo);
                            }
                        });
                }
                menu.children.push(WidgetNode {
                    widget: png_button,
                    children: Vec::new(),
                });

                let jpeg_button = Rc::new(RefCell::new(Button::new(
                    "JPEG",
                    Rect {
                        x: 20,
                        y: 140,
                        width: 80,
                        height: 20,
                    },
                )));
                {
                    let pending_add = pending_add.clone();
                    let pending_rm = pending_rm.clone();
                    let jpeg_demo = jpeg_ref.clone();
                    jpeg_button
                        .borrow_mut()
                        .set_on_click(move |_b: &mut Button| {
                            if let Some(jpeg_w) = jpeg_demo.borrow_mut().take() {
                                pending_rm.borrow_mut().push(jpeg_w);
                            } else {
                                let demo = build_jpeg_demo();
                                let handle = demo.widget.clone();
                                jpeg_demo.borrow_mut().replace(handle.clone());
                                pending_add.borrow_mut().push(demo);
                            }
                        });
                }
                menu.children.push(WidgetNode {
                    widget: jpeg_button,
                    children: Vec::new(),
                });

                menu_ref.borrow_mut().replace(menu_w);
                pending_add.borrow_mut().push(menu);
            }
        });
    }
    root.borrow_mut().children.push(WidgetNode {
        widget: plugins.clone(),
        children: Vec::new(),
    });

    Demo {
        root,
        counter: click_count,
        pending,
        to_remove,
    }
}

/// Build a widget demonstrating plugin features such as QR code generation.
pub fn build_plugin_demo() -> WidgetNode {
    let (pixels_vec, width, _) = qrcode::generate(b"https://github.com/SoftOboros/rlvgl").unwrap();
    let root_w = 320u32;
    let root_h = 240u32;
    // Match the area used by the PNG/JPEG demos: the lower-right 2/3rds of the display.
    let target = root_h * 2 / 3;
    let scale = target as f32 / width as f32;
    let new_w = target;
    let new_h = target;
    let mut scaled = Vec::with_capacity((new_w * new_h) as usize);
    for y in 0..new_h {
        for x in 0..new_w {
            let src_x = (x as f32 / scale).floor() as usize;
            let src_y = (y as f32 / scale).floor() as usize;
            let idx = src_y * width as usize + src_x;
            let color = pixels_vec
                .get(idx)
                .copied()
                .unwrap_or(Color(255, 255, 255, 255));
            scaled.push(color);
        }
    }
    let pixels: &'static [Color] = Box::leak(scaled.into_boxed_slice());
    let x_pos = (root_w - new_w) as i32;
    let y_pos = (root_h - new_h) as i32;
    WidgetNode {
        widget: Rc::new(RefCell::new(Image::new(
            Rect {
                x: x_pos,
                y: y_pos,
                width: new_w as i32,
                height: new_h as i32,
            },
            new_w as i32,
            new_h as i32,
            pixels,
        ))),
        children: Vec::new(),
    }
}

/// Build a widget displaying the rlvgl logo decoded from a PNG asset.
///
/// `scale` controls the desired scaling factor. The final image is clamped so
/// it never exceeds the 320x240 screen bounds, and it is anchored to the lower
/// right corner of the display.
pub fn build_png_demo_scaled(scale: f32) -> WidgetNode {
    let data = include_bytes!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/examples/sim/assets/rlvgl-logo.png"
    ));
    let (pixels_vec, width, height) =
        png::decode(data).expect("failed to decode built-in PNG asset");

    let root_w = 320u32;
    let root_h = 240u32;
    let mut scale = if scale.is_finite() && scale > 0.0 {
        scale
    } else {
        1.0
    };
    scale = scale
        .min(root_w as f32 / width as f32)
        .min(root_h as f32 / height as f32);

    let new_w = (width as f32 * scale).max(1.0).round() as u32;
    let new_h = (height as f32 * scale).max(1.0).round() as u32;

    let mut scaled = Vec::with_capacity((new_w * new_h) as usize);
    for y in 0..new_h {
        for x in 0..new_w {
            let src_x = (x as f32 / scale).floor() as usize;
            let src_y = (y as f32 / scale).floor() as usize;
            let idx = src_y * width as usize + src_x;
            let color = pixels_vec.get(idx).copied().unwrap_or(Color(0, 0, 0, 255));
            scaled.push(color);
        }
    }
    let pixels: &'static [Color] = Box::leak(scaled.into_boxed_slice());

    let x_pos = (root_w - new_w) as i32;
    let y_pos = (root_h - new_h) as i32;
    WidgetNode {
        widget: Rc::new(RefCell::new(Image::new(
            Rect {
                x: x_pos,
                y: y_pos,
                width: new_w as i32,
                height: new_h as i32,
            },
            new_w as i32,
            new_h as i32,
            pixels,
        ))),
        children: Vec::new(),
    }
}

/// Build a PNG demo using the default scale of `0.5`.
pub fn build_png_demo() -> WidgetNode {
    build_png_demo_scaled(0.5)
}

/// Build a widget displaying the rlvgl logo decoded from a JPEG asset.
///
/// Mirrors [`build_png_demo_scaled`] but decodes a JPEG image instead.
pub fn build_jpeg_demo_scaled(scale: f32) -> WidgetNode {
    let data = include_bytes!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/examples/sim/assets/rlvgl-logo.jpg"
    ));
    let (pixels_vec, width, height) =
        jpeg::decode(data).expect("failed to decode built-in JPEG asset");

    let width = width as u32;
    let height = height as u32;
    let root_w = 320u32;
    let root_h = 240u32;
    let mut scale = if scale.is_finite() && scale > 0.0 {
        scale
    } else {
        1.0
    };
    scale = scale
        .min(root_w as f32 / width as f32)
        .min(root_h as f32 / height as f32);

    let new_w = (width as f32 * scale).max(1.0).round() as u32;
    let new_h = (height as f32 * scale).max(1.0).round() as u32;

    let mut scaled = Vec::with_capacity((new_w * new_h) as usize);
    for y in 0..new_h {
        for x in 0..new_w {
            let src_x = (x as f32 / scale).floor() as usize;
            let src_y = (y as f32 / scale).floor() as usize;
            let idx = src_y * width as usize + src_x;
            let color = pixels_vec.get(idx).copied().unwrap_or(Color(0, 0, 0, 255));
            scaled.push(color);
        }
    }
    let pixels: &'static [Color] = Box::leak(scaled.into_boxed_slice());

    let x_pos = (root_w - new_w) as i32;
    let y_pos = (root_h - new_h) as i32;
    WidgetNode {
        widget: Rc::new(RefCell::new(Image::new(
            Rect {
                x: x_pos,
                y: y_pos,
                width: new_w as i32,
                height: new_h as i32,
            },
            new_w as i32,
            new_h as i32,
            pixels,
        ))),
        children: Vec::new(),
    }
}

/// Build a JPEG demo using the default scale of `0.5`.
pub fn build_jpeg_demo() -> WidgetNode {
    build_jpeg_demo_scaled(0.5)
}

/// Flush any widgets queued during event callbacks into the root tree.
pub fn flush_pending(
    root: &Rc<RefCell<WidgetNode>>,
    pending: &Rc<RefCell<Vec<WidgetNode>>>,
    to_remove: &Rc<RefCell<Vec<WidgetHandle>>>,
) {
    let mut root_ref = root.borrow_mut();
    for handle in to_remove.borrow_mut().drain(..) {
        root_ref
            .children
            .retain(|n| !Rc::ptr_eq(&n.widget, &handle));
    }
    let mut pending_nodes = pending.borrow_mut();
    root_ref.children.extend(pending_nodes.drain(..));
}

#[cfg(test)]
extern crate std;