Tools: tool.setOption refuses non-object parameters and keeps its target tool

Parameters that are not an object (true, 7, [], "x") are an error
instead of an empty read; no parameters (null) still read the options.
The tool an option is checked against is named before the edit in
progress is finished, so a held Cmd press made by finishing it no longer
sends the value to the selection tool it lends.
This commit is contained in:
dotCooCoo
2026-11-14 12:27:34 -08:00
parent a315b7ef6b
commit 72082aee46
5 changed files with 47 additions and 12 deletions
+20
View File
@@ -1049,6 +1049,26 @@ fn shape_builder_drops_a_press_held_during_a_merge() {
}
}
/// `tool.setOption` without a tool, while a Cmd press is held during a merge, sets the Shape
/// Builder's option: finishing the merge makes the press, which lends the selection tool, and the
/// value still goes to the Shape Builder it was checked against.
#[test]
fn shape_builder_option_set_while_a_cmd_press_is_held_goes_to_the_shape_builder() {
let v = ViewInfo::default();
let mut s = session();
s.select_tool("shapeBuilder", v).unwrap();
s.set_tool_option_cmd(&json!({"key": "pickColorFrom", "value": "swatches"})).unwrap();
square_row(&mut s);
click_row(&mut s, v);
s.pointer(&PointerEvent::new(PointerKind::Down, 305.0, 10.0).with_mods(Mods { cmd: true, ..Default::default() }), v).unwrap();
assert!(s.tool_editing(), "the press is held");
let out = s.set_tool_option_cmd(&json!({"key": "pickColorFrom", "value": "artwork"})).unwrap();
assert_eq!(s.tool_id(), "selection", "the held press lent the selection tool");
assert_eq!(s.lender.as_ref().map(|t| t.id()), Some("shapeBuilder"));
assert_eq!(out["pickColorFrom"], json!("artwork"));
assert_eq!(s.lender.as_ref().map(|t| t.options()["pickColorFrom"].clone()), Some(json!("artwork")));
}
/// A new revision of the document made outside a command while a merge is computed on another
/// thread keeps the merge when the selected art is as it was (the next poll applies it, as the
/// command computes it), and drops it when the art changed.
+12 -8
View File
@@ -1319,12 +1319,15 @@ impl Session {
{
return Err(format!("unknown tool `{t}`"));
}
let active = tool.is_none_or(|t| t == self.tool.id());
// The tool is named before the edit in progress is finished: finishing it can make a held
// Cmd press, which lends a selection tool and makes that the active tool.
let id = tool.unwrap_or(self.tool.id());
let active = id == self.tool.id();
if values.is_empty() {
return Ok(self.tool_options_of(tool.unwrap_or(self.tool.id())));
return Ok(self.tool_options_of(id));
}
// Every value is checked before any is set, so an invalid one changes nothing.
vectorcraft_tools::builder::check_builder_options(tool.unwrap_or(self.tool.id()), &values)?;
vectorcraft_tools::builder::check_builder_options(id, &values)?;
// The Place queue is read later, by a click outside any automation scope: its files must
// be readable now, under the roots of whoever sets it.
if let Some(entries) = values.get("queue").and_then(|q| q.get("entries")).and_then(Value::as_array) {
@@ -1332,7 +1335,7 @@ impl Session {
crate::file_access::check_read(path)?;
}
}
if tool.unwrap_or(self.tool_id()) == "dimension" {
if id == "dimension" {
if let Some(style) = values.get("style") {
vectorcraft_doc::dimension::DimensionOptions::from_params(style.clone())?;
}
@@ -1342,15 +1345,16 @@ impl Session {
return Err("Unknown dimension mode".into());
}
}
match tool.filter(|_| !active) {
Some(id) => vectorcraft_tools::option_check::check_values(self.make_tool(id).as_ref(), &values)?,
None => vectorcraft_tools::option_check::check_values(self.tool.as_ref(), &values)?,
if active {
vectorcraft_tools::option_check::check_values(self.tool.as_ref(), &values)?;
} else {
vectorcraft_tools::option_check::check_values(self.make_tool(id).as_ref(), &values)?;
}
// The active tool's merge being computed is applied with the options it was started with.
if active {
self.finish_tool_edit_later().map_err(|e| e.to_string())?;
}
Ok(self.set_tool_options(tool, &values))
Ok(self.set_tool_options(Some(id), &values))
}
/// Let the active tool finish work a command from outside it ended ([`vectorcraft_tools::Tool::after_command`]):
+11 -2
View File
@@ -65,9 +65,12 @@ pub fn string_or_null(key: &str, v: &Value) -> Result<(), String> {
/// What `tool.setOption {tool?, key?, value?, values?}` sets: the tool named (none: the active
/// tool) and the options, `key` and `value` among `values`. A parameter of the wrong type, a
/// `value` without a `key` or a `key` without a `value`, and an option given both as `key` and in
/// `values` are errors. With none of `key` and `values`, the options are empty: the command reads
/// them.
/// `values` are errors, and so are parameters that are not an object. With none of `key` and
/// `values`, or with no parameters (null), the options are empty: the command reads them.
pub fn set_option_params(p: &Value) -> Result<(Option<&str>, Map<String, Value>), String> {
if !p.is_object() && !p.is_null() {
return Err("the parameters must be an object".into());
}
let tool = match p.get("tool") {
None => None,
Some(t) => Some(t.as_str().ok_or("`tool` must be a tool id")?),
@@ -139,6 +142,12 @@ mod tests {
let empty = json!({});
let (tool, values) = set_option_params(&empty).unwrap();
assert!(tool.is_none() && values.is_empty());
// Omitted parameters arrive as null: a read, like `{}`.
let (tool, values) = set_option_params(&Value::Null).unwrap();
assert!(tool.is_none() && values.is_empty());
for bad in [json!(true), json!(7), json!([]), json!("x")] {
assert_eq!(set_option_params(&bad).err(), Some("the parameters must be an object".into()), "{bad}");
}
assert_eq!(set_option_params(&json!({"tool": 3})).err(), Some("`tool` must be a tool id".into()));
assert_eq!(set_option_params(&json!({"values": [1]})).err(), Some("`values` must be an object".into()));
assert_eq!(set_option_params(&json!({"key": 3, "value": 1})).err(), Some("`key` must be a string".into()));
+2 -1
View File
@@ -921,7 +921,8 @@ Halftone… (`engine.execute {command: "ui.menuDialog", params: {command: "objec
Tool options last: `tool.setOption {key, value}` or `{values: {…}}` (and `tool` to set another tool's) sets them, and
the options a tool keeps (Liquify brush and tool options, Mirror & Cut, Puppet Warp, Symbolism, drawing tools) stay
when you switch tools or documents and are saved with the preferences. The Liquify tools share their Global Brush
Dimensions (`width`, `height`, `angle`, `intensity`). `{}` reads the options and sets nothing. Every option given is
Dimensions (`width`, `height`, `angle`, `intensity`). `{}` reads the options and sets nothing; parameters that are
not an object are an error. Every option given is
checked before any is set: an option the tool doesn't have, a value of the wrong type, a number outside the tool's
range or an unknown choice is an error that names the tool and the option
(``tool `smooth`: `fidelity` must be a number from 0.05 to 100``), and no option, preference or undo step changes.
+2 -1
View File
@@ -9815,7 +9815,8 @@ Every option given is checked before any is set. A key that the tool's `check_op
to that tool), a value of the wrong type, a number outside the tool's range, an unknown choice, a `key` without a
`value`, a `values` that is not an object or a `tool` that is not a tool id is an error result that names the tool and
the option, and nothing changes: no option, no saved preference and no undo step, also when another option given with
it is good. `{}` reads the options and sets nothing. The ranges are VectorCraft's own (for example the Smooth tool's
it is good. Parameters that are not an object are an error too. `{}`, or no parameters, reads the options and sets
nothing. The ranges are VectorCraft's own (for example the Smooth tool's
`fidelity` takes 0.05 to 100, the Pencil's 0.01 to 100); a refused value in a Tool Options dialog keeps the dialog
open. The Live Paint tools' `width` and the Shape Builder's `gapLength` are set to the nearest end of their range
when a number outside it is given, as their sections above say.