diff --git a/crates/engine/src/tests/build.rs b/crates/engine/src/tests/build.rs index cb61ac1b9e..545551f13f 100644 --- a/crates/engine/src/tests/build.rs +++ b/crates/engine/src/tests/build.rs @@ -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. diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs index df7ef4b1d8..fcbaf664d0 100644 --- a/crates/engine/src/tooling.rs +++ b/crates/engine/src/tooling.rs @@ -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`]): diff --git a/crates/tools/src/option_check.rs b/crates/tools/src/option_check.rs index 9c16deb9cf..9a45caeb13 100644 --- a/crates/tools/src/option_check.rs +++ b/crates/tools/src/option_check.rs @@ -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> { + 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())); diff --git a/docs/control-protocol.md b/docs/control-protocol.md index a32c3272df..edd0d8f96f 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -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. diff --git a/docs/mcp.md b/docs/mcp.md index 3901375542..0c46157847 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -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.