PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Move tool: moving pixels again leaves no faint outline behind (#7538)
Hector Villarrealcb85f77f5a15
diff --git a/crates/engine/src/float_cmds.rs b/crates/engine/src/float_cmds.rsindex 8de2c3a7f..323c0bee9 100644--- a/crates/engine/src/float_cmds.rs+++ b/crates/engine/src/float_cmds.rs@@ -33,9 +33,9 @@ pub struct CutParts {pub layer: LayerId,rest: Surface,piece: Surface,- /// The selection the moved pixels take, unmoved: the selection limited to the layer's- /// transparency (Photoshop shrinks the marching ants to the piece as the drag starts, #7538).- /// `None` keeps the selection: a mask has no transparency, and an empty piece no outline.+ /// The selection the moved pixels take, unmoved: the selection limited to where the layer has+ /// pixels (Photoshop shrinks the marching ants to the piece as the drag starts, #7538). `None`+ /// keeps the selection: a mask has no transparency, and an empty piece no outline.outline: Option<Surface>,/// The pieces are the layer's mask (`"target":"mask"`), not its pixels.mask: bool,@@ -74,8 +74,15 @@ impl CutParts {let mut rp = full.read_region(b);let mut pp = rp.clone();let w = b.width() as usize;+ let mut cover = Vec::with_capacity(if mask { 0 } else { w * b.height() as usize });for (i, (p, r)) in pp.chunks_exact_mut(n).zip(rp.chunks_exact_mut(n)).enumerate() {let k = sel.sample_channel(b.x0 + (i % w) as i32, b.y0 + (i / w) as i32, 0);+ if !mask {+ // Where the layer has pixels, the selection keeps its strength: shrinking it to+ // their alpha would leave half-transparent edges partly behind on the next+ // move, a faint outline (#7538).+ cover.push(if p[a] > 0.0 { k.max(0.0) } else { 0.0 });+ }if k <= 0.0 {p.fill(0.0);} else {@@ -93,8 +100,7 @@ impl CutParts {}rest.prune();if !mask {- // The piece's alpha is the selection times the layer's transparency.- outline.write_region(b, &pp.chunks_exact(n).map(|p| p[a]).collect::<Vec<_>>());+ outline.write_region(b, &cover);outline.prune();}piece.write_region(b, &pp);@@ -270,7 +276,7 @@ pub fn specs() -> Vec<CommandSpec> {label: "Drop Floating Selection",menu: &[],shortcut: None,- params: r##"{} → {dropped, offset} (drops the floating piece into its layer and moves the selection with it, shrunk to the moved pixels: the selection times the layer's transparency, as in Photoshop; a mask or an empty piece keeps the selection's shape; one history step)"##,+ params: r##"{} → {dropped, offset} (drops the floating piece into its layer and moves the selection with it, shrunk to the moved pixels: the selection limited to where the layer has pixels, as in Photoshop; a mask or an empty piece keeps the selection's shape; one history step)"##,enabled: |s| s.active().map(|_| ()).ok_or_else(|| "no document".into()),journal: true,run: |s, _| drop_floating(s),diff --git a/crates/engine/src/float_cmds/tests.rs b/crates/engine/src/float_cmds/tests.rsindex bb75eccf4..f9a7e8572 100644--- a/crates/engine/src/float_cmds/tests.rs+++ b/crates/engine/src/float_cmds/tests.rs@@ -223,10 +223,10 @@ fn moving_shrinks_the_selection_to_the_moved_pixels_at_every_depth() {}}-/// The new selection is the old one times the layer's transparency: a soft selection over-/// half-transparent pixels keeps both (Photoshop's selection ∩ transparency).+/// The new selection is the old one where the layer has pixels: a soft selection keeps its soft+/// edge, and half-transparent pixels stay as selected as they were, so they move whole next time.#[test]-fn the_outline_keeps_soft_edges_and_partial_transparency() {+fn the_outline_keeps_soft_edges_and_selects_half_transparent_pixels_as_before() {let (mut s, id) = session();s.edit("soften", |doc, _| {// Half-transparent red over (30..40, 10..30), beside the opaque (10..30)² square.@@ -244,7 +244,7 @@ fn the_outline_keeps_soft_edges_and_partial_transparency() {let near = |x: i32, y: i32, want: f32| assert!((selected(doc, x, y) - want).abs() < 0.01, "({x}, {y}): {} for {want}", selected(doc, x, y));near(25, 20, 1.0); // fully selected, opaquenear(35, 20, 0.5); // half selected, opaque- near(45, 20, 0.25); // half selected, half transparent+ near(45, 20, 0.5); // half selected, half transparent: as selected as beforenear(5, 5, 0.0); // selected, but nothing thereassert_eq!(doc.selection.as_ref().unwrap().content_bounds(), Rect::new(20, 10, 50, 30));}@@ -279,3 +279,37 @@ fn the_selection_just_moves_without_transparency_to_shrink_to() {s.execute("select.drop", json!({})).unwrap();assert_eq!(s.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(), Rect::new(10, 5, 50, 45), "the mask");}++/// #7538: an anti-aliased shape moved, dropped, then moved again from its shrunk selection leaves+/// nothing behind. With the selection shrunk to the pixels' alpha, the second move left+/// a × (1 − a) of each edge pixel (up to 25 %), a faint outline where the shape had been.+#[test]+fn moving_again_from_the_shrunk_selection_leaves_no_edge_behind() {+ for depth in [8, 16, 32] {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 120, "height": 100, "background": "transparent", "depth": depth})).unwrap();+ s.execute("select.rect", json!({"x": 20, "y": 20, "width": 50, "height": 40, "ellipse": true})).unwrap();+ s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap();+ let id = s.active().unwrap().active_layer.unwrap();+ let total = |doc: &Document| (0..100).flat_map(|y| (0..120).map(move |x| (x, y))).map(|(x, y)| alpha(doc, id, x, y)).sum::<f32>();+ let (before, soft) = {+ let doc = &s.active().unwrap().doc;+ (total(doc), (0..100).flat_map(|y| (0..120).map(move |x| (x, y))).filter(|&(x, y)| (0.01..0.99).contains(&alpha(doc, id, x, y))).count())+ };+ assert!(soft > 20, "{depth}-bit: the ellipse has anti-aliased edges ({soft})");+ s.execute("select.rect", json!({"x": 10, "y": 10, "width": 70, "height": 60})).unwrap();+ s.execute("select.float", json!({"dx": 40, "dy": 30})).unwrap();+ s.execute("select.drop", json!({})).unwrap();+ // The ellipse now sits at (60..110, 50..90); move it back where it started.+ s.execute("select.float", json!({"dx": -40, "dy": -30})).unwrap();+ s.execute("select.drop", json!({})).unwrap();+ let doc = &s.active().unwrap().doc;+ let leftover = (50..100)+ .flat_map(|y| (55..115).map(move |x| (x, y)))+ .filter(|&(x, y)| x >= 72 || y >= 62)+ .map(|(x, y)| alpha(doc, id, x, y))+ .fold(0.0f32, f32::max);+ assert_eq!(leftover, 0.0, "{depth}-bit: nothing left where it was moved from");+ assert!((total(doc) - before).abs() < 0.05, "{depth}-bit: every edge pixel came back whole ({} vs {before})", total(doc));+ }+}diff --git a/docs/ui-parity.md b/docs/ui-parity.mdindex 1068cc812..008204400 100644--- a/docs/ui-parity.md+++ b/docs/ui-parity.md@@ -94,7 +94,7 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme| Arrow nudge of path points with Direct Selection | missing | no arrow handling in `direct_select.rs` || ⌘-arrow nudges the layer from any tool | missing | #2474 || ⌥-drag duplicates, ⇧ constrains to 45° | done | `move_mods.rs` |-| Dragging selected pixels shrinks the selection to them (the selection times the layer's transparency) as the drag starts, also with ⌥; the move box, snapping and Show Transform Controls use the moved pixels' bounds | done | #7538, `canvas.rs` `floating_outline` and `transform_controls_rect`, `snap_ui.rs` `follow_cut`, engine `float_cmds.rs` `CutParts::outline` |+| Dragging selected pixels shrinks the selection to them (the selection limited to where the layer has pixels, so moving again leaves no edge behind) as the drag starts, also with ⌥; the move box, snapping and Show Transform Controls use the moved pixels' bounds | done | #7538, `canvas.rs` `floating_outline` and `transform_controls_rect`, `snap_ui.rs` `follow_cut`, engine `float_cmds.rs` `CutParts::outline` || Auto-Select with hover outline, box-select layers, double-click fit | missing | TOOL-214-4 || Auto-Select bounding box and snapping follow the picked layer | partial | follow the previous layer (#2666) || Several layers selected (⇧-click adds, box-select): Show Transform Controls frames them all, with every selected shape's outline | partial: the shared frame has no handles, since Free Transform works on one layer (`canvas.rs` `transform_controls_layers`, `several_selected_layers_share_one_frame_without_handles`; `move_auto_select_tests.rs`) | |