rlvgl_core/lib.rs
1//! Core runtime types and utilities for the `rlvgl` UI toolkit.
2//!
3//! This crate exposes the building blocks used by higher level widgets and
4//! platform backends. It is intended to be usable in `no_std` environments and
5//! therefore avoids allocations where possible. Widgets are organised into a
6//! tree of [`WidgetNode`] values which receive [`Event`]s and draw themselves via
7//! a [`Renderer`] implementation.
8#![cfg_attr(not(test), no_std)]
9
10// When running tests, pull in the standard library so the test
11// harness can link successfully.
12#[cfg(any(test, feature = "png", feature = "jpeg", feature = "qrcode"))]
13extern crate std;
14
15extern crate alloc;
16
17pub mod animation;
18pub mod event;
19pub mod plugins;
20pub mod renderer;
21pub mod style;
22pub mod theme;
23pub mod widget;
24
25#[cfg(feature = "jpeg")]
26pub use plugins::jpeg;
27#[cfg(feature = "png")]
28pub use plugins::png;
29#[cfg(feature = "qrcode")]
30pub use plugins::qrcode;
31
32// Pull doc tests from the workspace README
33#[cfg(doctest)]
34doc_comment::doctest!("../../README.md");
35
36use alloc::rc::Rc;
37use alloc::vec::Vec;
38use core::cell::RefCell;
39
40/// Node in the widget hierarchy.
41///
42/// A `WidgetNode` owns a concrete widget instance and zero or more child nodes.
43/// Events are dispatched depth‑first and drawing occurs in the same order.
44/// This mirrors the behaviour of common retained‑mode UI frameworks.
45pub struct WidgetNode {
46 pub widget: Rc<RefCell<dyn widget::Widget>>,
47 pub children: Vec<WidgetNode>,
48}
49
50impl WidgetNode {
51 /// Propagate an event to this node and its children.
52 ///
53 /// Returns `true` if any widget handled the event.
54 pub fn dispatch_event(&mut self, event: &event::Event) -> bool {
55 if self.widget.borrow_mut().handle_event(event) {
56 return true;
57 }
58 for child in &mut self.children {
59 if child.dispatch_event(event) {
60 return true;
61 }
62 }
63 false
64 }
65
66 /// Recursively draw this node and all child nodes using the given renderer.
67 pub fn draw(&self, renderer: &mut dyn renderer::Renderer) {
68 self.widget.borrow().draw(renderer);
69 for child in &self.children {
70 child.draw(renderer);
71 }
72 }
73}