canvas.rs

 1use super::Element;
 2use crate::PaintContext;
 3use pathfinder_geometry::{
 4    rect::RectF,
 5    vector::{vec2f, Vector2F},
 6};
 7
 8pub struct Canvas<F>(F)
 9where
10    F: FnMut(RectF, &mut PaintContext);
11
12impl<F> Canvas<F>
13where
14    F: FnMut(RectF, &mut PaintContext),
15{
16    pub fn new(f: F) -> Self {
17        Self(f)
18    }
19}
20
21impl<F> Element for Canvas<F>
22where
23    F: FnMut(RectF, &mut PaintContext),
24{
25    type LayoutState = ();
26    type PaintState = ();
27
28    fn layout(
29        &mut self,
30        constraint: crate::SizeConstraint,
31        _: &mut crate::LayoutContext,
32    ) -> (Vector2F, Self::LayoutState) {
33        let x = if constraint.max.x().is_finite() {
34            constraint.max.x()
35        } else {
36            constraint.min.x()
37        };
38        let y = if constraint.max.y().is_finite() {
39            constraint.max.y()
40        } else {
41            constraint.min.y()
42        };
43        (vec2f(x, y), ())
44    }
45
46    fn paint(
47        &mut self,
48        bounds: RectF,
49        _: &mut Self::LayoutState,
50        ctx: &mut PaintContext,
51    ) -> Self::PaintState {
52        self.0(bounds, ctx)
53    }
54
55    fn after_layout(
56        &mut self,
57        _: Vector2F,
58        _: &mut Self::LayoutState,
59        _: &mut crate::AfterLayoutContext,
60    ) {
61    }
62
63    fn dispatch_event(
64        &mut self,
65        _: &crate::Event,
66        _: RectF,
67        _: &mut Self::LayoutState,
68        _: &mut Self::PaintState,
69        _: &mut crate::EventContext,
70    ) -> bool {
71        false
72    }
73}