canvas.rs

 1use refineable::Refineable as _;
 2
 3use crate::{Bounds, Element, ElementContext, IntoElement, Pixels, Style, StyleRefinement, Styled};
 4
 5/// Construct a canvas element with the given paint callback.
 6/// Useful for adding short term custom drawing to a view.
 7pub fn canvas<T>(
 8    prepaint: impl 'static + FnOnce(Bounds<Pixels>, &mut ElementContext) -> T,
 9    paint: impl 'static + FnOnce(Bounds<Pixels>, T, &mut ElementContext),
10) -> Canvas<T> {
11    Canvas {
12        prepaint: Some(Box::new(prepaint)),
13        paint: Some(Box::new(paint)),
14        style: StyleRefinement::default(),
15    }
16}
17
18/// A canvas element, meant for accessing the low level paint API without defining a whole
19/// custom element
20pub struct Canvas<T> {
21    prepaint: Option<Box<dyn FnOnce(Bounds<Pixels>, &mut ElementContext) -> T>>,
22    paint: Option<Box<dyn FnOnce(Bounds<Pixels>, T, &mut ElementContext)>>,
23    style: StyleRefinement,
24}
25
26impl<T: 'static> IntoElement for Canvas<T> {
27    type Element = Self;
28
29    fn into_element(self) -> Self::Element {
30        self
31    }
32}
33
34impl<T: 'static> Element for Canvas<T> {
35    type RequestLayoutState = Style;
36    type PrepaintState = Option<T>;
37
38    fn request_layout(
39        &mut self,
40        cx: &mut ElementContext,
41    ) -> (crate::LayoutId, Self::RequestLayoutState) {
42        let mut style = Style::default();
43        style.refine(&self.style);
44        let layout_id = cx.request_layout(&style, []);
45        (layout_id, style)
46    }
47
48    fn prepaint(
49        &mut self,
50        bounds: Bounds<Pixels>,
51        _request_layout: &mut Style,
52        cx: &mut ElementContext,
53    ) -> Option<T> {
54        Some(self.prepaint.take().unwrap()(bounds, cx))
55    }
56
57    fn paint(
58        &mut self,
59        bounds: Bounds<Pixels>,
60        style: &mut Style,
61        prepaint: &mut Self::PrepaintState,
62        cx: &mut ElementContext,
63    ) {
64        let prepaint = prepaint.take().unwrap();
65        style.paint(bounds, cx, |cx| {
66            (self.paint.take().unwrap())(bounds, prepaint, cx)
67        });
68    }
69}
70
71impl<T> Styled for Canvas<T> {
72    fn style(&mut self) -> &mut crate::StyleRefinement {
73        &mut self.style
74    }
75}