Skip to main content

telar_ui_core/
path.rs

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