VectorCraft: An open-source, vector art tool written by scratch in pure Rust
Commands: objects given as ids stand in for the selection
Gert Pellindfc1b846cccc
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 1658e831c..717e9f672 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -1300,6 +1300,17 @@ impl Session {true}+ /// `spec`'s `enabled` with `ids` selected instead of the selection, which is then put back.+ fn enabled_with_selection(&mut self, spec: &CommandSpec, ids: Vec<NodeId>) -> std::result::Result<(), String> {+ let Some(d) = self.active_mut() else { return (spec.enabled)(self) };+ let old = std::mem::replace(&mut d.selection, Selection { objects: ids, ..Selection::default() });+ let r = (spec.enabled)(self);+ if let Some(d) = self.active_mut() {+ d.selection = old;+ }+ r+ }+/// Execute a command by id. This is THE entry point for every frontend.pub fn execute(&mut self, id: &str, params: &Value) -> Result<Value> {let spec = find_command(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?;@@ -1309,7 +1320,15 @@ impl Session {cmd::params::check_params(id, params)?;// Clear's menu needs a selection; an agent supplying explicit targets only needs a// document. The command validates those targets before making any edit.- let enabled = if id == "edit.clear" && params.get("ids").is_some() { cmd::has_doc(self) } else { (spec.enabled)(self) };+ let enabled = if id == "edit.clear" && params.get("ids").is_some() {+ cmd::has_doc(self)+ } else if let Some(ids) = given_ids(spec.params, params) {+ // Objects given as `ids` stand in for the selection: the command is enabled as it is+ // with them selected (#1575).+ self.enabled_with_selection(spec, ids)+ } else {+ (spec.enabled)(self)+ };if let Err(why) = enabled {return Err(EngineError::Disabled(id.to_string(), why));}@@ -1770,6 +1789,8 @@ mod tests_halftone;#[cfg(test)]mod tests_history_mem;#[cfg(test)]+mod tests_ids_without_selection;+#[cfg(test)]mod tests_inline;#[cfg(test)]mod tests_journal;@@ -1955,3 +1976,14 @@ mod tests_widthprofiles;mod tests_widthtool;#[cfg(test)]mod tests_xform;++/// The non-empty `ids` given to a command whose params description takes them (an `ids` word in it).+fn given_ids(description: &str, params: &Value) -> Option<Vec<NodeId>> {+ let takes = description.match_indices("ids").any(|(i, _)| {+ let before = description.get(..i).and_then(|s| s.chars().next_back());+ let after = description.get(i + 3..).and_then(|s| s.chars().next());+ !before.is_some_and(|c| c.is_ascii_alphanumeric() || c == '_') && !after.is_some_and(|c| c.is_ascii_alphanumeric() || c == '_')+ });+ let ids: Vec<NodeId> = params.get("ids")?.as_array()?.iter().filter_map(Value::as_u64).map(NodeId).collect();+ (takes && !ids.is_empty()).then_some(ids)+}diff --git a/crates/engine/src/tests_ids_without_selection.rs b/crates/engine/src/tests_ids_without_selection.rsnew file mode 100644index 000000000..752fa6300--- /dev/null+++ b/crates/engine/src/tests_ids_without_selection.rs@@ -0,0 +1,53 @@+//! #1575: commands that take `ids` work on the objects given, with nothing selected; the+//! selection is left as it was.++use serde_json::json;++use super::*;++fn doc_with_rect() -> (Session, u64) {+ let mut s = Session::new();+ s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();+ let id = s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap()["id"].as_u64().unwrap();+ s.execute("select.none", &json!({})).unwrap();+ (s, id)+}++#[test]+fn ids_stand_in_for_the_selection() {+ for (cmd, params) in [+ ("object.repeat.radial", json!({})),+ ("edit.colors.invert", json!({})),+ ("object.pattern.make", json!({"name": "P"})),+ ("symbol.new", json!({"name": "S"})),+ ("object.mesh.create", json!({})),+ ("object.makePixelPerfect", json!({})),+ ] {+ let (mut s, id) = doc_with_rect();+ let mut p = params;+ p["ids"] = json!([id]);+ let r = s.execute(cmd, &p);+ assert!(r.is_ok(), "{cmd}: {r:?}");+ }+}++#[test]+fn the_selection_is_put_back_and_the_check_still_applies() {+ let (mut s, id) = doc_with_rect();+ // Disabled even with the object given: Make with Top Object needs two.+ let r = s.execute("object.envelope.makeWithTopObject", &json!({"ids": [id]}));+ assert!(matches!(r, Err(EngineError::Disabled(..))), "{r:?}");+ assert!(s.doc().unwrap().selection.objects.is_empty(), "the stand-in selection is put back");+ // Without ids, nothing selected is still nothing selected.+ let r = s.execute("object.repeat.radial", &json!({}));+ assert!(matches!(r, Err(EngineError::Disabled(..))), "{r:?}");+}++#[test]+fn only_an_ids_word_counts() {+ let p = json!({"ids": [3]});+ assert_eq!(given_ids("{ids?: [id]}", &p), Some(vec![NodeId(3)]));+ assert_eq!(given_ids(r#"{"ids":[id]}"#, &p), Some(vec![NodeId(3)]));+ assert_eq!(given_ids("{grids: n, guides: bool}", &p), None);+ assert_eq!(given_ids("{ids?: [id]}", &json!({"ids": []})), None);+}