Merge PR #6410: Artboards: layers outside the boards stay visible over the pasteboard (#5712)

This commit is contained in:
aiongg committed 2026-10-21 21:56:21 +00:00
commit 20adb5f1d7
2 files changed
+176 -20

No files matched your search

+155 -10
View File
@@ -2,7 +2,7 @@
//! View › Fit Artboard on Screen, and the Properties panel section (X/Y/W/H, preset,
//! background). Everything that changes the document is `layer.artboard.set` and friends.
use egui::{Align2, Color32, Pos2, Rect, Stroke};
use egui::{Align2, Color32, Pos2, Stroke};
use photocraft_doc::{ArtboardBackground, Document, Layer};
use serde_json::{Value, json};
@@ -10,10 +10,24 @@ use crate::PhotocraftApp;
use crate::canvas::ViewXform;
use crate::theme::Tokens;
/// Screen rects covering the document image minus every artboard (vertical slabs between the
/// boards' x edges, split around the boards they cross).
pub fn pasteboard_rects(xf: &ViewXform, doc: &Document) -> Vec<Rect> {
/// Screen quads covering the document image minus every artboard (vertical slabs between the
/// boards' x edges, split around the boards they cross), and minus where visible layers outside
/// the boards draw: Photoshop shows those over the pasteboard, so a board added next to earlier
/// work must not paint over it (#5712). The Background and any layer that covers the whole
/// canvas (or may draw anywhere, such as a fill layer) are backdrop, not loose work: they keep
/// the pasteboard. Each piece is returned as its four screen corners rather than a screen
/// bounding box, so in a rotated view the pieces keep their shape and do not cover the boards
/// or the loose layers they were cut around.
pub fn pasteboard_quads(xf: &ViewXform, doc: &Document) -> Vec<[Pos2; 4]> {
let canvas = doc.bounds();
let background = doc.background().map(|b| b.id);
let loose: Vec<photocraft_geom::Rect> = doc
.layers
.iter()
.filter(|l| l.visible && !is_artboard(l) && Some(l.id) != background)
.filter_map(|l| photocraft_compose::composite_bounds(l, canvas))
.filter(|b| !b.is_empty() && !b.contains_rect(&canvas))
.collect();
let boards: Vec<photocraft_geom::Rect> = doc.artboards().iter().map(|b| b.2.rect.intersect(&canvas)).filter(|r| !r.is_empty()).collect();
let mut xs: Vec<i32> = vec![canvas.x0, canvas.x1];
for b in &boards {
@@ -29,17 +43,51 @@ pub fn pasteboard_rects(xf: &ViewXform, doc: &Document) -> Vec<Rect> {
let mut y = canvas.y0;
for (a, b) in spans {
if a > y {
out.push(xf.doc_rect(photocraft_geom::Rect::new(x0, y, x1, a)));
out.extend(subtract(photocraft_geom::Rect::new(x0, y, x1, a), &loose).into_iter().map(|r| quad(xf, r)));
}
y = y.max(b);
}
if y < canvas.y1 {
out.push(xf.doc_rect(photocraft_geom::Rect::new(x0, y, x1, canvas.y1)));
out.extend(subtract(photocraft_geom::Rect::new(x0, y, x1, canvas.y1), &loose).into_iter().map(|r| quad(xf, r)));
}
}
out
}
/// The corners of a document rect on screen, in order (a parallelogram when the view is rotated).
fn quad(xf: &ViewXform, r: photocraft_geom::Rect) -> [Pos2; 4] {
let (x0, y0, x1, y1) = (r.x0 as f32, r.y0 as f32, r.x1 as f32, r.y1 as f32);
[xf.to_screen(x0, y0), xf.to_screen(x1, y0), xf.to_screen(x1, y1), xf.to_screen(x0, y1)]
}
fn is_artboard(l: &Layer) -> bool {
matches!(&l.content, photocraft_doc::LayerContent::Group(g) if g.artboard.is_some())
}
/// `r` minus every hole, as disjoint rects (each hole splits a piece into at most four).
fn subtract(r: photocraft_geom::Rect, holes: &[photocraft_geom::Rect]) -> Vec<photocraft_geom::Rect> {
let mut pieces = vec![r];
for h in holes {
let mut next = Vec::with_capacity(pieces.len());
for p in pieces {
let i = p.intersect(h);
if i.is_empty() {
next.push(p);
continue;
}
let parts = [
photocraft_geom::Rect::new(p.x0, p.y0, p.x1, i.y0),
photocraft_geom::Rect::new(p.x0, i.y1, p.x1, p.y1),
photocraft_geom::Rect::new(p.x0, i.y0, i.x0, i.y1),
photocraft_geom::Rect::new(i.x1, i.y0, p.x1, i.y1),
];
next.extend(parts.into_iter().filter(|q| !q.is_empty()));
}
pieces = next;
}
pieces
}
/// Board outlines and names (the active board's name is highlighted, as in Photoshop).
pub fn draw_frames(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {
let Some(st) = app.session.active() else { return };
@@ -166,6 +214,7 @@ pub fn properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) {
mod tests {
use super::*;
use crate::menus::{invoke as menu, is_live, menu_items};
use egui::Rect;
fn app() -> (PhotocraftApp, egui::Context) {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
@@ -188,11 +237,107 @@ mod tests {
rotation: 0.0,
aspect: 1.0,
};
let rects = pasteboard_rects(&xf, &doc);
let area: f32 = rects.iter().map(|r| r.area()).sum();
let quads = pasteboard_quads(&xf, &doc);
let area: f32 = quads.iter().map(quad_area).sum();
// 100×50 canvas − 40×50 − 40×30 boards.
assert!((area - (5000.0 - 2000.0 - 1200.0)).abs() < 0.5, "{rects:?}");
assert!(rects.iter().all(|r| !r.intersects(Rect::from_min_max(Pos2::new(0.5, 0.5), Pos2::new(39.5, 49.5)))));
assert!((area - (5000.0 - 2000.0 - 1200.0)).abs() < 0.5, "{quads:?}");
assert!(quads.iter().all(|q| !Rect::from_points(q).intersects(Rect::from_min_max(Pos2::new(0.5, 0.5), Pos2::new(39.5, 49.5)))));
}
#[test]
fn layers_outside_the_boards_show_over_the_pasteboard() {
// #5712: a board drawn left of earlier work must not cover it with pasteboard.
let (mut app, _) = app();
app.run("layer.new.artboard", json!({"rect": [0, 0, 40, 50]})).unwrap();
let mut doc = (*app.session.active().unwrap().doc).clone();
let mut px = photocraft_doc::Layer::raster("Work", doc.pixel_format());
px.surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(60, 10, 80, 30), &[1.0, 0.0, 0.0, 1.0]);
doc.layers.insert(0, px);
let xf = ViewXform {
rect: Rect::from_min_size(Pos2::ZERO, egui::vec2(100.0, 50.0)),
zoom: 1.0,
center: [50.0, 25.0],
flip: false,
rotation: 0.0,
aspect: 1.0,
};
let quads = pasteboard_quads(&xf, &doc);
let area: f32 = quads.iter().map(quad_area).sum();
// 100×50 canvas − the 40×50 board − the 20×20 loose pixels.
assert!((area - (5000.0 - 2000.0 - 400.0)).abs() < 0.5, "{quads:?}");
assert!(quads.iter().all(|q| !Rect::from_points(q).intersects(Rect::from_min_max(Pos2::new(60.5, 10.5), Pos2::new(79.5, 29.5)))));
}
#[test]
fn full_canvas_layers_keep_the_pasteboard() {
// The white Background (from `app()`) and a loose layer filling the whole canvas are
// backdrop: the pasteboard still shows outside the board.
let (mut app, _) = app();
app.run("layer.new.artboard", json!({"rect": [0, 0, 40, 50]})).unwrap();
let mut doc = (*app.session.active().unwrap().doc).clone();
assert!(doc.background().is_some());
let mut px = photocraft_doc::Layer::raster("Wash", doc.pixel_format());
px.surface_mut().unwrap().fill_rect(doc.bounds(), &[0.0, 0.0, 1.0, 1.0]);
doc.layers.push(px);
let xf = ViewXform {
rect: Rect::from_min_size(Pos2::ZERO, egui::vec2(100.0, 50.0)),
zoom: 1.0,
center: [50.0, 25.0],
flip: false,
rotation: 0.0,
aspect: 1.0,
};
let area: f32 = pasteboard_quads(&xf, &doc).iter().map(quad_area).sum();
assert!((area - (5000.0 - 2000.0)).abs() < 0.5);
}
fn quad_area(q: &[Pos2; 4]) -> f32 {
(0..4).map(|i| q[i].x * q[(i + 1) % 4].y - q[(i + 1) % 4].x * q[i].y).sum::<f32>().abs() / 2.0
}
/// Whether `p` lies strictly inside the convex quad `q` (either winding).
fn inside(q: &[Pos2; 4], p: Pos2) -> bool {
let side = |i: usize| (q[(i + 1) % 4] - q[i]).x * (p - q[i]).y - (q[(i + 1) % 4] - q[i]).y * (p - q[i]).x;
let s: Vec<f32> = (0..4).map(side).collect();
s.iter().all(|&v| v > 0.0) || s.iter().all(|&v| v < 0.0)
}
#[test]
fn rotated_view_keeps_the_holes_around_loose_layers() {
// A loose layer in the middle of the pasteboard, seen at 45°: the strips around it must
// not cover it (their screen bounding boxes would).
let (mut app, _) = app();
app.run("layer.new.artboard", json!({"rect": [0, 0, 20, 50]})).unwrap();
let mut doc = (*app.session.active().unwrap().doc).clone();
let mut px = photocraft_doc::Layer::raster("Work", doc.pixel_format());
px.surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(50, 15, 70, 35), &[1.0, 0.0, 0.0, 1.0]);
doc.layers.insert(0, px);
let xf = ViewXform {
rect: Rect::from_min_size(Pos2::ZERO, egui::vec2(200.0, 200.0)),
zoom: 1.0,
center: [50.0, 25.0],
flip: true,
rotation: 45.0,
aspect: 1.0,
};
let quads = pasteboard_quads(&xf, &doc);
let area: f32 = quads.iter().map(quad_area).sum();
// Rotation keeps areas: 100×50 canvas − the 20×50 board − the 20×20 loose pixels.
assert!((area - (5000.0 - 1000.0 - 400.0)).abs() < 0.5, "{quads:?}");
for (x, y) in [(60.0, 25.0), (51.0, 16.0), (69.0, 34.0), (10.0, 25.0)] {
let p = xf.to_screen(x, y);
assert!(quads.iter().all(|q| !inside(q, p)), "({x}, {y}) is covered");
}
// Pasteboard still shows next to the hole.
assert!(quads.iter().any(|q| inside(q, xf.to_screen(80.0, 25.0))));
}
#[test]
fn subtract_splits_around_holes() {
let r = photocraft_geom::Rect::new(0, 0, 10, 10);
let parts = subtract(r, &[photocraft_geom::Rect::new(2, 2, 4, 4), photocraft_geom::Rect::new(20, 20, 30, 30)]);
assert_eq!(parts.iter().map(|p| i64::from(p.width()) * i64::from(p.height())).sum::<i64>(), 96);
assert!(subtract(r, &[photocraft_geom::Rect::new(-5, -5, 50, 50)]).is_empty());
}
#[test]
+21 -10
View File
@@ -3275,21 +3275,32 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
let t = crate::theme::Tokens::get(&ctx);
let pasteboard = crate::prefs_ui::pasteboard_color(app);
let dot_tex = dots(&ctx, &t);
for r in crate::artboard_ui::pasteboard_rects(&xf, &doc) {
let r = r.intersect(rect);
if !r.is_positive() {
// Cut around the layers as they are shown, so a loose layer dragged with the Move tool
// (or otherwise previewed) is not covered where it now draws.
let shown = display_doc(app, idx).0;
// Each piece is painted as its own quad (the painter clips to the canvas), so a
// rotated view does not paint a piece's bounding box over the boards or loose work.
let fill = |q: &[Pos2; 4], tex: Option<egui::TextureId>, color: Color32| {
let mut mesh = tex.map_or_else(egui::Mesh::default, egui::Mesh::with_texture);
for &p in q {
// Same phase as the dots around the document.
let uv = if tex.is_some() { ((p - rect.min) / 22.0).to_pos2() } else { egui::epaint::WHITE_UV };
mesh.vertices.push(egui::epaint::Vertex { pos: p, uv, color });
}
mesh.add_triangle(0, 1, 2);
mesh.add_triangle(0, 2, 3);
painter.add(mesh);
};
for q in crate::artboard_ui::pasteboard_quads(&xf, &shown) {
if !Rect::from_points(&q).intersects(rect) {
continue;
}
match pasteboard {
Some(c) => {
painter.rect_filled(r, 0.0, c);
}
Some(c) => fill(&q, None, c),
None => {
painter.rect_filled(r, 0.0, t.canvas);
fill(&q, None, t.canvas);
if let Some(id) = dot_tex {
// Same phase as the dots around the document.
let uv = Rect::from_min_max(((r.min - rect.min) / 22.0).to_pos2(), ((r.max - rect.min) / 22.0).to_pos2());
painter.image(id, r, uv, Color32::WHITE);
fill(&q, Some(id), Color32::WHITE);
}
}
}