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