use telar::{
Color, Component, DrawCommand, EventResult, LayoutItem, LayoutStyle, LocalTree, RectStyle,
Rectangle, RenderNode, ShapeStyle, SizeDimension, Text, TextStyle, UiTree, WindowRoot,
box_item, reset_layout_runtime, signal,
};
struct Root(Box<dyn LayoutItem>);
impl Component for Root {
fn view(&self) -> RenderNode {
self.0.view()
}
fn on_event(&mut self, _: &platform_core::Event) -> EventResult {
EventResult::Ignored
}
fn debug_name(&self) -> &'static str {
"LocalRoot"
}
}
fn drawn_text(tree: &LocalTree) -> String {
tree.frame()
.iter()
.find_map(|c| match c {
DrawCommand::Text { text, .. } => Some(text.to_string()),
_ => None,
})
.expect("the tree drew its label")
}
fn drawn_rect(tree: &LocalTree) -> geometry_core::Rect {
tree.frame()
.iter()
.find_map(|c| match c {
DrawCommand::Rect { rect, .. } => Some(*rect),
_ => None,
})
.expect("the tree drew its box")
}
#[test]
fn a_changed_signal_repaints_without_being_told_to() {
reset_layout_runtime();
let label = signal("before".to_string());
let read = label;
let text = Text::new(
move || read.get(),
LayoutStyle::new(),
|| TextStyle::new(14.0, Color::BLACK),
)
.unwrap();
let tree = LocalTree::new(Box::new(Root(box_item(text))));
assert_eq!(drawn_text(&tree), "before");
label.set("after".to_string());
telar::relayout_if_dirty();
assert_eq!(drawn_text(&tree), "after");
}
#[test]
fn a_resize_repaints_without_being_told_to() {
reset_layout_runtime();
let boxed = Rectangle::new(
LayoutStyle::new()
.width(SizeDimension::Percent(1.0))
.height(40.0),
|| RectStyle::default().with_fill(Color::BLACK),
)
.unwrap();
let mut tree = LocalTree::new(Box::new(WindowRoot::new(box_item(boxed))));
tree.on_event(&platform_core::Event::WindowResized {
width: 400,
height: 300,
});
assert_eq!(drawn_rect(&tree).width, 400.0);
tree.on_event(&platform_core::Event::WindowResized {
width: 900,
height: 300,
});
assert_eq!(
drawn_rect(&tree).width,
900.0,
"the box follows the window without a force tick"
);
}