canvas.rs

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