Skip to main content

telar_ui_core/
line.rs

1use geometry_core::Point;
2use platform_core::Event;
3use renderer_core::{DrawCommand, Stroke};
4use ui_tree::{Component, EventResult, RenderNode};
5
6/// `Line` is designed for use inside `Canvas` closures where you control absolute coordinates. It does not implement `LayoutItem` because its `p1`/`p2` points are absolute, not relative to a layout rect. To use `Line` in a layout context, embed it in a `Canvas` widget.
7pub struct Line {
8    p1: Box<dyn Fn() -> Point>,
9    p2: Box<dyn Fn() -> Point>,
10    style: Box<dyn Fn() -> Stroke>,
11}
12
13impl Line {
14    pub fn new(
15        p1: impl Fn() -> Point + 'static,
16        p2: impl Fn() -> Point + 'static,
17        style: impl Fn() -> Stroke + 'static,
18    ) -> Self {
19        Self {
20            p1: Box::new(p1),
21            p2: Box::new(p2),
22            style: Box::new(style),
23        }
24    }
25}
26
27impl Component for Line {
28    fn view(&self) -> RenderNode {
29        let p1 = (self.p1)();
30        let p2 = (self.p2)();
31        let style = (self.style)();
32        RenderNode::Primitive(DrawCommand::Line { p1, p2, style })
33    }
34
35    fn on_event(&mut self, _event: &Event) -> EventResult {
36        EventResult::Ignored
37    }
38
39    fn debug_name(&self) -> &'static str {
40        "Line"
41    }
42}