Skip to content

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

Actions: Last Filter is recorded as the filter it repeated (#3089) (#3091)

Valentin Klein8584e5dbf82e
diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs
index e0941d525..2ccc999b4 100644
--- a/crates/engine/src/filters.rs
+++ b/crates/engine/src/filters.rs
@@ -410,7 +410,7 @@ pub fn specs() -> Vec<CommandSpec> {
/// The most recent filter command in the session journal, retargeted at the active layer: dialogs
/// such as the Filter Gallery record the layer they ran on, and Last Filter applies to the
/// current layer, as in Photoshop (#1270).
-fn last_filter(s: &Session) -> Option<(String, Value)> {
+pub(crate) fn last_filter(s: &Session) -> Option<(String, Value)> {
let (id, mut params) = s.journal.iter().rev().find(|(id, _)| params_for(id, &Value::Null).is_some() && id.starts_with("filter.")).cloned()?;
if let Value::Object(m) = &mut params {
m.remove("layer");
@@ -652,6 +652,31 @@ mod tests {
assert_eq!(active_pixels(&s2), twice);
}
+ #[test]
+ fn recorded_last_filter_replays_the_filter_it_repeated() {
+ // #3089: the action holds the filter with its settings, not a "repeat whatever ran
+ // last" step that fails in a new session.
+ let mut s = session();
+ paint_pattern(&mut s);
+ s.execute("filter.other.offset", json!({"horizontal": 3})).unwrap();
+ s.execute("actions.record", json!({"name": "Again"})).unwrap();
+ s.execute("filter.lastFilter", json!({})).unwrap();
+ s.execute("actions.stop", json!({})).unwrap();
+ let steps = s.execute("actions.get", json!({"action": "Again"})).unwrap()["steps"].clone();
+ assert_eq!(steps, json!([["filter.other.offset", {"horizontal": 3}]]));
+ let action = s.actions.list[0].clone();
+
+ let mut fresh = session();
+ paint_pattern(&mut fresh);
+ fresh.actions.list.push(action);
+ let played = fresh.execute("actions.play", json!({"action": "Again"})).unwrap();
+ assert!(played.get("failed").is_none(), "{played}");
+ let mut direct = session();
+ paint_pattern(&mut direct);
+ direct.execute("filter.other.offset", json!({"horizontal": 3})).unwrap();
+ assert_eq!(active_pixels(&fresh), active_pixels(&direct));
+ }
+
#[test]
fn last_filter_applies_to_the_active_layer() {
let mut s = session();
diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs
index 7b567530d..15e7387b0 100644
--- a/crates/engine/src/jobs.rs
+++ b/crates/engine/src/jobs.rs
@@ -615,6 +615,15 @@ impl Session {
if let Some(gate) = self.authorize {
gate(id, &params)?;
}
+ // Last Filter runs as the filter it repeats: journaled, recorded in an action, faded and
+ // run as a job like that filter, so a recorded step replays the same filter anywhere
+ // (#3089), as Transform Again records its transform (#1440). With no filter yet it falls
+ // through to its own precondition.
+ if id == "filter.lastFilter"
+ && let Some((filter, p)) = crate::filters::last_filter(self).filter(|(f, _)| f != id)
+ {
+ return self.dispatch(&filter, p, background);
+ }
if id == "file.new" {
crate::document_preset_cmds::validate_new_params(self, &params)?;
}