From 55673a1e490c2b65a4a951ff9fcc3303302a7120 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 27 Oct 2026 01:42:42 +0100 Subject: [PATCH] Address CodeRabbit review: pick the first snapshot's type layers on their own (#6087) Served fonts chose the layers to redraw from the open document only, so the first History snapshot was refreshed only for layers the open document still had, and with the open layer's "shows our own layout" verdict. A type layer deleted since the document opened kept its fallback-font pixels in the snapshot, and a PSD layer edited since lost the file's pixels there. `served_fonts::pick` now checks the snapshot (when it no longer is the open document) on its own, and `Session::refresh_type_layers` takes `TypeLayerRef`s that say which version each layer was picked in. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lib.rs | 28 ++++++++---- crates/engine/src/type_cmds.rs | 38 +++++++++++++--- crates/ui-egui/src/served_fonts.rs | 69 ++++++++++++++++++++++++------ 3 files changed, 107 insertions(+), 28 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 52f065b5fb5..70e2207cc29 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -255,6 +255,16 @@ pub struct DocState { pub(crate) source_snapshot: Option, } +/// A type layer for [`Session::refresh_type_layers`]: in the open document, or in its first +/// History snapshot (the document as opened) when `first_snapshot`. The caller picks each against +/// that version of the layer, which may differ from the open one or be gone from it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct TypeLayerRef { + pub doc: photocraft_doc::DocId, + pub layer: LayerId, + pub first_snapshot: bool, +} + /// `doc` with its type layers among `ids` re-rendered (`Session::refresh_type_layers`), or `None` /// when none of them is a type layer of `doc`. fn refresh_text_layers(doc: &Arc, ids: &[LayerId]) -> Option { @@ -1019,33 +1029,33 @@ impl Session { /// unchanged, so this is no history step and leaves a clean document clean. Unknown documents /// and layers are skipped. A document a background job is computing from must not move under /// it: its layers are returned, to try again later. - pub fn refresh_type_layers(&mut self, layers: &[(photocraft_doc::DocId, LayerId)]) -> Vec<(photocraft_doc::DocId, LayerId)> { + pub fn refresh_type_layers(&mut self, layers: &[TypeLayerRef]) -> Vec { let mut busy = Vec::new(); let mut docs: Vec = Vec::new(); - for (d, _) in layers { - if !docs.contains(d) { - docs.push(*d); + for l in layers { + if !docs.contains(&l.doc) { + docs.push(l.doc); } } for doc_id in docs { - let mine: Vec = layers.iter().filter(|(d, _)| *d == doc_id).map(|(_, id)| *id).collect(); if self.job_on(doc_id).is_some() { - busy.extend(mine.iter().map(|id| (doc_id, *id))); + busy.extend(layers.iter().filter(|l| l.doc == doc_id)); continue; } + let ids = |first: bool| -> Vec { layers.iter().filter(|l| l.doc == doc_id && l.first_snapshot == first).map(|l| l.layer).collect() }; let Some(st) = self.docs.iter_mut().find(|st| st.doc.id == doc_id) else { continue }; // The first snapshot is the document as opened (#2966, #6087): it was taken before the // font arrived, so its type layers get the same refresh, or returning to it brings the // fallback-font pixels back. While it still is the open document, it shares the - // refreshed one. + // refreshed one; once edited, it is refreshed on its own, layers the edit removed too. let first_is_doc = st.snapshots.iter().any(|s| s.id == 0 && Arc::ptr_eq(&s.doc, &st.doc)); if !first_is_doc && let Some(first) = st.snapshots.iter_mut().find(|s| s.id == 0) - && let Some(doc) = refresh_text_layers(&first.doc, &mine) + && let Some(doc) = refresh_text_layers(&first.doc, &ids(true)) { first.doc = Arc::new(doc); } - let Some(doc) = refresh_text_layers(&st.doc, &mine) else { continue }; + let Some(doc) = refresh_text_layers(&st.doc, &ids(false)) else { continue }; let doc = Arc::new(doc); if first_is_doc && let Some(first) = st.snapshots.iter_mut().find(|s| s.id == 0) { first.doc = doc.clone(); diff --git a/crates/engine/src/type_cmds.rs b/crates/engine/src/type_cmds.rs index c5840cac6ce..ffa4bb11bb4 100644 --- a/crates/engine/src/type_cmds.rs +++ b/crates/engine/src/type_cmds.rs @@ -1291,8 +1291,9 @@ mod tests { st.saved_revision = st.revision; } let rev = s.active().unwrap().revision; - let unknown = [(doc_id, LayerId(u64::MAX)), (photocraft_doc::DocId(u64::MAX), id)]; - assert!(s.refresh_type_layers(&[&[(doc_id, id)][..], &unknown].concat()).is_empty()); + let layers = [(doc_id, id), (doc_id, LayerId(u64::MAX)), (photocraft_doc::DocId(u64::MAX), id)]; + let layers: Vec<_> = layers.iter().map(|&(doc, layer)| crate::TypeLayerRef { doc, layer, first_snapshot: false }).collect(); + assert!(s.refresh_type_layers(&layers).is_empty()); assert!(text_layer(&s, id.0).cache.is_some()); let st = s.active().unwrap(); assert!(st.revision > rev); @@ -1306,6 +1307,7 @@ mod tests { /// the refresh re-renders its type layers too, so returning to it keeps the real font (#6087). #[test] fn refreshing_type_layers_refreshes_the_first_snapshot() { + use crate::TypeLayerRef; let mut s = session(); let id = LayerId(s.execute("type.create", json!({"text": "Hello", "size": 24})).unwrap()["layer"].as_u64().unwrap()); let mut stale = (*s.active().unwrap().doc).clone(); @@ -1315,26 +1317,52 @@ mod tests { LayerContent::Text(t) => t.cache.is_some(), other => panic!("{}", other.kind_name()), }; + let open = |doc, layer| TypeLayerRef { doc, layer, first_snapshot: false }; + let first = |doc, layer| TypeLayerRef { doc, layer, first_snapshot: true }; // Opened with the fallback font: the first snapshot is the open document itself. s.add_document(stale.clone(), None); let doc_id = s.active().unwrap().doc.id; assert!(!snap_cache(&s)); - assert!(s.refresh_type_layers(&[(doc_id, id)]).is_empty()); + assert!(s.refresh_type_layers(&[open(doc_id, id)]).is_empty()); assert!(text_layer(&s, id.0).cache.is_some()); assert!(snap_cache(&s), "the first snapshot is refreshed with the document"); let st = s.active().unwrap(); assert!(Arc::ptr_eq(&st.snapshots[0].doc, &st.doc), "and still shares it"); // Edited since it opened: the snapshot is refreshed on its own, the edit stays. - s.add_document(stale, None); + s.add_document(stale.clone(), None); let doc_id = s.active().unwrap().doc.id; s.execute("layer.new.layer", json!({})).unwrap(); let layers = s.active().unwrap().doc.layers.len(); - assert!(s.refresh_type_layers(&[(doc_id, id), (doc_id, LayerId(9999))]).is_empty()); + assert!(s.refresh_type_layers(&[open(doc_id, id), first(doc_id, id), first(doc_id, LayerId(9999))]).is_empty()); assert!(snap_cache(&s)); assert!(text_layer(&s, id.0).cache.is_some()); let st = s.active().unwrap(); assert_eq!(st.doc.layers.len(), layers); assert_eq!(st.snapshots[0].doc.layers.len(), layers - 1, "the snapshot keeps the document as opened"); + // Each version is refreshed only for the layers picked in it: one picked in the open + // document (e.g. a PSD layer edited since, while the snapshot keeps the file's pixels) + // leaves the snapshot alone, and the other way round. + s.add_document(stale.clone(), None); + let doc_id = s.active().unwrap().doc.id; + s.execute("layer.new.layer", json!({})).unwrap(); + assert!(s.refresh_type_layers(&[open(doc_id, id)]).is_empty()); + assert!(text_layer(&s, id.0).cache.is_some()); + assert!(!snap_cache(&s), "the snapshot was not picked"); + s.add_document(stale.clone(), None); + let doc_id = s.active().unwrap().doc.id; + s.execute("layer.new.layer", json!({})).unwrap(); + let rev = s.active().unwrap().revision; + assert!(s.refresh_type_layers(&[first(doc_id, id)]).is_empty()); + assert!(snap_cache(&s)); + assert!(text_layer(&s, id.0).cache.is_none(), "the open layer was not picked"); + assert_eq!(s.active().unwrap().revision, rev, "and the open document did not change"); + // The layer is gone from the open document: the snapshot still gets the real font. + s.add_document(stale, None); + let doc_id = s.active().unwrap().doc.id; + s.execute("layer.delete", json!({"layer": id.0})).unwrap(); + assert!(s.active().unwrap().doc.layer(id).is_none()); + assert!(s.refresh_type_layers(&[first(doc_id, id)]).is_empty()); + assert!(snap_cache(&s), "a layer deleted since it opened is refreshed in the snapshot"); } #[test] diff --git a/crates/ui-egui/src/served_fonts.rs b/crates/ui-egui/src/served_fonts.rs index b7ae085f41e..142cedd7daf 100644 --- a/crates/ui-egui/src/served_fonts.rs +++ b/crates/ui-egui/src/served_fonts.rs @@ -2,15 +2,16 @@ //! see `photocraft_text::served`): reading the manifest, the families to fetch, and installing //! what arrived, which registers the files and re-renders the type layers drawn without them. -use std::sync::{Mutex, PoisonError}; +use std::sync::{Arc, Mutex, PoisonError}; -use photocraft_doc::{DocId, LayerContent, LayerId, TextLayer}; +use photocraft_doc::{LayerContent, TextLayer}; +use photocraft_engine::TypeLayerRef; pub use photocraft_text::served::{ServedFont, add_families, add_fonts, is_relative_path, parse_manifest, request_for_languages, take_requests}; use crate::PhotocraftApp; /// Layers whose document was busy with a background job when their font arrived. -static PENDING: Mutex> = Mutex::new(Vec::new()); +static PENDING: Mutex> = Mutex::new(Vec::new()); /// Registers delivered font files (`(family, bytes)`, the family as the manifest lists it) and /// re-renders the type layers that use those families, or whose text falls back to them (a script @@ -23,17 +24,7 @@ pub fn install(app: &mut PhotocraftApp, delivered: Vec<(String, Vec)>) { let families: Vec = delivered.iter().map(|(f, _)| f.clone()).collect(); // Decided before the fonts are in: "shows our own layout" compares the layer's pixels // with what the engine draws now, that is with the fallback font. - for st in app.session.documents() { - for (_, _, l) in st.doc.walk() { - if let LayerContent::Text(t) = &l.content - && redraws_for(t, &families) - && crate::type_tool::shows_own_layout(&st.doc, t) - && !layers.contains(&(st.doc.id, l.id)) - { - layers.push((st.doc.id, l.id)); - } - } - } + pick(&app.session, &families, &mut layers); let mut eng = photocraft_text::shared().lock().unwrap_or_else(PoisonError::into_inner); for (family, bytes) in delivered { let added = eng.fonts.register_font_data(bytes); @@ -48,6 +39,28 @@ pub fn install(app: &mut PhotocraftApp, delivered: Vec<(String, Vec)>) { } } +/// Adds to `layers` the type layers to redraw once `families` arrive, in every open document and +/// in its first History snapshot (the document as opened), which is checked on its own: since +/// then its layers may have been edited (a PSD layer re-rendered) or deleted (#6087). +fn pick(session: &photocraft_engine::Session, families: &[String], layers: &mut Vec) { + for st in session.documents() { + // While it still is the open document, the engine refreshes it with it. + let first = st.snapshots.iter().find(|s| s.id == 0 && !Arc::ptr_eq(&s.doc, &st.doc)).map(|s| &s.doc); + for (doc, first_snapshot) in std::iter::once((&st.doc, false)).chain(first.map(|d| (d, true))) { + for (_, _, l) in doc.walk() { + let key = TypeLayerRef { doc: st.doc.id, layer: l.id, first_snapshot }; + if let LayerContent::Text(t) = &l.content + && redraws_for(t, families) + && crate::type_tool::shows_own_layout(doc, t) + && !layers.contains(&key) + { + layers.push(key); + } + } + } + } +} + /// Should the layer be redrawn when `families` arrive: it uses one of them, or its text is in a /// script one of them is the served fallback for? fn redraws_for(t: &TextLayer, families: &[String]) -> bool { @@ -79,6 +92,34 @@ mod tests { assert!(uses_family(&psd, &["Lato".into()])); } + /// #6087: the first History snapshot is picked on its own, so a type layer deleted since the + /// document opened still gets the font there, and one still shared with the open document is + /// picked once. + #[test] + fn the_first_snapshot_is_picked_on_its_own() { + use photocraft_doc::LayerId; + use serde_json::json; + let family = "Served Test Pick"; + let mut s = photocraft_engine::Session::new(); + s.execute("file.new", json!({"width": 200, "height": 100, "background": "transparent"})).unwrap(); + let id = LayerId(s.execute("type.create", json!({"text": "Hello", "size": 24, "font": family})).unwrap()["layer"].as_u64().unwrap()); + let doc = (*s.active().unwrap().doc).clone(); + s.add_document(doc, None); + let doc_id = s.active().unwrap().doc.id; + let key = |first_snapshot| TypeLayerRef { doc: doc_id, layer: id, first_snapshot }; + let picked = |s: &photocraft_engine::Session| { + let mut layers = Vec::new(); + pick(s, &[family.into()], &mut layers); + layers.retain(|l| l.doc == doc_id); + layers + }; + assert_eq!(picked(&s), [key(false)], "the snapshot still is the open document"); + s.execute("layer.new.layer", json!({})).unwrap(); + assert_eq!(picked(&s), [key(false), key(true)]); + s.execute("layer.delete", json!({"layer": id.0})).unwrap(); + assert_eq!(picked(&s), [key(true)], "deleted from the open document, still in the snapshot"); + } + #[test] fn a_layer_in_a_script_redraws_when_that_scripts_fallback_arrives() { // Thai: other tests use other scripts (the served catalog is process-wide).