Skip to content

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

layer.translate: reject a non-boolean `selection` even with no offset (#7538)

Hector Villarreal344b8e4be691
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs
index aadb4023f..6400f1214 100644
--- a/crates/engine/src/layer_multi_cmds.rs
+++ b/crates/engine/src/layer_multi_cmds.rs
@@ -361,6 +361,10 @@ pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {
}
Some(other) => return Err(bad("layer.translate", format!("unknown units `{other}` (px|pt)"))),
};
+ let carry = match p.get("selection") {
+ None | Some(Value::Null) => false,
+ Some(v) => v.as_bool().ok_or_else(|| bad("layer.translate", "`selection` must be a boolean"))?,
+ };
if dx == 0 && dy == 0 {
return Ok(Value::Null);
}
@@ -372,10 +376,6 @@ pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {
return Err(EngineError::Other("no active layer".into()));
}
let mask_target = p.get("target").and_then(Value::as_str) == Some("mask");
- let carry = match p.get("selection") {
- None | Some(Value::Null) => false,
- Some(v) => v.as_bool().ok_or_else(|| bad("layer.translate", "`selection` must be a boolean"))?,
- };
let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
let ids = s.edit("Move", |doc, _| {
let (alone, rest): (Vec<LayerId>, Vec<LayerId>) = roots.iter().partition(|id| mask_target && doc.layer(**id).is_some_and(moves_mask_alone));
@@ -2760,6 +2760,11 @@ mod tests {
assert_eq!((sel(&s), bounds(&s, a)), (Some(Rect::new(5, 5, 75, 35)), Rect::new(10, 10, 30, 30)), "undone together");
s.execute("layer.translate", json!({"dx": 7, "dy": 20})).unwrap();
assert_eq!(sel(&s), Some(Rect::new(5, 5, 75, 35)), "without it the selection stays");
- assert!(s.execute("layer.translate", json!({"dx": 1, "selection": "yes"})).is_err(), "a bad `selection` is an error");
+ let state = |s: &Session| (bounds(s, a), bounds(s, b), sel(s), s.active().unwrap().history.past_len());
+ let before = state(&s);
+ for dx in [1, 0] {
+ assert!(s.execute("layer.translate", json!({"dx": dx, "selection": "yes"})).is_err(), "dx {dx}: a bad `selection` is an error");
+ assert_eq!(state(&s), before, "dx {dx}: and changes nothing");
+ }
}
}
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 3267c409c..3b2519ed5 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -8459,6 +8459,20 @@ mod transform_controls_tests {
assert!(app.session.undo());
let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds());
assert_eq!(sel, Some(photocraft_geom::Rect::new(20, 20, 120, 110)), "Undo takes the selection back too");
+
+ // A piece floating (a marquee ⌘-drag with both selected) when the layers are dragged: the
+ // press drops it first, so the ants follow the drag from where it was dropped.
+ app.run("select.float", json!({"dx": 10, "dy": 5})).unwrap();
+ let outline = photocraft_engine::float_cmds::floating(app.session.active().unwrap()).unwrap().parts.outline().unwrap().content_bounds();
+ tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m);
+ let st = app.session.active().unwrap();
+ assert!(photocraft_engine::float_cmds::floating(st).is_none(), "the press dropped the piece");
+ assert_eq!(st.doc.selection.as_ref().map(|s| s.content_bounds()), Some(outline.translate(10, 5)), "at its offset");
+ tool_event(&mut app, ToolEvent::Move { x: 72.0, y: 68.0, pressure: 1.0 }, m);
+ assert_eq!(selection_shown_offset(&app), Some((12, 8)), "then the ants follow the drag");
+ tool_event(&mut app, ToolEvent::Up { x: 72.0, y: 68.0 }, m);
+ let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds());
+ assert_eq!(sel, Some(outline.translate(22, 13)), "released where the ants were");
}
/// A transparent 200² document: a red square (40..90, 40..80) on its layer, a loose marquee