mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge #15063 into the merge queue batch
engine: name a duplicated new document Untitled-N (#14608)
This commit is contained in:
commit
e9fd539305
5 files changed
+60
-3
No files matched your search
@@ -74,6 +74,8 @@ pub mod layer_copy_cmds;
|
||||
#[cfg(test)]
|
||||
mod layer_duplicate_new_document_history_tests;
|
||||
#[cfg(test)]
|
||||
mod layer_duplicate_new_document_name_tests;
|
||||
#[cfg(test)]
|
||||
mod layer_from_background_name_tests;
|
||||
pub mod layer_label_cmds;
|
||||
pub mod layer_list_cmds;
|
||||
|
||||
@@ -370,7 +370,12 @@ pub(crate) fn duplicate_to_new_document(s: &mut Session, p: &Value) -> Result<Va
|
||||
if !layered(sdoc.mode) {
|
||||
return Err(EngineError::Other(format!("layers can't be duplicated with a {:?} document", sdoc.mode)));
|
||||
}
|
||||
let doc_name = p.get("documentName").and_then(Value::as_str).filter(|n| !n.is_empty()).map(str::to_string).unwrap_or_else(|| format!("{} copy", sdoc.name));
|
||||
let doc_name = p
|
||||
.get("documentName")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|n| !n.is_empty())
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| crate::cmd::file_cmds::untitled_name("Untitled", |n| s.documents().iter().any(|d| d.doc.name == n) || s.held_open_named(n)));
|
||||
let mut nd = Document::new(doc_name, sdoc.size, sdoc.mode, sdoc.depth);
|
||||
nd.resolution_dpi = sdoc.resolution_dpi;
|
||||
nd.icc_profile.clone_from(&sdoc.icc_profile);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
//! #14608: Duplicate Layer… with Document = New and an empty Name makes an "Untitled-N"
|
||||
//! document, as the reference application does, not "<source> copy".
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn doc_name(s: &Session, i: usize) -> String {
|
||||
s.documents().get(i).map(|d| d.doc.name.clone()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// A one-layer document named `name`, with its only layer active.
|
||||
fn source(name: &str) -> Session {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"name": name, "width": 40, "height": 30, "background": "white"})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_name_makes_the_next_untitled_document() {
|
||||
let mut s = source("oracle");
|
||||
s.execute("layer.duplicate", json!({"document": "new"})).unwrap();
|
||||
assert_eq!(doc_name(&s, 0), "oracle", "the source keeps its name");
|
||||
assert_eq!(doc_name(&s, 1), "Untitled-1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_counter_skips_untitled_documents_already_open() {
|
||||
let mut s = source("oracle");
|
||||
// An Untitled-1 is open beside the source; the copy takes the next number.
|
||||
s.execute("file.new", json!({"name": "Untitled-1", "width": 8, "height": 8})).unwrap();
|
||||
let oracle = s.documents().iter().position(|d| d.doc.name == "oracle").unwrap();
|
||||
s.set_active(oracle);
|
||||
s.execute("layer.duplicate", json!({"document": "new"})).unwrap();
|
||||
assert_eq!(doc_name(&s, s.active_index().unwrap()), "Untitled-2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_typed_name_is_used_as_given() {
|
||||
let mut s = source("oracle");
|
||||
s.execute("layer.duplicate", json!({"document": "new", "documentName": "MyNew"})).unwrap();
|
||||
assert_eq!(doc_name(&s, 1), "MyNew");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_typed_name_falls_back_to_untitled() {
|
||||
let mut s = source("oracle");
|
||||
s.execute("layer.duplicate", json!({"document": "new", "documentName": ""})).unwrap();
|
||||
assert_eq!(doc_name(&s, 1), "Untitled-1");
|
||||
}
|
||||
@@ -232,7 +232,7 @@ fn duplicate_to_a_new_document_keeps_size_pixels_and_place_at_every_depth() {
|
||||
assert_eq!(*src, source_before, "the source is untouched");
|
||||
assert_eq!(dst.size, src.size, "same canvas, as Photoshop");
|
||||
assert_eq!((dst.mode, dst.depth, dst.resolution_dpi), (src.mode, src.depth, src.resolution_dpi));
|
||||
assert_eq!(dst.name, format!("{} copy", src.name));
|
||||
assert_eq!(dst.name, "Untitled-1");
|
||||
let layers = r["layers"].as_array().unwrap();
|
||||
assert_eq!(layers.len(), 1);
|
||||
let id = LayerId(layers[0].as_u64().unwrap());
|
||||
|
||||
@@ -249,7 +249,7 @@ mod tests {
|
||||
let dialog = invoke(&mut app);
|
||||
set(&mut app, dialog, "document", json!("new")).unwrap();
|
||||
confirm(&mut app, &ctx, dialog).unwrap();
|
||||
assert_eq!(app.session.documents()[1].doc.name, "Poster copy");
|
||||
assert_eq!(app.session.documents()[1].doc.name, "Untitled-1");
|
||||
let dialog = invoke(&mut app);
|
||||
// Either the select refuses the id or the held OK does.
|
||||
if set(&mut app, dialog, "document", json!(u64::MAX.to_string())).is_ok() {
|
||||
|
||||
Reference in new issue
Block a user