use alloc::rc::Rc;
use core::cell::RefCell;
use crate::WidgetNode;
use crate::event::Event;
use crate::object::ObjectNode;
pub struct AppInfo {
pub name: &'static str,
pub version: &'static str,
pub preferred_width: u32,
pub preferred_height: u32,
}
pub trait Application {
fn info(&self) -> AppInfo;
fn build(&mut self, width: u32, height: u32) -> WidgetNode;
fn after_event(&mut self, root: &Rc<RefCell<WidgetNode>>, event: &Event);
fn tick(&mut self, _root: &Rc<RefCell<WidgetNode>>) {}
fn destroy(&mut self) {}
}
pub trait ApplicationObjectExt: Application {
fn build_object_root(&mut self, width: u32, height: u32) -> ObjectNode {
ObjectNode::adopt(self.build(width, height))
}
}
impl<T: Application + ?Sized> ApplicationObjectExt for T {}
pub trait ObjectApplication {
fn info(&self) -> AppInfo;
fn build_object(&mut self, width: u32, height: u32) -> ObjectNode;
fn after_object_event(&mut self, _root: &Rc<RefCell<ObjectNode>>, _event: &Event) {}
fn tick_object(&mut self, _root: &Rc<RefCell<ObjectNode>>) {}
fn destroy(&mut self) {}
}
pub const CREATE_APP_SYMBOL: &[u8] = b"rlvgl_create_app";
pub const DESTROY_APP_SYMBOL: &[u8] = b"rlvgl_destroy_app";
#[cfg(test)]
mod tests {
use alloc::rc::Rc;
use core::cell::RefCell;
use super::*;
use crate::event::Event;
use crate::renderer::Renderer;
use crate::widget::{Rect, Widget};
struct TestWidget;
impl Widget for TestWidget {
fn bounds(&self) -> Rect {
Rect {
x: 0,
y: 0,
width: 10,
height: 10,
}
}
fn draw(&self, _renderer: &mut dyn Renderer) {}
fn handle_event(&mut self, _event: &Event) -> bool {
false
}
}
struct TestApp;
impl Application for TestApp {
fn info(&self) -> AppInfo {
AppInfo {
name: "test",
version: "0.0.0",
preferred_width: 10,
preferred_height: 10,
}
}
fn build(&mut self, _width: u32, _height: u32) -> WidgetNode {
WidgetNode::new(Rc::new(RefCell::new(TestWidget))).with_tag("root")
}
fn after_event(&mut self, _root: &Rc<RefCell<WidgetNode>>, _event: &Event) {}
}
#[test]
fn legacy_application_can_build_object_root() {
let mut app = TestApp;
let root = app.build_object_root(10, 10);
assert_eq!(root.tag(), Some("root"));
assert_eq!(root.widget().borrow().bounds().width, 10);
}
}