ravenrs_widgets/
canvas.rs1use std::fmt;
2use std::rc::Rc;
3
4use ravenrs_core::{Rect, Scene};
5use ravenrs_layout::LayoutStyle;
6
7use crate::View;
8use crate::context::{BuildCx, DrawFn, NodeId, Paint};
9
10#[cfg_attr(feature = "live", ravenrs_live::expose(part = "fields"))]
28pub struct Canvas {
29 draw: DrawFn,
30 style: LayoutStyle,
31}
32
33impl fmt::Debug for Canvas {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 f.debug_struct("Canvas").field("style", &self.style).finish_non_exhaustive()
36 }
37}
38
39#[cfg_attr(feature = "live", ravenrs_live::expose)]
40impl Canvas {
41 pub fn new(draw: impl Fn(Rect, &mut Scene) + 'static) -> Self {
43 Self { draw: Rc::new(draw), style: LayoutStyle::leaf() }
44 }
45
46 pub fn size(mut self, width: f32, height: f32) -> Self {
48 self.style.width = Some(width);
49 self.style.height = Some(height);
50 self
51 }
52
53 pub fn grow(mut self, grow: f32) -> Self {
55 self.style.grow = grow;
56 self
57 }
58}
59
60impl View for Canvas {
61 fn build(&self, cx: &mut BuildCx<'_>) -> NodeId {
62 cx.container(self.style, &[], Paint::Custom(self.draw.clone()))
63 }
64}