Move: allow multi-layer moves when the active layer is hidden (#18136)

`hidden_target::refusal` in `crates/engine-core/src/hidden_target.rs` checked only the active layer for `layer.translate`, refusing Move drags, Alt+drags, and arrow nudges with "Could not complete your request because the target layer is hidden." whenever a hidden layer happened to be active in a multi-layer selection.

Allow `layer.translate` when no explicit `layer` parameter is given and two or more layers are selected, keeping the refusal for a single hidden layer selected alone and for calls that explicitly name a hidden layer.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
dholzricandCursor committed 2026-12-01 01:43:18 -06:00
1 parent 59153889bd
commit a67b3344ec
2 files changed
+137

No files matched your search

@@ -0,0 +1,130 @@
//! #18136: Moving a multi-layer selection moves every selected layer, hidden ones included, even
//! when the active layer is hidden. Moving a single hidden layer alone, or an explicitly named
//! hidden layer, keeps the hidden-target refusal.
use photocraft_doc::LayerId;
use photocraft_geom::Rect;
use serde_json::json;
use crate::Session;
use crate::hidden_target::{HIDDEN, refusal};
fn painted_layer(s: &mut Session, name: &str, rect: Rect) -> LayerId {
let id = LayerId(s.execute("layer.new.layer", json!({"name": name})).unwrap()["layer"].as_u64().unwrap());
s.edit("paint", |doc, _| {
doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(rect, &[1.0, 0.0, 0.0, 1.0]);
Ok(())
})
.unwrap();
id
}
fn select(s: &mut Session, ids: &[LayerId]) {
for (i, id) in ids.iter().enumerate() {
let mode = if i == 0 { "replace" } else { "add" };
s.execute("layer.select", json!({"layer": id.0, "mode": mode})).unwrap();
}
assert_eq!(s.active().unwrap().active_layer, ids.last().copied());
}
fn bounds(s: &Session, id: LayerId) -> Option<Rect> {
crate::damage::layer_bounds(s.active().unwrap().doc.layer(id).unwrap())
}
fn three_layer_doc() -> (Session, LayerId, LayerId, LayerId) {
let mut s = crate::session();
s.execute("file.new", json!({"width": 1000, "height": 700, "background": "white"})).unwrap();
let a = painted_layer(&mut s, "A", Rect::new(10, 20, 50, 60));
let b = painted_layer(&mut s, "B", Rect::new(80, 90, 130, 140));
let h = painted_layer(&mut s, "Hidden", Rect::new(100, 450, 200, 500));
s.execute("layer.setProps", json!({"layer": h.0, "visible": false})).unwrap();
(s, a, b, h)
}
#[test]
fn multi_layer_selection_moves_every_layer_when_the_active_layer_is_hidden() {
for hidden_active in [true, false] {
let (mut s, a, b, h) = three_layer_doc();
let order = if hidden_active { [a, b, h] } else { [h, a, b] };
select(&mut s, &order);
assert_eq!(refusal(&s, "layer.translate", &json!({})), None, "hidden_active={hidden_active}");
let (ra, rb, rh) = (bounds(&s, a).unwrap(), bounds(&s, b).unwrap(), bounds(&s, h).unwrap());
let steps = s.active().unwrap().history.past_len();
s.execute("layer.translate", json!({"dx": 100, "dy": 50})).unwrap();
assert_eq!(bounds(&s, a), Some(ra.translate(100, 50)));
assert_eq!(bounds(&s, b), Some(rb.translate(100, 50)));
assert_eq!(bounds(&s, h), Some(rh.translate(100, 50)));
let doc = &s.active().unwrap().doc;
assert!(!doc.layer(h).unwrap().visible, "the hidden layer stays hidden");
assert!(doc.layer(a).unwrap().visible && doc.layer(b).unwrap().visible);
let hist = &s.active().unwrap().history;
assert_eq!((hist.past_len(), hist.undo_label()), (steps + 1, Some("Move")));
}
// A layer hidden through a hidden parent group also moves when selected with another layer,
// and two hidden layers selected together follow the same multi-layer rule.
let mut s = crate::session();
s.execute("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap();
let vis = painted_layer(&mut s, "Visible", Rect::new(10, 10, 30, 30));
let nested = painted_layer(&mut s, "Nested", Rect::new(40, 40, 60, 60));
select(&mut s, &[nested]);
let group = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap());
s.execute("layer.setProps", json!({"layer": group.0, "visible": false})).unwrap();
select(&mut s, &[vis, nested]);
let (rv, rn) = (bounds(&s, vis).unwrap(), bounds(&s, nested).unwrap());
s.execute("layer.translate", json!({"dx": 15, "dy": 25})).unwrap();
assert_eq!((bounds(&s, vis), bounds(&s, nested)), (Some(rv.translate(15, 25)), Some(rn.translate(15, 25))));
s.execute("layer.setProps", json!({"layer": vis.0, "visible": false})).unwrap();
select(&mut s, &[vis, nested]);
let (rv, rn) = (bounds(&s, vis).unwrap(), bounds(&s, nested).unwrap());
s.execute("layer.translate", json!({"dx": 5, "dy": 10})).unwrap();
assert_eq!((bounds(&s, vis), bounds(&s, nested)), (Some(rv.translate(5, 10)), Some(rn.translate(5, 10))));
}
#[test]
fn a_single_hidden_layer_alone_is_still_refused() {
let (mut s, _, _, h) = three_layer_doc();
select(&mut s, &[h]);
let rh = bounds(&s, h);
let steps = s.active().unwrap().history.past_len();
assert_eq!(refusal(&s, "layer.translate", &json!({})), Some(HIDDEN));
let err = s.execute("layer.translate", json!({"dx": 100, "dy": 50})).unwrap_err().to_string();
assert!(err.contains(HIDDEN), "{err}");
assert_eq!(bounds(&s, h), rh);
assert_eq!(s.active().unwrap().history.past_len(), steps);
// A single layer hidden through its parent group is still refused too.
let child = painted_layer(&mut s, "InGroup", Rect::new(20, 20, 40, 40));
select(&mut s, &[child]);
let g = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap());
s.execute("layer.setProps", json!({"layer": g.0, "visible": false})).unwrap();
select(&mut s, &[child]);
let rc = bounds(&s, child);
let steps = s.active().unwrap().history.past_len();
assert_eq!(refusal(&s, "layer.translate", &json!({})), Some(HIDDEN));
assert!(s.execute("layer.translate", json!({"dx": 10, "dy": 10})).unwrap_err().to_string().contains(HIDDEN));
assert_eq!(bounds(&s, child), rc);
assert_eq!(s.active().unwrap().history.past_len(), steps);
}
#[test]
fn an_explicit_hidden_layer_param_is_still_refused() {
let (mut s, a, b, h) = three_layer_doc();
for order in [[a, b, h], [h, a, b]] {
select(&mut s, &order);
let before = s.active().unwrap().doc.clone();
let steps = s.active().unwrap().history.past_len();
for layer in [json!(h.0), json!("Hidden")] {
let err = s.execute("layer.translate", json!({"layer": layer, "dx": 100, "dy": 50})).unwrap_err().to_string();
assert!(err.contains(HIDDEN), "{err}");
assert_eq!(s.active().unwrap().doc, before);
assert_eq!(s.active().unwrap().history.past_len(), steps);
}
}
}
+7
View File
@@ -55,9 +55,16 @@ pub fn refusal(s: &Session, id: &str, params: &Value) -> Option<&'static str> {
return None;
}
let st = s.active()?;
if id == "layer.translate" && matches!(crate::commands::explicit_layer(s, &params), Ok(None)) && st.selected_layers().len() > 1 {
return None;
}
let layer = crate::commands::layer_param(s, &params).ok()?;
// The Art History Brush's text was measured on a layer's pixels only.
let pixels = crate::channel_cmds::target_of(&params) == crate::channel_cmds::Target::Pixels;
let why = if id == "paint.artHistoryBrush" && pixels { ART_HISTORY_HIDDEN } else { HIDDEN };
(st.doc.layer(layer).is_some() && !shown(&st.doc, layer)).then_some(why)
}
#[cfg(test)]
#[path = "cmd/layer_multi_cmds/move_hidden_in_selection_tests.rs"]
mod move_hidden_in_selection_tests;