PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Transform Again on a Copy (Cmd+Alt+Shift+T): step and repeat (part of #352) (#474)
BFlatf697e43ef775
diff --git a/crates/engine/src/edit_cmds.rs b/crates/engine/src/edit_cmds.rsindex cdfa80fb0..e5b16cff4 100644--- a/crates/engine/src/edit_cmds.rs+++ b/crates/engine/src/edit_cmds.rs@@ -357,6 +357,38 @@ fn toggle_last_state(s: &mut Session) -> Result<Value> {/// Edit › Transform › Again: replay the last `edit.transform` on the active layer.fn transform_again(s: &mut Session) -> Result<Value> {+ let p = transform_again_params(s)?;+ s.execute("edit.transform", p)+}++/// Transform Again on a copy (⌥⇧⌘T, no menu item): duplicates the active layer and repeats the+/// last transform on the copy, as one history step, so pressing it again steps and repeats+/// (#352). The transform is worked out before anything changes, so a refusal leaves no copy.+fn transform_again_copy(s: &mut Session) -> Result<Value> {+ let p = transform_again_params(s)?;+ let id = active_id(s)?;+ s.execute("layer.duplicate", json!({"layer": id.0}))?;+ match s.execute("edit.transform", p) {+ Ok(r) => {+ let st = s.active_mut().ok_or(EngineError::NoDocument)?;+ st.history.purge_last();+ st.history.set_current_label("Transform Again");+ Ok(r)+ }+ Err(e) => {+ // The copy (e.g. of a layer whose position is locked) couldn't be transformed: take+ // it back, leaving nothing to redo.+ s.undo();+ if let Some(st) = s.active_mut() {+ st.history.clear_redo();+ }+ Err(e)+ }+ }+}++/// The `edit.transform` params that repeat the last transform on the active layer.+fn transform_again_params(s: &Session) -> Result<Value> {let (_, last) =s.journal.iter().rev().find(|(id, _)| id == "edit.transform").cloned().ok_or(EngineError::Other("there is no transform to repeat".into()))?;let (rect, quad) = (last.get("rect").cloned(), last.get("quad").cloned());@@ -391,7 +423,7 @@ fn transform_again(s: &mut Session) -> Result<Value> {(_, _, Some(m)) => p["matrix"] = m,_ => return Err(EngineError::Other("the last transform can't be repeated".into())),}- s.execute("edit.transform", p)+ Ok(p)}/// View › New Guide / guide moves (undoable, like Photoshop's "New Guide"/"Move Guide" states).@@ -470,6 +502,15 @@ pub fn specs() -> Vec<CommandSpec> {spec!("image.autoColor", "Auto Color", &["Image"], Some("Cmd+Shift+B"), "{}", has_pixels, |s, _| auto_adjust(s, "color")),spec!("edit.toggleLastState", "Toggle Last State", &["Edit"], Some("Cmd+Alt+Z"), "{}", has_doc, |s, _| toggle_last_state(s)),spec!("edit.transform.again", "Again", &["Edit", "Transform"], Some("Cmd+Shift+T"), "{}", has_doc, |s, _| transform_again(s)),+ spec!(+ "edit.transform.againCopy",+ "Transform Again on a Copy",+ &[],+ Some("Cmd+Alt+Shift+T"),+ "{} (duplicates the active layer and repeats the last transform on the copy: step and repeat)",+ has_doc,+ |s, _| transform_again_copy(s)+ ),spec!("view.newGuide", "New Guide…", &["View"], None, r##"{"orientation":"horizontal|vertical","position":px}"##, has_doc, |s, p| guide_cmd(s, p, "new")),@@ -698,6 +739,40 @@ mod tests {}}+ /// #352: ⌥⇧⌘T steps and repeats: each press adds a copy one transform further on, and each+ /// is one undo step.+ #[test]+ fn transform_again_on_a_copy_steps_and_repeats() {+ let mut s = session();+ assert!(s.execute("edit.transform.againCopy", json!({})).is_err(), "nothing to repeat yet");+ assert_eq!(s.active().unwrap().doc.layers.len(), 2, "a refusal leaves no copy");+ s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 10, 0]})).unwrap();+ let steps = s.active().unwrap().history.past_len();+ for (i, x) in [30, 40].into_iter().enumerate() {+ s.execute("edit.transform.againCopy", json!({})).unwrap();+ let d = &s.active().unwrap().doc;+ assert_eq!(d.layers.len(), 3 + i);+ assert_eq!(active_bounds(&s), Rect::new(x, 10, x + 40, 30), "the copy moved one step further");+ assert_eq!(s.active().unwrap().history.past_len(), steps + 1 + i, "one history step per press");+ }+ assert_eq!(s.active().unwrap().history.undo_label(), Some("Transform Again"));+ // The original and the first copy stay where they were.+ let d = &s.active().unwrap().doc;+ let b = |i: usize| d.layers[i].surface().unwrap().content_bounds();+ assert_eq!((b(1), b(2)), (Rect::new(20, 10, 60, 30), Rect::new(30, 10, 70, 30)));+ // Undo removes the copy and its move together.+ s.undo();+ assert_eq!(s.active().unwrap().doc.layers.len(), 3);+ assert_eq!(active_bounds(&s), Rect::new(30, 10, 70, 30));+ // A copy that can't move (position locked) is taken back.+ let id = s.active().unwrap().active_layer.unwrap();+ s.execute("layer.setProps", json!({"layer": id.0, "locks": {"position": true}})).unwrap();+ let layers = s.active().unwrap().doc.layers.len();+ assert!(s.execute("edit.transform.againCopy", json!({})).unwrap_err().to_string().contains("locked"));+ assert_eq!(s.active().unwrap().doc.layers.len(), layers);+ assert!(!s.active().unwrap().history.can_redo(), "nothing to redo");+ }+#[test]fn toggle_last_state_and_transform_again() {let mut s = session();diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rsindex 6f3d75be9..d17c23967 100644--- a/crates/ops/src/lib.rs+++ b/crates/ops/src/lib.rs@@ -111,6 +111,11 @@ impl History {self.undo.pop_back().is_some()}+ /// Forget the redo states, e.g. after undoing half of a compound step that failed.+ pub fn clear_redo(&mut self) {+ self.redo.clear();+ }+pub fn clear(&mut self) {self.undo.clear();self.redo.clear();diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsvindex 30b870afb..6de12a31a 100644--- a/crates/ui-egui/src/i18n/cs.tsv+++ b/crates/ui-egui/src/i18n/cs.tsv@@ -110,6 +110,7 @@Free Transform Volná transformaceTransform TransformaceAgain Opakovat+ Transform Again on a Copy Duplikovat a opakovat transformaciScale MěřítkoRotate OtočitSkew Zkositdiff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsvindex 96c545fb8..cfac9c383 100644--- a/crates/ui-egui/src/i18n/es.tsv+++ b/crates/ui-egui/src/i18n/es.tsv@@ -102,6 +102,7 @@Free Transform Transformación libreTransform TransformarAgain Repetir+ Transform Again on a Copy Duplicar y repetir la transformaciónScale EscalarRotate RotarSkew Sesgardiff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsvindex a908e0a8c..d9d11113f 100644--- a/crates/ui-egui/src/i18n/ja.tsv+++ b/crates/ui-egui/src/i18n/ja.tsv@@ -106,6 +106,7 @@Free Transform 自由変形Transform 変形Again もう一度+ Transform Again on a Copy 複製してもう一度変形Scale 拡大・縮小Rotate 回転Skew 斜体diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsvindex 3cbecb14a..ca3701fed 100644--- a/crates/ui-egui/src/i18n/ru.tsv+++ b/crates/ui-egui/src/i18n/ru.tsv@@ -1679,6 +1679,7 @@90° Counter Clockwise 90° против часовой стрелкеAI Integrations… Интеграции ИИ…Again Снова+ Transform Again on a Copy Дублировать и снова трансформироватьAnamorphic 2:1 (2) Анаморф 2:1 (2)Arbitrary… Другой угол…Artboard Guides Направляющие монтажной областиdiff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsvindex 5262400bb..7c0291242 100644--- a/crates/ui-egui/src/i18n/zh-hans.tsv+++ b/crates/ui-egui/src/i18n/zh-hans.tsv@@ -101,6 +101,7 @@Free Transform 自由变换Transform 变换Again 再次变换+ Transform Again on a Copy 复制并再次变换Scale 缩放Rotate 旋转Skew 斜切diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsvindex ce91a0692..e0b6a53eb 100644--- a/crates/ui-egui/src/i18n/zh-hant.tsv+++ b/crates/ui-egui/src/i18n/zh-hant.tsv@@ -110,6 +110,7 @@Free Transform 任意變形Transform 變形Again 再次+ Transform Again on a Copy 複製並再次變形Scale 縮放Rotate 旋轉Skew 傾斜