This commit is contained in:
parent
6e9ba6511d
commit
6f6d2ea467
|
|
@ -41,6 +41,17 @@ pub fn previews(doc: &Document) -> SaveOptions {
|
|||
SaveOptions { thumbnail: Some(photocraft_compose::thumbnail(doc, 256)), composite: Some(photocraft_compose::thumbnail(doc, 1024)) }
|
||||
}
|
||||
|
||||
/// Encode the name the saved native bundle will report on reopening. Save As updates
|
||||
/// the session identity only *after* a successful write; without a separate snapshot
|
||||
/// the manifest serializes the previous name instead.
|
||||
fn named_for_output(doc: &Document, path: &str) -> Document {
|
||||
let mut named = doc.clone();
|
||||
if let Some(name) = path.rsplit(['/', '\\']).next().filter(|name| !name.is_empty()) {
|
||||
named.name = name.to_owned();
|
||||
}
|
||||
named
|
||||
}
|
||||
|
||||
/// Save or export `doc` to `path`, choosing the format from the extension
|
||||
/// (or `format_override`, an extension such as `"png"`). `writer` makes
|
||||
/// repeated `.pcraft` saves incremental. Returns warnings about lost data.
|
||||
|
|
@ -56,9 +67,10 @@ pub fn save(
|
|||
.or_else(|| path.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()))
|
||||
.ok_or_else(|| AutomationError::BadRequest(format!("cannot tell the format of `{}`; pass a format", path.display())))?;
|
||||
if ext == photocraft_format::EXTENSION {
|
||||
let named = named_for_output(doc, &path.to_string_lossy());
|
||||
let mut local = PcraftWriter::new();
|
||||
let w = writer.unwrap_or(&mut local);
|
||||
w.save_path(doc, path, &previews(doc))?;
|
||||
w.save_path(&named, path, &previews(&named))?;
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let r = photocraft_io::export(doc, &ext, opts)?;
|
||||
|
|
@ -75,7 +87,8 @@ pub fn save_bytes(doc: &Document, name: &str, format_override: Option<&str>, opt
|
|||
.or_else(|| Path::new(name).extension().map(|extension| extension.to_string_lossy().to_ascii_lowercase()))
|
||||
.ok_or_else(|| AutomationError::BadRequest(format!("cannot tell the format of `{name}`; pass a format")))?;
|
||||
if ext == photocraft_format::EXTENSION {
|
||||
return Ok((photocraft_format::save_to_bytes(doc, &previews(doc))?, Vec::new()));
|
||||
let named = named_for_output(doc, name);
|
||||
return Ok((photocraft_format::save_to_bytes(&named, &previews(&named))?, Vec::new()));
|
||||
}
|
||||
let result = photocraft_io::export(doc, &ext, opts)?;
|
||||
Ok((result.bytes, result.warnings))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
//! Save As must store the new filename in the native manifest, not just the in-session tab name.
|
||||
|
||||
use photocraft_automation::{Headless, files};
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn native_save_as_reopens_with_the_written_filename() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"pc-native-save-name-{}-{}",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
|
||||
));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let first = dir.join("FirstTest.pcraft");
|
||||
let second = dir.join("SecondTest.pcraft");
|
||||
let mut headless = Headless::trusted_local();
|
||||
headless.handle("doc.new", json!({"width": 8, "height": 8, "name": "Old Title"})).unwrap();
|
||||
headless.handle("doc.save", json!({"path": first.to_str().unwrap()})).unwrap();
|
||||
assert_eq!(files::open(&first).unwrap().document.name, "FirstTest.pcraft");
|
||||
headless.handle("doc.save", json!({"path": second.to_str().unwrap()})).unwrap();
|
||||
assert_eq!(headless.session.active().unwrap().doc.name, "SecondTest.pcraft");
|
||||
assert_eq!(files::open(&second).unwrap().document.name, "SecondTest.pcraft");
|
||||
// Save back in place and retain the correct title after another edit.
|
||||
headless.handle("engine.execute", json!({"command":"image.adjustments.invert", "params":{}})).unwrap();
|
||||
headless.handle("doc.save", json!({})).unwrap();
|
||||
assert_eq!(files::open(&second).unwrap().document.name, "SecondTest.pcraft");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workspace_encoded_native_bundle_uses_requested_name_without_mutating_source() {
|
||||
let mut headless = Headless::trusted_local();
|
||||
headless.handle("doc.new", json!({"width": 8, "height": 8, "name": "Original"})).unwrap();
|
||||
let doc = &headless.session.active().unwrap().doc;
|
||||
let (bytes, warnings) = files::save_bytes(doc, "exports/Renamed.pcraft", None, &Default::default()).unwrap();
|
||||
assert!(warnings.is_empty());
|
||||
let reopened = files::open_bytes("Renamed.pcraft", &bytes).unwrap().document;
|
||||
assert_eq!(reopened.name, "Renamed.pcraft");
|
||||
assert_eq!(doc.name, "Original", "a copy should not retarget the open document");
|
||||
}
|
||||
Loading…
Reference in New Issue