From 11aa5831293fd5b36c9716f3af0103a6c8403d6b Mon Sep 17 00:00:00 2001 From: yoriswind Date: Mon, 30 Nov 2026 13:51:55 -0800 Subject: [PATCH 1/2] Avoid empty Update All Text Layers history steps --- crates/engine-core/src/cmd/type_extra_cmds.rs | 57 ++++++++++--- .../src/cmd/type_update_all_layers_tests.rs | 80 +++++++++++++++++++ 2 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 crates/engine-core/src/cmd/type_update_all_layers_tests.rs diff --git a/crates/engine-core/src/cmd/type_extra_cmds.rs b/crates/engine-core/src/cmd/type_extra_cmds.rs index c3ce98effe2..950f44233e1 100644 --- a/crates/engine-core/src/cmd/type_extra_cmds.rs +++ b/crates/engine-core/src/cmd/type_extra_cmds.rs @@ -816,19 +816,46 @@ fn paste_lorem(s: &mut Session, p: &Value) -> Result { } fn update_all(s: &mut Session) -> Result { - let mut n = 0; - s.edit("Update All Text Layers", |doc, _| { - let (dpi, format) = (doc.resolution_dpi, doc.pixel_format()); - let ids: Vec = doc.walk().into_iter().filter(|(_, _, l)| matches!(l.content, LayerContent::Text(_))).map(|(_, _, l)| l.id).collect(); - for id in ids { - if let Some(Layer { content: LayerContent::Text(t), .. }) = doc.layer_mut(id) { - refresh_at(dpi, format, t); - n += 1; - } + let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; + let mut texts = doc + .walk() + .into_iter() + .filter_map(|(_, _, l)| match &l.content { + LayerContent::Text(text) => Some((l.id, text)), + _ => None, + }) + .peekable(); + if texts.peek().is_none() { + return Ok(json!({"updated": 0})); + } + let generation = photocraft_raster::spill::read_error_generation(); + doc.scratch_health().map_err(EngineError::Other)?; + let (dpi, format) = (doc.resolution_dpi, doc.pixel_format()); + let mut count = 0; + let mut changes = Vec::new(); + for (id, text) in texts { + let mut refreshed = text.clone(); + refresh_at(dpi, format, &mut refreshed); + // An identical render keeps its existing PSD block, including imported metadata. + // A missing block can still be restored even if its pixels were already current. + if text.cache != refreshed.cache || (text.psd_raw.is_none() && refreshed.psd_raw.is_some()) { + changes.push((id, refreshed.cache, refreshed.psd_raw)); } - Ok(()) - })?; - Ok(json!({"updated": n})) + count += 1; + } + photocraft_raster::spill::check_since(generation).and_then(|()| doc.scratch_health()).map_err(EngineError::Other)?; + if !changes.is_empty() { + s.edit("Update All Text Layers", |doc, _| { + for (id, cache, psd_raw) in changes { + if let Some(Layer { content: LayerContent::Text(text), .. }) = doc.layer_mut(id) { + text.cache = cache; + text.psd_raw = psd_raw; + } + } + Ok(()) + })?; + } + Ok(json!({"updated": count})) } fn style_font_missing(fonts: &mut photocraft_text::FontDb, style: &photocraft_doc::text::CharStyle) -> bool { @@ -1098,7 +1125,7 @@ pub fn specs() -> Vec { set_path_placement ), spec!("type.detachFromPath", "Release Type from Path", &[], LP, is_path_text, detach_from_path), - spec!("type.updateAllTextLayers", "Update All Text Layers", &["Type"], "{}", any_text, |s, _| update_all(s)), + spec!("type.updateAllTextLayers", "Update All Text Layers", &["Type"], "{}", has_doc, |s, _| update_all(s)), spec!("type.replaceAllMissingFonts", "Replace All Missing Fonts", &["Type"], "{} (with the default family)", any_text, |s, _| replace_fonts( s, &serde_json::Map::new(), @@ -1141,3 +1168,7 @@ mod tests; #[cfg(test)] #[path = "type_anti_alias_history_tests.rs"] mod anti_alias_history_tests; + +#[cfg(test)] +#[path = "type_update_all_layers_tests.rs"] +mod update_all_layers_tests; diff --git a/crates/engine-core/src/cmd/type_update_all_layers_tests.rs b/crates/engine-core/src/cmd/type_update_all_layers_tests.rs new file mode 100644 index 00000000000..843fdc82607 --- /dev/null +++ b/crates/engine-core/src/cmd/type_update_all_layers_tests.rs @@ -0,0 +1,80 @@ +use serde_json::json; + +use super::*; + +const UPDATE: &str = "type.updateAllTextLayers"; + +fn create_text(session: &mut Session, text: &str, y: i32) -> LayerId { + LayerId(session.execute("type.create", json!({"text": text, "x": 6, "y": y})).unwrap()["layer"].as_u64().unwrap()) +} + +fn text(session: &Session, id: LayerId) -> &TextLayer { + let LayerContent::Text(text) = &session.active().unwrap().doc.layer(id).unwrap().content else { panic!("expected text") }; + text +} + +fn assert_clean_refresh(session: &mut Session, visited: usize) { + session.saved_to("type-update.psd".into()); + let before = session.active().unwrap(); + let doc = before.doc.clone(); + let revision = before.revision; + let steps = before.history.past_len(); + assert_eq!(session.execute(UPDATE, json!({})).unwrap()["updated"], json!(visited)); + let after = session.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&doc, &after.doc)); + assert_eq!(after.history.past_len(), steps); + assert_eq!(after.revision, revision); + assert!(!after.is_dirty()); +} + +#[test] +fn update_all_is_available_without_text_and_keeps_healthy_layers_clean() { + let mut session = Session::new(); + assert!(!session.is_enabled(UPDATE)); + session.execute("file.new", json!({"width": 256, "height": 128, "background": "white"})).unwrap(); + assert!(session.is_enabled(UPDATE)); + assert_clean_refresh(&mut session, 0); + + create_text(&mut session, "First text", 24); + create_text(&mut session, "Second text", 54); + assert!(session.is_enabled(UPDATE)); + assert_clean_refresh(&mut session, 2); + session.execute("select.deselectLayers", json!({})).unwrap(); + assert!(session.is_enabled(UPDATE)); + assert_clean_refresh(&mut session, 2); +} + +#[test] +fn update_all_repairs_stale_text_in_one_undo_step() { + let mut session = Session::new(); + session.execute("file.new", json!({"width": 256, "height": 128, "background": "white"})).unwrap(); + let a = create_text(&mut session, "Existing text", 24); + let b = create_text(&mut session, "Text needing layout", 54); + let current = text(&session, b).cache.clone().unwrap(); + let missing = text(&session, b).psd_raw.clone().unwrap(); + let healthy = text(&session, a).psd_raw.clone().unwrap(); + session + .edit("Stale text fixture", |doc, _| { + let LayerContent::Text(text) = &mut doc.layer_mut(b).unwrap().content else { panic!("expected text") }; + text.cache = None; + text.psd_raw = None; + Ok(()) + }) + .unwrap(); + session.saved_to("type-update.psd".into()); + let prior_steps = session.active().unwrap().history.past_len(); + + assert_eq!(session.execute(UPDATE, json!({})).unwrap()["updated"], 2); + let updated = session.active().unwrap(); + assert!(updated.is_dirty()); + assert_eq!(updated.history.past_len(), prior_steps + 1); + assert_eq!(updated.history.undo_label(), Some("Update All Text Layers")); + assert_eq!(text(&session, b).cache.as_ref(), Some(¤t)); + assert_eq!(text(&session, b).psd_raw.as_ref(), Some(&missing)); + assert!(std::sync::Arc::ptr_eq(&healthy, text(&session, a).psd_raw.as_ref().unwrap())); + + session.execute("edit.undo", json!({})).unwrap(); + assert!(text(&session, b).cache.is_none()); + assert!(text(&session, b).psd_raw.is_none()); + assert_eq!(session.active().unwrap().history.past_len(), prior_steps); +} From 99f58639708cf54a36e2eb3c012c15920604e413 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Mon, 30 Nov 2026 14:31:13 -0800 Subject: [PATCH 2/2] Initialize command registry in text refresh tests --- crates/engine-core/src/cmd/type_update_all_layers_tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/engine-core/src/cmd/type_update_all_layers_tests.rs b/crates/engine-core/src/cmd/type_update_all_layers_tests.rs index 843fdc82607..fdf55e1d4f1 100644 --- a/crates/engine-core/src/cmd/type_update_all_layers_tests.rs +++ b/crates/engine-core/src/cmd/type_update_all_layers_tests.rs @@ -29,7 +29,7 @@ fn assert_clean_refresh(session: &mut Session, visited: usize) { #[test] fn update_all_is_available_without_text_and_keeps_healthy_layers_clean() { - let mut session = Session::new(); + let mut session = crate::session(); assert!(!session.is_enabled(UPDATE)); session.execute("file.new", json!({"width": 256, "height": 128, "background": "white"})).unwrap(); assert!(session.is_enabled(UPDATE)); @@ -46,7 +46,7 @@ fn update_all_is_available_without_text_and_keeps_healthy_layers_clean() { #[test] fn update_all_repairs_stale_text_in_one_undo_step() { - let mut session = Session::new(); + let mut session = crate::session(); session.execute("file.new", json!({"width": 256, "height": 128, "background": "white"})).unwrap(); let a = create_text(&mut session, "Existing text", 24); let b = create_text(&mut session, "Text needing layout", 54);