Merge #18582 into the merge queue batch

Layer Align: grey it for a selected layer with Lock Position (#18443)
This commit is contained in:
David Mann committed 2026-12-01 10:55:58 +01:00
commit fa80f38c48
3 files changed
+205 -3

No files matched your search

@@ -19,7 +19,7 @@ mod selection;
mod structure;
pub use crate::damage::{layer_bounds, same_pixels, selection_only_change, step_damage};
use align::{align, distribute};
use align::{align, distribute, position_locked};
use moves::link_groups;
pub use moves::shift_surface;
#[cfg(test)]
@@ -52,8 +52,9 @@ fn can_align(s: &Session) -> std::result::Result<(), String> {
match d.selected_layers().len() {
0 => Err("no layer selected".into()),
// Photoshop aligns to an active selection however many layers are selected; without one,
// a single layer aligns to the canvas and 2+ align to each other.
_ => Ok(()),
// a single layer aligns to the canvas and 2+ align to each other. A selected layer with
// Lock Position greys the entries (#18443).
_ => position_locked(&d.doc, d.selected_layers(), false).map_or(Ok(()), Err),
}
}
fn can_distribute(s: &Session) -> std::result::Result<(), String> {
@@ -47,12 +47,29 @@ fn bounded_selection(doc: &Document, sel: &[LayerId]) -> Vec<(LayerId, Rect)> {
top_level(doc, sel).into_iter().filter_map(|id| Some((id, align_bounds(doc.layer(id)?, doc.bounds(), doc.pixel_format().sample)?))).collect()
}
/// The refusal for a selected layer whose position is locked, naming it (#18443). `lock_all` counts Lock All as well:
/// the command refuses it, while its menu state was not measured. The Background is locked in place by construction
/// and still takes part. A locked layer together with a pixel selection was not measured either: there only
/// `move_layers` refuses, when the layer would have to move.
pub(super) fn position_locked(doc: &Document, sel: &[LayerId], lock_all: bool) -> Option<String> {
let held = sel.iter().filter_map(|id| doc.layer(*id)).find(|l| {
let locks = doc.effective_locks(l.id);
(locks.position || (lock_all && locks.all)) && !doc.is_background(l.id)
})?;
let pixel_selection = doc.selection.as_ref().is_some_and(|m| !m.content_bounds().is_empty());
(!pixel_selection).then(|| format!("layer \"{}\" is position-locked", held.name))
}
pub(super) fn align(s: &mut Session, p: &Value, kind: &str) -> Result<Value> {
let cmd = format!("layer.align.{kind}");
let edge = Edge::parse(kind).ok_or_else(|| bad(&cmd, "unknown edge"))?;
let sel = selected(s);
let to = p.get("to").and_then(Value::as_str).unwrap_or("auto").to_string();
let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
// Refused before anything moves, also when the locked layer would stay where it is.
if let Some(refusal) = position_locked(&before, &sel, true) {
return Err(EngineError::Other(refusal));
}
// The History state is named after the entry (#18283).
let name = match edge {
Edge::Top => "Align Top Edges",
@@ -198,3 +215,7 @@ mod align_history_names_tests;
#[cfg(test)]
#[path = "align_half_pixel_tests.rs"]
mod align_half_pixel_tests;
#[cfg(test)]
#[path = "align_position_lock_enabled_tests.rs"]
mod align_position_lock_enabled_tests;
@@ -0,0 +1,180 @@
//! #18443: Layer › Align is greyed when a selected layer has Lock Position, and the command refuses a selected layer
//! with Lock Position or Lock All by name, also when that layer would not move. Lock Transparent Pixels, Lock Image
//! Pixels and the Background in the selection do not block it. Not measured, so held as they were: the menu state
//! with Lock All, and a locked layer together with a pixel selection.
use photocraft_doc::{Document, LayerId, Locks};
use photocraft_geom::Rect;
use serde_json::json;
use crate::Session;
const ENTRIES: [&str; 6] = [
"layer.align.topEdges",
"layer.align.verticalCenters",
"layer.align.bottomEdges",
"layer.align.leftEdges",
"layer.align.horizontalCenters",
"layer.align.rightEdges",
];
const POSITION: Locks = Locks { transparency: false, pixels: false, position: true, artboard: false, all: false };
const ALL: Locks = Locks { transparency: false, pixels: false, position: false, artboard: false, all: true };
const PIXELS: Locks = Locks { transparency: false, pixels: true, position: false, artboard: false, all: false };
const TRANSPARENCY: Locks = Locks { transparency: true, pixels: false, position: false, artboard: false, all: false };
/// The name of the layer the tests lock, which a refusal has to mention.
const HELD: &str = "Held in place";
fn doc(s: &Session) -> &Document {
&s.active().unwrap().doc
}
fn bounds(s: &Session, id: LayerId) -> Rect {
crate::damage::layer_bounds(doc(s).layer(id).unwrap()).unwrap()
}
fn states(s: &Session) -> usize {
s.active().unwrap().history.past_len()
}
fn pixel_layer(s: &mut Session, name: &str, r: Rect) -> LayerId {
let id = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap());
s.edit("paint", |doc, _| {
let layer = doc.layer_mut(id).unwrap();
layer.name = name.into();
layer.surface_mut().unwrap().fill_rect(r, &[1.0, 0.0, 0.0, 1.0]);
Ok(())
})
.unwrap();
id
}
/// A 100 x 80 document with a Background and two pixel layers: the one the tests lock, and a larger one down and to
/// the right of it. Left Edges and Top Edges leave the first where it is; the other four entries have to move it.
fn two_layers() -> (Session, LayerId, LayerId) {
let mut s = crate::session();
s.execute("file.new", json!({"width": 100, "height": 80})).unwrap();
let held = pixel_layer(&mut s, HELD, Rect::new(10, 10, 20, 20));
let free = pixel_layer(&mut s, "Free", Rect::new(40, 30, 70, 60));
(s, held, free)
}
/// Selects `ids`; the last one is the active layer.
fn select(s: &mut Session, ids: &[LayerId]) {
s.execute("layer.select", json!({"layer": ids[0].0})).unwrap();
for id in &ids[1..] {
s.execute("layer.select", json!({"layer": id.0, "mode": "add"})).unwrap();
}
}
fn lock(s: &mut Session, id: LayerId, locks: Locks) {
s.edit("lock", |doc, _| {
doc.layer_mut(id).unwrap().locks = locks;
Ok(())
})
.unwrap();
}
#[test]
fn lock_position_on_a_selected_layer_greys_the_six_entries() {
for held_is_active in [false, true] {
let (mut s, held, free) = two_layers();
select(&mut s, &if held_is_active { [free, held] } else { [held, free] });
for entry in ENTRIES {
assert!(s.is_enabled(entry), "{entry}: enabled without a lock");
}
lock(&mut s, held, POSITION);
for entry in ENTRIES {
let reason = s.disabled_reason(entry).unwrap_or_else(|| panic!("{entry} stays enabled, locked layer active: {held_is_active}"));
assert!(reason.contains(HELD), "{entry}: {reason}");
}
}
}
#[test]
fn a_selected_layer_with_lock_position_or_lock_all_refuses_the_align_by_name() {
for (locks, case) in [(POSITION, "Lock Position"), (ALL, "Lock All")] {
// Left Edges and Top Edges would leave the locked layer where it is.
for entry in ENTRIES {
let (mut s, held, free) = two_layers();
select(&mut s, &[held, free]);
lock(&mut s, held, locks);
let (before, states_before) = (doc(&s).clone(), states(&s));
let error = s.execute(entry, json!({})).expect_err(&format!("{entry}, {case}: refused"));
assert!(error.to_string().contains(HELD), "{entry}, {case}: {error}");
assert!(*doc(&s) == before, "{entry}, {case}: nothing moved");
assert_eq!(states(&s), states_before, "{entry}, {case}: no History state");
}
}
}
#[test]
fn lock_transparent_pixels_and_lock_image_pixels_do_not_block() {
for (locks, case) in [(TRANSPARENCY, "Lock Transparent Pixels"), (PIXELS, "Lock Image Pixels")] {
let (mut s, lower, upper) = two_layers();
select(&mut s, &[lower, upper]);
lock(&mut s, lower, locks);
lock(&mut s, upper, locks);
for entry in ENTRIES {
assert!(s.is_enabled(entry), "{entry}, {case}");
}
let was = bounds(&s, lower);
s.execute("layer.align.rightEdges", json!({})).unwrap();
assert!(bounds(&s, lower).x0 > was.x0, "{case}: the locked layer moved");
assert_eq!(bounds(&s, lower).x1, bounds(&s, upper).x1, "{case}: the right edges agree");
}
}
#[test]
fn the_background_in_the_selection_keeps_the_entries_enabled() {
let (mut s, _, free) = two_layers();
let background = doc(&s).background().unwrap().id;
assert!(doc(&s).effective_locks(background).position, "the Background is locked in place");
select(&mut s, &[background, free]);
for entry in ENTRIES {
assert!(s.is_enabled(entry), "{entry}");
}
s.execute("layer.align.rightEdges", json!({})).unwrap();
assert_eq!(bounds(&s, free).x1, doc(&s).bounds().x1, "the layer aligns to the Background's edge");
}
/// Not measured, so as before: Lock All leaves the menu entries enabled, and with a pixel selection a locked layer is
/// refused only when it would have to move.
#[test]
fn the_unmeasured_cases_keep_todays_behaviour() {
let (mut s, held, free) = two_layers();
select(&mut s, &[held, free]);
lock(&mut s, held, ALL);
for entry in ENTRIES {
assert!(s.is_enabled(entry), "{entry}: Lock All");
}
for (locks, case) in [(POSITION, "Lock Position"), (ALL, "Lock All")] {
let (mut s, held, free) = two_layers();
select(&mut s, &[held, free]);
lock(&mut s, held, locks);
// A selection whose left edge is the locked layer's.
s.execute("select.rect", json!({"x": 10, "y": 40, "width": 50, "height": 20})).unwrap();
for entry in ENTRIES {
assert!(s.is_enabled(entry), "{entry}: {case} with a pixel selection");
}
s.execute("layer.align.leftEdges", json!({})).unwrap();
assert_eq!(bounds(&s, free).x0, bounds(&s, held).x0, "{case}: the other layer aligns to the selection");
assert!(s.execute("layer.align.rightEdges", json!({})).is_err(), "{case}: the locked layer would have to move");
}
}
/// Not in the item: a lock inherited from a group counts as the layer's, as it does when a move is refused.
#[test]
fn lock_position_inherited_from_a_group_greys_the_entries_too() {
let (mut s, held, free) = two_layers();
select(&mut s, &[held]);
let group = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap());
lock(&mut s, group, POSITION);
select(&mut s, &[held, free]);
assert!(!doc(&s).layer(held).unwrap().locks.position && doc(&s).effective_locks(held).position, "only the group holds the lock");
for entry in ENTRIES {
assert!(!s.is_enabled(entry), "{entry}");
}
}