Merge PR #6440: fix(engine): convert the background and match mask depth where they were missed

This commit is contained in:
aiongg committed 2026-10-22 02:22:00 +00:00
commit 4484a07a5c
6 files changed
+115 -5

No files matched your search

+2 -1
View File
@@ -66,7 +66,8 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
}
}
crate::extra_cmds::background_to_layer_for_mask(doc, id);
doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.mask = Some(LayerMask { surface: matting::region_surface(&region), ..LayerMask::reveal_all() });
let mask = crate::commands::mask_at_depth(LayerMask { surface: matting::region_surface(&region), ..LayerMask::reveal_all() }, doc.depth)?;
doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.mask = Some(mask);
doc.selection = None;
let b = region.bbox;
Ok([b.x0, b.y0, b.width() as i32, b.height() as i32])
+1
View File
@@ -887,6 +887,7 @@ pub fn specs() -> Vec<CommandSpec> {
gray
};
for id in ids {
background_to_layer(doc, id);
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
l.effects = fx.clone();
// Pass Through is a group's mode: a group's style pastes onto other layers as
+4 -3
View File
@@ -72,8 +72,8 @@ fn frame_group(s: &mut Session, grouped: &Value) -> Result<Value> {
crate::layer_multi_cmds::layer_bounds(d.doc.layer(gid).ok_or(EngineError::NoLayer(gid))?).filter(|r| !r.is_empty()).unwrap_or_else(|| d.doc.bounds());
s.edit("Frame from Layers", |doc, _| {
let g = doc.layer_mut(gid).ok_or(EngineError::NoLayer(gid))?;
g.mask = Some(clip_mask(rect));
let mask = crate::commands::mask_at_depth(clip_mask(rect), doc.depth)?;
doc.layer_mut(gid).ok_or(EngineError::NoLayer(gid))?.mask = Some(mask);
Ok(())
})?;
Ok(json!({"layer": gid.0, "frame": [rect.x0, rect.y0, rect.width(), rect.height()]}))
@@ -133,7 +133,8 @@ fn new_frame(s: &mut Session, p: &Value) -> Result<Value> {
};
let name = p.get("name").and_then(Value::as_str).map(str::to_string);
let gid = s.edit("New Frame", |doc, active| {
let mask = if ellipse { ellipse_mask(rect, doc.bounds()) } else { clip_mask(rect.intersect(&doc.bounds())) };
let mask =
crate::commands::mask_at_depth(if ellipse { ellipse_mask(rect, doc.bounds()) } else { clip_mask(rect.intersect(&doc.bounds())) }, doc.depth)?;
let name = name.unwrap_or_else(|| doc.next_layer_name("Frame"));
let gid = match content {
Some(id) => {
+4
View File
@@ -914,6 +914,9 @@ fn replace_effects(s: &mut Session, p: &Value) -> Result<Value> {
crate::pattern_cmds::ensure_in_doc(doc, pat);
}
let mode = doc.mode;
if !list.is_empty() {
crate::extra_cmds::background_to_layer(doc, id);
}
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
// The list's master switch (`layer.setEffectsVisible`, the Layers panel's "Effects" eye)
// survives the edit: the dialog models per-effect state, not the switch (#3648). A layer
@@ -937,6 +940,7 @@ fn set_effect(s: &mut Session, p: &Value, kind: &str) -> Result<Value> {
}
// Colours in the document's mode, as Image › Mode keeps them.
let fx = fx.in_mode(doc.mode);
crate::extra_cmds::background_to_layer(doc, id);
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
// Adding or replacing one effect leaves the list's master switch alone (#3648); a fresh
// effects list already has the default `enabled: true`, so a first effect still shows.
+2 -1
View File
@@ -1425,7 +1425,8 @@ fn vector_mask_add(s: &mut Session, p: &Value, from_path: bool) -> Result<Value>
if locked {
return Err(EngineError::Other(format!("layer \"{}\" is locked", l.name)));
}
l.vector_mask = Some(VectorMask::new(path));
crate::extra_cmds::background_to_layer(doc, id);
doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.vector_mask = Some(VectorMask::new(path));
Ok(())
})?;
vector_mask_info(s, id)
@@ -0,0 +1,102 @@
use photocraft_color::SampleType;
use photocraft_engine::Session;
use serde_json::{Value, json};
const STYLES: [&str; 10] =
["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "patternOverlay", "bevelEmboss", "satin"];
const VECTOR_MASKS: [&str; 3] = ["layer.vectorMask.add", "layer.vectorMask.revealAll", "layer.vectorMask.hideAll"];
fn background_only() -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 40, "height": 30})).unwrap();
s
}
fn has_background(s: &Session) -> bool {
s.active().unwrap().doc.background().is_some()
}
fn converts_and_undoes(cmd: &str, params: Value) {
let mut s = background_only();
s.execute(cmd, params).unwrap_or_else(|e| panic!("{cmd}: {e}"));
assert!(!has_background(&s), "{cmd} left a Background carrying the change");
s.execute("edit.undo", json!({})).unwrap();
assert!(has_background(&s), "{cmd}: one undo restores the Background");
}
#[test]
fn layer_styles_and_vector_masks_turn_the_background_into_a_layer() {
for kind in STYLES {
converts_and_undoes(&format!("layer.layerStyle.{kind}"), json!({}));
}
for cmd in VECTOR_MASKS {
converts_and_undoes(cmd, json!({}));
}
converts_and_undoes("layer.layerStyle.replace", json!({"effects": [{"kind": "stroke"}]}));
}
#[test]
fn clearing_a_style_keeps_the_background() {
let mut s = background_only();
s.execute("layer.layerStyle.replace", json!({"effects": []})).unwrap();
assert!(has_background(&s));
}
fn mask_sample(s: &Session) -> SampleType {
let st = s.active().unwrap();
st.doc.walk().into_iter().find_map(|(_, _, l)| l.mask.as_ref().map(|m| m.surface.format().sample)).expect("a layer mask")
}
fn painted(depth: u32) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 40, "height": 30, "depth": depth})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("paint.stroke", json!({"points": [[5, 10], [30, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap();
s
}
#[test]
fn frame_and_remove_background_masks_match_the_document_depth() {
for depth in [16, 32] {
let want = if depth == 16 { SampleType::U16 } else { SampleType::F32 };
for (cmd, params) in [
("layer.new.frameFromLayers", json!({})),
("layer.new.frame", json!({"x": 4, "y": 4, "width": 16, "height": 12})),
("layer.new.frame", json!({"x": 4, "y": 4, "width": 16, "height": 12, "shape": "ellipse"})),
("layer.removeBackground", json!({})),
] {
let mut s = painted(depth);
s.execute(cmd, params.clone()).unwrap_or_else(|e| panic!("{cmd} {params}: {e}"));
assert_eq!(mask_sample(&s), want, "{cmd} {params} in a {depth}-bit document");
}
}
}
fn copied_style_over_background() -> (Session, u64, u64) {
let mut s = background_only();
let bg = s.active().unwrap().doc.background().unwrap().id.0;
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("layer.layerStyle.dropShadow", json!({})).unwrap();
s.execute("layer.layerStyle.copyLayerStyle", json!({})).unwrap();
let layer = s.active().unwrap().active_layer.unwrap().0;
(s, bg, layer)
}
#[test]
fn pasting_a_style_onto_the_background_turns_it_into_a_layer() {
let (mut s, bg, _) = copied_style_over_background();
s.execute("layer.layerStyle.pasteLayerStyle", json!({"layer": bg})).unwrap();
assert!(!has_background(&s), "the pasted style landed on a Background");
s.execute("edit.undo", json!({})).unwrap();
assert!(has_background(&s), "one undo restores the Background");
}
#[test]
fn pasting_a_style_with_no_layer_skips_the_background() {
let (mut s, bg, layer) = copied_style_over_background();
s.execute("layer.select", json!({"layer": bg})).unwrap();
s.execute("layer.select", json!({"layer": layer, "mode": "add"})).unwrap();
s.execute("layer.layerStyle.pasteLayerStyle", json!({})).unwrap();
assert!(has_background(&s));
assert!(s.active().unwrap().doc.background().unwrap().effects.items.is_empty());
}