Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Actions: a Deselect step with nothing selected plays on instead of stopping (#6744)

Higgs32584606b2dadbaba
diff --git a/crates/engine/src/actions_cmds.rs b/crates/engine/src/actions_cmds.rs
index d881a6112..b87d78c21 100644
--- a/crates/engine/src/actions_cmds.rs
+++ b/crates/engine/src/actions_cmds.rs
@@ -307,6 +307,12 @@ fn stop(s: &mut Session, _p: &Value) -> Result<Value> {
Ok(json!({"action": action.name, "steps": action.steps.len()}))
}
+/// A step Photoshop plays as a silent no-op although its menu item is greyed: Deselect with an
+/// open document and nothing selected (actions often start with it to clear any selection, #6744).
+fn noop_step(s: &Session, id: &str) -> bool {
+ id == "select.deselect" && s.active().is_some_and(|d| d.doc.selection.is_none())
+}
+
fn run_recorded(s: &mut Session, action: &Action, from: usize) -> (u64, Option<Value>) {
let mut ran = 0u64;
let mut failed = None;
@@ -317,6 +323,10 @@ fn run_recorded(s: &mut Session, action: &Action, from: usize) -> (u64, Option<V
failed = Some(json!({"step": i, "id": id, "error": e.to_string()}));
break;
}
+ if noop_step(s, id) {
+ ran += 1;
+ continue;
+ }
match s.execute(id, params.clone()) {
Ok(value) => {
if let Some(error) = nested_failure(id, &value) {
@@ -692,6 +702,23 @@ mod tests {
s.active().unwrap().doc.layers[layer].surface().unwrap().pixel(x, y)
}
+ #[test]
+ fn deselect_with_nothing_selected_is_a_noop_in_playback() {
+ // #6744: imported actions often start with Deselect; with no selection Photoshop plays on.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
+ let before = s.active().unwrap().history.past_len();
+ let deselect = crate::commands::find("select.deselect").unwrap();
+ assert_eq!((deselect.run)(&mut s, &json!({})).unwrap(), Value::Null);
+ assert_eq!(s.active().unwrap().history.past_len(), before, "no history step");
+
+ let steps = vec![("select.deselect".to_string(), json!({})), ("layer.new.layer".to_string(), json!({"name": "A"}))];
+ s.actions.list.push(Action::new("Clear", steps));
+ let played = s.execute("actions.play", json!({"action": 0})).unwrap();
+ assert_eq!(played["ran"], 2, "{played}");
+ assert_eq!(s.active().unwrap().doc.layers.last().unwrap().name, "A");
+ }
+
#[test]
fn unchecked_steps_are_skipped_and_follow_move_and_delete() {
let mut s = Session::new();
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 2f0418712..f74388af5 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -555,6 +555,10 @@ fn build() -> Vec<CommandSpec> {
Ok(Value::Null)
}),
cmd!("select.deselect", "Deselect", ["Select"], Some("Cmd+D"), "{}", has_selection, |s, _| {
+ // Nothing selected: nothing to do and no history step, as in Photoshop (#6744).
+ if s.active().is_none_or(|d| d.doc.selection.is_none()) {
+ return Ok(Value::Null);
+ }
s.edit("Deselect", |doc, _| {
doc.selection = None;
Ok(())