diff --git a/crates/ui-canvas/src/canvas/pasteboard/mod.rs b/crates/ui-canvas/src/canvas/pasteboard/mod.rs index 24f5d69b0..7c091eeb9 100644 --- a/crates/ui-canvas/src/canvas/pasteboard/mod.rs +++ b/crates/ui-canvas/src/canvas/pasteboard/mod.rs @@ -8,7 +8,7 @@ mod stories; use super::space::canvas_space; use crate::contract::{Access, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role}; -use crate::primitives::{CheckerArea, PaintShape, Rule, StrokeWidth, TextRun, paint_surround}; +use crate::primitives::{CheckerArea, PaintShape, PaintSlot, Rule, StrokeWidth, TextRun, paint_surround}; use crate::runtime::env::Label; use crate::style::{ ComponentStyle, DataColor, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceEdge, SurfaceKind, SurfaceStyle, SurroundLook, TypeRole, @@ -163,7 +163,12 @@ impl Outcome for PasteboardOutput { } /// The pasteboard and page frames under a document. Show it first in the canvas body, then the document, then the -/// overlays. The canvas must be given a view; without one nothing is shown. +/// overlays. The pages' shadows and fills paint in the canvas's picture slot, ahead of the pictures shown after it, so +/// a picture under the controls ([`RasterDisplay::picture`](super::RasterDisplay::picture)) lies over them. The +/// surround of plain pages paints there too; the surround between artboards (pages marked with [`PageFrame::active()`]) paints over the +/// pictures, hiding the document outside the boards except where [`uncovered`](Self::uncovered) leaves it open. The +/// page edges and names paint in place, over the pictures. The canvas must be given a view; without one nothing is +/// shown. #[derive(crate::contract::Component, crate::contract::Props)] #[component(canvas_control, content_painter)] pub struct Pasteboard<'a> { @@ -349,15 +354,28 @@ impl Component for Pasteboard<'_> { let (view, style) = (space.view, cx.style); let draw = ui.draw(); let edges = ui.content_painter(cx); + // What lies under the pages (their shadows and fills) marks where it starts; it moves into the canvas's picture + // slot, in show order before the pictures, so every picture lies over it. The surround of plain pages goes + // with it, so a picture reaching past the page (copies around it, pixels past the canvas) shows over it; the + // surround between artboards paints over the pictures and hides the document there, except where `uncovered` + // leaves it open. The page edges and names, which mark the pages, paint in place over the pictures. Outside a + // canvas, or in a space with no slot, everything paints in place. + let under = space.picture_slot().map(|_| PaintSlot::reserve(&edges.painter)); + let artboards = self.pages.iter().any(|page| page.active.is_some()); let polygon = |quad: &[Pos2; 4]| PaintShape::Polygon(quad.to_vec()); // A colour the canvas sets is a plain data fill, with no pattern, as in production. let fill = space.pasteboard().fill(); let look = style.surround.shown(cx.env.canvas_pattern()); - for quad in Self::quads(&view, self.pages, self.uncovered).iter().filter(|quad| bounds(quad).intersects(view.rect)) { - match fill { - Some(color) => draw.data_fill(&polygon(quad), color), - None => paint_surround(&edges.painter, CheckerArea::Quad(*quad), &look), + let surround = || { + for quad in Self::quads(&view, self.pages, self.uncovered).iter().filter(|quad| bounds(quad).intersects(view.rect)) { + match fill { + Some(color) => draw.data_fill(&polygon(quad), color), + None => paint_surround(&edges.painter, CheckerArea::Quad(*quad), &look), + } } + }; + if !artboards { + surround(); } let aligned = view.axis_aligned(); let edge = |quad: &[Pos2; 4], color: StyleColor| { @@ -372,12 +390,9 @@ impl Component for Pasteboard<'_> { draw.stroke(&polygon(quad), StrokeWidth::Thin, color); } }; - for (at, page) in self.pages.iter().enumerate() { + let shown = |page: &PageFrame| page.name.is_some() || bounds(&view.quad(page.rect)).intersects(view.rect); + for page in self.pages.iter().filter(|page| shown(*page)) { let quad = view.quad(page.rect); - let named = page.name.is_some(); - if !bounds(&quad).intersects(view.rect) && !named { - continue; - } if self.border == PageBorder::DropShadow { if aligned { draw.surface_styled(bounds(&quad), &style.shadow); @@ -388,6 +403,17 @@ impl Component for Pasteboard<'_> { if let Some(color) = page.fill { draw.data_fill(&polygon(&quad), color); } + } + if let Some(under) = under { + let ctx = edges.painter.ctx().clone(); + let shapes = under.take_after(&ctx); + space.paint_picture(&ctx, shapes); + } + if artboards { + surround(); + } + for (at, page) in self.pages.iter().enumerate().filter(|(_, page)| shown(*page)) { + let quad = view.quad(page.rect); if self.border == PageBorder::Line { edge(&quad, style.outline); } diff --git a/crates/ui-canvas/tests/it/canvas/raster.rs b/crates/ui-canvas/tests/it/canvas/raster.rs index f05ce03f7..08f84ef20 100644 --- a/crates/ui-canvas/tests/it/canvas/raster.rs +++ b/crates/ui-canvas/tests/it/canvas/raster.rs @@ -7,6 +7,7 @@ use craft_ui::canvas::{AppPaint, Canvas, Extent, PageFrame, Pasteboard, PixelRec use craft_ui::canvas_controls::PixelGrid; use craft_ui::contract::{Show as _, ShowContainer as _}; use craft_ui::primitives::{Checker, Revision}; +use craft_ui::style::DataColor; use craft_ui::testing::{Fixture, trial}; use egui::{Color32, ColorImage, Mesh, PaintCallback, Pos2, Rect, Shape, TextureId, Vec2}; use std::cell::{Cell, RefCell}; @@ -426,3 +427,80 @@ fn a_picture_whose_area_is_off_the_document_has_its_checkerboard_and_edge_there( } } } + +/// A picture shown under the canvas controls (in the canvas's picture slot) shows its own colour: the canvas surface, +/// the page's drop shadow and its fill, which the pasteboard shown before it paints, all lie under it. Between +/// artboards the pasteboard covers the picture, except where it is left uncovered (a loose layer); on a board the +/// picture shows its own colour. +#[trial] +fn a_picture_in_the_canvas_slot_shows_its_own_colour_over_the_pasteboard() { + let page = Rect64::new(0.0, 0.0, 400.0, 300.0); + let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, CANVAS), page.center(), 1.0); + let at = |x: f64, y: f64| view.to_screen(Point::new(x, y)); + let plain = slot_canvas(view, vec![PageFrame::new(page).fill(DataColor::new(Color32::WHITE))], Vec::new()); + assert!(shows_picture(&plain, at(200.0, 150.0)), "the plain page's centre shows {:?}", colour_at(&plain, at(200.0, 150.0))); + let boards = vec![ + PageFrame::new(Rect64::new(20.0, 20.0, 180.0, 280.0)).name("One").active(true), + PageFrame::new(Rect64::new(220.0, 20.0, 380.0, 280.0)).name("Two").active(false), + ]; + let loose = vec![Rect64::new(185.0, 20.0, 215.0, 60.0)]; + let artboards = slot_canvas(view, boards, loose); + for (x, y, what) in [(100.0, 150.0, "the first board"), (300.0, 150.0, "the second board"), (200.0, 40.0, "the loose layer")] { + assert!(shows_picture(&artboards, at(x, y)), "{what} shows {:?}", colour_at(&artboards, at(x, y))); + } + assert!(!shows_picture(&artboards, at(200.0, 150.0)), "the pasteboard covers the document between the boards"); +} + +/// The picture's own colour. +const PICTURE: Color32 = Color32::from_rgb(204, 128, 77); + +/// The shapes of a frame of a canvas whose body shows a pasteboard with `pages` and `uncovered`, then a picture in +/// the canvas's slot that fills the canvas with [`PICTURE`]. +fn slot_canvas(view: CanvasView, pages: Vec, uncovered: Vec) -> Vec { + let size = PixelSize::new(400, 300); + let mut fixture = Fixture::new(move |ui| { + Canvas::new("Document").view(view).show(ui, |ui, body| { + let _ = Pasteboard::new("Pasteboard", &pages).uncovered(&uncovered).show_at(ui, body.rect); + let paint = AppPaint::new(|painter, view: CanvasView| { + painter.painter.add(Shape::rect_filled(view.rect, 0.0, PICTURE)); + }); + RasterDisplay::painted("Picture", size, paint).picture().show_at(ui, body.rect); + }); + }); + fixture.room(CANVAS); + fixture.settle(); + fixture.frame_shapes() +} + +/// Whether `shapes` leave [`PICTURE`] at `at`, within one level. +fn shows_picture(shapes: &[Shape], at: Pos2) -> bool { + let want = [PICTURE.r(), PICTURE.g(), PICTURE.b(), PICTURE.a()].map(f32::from); + colour_at(shapes, at).iter().zip(want).all(|(got, want)| (got - want).abs() <= 1.0) +} + +/// The colour `shapes` leave at `at` (premultiplied, 0..=255 per channel): each filled rect, closed path and untextured +/// mesh that covers the point, blended over what was painted before it as the renderer blends. +fn colour_at(shapes: &[Shape], at: Pos2) -> [f32; 4] { + shapes.iter().fold([0.0; 4], |under, shape| { + let fill = match shape { + Shape::Rect(rect) if rect.rect.contains(at) => rect.fill, + Shape::Path(path) if path.closed && encloses(&path.points, at) => path.fill, + Shape::Mesh(mesh) if mesh.texture_id == TextureId::default() => mesh + .indices + .chunks_exact(3) + .map(|triangle| triangle.iter().map(|&index| &mesh.vertices[index as usize]).collect::>()) + .find(|corners| encloses(&corners.iter().map(|vertex| vertex.pos).collect::>(), at)) + .map_or(Color32::TRANSPARENT, |corners| corners[0].color), + _ => Color32::TRANSPARENT, + }; + let top = [fill.r(), fill.g(), fill.b(), fill.a()].map(f32::from); + let keep = 1.0 - top[3] / 255.0; + [0, 1, 2, 3].map(|channel| top[channel] + under[channel] * keep) + }) +} + +/// Whether the polygon `points` holds `at` (even-odd rule). +fn encloses(points: &[Pos2], at: Pos2) -> bool { + let edges = points.iter().zip(points.iter().cycle().skip(1)); + edges.filter(|(a, b)| (a.y > at.y) != (b.y > at.y) && at.x < a.x + (at.y - a.y) / (b.y - a.y) * (b.x - a.x)).count() % 2 == 1 +}