Merge pull request #3597 from FredApps/feat/3592-multi-layer-transform

Free Transform several selected layers together (#3592)
This commit is contained in:
FredApps authored and GitHub committed 2026-11-14 12:16:38 +01:00
commit 59c82a73ce
6 files changed
+370 -3

No files matched your search

+8 -3
View File
@@ -333,9 +333,14 @@ fn transform_preset(s: &mut Session, p: &Value, kind: &str) -> Result<Value> {
let (b, mut params) = match crate::transform_cmds::lone_target(&d.doc, d.active_layer, p)? {
Some(surf) => (crate::transform_cmds::target_frame(&d.doc, surf), json!({"target": p.get("target")})),
None => {
let id = d.active_layer.ok_or(EngineError::Other("no active layer".into()))?;
let l = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?;
(crate::transform_cmds::transform_frame(&d.doc, l), json!({"layer": id.0}))
let explicit = crate::commands::explicit_layer(s, p)?;
let ids = explicit.map_or_else(|| crate::transform_cmds::selection_targets(d), |id| vec![id]);
let bounds = crate::transform_cmds::transform_frame_of(&d.doc, &ids);
let params = match explicit {
Some(id) => json!({"layer": id.0}),
None => json!({"layers": ids.iter().map(|id| id.0).collect::<Vec<_>>()}),
};
(bounds, params)
}
};
if b.is_empty() {
+3
View File
@@ -1049,3 +1049,6 @@ mod tests;
#[cfg(test)]
mod multi_tests;
#[cfg(test)]
mod reconciliation_tests;
@@ -0,0 +1,146 @@
use super::*;
fn session() -> Session {
session_depth(8)
}
fn session_depth(depth: u32) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 100, "height": 100, "depth": depth})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
s.edit("paint", |doc, active| {
let l = doc.layer_mut(active.unwrap()).unwrap();
l.surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 20), &[1.0, 0.0, 0.0, 1.0]);
Ok(())
})
.unwrap();
s
}
/// Two layers: the session's red rect at (10,10)-(30,20) and a blue one at (60,50)-(80,70),
/// both selected; returns (bottom, top).
fn two_layers() -> (Session, LayerId, LayerId) {
let mut s = session();
let a = s.active().unwrap().active_layer.unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
s.edit("paint", |doc, active| {
doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(60, 50, 80, 70), &[0.0, 0.0, 1.0, 1.0]);
Ok(())
})
.unwrap();
let b = s.active().unwrap().active_layer.unwrap();
(s, a, b)
}
fn bounds_of(s: &Session, id: LayerId) -> Rect {
s.active().unwrap().doc.layer(id).unwrap().surface().unwrap().content_bounds()
}
#[test]
fn several_layers_scale_around_one_shared_frame_in_one_step() {
let (mut s, a, b) = two_layers();
assert_eq!(transform_bounds_of(&s.active().unwrap().doc, &[a, b]), Rect::new(10, 10, 80, 70));
let steps = s.active().unwrap().history.entries().len();
// Halve the shared frame about its top-left corner.
let r = s.execute("edit.transform", json!({"layers": [a.0, b.0], "quad": [[10, 10], [45, 10], [45, 40], [10, 40]]})).unwrap();
assert_eq!(r["rect"], json!([10.0, 10.0, 80.0, 70.0]));
assert_eq!(bounds_of(&s, a), Rect::new(10, 10, 20, 15));
assert_eq!(bounds_of(&s, b), Rect::new(35, 30, 45, 40));
assert_eq!(s.active().unwrap().history.entries().len(), steps + 1, "one history step");
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(10, 10, 30, 20));
assert_eq!(bounds_of(&s, b), Rect::new(60, 50, 80, 70));
}
#[test]
fn several_layers_move_and_rotate_consistently() {
let (mut s, a, b) = two_layers();
s.execute("edit.transform", json!({"layers": [a.0, b.0], "matrix": [1, 0, 0, 1, 5, 7]})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(15, 17, 35, 27));
assert_eq!(bounds_of(&s, b), Rect::new(65, 57, 85, 77));
s.execute("edit.undo", json!({})).unwrap();
// 180° about the shared centre (45, 40): each rect lands where the other's mirror is.
s.execute("edit.transform", json!({"layers": [a.0, b.0], "quad": [[80, 70], [10, 70], [10, 10], [80, 10]], "interpolation": "nearest"})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(60, 60, 80, 70));
assert_eq!(bounds_of(&s, b), Rect::new(10, 10, 30, 30));
}
#[test]
fn several_layers_accept_relative_transform_params_after_main_merge() {
let (mut s, a, b) = two_layers();
let steps = s.active().unwrap().history.entries().len();
s.execute("edit.transform", json!({"layers": [a.0, b.0], "dx": 5, "dy": 7})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(15, 17, 35, 27));
assert_eq!(bounds_of(&s, b), Rect::new(65, 57, 85, 77));
assert_eq!(s.active().unwrap().history.entries().len(), steps + 1);
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(10, 10, 30, 20));
assert_eq!(bounds_of(&s, b), Rect::new(60, 50, 80, 70));
}
#[test]
fn several_layers_refuse_when_one_is_locked_and_skip_the_empty() {
let (mut s, a, b) = two_layers();
s.execute("layer.new.layer", json!({})).unwrap();
let empty = s.active().unwrap().active_layer.unwrap();
s.edit("lock", |doc, _| {
doc.layer_mut(b).unwrap().locks.position = true;
Ok(())
})
.unwrap();
let before = s.active().unwrap().history.entries().len();
assert!(s.execute("edit.transform", json!({"layers": [a.0, b.0], "matrix": [1, 0, 0, 1, 5, 0]})).is_err());
assert_eq!(bounds_of(&s, a), Rect::new(10, 10, 30, 20), "nothing moved");
assert_eq!(s.active().unwrap().history.entries().len(), before);
// One layer left: the single-layer path, with its own frame.
s.execute("edit.transform", json!({"layers": [a.0, empty.0], "matrix": [1, 0, 0, 1, 5, 0]})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(15, 10, 35, 20));
}
#[test]
fn flip_and_rotate_turn_every_selected_layer_around_the_shared_box() {
let (mut s, a, b) = two_layers();
s.execute("layer.select", json!({"layer": a.0, "mode": "add"})).unwrap();
let steps = s.active().unwrap().history.entries().len();
s.execute("edit.transform.flipHorizontal", json!({})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(60, 10, 80, 20));
assert_eq!(bounds_of(&s, b), Rect::new(10, 50, 30, 70));
assert_eq!(s.active().unwrap().history.entries().len(), steps + 1, "one step");
s.execute("edit.undo", json!({})).unwrap();
s.execute("edit.transform.rotate180", json!({})).unwrap();
assert_eq!(bounds_of(&s, a), Rect::new(60, 60, 80, 70));
assert_eq!(bounds_of(&s, b), Rect::new(10, 10, 30, 30));
s.execute("edit.undo", json!({})).unwrap();
// A named layer still turns alone, around its own box.
s.execute("edit.transform.flipVertical", json!({"layer": a.0})).unwrap();
assert_eq!((bounds_of(&s, a), bounds_of(&s, b)), (Rect::new(10, 10, 30, 20), Rect::new(60, 50, 80, 70)));
}
#[test]
fn several_layers_convert_the_background_for_transform() {
let mut s = session();
let bg = s.active().unwrap().doc.layers[0].id;
let layer = s.active().unwrap().active_layer.unwrap();
assert!(s.active().unwrap().doc.is_background(bg));
let before = s.active().unwrap().doc.clone();
let steps = s.active().unwrap().history.past_len();
s.execute("edit.transform", json!({"layers": [bg.0, layer.0], "dx": 5})).unwrap();
let st = s.active().unwrap();
assert!(!st.doc.is_background(bg));
assert!(st.doc.layer(bg).unwrap().surface().unwrap().format().alpha);
assert_eq!(st.history.past_len(), steps + 1);
s.execute("edit.undo", json!({})).unwrap();
assert!(s.active().unwrap().doc == before);
}
#[test]
fn several_layers_preflight_the_selection_pixel_budget() {
let (mut s, a, b) = two_layers();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 100, "height": 100})).unwrap();
let before = s.active().unwrap().doc.clone();
let steps = s.active().unwrap().history.past_len();
let err = s.execute("edit.transform", json!({"layers": [a.0, b.0], "matrix": [50, 0, 0, 50, 0, 0]})).unwrap_err().to_string();
assert!(err.contains("selection"), "{err}");
assert!(s.active().unwrap().doc == before);
assert_eq!(s.active().unwrap().history.past_len(), steps);
}
+11
View File
@@ -88,6 +88,17 @@ impl PreviewTextures {
}
}
/// Level-zero texel for preview regressions.
#[cfg(test)]
pub fn texel(&self, x: usize, y: usize) -> Option<egui::Color32> {
let images = self.images.lock().ok()?;
let image = images.first()?;
if x >= image.size[0] || y >= image.size[1] {
return None;
}
image.pixels.get(y.checked_mul(image.size[0])?.checked_add(x)?).copied()
}
/// Build mip levels off the UI thread (`true`) or in the frame that first needs them.
#[cfg(test)]
pub(crate) fn set_background_builds(&mut self, on: bool) {
@@ -4119,6 +4119,41 @@ fn the_warp_bar_greys_its_fields_for_a_cylinder() {
assert!(h.query_by_label("Top depth").is_none() && h.query_by_label("Wrap").is_none(), "no cylinder fields");
}
#[test]
fn a_multi_layer_box_follows_opacity_and_visibility_edits() {
let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
let red = app.session.active().unwrap().active_layer.unwrap();
app.session.execute("layer.new.layer", json!({})).unwrap();
app.session
.edit("paint", |doc, a| {
doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(40, 40, 56, 56), &[0.0, 0.0, 1.0, 1.0]);
Ok(())
})
.unwrap();
let blue = app.session.active().unwrap().active_layer.unwrap();
app.session.execute("layer.select", json!({"layer": red.0, "mode": "add"})).unwrap();
app.sync_views();
begin(&mut app, &egui::Context::default()).unwrap();
let texel = |app: &PhotocraftApp, x: usize, y: usize| app.transform_preview.as_ref().unwrap().texture.texel(x, y).unwrap();
// The box is (8, 8)-(56, 56): red at its top-left texel, blue at (32, 32).
assert_eq!(texel(&app, 0, 0).a(), 255);
assert_eq!(texel(&app, 40, 40).a(), 255);
app.session
.edit("opacity", |doc, _| {
doc.layer_mut(red).unwrap().opacity = 0.5;
doc.layer_mut(blue).unwrap().visible = false;
Ok(())
})
.unwrap();
follow_edits(&mut app);
let a = texel(&app, 0, 0).a();
assert!((120..=135).contains(&a), "half-opaque red: {a}");
assert_eq!(texel(&app, 40, 40).a(), 0, "hidden blue drops out of the preview");
let pv = app.transform_preview.as_ref().unwrap();
assert!(!pv.doc.layer(red).unwrap().visible && !pv.doc.layer(blue).unwrap().visible, "both still hidden under the box");
assert_eq!(app.ui.transform.as_ref().unwrap().layers, vec![red.0, blue.0]);
}
#[test]
fn several_layers_reuse_the_transform_interpolation_preference() {
let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
+167
View File
@@ -386,3 +386,170 @@ fn free_transform_opens_on_the_selection_and_moves_it_with_the_pixels() {
// What stayed (160..200 × 100..160) and what moved (to 150..210 × 130..190).
assert_eq!(st.doc.layer(layer).unwrap().surface().unwrap().content_bounds(), photocraft_geom::Rect::new(150, 100, 210, 190));
}
/// The harness's red layer plus a blue 50 × 50 one at (250, 200), both selected (#3592).
fn two_selected(h: &mut Harness<'static, PhotocraftApp>) -> (u64, u64) {
let app = h.state_mut();
let red = app.session.active().unwrap().active_layer.unwrap().0;
app.run("layer.new.layer", json!({})).unwrap();
app.run("select.rect", json!({"x": 250, "y": 200, "width": 50, "height": 50})).unwrap();
app.run("edit.fill", json!({"color": "#0000ff"})).unwrap();
app.run("select.deselect", json!({})).unwrap();
let blue = app.session.active().unwrap().active_layer.unwrap().0;
app.run("layer.select", json!({"layer": red, "mode": "add"})).unwrap();
app.sync_views();
h.run_steps(2);
(red, blue)
}
fn bounds(h: &Harness<'static, PhotocraftApp>, id: u64) -> photocraft_geom::Rect {
let st = h.state().session.active().unwrap();
st.doc.layer(photocraft_doc::LayerId(id)).unwrap().surface().unwrap().content_bounds()
}
#[test]
fn free_transform_moves_every_selected_layer_in_one_step() {
let mut h = harness();
let (red, blue) = two_selected(&mut h);
let steps = history(&h);
begin(&mut h);
let t = h.state().ui.transform.clone().expect("transforming");
assert_eq!(t.layers.len(), 2, "both layers move: {:?}", t.layers);
assert!(close(t.quad, [[100.0, 100.0], [300.0, 100.0], [300.0, 250.0], [100.0, 250.0]]), "one box around both: {:?}", t.quad);
// Scale the shared box by ½ about its top-left corner.
h.state_mut().ui.transform.as_mut().unwrap().quad = [[100.0, 100.0], [200.0, 100.0], [200.0, 175.0], [100.0, 175.0]];
key(&mut h, Modifiers::NONE, Key::Enter);
assert!(h.state().ui.transform.is_none());
assert_eq!(history(&h), steps + 1, "one history step");
assert_eq!(bounds(&h, red), photocraft_geom::Rect::new(100, 100, 150, 130));
assert_eq!(bounds(&h, blue), photocraft_geom::Rect::new(175, 150, 200, 175));
// One Undo restores both.
key(&mut h, Modifiers::COMMAND, Key::Z);
assert_eq!(history(&h), steps);
assert_eq!(bounds(&h, red), photocraft_geom::Rect::new(100, 100, 200, 160));
assert_eq!(bounds(&h, blue), photocraft_geom::Rect::new(250, 200, 300, 250));
}
#[test]
fn cancelling_a_multi_layer_transform_leaves_every_layer() {
let mut h = harness();
let (red, blue) = two_selected(&mut h);
let steps = history(&h);
begin(&mut h);
key(&mut h, Modifiers::NONE, Key::ArrowRight);
key(&mut h, Modifiers::NONE, Key::ArrowDown);
assert!(close(quad(&h), [[101.0, 101.0], [301.0, 101.0], [301.0, 251.0], [101.0, 251.0]]), "{:?}", quad(&h));
key(&mut h, Modifiers::NONE, Key::Escape);
assert!(h.state().ui.transform.is_none());
assert_eq!(history(&h), steps);
assert_eq!(bounds(&h, red), photocraft_geom::Rect::new(100, 100, 200, 160));
assert_eq!(bounds(&h, blue), photocraft_geom::Rect::new(250, 200, 300, 250));
}
#[test]
fn a_single_selected_layer_keeps_its_own_box() {
let mut h = harness();
begin(&mut h);
let t = h.state().ui.transform.clone().expect("transforming");
assert!(t.layers.is_empty());
assert!(close(t.quad, [[100.0, 100.0], [200.0, 100.0], [200.0, 160.0], [100.0, 160.0]]));
}
#[test]
fn a_locked_layer_in_the_selection_refuses_the_box() {
let mut h = harness();
let (_, blue) = two_selected(&mut h);
h.state_mut()
.session
.edit("lock", |doc, _| {
doc.layer_mut(photocraft_doc::LayerId(blue)).unwrap().locks.position = true;
Ok(())
})
.unwrap();
let ctx = h.ctx.clone();
assert!(crate::menus::invoke(h.state_mut(), &ctx, "edit.freeTransform", json!({})).is_err());
assert!(h.state().ui.transform.is_none());
}
fn shift_x(q: [[f64; 2]; 4], dx: f64) -> [[f64; 2]; 4] {
q.map(|p| [p[0] + dx, p[1]])
}
#[test]
fn several_layers_grey_warp_but_not_rotate_and_flip() {
// As in the reference application: Edit › Transform › Warp and its split items are greyed
// with two pixel layers selected; Rotate and Flip turn both around the shared box.
let mut h = harness();
two_selected(&mut h);
for id in ["edit.transform.warp", "edit.transform.splitWarpCrosswise", "edit.transform.removeWarpSplit"] {
assert!(!crate::menus::is_enabled(h.state(), id), "{id}");
}
for id in ["edit.transform.flipHorizontal", "edit.transform.flipVertical", "edit.transform.rotate180", "edit.transform.rotate90Cw"] {
assert!(crate::menus::is_enabled(h.state(), id), "{id}");
}
}
#[test]
fn free_transform_a_copy_duplicates_every_selected_layer_and_moves_the_copies() {
let mut h = harness();
let (red, blue) = two_selected(&mut h);
let steps = history(&h);
let count = |h: &Harness<'static, PhotocraftApp>| h.state().session.active().unwrap().doc.walk().len();
let n = count(&h);
let ctx = h.ctx.clone();
crate::menus::invoke(h.state_mut(), &ctx, "edit.freeTransformCopy", json!({})).unwrap();
h.run_steps(2);
let t = h.state().ui.transform.clone().expect("transforming");
assert_eq!(t.layers.len(), 2, "both copies in the box");
assert!(!t.layers.contains(&red) && !t.layers.contains(&blue), "the copies move, not the originals");
assert_eq!(count(&h), n + 2);
h.state_mut().ui.transform.as_mut().unwrap().quad = shift_x(t.quad, 20.0);
key(&mut h, Modifiers::NONE, Key::Enter);
assert_eq!(history(&h), steps + 1, "the copies and their transform are one step");
assert_eq!(bounds(&h, red), photocraft_geom::Rect::new(100, 100, 200, 160));
assert_eq!(bounds(&h, blue), photocraft_geom::Rect::new(250, 200, 300, 250));
assert_eq!(bounds(&h, t.layers[0]), photocraft_geom::Rect::new(120, 100, 220, 160));
assert_eq!(bounds(&h, t.layers[1]), photocraft_geom::Rect::new(270, 200, 320, 250));
key(&mut h, Modifiers::COMMAND, Key::Z);
assert_eq!((count(&h), history(&h)), (n, steps), "one Undo takes both copies back");
// Esc takes the copies back too, leaving nothing to redo.
crate::menus::invoke(h.state_mut(), &ctx, "edit.freeTransformCopy", json!({})).unwrap();
h.run_steps(2);
key(&mut h, Modifiers::NONE, Key::Escape);
assert_eq!((count(&h), history(&h)), (n, steps));
}
#[test]
fn flip_and_rotate_menu_items_turn_every_selected_layer_around_the_shared_box() {
let mut h = harness();
let (red, blue) = two_selected(&mut h);
let steps = history(&h);
let ctx = h.ctx.clone();
crate::menus::invoke(h.state_mut(), &ctx, "edit.transform.flipHorizontal", json!({})).unwrap();
h.run_steps(2);
assert_eq!(history(&h), steps + 1);
assert_eq!(bounds(&h, red), photocraft_geom::Rect::new(200, 100, 300, 160));
assert_eq!(bounds(&h, blue), photocraft_geom::Rect::new(100, 200, 150, 250));
key(&mut h, Modifiers::COMMAND, Key::Z);
crate::menus::invoke(h.state_mut(), &ctx, "edit.transform.rotate180", json!({})).unwrap();
h.run_steps(2);
assert_eq!(bounds(&h, red), photocraft_geom::Rect::new(200, 190, 300, 250));
assert_eq!(bounds(&h, blue), photocraft_geom::Rect::new(100, 100, 150, 150));
}
#[test]
fn several_layers_keep_the_selection_frame_and_move_the_selection_once() {
let mut h = harness();
let (red, blue) = two_selected(&mut h);
h.state_mut().run("select.rect", json!({"x": 60, "y": 80, "width": 280, "height": 220})).unwrap();
begin(&mut h);
assert_eq!(quad(&h), [[60.0, 80.0], [340.0, 80.0], [340.0, 300.0], [60.0, 300.0]]);
let q = quad(&h);
h.state_mut().ui.transform.as_mut().unwrap().quad = shift_x(q, 10.0);
key(&mut h, Modifiers::NONE, Key::Enter);
assert!(h.state().ui.transform.is_none());
assert_eq!(bounds(&h, red), photocraft_geom::Rect::new(110, 100, 210, 160));
assert_eq!(bounds(&h, blue), photocraft_geom::Rect::new(260, 200, 310, 250));
assert_eq!(h.state().session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(), photocraft_geom::Rect::new(70, 80, 350, 300));
}