diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index e5b2056c0b..5213b6cfc5 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -1378,12 +1378,14 @@ impl Document { } /// The "Layer N" a new plain layer is suggested: N is one above the highest number among the - /// names that are exactly "Layer ", or 1 when no layer has such a name (#17365). Only - /// Layer > New > Layer and its dialog use this; other callers keep [`Self::next_layer_name`]. - /// Names are gathered as `next_layer_name` gathers them (every layer, compared exactly), and a - /// number too large to hold is not counted, like a name that is not "Layer ". + /// top-level names that are exactly "Layer ", or 1 when no top-level layer has such a + /// name (#17365). Only Layer > New > Layer and its dialog use this; other callers keep + /// [`Self::next_layer_name`]. Top-level names only, compared exactly, the reading the Layer + /// from Background rule chose for its search (#14821): whether numbered layers inside groups + /// count is not measured in #17366, so nested names stay out of it. A number too large to hold + /// is not counted, like a name that is not "Layer ". pub fn next_layer_suggestion(&self) -> String { - let highest = self.walk().into_iter().filter_map(|(_, _, l)| l.name.strip_prefix("Layer ")).filter_map(|digits| digits.parse::().ok()).max(); + let highest = self.layers.iter().filter_map(|l| l.name.strip_prefix("Layer ")).filter_map(|digits| digits.parse::().ok()).max(); format!("Layer {}", highest.unwrap_or(0).saturating_add(1)) } diff --git a/crates/engine-core/src/new_layer_name_counter_tests.rs b/crates/engine-core/src/new_layer_name_counter_tests.rs index a04b430739..aa50ae64de 100644 --- a/crates/engine-core/src/new_layer_name_counter_tests.rs +++ b/crates/engine-core/src/new_layer_name_counter_tests.rs @@ -4,8 +4,10 @@ //! `crates/ui-egui/src/dialogs/new_layer_ui.rs` pins that the prefilled name equals the name the //! command gives, by behaviour rather than by naming one function twice. //! -//! Not measured in the issue: whether numbered layers inside groups count. Names are gathered as -//! `Document::next_layer_name` gathers them today: every layer in the document, compared exactly. +//! Not measured in the issue: whether numbered layers inside groups count. They keep today's +//! suggestion: the counter reads top-level names only, the reading the merged Layer from +//! Background rule chose for its own search (#14821), pinned by +//! `a_numbered_layer_inside_a_group_does_not_count`. //! Layer from Background keeps its own measured lowest-free rule (#14821), pinned below. use photocraft_doc::LayerId; @@ -67,6 +69,21 @@ fn names_that_are_not_exactly_layer_number_do_not_count() { assert_eq!(default_name(&others), "Layer 1", "only exact \"Layer \" names count"); } +/// Not measured in #17366: numbered layers inside groups. They keep today's suggestion, taking the +/// top-level reading the sibling Layer from Background rule chose (#14821): a group holding +/// "Layer 7" leaves the default at "Layer 1", where today's first-free rule lands as well. +#[test] +fn a_numbered_layer_inside_a_group_does_not_count() { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 400, "height": 300})).unwrap(); + s.execute("layer.new.group", json!({ "name": "G" })).unwrap(); + // A fresh group is open, so this layer lands inside it (see layer_place_tests). + let nested = LayerId(s.execute("layer.new.layer", json!({ "name": "Layer 7" })).unwrap()["layer"].as_u64().unwrap()); + assert_eq!(doc(&s).path_of(nested).map(|p| p.len()), Some(2), "Layer 7 sits inside the group"); + let made = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); + assert_eq!(doc(&s).layer(made).unwrap().name, "Layer 1", "the nested Layer 7 does not lift the suggestion"); +} + /// #14821 measured Layer from Background as its own lowest-free rule, counting from 0. The shared /// counter must not change what that command names the converted Background. #[test]