diff --git a/crates/engine-core/src/cmd.rs b/crates/engine-core/src/cmd.rs index 764d48d423..16cdb14180 100644 --- a/crates/engine-core/src/cmd.rs +++ b/crates/engine-core/src/cmd.rs @@ -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; diff --git a/crates/engine-core/src/cmd/layer_copy_cmds.rs b/crates/engine-core/src/cmd/layer_copy_cmds.rs index 176518f937..4671ed0ee8 100644 --- a/crates/engine-core/src/cmd/layer_copy_cmds.rs +++ b/crates/engine-core/src/cmd/layer_copy_cmds.rs @@ -370,7 +370,12 @@ pub(crate) fn duplicate_to_new_document(s: &mut Session, p: &Value) -> Result 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"); +} diff --git a/crates/engine/tests/it/paint_in_layer_copy_cmds.rs b/crates/engine/tests/it/paint_in_layer_copy_cmds.rs index cbd947ce58..62fc8f1b65 100644 --- a/crates/engine/tests/it/paint_in_layer_copy_cmds.rs +++ b/crates/engine/tests/it/paint_in_layer_copy_cmds.rs @@ -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()); diff --git a/crates/ui-egui/src/dialogs/duplicate_layer_ui.rs b/crates/ui-egui/src/dialogs/duplicate_layer_ui.rs index 83a8b34d83..0b038712c9 100644 --- a/crates/ui-egui/src/dialogs/duplicate_layer_ui.rs +++ b/crates/ui-egui/src/dialogs/duplicate_layer_ui.rs @@ -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() {