From df95a67ea7879bab3dacf0347883d8976e80c556 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 14:10:16 -0800 Subject: [PATCH] Tools: tool.setOption reads and sets the tool that lent the Selection tool When a held Cmd press lends the Selection tool, `tool.setOption` that names the waiting tool (the Shape Builder) reads its options from that tool instead of making it again from the saved preferences, and a value set on it counts as set since the tool last started afresh. Params that are not an object now give the error the other commands give, "the params must be an object, not {got}", which the message catalogs already translate. The params text of tool.setOption in the MCP catalog, the desktop commands and the headless host says so. --- crates/engine/src/tests/build.rs | 16 ++++++++--- crates/engine/src/tests/toolsettings.rs | 5 ++++ crates/engine/src/tooling.rs | 28 ++++++++++++++----- crates/mcp/src/headless.rs | 2 +- crates/mcp/src/tests/liquify.rs | 15 +++++++++++ crates/mcp/src/ui_catalog.rs | 2 +- crates/tools/src/option_check.rs | 36 ++++++++++++++++++++++--- crates/ui-egui/src/menus/ui_commands.rs | 2 +- docs/control-protocol.md | 5 ++-- docs/mcp.md | 8 +++--- 10 files changed, 96 insertions(+), 23 deletions(-) diff --git a/crates/engine/src/tests/build.rs b/crates/engine/src/tests/build.rs index 545551f13f..cca6de810f 100644 --- a/crates/engine/src/tests/build.rs +++ b/crates/engine/src/tests/build.rs @@ -1049,9 +1049,9 @@ 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. +/// When `tool.setOption` names no tool, the option is set on the tool that was active when the +/// call arrived (the Shape Builder), even when finishing its merge replays a held Cmd press that +/// switches to the Selection tool. #[test] fn shape_builder_option_set_while_a_cmd_press_is_held_goes_to_the_shape_builder() { let v = ViewInfo::default(); @@ -1067,6 +1067,16 @@ fn shape_builder_option_set_while_a_cmd_press_is_held_goes_to_the_shape_builder( 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"))); + // Named while it waits, the Shape Builder is read where it waits, not made afresh from the + // preferences. + s.lender.as_mut().unwrap().set_option("pickColorFrom", &json!("swatches")); + let read = s.set_tool_option_cmd(&json!({"tool": "shapeBuilder"})).unwrap(); + assert_eq!(read["pickColorFrom"], json!("swatches"), "read from the waiting tool"); + // A value set on it counts as set on the tool chosen. + s.tool_set_since_reset = false; + s.set_tool_option_cmd(&json!({"tool": "shapeBuilder", "key": "highlightFill", "value": false})).unwrap(); + assert!(s.tool_set_since_reset(), "the tool chosen was set"); + assert_eq!(s.lender.as_ref().map(|t| t.options()["highlightFill"].clone()), Some(json!(false))); } /// A new revision of the document made outside a command while a merge is computed on another diff --git a/crates/engine/src/tests/toolsettings.rs b/crates/engine/src/tests/toolsettings.rs index eae2a16aa4..a893244021 100644 --- a/crates/engine/src/tests/toolsettings.rs +++ b/crates/engine/src/tests/toolsettings.rs @@ -370,6 +370,11 @@ fn an_empty_set_option_reads_and_saves_nothing() { let saved = s.prefs.tool_settings.clone(); assert_eq!(s.set_tool_option_cmd(&json!({})).unwrap(), s.tool_options()); assert_eq!(s.set_tool_option_cmd(&json!({"tool": "flare"})).unwrap(), s.tool_options_of("flare")); + // No params (null) read too; params that are not an object are an error. + assert_eq!(s.set_tool_option_cmd(&serde_json::Value::Null).unwrap(), s.tool_options()); + for (bad, got) in [(json!(true), "true"), (json!([]), "[]")] { + assert_eq!(s.set_tool_option_cmd(&bad).unwrap_err(), format!("the params must be an object, not {got}")); + } assert_eq!(s.prefs.tool_settings, saved); } diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs index fcbaf664d0..eeb1383c3c 100644 --- a/crates/engine/src/tooling.rs +++ b/crates/engine/src/tooling.rs @@ -1260,7 +1260,14 @@ impl Session { /// The options of tool `id`: the active tool's, or those it would have if chosen now. pub fn tool_options_of(&self, id: &str) -> Value { - if id == self.tool.id() { self.tool.options() } else { self.make_tool(id).options() } + if id == self.tool.id() { + self.tool.options() + } else if let Some(lender) = self.lender.as_deref().filter(|l| l.id() == id) { + // The tool that lent a selection tool for a Cmd-drag holds its options while it waits. + lender.options() + } else { + self.make_tool(id).options() + } } /// Set options of tool `id` (default: the active tool). Another tool's persistent options are @@ -1272,7 +1279,10 @@ impl Session { self.note_place_queue_roots(); } let active = id.is_none_or(|id| id == self.tool.id()); - self.tool_set_since_reset |= active; + // The tool that lent a selection tool for a Cmd-drag is the tool chosen, and active again + // when the drag ends. + let lender = id.is_some_and(|id| self.lender.as_deref().is_some_and(|l| l.id() == id)); + self.tool_set_since_reset |= active || lender; // The Place queue and a parametric shape's kind are set first: setting the queue starts // again at its first file, and a `current` given with it is an index into it; setting the // kind puts the shape's other numbers back to that kind's, and those given with it are its. @@ -1284,7 +1294,7 @@ impl Session { t.set_option(k, v); } Self::store_tool_settings(&mut self.prefs, t); - let out = t.options(); + let mut out = t.options(); if let Some(other) = other { // The tool that lent a selection tool for a Cmd-drag is kept aside, not made again when // it is back: when it is the tool set, it takes the writes too (the Rounded Rectangle's @@ -1297,6 +1307,9 @@ impl Session { } } Self::restore_tool_settings(&self.prefs, lender); + if lender.id() == other.id() { + out = lender.options(); + } } // Only the stores it shares with the tool set: the active tool's own options stay as they // are, also in the middle of a drag that Escape can cancel. @@ -1308,10 +1321,11 @@ impl Session { /// `tool.setOption {tool?, key?, value?, values?}` (a command of the desktop app and of the /// headless host): set one option (`key`, `value`) or several (`values`) of `tool` (default: - /// the active tool) → its options. With no option given, the options are read and nothing is - /// set. Every option given is checked first ([`vectorcraft_tools::Tool::check_option`]): an - /// unknown option, a value of the wrong type or a number out of range is an error that sets - /// none of them. + /// the tool active when the call arrives, named before the edit in progress is finished) → its + /// options. With no option given, the options are read and nothing is set. Parameters that are + /// not an object are an error; none (null) read the options. Every option given is checked + /// first ([`vectorcraft_tools::Tool::check_option`]): an unknown option, a value of the wrong + /// type or a number out of range is an error that sets none of them. pub fn set_tool_option_cmd(&mut self, p: &Value) -> std::result::Result { let (tool, values) = vectorcraft_tools::option_check::set_option_params(p)?; if let Some(t) = tool diff --git a/crates/mcp/src/headless.rs b/crates/mcp/src/headless.rs index 69c7b5ba0e..cd08ece378 100644 --- a/crates/mcp/src/headless.rs +++ b/crates/mcp/src/headless.rs @@ -48,7 +48,7 @@ const HOST_COMMANDS: &[(&str, &str, &str)] = &[ ( "tool.setOption", "Tool Option", - "{key?, value? | values?: {key: value…}, tool?: id (default: the active tool)} → the tool's options (`{}` reads them); kept options last across tool switches", + "{key?, value? | values?: {key: value…}, tool?: id (default: the active tool)} → the tool's options (`{}` or no params reads them; params that are not an object are an error); kept options last across tool switches", ), ]; diff --git a/crates/mcp/src/tests/liquify.rs b/crates/mcp/src/tests/liquify.rs index c6d3862384..793f16efee 100644 --- a/crates/mcp/src/tests/liquify.rs +++ b/crates/mcp/src/tests/liquify.rs @@ -43,6 +43,21 @@ fn a_refused_option_value_is_an_error_result() { assert_eq!(command(&mut h, "tool.setOption", json!({})), before); } +/// `tool.setOption` with params that are not an object is an error result that sets nothing; +/// without params it reads the options, as `{}` does. +#[test] +fn set_option_params_that_are_not_an_object_are_an_error_result() { + let mut h = Headless::with_document(); + command(&mut h, "tool.select", json!({"tool": "warp"})); + let before = command(&mut h, "tool.setOption", json!({})); + for params in [json!(true), json!(7), json!([]), json!("x")] { + let r = call_tool(&mut h, "run_command", &json!({"command": "tool.setOption", "params": params})); + assert!(r.is_error, "{params}: {r:?}"); + } + assert_eq!(ok(&mut h, "run_command", json!({"command": "tool.setOption"})), before); + assert_eq!(command(&mut h, "tool.setOption", json!({})), before); +} + #[test] fn pointer_gestures_carry_pen_pressure() { let bloat = |pressure: f64| { diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index 7783f72f1f..7f54d48a82 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -775,7 +775,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "tool.setOption", "Tool Option", "", - "{key, value} | {values: {key: value…}}, tool?: id (default: the active tool) → the tool's options (`{}` reads them). The options a tool keeps (Liquify brush and tool options, Mirror & Cut, Puppet Warp, drawing tools…) last across tool switches and are saved with the preferences; another tool's are stored for when it is chosen. Every option is checked before any is set: an option the tool doesn't have, a value of the wrong type, a number out of the tool's range or an unknown choice is an error naming the tool and the option, and nothing changes. The Live Paint tools' width and the Shape Builder's gapLength are set to the nearest end of their range instead", + "{key, value} | {values: {key: value…}}, tool?: id (default: the active tool) → the tool's options (`{}` or no params reads them; params that are not an object are an error). The options a tool keeps (Liquify brush and tool options, Mirror & Cut, Puppet Warp, drawing tools…) last across tool switches and are saved with the preferences; another tool's are stored for when it is chosen. Every option is checked before any is set: an option the tool doesn't have, a value of the wrong type, a number out of the tool's range or an unknown choice is an error naming the tool and the option, and nothing changes. The Live Paint tools' width and the Shape Builder's gapLength are set to the nearest end of their range instead", ), ( "tool.toggleTouchMode", diff --git a/crates/tools/src/option_check.rs b/crates/tools/src/option_check.rs index 9a45caeb13..8507a82184 100644 --- a/crates/tools/src/option_check.rs +++ b/crates/tools/src/option_check.rs @@ -69,7 +69,7 @@ pub fn string_or_null(key: &str, v: &Value) -> Result<(), String> { /// `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()); + return Err(format!("the params must be an object, not {}", shown(p))); } let tool = match p.get("tool") { None => None, @@ -94,6 +94,32 @@ pub fn set_option_params(p: &Value) -> Result<(Option<&str>, Map) Ok((tool, values)) } +/// The JSON text of `v` as a parameter error shows it: cut to 40 characters, with `…` when it was +/// longer. The text is written only up to the cut. +fn shown(v: &Value) -> String { + struct Cut { + text: String, + left: usize, + } + impl std::fmt::Write for Cut { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + for c in s.chars() { + if self.left == 0 { + return Err(std::fmt::Error); + } + self.left -= 1; + self.text.push(c); + } + Ok(()) + } + } + let mut out = Cut { text: String::new(), left: 40 }; + if std::fmt::Write::write_fmt(&mut out, format_args!("{v}")).is_err() { + out.text.push('…'); + } + out.text +} + /// Check every one of `values` as an option of `tool` ([`Tool::check_options`]), before any is /// set: the first one the tool doesn't take is an error that names the tool. pub fn check_values(tool: &dyn Tool, values: &Map) -> Result<(), String> { @@ -145,9 +171,13 @@ mod tests { // 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}"); + for (bad, got) in [(json!(true), "true"), (json!(7), "7"), (json!([]), "[]"), (json!("x"), "\"x\"")] { + assert_eq!(set_option_params(&bad).err(), Some(format!("the params must be an object, not {got}")), "{bad}"); } + // A long value is shown cut, as the other parameter errors show it. + let long = json!("a".repeat(100)); + let shown = format!("\"{}…", "a".repeat(39)); + assert_eq!(set_option_params(&long).err(), Some(format!("the params must be an object, not {shown}"))); 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/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index 582bc77802..c419237a13 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -776,7 +776,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "tool.setOption", "Tool Option", "", - "{key, value} | {values: {key: value…}}, tool?: id (default: the active tool) → the tool's options (`{}` reads them). The options a tool keeps (Liquify brush and tool options, Mirror & Cut, Puppet Warp, drawing tools…) last across tool switches and are saved with the preferences; another tool's are stored for when it is chosen. Every option is checked before any is set: an option the tool doesn't have, a value of the wrong type, a number out of the tool's range or an unknown choice is an error naming the tool and the option, and nothing changes. The Live Paint tools' width and the Shape Builder's gapLength are set to the nearest end of their range instead", + "{key, value} | {values: {key: value…}}, tool?: id (default: the active tool) → the tool's options (`{}` or no params reads them; params that are not an object are an error). The options a tool keeps (Liquify brush and tool options, Mirror & Cut, Puppet Warp, drawing tools…) last across tool switches and are saved with the preferences; another tool's are stored for when it is chosen. Every option is checked before any is set: an option the tool doesn't have, a value of the wrong type, a number out of the tool's range or an unknown choice is an error naming the tool and the option, and nothing changes. The Live Paint tools' width and the Shape Builder's gapLength are set to the nearest end of their range instead", ), ( crate::touch::TOGGLE_MODE, diff --git a/docs/control-protocol.md b/docs/control-protocol.md index edd0d8f96f..0d78baca0a 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -922,9 +922,8 @@ Tool options last: `tool.setOption {key, value}` or `{values: {…}}` (and `tool 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; 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 +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. The Live Paint tools' `width` and the Shape Builder's `gapLength` are set to the nearest end of their range instead. diff --git a/docs/mcp.md b/docs/mcp.md index 0c46157847..7ec64a52de 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9816,10 +9816,10 @@ to that tool), a value of the wrong type, a number outside the tool's range, an `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. 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. +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. ```json {"name":"run_command","arguments":{"command":"tool.setOption","params":{"tool":"twirl","values":{"width":60,"rate":90}}}}