mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge #18540 into the merge queue batch
Avoid empty Update All Text Layers history steps (#18520)
This commit is contained in:
commit
9ad0253e64
2 files changed
+124
-13
No files matched your search
@@ -816,19 +816,46 @@ fn paste_lorem(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
fn update_all(s: &mut Session) -> Result<Value> {
|
||||
let mut n = 0;
|
||||
s.edit("Update All Text Layers", |doc, _| {
|
||||
let (dpi, format) = (doc.resolution_dpi, doc.pixel_format());
|
||||
let ids: Vec<LayerId> = 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 {
|
||||
@@ -1104,7 +1131,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
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(),
|
||||
@@ -1144,6 +1171,10 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
#[path = "type_extra_cmds/tests.rs"]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "type_update_all_layers_tests.rs"]
|
||||
mod update_all_layers_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "type_anti_alias_history_tests.rs"]
|
||||
mod anti_alias_history_tests;
|
||||
|
||||
@@ -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 = crate::session();
|
||||
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 = 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);
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user