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