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.
This commit is contained in:
dotCooCoo
2026-11-14 14:10:16 -08:00
parent 72082aee46
commit df95a67ea7
10 changed files with 96 additions and 23 deletions
+13 -3
View File
@@ -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
+5
View File
@@ -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);
}
+21 -7
View File
@@ -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<Value, String> {
let (tool, values) = vectorcraft_tools::option_check::set_option_params(p)?;
if let Some(t) = tool
+1 -1
View File
@@ -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",
),
];
+15
View File
@@ -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| {
+1 -1
View File
@@ -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",
+33 -3
View File
@@ -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, Value>), 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<String, Value>)
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<String, Value>) -> 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()));
+1 -1
View File
@@ -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,
+2 -3
View File
@@ -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.
+4 -4
View File
@@ -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}}}}