mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Free Transform: in artboard documents only a click outside the boards' bounding box applies the box (#17019)
Measured in the reference application with two boards, a hidden board and a loose layer: a click applies the box only outside the bounding box of the visible boards. Inside it the box stays open, also in the gap between two boards and on a loose layer's pixels there; a hidden board does not count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
32d14daf97
commit
ab42a07c5a
2 files changed
+96
-47
No files matched your search
@@ -151,21 +151,21 @@ pub fn auto_commit(app: &mut PhotocraftApp, edits: &[TransformEdit<SessionWarp>]
|
||||
true
|
||||
}
|
||||
|
||||
/// Whether a document point is outside the document, on the pasteboard around the canvas. In an
|
||||
/// artboard document everything outside the boards is pasteboard, also within the canvas size, as
|
||||
/// measured in the reference application (#17019).
|
||||
/// Whether a document point is on the pasteboard: outside the document, or in an artboard document
|
||||
/// outside the bounding box of its visible boards, also within the canvas size (#17019).
|
||||
fn on_pasteboard(app: &PhotocraftApp, p: [f64; 2]) -> bool {
|
||||
let Some(st) = app.session.active() else { return false };
|
||||
let doc = &st.doc;
|
||||
if p[0] < 0.0 || p[1] < 0.0 || p[0] >= f64::from(doc.size.width) || p[1] >= f64::from(doc.size.height) {
|
||||
return true;
|
||||
if let Some(span) = boards_span(&st.doc) {
|
||||
return p[0].is_finite() && p[1].is_finite() && !span.contains(p[0].floor() as i32, p[1].floor() as i32);
|
||||
}
|
||||
if !p[0].is_finite() || !p[1].is_finite() {
|
||||
return false;
|
||||
}
|
||||
// Inside the canvas size here, so the casts are in range.
|
||||
let (x, y) = (p[0].floor() as i32, p[1].floor() as i32);
|
||||
doc.has_artboards() && !doc.layers.iter().filter_map(|l| l.artboard()).any(|a| a.rect.contains(x, y))
|
||||
p[0] < 0.0 || p[1] < 0.0 || p[0] >= f64::from(st.doc.size.width) || p[1] >= f64::from(st.doc.size.height)
|
||||
}
|
||||
|
||||
/// The bounding box of the visible artboards, `None` without one. Measured in the reference
|
||||
/// application (#17019): with boards shown, a click applies the box only outside it; a click inside
|
||||
/// keeps the box open, on a board, in a gap between boards, and on a loose layer's pixels there.
|
||||
fn boards_span(doc: &photocraft_doc::Document) -> Option<photocraft_geom::Rect> {
|
||||
doc.layers.iter().filter(|l| l.visible).filter_map(|l| l.artboard()).map(|a| a.rect).reduce(|a, b| a.union(&b))
|
||||
}
|
||||
|
||||
/// Whether a document point lands on pixels of a layer the box doesn't move: the frontmost hit
|
||||
@@ -181,6 +181,10 @@ fn on_other_layer(app: &PhotocraftApp, t: &FreeTransform, p: [f64; 2]) -> bool {
|
||||
}
|
||||
let Some(st) = app.session.active() else { return false };
|
||||
let doc = &st.doc;
|
||||
// With boards shown only the pasteboard applies the box (#17019).
|
||||
if boards_span(doc).is_some() {
|
||||
return false;
|
||||
}
|
||||
// The layers the commit moves: `edit.transform` takes the link group along as it is then, so a
|
||||
// layer linked after the box opened counts as moved too.
|
||||
let moved = photocraft_engine::transform_cmds::box_targets(doc, &moving_layers(t));
|
||||
|
||||
@@ -189,67 +189,112 @@ fn a_click_on_a_grouped_layer_covering_another_layer_keeps_the_box_open() {
|
||||
}
|
||||
|
||||
/// An 800 × 600 document with "A" (red, x 120..220, y 150..250) and "B" (blue, x 300..400) on one
|
||||
/// artboard, Free Transform open on A and its box moved 10 px right.
|
||||
fn on_artboard() -> (Box<PhotocraftApp>, LayerId) {
|
||||
/// artboard, the loose layer "C" (magenta, 100 × 80 at `loose`) on no board, and an empty second
|
||||
/// board 100 px right of the first, hidden when `hidden`. Free Transform is open on A, its box
|
||||
/// moved 10 px right. Returns A and the two boards' rects.
|
||||
fn on_artboards(loose: [i32; 2], hidden: bool) -> (Box<PhotocraftApp>, LayerId, [photocraft_geom::Rect; 2]) {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 800, "height": 600, "background": "transparent"})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.extras.rulers = false;
|
||||
app.ui.extras.snap = false;
|
||||
let mut ids = Vec::new();
|
||||
for (name, at, color) in [("B", 300, "#0000ff"), ("A", 120, "#ff0000")] {
|
||||
for (name, at, size, color) in [("C", loose, [100, 80], "#ff00ff"), ("B", [300, 150], [100, 100], "#0000ff"), ("A", [120, 150], [100, 100], "#ff0000")] {
|
||||
let id = app.run("layer.new.layer", json!({"name": name})).unwrap()["layer"].as_u64().unwrap();
|
||||
app.run("layer.select", json!({"layer": id})).unwrap();
|
||||
app.run("select.rect", json!({"x": at, "y": 150, "width": 100, "height": 100})).unwrap();
|
||||
app.run("select.rect", json!({"x": at[0], "y": at[1], "width": size[0], "height": size[1]})).unwrap();
|
||||
app.run("edit.fill", json!({"contents": "color", "color": color})).unwrap();
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
ids.push(id);
|
||||
}
|
||||
app.run("layer.select", json!({"layer": ids[0], "mode": "add"})).unwrap();
|
||||
app.run("layer.select", json!({"layer": ids[1], "mode": "add"})).unwrap();
|
||||
app.run("layer.new.artboardFromLayers", json!({"name": "Board"})).unwrap();
|
||||
app.run("layer.select", json!({"layer": ids[1]})).unwrap();
|
||||
let first = board_rects(&app)[0];
|
||||
let second = [first.x1 + 100, first.y0, 200, first.height() as i32];
|
||||
let board = app.run("layer.new.artboard", json!({"rect": second, "name": "Second"})).unwrap()["layer"].as_u64().unwrap();
|
||||
if hidden {
|
||||
app.run("layer.hideLayers", json!({"layer": board})).unwrap();
|
||||
}
|
||||
app.run("layer.select", json!({"layer": ids[2]})).unwrap();
|
||||
let doc = &app.session.active().unwrap().doc;
|
||||
assert!(doc.layers.iter().all(|l| l.id.0 != ids[1]), "A is on the board");
|
||||
assert!(doc.layers.iter().all(|l| l.id.0 != ids[2]), "A is on a board");
|
||||
assert!(doc.layers.iter().any(|l| l.id.0 == ids[0]), "C is on no board");
|
||||
let rects = board_rects(&app);
|
||||
assert_eq!(rects.len(), 2);
|
||||
let app_ctx = app.ctx().clone();
|
||||
crate::menus::invoke(&mut app, &app_ctx, "edit.freeTransform", json!({})).unwrap();
|
||||
shift_right(&mut app);
|
||||
(app, LayerId(ids[1]))
|
||||
(app, LayerId(ids[2]), [rects[0], rects[1]])
|
||||
}
|
||||
|
||||
/// The artboard's rect, in document pixels.
|
||||
fn board_rect(app: &PhotocraftApp) -> photocraft_geom::Rect {
|
||||
app.session.active().unwrap().doc.layers.iter().find_map(|l| l.artboard()).expect("an artboard").rect
|
||||
/// The artboards' rects, in document pixels, first board first.
|
||||
fn board_rects(app: &PhotocraftApp) -> Vec<photocraft_geom::Rect> {
|
||||
let mut rects: Vec<_> = app.session.active().unwrap().doc.layers.iter().filter_map(|l| l.artboard()).map(|a| a.rect).collect();
|
||||
rects.sort_by_key(|r| r.x0);
|
||||
rects
|
||||
}
|
||||
|
||||
/// Measured in the reference application: in an artboard document the canvas outside the boards
|
||||
/// is pasteboard, also within the document's canvas size, so a click there commits one Free
|
||||
/// Transform step and the transformed layer stays selected.
|
||||
#[test]
|
||||
fn a_click_outside_the_artboards_commits() {
|
||||
let (mut app, a) = on_artboard();
|
||||
let board = board_rect(&app);
|
||||
let p = [f64::from(board.x1) + 20.0, f64::from(board.y0) + 10.0];
|
||||
fn inside_canvas(app: &PhotocraftApp, p: [f64; 2]) {
|
||||
let size = app.session.active().unwrap().doc.size;
|
||||
assert!(p[0] < f64::from(size.width) && p[1] < f64::from(size.height), "the click is inside the canvas size: {p:?} {size:?}");
|
||||
let before = steps(&app);
|
||||
click(&mut app, p);
|
||||
assert!(app.ui.transform.is_none(), "committed");
|
||||
assert_eq!(steps(&app), before + 1);
|
||||
assert!(p[0] >= 0.0 && p[1] >= 0.0 && p[0] < f64::from(size.width) && p[1] < f64::from(size.height), "{p:?} is inside the canvas size {size:?}");
|
||||
}
|
||||
|
||||
fn commits(app: &mut PhotocraftApp, a: LayerId, p: [f64; 2]) {
|
||||
inside_canvas(app, p);
|
||||
let before = steps(app);
|
||||
click(app, p);
|
||||
assert!(app.ui.transform.is_none(), "committed at {p:?}");
|
||||
assert_eq!(steps(app), before + 1);
|
||||
assert_eq!(app.session.active().unwrap().history.entries().last().map(ToString::to_string).as_deref(), Some("Free Transform"));
|
||||
assert_eq!(app.session.active().unwrap().selected_layers(), vec![a], "the selection doesn't change");
|
||||
}
|
||||
|
||||
/// Measured in the reference application: inside an artboard a click on another layer's pixels or
|
||||
/// on an empty spot of the board keeps the box open.
|
||||
fn stays_open(app: &mut PhotocraftApp, p: [f64; 2]) {
|
||||
inside_canvas(app, p);
|
||||
let (before, q) = (steps(app), quad(app));
|
||||
click(app, p);
|
||||
assert!(app.ui.transform.is_some(), "the box stays open at {p:?}");
|
||||
assert_eq!(quad(app), q);
|
||||
assert_eq!(steps(app), before);
|
||||
}
|
||||
|
||||
/// Measured in the reference application (#17019): in an artboard document the pasteboard is what
|
||||
/// lies outside the bounding box of the visible boards, also within the canvas size, so a click
|
||||
/// there commits one Free Transform step and the transformed layer stays selected. That holds on a
|
||||
/// loose layer's pixels out there too.
|
||||
#[test]
|
||||
fn a_click_on_the_artboard_keeps_the_box_open() {
|
||||
for p in [[350.0, 200.0], [150.0, 240.0]] {
|
||||
let (mut app, _) = on_artboard();
|
||||
assert!(board_rect(&app).contains(p[0] as i32, p[1] as i32), "{p:?} is on the board");
|
||||
let (before, q) = (steps(&app), quad(&app));
|
||||
click(&mut app, p);
|
||||
assert!(app.ui.transform.is_some(), "the box stays open at {p:?}");
|
||||
assert_eq!(quad(&app), q);
|
||||
assert_eq!(steps(&app), before);
|
||||
fn a_click_outside_the_artboards_commits() {
|
||||
let (mut app, a, [_, second]) = on_artboards([100, 480], false);
|
||||
commits(&mut app, a, [f64::from(second.x1) + 20.0, f64::from(second.y0) + 10.0]);
|
||||
let (mut app, a, _) = on_artboards([100, 480], false);
|
||||
commits(&mut app, a, [150.0, 520.0]);
|
||||
}
|
||||
|
||||
/// Measured in the reference application (#17019): a hidden board doesn't count, a click on its
|
||||
/// area commits.
|
||||
#[test]
|
||||
fn a_click_on_a_hidden_artboard_commits() {
|
||||
let (mut app, a, [_, second]) = on_artboards([100, 480], true);
|
||||
commits(&mut app, a, [f64::from(second.x0) + 50.0, f64::from(second.y0) + 50.0]);
|
||||
}
|
||||
|
||||
/// Measured in the reference application (#17019): inside the boards' bounding box the box stays
|
||||
/// open: on another layer's pixels on the board, on an empty spot of it, in the gap between two
|
||||
/// boards, and on a loose layer's pixels in that gap.
|
||||
#[test]
|
||||
fn a_click_within_the_artboards_keeps_the_box_open() {
|
||||
let (mut app, _, [first, _]) = on_artboards([100, 480], false);
|
||||
let gap = [f64::from(first.x1) + 50.0, f64::from(first.y0) + 20.0];
|
||||
// B's pixels, then the board between A's box (x 130..230) and B (x 300..400), then the gap.
|
||||
for p in [[350.0, 200.0], [260.0, 200.0], gap] {
|
||||
assert!(app.ui.transform.as_ref().is_some(), "the box is open");
|
||||
stays_open(&mut app, p);
|
||||
}
|
||||
// C fills the 100 px gap between the boards.
|
||||
let (mut app, _, _) = on_artboards([first.x1, first.y0], false);
|
||||
let p = [f64::from(first.x1) + 50.0, f64::from(first.y0) + 40.0];
|
||||
let doc = &app.session.active().unwrap().doc;
|
||||
let hit = photocraft_engine::pick_cmds::layers_at(doc, p[0] as i32, p[1] as i32);
|
||||
assert_eq!(hit.first().and_then(|id| doc.layer(*id)).map(|l| l.name.as_str()), Some("C"), "the click lands on C");
|
||||
stays_open(&mut app, p);
|
||||
}
|
||||
Reference in new issue
Block a user