Merge branch 'bb-move-tool-mx4onk76' of github.com:storytold/photocraft into bb-move-tool-mx4onk76--18157-board-position-lock

This commit is contained in:
David Mann committed 2026-12-01 19:08:49 +01:00
commit 214250910f
2 files changed
+217 -2

No files matched your search

+9 -2
View File
@@ -198,12 +198,15 @@ pub fn nudge(app: &mut PhotocraftApp, dx: f64, dy: f64, duplicate_first: bool) {
}
let from = if duplicate_first { duplicate(app) } else { None };
let revision = app.session.active().map(|st| st.revision);
// `nest`: after every press a layer moved onto or off an artboard changes board, as after a
// drag (#18153).
let params = crate::move_ui::pattern_wrap(app, json!({"dx": dx, "dy": dy, "nest": app.ui.artboard_tool.auto_nest}));
let moved = if duplicate_first {
// A copy is a state of its own, folded with its move below.
app.nudge_bundle.reset();
app.run("layer.translate", crate::move_ui::pattern_wrap(app, json!({"dx": dx, "dy": dy})))
app.run("layer.translate", params)
} else {
crate::nudge_bundle::run(app, "layers", "layer.translate", crate::move_ui::pattern_wrap(app, json!({"dx": dx, "dy": dy})))
crate::nudge_bundle::run(app, "layers", "layer.translate", params)
};
match moved {
// The reference names an arrow-key move "Nudge"; a bundled run is one step with that name.
@@ -257,3 +260,7 @@ mod tests;
#[cfg(test)]
#[path = "move_alt_drag_linked_tests.rs"]
mod move_alt_drag_linked_tests;
#[cfg(test)]
#[path = "move_nudge_artboard_nest_tests.rs"]
mod move_nudge_artboard_nest_tests;
@@ -0,0 +1,208 @@
//! #18153: an arrow-key nudge with the Move tool re-parents the nudged layers after every press by
//! the rule of a Move drag: a layer nudged off its artboard leaves it for the top level, a loose
//! layer nudged onto a board joins it once more than a tenth of it is there. A nudged board takes
//! its contents along and no layer it does not cover. The presses keep their "Nudge" state, and
//! Undo restores place and parent.
use photocraft_doc::{Layer, LayerId};
use photocraft_geom::Rect;
use serde_json::json;
use super::arrow_key;
use crate::PhotocraftApp;
use crate::state::Tool;
struct Doc {
app: Box<PhotocraftApp>,
b1: LayerId,
a: LayerId,
b: LayerId,
g: LayerId,
d: LayerId,
}
fn add_rect(app: &mut PhotocraftApp, r: Rect, name: &str) -> LayerId {
app.session
.edit("rect", |doc, active| {
let mut l = Layer::raster(name.to_string(), doc.pixel_format());
l.surface_mut().unwrap().fill_rect(r, &[1.0, 0.0, 0.0, 1.0]);
let id = doc.insert_above(None, l);
*active = Some(id);
Ok(id)
})
.unwrap()
}
fn board(app: &mut PhotocraftApp, rect: [i32; 4], name: &str) -> LayerId {
LayerId(app.session.execute("layer.new.artboard", json!({"rect": rect, "name": name})).unwrap()["layer"].as_u64().unwrap())
}
fn into(app: &mut PhotocraftApp, id: LayerId, parent: LayerId) {
app.session.execute("layer.moveTo", json!({"layer": id.0, "target": parent.0, "position": "into"})).unwrap();
}
fn select(app: &mut PhotocraftApp, id: LayerId) {
app.session.execute("layer.select", json!({"layer": id.0, "mode": "replace"})).unwrap();
}
/// The issue's test document at 100 %: Board1 (50,50-450,450) holds A (100,100-200,200), B and
/// group G with G1; Board2 (550,50-950,450) holds C; D (100,550-200,630) is a loose top-level
/// layer. The Move tool, auto-select off.
fn setup() -> Doc {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.session.execute("file.new", json!({"width": 1000, "height": 700})).unwrap();
app.sync_views();
app.ui.views[0].zoom = 1.0;
let b1 = board(&mut app, [50, 50, 400, 400], "Board1");
let b2 = board(&mut app, [550, 50, 400, 400], "Board2");
let a = add_rect(&mut app, Rect::new(100, 100, 200, 200), "A");
into(&mut app, a, b1);
let b = add_rect(&mut app, Rect::new(250, 250, 350, 350), "B");
into(&mut app, b, b1);
let g = app
.session
.edit("group", |doc, _| {
let mut g1 = Layer::raster("G1".to_string(), doc.pixel_format());
g1.surface_mut().unwrap().fill_rect(Rect::new(300, 100, 400, 150), &[0.5, 0.5, 0.5, 1.0]);
Ok(doc.insert_above(None, Layer::group("G", vec![g1])))
})
.unwrap();
into(&mut app, g, b1);
let c = add_rect(&mut app, Rect::new(600, 100, 700, 200), "C");
into(&mut app, c, b2);
let d = add_rect(&mut app, Rect::new(100, 550, 200, 630), "D");
app.ui.tool = Tool::Move;
app.ui.tool_options.move_auto_select = false;
Doc { app, b1, a, b, g, d }
}
/// Presses the arrow `(x, y)` `n` times, 0.1 s apart from `*t` on (one run of nudges), ⇧ with `shift`.
fn press(app: &mut PhotocraftApp, t: &mut f64, (x, y): (f32, f32), shift: bool, n: usize) {
let mods = craft::keymap::Mods { shift, ..Default::default() };
for _ in 0..n {
*t += 0.1;
assert!(arrow_key(app, egui::vec2(x, y), mods, *t, false));
}
}
const RIGHT: (f32, f32) = (1.0, 0.0);
const LEFT: (f32, f32) = (-1.0, 0.0);
const UP: (f32, f32) = (0.0, -1.0);
const DOWN: (f32, f32) = (0.0, 1.0);
fn left(app: &PhotocraftApp, id: LayerId) -> i32 {
app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().content_bounds().x0
}
fn top(app: &PhotocraftApp, id: LayerId) -> i32 {
app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().content_bounds().y0
}
fn board_of(app: &PhotocraftApp, id: LayerId) -> Option<LayerId> {
app.session.active().unwrap().doc.artboard_of(id)
}
fn board_rect(app: &PhotocraftApp, id: LayerId) -> Rect {
app.session.active().unwrap().doc.layer(id).unwrap().artboard().unwrap().rect
}
fn history(app: &PhotocraftApp) -> usize {
app.session.active().unwrap().history.past_len()
}
#[test]
fn a_nudged_layer_leaves_its_board_once_off_it_and_comes_back_with_a_tenth_on_it() {
let Doc { mut app, b1, a, .. } = setup();
select(&mut app, a);
let h0 = history(&app);
let mut t = 1.0;
// 34 × ⇧→ to x 440, then → one pixel at a time.
press(&mut app, &mut t, RIGHT, true, 34);
press(&mut app, &mut t, RIGHT, false, 9);
assert_eq!(left(&app, a), 449);
assert_eq!(board_of(&app, a), Some(b1), "a column still on Board1: A stays in it");
press(&mut app, &mut t, RIGHT, false, 1);
assert_eq!(left(&app, a), 450);
assert_eq!(board_of(&app, a), None, "off Board1: A is a top-level layer");
press(&mut app, &mut t, LEFT, false, 10);
assert_eq!(left(&app, a), 440);
assert_eq!(board_of(&app, a), None, "a tenth of A on Board1 is not more than a tenth");
press(&mut app, &mut t, LEFT, false, 1);
assert_eq!(board_of(&app, a), Some(b1), "at x 439 A is back in Board1");
press(&mut app, &mut t, RIGHT, false, 11);
assert_eq!(board_of(&app, a), None);
// The run is one Nudge state, and Undo takes back place and parent.
let st = app.session.active().unwrap();
assert!(history(&app) > h0);
assert_eq!(st.history.undo_label(), Some("Nudge"));
while history(&app) > h0 {
assert!(app.session.undo());
}
assert_eq!(left(&app, a), 100);
assert_eq!(board_of(&app, a), Some(b1), "Undo puts A back into Board1");
}
#[test]
fn a_loose_layer_nudged_onto_a_board_joins_it_past_a_tenth() {
let Doc { mut app, b1, d, .. } = setup();
select(&mut app, d);
let mut t = 1.0;
// D is 80 px high; Board1 ends at y 450. 8 rows on it are a tenth, 9 more than that.
press(&mut app, &mut t, UP, true, 10);
press(&mut app, &mut t, UP, false, 8);
assert_eq!(top(&app, d), 442);
assert_eq!(board_of(&app, d), None);
press(&mut app, &mut t, UP, false, 1);
assert_eq!(board_of(&app, d), Some(b1), "nine rows on Board1: D joins it");
let children = app.session.active().unwrap().doc.layer(b1).unwrap().children().unwrap().iter().map(|l| l.id).collect::<Vec<_>>();
assert_eq!(children.last(), Some(&d), "on top of the board's stack");
}
#[test]
fn a_nudged_board_takes_its_contents_along_and_no_other_layer() {
let Doc { mut app, b1, a, b, g, d } = setup();
select(&mut app, b1);
let rect = board_rect(&app, b1);
let mut t = 1.0;
press(&mut app, &mut t, RIGHT, false, 3);
press(&mut app, &mut t, DOWN, true, 1);
let moved = board_rect(&app, b1);
assert_eq!((moved.x0 - rect.x0, moved.y0 - rect.y0), (3, 10));
assert_eq!((left(&app, a), top(&app, a)), (103, 110), "the board's layers move with it");
for id in [a, b, g] {
assert_eq!(board_of(&app, id), Some(b1), "its contents stay in it");
}
assert_eq!(board_of(&app, d), None, "D is not under the board and stays loose");
assert_eq!(app.session.active().unwrap().history.undo_label(), Some("Nudge"));
}
/// The Alt-nudge copy is nested by the same rule: a copy nudged off the board leaves it, while
/// the layer it copies stays where it is.
#[test]
fn an_alt_nudged_copy_off_the_board_leaves_it() {
let Doc { mut app, b1, a, .. } = setup();
select(&mut app, a);
let mut t = 1.0;
press(&mut app, &mut t, RIGHT, true, 34);
press(&mut app, &mut t, RIGHT, false, 9);
assert_eq!((left(&app, a), board_of(&app, a)), (449, Some(b1)));
let alt = craft::keymap::Mods { alt: true, ..Default::default() };
assert!(arrow_key(&mut app, egui::vec2(1.0, 0.0), alt, t + 0.1, false));
let copy = app.session.active().unwrap().active_layer.unwrap();
assert_ne!(copy, a);
assert_eq!((left(&app, copy), board_of(&app, copy)), (450, None), "the copy is off Board1");
assert_eq!((left(&app, a), board_of(&app, a)), (449, Some(b1)), "the original stays");
}
/// With Auto-Nest off (the Artboard tool's gear) a nudge keeps the layer's parent, as before.
#[test]
fn auto_nest_off_keeps_the_parent() {
let Doc { mut app, b1, a, .. } = setup();
app.ui.artboard_tool.auto_nest = false;
select(&mut app, a);
let mut t = 1.0;
press(&mut app, &mut t, RIGHT, true, 36);
assert_eq!(left(&app, a), 460);
assert_eq!(board_of(&app, a), Some(b1));
}