diff --git a/crates/engine/src/cmd/paint.rs b/crates/engine/src/cmd/paint.rs index 7f4316ac80..a3fec4a9e6 100644 --- a/crates/engine/src/cmd/paint.rs +++ b/crates/engine/src/cmd/paint.rs @@ -702,8 +702,8 @@ fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option, opacity // The type objects whose characters took the paint (no row targeted): only those change what a graph's labels keep // for their characters; a row edit is kept with the label's rows. let characters = std::cell::RefCell::new(Vec::new()); - // Whether a target took the opacity, and whether one would have given it to its characters. - let (took_opacity, characters_skipped) = (std::cell::Cell::new(false), std::cell::Cell::new(false)); + // The targets that took the opacity, and whether one would have given it to its characters. + let (took_opacity, characters_skipped) = (std::cell::RefCell::new(std::collections::BTreeSet::new()), std::cell::Cell::new(false)); let paint_one = |n: &mut Node, index: Option, paint: &Paint| -> Result<()> { if index.is_none() && matches!(n.kind, NodeKind::Text(_)) { characters.borrow_mut().push(n.id); @@ -779,7 +779,7 @@ fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option, opacity for i in points { if let Some(q) = m.points.get_mut(*i) { q.opacity = o; - took_opacity.set(true); + took_opacity.borrow_mut().insert(n.id); } } return; @@ -788,7 +788,7 @@ fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option, opacity if fill { n.appearance.fill_at_mut(index).map(|f| &mut f.opacity) } else { n.appearance.stroke_at_mut(index).map(|s| &mut s.opacity) }; if let Some(item) = item { *item = o; - took_opacity.set(true); + took_opacity.borrow_mut().insert(n.id); } }; // The objects counted: with a design's art, those with a row equal to the art's. @@ -811,7 +811,7 @@ fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option, opacity }, |before, d, changed| { // An opacity that no target took is an error, which also takes back the paint given with it. - if opacity.is_some() && !took_opacity.get() { + if opacity.is_some() && took_opacity.borrow().is_empty() { if characters_skipped.get() { return Err(characters_have_no_opacity(cmd)); } @@ -844,7 +844,9 @@ fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option, opacity paint_new_art(s, paint, fill); s.remember_paint(paint); } - Ok(json!({"objects": ids.iter().collect::>().len()})) + // An opacity alone counts only the targets that took it. + let objects = if paint.is_none() { took_opacity.borrow().len() } else { ids.iter().collect::>().len() }; + Ok(json!({"objects": objects})) } /// The instance whose object is selected with Direct Selection, and that object's path diff --git a/crates/engine/src/tests/paintopacity.rs b/crates/engine/src/tests/paintopacity.rs index 380ab6fd6f..2a8b753366 100644 --- a/crates/engine/src/tests/paintopacity.rs +++ b/crates/engine/src/tests/paintopacity.rs @@ -298,7 +298,8 @@ fn type_beside_other_art_is_left_alone() { }; let before = (node(&s, t), runs(&s)); select(&mut s, &[r, t]); - s.execute("paint.setFill", &json!({"opacity": 40})).unwrap(); + let out = s.execute("paint.setFill", &json!({"opacity": 40})).unwrap(); + assert_eq!(out["objects"], 1, "only the rectangle took the opacity"); assert!((fill_opacity(&s, r) - 0.4).abs() < 1e-6); assert_eq!((node(&s, t), runs(&s)), before); select(&mut s, &[t]); @@ -348,7 +349,8 @@ fn an_opacity_no_target_takes_is_an_error() { s.execute("paint.setFill", &json!({"opacity": 70})).unwrap(); select(&mut s, &[a, b]); assert_eq!(proxies(&mut s)["fillOpacity"], 70.0, "the first selected object with a fill item answers"); - s.execute("paint.setFill", &json!({"opacity": 20})).unwrap(); + let out = s.execute("paint.setFill", &json!({"opacity": 20})).unwrap(); + assert_eq!(out["objects"], 1, "the object without a fill item is not counted"); assert!((fill_opacity(&s, b) - 0.2).abs() < 1e-6); assert!(node(&s, a).appearance.fill().is_none()); } diff --git a/docs/mcp.md b/docs/mcp.md index 986cde2002..5a3d6d54e1 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -841,7 +841,7 @@ its kind (`paint.proxies`) and the Gradient tool's annotator then show that row. `inspect_document` reports it as `paint.appearanceItem`. `paint.setFill` and `paint.setStroke` also take `opacity` (0 to 100, a percentage as in `transparency.set`), which sets the opacity of the fill or stroke row they paint, with or without a new paint, as one undo step. An opacity that no target takes is an error and also leaves the paint -undone. `paint.proxies` answers it as `fillOpacity` and `strokeOpacity`, from the first selected object that has +undone. Without a new paint, `objects` counts only the targets that took the opacity. `paint.proxies` answers it as `fillOpacity` and `strokeOpacity`, from the first selected object that has that row (a selected path of a compound path answers with its compound path's row, which the commands edit). It answers null with nothing selected, for an object selected in a symbol instance, while the Type tool has characters selected, and when only type characters (which have no opacity of their own) or objects without that row are selected. Live effects take the same `item` to apply to one fill or