Skip to main content

telar_ui_core/
rect.rs

1use geometry_core::Rect;
2use layout_core::{LayoutError, LayoutStyle};
3use platform_core::Event;
4use renderer_core::RectStyle;
5use ui_tree::{Component, EventResult, RenderNode};
6
7use crate::impl_leaf_widget;
8use crate::layout_leaf::LayoutLeaf;
9
10pub struct Rectangle {
11    leaf: LayoutLeaf,
12    style: Box<dyn Fn() -> RectStyle>,
13}
14
15impl Rectangle {
16    pub fn new(
17        layout_style: LayoutStyle,
18        style: impl Fn() -> RectStyle + 'static,
19    ) -> Result<Self, LayoutError> {
20        let leaf = LayoutLeaf::register(layout_style)?;
21        Ok(Self {
22            leaf,
23            style: Box::new(style),
24        })
25    }
26}
27
28impl Component for Rectangle {
29    fn view(&self) -> RenderNode {
30        let r = self.leaf.rect.get();
31        let style = (self.style)();
32        self.leaf.at_layout_position(RenderNode::rect(
33            Rect {
34                x: 0.0,
35                y: 0.0,
36                width: r.width,
37                height: r.height,
38            },
39            style,
40        ))
41    }
42
43    fn on_event(&mut self, _event: &Event) -> EventResult {
44        EventResult::Ignored
45    }
46
47    fn debug_name(&self) -> &'static str {
48        "Rectangle"
49    }
50}
51
52impl_leaf_widget!(Rectangle);