diff --git a/crates/engine-vector/src/cmd/layer_css_cmds.rs b/crates/engine-vector/src/cmd/layer_css_cmds.rs index cf6e12a66c..5d0e2a8f35 100644 --- a/crates/engine-vector/src/cmd/layer_css_cmds.rs +++ b/crates/engine-vector/src/cmd/layer_css_cmds.rs @@ -50,9 +50,39 @@ fn enabled(s: &Session) -> std::result::Result<(), String> { if ids.is_empty() { return Err("no layer selected".into()); } + // Greyed in these document modes, for more than one selected layer, for a smart object and for a + // hidden shape, as measured in the reference application (#18301). Other modes and other hidden + // layers were not measured. + use photocraft_color::ColorMode; + let mode = match st.doc.mode { + ColorMode::Cmyk => Some("CMYK"), + ColorMode::Grayscale => Some("Grayscale"), + ColorMode::Lab => Some("Lab"), + ColorMode::Indexed => Some("Indexed Color"), + _ => None, + }; + if let Some(mode) = mode { + return Err(format!("not available in {mode} mode")); + } + if ids.len() > 1 { + return Err("more than one layer is selected".into()); + } + let selected = st.doc.walk().into_iter().map(|(_, _, l)| l).find(|l| ids.contains(&l.id)).ok_or("no layer selected")?; + // A fill layer with a vector mask is a shape here too. + let shape = match &selected.content { + LayerContent::Shape(_) => true, + LayerContent::Fill(_) => selected.vector_mask.is_some(), + _ => false, + }; + if matches!(selected.content, LayerContent::Smart(_)) { + return Err("the layer is a smart object".into()); + } + if shape && !selected.visible { + return Err("the shape layer is hidden".into()); + } // Photoshop greys Copy CSS out for a fill layer without a shape (measured), and for an // adjustment layer, which has nothing to place either. - if st.doc.walk().iter().any(|(_, _, l)| ids.contains(&l.id) && has_css(l)) { Ok(()) } else { Err("no selected layer has CSS".into()) } + if has_css(selected) { Ok(()) } else { Err("no selected layer has CSS".into()) } } /// Whether a layer can have a rule (one with nothing to place still gets none). @@ -374,3 +404,7 @@ pub fn specs() -> Vec { #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "layer_css_enabled_states_tests.rs"] +mod enabled_states_tests; diff --git a/crates/engine-vector/src/cmd/layer_css_enabled_states_tests.rs b/crates/engine-vector/src/cmd/layer_css_enabled_states_tests.rs new file mode 100644 index 0000000000..5b8d86b2d1 --- /dev/null +++ b/crates/engine-vector/src/cmd/layer_css_enabled_states_tests.rs @@ -0,0 +1,120 @@ +//! #18301: Layer › Copy CSS is greyed, with a reason, for a smart object, a hidden shape layer, two or more selected +//! layers and CMYK, Grayscale, Lab and Indexed documents, as in the reference application. One visible shape, type, +//! pixel, Background or group layer in an RGB document keeps it enabled. Hidden layers of other kinds and Bitmap +//! documents were not measured and keep it enabled too. + +use photocraft_doc::LayerId; +use photocraft_geom::Rect; +use serde_json::json; + +use super::ID; +use crate::Session; + +fn document(mode: &str, depth: u32) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 64, "height": 48, "mode": mode, "depth": depth})).unwrap(); + s +} + +fn shape(s: &mut Session) -> u64 { + s.execute("shape.create", json!({"rect": [8, 8, 20, 16], "fill": "#ff0000"})).unwrap()["layer"].as_u64().unwrap() +} + +fn pixels(s: &mut Session) -> u64 { + let id = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap(); + s.edit("setup", |doc, _| { + doc.layer_mut(LayerId(id)).unwrap().surface_mut().unwrap().fill_rect(Rect::new(4, 4, 20, 20), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + id +} + +fn smart_object(s: &mut Session) -> u64 { + pixels(s); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap()["layer"].as_u64().unwrap() +} + +fn point_type(s: &mut Session) -> u64 { + s.execute("type.create", json!({"x": 10, "y": 30, "text": "Hi", "size": 14})).unwrap()["layer"].as_u64().unwrap() +} + +fn group(s: &mut Session) -> u64 { + s.execute("layer.new.group", json!({})).unwrap()["layer"].as_u64().unwrap() +} + +fn hide(s: &mut Session, id: u64) { + s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + s.execute("layer.select", json!({"layer": id})).unwrap(); +} + +/// Greyed with a reason, and running it is refused. +fn assert_greyed(s: &mut Session, case: &str) { + assert!(!s.is_enabled(ID), "{case}: greyed"); + assert!(s.disabled_reason(ID).is_some_and(|reason| !reason.is_empty()), "{case}: with a reason"); + assert!(s.execute(ID, json!({})).is_err(), "{case}: refused"); +} + +#[test] +fn a_smart_object_a_hidden_shape_and_several_layers_grey_copy_css() { + let mut s = document("rgb", 8); + smart_object(&mut s); + assert_greyed(&mut s, "a smart object"); + + let mut s = document("rgb", 8); + let id = shape(&mut s); + assert!(s.is_enabled(ID), "the shape while it is visible"); + hide(&mut s, id); + assert_greyed(&mut s, "a hidden shape layer"); + + for with_smart_object in [false, true] { + let mut s = document("rgb", 8); + let a = shape(&mut s); + let b = if with_smart_object { smart_object(&mut s) } else { shape(&mut s) }; + s.execute("layer.select", json!({"layer": a})).unwrap(); + s.execute("layer.select", json!({"layer": b, "mode": "add"})).unwrap(); + assert_greyed(&mut s, if with_smart_object { "a shape and a smart object" } else { "two shapes" }); + } +} + +#[test] +fn cmyk_grayscale_lab_and_indexed_documents_grey_copy_css() { + for mode in ["cmyk", "grayscale", "lab"] { + let mut s = document(mode, 8); + shape(&mut s); + assert_greyed(&mut s, mode); + } + let mut s = document("rgb", 8); + assert!(s.is_enabled(ID), "the Background before the mode change"); + s.execute("image.mode.indexedColor", json!({})).unwrap(); + assert_greyed(&mut s, "indexed"); +} + +#[test] +fn one_visible_layer_in_an_rgb_document_keeps_copy_css_enabled() { + for depth in [8, 16, 32] { + let mut s = document("rgb", depth); + assert!(s.is_enabled(ID), "{depth}-bit: the Background"); + for (kind, make) in [("a shape", shape as fn(&mut Session) -> u64), ("point type", point_type), ("a pixel layer", pixels), ("a group", group)] { + make(&mut s); + assert!(s.is_enabled(ID), "{depth}-bit: {kind}"); + } + shape(&mut s); + s.execute("layer.lockLayers", json!({"all": true})).unwrap(); + assert!(s.is_enabled(ID), "{depth}-bit: a shape with Lock All"); + } +} + +/// Not measured, so these stay enabled as they were. +#[test] +fn the_unmeasured_cases_keep_copy_css_enabled() { + for (kind, make) in [("type", point_type as fn(&mut Session) -> u64), ("pixel", pixels), ("group", group)] { + let mut s = document("rgb", 8); + let id = make(&mut s); + hide(&mut s, id); + assert!(s.is_enabled(ID), "a hidden {kind} layer"); + } + let mut s = crate::session(); + s.execute("file.new", json!({"width": 16, "height": 16, "mode": "bitmap", "depth": 1})).unwrap(); + assert!(s.is_enabled(ID), "a Bitmap document"); +}