Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #275: fix(engine): object.opacity accepts its documented lowercase blend modes

aiongg0f2dbd2fcd24
diff --cc crates/engine/src/cmd/object.rs
index b42ddc38d,86fc85673..c03d9ffa9
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@@ -2464,52 -2433,65 +2474,116 @@@ mod named_target_tests
}
#[cfg(test)]
+mod column_rule_tests {
+ use super::*;
+
+ #[test]
+ fn column_rules_set_validate_undo_and_inspect() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [36, 36, 300, 300], "content": "text", "text": "Hello"})).unwrap();
+ let id = ItemId(r["id"].as_u64().unwrap());
+ let opts = |s: &Session| s.doc().unwrap().doc.item(id).unwrap().text_frame().unwrap().options.clone();
+ s.execute("object.textFrameOptions", &json!({"ids": [id.0], "columns": 3, "inset": [1, 2, 3, 4]})).unwrap();
+ let before = opts(&s);
+ // An unknown swatch or a bad weight is an error and changes nothing.
+ let e = s.execute("object.textFrameOptions", &json!({"ids": [id.0], "columnRule": true, "columnRuleColor": "Mauve"})).unwrap_err();
+ assert!(e.to_string().contains("Mauve"), "{e}");
+ assert!(s.execute("object.textFrameOptions", &json!({"ids": [id.0], "columnRuleWeight": -1})).is_err());
+ assert!(s.execute("object.textFrameOptions", &json!({"ids": [id.0], "columnRuleTint": 2})).is_err());
+ assert!(s.execute("object.textFrameOptions", &json!({"ids": [id.0], "columnRuleTopInset": "x"})).is_err());
+ assert_eq!(opts(&s), before);
+ s.execute(
+ "object.textFrameOptions",
+ &json!({"ids": [id.0], "columnRule": true, "columnRuleWeight": 0.5, "columnRuleColor": "C=100 M=0 Y=0 K=0", "columnRuleTint": 0.5,
+ "columnRuleOffset": -2, "columnRuleTopInset": 4, "columnRuleBottomInset": 6}),
+ )
+ .unwrap();
+ let o = opts(&s);
+ assert!(o.column_rule && o.column_rule_weight == 0.5 && o.column_rule_color == "C=100 M=0 Y=0 K=0");
+ assert_eq!(o.inset, [1.0, 2.0, 3.0, 4.0], "untouched options stay");
+ assert_eq!((o.column_rule_tint, o.column_rule_offset, o.column_rule_top_inset, o.column_rule_bottom_inset), (0.5, -2.0, 4.0, 6.0));
+ let item = s.execute("document.inspect", &json!({})).unwrap()["spreads"][0]["items"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .find(|i| i["id"] == json!(id.0))
+ .cloned()
+ .unwrap();
+ assert_eq!(
+ (&item["columnRule"], &item["columnRuleWeight"], &item["columnRuleColor"]),
+ (&json!(true), &json!(0.5), &json!("C=100 M=0 Y=0 K=0"))
+ );
+ assert_eq!(
+ (&item["columnRuleTint"], &item["columnRuleOffset"], &item["columnRuleTopInset"], &item["columnRuleBottomInset"]),
+ (&json!(0.5), &json!(-2.0), &json!(4.0), &json!(6.0))
+ );
+ // One undo step.
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert_eq!(opts(&s), before);
+ }
+}
++
++#[cfg(test)]
+ mod blend_mode_tests {
+ use designcraft_color::BlendMode;
+
+ use super::*;
+
+ /// `object.opacity` documents `blend?: normal|multiply|…`, but deserializing the value
+ /// against the enum only accepted the PascalCase variant names, so the documented
+ /// lowercase forms were silently dropped: the command reported `changed: 1` while the
+ /// stored blend stayed as it was (#260).
+ #[test]
+ fn lowercase_documented_blend_modes_apply() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [36, 36, 300, 300], "content": "unassigned"})).unwrap();
+ let id = ItemId(r["id"].as_u64().unwrap());
+ let blend = |s: &Session| s.doc().unwrap().doc.item(id).map(|i| i.blend);
+
+ for (given, want) in [
+ ("multiply", BlendMode::Multiply),
+ ("screen", BlendMode::Screen),
+ ("MULTIPLY", BlendMode::Multiply),
+ ("Color Burn", BlendMode::ColorBurn),
+ ("color-burn", BlendMode::ColorBurn),
+ ("soft_light", BlendMode::SoftLight),
+ ("normal", BlendMode::Normal),
+ ] {
+ let out = s.execute("object.opacity", &json!({"opacity": 1, "blend": given})).unwrap();
+ assert_eq!(out["changed"], 1, "{given}");
+ assert_eq!(blend(&s), Some(want), "blend mode {given:?} must be applied");
+ }
+ }
+
+ /// The PascalCase names keep working, so the Properties panel's own submissions are
+ /// unaffected.
+ #[test]
+ fn pascal_case_blend_modes_still_apply() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [36, 36, 300, 300], "content": "unassigned"})).unwrap();
+ let id = ItemId(r["id"].as_u64().unwrap());
+ s.execute("object.opacity", &json!({"opacity": 0.5, "blend": "Multiply"})).unwrap();
+ let it = s.doc().unwrap().doc.item(id).cloned().unwrap();
+ assert_eq!(it.blend, BlendMode::Multiply);
+ assert!((it.opacity - 0.5).abs() < 1e-6, "opacity must still be set");
+ }
+
+ /// A blend mode the command doesn't know is an error, not a silent success that leaves
+ /// the previous appearance in place.
+ #[test]
+ fn an_unknown_blend_mode_is_an_error() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [36, 36, 300, 300], "content": "unassigned"})).unwrap();
+ let id = ItemId(r["id"].as_u64().unwrap());
+ s.execute("object.opacity", &json!({"opacity": 1, "blend": "Multiply"})).unwrap();
+ let e = s.execute("object.opacity", &json!({"opacity": 1, "blend": "sparkle"})).unwrap_err().to_string();
+ assert!(e.contains("sparkle") && e.contains("blend"), "{e}");
+ let it = s.doc().unwrap().doc.item(id).cloned().unwrap();
+ assert_eq!(it.blend, BlendMode::Multiply, "a rejected value changes nothing");
+ assert_eq!(it.opacity, 1.0);
+ }
+ }