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