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