Treat a % suffix on node.set opacity as a percentage

Found while testing crates/engine/src/cmd/props.rs:476-479 (the "opacity" arm), with crates/engine/src/automation/params.rs:14-20 (f64_of)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-27 11:57:38 +01:00
co-authored by Claude Haiku 5.5
parent 43af145a12
commit 3b78ca9558
2 changed files with 13 additions and 1 deletions
+2 -1
View File
@@ -475,7 +475,8 @@ pub fn apply(cmd: &str, doc: &mut Document, id: NodeId, props: &Map<String, Valu
"locked" => doc.get_mut(id)?.locked = v.as_bool().ok_or_else(|| bad(cmd, "`locked` must be a boolean"))?,
"opacity" => {
let o = f64_of(v).ok_or_else(|| bad(cmd, "`opacity` must be a number"))?;
let o = if o > 1.0 { o / 100.0 } else { o };
let percent = v.as_str().is_some_and(|s| s.trim().ends_with('%'));
let o = if percent || o > 1.0 { o / 100.0 } else { o };
doc.get_mut(id)?.opacity = o.clamp(0.0, 1.0) as f32;
}
"blend" | "blendMode" => {
@@ -475,3 +475,14 @@ fn page_navigation_skips_dividers() {
ex(&mut s, "view.nextPage", json!({}));
assert_eq!(s.page().unwrap(), later, "Next page stops on the last page");
}
#[test]
fn opacity_percent_strings_always_divide_by_100() {
let mut s = Session::new();
let r = rect(&mut s, "R", 0.0, 0.0, None);
for (v, want) in [(json!("1%"), 0.01), (json!("0.5%"), 0.005), (json!("50%"), 0.5), (json!(1), 1.0), (json!(50), 0.5), (json!(0.25), 0.25)] {
ex(&mut s, "node.set", json!({"id": r, "props": {"opacity": v.clone()}}));
let o = s.doc().unwrap().node(r).unwrap().opacity;
assert!((o - want).abs() < 1e-6, "opacity {v} gave {o}, want {want}");
}
}