From e88d24bc045639c415ea6ecf221deea91ee6a212 Mon Sep 17 00:00:00 2001 From: jonathanwxh-cell Date: Thu, 22 Oct 2026 17:42:50 +0800 Subject: [PATCH] Fix ATN document offsets after tab reordering Resolve relative activation against stable document opening order while keeping absolute indices in visual tab order. Preserve lifecycle identity across close, reopen and imported-ID collisions. Add command and ATN playback regressions and document the behavioral source evidence. --- crates/engine/src/atn_cmds.rs | 5 ++- crates/engine/src/atn_cmds/tests.rs | 4 +- crates/engine/src/commands.rs | 13 ++---- crates/engine/src/lib.rs | 18 +++++++++ crates/engine/src/tests.rs | 63 +++++++++++++++++++++++++---- docs/gaps.md | 14 ++++++- 6 files changed, 95 insertions(+), 22 deletions(-) diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs index b851180529..46ed1cf32c 100644 --- a/crates/engine/src/atn_cmds.rs +++ b/crates/engine/src/atn_cmds.rs @@ -15,7 +15,8 @@ //! Layer references by name (`select` of layer "Shadow") become `{"layer": "Shadow"}`, which //! `commands::layer_param` resolves in whatever document the action plays on (#219). A switch //! to another open document (`select` of `Dcmn` by offset) becomes `document.activate -//! {"offset"}`, the tab that many places from the active one (#7103). +//! {"offset"}`, the document that many places from the active one in opening order, not dragged +//! tab order (#7103). use photocraft_psd::atn::{self, AtnAction, AtnEvent, AtnFile, AtnItem}; use photocraft_psd::descriptor::{Class, Descriptor, Id, ReferenceItem, UnicodeString, Value as D}; @@ -1591,7 +1592,7 @@ fn select_from(d: &Descriptor) -> Mapped { } (Some(c), Target::Class) if is_tool_class(c) => Mapped::Unsupported { harmless: true }, // Switching to another open document: its offset from the active one, -1 for the previous - // tab and 1 for the next (a template action copies from one and pastes into the other, #7103). + // opened document and 1 for the next, independently of dragged tabs (#7103). (Some("Dcmn"), Target::Offset(o)) => step("document.activate", json!({"offset": o})), // A menu item chosen while recording (`Mn`): the View menu's zoom items replay. (Some("Mn"), Target::Ordinal(item)) => match lookup(MENU_ITEMS, &item) { diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs index 2aa1fac31e..8bcd88ccc6 100644 --- a/crates/engine/src/atn_cmds/tests.rs +++ b/crates/engine/src/atn_cmds/tests.rs @@ -1216,6 +1216,8 @@ fn select_document_by_offset_imports_plays_and_saves_back() { s.execute("file.new", json!({"width": 8, "height": 8, "name": "source"})).unwrap(); s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); s.execute("file.new", json!({"width": 8, "height": 8, "name": "template", "background": "transparent"})).unwrap(); + // Reorder tabs before playback: the source remains the previous *opened* document. + s.execute("document.move", json!({"document": 1, "to": 0})).unwrap(); let r = s.execute("actions.load", json!({"data": data})).unwrap(); assert_eq!(r["unsupported"], json!([]), "{r}"); let steps = &s.actions.list[0].steps; @@ -1229,7 +1231,7 @@ fn select_document_by_offset_imports_plays_and_saves_back() { assert_eq!(s.active().unwrap().doc.name, "template"); assert_eq!(s.active().unwrap().doc.layers.len(), 2, "the source's pixels are pasted on a new layer"); assert_eq!(pixel(&mut s), [1.0, 0.0, 0.0, 1.0]); - assert_eq!(s.documents()[0].doc.layers.len(), 1, "the source is only copied from"); + assert_eq!(s.documents()[1].doc.layers.len(), 1, "the source is only copied from"); let first = s.actions.list.clone(); let r = s.execute("actions.save", json!({"set": "Template"})).unwrap(); diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index 95930f11d1..2b42b4b48e 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -1272,7 +1272,7 @@ fn build() -> Vec { label: "Activate Document", menu: &[], shortcut: None, - params: r##"{"document":index} or {"offset":int} (the open document that many tabs after the active one, before it when negative: a Photoshop action's switch to another document) → {document}"##, + params: r##"{"document":index} or {"offset":int} (the document that many places after the active one in opening order, before it when negative: a Photoshop action's switch to another document) → {document}"##, enabled: has_doc, run: |s, p| { // A null key counts as omitted. @@ -1280,18 +1280,11 @@ fn build() -> Vec { let idx = match (given("document"), given("offset")) { (Some(_), Some(_)) => return Err(bad("document.activate", "give `document` or `offset`, not both")), (None, None) => return Err(bad("document.activate", "needs `document` (a tab index) or `offset`")), - // Past the first or last tab is refused rather than wrapped, so an action that + // Refuse offsets past the opening-order ends rather than wrap, so an action that // copies from one document never silently pastes into the wrong one (#7103). (None, Some(o)) => { let o = o.as_i64().ok_or_else(|| bad("document.activate", "`offset` must be an integer"))?; - let (active, len) = (s.active_index().ok_or(EngineError::NoDocument)?, s.documents().len()); - let target = i64::try_from(active).ok().and_then(|a| a.checked_add(o)).and_then(|i| usize::try_from(i).ok()); - let Some(i) = target.filter(|&i| i < len) else { - let side = if o < 0 { "before" } else { "after" }; - let msg = format!("no document {} {side} the active one, which is {} of {len}", o.unsigned_abs(), active + 1); - return Err(EngineError::Other(msg)); - }; - i + s.document_at_offset(o)? } (Some(d), None) => { let i = d.as_u64().ok_or_else(|| bad("document.activate", "`document` must be a tab index"))?; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 4408766f1f..ac94e8daf8 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -463,6 +463,8 @@ pub const PRECHECKED: &[&str] = &["layer.new.layer", "layer.new.group", "layer.n #[derive(Default)] pub struct Session { docs: Vec, + /// Admitted document identities in opening order, independent of visual-tab reordering. + open_document_order: Vec, active: Option, pub tools: ToolState, /// Log of executed commands (for action recording and debugging). @@ -618,6 +620,20 @@ impl Session { Some(to) } + /// Resolve a Photoshop action's offset in opening order, returning its current tab index. + /// Tab dragging changes `docs`, but must not change which document an imported action edits. + pub(crate) fn document_at_offset(&self, offset: i64) -> Result { + let id = self.active().ok_or(EngineError::NoDocument)?.doc.id; + let active = self.open_document_order.iter().position(|&open| open == id).ok_or(EngineError::NoDocument)?; + let len = self.open_document_order.len(); + let target = i64::try_from(active).ok().and_then(|a| a.checked_add(offset)).and_then(|i| usize::try_from(i).ok()); + let Some(id) = target.and_then(|i| self.open_document_order.get(i)) else { + let side = if offset < 0 { "before" } else { "after" }; + return Err(EngineError::Other(format!("no document {} {side} the active one, which is {} of {len}", offset.unsigned_abs(), active + 1))); + }; + self.docs.iter().position(|st| st.doc.id == *id).ok_or(EngineError::NoDocument) + } + /// Add a document (from File → New, an import, etc.) and make it active. /// Preserve its identity unless another open document already owns it. /// Identity-dependent callers must read the admitted document at the returned index. @@ -626,6 +642,7 @@ impl Session { while self.docs.iter().any(|st| st.doc.id == doc.id) { doc.id = photocraft_doc::DocId::fresh(); } + self.open_document_order.push(doc.id); let mut st = DocState::new(doc, path); st.history.max_states = self.prefs.get().performance.history_states.max(1) as usize; st.history.max_bytes = self.prefs.get().performance.history_budget_bytes(); @@ -661,6 +678,7 @@ impl Session { self.object_finder.forget_doc(id); } let d = self.docs.remove(index); + self.open_document_order.retain(|&id| id != d.doc.id); self.active = if self.docs.is_empty() { None } else { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 0b1b48aa3b..c8ba2a8fcc 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1573,11 +1573,11 @@ fn document_inspect_and_activate_invalid_index() { assert_eq!(err_act.to_string(), "no document at index 9"); } -/// #7103: Photoshop's Select Document by offset (previous is -1, next +1) walks the open -/// documents in tab order from the active one. Past either end it is refused, not wrapped onto -/// another document, and bad params leave the active document as it was. +/// #7103: Photoshop's Select Document by offset (previous is -1, next +1) walks opening order +/// from the active document. PhotoCraft refuses offsets past either end instead of wrapping +/// onto another document, and bad params leave the active document as it was. #[test] -fn document_activate_by_offset_walks_the_tabs() { +fn document_activate_by_offset_walks_opening_order() { let mut s = Session::new(); for name in ["a", "b", "c"] { s.execute("file.new", json!({"width": 2, "height": 2, "name": name})).unwrap(); @@ -1613,11 +1613,60 @@ fn document_activate_by_offset_walks_the_tabs() { assert!(s.execute("document.activate", bad.clone()).is_err(), "{bad}"); assert_eq!(active(&s), "b", "{bad}"); } - // Tabs, not the order the documents were opened in: with "c" dragged to the front, two tabs - // before "b" is "c". + // Dragging "c" first changes tab indices, not the opening order a, b, c. s.execute("document.move", json!({"document": 2, "to": 0})).unwrap(); - s.execute("document.activate", json!({"offset": -2})).unwrap(); + assert_eq!(active(&s), "b"); + let err = s.execute("document.activate", json!({"offset": -2})).unwrap_err().to_string(); + assert_eq!(err, "no document 2 before the active one, which is 2 of 3", "the error reports opening order, not tab position"); + assert_eq!(active(&s), "b", "a failed offset must not activate the visually first tab"); + assert_eq!(s.execute("document.activate", json!({"offset": -1})).unwrap(), json!({"document": 1})); + assert_eq!(active(&s), "a"); + assert_eq!(s.execute("document.activate", json!({"offset": 2})).unwrap(), json!({"document": 0})); assert_eq!(active(&s), "c"); + // Absolute indices and the returned document index remain in visual-tab order. + s.execute("document.activate", json!({"document": 2})).unwrap(); + assert_eq!(active(&s), "b"); + assert_eq!(s.execute("document.activate", json!({"offset": 0})).unwrap(), json!({"document": 2})); + s.execute("document.activate", json!({"document": 0})).unwrap(); + assert_eq!(active(&s), "c"); +} + +#[test] +fn document_activate_opening_order_survives_close_and_reopen_with_persisted_ids() { + let mut s = Session::new(); + // Persisted IDs can be non-monotonic or duplicated: opening order cannot be sorted by ID. + for (name, id) in [("a", 300), ("b", 100), ("c", 300)] { + let mut doc = Document::new(name, photocraft_geom::Size::new(2, 2), photocraft_doc::ColorMode::Rgb, photocraft_color::SampleType::U8); + doc.id = photocraft_doc::DocId(id); + s.add_document(doc, None); + } + let a = s.documents()[0].doc.id; + let b = s.documents()[1].doc.id; + let c = s.documents()[2].doc.id; + assert_ne!(a, c, "admission replaces an already-open persisted ID"); + s.move_document(2, 0).unwrap(); // Visual tabs c, a, b; opening order a, b, c. + assert_eq!(s.execute("document.activate", json!({"offset": -1})).unwrap(), json!({"document": 2})); + assert_eq!(s.active().unwrap().doc.id, b); + let closed = s.close(2).unwrap(); // Closing the middle opening leaves a, c. + assert_eq!(closed.doc.id, b); + s.set_active(0); // c is visually first, but a is still its predecessor. + assert_eq!(s.execute("document.activate", json!({"offset": -1})).unwrap(), json!({"document": 1})); + assert_eq!(s.active().unwrap().doc.id, a); + s.add_document((*closed.doc).clone(), None); // Reopened b goes last, even with the same ID. + assert_eq!(s.active().unwrap().doc.id, b); + assert_eq!(s.execute("document.activate", json!({"offset": -1})).unwrap(), json!({"document": 0})); + assert_eq!(s.active().unwrap().doc.id, c); + assert!(s.close(99).is_none(), "invalid close must leave both orders alone"); + assert_eq!(s.execute("document.activate", json!({"offset": 1})).unwrap(), json!({"document": 2})); + assert_eq!(s.active().unwrap().doc.id, b); + while !s.documents().is_empty() { + s.close(0).unwrap(); + } + s.execute("file.new", json!({"width": 2, "height": 2, "name": "fresh"})).unwrap(); + assert_eq!(s.execute("document.activate", json!({"offset": 0})).unwrap(), json!({"document": 0})); + assert!(s.execute("document.activate", json!({"offset": -1})).is_err()); + assert!(s.execute("document.activate", json!({"offset": 1})).is_err()); + assert_eq!(s.active().unwrap().doc.name, "fresh"); } /// #2166, measured in Photoshop 25.1: a layer mask and the layer pixels keep separate diff --git a/docs/gaps.md b/docs/gaps.md index 9ea8dc9040..f250a7a58d 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where PhotoCraft falls short of Photoshop -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (G11: `.atn` steps that switch to another open document import, play and save back, #7103; G9: Preferences › File Handling › Save in Background takes effect, #6207, 24 of 169 preferences do nothing; G7: Content-Aware Move previews every selection the command takes, coarse then exact, TOOL-213-4 done, #6680; G4: Pattern Stamp and Background Eraser draw live, TOOL-213-7; G9: Animated Zoom pref read: holding the Zoom tool zooms continuously, #6210, prefs that do nothing 25 of 166; G2: a cancelled 24 MP Gaussian Blur job stops in at most 44 ms, P25 under its 200 ms budget in local runs, baseline not re-run, #6205; G1: generated large-file fixtures, a 4.38 GB PSB opens on Windows through io, FILE-216-2 done, #5107; G9: fill and adjustment layers copy and paste whole, #2344; G1: smart filter masks show on PSD open, Photoshop-oracle floor 188/258; G10: DPX read and write, #1049, Cineon read; G1: CMYK Hue/Saturation and Color Balance keep the black ink; G1: CMYK Posterize on the stored values, psd-tools oracle floor 242/309; G1: CMYK adjustment layers composite on the document's inks, Photoshop-oracle floor 165/258; G1: CMYK Brightness/Contrast on the inks, CMYK Threshold's luma; G1: modern Brightness/Contrast's brightness curve matches Photoshop, Photoshop-oracle floor 151/258; G1: Lab documents composite on their stored L, a, b, Photoshop-oracle floor 152/258; G1: Levels on whole levels at every step, Photoshop-oracle floor 146/258, psd-tools 238/309; G1: 32-bit Brightness/Contrast legacy, Channel Mixer, Photo Filter and Black & White tint unclipped as in Photoshop, Photoshop-oracle floor 149/258; G1: mask feathers blur with their exact variance, psd-tools oracle floor 240/309; G1: Threshold splits on Photoshop's luminosity, psd-tools oracle floor 241/309; G1: pass-through clipping base with an adjustment renders isolated, psd-tools oracle floor 240/309; G1: 16-bit Posterize as Photoshop bins it, psd-tools oracle floor 241/309; G1: Gradient Map driven by the W3C luminosity, Photoshop-oracle floor 147/258; G7: Field Blur's on-canvas pins, #3373; G4: Hue Jitter amount track, #3825 partial; G4: minimum field interaction, #3825 partial; G1: CMYK Invert, Posterize and Threshold on the inks, 16-bit Lab Invert; Photoshop-oracle floor 145/258; G11: `.atn` import maps 112 events: Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, nudges in points, Bitmap mode; G5: Free Transform H/V skew fields; G9: toolbar tool shortcut editing; refreshed against main: G2, G5, G7, G9, G10, G13, G14, G17 items that landed; G10: DPX read and write, #1049; G4: checkbox-only Brush Settings rows; G11: Load/Save Actions `.atn` landed, AUTO-219-2 partial; G5: hold-a-key spring-loaded tools landed, #2672; G1: #2465 fixed; stale missing-tool rows corrected: Color Replacement, Red Eye and Add/Delete Anchor Point are live tools, so only Art History Brush is missing; no estimate changed) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (G11: `.atn` document offsets preserve opening order after tab reordering, #7103; G9: Preferences › File Handling › Save in Background takes effect, #6207, 24 of 169 preferences do nothing; G7: Content-Aware Move previews every selection the command takes, coarse then exact, TOOL-213-4 done, #6680; G4: Pattern Stamp and Background Eraser draw live, TOOL-213-7; G9: Animated Zoom pref read: holding the Zoom tool zooms continuously, #6210, prefs that do nothing 25 of 166; G2: a cancelled 24 MP Gaussian Blur job stops in at most 44 ms, P25 under its 200 ms budget in local runs, baseline not re-run, #6205; G1: generated large-file fixtures, a 4.38 GB PSB opens on Windows through io, FILE-216-2 done, #5107; G9: fill and adjustment layers copy and paste whole, #2344; G1: smart filter masks show on PSD open, Photoshop-oracle floor 188/258; G10: DPX read and write, #1049, Cineon read; G1: CMYK Hue/Saturation and Color Balance keep the black ink; G1: CMYK Posterize on the stored values, psd-tools oracle floor 242/309; G1: CMYK adjustment layers composite on the document's inks, Photoshop-oracle floor 165/258; G1: CMYK Brightness/Contrast on the inks, CMYK Threshold's luma; G1: modern Brightness/Contrast's brightness curve matches Photoshop, Photoshop-oracle floor 151/258; G1: Lab documents composite on their stored L, a, b, Photoshop-oracle floor 152/258; G1: Levels on whole levels at every step, Photoshop-oracle floor 146/258, psd-tools 238/309; G1: 32-bit Brightness/Contrast legacy, Channel Mixer, Photo Filter and Black & White tint unclipped as in Photoshop, Photoshop-oracle floor 149/258; G1: mask feathers blur with their exact variance, psd-tools oracle floor 240/309; G1: Threshold splits on Photoshop's luminosity, psd-tools oracle floor 241/309; G1: pass-through clipping base with an adjustment renders isolated, psd-tools oracle floor 240/309; G1: 16-bit Posterize as Photoshop bins it, psd-tools oracle floor 241/309; G1: Gradient Map driven by the W3C luminosity, Photoshop-oracle floor 147/258; G7: Field Blur's on-canvas pins, #3373; G4: Hue Jitter amount track, #3825 partial; G4: minimum field interaction, #3825 partial; G1: CMYK Invert, Posterize and Threshold on the inks, 16-bit Lab Invert; Photoshop-oracle floor 145/258; G11: `.atn` import maps 112 events: Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, nudges in points, Bitmap mode; G5: Free Transform H/V skew fields; G9: toolbar tool shortcut editing; refreshed against main: G2, G5, G7, G9, G10, G13, G14, G17 items that landed; G10: DPX read and write, #1049; G4: checkbox-only Brush Settings rows; G11: Load/Save Actions `.atn` landed, AUTO-219-2 partial; G5: hold-a-key spring-loaded tools landed, #2672; G1: #2465 fixed; stale missing-tool rows corrected: Color Replacement, Red Eye and Add/Delete Anchor Point are live tools, so only Art History Brush is missing; no estimate changed) · **Target:** Adobe Photoshop 2026 Every known shortfall against Photoshop 2026 (27.11.0), one entry each, ranked by user impact on the way to beta. This is the work list agents pick from: take the highest-ranked entry that @@ -107,10 +107,19 @@ what an agent can't finish alone. Duplicate Layers into a new document, Bitmap halftone screens…) load as kept Photoshop steps that fail at playback by name. - **Done 2026-10-19:** `.atn` steps that switch to another open document (a `select` of `Dcmn` by - offset: the previous, next or Nth tab from the active one) import as `document.activate {"offset"}`, + offset: the previous, next or Nth open document from the active one) import as `document.activate {"offset"}`, play and save back in the same shape, so actions that copy from one open document and paste into another no longer stop at the switch (#7103). An offset past the first or last open document is refused, not wrapped. +- **2026-10-22 correction:** document offsets follow opening/creation order, independently of + dragged tab order. Closing removes an entry; reopening appends it. Absolute document indices + and command results still use tab order. Evidence is the firsthand behavior reports in + [Adobe's 2016 tab-order discussion](https://community.adobe.com/questions-712/how-select-real-next-document-1100886) + and [2021 action-order discussion](https://community.adobe.com/questions-712/how-to-change-the-order-of-open-documents-for-an-action-1129531), + corroborated by a [reported Dcmn offset test](https://community.adobe.com/questions-712/how-to-select-the-document-just-back-of-the-active-document-1102704). + This is a behavioral inference from those reports, not a new live Photoshop reproduction or + an official format guarantee. The existing out-of-range rejection is unchanged. Regression + tests cover tab reordering, close/reopen, imported document IDs and `.atn` copy/paste playback. - **Done 2026-10-12:** Load/Save Actions `.atn` in the panel menu, `batch --actions set.atn` and `actions.load`/`actions.save`; 112 Photoshop events map to commands both ways (Transform, Apply Image, Save for Web, smart-object edits, Gradient Map, Quick Mask, polygon selections, @@ -295,6 +304,7 @@ what an agent can't finish alone. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | G11: document offsets use opening order despite tab reordering; lifecycle and `.atn` playback regressions; behavioral source evidence, no new live Photoshop reproduction | | 2026-10-19 | minor | G11: `.atn` steps that select another open document by offset import as `document.activate {"offset"}`, play and save back (#7103); estimate unchanged | | 2026-10-15 | minor | G1 / #5972 A1: render only provably constant-RGB `GdFl` noise fills; variable noise synthesis remains open | | 2026-10-18 | minor | G9: with no selection, Copy takes fill and adjustment layers whole and Paste recreates them (#2344); UI-217-23 stays partial (a single pixel layer copies its pixels only); estimate unchanged |