diff --git a/crates/ui-egui/src/dialogs/new_layer_name_trim_tests.rs b/crates/ui-egui/src/dialogs/new_layer_name_trim_tests.rs new file mode 100644 index 00000000000..78a8f17f86f --- /dev/null +++ b/crates/ui-egui/src/dialogs/new_layer_name_trim_tests.rs @@ -0,0 +1,22 @@ +use super::{COMMAND, testing}; +use crate::{PhotocraftApp, Services}; +use serde_json::json; + +#[test] +fn new_layer_dialog_trims_only_its_name() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default()); + app.run("file.new", json!({"width": 16, "height": 16})).unwrap(); + let ctx = app.ctx().clone(); + + for (typed, expected) in [(" sp a ", Some("sp a")), ("", None), (" ", None)] { + let default = app.session.active().unwrap().doc.next_layer_name("Layer"); + crate::menus::invoke(&mut app, &ctx, COMMAND, json!({})).unwrap(); + let id = testing::top(&app); + testing::set(&mut app, id, "name", json!(typed)).unwrap(); + crate::dialogs::confirm(&mut app, &ctx, id).unwrap(); + assert_eq!(app.session.active().unwrap().doc.layers.last().unwrap().name, expected.unwrap_or(&default)); + } + + app.run(COMMAND, json!({"name": "abc "})).unwrap(); + assert_eq!(app.session.active().unwrap().doc.layers.last().unwrap().name, "abc "); +} diff --git a/crates/ui-egui/src/dialogs/new_layer_ui.rs b/crates/ui-egui/src/dialogs/new_layer_ui.rs index f890274cf84..09974c91810 100644 --- a/crates/ui-egui/src/dialogs/new_layer_ui.rs +++ b/crates/ui-egui/src/dialogs/new_layer_ui.rs @@ -81,7 +81,7 @@ pub fn params(f: &DialogFields) -> Value { }); // An empty name gets the next "Layer N", as the Layers panel button does. if let Some(name) = f.text("name").filter(|n| !n.trim().is_empty()) { - p["name"] = json!(name); + p["name"] = json!(name.trim_matches(' ')); } p } @@ -123,6 +123,10 @@ pub(crate) mod testing { } } +#[cfg(test)] +#[path = "new_layer_name_trim_tests.rs"] +mod new_layer_name_trim_tests; + #[cfg(test)] mod tests { use serde_json::json;