diff --git a/crates/ui-canvas/src/canvas/raster/mod.rs b/crates/ui-canvas/src/canvas/raster/mod.rs index cc95aa33f..16291e3cf 100644 --- a/crates/ui-canvas/src/canvas/raster/mod.rs +++ b/crates/ui-canvas/src/canvas/raster/mod.rs @@ -66,6 +66,22 @@ pub enum Extent { Tiled, } +/// `outer` without `inner`: the bands above, below, left and right of it, empty ones left out. +fn ring(outer: PixelRect, inner: PixelRect) -> impl Iterator { + let i = inner.intersect(&outer); + let bands = if i.is_empty() { + [outer, PixelRect::default(), PixelRect::default(), PixelRect::default()] + } else { + [ + PixelRect::new(outer.x0, outer.y0, outer.x1, i.y0), + PixelRect::new(outer.x0, i.y1, outer.x1, outer.y1), + PixelRect::new(outer.x0, i.y0, i.x0, i.y1), + PixelRect::new(i.x1, i.y0, outer.x1, i.y1), + ] + }; + bands.into_iter().filter(|band| !band.is_empty()) +} + /// Most copies a tiled display draws along each axis, the document included. A tiny document zoomed far out would /// otherwise need thousands of copies per pass; past this the pattern stops, and the run is centred on the view. const MOST_COPIES: i32 = 31; @@ -262,6 +278,10 @@ pub struct RasterDisplay<'a> { /// How far the picture reaches past the document's edge. #[prop] extent: Extent, + /// What the region of [`Extent::Region`] shows past the document, under the picture: the checkerboard when none, + /// else this colour (Crop's preview of the area a crop adds to a Background, which takes the background colour). + #[prop] + region_fill: Option, /// The display paints in the canvas's picture slot, under every canvas control of the body, when the canvas /// space carries a slot; otherwise it paints in place. A display under the controls fills its canvas, so the /// canvas content rect, the slot's clip, is its clip. It declares no faded look of its own (`DisabledLook::States`): @@ -331,6 +351,7 @@ impl<'a> RasterDisplay<'a> { checkerboard: None, outlined: false, extent: Extent::Document, + region_fill: None, under_controls: false, read: PixelRect::default(), shown: None, @@ -540,6 +561,14 @@ impl Component for RasterDisplay<'_> { painter.painter.add(checker.shape(painter.painter.ctx(), &ui.env.tokens, CheckerArea::Quad(area), origin)); } } + // The region's colour past the document, over the checkerboard and under the picture. Only a region reaches + // past the document: a mapped or tiled picture's bounds are the document. + if let Some(fill) = self.region_fill { + for band in ring(shown.bounds, PixelRect::of_size(shown.size)) { + let band = Rect64::new(f64::from(band.x0), f64::from(band.y0), f64::from(band.x1), f64::from(band.y1)); + painter.painter.add(Shape::convex_polygon(shown.view.quad(band).to_vec(), fill, egui::Stroke::NONE)); + } + } match (&mut self.picture, &shown.textures, &self.mapping) { (Picture::Source(_), Some(textures), Some(mapping)) => { let (texture, factor) = textures.base; diff --git a/crates/ui-canvas/tests/it/canvas/raster.rs b/crates/ui-canvas/tests/it/canvas/raster.rs index f5d1e4c10..29f263535 100644 --- a/crates/ui-canvas/tests/it/canvas/raster.rs +++ b/crates/ui-canvas/tests/it/canvas/raster.rs @@ -420,6 +420,72 @@ fn a_region_past_the_document_draws_out_to_the_region() { assert!(document.reads.borrow().contains(&(moved, 1)), "a moved region is read: {:?}", document.reads.borrow()); } +/// One frame of a display of `document` reaching out to `region`, with `fill` past the document: its shapes and the +/// view. +fn region_fill_shapes(document: &Rc, extent: Extent, fill: Option, painted: bool) -> (Vec, CanvasView) { + let center = Point::new(70.0, 45.0); + let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, CANVAS), center, 2.0); + let document = Rc::clone(document); + let callback: Arc = Arc::new(()); + let mut fixture = Fixture::new(move |ui| { + Canvas::new("Document").view(view).show(ui, |ui, body| { + let display = if painted { + let callback = Arc::clone(&callback); + let paint = AppPaint::new(move |painter, view: CanvasView| { + painter.painter.add(Shape::Callback(PaintCallback { rect: view.rect, callback: Arc::clone(&callback) })); + }); + RasterDisplay::painted("Picture", document.size, paint) + } else { + RasterDisplay::new("Picture", &*document) + }; + let display = display.checkerboard(Checker::new()).extent(extent); + let display = match fill { + Some(fill) => display.region_fill(fill), + None => display, + }; + display.show_at(ui, body.rect); + }); + }); + fixture.room(CANVAS); + fixture.settle(); + (fixture.frame_shapes(), view) +} + +#[trial] +fn a_region_fill_shows_past_the_document_under_the_picture() { + // Crop's preview of a crop that grows a Background: the area past the canvas takes the background colour. + let document = Document::new(100, 70); + let region = PixelRect::new(-60, -30, 200, 120); + let red = Color32::from_rgb(255, 0, 0); + let filled = |shape: &Shape| matches!(shape, Shape::Path(path) if path.fill == red); + for painted in [false, true] { + let (shapes, view) = region_fill_shapes(&document, Extent::Region(region), Some(red), painted); + let at = |x: f64, y: f64| view.to_screen(Point::new(x, y)); + // Left of, above, below and right of the document, and at a corner of the region: the fill. + for (x, y) in [(-30.0, 10.0), (50.0, -10.0), (50.0, 100.0), (150.0, 10.0), (-55.0, 115.0)] { + assert_eq!(colour_at(&shapes, at(x, y)), [255.0, 0.0, 0.0, 255.0], "painted {painted}: ({x}, {y}) past the document is filled"); + } + assert_ne!(colour_at(&shapes, at(50.0, 35.0)), [255.0, 0.0, 0.0, 255.0], "painted {painted}: the document itself is not filled"); + assert_ne!(colour_at(&shapes, at(-70.0, 10.0)), [255.0, 0.0, 0.0, 255.0], "painted {painted}: nothing past the region is filled"); + // The fill lies over the checkerboard and under the picture, which shows layers past the canvas over it. + let first_fill = shapes.iter().position(filled).expect("a fill"); + let last_fill = shapes.iter().rposition(filled).expect("a fill"); + let checker = shapes.iter().position(|shape| matches!(shape, Shape::Mesh(mesh) if mesh.texture_id != TextureId::default())); + let picture = shapes + .iter() + .rposition(|shape| matches!(shape, Shape::Callback(_)) || matches!(shape, Shape::Mesh(mesh) if mesh.texture_id != TextureId::default())); + assert!(checker.is_some_and(|c| c < first_fill), "painted {painted}: over the checkerboard"); + assert!(picture.is_some_and(|p| last_fill < p), "painted {painted}: under the picture"); + + // No fill: the checkerboard alone, as before. + let (plain, _) = region_fill_shapes(&document, Extent::Region(region), None, painted); + assert!(!plain.iter().any(filled), "painted {painted}: no fill without a colour"); + // A fill with nothing past the document draws nothing. + let (document_only, _) = region_fill_shapes(&document, Extent::Document, Some(red), painted); + assert!(!document_only.iter().any(filled), "painted {painted}: no region, no fill"); + } +} + #[trial] fn a_picture_whose_area_is_off_the_document_has_its_checkerboard_and_edge_there() { // A 100 x 70 document; its picture turned a quarter about the document's centre (70 x 100), then moved past the