Tools: tool.setOption takes Crop Image's and the shape tools' newer options

Crop Image's ppi, reference and linked and the shape tools' anchors
reached main after the option checks were written, so tool.setOption
refused them as unknown. Crop Image now checks them with the ranges
check_options uses (the selected image's ppi limit stays in
tool.setOption), and the shape tools take anchors from 2 to 1000 and
refuse other counts, which the Ellipse dialog test now expects.

The test that every tool takes back the options it reports now also
covers the Crop Image and Intertwine tools, which the tool catalog does
not list.
This commit is contained in:
dotCooCoo
2026-11-15 22:01:44 -08:00
parent 144aec7dfc
commit 33ff910392
5 changed files with 35 additions and 5 deletions
+15 -1
View File
@@ -419,8 +419,13 @@ impl Tool for CropImageTool {
};
}
/// `rect` is `[x, y, width, height]`, or null to put the box back where it starts.
/// `rect` is `[x, y, width, height]`, or null to put the box back where it starts. `ppi`,
/// `reference` and `linked` take the values [`check_options`] takes (the selected image's
/// limit on `ppi` is checked by `tool.setOption`, which knows the selection).
fn check_option(&self, key: &str, value: &Value) -> Result<(), String> {
if matches!(key, "ppi" | "reference" | "linked") {
return check_options(&serde_json::Map::from_iter([(key.to_string(), value.clone())]), None);
}
if key != "rect" {
return Err(crate::option_check::unknown(key));
}
@@ -648,6 +653,15 @@ mod tests {
] {
assert_eq!(check_options(&values(v.clone()), Some(36.0)), Err(want.to_string()), "{v}");
}
// The tool takes the same values on its own, without an image's limit.
let t = CropImageTool::default();
for (k, v) in [("ppi", json!(72)), ("ppi", json!(null)), ("reference", json!(0)), ("linked", json!(true)), ("rect", json!(null))] {
assert_eq!(t.check_option(k, &v), Ok(()), "{k}: {v}");
}
assert_eq!(t.check_option("reference", &json!(9)), Err("`reference` must be a whole number from 0 to 8, not 9".into()));
assert_eq!(t.check_option("linked", &json!(1)), Err("`linked` must be true or false, not 1".into()));
assert_eq!(t.check_option("ppi", &json!(2401)), Err("`ppi` must be a number from 1 to 2400 or null, not 2401".into()));
assert_eq!(t.check_option("size", &json!(1)), Err("unknown option `size`".into()));
}
/// An image's own resolution, and the most it can be cropped to.
+2 -1
View File
@@ -174,7 +174,8 @@ mod tests {
(&["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"];
// Tools that `create` makes and the catalog doesn't list.
let extra = ["place", "dimension", crate::cropimage::ID, "intertwine"];
let mut refused = Vec::new();
let mut check = |id: &str, tool: &dyn Tool, after: &str| {
let Some(mut options) = tool.options().as_object().cloned() else { return };
+1
View File
@@ -875,6 +875,7 @@ impl Tool for ShapeTool {
use crate::option_check as c;
match key {
"sides" | "points" => c::whole(key, v, 3, 1000),
"anchors" => c::whole(key, v, 2, 1000),
"cornerRadius" => c::number(key, v, 0.0, MAX_CORNER_RADIUS),
"starRatio" => c::number(key, v, 0.01, 1.0),
"length" => c::number(key, v, 0.0, 1e6),
+12
View File
@@ -1119,3 +1119,15 @@ fn a_move_from_the_centre_point_snaps_and_shows_the_hollow_arrow() {
assert!(matches!(a.last(), Some(Action::Preview(_, v)) if v["matrix"][4] == json!(10.0)), "{a:?}");
assert_eq!(t.cursor(&c, Point::new(160.0, 150.0), Mods::default()), Cursor::Arrow);
}
/// `tool.setOption` takes an ellipse's anchor count from 2 to 1000, as the tool keeps it.
#[test]
fn the_anchor_count_is_checked_against_its_range() {
let t = ShapeTool::new("ellipse");
for n in [2, 4, 1000] {
assert_eq!(t.check_option("anchors", &json!(n)), Ok(()), "{n}");
}
for bad in [json!(1), json!(1001), json!(4.5), json!("4")] {
assert_eq!(t.check_option("anchors", &bad), Err("`anchors` must be a whole number from 2 to 1000".into()), "{bad}");
}
}
+5 -3
View File
@@ -701,9 +701,11 @@ mod tests {
super::super::open_tool_dialog(&mut app, "ellipse", json!({"x": 10.0, "y": 150.0}));
assert_eq!(app.ui.dialog.as_ref().unwrap().f64("anchors", 0.0), 6.0);
app.ui.dialog = None;
// `tool.setOption` keeps it from 2 to 1000, and the dialog opens on what it keeps.
for (set, want) in [(json!(1), 2.0), (json!(2000), 1000.0), (json!(7), 7.0)] {
app.run("tool.setOption", json!({"tool": "ellipse", "values": {"anchors": set}})).unwrap();
// `tool.setOption` takes it from 2 to 1000 and refuses other counts, and the dialog opens on
// what it keeps.
for (set, want) in [(json!(1), 6.0), (json!(2000), 6.0), (json!(2), 2.0), (json!(1000), 1000.0), (json!(7), 7.0)] {
let r = app.run("tool.setOption", json!({"tool": "ellipse", "values": {"anchors": set}}));
assert_eq!(r.is_ok(), set.as_u64().is_some_and(|n| (2..=1000).contains(&n)), "{set}: {r:?}");
super::super::open_tool_dialog(&mut app, "ellipse", json!({"x": 10.0, "y": 150.0}));
assert_eq!(app.ui.dialog.as_ref().unwrap().f64("anchors", 0.0), want, "{set}");
app.ui.dialog = None;