mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 14:51:31 -05:00
Merge pull request #19106
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
use craft::behavior::CanvasView;
|
||||
use craft::behavior::kurbo::Point;
|
||||
use craft::ui::canvas::Pasteboard;
|
||||
use egui::{Rect, pos2};
|
||||
use photocraft_color::Color;
|
||||
use photocraft_doc::Fill;
|
||||
use photocraft_engine::Session;
|
||||
use photocraft_geom::Rect as PixelRect;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn in_pasteboard(doc: &Document, point: Point) -> bool {
|
||||
let screen = Rect::from_min_max(pos2(-40.0, -40.0), pos2(440.0, 340.0));
|
||||
let view = CanvasView::new(screen, Point::new(200.0, 150.0), 1.0);
|
||||
let pages: Vec<_> = doc.artboards().into_iter().map(|(_, _, board)| PageFrame::new(doc_rect(board.rect))).collect();
|
||||
let point = view.to_screen(point);
|
||||
Pasteboard::quads(&view, &pages, &uncovered(doc)).iter().any(|quad| Rect::from_points(quad).contains(point))
|
||||
}
|
||||
|
||||
fn fill_square(session: &mut Session, x: i32, y: i32, color: &str) {
|
||||
session.execute("select.rect", json!({"x": x, "y": y, "width": 100, "height": 100})).unwrap();
|
||||
session.execute("edit.fill", json!({"color": color})).unwrap();
|
||||
session.execute("select.deselect", json!({})).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layer_from_background_stays_visible_around_the_artboard() {
|
||||
let mut session = Session::new();
|
||||
session.execute("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap();
|
||||
let background = session.active().unwrap().doc.background().unwrap().id;
|
||||
fill_square(&mut session, 50, 50, "#ff0000");
|
||||
session.execute("layer.new.layer", json!({"name": "Top"})).unwrap();
|
||||
fill_square(&mut session, 200, 100, "#0000ff");
|
||||
session.execute("layer.new.artboardFromLayers", json!({})).unwrap();
|
||||
let around = Point::new(100.0, 100.0);
|
||||
let on_board = Point::new(250.0, 150.0);
|
||||
let outside = Point::new(420.0, 320.0);
|
||||
assert!(!in_pasteboard(&session.active().unwrap().doc, around), "the original Background is visible");
|
||||
|
||||
session.execute("layer.new.layerFromBackground", json!({})).unwrap();
|
||||
let doc = &session.active().unwrap().doc;
|
||||
assert_eq!(doc.layers[0].id, background);
|
||||
assert_eq!(doc.layers[0].name, "Layer 0");
|
||||
assert!(doc.background().is_none());
|
||||
assert!(!in_pasteboard(doc, around), "the converted pixel layer remains visible around the board");
|
||||
assert!(!in_pasteboard(doc, on_board));
|
||||
assert!(in_pasteboard(doc, outside));
|
||||
assert_eq!(photocraft_compose::render(doc, PixelRect::new(100, 100, 101, 101)).px[0], [1.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(photocraft_compose::render(doc, PixelRect::new(250, 150, 251, 151)).px[0], [0.0, 0.0, 1.0, 1.0]);
|
||||
|
||||
session.execute("layer.setProps", json!({"layer": background.0, "visible": false})).unwrap();
|
||||
assert!(in_pasteboard(&session.active().unwrap().doc, around), "hiding the layer restores the pasteboard");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_canvas_wide_fill_keeps_the_pasteboard_over_canvas_wide_pixels() {
|
||||
let mut session = Session::new();
|
||||
session.execute("file.new", json!({"width": 400, "height": 300, "background": "transparent"})).unwrap();
|
||||
session.execute("layer.new.artboard", json!({"rect": [200, 100, 300, 200]})).unwrap();
|
||||
let mut doc = (*session.active().unwrap().doc).clone();
|
||||
let mut pixels = Layer::raster("Pixels", doc.pixel_format());
|
||||
pixels.surface_mut().unwrap().fill_rect(doc.bounds(), &[1.0, 0.0, 0.0, 1.0]);
|
||||
doc.layers.push(pixels);
|
||||
let around = Point::new(100.0, 100.0);
|
||||
assert!(!in_pasteboard(&doc, around), "bounded pixels alone open the canvas");
|
||||
|
||||
let fill = Layer::new("Fill", LayerContent::Fill(Fill::Solid(Color::rgb(0.0, 1.0, 0.0))));
|
||||
doc.layers.push(fill);
|
||||
assert!(in_pasteboard(&doc, around), "the canvas-wide fill keeps the pasteboard");
|
||||
assert!(!in_pasteboard(&doc, Point::new(250.0, 150.0)), "the board remains visible");
|
||||
|
||||
doc.layers.last_mut().unwrap().visible = false;
|
||||
assert!(!in_pasteboard(&doc, around), "a hidden fill leaves the raster visible");
|
||||
}
|
||||
@@ -10,7 +10,7 @@ use craft::behavior::kurbo::{Rect as DocRect, Size};
|
||||
use craft::draw::ui::artboard::{ArtboardBackground as BoardBackground, ArtboardItem, ArtboardPreset, ArtboardProperties};
|
||||
use craft::theme::Srgba;
|
||||
use craft::ui::canvas::PageFrame;
|
||||
use photocraft_doc::{ArtboardBackground, Document, Layer};
|
||||
use photocraft_doc::{ArtboardBackground, Document, Layer, LayerContent};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
@@ -33,29 +33,37 @@ pub(crate) fn pages(app: &PhotocraftApp, idx: usize) -> Arc<[PageFrame]> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Where visible layers outside the boards draw. Loose work stays over the pasteboard (#5712),
|
||||
/// and a visible Background opens the whole canvas (#17309) unless an ordinary canvas-wide
|
||||
/// layer keeps the pasteboard. The framework subtracts these rects and the pages from it.
|
||||
/// Where visible layers outside the boards draw. Loose work stays over the pasteboard (#5712).
|
||||
/// A bounded pixel layer spanning the canvas opens it unless a canvas-wide fill keeps the
|
||||
/// pasteboard. A visible Background opens it when no other canvas-wide layer is visible (#17309).
|
||||
/// The framework subtracts these rects and the pages from the pasteboard.
|
||||
pub(crate) fn uncovered(doc: &Document) -> Vec<DocRect> {
|
||||
let canvas = doc.bounds();
|
||||
let background = doc.background();
|
||||
let background_id = background.map(|layer| layer.id);
|
||||
let mut regions = Vec::new();
|
||||
let mut has_canvas_wide_layer = false;
|
||||
let layer_bounds = doc
|
||||
.layers
|
||||
.iter()
|
||||
.filter(|l| l.visible && !is_artboard(l) && Some(l.id) != background_id)
|
||||
.map(|l| photocraft_compose::composite_bounds(l, canvas).unwrap_or(canvas))
|
||||
.filter(|b| !b.is_empty());
|
||||
for bounds in layer_bounds {
|
||||
let mut has_canvas_wide_pixels = false;
|
||||
let mut has_canvas_wide_fill = false;
|
||||
for layer in doc.layers.iter().filter(|l| l.visible && !is_artboard(l) && Some(l.id) != background_id) {
|
||||
let Some(bounds) = photocraft_compose::composite_bounds(layer, canvas) else {
|
||||
has_canvas_wide_layer = true;
|
||||
has_canvas_wide_fill |= matches!(layer.content, LayerContent::Fill(_));
|
||||
continue;
|
||||
};
|
||||
if bounds.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if bounds.contains_rect(&canvas) {
|
||||
has_canvas_wide_layer = true;
|
||||
has_canvas_wide_pixels |= matches!(layer.content, LayerContent::Raster(_));
|
||||
has_canvas_wide_fill |= matches!(layer.content, LayerContent::Fill(_));
|
||||
} else {
|
||||
regions.push(doc_rect(bounds));
|
||||
}
|
||||
}
|
||||
if background.is_some_and(|layer| layer.visible) && !canvas.is_empty() && !has_canvas_wide_layer {
|
||||
let show_background = background.is_some_and(|layer| layer.visible) && !has_canvas_wide_layer;
|
||||
if !canvas.is_empty() && ((has_canvas_wide_pixels && !has_canvas_wide_fill) || show_background) {
|
||||
regions.push(doc_rect(canvas));
|
||||
}
|
||||
regions
|
||||
@@ -152,28 +160,14 @@ mod tests {
|
||||
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();
|
||||
// Only the loose layer should open the pasteboard in this case.
|
||||
doc.background_mut().unwrap().visible = false;
|
||||
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);
|
||||
assert_eq!(uncovered(&doc), vec![DocRect::new(60.0, 10.0, 80.0, 30.0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_canvas_layers_keep_the_pasteboard() {
|
||||
// An ordinary layer filling the whole canvas keeps the pasteboard, with or without a Background.
|
||||
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().unwrap().visible);
|
||||
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);
|
||||
assert!(uncovered(&doc).is_empty(), "a visible Background does not expose the full-canvas layer");
|
||||
doc.background_mut().unwrap().locks.position = false;
|
||||
assert!(doc.background().is_none());
|
||||
assert!(uncovered(&doc).is_empty(), "the same rule applies without a Background");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layers_with_an_opaque_default_keep_the_pasteboard() {
|
||||
let (mut app, _) = app();
|
||||
@@ -227,3 +221,7 @@ mod tests {
|
||||
#[cfg(test)]
|
||||
#[path = "../artboard_top_level_background_tests.rs"]
|
||||
mod top_level_background_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "artboard_outside_layers_tests.rs"]
|
||||
mod artboard_outside_layers_tests;
|
||||
|
||||
Reference in New Issue
Block a user