1use crate::*;
2
3#[derive(Clone)]
5pub struct Canvas<F> {
6 func: F,
7}
8
9impl<F> View for Canvas<F>
10where
11 F: Fn(&mut Context, LocalRect, &mut Vger) + 'static,
12{
13 fn draw(&self, id: ViewId, args: &mut DrawArgs) {
14 let rect = args.cx.layout.entry(id).or_default().rect;
15
16 args.vger.save();
17 (self.func)(args.cx, rect, args.vger);
18 args.vger.restore();
19 }
20
21 fn layout(&self, id: ViewId, args: &mut LayoutArgs) -> LocalSize {
22 args.cx.layout.insert(
23 id,
24 LayoutBox {
25 rect: LocalRect::new(LocalPoint::zero(), args.sz),
26 offset: LocalOffset::zero(),
27 },
28 );
29 args.sz
30 }
31
32 fn hittest(&self, id: ViewId, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
33 let rect = cx.layout.entry(id).or_default().rect;
34
35 if rect.contains(pt) {
36 Some(id)
37 } else {
38 None
39 }
40 }
41
42 fn gc(&self, id: ViewId, _cx: &mut Context, map: &mut Vec<ViewId>) {
43 map.push(id);
44 }
45}
46
47pub fn canvas<F: Fn(&mut Context, LocalRect, &mut Vger) + 'static>(f: F) -> impl View {
49 Canvas { func: f }
50}
51
52impl<F> private::Sealed for Canvas<F> {}