diff --git a/crates/ui-egui/src/dialogs/kinsoku_sets.rs b/crates/ui-egui/src/dialogs/kinsoku_sets.rs index 35c80691ed..2f67d199ab 100644 --- a/crates/ui-egui/src/dialogs/kinsoku_sets.rs +++ b/crates/ui-egui/src/dialogs/kinsoku_sets.rs @@ -73,6 +73,12 @@ fn sets(d: &Dialog) -> Vec> { d.fields.get("sets").and_then(Value::as_array).map(|a| a.iter().filter_map(|v| v.as_object().cloned()).collect()).unwrap_or_default() } +/// Is the `sets` field a list of sets? One that is not (set with `ui.dialog.set`) is kept as it is: +/// no frame or button replaces it, and OK sends it to the command, which refuses it. +fn holds_sets(d: &Dialog) -> bool { + d.fields.get("sets").and_then(Value::as_array).is_some_and(|a| a.iter().all(Value::is_object)) +} + fn put_sets(d: &mut Dialog, sets: &[Map]) { d.fields.insert("sets".into(), json!(sets)); } @@ -242,7 +248,9 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { } d.fields.insert("shown".into(), Value::Object(set.clone())); } - put_sets(d, &list); + if holds_sets(d) { + put_sets(d, &list); + } if let Some(a) = act && let Err(e) = run(app, d, a) { @@ -253,6 +261,9 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { /// Do what button `act` asks for on the dialog's sets. fn run(app: &mut VectorcraftApp, d: &mut Dialog, act: Action) -> Result<(), String> { + if !holds_sets(d) { + return Err("the sets must be a list of Kinsoku sets".into()); + } let mut list = sets(d); let i = selected(d, list.len()); match act { @@ -325,8 +336,7 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { app.run("document.activate", json!({ "index": index }))?; } // A `sets` field that is not a list of sets goes to the command as it is, which refuses it. - let raw = d.fields.get("sets").cloned().unwrap_or(Value::Null); - let sets = if raw.as_array().is_some_and(|a| a.iter().all(Value::is_object)) { + let sets = if holds_sets(d) { // A set the document gained while the dialog was open (a script, another command) is // kept, after the dialog's sets. let opened: Vec<&str> = @@ -336,7 +346,7 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { } json!(list) } else { - raw + d.fields.get("sets").cloned().unwrap_or(Value::Null) }; let r = app.run(COMMAND, json!({ "sets": sets })); // A name two sets share, a reserved name or a category too long keeps the dialog open to be diff --git a/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs b/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs index 28f59c3938..1b3ddd9520 100644 --- a/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs +++ b/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs @@ -246,7 +246,8 @@ fn ok_sets_the_document_the_dialog_opened_on() { /// OK keeps a set the document gained while the dialog was open, after the dialog's sets, and a /// set deleted in the dialog stays deleted. A `sets` field that is not a list of sets keeps the -/// dialog open and leaves the document's sets as they are. +/// dialog open and leaves the document's sets as they are, also when a frame is drawn before OK, +/// and New refuses to start from it. #[test] fn ok_keeps_sets_added_while_open_and_refuses_malformed_sets() { let mut app = app(); @@ -264,12 +265,21 @@ fn ok_keeps_sets_added_while_open_and_refuses_malformed_sets() { super::super::confirm(&mut app).unwrap(); let names = |app: &VectorcraftApp| doc_sets(app).iter().map(|k| k.name.clone()).collect::>(); assert_eq!(names(&app), ["Later"]); - for bad in [Value::Null, json!({}), json!("Later"), json!([1])] { - open(&mut app).unwrap(); - dialog(&mut app).fields.insert("sets".into(), bad.clone()); - assert!(super::super::confirm(&mut app).is_err(), "{bad}"); - assert!(app.ui.dialog.is_some(), "{bad}"); - assert_eq!(names(&app), ["Later"], "{bad}"); + for bad in [Value::Null, json!({}), json!("Later"), json!([1]), json!([{"name": "Later", "cannotStart": "ー"}, 1])] { + for drawn in [false, true] { + open(&mut app).unwrap(); + dialog(&mut app).fields.insert("sets".into(), bad.clone()); + if drawn { + frame(&mut app); + assert_eq!(dialog(&mut app).fields.get("sets"), Some(&bad), "a frame keeps {bad} as it is"); + let mut d = app.ui.dialog.clone().unwrap(); + assert!(run(&mut app, &mut d, Action::New).is_err(), "{bad}"); + assert_eq!(d.fields.get("sets"), Some(&bad), "New keeps {bad} as it is"); + } + assert!(super::super::confirm(&mut app).is_err(), "{bad} drawn: {drawn}"); + assert!(app.ui.dialog.is_some(), "{bad} drawn: {drawn}"); + assert_eq!(names(&app), ["Later"], "{bad} drawn: {drawn}"); + } } }