PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Layer commands accept an explicit layer with none selected; Deselect Layers is a no-op when nothing is selected (#2457) (#2481)
MarekSpodBiedry99570a7538c6
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 6bf0558a9..28287dbbc 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -1249,7 +1249,7 @@ pub fn specs() -> Vec<CommandSpec> {const ALIGN: &str = r##"{"to":"auto|layers|selection|canvas"="auto"} (auto: the selection bounds with one layer and an active selection, else the selected layers' bounds)"##;vec![spec!("select.allLayers", "All Layers", &["Select"], Some("Cmd+Alt+A"), "{}", has_doc, |s, _| select_all_layers(s)),- spec!("select.deselectLayers", "Deselect Layers", &["Select"], None, "{}", has_layer, |s, _| {+ spec!("select.deselectLayers", "Deselect Layers", &["Select"], None, "{}", has_doc, |s, _| {set_selection(s, Vec::new(), None, None)?;if let Some(st) = s.active_mut() {st.active_layer = None;@@ -1530,6 +1530,18 @@ mod tests {s.execute("select.deselectLayers", json!({})).unwrap();assert!(sel(&s).is_empty());assert_eq!(s.active().unwrap().history.entries().len(), history);+ // Nothing selected: a no-op, not an error (#2457).+ s.execute("select.deselectLayers", json!({})).unwrap();+ assert_eq!(s.active().unwrap().history.entries().len(), history);+ // An explicit layer needs no active layer.+ s.execute("layer.hideLayers", json!({"layer": a.0})).unwrap();+ assert!(!s.active().unwrap().doc.layer(a).unwrap().visible);+ s.execute("layer.renameLayer", json!({"layer": a.0, "name": "x"})).unwrap();+ assert_eq!(s.active().unwrap().doc.layer(a).unwrap().name, "x");+ s.execute("layer.showLayers", json!({"layer": a.0})).unwrap();+ assert!(s.active().unwrap().doc.layer(a).unwrap().visible);+ assert!(s.execute("layer.hideLayers", json!({"layer": 999_999})).is_err());+ assert!(s.execute("layer.hideLayers", json!({})).is_err());assert!(!s.is_enabled("layer.align.topEdges"));}diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 1a83f5688..b6d8016e4 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -489,9 +489,19 @@ impl Session {/// The command's own precondition for a call with `params` (their target filled in).fn precondition(&self, spec: &commands::CommandSpec, params: &Value) -> std::result::Result<(), String> {+ if self.names_existing_layer(spec.id, params) {+ return Ok(());+ }channel_cmds::mask_target_enabled(self, spec.id, params).unwrap_or_else(|| (spec.enabled)(self))}+ /// Whether `id` is a layer command whose `"layer"` param names an existing layer, so it needs no+ /// active layer (#2457). Matches how `layer_param` resolves the layer when the command runs.+ fn names_existing_layer(&self, id: &str, params: &Value) -> bool {+ matches!(id, "layer.hideLayers" | "layer.showLayers" | "layer.renameLayer")+ && params.get("layer").and_then(Value::as_u64).is_some_and(|l| self.active().is_some_and(|st| st.doc.layer(LayerId(l)).is_some()))+ }+/// Apply an undoable edit to the active document.pub fn edit<R>(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Option<LayerId>) -> Result<R>) -> Result<R> {let restrict = self.color_restrict;