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