// `canvas()`, ported from gpui/src/elements/canvas.rs: an element laid out by // its style that paints with a closure. ///| /// An element that paints itself with a closure (GPUI: `Canvas`). pub struct Canvas { priv base_style : @style.StyleRefinement priv prepaint_fn : ( @geometry.Bounds[@geometry.Pixels], @window.Window, @app.App, ) -> Unit priv paint_fn : (@geometry.Bounds[@geometry.Pixels], @window.Window, @app.App) -> Unit priv mut layout_style : @style.Style? } ///| /// A canvas that runs `paint` with its bounds during the paint phase, and /// `prepaint` (if given) during prepaint. Size it with style methods, e.g. /// `canvas(paint).size_full()`. pub fn canvas( paint : (@geometry.Bounds[@geometry.Pixels], @window.Window, @app.App) -> Unit, prepaint~ : (@geometry.Bounds[@geometry.Pixels], @window.Window, @app.App) -> Unit = fn( _, _, _, ) { }, ) -> Canvas { { base_style: @style.StyleRefinement::new(), prepaint_fn: prepaint, paint_fn: paint, layout_style: None, } } ///| pub impl @style.Styled for Canvas with fn style(self) { self.base_style } ///| pub impl @window.IntoElement for Canvas with fn into_any_element(self) { @window.AnyElement::new(self) } ///| pub extend Canvas with @window.IntoElement::{into_any_element} ///| impl @window.Element for Canvas with fn request_layout(self, window, _app) { let style = (Default::default() : @style.Style).refined(self.base_style) self.layout_style = Some(style) window.request_layout(style, []) } ///| impl @window.Element for Canvas with fn prepaint(self, bounds, window, app) { (self.prepaint_fn)(bounds, window, app) } ///| impl @window.Element for Canvas with fn paint(self, bounds, window, app) { guard self.layout_style is Some(style) else { abort("canvas painted before layout") } window.paint_style(style, bounds, fn() { (self.paint_fn)(bounds, window, app) }) }