PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Layer styles: editing effects no longer re-enables a disabled effect list (#3648) (#3799)
max6a766345d37b
diff --git a/crates/engine/src/layer_style.rs b/crates/engine/src/layer_style.rsindex 5f7b8f0ee..fa6f5be2e 100644--- a/crates/engine/src/layer_style.rs+++ b/crates/engine/src/layer_style.rs@@ -895,8 +895,10 @@ fn replace_effects(s: &mut Session, p: &Value) -> Result<Value> {}let mode = doc.mode;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+ // with no effects yet still has the default `enabled: true`, so its style shows.l.effects.items = list.into_iter().map(|e| e.in_mode(mode)).collect();- l.effects.enabled = true;Ok(())})?;Ok(json!({ "layer": id.0, "effects": count }))@@ -915,7 +917,8 @@ 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);let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;- l.effects.enabled = true;+ // 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.match l.effects.items.iter_mut().find(|e| same_kind(e, &fx)) {Some(slot) if !add => *slot = fx,_ => l.effects.items.push(fx),@@ -1197,6 +1200,62 @@ mod tests {assert_eq!(effects(&s).len(), 1);}+ fn pixel(s: &mut Session) -> Vec<f32> {+ serde_json::from_value(s.execute("document.pixel", json!({"x": 3, "y": 3})).unwrap()).unwrap()+ }++ fn list_enabled(s: &Session) -> bool {+ let d = s.active().unwrap();+ d.doc.layer(d.active_layer.unwrap()).unwrap().effects.enabled+ }++ /// #3648: the Layers panel's "Effects" eye (`layer.setEffectsVisible`) must survive a Layer+ /// Style dialog OK: the dialog's whole-list replace re-creates the items but leaves the+ /// list's master switch alone.+ #[test]+ fn replacing_the_list_keeps_a_disabled_effect_list_disabled() {+ let mut s = session();+ s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();+ s.execute("layer.layerStyle.colorOverlay", json!({"color": "#0000ff"})).unwrap();+ let shown = pixel(&mut s);+ s.execute("layer.setEffectsVisible", json!({"visible": false})).unwrap();+ let hidden = pixel(&mut s);+ assert_ne!(hidden, shown, "the eye really hides the style");+ let r = s.execute("layer.layerStyle.replace", json!({"effects": [{"kind": "colorOverlay", "params": {"color": "#00ff00"}}]})).unwrap();+ assert_eq!(r["effects"], 1, "the list itself is updated");+ assert!(!list_enabled(&s), "the whole-list switch survives the edit");+ assert!(effects(&s)[0].enabled(), "the effect's own state is updated");+ assert_eq!(pixel(&mut s), hidden, "a disabled list still draws nothing");+ s.execute("layer.setEffectsVisible", json!({"visible": true})).unwrap();+ assert_ne!(pixel(&mut s), hidden, "re-showing the list draws the edited effect");+ }++ /// #3648: adding or replacing one effect (`layer.layerStyle.<kind>`) also leaves the master+ /// switch as the user left it.+ #[test]+ fn set_effect_keeps_a_disabled_effect_list_disabled() {+ let mut s = session();+ s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();+ s.execute("layer.layerStyle.colorOverlay", json!({"color": "#0000ff"})).unwrap();+ s.execute("layer.setEffectsVisible", json!({"visible": false})).unwrap();+ let hidden = pixel(&mut s);+ s.execute("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"})).unwrap();+ assert!(!list_enabled(&s), "the whole-list switch survives the edit");+ assert!(effects(&s)[0].enabled(), "the effect's own state is updated");+ assert_eq!(pixel(&mut s), hidden, "a disabled list still draws nothing");+ }++ /// A layer that never had effects keeps the default: its first effect shows.+ #[test]+ fn a_first_effect_shows_on_a_layer_that_never_had_effects() {+ let mut s = session();+ s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();+ let plain = pixel(&mut s);+ s.execute("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"})).unwrap();+ assert!(list_enabled(&s), "a fresh effects list defaults to enabled");+ assert_ne!(pixel(&mut s), plain, "the first effect renders");+ }+/// #3102: an effect row dropped on the Delete button deletes only that effect, in one step.#[test]fn clear_one_effect_by_index() {diff --git a/crates/ui-egui/src/layer_style.rs b/crates/ui-egui/src/layer_style.rsindex af3e36c90..52e53963d 100644--- a/crates/ui-egui/src/layer_style.rs+++ b/crates/ui-egui/src/layer_style.rs@@ -1990,6 +1990,56 @@ mod tests {assert_eq!(color(&d.doc), photocraft_color::Color::rgb(0.0, 1.0, 0.0), "OK commits with Preview off");}+ /// #3648's steps in one dialog session: disable the stroke row, add a Color Overlay,+ /// untick Preview. Preview off shows the document as saved — the stroke was never+ /// disabled in it, so it keeps rendering until OK, exactly like Photoshop's preview+ /// semantics; nothing disabled resurrects, and OK applies the dialog's state.+ #[test]+ fn issue_steps_disable_stroke_add_overlay_untick_preview() {+ let mut s = session();+ s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();+ // Steps 1-3: a stroke is enabled and OK'd; reopening the dialog loads it enabled.+ s.execute("layer.layerStyle.stroke", json!({"size": 40, "position": "inside", "color": "#00ff00"})).unwrap();+ let st = s.active().unwrap();+ let id = st.active_layer.unwrap();+ let mut f = initial_fields(st.doc.layer(id).unwrap(), None, st.doc.global_light.angle);+ let rows = f["effects"].as_array().unwrap();+ assert_eq!(rows.len(), 1, "the saved stroke is the only row");+ assert_eq!((rows[0]["kind"].as_str(), rows[0]["on"].as_bool()), (Some("stroke"), Some(true)));+ // Step 4: disable the stroke row — a pending edit, the document is untouched.+ f["effects"][0]["on"] = json!(false);+ // Step 5: add a Color Overlay (a new instance: no fx snapshot, factory defaults).+ f["effects"].as_array_mut().unwrap().push(json!({+ "id": "fx2",+ "kind": "colorOverlay",+ "on": true,+ "params": {"color": "#0000ff"},+ }));+ // While Preview is on, the live preview honours the in-dialog disable: the stroke+ // is disabled in the scratch document, the overlay renders.+ let shown = preview_document(&st.doc, &s.patterns, &f).unwrap();+ let mut shown_effects = shown.layer(id).unwrap().effects.items.iter();+ assert!(!shown_effects.find(|e| kind_of(e) == "stroke").is_none_or(|e| e.enabled()), "the preview leaves the stroke off");+ assert!(shown_effects.find(|e| kind_of(e) == "colorOverlay").is_some_and(|e| e.enabled()), "the preview shows the overlay");+ // Step 6: untick Preview — the canvas shows the real document, whose stroke is+ // still enabled (the disable is an unapplied edit). This is the state the issue+ // reports; it is the saved state, not a resurrected effect.+ f.insert("preview".into(), json!(false));+ let green: Vec<f32> = serde_json::from_value(s.execute("document.pixel", json!({"x": 32, "y": 32})).unwrap()).unwrap();+ assert!(green[1] > 0.9 && green[0] < 0.1, "preview off shows the saved stroke: {green:?}");+ // OK (with Preview off) applies the dialog state: the stroke is now disabled in the+ // document too, the overlay shows, and the dialog — which never models the list's+ // master switch — leaves it at its default.+ apply(&f, |cmd, p| s.execute(cmd, p).map_err(|e| e.to_string())).unwrap();+ let d = s.active().unwrap();+ let effects = &d.doc.layer(id).unwrap().effects;+ assert!(!effects.items.iter().find(|e| kind_of(e) == "stroke").is_none_or(|e| e.enabled()), "OK disables the stroke");+ assert!(effects.items.iter().find(|e| kind_of(e) == "colorOverlay").is_some_and(|e| e.enabled()), "OK keeps the overlay on");+ assert!(effects.enabled, "the list's master switch keeps its value");+ let blue: Vec<f32> = serde_json::from_value(s.execute("document.pixel", json!({"x": 32, "y": 32})).unwrap()).unwrap();+ assert!(blue[2] > 0.9 && blue[1] < 0.1, "after OK only the overlay renders: {blue:?}");+ }+#[test]fn new_style_saves_pending_state_and_a_swatch_apply_restores_it() {let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());