Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Move tool: soft edges stay partly selected and move whole, as in Photoshop (#7538)

Hector Villarrealafa87061390e
diff --git a/crates/engine/src/float_cmds.rs b/crates/engine/src/float_cmds.rs
index 323c0bee9..92e48692d 100644
--- a/crates/engine/src/float_cmds.rs
+++ b/crates/engine/src/float_cmds.rs
@@ -33,9 +33,10 @@ pub struct CutParts {
pub layer: LayerId,
rest: Surface,
piece: Surface,
- /// 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.
+ /// The selection the moved pixels take, unmoved: the alpha the piece took, 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.
outline: Option<Surface>,
/// The pieces are the layer's mask (`"target":"mask"`), not its pixels.
mask: bool,
@@ -76,20 +77,32 @@ impl CutParts {
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 });
+ let k = sel.sample_channel(b.x0 + (i % w) as i32, b.y0 + (i / w) as i32, 0).clamp(0.0, 1.0);
+ if mask {
+ // A mask has no alpha of its own: its selected part moves by the selection.
+ if k <= 0.0 {
+ p.fill(0.0);
+ } else {
+ p[a] *= k;
+ }
+ continue;
}
- if k <= 0.0 {
+ // The selection is how much of each pixel's alpha moves, at most all of it, as
+ // Photoshop's Move tool lifts it (#7538): a feathered selection over opaque pixels
+ // lifts k of them and leaves 1 − k, while one no stronger than the pixels' own
+ // alpha (the outline a previous move left) lifts them whole. Multiplying instead
+ // left a × (1 − a) of every half-transparent edge pixel behind, a faint outline.
+ let lifted = p[a].min(k);
+ // The moved pixels' selection is what they took with them (Photoshop's marching
+ // ants hug the 50 % line of a soft edge), so moving them again lifts them whole.
+ cover.push(lifted);
+ if lifted <= 0.0 {
p.fill(0.0);
} else {
- p[a] *= k;
+ p[a] = lifted;
}
if !copy && !fill {
- r[a] *= 1.0 - k;
+ r[a] -= lifted;
}
}
if !mask {
@@ -149,6 +162,9 @@ pub struct Floating {
pub offset: (i32, i32),
pub revision: u64,
pub parts: Arc<CutParts>,
+ /// The piece has been moved at least once, even if it is back where it was cut (nudged away
+ /// and back): its selection is the moved pixels' then, as each of Photoshop's nudges leaves it.
+ pub moved: bool,
}
/// The floating piece of `st`, if it is still valid (the document hasn't changed under it).
@@ -200,10 +216,11 @@ fn float(s: &mut Session, p: &Value) -> Result<Value> {
if floating(st).is_none() {
let layer = st.active_layer.ok_or_else(|| EngineError::Other("no active layer".into()))?;
let parts = CutParts::new(&st.doc, layer, copy, background, crate::commands::is_mask_target(p))?;
- st.floating = Some(Floating { layer, offset: (0, 0), revision: st.revision, parts: Arc::new(parts) });
+ st.floating = Some(Floating { layer, offset: (0, 0), revision: st.revision, parts: Arc::new(parts), moved: false });
}
let f = st.floating.as_mut().ok_or(EngineError::NoDocument)?;
f.offset = (f.offset.0.saturating_add(dx), f.offset.1.saturating_add(dy));
+ f.moved |= (dx, dy) != (0, 0);
Ok(json!({"layer": f.layer.0, "offset": [f.offset.0, f.offset.1]}))
}
@@ -218,6 +235,15 @@ pub fn drop_floating(s: &mut Session) -> Result<Value> {
st.floating = None;
let (dx, dy) = f.offset;
if (dx, dy) == (0, 0) {
+ // Back where it was cut: the pixels are as they were. A piece that never moved (a click)
+ // changes nothing; one nudged away and back keeps the moved pixels' selection (#7538).
+ if let Some(outline) = f.parts.outline().filter(|_| f.moved) {
+ let outline = outline.clone();
+ s.edit("Move", |doc, _| {
+ doc.selection = Some(outline);
+ Ok(())
+ })?;
+ }
return Ok(json!({"dropped": true, "offset": [0, 0]}));
}
s.edit("Move", |doc, _| {
diff --git a/crates/engine/src/float_cmds/tests.rs b/crates/engine/src/float_cmds/tests.rs
index f9a7e8572..eca31d43a 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 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.
+/// The new selection is what each pixel took with it: the selection, at most the pixel's alpha. A
+/// soft selection keeps its soft edge; a half-transparent pixel half selected moves whole.
#[test]
-fn the_outline_keeps_soft_edges_and_selects_half_transparent_pixels_as_before() {
+fn the_outline_is_what_the_moved_pixels_took() {
let (mut s, id) = session();
s.edit("soften", |doc, _| {
// Half-transparent red over (30..40, 10..30), beside the opaque (10..30)² square.
@@ -243,8 +243,8 @@ fn the_outline_keeps_soft_edges_and_selects_half_transparent_pixels_as_before()
let doc = &s.active().unwrap().doc;
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, opaque
- near(35, 20, 0.5); // half selected, opaque
- near(45, 20, 0.5); // half selected, half transparent: as selected as before
+ near(35, 20, 0.5); // half selected, opaque: half of it moved
+ near(45, 20, 0.5); // half selected, half transparent: all of it moved
near(5, 5, 0.0); // selected, but nothing there
assert_eq!(doc.selection.as_ref().unwrap().content_bounds(), Rect::new(20, 10, 50, 30));
}
@@ -313,3 +313,68 @@ fn moving_again_from_the_shrunk_selection_leaves_no_edge_behind() {
assert!((total(doc) - before).abs() < 0.05, "{depth}-bit: every edge pixel came back whole ({} vs {before})", total(doc));
}
}
+
+/// #7538, measured in Photoshop 2025: after moving a soft-edged shape from a loose marquee, the
+/// selection's strength at each edge pixel is that pixel's alpha (its marching ants run along the
+/// edge's 50 % line, and Quick Mask shows the edge partly selected), and a feathered selection
+/// over opaque pixels still lifts only part of them, leaving the rest behind.
+#[test]
+fn a_soft_edge_is_as_selected_as_it_is_opaque_and_feathered_moves_leave_the_rest() {
+ let (mut s, id) = session();
+ s.edit("soft edge", |doc, _| {
+ let surf = doc.layer_mut(id).unwrap().surface_mut().unwrap();
+ for (x, a) in [(30, 0.75), (31, 0.5), (32, 0.25)] {
+ surf.fill_rect(Rect::new(x, 10, x + 1, 30), &[1.0, 0.0, 0.0, a]);
+ }
+ Ok(())
+ })
+ .unwrap();
+ s.execute("select.rect", json!({"x": 5, "y": 5, "width": 40, "height": 30})).unwrap();
+ s.execute("select.float", json!({"dx": 10, "dy": 0})).unwrap();
+ s.execute("select.drop", json!({})).unwrap();
+ let doc = &s.active().unwrap().doc;
+ for (x, a) in [(39, 1.0), (40, 0.75), (41, 0.5), (42, 0.25), (43, 0.0)] {
+ assert!((selected(doc, x, 20) - a).abs() < 0.01, "x {x}: selected {} where alpha {a}", selected(doc, x, 20));
+ assert!((alpha(doc, id, x, 20) - a).abs() < 0.01, "x {x}: the pixel moved whole");
+ }
+ assert!((10..20).all(|x| alpha(doc, id, x, 20) == 0.0), "nothing left where it was");
+
+ // A feathered (half) selection over opaque pixels lifts half of them and leaves half.
+ let (mut s, id) = session();
+ s.edit("half", |doc, _| {
+ let mut sel = Surface::new(PixelFormat::GRAY8);
+ sel.fill_rect(Rect::new(10, 10, 30, 30), &[0.5]);
+ doc.selection = Some(sel);
+ Ok(())
+ })
+ .unwrap();
+ s.execute("select.float", json!({"dx": 30, "dy": 0})).unwrap();
+ s.execute("select.drop", json!({})).unwrap();
+ let doc = &s.active().unwrap().doc;
+ assert!((alpha(doc, id, 15, 20) - 0.5).abs() < 0.01 && (alpha(doc, id, 45, 20) - 0.5).abs() < 0.01, "half left, half moved");
+}
+
+/// #7538: arrow nudges add up to one floating piece, so up then down is back at no offset. The
+/// selection is the moved pixels' all the same, as after each of Photoshop's nudges, in one step;
+/// a piece that never moved (a click) changes nothing.
+#[test]
+fn nudging_away_and_back_keeps_the_moved_pixels_selection() {
+ let (mut s, _) = session();
+ s.execute("select.rect", json!({"x": 2, "y": 2, "width": 40, "height": 40})).unwrap();
+ let steps = s.active().unwrap().history.past_len();
+ s.execute("select.float", json!({"dx": 0, "dy": -1})).unwrap();
+ s.execute("select.float", json!({"dx": 0, "dy": 1})).unwrap();
+ s.execute("select.drop", json!({})).unwrap();
+ let st = s.active().unwrap();
+ assert_eq!(st.doc.selection.as_ref().map(|s| s.content_bounds()), Some(Rect::new(10, 10, 30, 30)), "the moved square's");
+ assert_eq!(st.history.past_len(), steps + 1, "one step");
+ s.undo();
+ assert_eq!(s.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds()), Some(Rect::new(2, 2, 42, 42)), "undone");
+
+ // A click: cut, never moved, dropped.
+ s.execute("select.float", json!({"dx": 0, "dy": 0})).unwrap();
+ s.execute("select.drop", json!({})).unwrap();
+ let st = s.active().unwrap();
+ assert_eq!(st.doc.selection.as_ref().map(|s| s.content_bounds()), Some(Rect::new(2, 2, 42, 42)), "a click keeps the marquee");
+ assert_eq!(st.history.past_len(), steps, "and adds no step");
+}