From 23bdbada815d4c3d9a3b1d016eff480ded25879d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 11:22:47 -0800 Subject: [PATCH] Tools: tool.setOption takes the Eraser's angle and roundness modes and variations The Eraser reports angleMode, roundnessMode, angleVariation and roundnessVariation, but the check before tool.setOption sent every key it did not list to the true-or-false check, so the Freehand dialog's OK for the Eraser failed. sizeMode was also limited to fixed and pressure for the Eraser, which takes every mode. The gesture tools' check now takes each mode (any of the six for the Eraser, fixed or pressure for the Blob Brush's size), the angle variation from 0 to 180 and the roundness variation from 0 to 100, and names its true-or-false options instead of defaulting to them. A new test checks that every tool in the catalog takes back the options it reports, apart from the state it reports and does not take. --- crates/tools/src/draw2/gesture.rs | 10 ++++++-- crates/tools/src/draw2/gesture/tests.rs | 14 +++++++++++ crates/tools/src/option_check.rs | 33 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/crates/tools/src/draw2/gesture.rs b/crates/tools/src/draw2/gesture.rs index 7276311076..0b2be462b8 100644 --- a/crates/tools/src/draw2/gesture.rs +++ b/crates/tools/src/draw2/gesture.rs @@ -1841,8 +1841,14 @@ impl Tool for GestureTool { "angle" => c::number(key, v, -180.0, 180.0), "roundness" => c::number(key, v, if self.id == "blobBrush" { 0.0 } else { 1.0 }, 100.0), "editWithin" | "closeWithin" => c::number(key, v, 0.0, 100.0), - "sizeMode" => c::one_of(key, v, &["fixed", "pressure"]), - _ => c::boolean(key, v), + "angleVariation" => c::number(key, v, 0.0, 180.0), + "roundnessVariation" => c::number(key, v, 0.0, 100.0), + // The Blob Brush's Size is Fixed or Pressure; the Eraser's Angle, Roundness and Size + // take any mode (`set_option`). + "sizeMode" if self.id != "eraser" => c::one_of(key, v, &[Variation::Fixed.id(), Variation::Pressure.id()]), + "sizeMode" | "angleMode" | "roundnessMode" => c::one_of(key, v, &Variation::ALL.map(Variation::id)), + "fill" | "altSmooth" | "livePreview" | "liveFit" | "keepSelected" | "mergeOnlySelection" => c::boolean(key, v), + _ => Err(c::unknown(key)), } } } diff --git a/crates/tools/src/draw2/gesture/tests.rs b/crates/tools/src/draw2/gesture/tests.rs index 9dfbdba2de..b554cfd045 100644 --- a/crates/tools/src/draw2/gesture/tests.rs +++ b/crates/tools/src/draw2/gesture/tests.rs @@ -2822,6 +2822,20 @@ fn the_eraser_tip_follows_its_variation_modes() { [&o["angleMode"], &o["angleVariation"], &o["roundnessMode"], &o["roundnessVariation"], &o["sizeMode"], &o["sizeVariation"]], [&json!("rotation"), &json!(90.0), &json!("random"), &json!(10.0), &json!("tilt"), &json!(8.0)] ); + // `tool.setOption` takes back what the tool reports, and refuses a mode or Variation it does + // not take. + let reported = o.as_object().unwrap().clone(); + assert_eq!(t.check_options(&reported), Ok(())); + for mode in Variation::ALL { + for k in ["sizeMode", "angleMode", "roundnessMode"] { + assert_eq!(t.check_option(k, &json!(mode.id())), Ok(()), "{k} {mode:?}"); + } + } + let modes = "must be one of `fixed`, `random`, `pressure`, `tilt`, `bearing`, `rotation`"; + assert_eq!(t.check_option("angleMode", &json!("wheel")), Err(format!("`angleMode` {modes}"))); + assert_eq!(t.check_option("angleVariation", &json!(181)), Err("`angleVariation` must be a number from 0 to 180".into())); + assert_eq!(t.check_option("roundnessVariation", &json!(500)), Err("`roundnessVariation` must be a number from 0 to 100".into())); + assert_eq!(GestureTool::new("blobBrush").check_option("sizeMode", &json!("tilt")), Err("`sizeMode` must be one of `fixed`, `pressure`".into())); // A mode it does not have leaves the mode as it was; a Variation is kept within its range. t.set_option("angleMode", &json!("wheel")); t.set_option("roundnessVariation", &json!(500)); diff --git a/crates/tools/src/option_check.rs b/crates/tools/src/option_check.rs index 8507a82184..8389c6c297 100644 --- a/crates/tools/src/option_check.rs +++ b/crates/tools/src/option_check.rs @@ -159,6 +159,39 @@ mod tests { assert!(string_or_null("prev", &json!(3)).is_err()); } + /// Every tool takes back the options it reports, each on its own and all together: a dialog's + /// OK sends every field the tool showed it. The state a tool reports and doesn't take (the + /// picked mesh points, the text being edited, the pins, the queue of files to place) is left + /// out. + #[test] + fn every_tool_takes_the_options_it_reports() { + const TYPE: &[&str] = &["type", "areaType", "typeOnPath", "verticalType", "verticalAreaType", "verticalTypeOnPath"]; + const READ_ONLY: &[(&[&str], &[&str])] = &[ + (&["directSelection", "groupSelection", "lasso", "mesh", "magicWand"], &["meshPoints"]), + (TYPE, &["editing"]), + (&["puppetWarp"], &["pins", "selected"]), + // With nothing queued, `current` (0) is no file's index either. + (&["place"], &["count", "current", "name", "width", "height", "names"]), + ]; + let read_only = |id: &str, k: &str| READ_ONLY.iter().any(|(ids, keys)| ids.contains(&id) && keys.contains(&k)); + let extra = ["place", "dimension"]; + let mut refused = Vec::new(); + for id in crate::catalog::all_tools().map(|t| t.id).chain(extra) { + let tool = crate::create(id); + let Some(mut options) = tool.options().as_object().cloned() else { continue }; + options.retain(|k, _| !read_only(id, k)); + for (k, v) in &options { + if let Err(e) = tool.check_option(k, v) { + refused.push(format!("tool `{id}`, option `{k}` = {v}: {e}")); + } + } + if let Err(e) = check_values(tool.as_ref(), &options) { + refused.push(e); + } + } + assert!(refused.is_empty(), "{}", refused.join("\n")); + } + #[test] fn set_option_params_reads_the_tool_and_the_options() { let p = json!({"tool": "smooth", "key": "fidelity", "value": 2, "values": {"x": 1}});