Actions: a recorded Fade step without settings plays as 100 % Normal (#13816)

A Fade step with no Opct or Md key now maps to edit.fade with the missing
setting at its fixed default (100 %, Normal) instead of becoming an
unsupported step. A recorded opacity is rounded to a whole percent and,
outside 0..=100, wraps modulo 100 (150 plays as 50, -5 as 95) rather than
clamping. Recording still writes both keys.

Fixes #13816
This commit is contained in:
RickiNano committed 2026-11-13 16:26:49 +01:00
1 parent e56668ee40
commit eedfccfc23
4 files changed
+118 -11

No files matched your search

+3
View File
@@ -214,3 +214,6 @@ mod recorded_tools_tests;
#[cfg(test)]
mod artboard_section_tests;
#[cfg(test)]
mod fade_step_tests;
@@ -0,0 +1,85 @@
use super::*;
/// One action with a single recorded Fade step.
fn fade_file(descriptor: Descriptor) -> Vec<u8> {
atn::write(&AtnFile {
name: UnicodeString::new_nul("Fade"),
actions: vec![AtnAction {
name: UnicodeString::new_nul("Step"),
items: vec![AtnItem { event: AtnEvent::Code(*b"Fade"), dictionary: b"Fade".to_vec(), descriptor: Some(descriptor), ..Default::default() }],
..Default::default()
}],
..Default::default()
})
.unwrap()
}
/// The command, opacity and mode a recorded Fade step imports as.
fn imported(descriptor: Descriptor) -> (String, f64, String) {
let loaded = import(&fade_file(descriptor)).unwrap();
assert!(loaded.unsupported.is_empty());
let (id, params) = &loaded.actions[0].steps[0];
(id.clone(), params["opacity"].as_f64().unwrap(), params["mode"].as_str().unwrap().to_owned())
}
/// An 8 x 8 image, inverted so there is an edit to fade.
fn inverted(depth: u8) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width":8,"height":8,"depth":depth,"background":"transparent"})).unwrap();
s.execute("edit.fill", json!({"contents":"color","color":"#4080c0"})).unwrap();
s.execute("image.adjustments.invert", json!({})).unwrap();
s
}
/// The pixel a recorded Fade step leaves after the Invert it fades.
fn played(depth: u8, descriptor: Descriptor) -> Value {
let mut s = inverted(depth);
let loaded = s.execute("actions.load", json!({"data":photocraft_paint::tile::b64_encode(&fade_file(descriptor))})).unwrap();
assert_eq!(loaded["unsupported"], json!([]));
let result = s.execute("actions.play", json!({"action":loaded["actions"][0]})).unwrap();
assert!(result.get("failed").is_none(), "{result}");
s.execute("document.pixel", json!({"x":3,"y":3})).unwrap()
}
/// The pixel `edit.fade` leaves when run directly with these params.
fn faded(depth: u8, params: Value) -> Value {
let mut s = inverted(depth);
s.execute("edit.fade", params).unwrap();
s.execute("document.pixel", json!({"x":3,"y":3})).unwrap()
}
/// A step with no keys plays at 100 % Normal; with one key, the other takes its default.
#[test]
fn fade_step_missing_keys_take_fixed_defaults() {
assert_eq!(imported(desc("null")), ("edit.fade".into(), 100.0, "Normal".into()));
assert_eq!(imported(desc("null").with("Opct", pct(40.0))), ("edit.fade".into(), 40.0, "Normal".into()));
assert_eq!(imported(desc("null").with("Md ", enm("BlnM", "Mltp"))), ("edit.fade".into(), 100.0, "Multiply".into()));
for depth in [8, 16, 32] {
assert_eq!(played(depth, desc("null")), faded(depth, json!({"opacity":100,"mode":"normal"})));
assert_eq!(played(depth, desc("null").with("Md ", enm("BlnM", "Mltp"))), faded(depth, json!({"opacity":100,"mode":"multiply"})));
}
}
/// Out-of-range opacity wraps modulo 100 and fractions round; whole values in range are unchanged.
#[test]
fn fade_step_opacity_wraps_and_rounds() {
for (recorded, expected) in [(150.0, 50.0), (-5.0, 95.0), (50.5, 51.0), (0.0, 0.0), (37.0, 37.0), (100.0, 100.0), (-0.4, 0.0)] {
let (id, opacity, _) = imported(desc("null").with("Opct", pct(recorded)).with("Md ", enm("BlnM", "Nrml")));
assert_eq!(id, "edit.fade");
assert_eq!(opacity, expected, "{recorded}");
assert!(opacity.is_sign_positive(), "{recorded}");
}
for depth in [8, 16, 32] {
let recorded = desc("null").with("Opct", pct(150.0)).with("Md ", enm("BlnM", "Nrml"));
assert_eq!(played(depth, recorded), faded(depth, json!({"opacity":50,"mode":"normal"})));
}
}
/// Recording still writes both keys, so a step imported without them saves them explicitly.
#[test]
fn fade_step_saves_both_keys() {
let loaded = import(&fade_file(desc("null"))).unwrap();
let saved = export("Fade", &[&loaded.actions[0]], &[], false).unwrap().0;
let d = atn::parse(&saved).unwrap().actions[0].items[0].descriptor.clone().unwrap();
assert_eq!(d, desc("null").with("Opct", pct(100.0)).with("Md ", enm("BlnM", "Nrml")));
}
@@ -56,7 +56,7 @@ fn fixtures() -> Vec<(&'static str, Option<&'static str>, Descriptor)> {
ellipse.items.last_mut().unwrap().1 = D::Boolean(false);
cases.push(("AddT", Some("addTo"), ellipse));
for mode in ["Scrn", "H", "linearDodge"] {
cases.push(("Fade", Some("fade"), desc("null").with("Opct", pct(25.5)).with("Md ", enm("BlnM", mode))));
cases.push(("Fade", Some("fade"), desc("null").with("Opct", pct(25.0)).with("Md ", enm("BlnM", mode))));
}
for edge in ["AdCH", "AdRg", "AdTp", "AdCV", "AdBt"] {
cases.push(("Dstr", Some("distribute"), desc("null").with("null", target_ref("Lyr ")).with("Usng", enm("ADSt", edge))));
@@ -262,12 +262,12 @@ fn noncanonical_group_f_enums_keep_their_bytes_and_flags() {
}
}
/// Fade writes native defaults and clamps only its documented opacity range.
/// Fade writes native defaults; a recorded opacity outside 0..=100 wraps modulo 100 on import.
#[test]
fn fade_defaults_and_opacity_limits_are_explicit() {
let native = export_item("edit.fade", &json!({}), "S").unwrap().descriptor.unwrap();
assert_eq!(native, desc("null").with("Opct", pct(100.0)).with("Md ", enm("BlnM", "Nrml")));
for (opacity, expected) in [(-5.0, 0.0), (0.0, 0.0), (100.0, 100.0), (105.0, 100.0)] {
for (opacity, expected) in [(-5.0, 95.0), (0.0, 0.0), (100.0, 100.0), (105.0, 5.0)] {
let bytes = file(AtnEvent::Code(*b"Fade"), desc("null").with("Opct", pct(opacity)).with("Md ", enm("BlnM", "Nrml")));
let loaded = import(&bytes).unwrap();
assert!(loaded.unsupported.is_empty());
@@ -302,7 +302,6 @@ fn group_f_unsupported_settings_are_not_silently_replaced() {
(*b"AddT", wrong_ref),
(*b"PstI", desc("null").with("AntA", enm("Annt", "Unknown")).with("As ", D::Class(class("Pxel")))),
(*b"PstO", desc("null").with("AntA", enm("Annt", "Anno")).with("As ", D::Class(class("Other")))),
(*b"Fade", desc("null").with("Opct", pct(50.0))),
(*b"Fade", desc("null").with("Opct", px(50.0)).with("Md ", enm("BlnM", "Nrml"))),
(*b"Dstr", desc("null").with("null", target_ref("Lyr ")).with("Usng", enm("ADSt", "Unknown"))),
(*b"RmvL", desc("null").with("apply", D::Boolean(true))),
@@ -151,22 +151,42 @@ pub(super) fn paste_to(_: &str, p: &Value) -> Option<Descriptor> {
Some(desc("null").with("AntA", enm("Annt", "Anno")).with("As ", D::Class(class("Pxel"))))
}
/// Fade requires the settings recorded after the edit it fades, not guessed factory values.
/// Fade plays the recorded settings; a missing key takes the reference application's fixed default
/// (100 % opacity, Normal mode), not the dialog's last values. Opacity is rounded to a whole percent.
pub(super) fn fade_from(d: &Descriptor) -> Mapped {
let mapped = || {
if !keys(d, &["Opct", "Md "]) {
return None;
}
let mode = blend_label(&enumeration(d, "Md ", "BlnM")?)?;
// An accepted alias would be rewritten to the exporter's preferred blend identifier.
if d.get("Md ") != Some(&enm("BlnM", blend_ps(mode)?)) {
return None;
}
Some(json!({"opacity":unit(d,"Opct",b"#Prc")?.clamp(0.0,100.0),"mode":mode}))
let mode = match d.get("Md ") {
None => "Normal",
Some(recorded) => {
let mode = blend_label(&enumeration(d, "Md ", "BlnM")?)?;
// An accepted alias would be rewritten to the exporter's preferred blend identifier.
if *recorded != enm("BlnM", blend_ps(mode)?) {
return None;
}
mode
}
};
let opacity = match d.get("Opct") {
None => 100.0,
Some(_) => fade_opacity(unit(d, "Opct", b"#Prc")?),
};
Some(json!({"opacity":opacity,"mode":mode}))
};
mapped().map_or(Mapped::Unsupported { harmless: false }, |p| step("edit.fade", p))
}
/// A recorded Fade opacity outside 0..=100 wraps modulo 100 (150 plays as 50, -5 as 95), as
/// measured in the reference application, rather than clamping.
fn fade_opacity(recorded: f64) -> f64 {
let rounded = recorded.round();
let wrapped = if (0.0..=100.0).contains(&rounded) { rounded } else { rounded.rem_euclid(100.0) };
// -0.4 rounds, and -200 wraps, to -0.0; adding zero stores it as 0.
wrapped + 0.0
}
/// Native omitted Fade settings retain the command's 100% Normal behavior.
pub(super) fn fade_to(_: &str, p: &Value) -> Option<Descriptor> {
if !params(p, &["opacity", "mode"]) {