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#![deny(missing_docs)]
10
11// When running tests, pull in the standard library so the test
12// harness can link successfully.
13#[cfg(any(
14    test,
15    feature = "png",
16    feature = "jpeg",
17    feature = "qrcode",
18    feature = "gif",
19    feature = "fontdue",
20    feature = "lottie",
21    feature = "pinyin",
22    feature = "fatfs",
23    feature = "nes"
24))]
25extern crate std;
26
27extern crate alloc;
28
29pub mod animation;
30pub mod event;
31pub mod plugins;
32pub mod renderer;
33pub mod style;
34pub mod theme;
35pub mod widget;
36
37#[cfg(feature = "canvas")]
38pub use plugins::canvas;
39#[cfg(feature = "fatfs")]
40pub use plugins::fatfs;
41#[cfg(feature = "fontdue")]
42pub use plugins::fontdue;
43#[cfg(feature = "gif")]
44pub use plugins::gif;
45#[cfg(feature = "jpeg")]
46pub use plugins::jpeg;
47#[cfg(feature = "lottie")]
48pub use plugins::lottie;
49#[cfg(feature = "nes")]
50pub use plugins::nes;
51#[cfg(feature = "pinyin")]
52pub use plugins::pinyin;
53#[cfg(feature = "png")]
54pub use plugins::png;
55#[cfg(feature = "qrcode")]
56pub use plugins::qrcode;
57
58// Pull doc tests from the workspace README
59#[cfg(doctest)]
60doc_comment::doctest!("../../README.md");
61
62use alloc::rc::Rc;
63use alloc::vec::Vec;
64use core::cell::RefCell;
65
66/// Node in the widget hierarchy.
67///
68/// A `WidgetNode` owns a concrete widget instance and zero or more child nodes.
69/// Events are dispatched depth‑first and drawing occurs in the same order.
70/// This mirrors the behaviour of common retained‑mode UI frameworks.
71pub struct WidgetNode {
72    /// The widget instance held by this node.
73    pub widget: Rc<RefCell<dyn widget::Widget>>,
74    /// Child nodes that make up this widget's hierarchy.
75    pub children: Vec<WidgetNode>,
76}
77
78impl WidgetNode {
79    /// Propagate an event to this node and its children.
80    ///
81    /// Returns `true` if any widget handled the event.
82    pub fn dispatch_event(&mut self, event: &event::Event) -> bool {
83        if self.widget.borrow_mut().handle_event(event) {
84            return true;
85        }
86        for child in &mut self.children {
87            if child.dispatch_event(event) {
88                return true;
89            }
90        }
91        false
92    }
93
94    /// Recursively draw this node and all child nodes using the given renderer.
95    pub fn draw(&self, renderer: &mut dyn renderer::Renderer) {
96        self.widget.borrow().draw(renderer);
97        for child in &self.children {
98            child.draw(renderer);
99        }
100    }
101}