diff --git a/crates/ui-egui/src/variables_ui.rs b/crates/ui-egui/src/variables_ui.rs index 81f9aa929e..a421de3915 100644 --- a/crates/ui-egui/src/variables_ui.rs +++ b/crates/ui-egui/src/variables_ui.rs @@ -110,7 +110,7 @@ const METHODS: &[(&str, &str)] = &[("fit", "Fit"), ("fill", "Fill"), ("conform", pub fn body(app: &mut PhotocraftApp, ui: &mut egui::Ui, fields: &mut Map) { let t = Tokens::get(ui.ctx()); let layers = layer_options(app); - let mut state = fields.get("__variables").cloned().unwrap_or_else(|| json!({"defs": [], "dataSets": []})); + let mut state = normalize(fields.get("__variables").unwrap_or(&Value::Null)); let mut page = fields.get("__page").and_then(Value::as_str).unwrap_or("define").to_string(); ui.set_min_width(420.0); @@ -137,6 +137,22 @@ pub fn body(app: &mut PhotocraftApp, ui: &mut egui::Ui, fields: &mut Map Value { + let objects = + |v: Option<&Value>| -> Vec { v.and_then(Value::as_array).map(|a| a.iter().filter(|e| e.is_object()).cloned().collect()).unwrap_or_default() }; + let sets: Vec = objects(state.get("dataSets")) + .into_iter() + .map(|mut set| { + set["values"] = Value::Array(objects(set.get("values"))); + set + }) + .collect(); + json!({"defs": objects(state.get("defs")), "dataSets": sets}) +} + fn define_page(ui: &mut egui::Ui, _t: &Tokens, layers: &[(u64, String, bool)], state: &mut Value) { let Some(defs) = state.get_mut("defs").and_then(Value::as_array_mut) else { return }; ui.label(tl!("Variables bind a layer's visibility, text or pixels to a named data slot.")); @@ -339,6 +355,15 @@ mod tests { assert_eq!(data_set_index(&fields, 0), 0); } + #[test] + fn normalize_keeps_only_object_entries() { + let s = normalize(&json!({"defs": [1, {"name": "a"}], "dataSets": ["x", {"name": "A", "values": [true, {"variable": "a"}]}, {"values": 3}]})); + assert_eq!(s, json!({"defs": [{"name": "a"}], "dataSets": [{"name": "A", "values": [{"variable": "a"}]}, {"values": []}]})); + for bad in [Value::Null, json!(5), json!("s"), json!([1]), json!({"defs": "x", "dataSets": {}})] { + assert_eq!(normalize(&bad), json!({"defs": [], "dataSets": []}), "{bad}"); + } + } + #[test] fn owns_detects_the_field() { let mut f = Map::new(); diff --git a/crates/ui-egui/tests/variables_data_sets.rs b/crates/ui-egui/tests/variables_data_sets.rs index cb79eb9335..c79f1c1693 100644 --- a/crates/ui-egui/tests/variables_data_sets.rs +++ b/crates/ui-egui/tests/variables_data_sets.rs @@ -55,3 +55,37 @@ fn out_of_range_data_set_index_is_clamped_before_delete() { h.get_by_label("Delete").click(); h.run_steps(2); } + +/// `ui.dialog.set` can store any JSON in `__variables`; the editors write into its entries. +fn app_with_malformed_variables(page: &str) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), photocraft_ui_egui::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + let mut fields = Map::new(); + fields.insert( + "__variables".into(), + json!({"defs": [42, {"name": "v1", "layer": 0, "type": "textReplacement"}], "dataSets": ["bad", {"name": "A", "values": [7, "x"]}, {"name": "B", "values": "nope"}]}), + ); + fields.insert("__page".into(), json!(page)); + app.ui.open_dialog(DialogKind::Command, fields); + app +} + +fn type_into_first_text_field(h: &mut Harness<'static, PhotocraftApp>) { + h.query_all_by_role(egui::accesskit::Role::TextInput).next().expect("a text field").click(); + h.run_steps(2); + h.query_all_by_role(egui::accesskit::Role::TextInput).next().expect("a text field").type_text("z"); + h.run_steps(2); +} + +#[test] +fn malformed_variable_entries_are_dropped_before_an_edit() { + for page in ["define", "dataSets"] { + let mut h = harness(app_with_malformed_variables(page)); + type_into_first_text_field(&mut h); + let state = &h.state().ui.dialogs.last().unwrap().fields["__variables"]; + assert_eq!(state["defs"].as_array().unwrap().len(), 1, "{page}: {state}"); + assert_eq!(state["dataSets"].as_array().unwrap().len(), 2, "{page}: {state}"); + assert!(state["dataSets"].as_array().unwrap().iter().all(|s| s["values"].as_array().unwrap().iter().all(|v| v.is_object())), "{page}: {state}"); + } +}