1use crate::{Bounds, Element, IntoElement, Pixels, StyleRefinement, Styled, WindowContext};
2
3pub fn canvas(callback: impl 'static + FnOnce(Bounds<Pixels>, &mut WindowContext)) -> Canvas {
4 Canvas {
5 paint_callback: Box::new(callback),
6 style: Default::default(),
7 }
8}
9
10pub struct Canvas {
11 paint_callback: Box<dyn FnOnce(Bounds<Pixels>, &mut WindowContext)>,
12 style: StyleRefinement,
13}
14
15impl IntoElement for Canvas {
16 type Element = Self;
17
18 fn element_id(&self) -> Option<crate::ElementId> {
19 None
20 }
21
22 fn into_element(self) -> Self::Element {
23 self
24 }
25}
26
27impl Element for Canvas {
28 type State = ();
29
30 fn layout(
31 &mut self,
32 _: Option<Self::State>,
33 cx: &mut WindowContext,
34 ) -> (crate::LayoutId, Self::State) {
35 let layout_id = cx.request_layout(&self.style.clone().into(), []);
36 (layout_id, ())
37 }
38
39 fn paint(self, bounds: Bounds<Pixels>, _: &mut (), cx: &mut WindowContext) {
40 (self.paint_callback)(bounds, cx)
41 }
42}
43
44impl Styled for Canvas {
45 fn style(&mut self) -> &mut crate::StyleRefinement {
46 &mut self.style
47 }
48}