Files
Daniel 0e83a0e494 Refusals: five windows drop a refused action's message once its inputs change (#12576)
* Parameters: a refused new parameter's message goes once the draft is corrected

In Parameters, adding a parameter whose name is taken, or whose expression does not evaluate, showed the reason in the window's banner. The banner stayed after the name or the expression was corrected, until the next Enter, beside an expression cell that already said the draft was fine.

The add row now keeps the draft it was refused with (name, unit, expression, comment), the design document the refusal was about and the message, as a command window does for a refused OK (#641). Each frame the window drops the message once the draft differs or the document is another one; a selection or display change keeps the document and so keeps the message. The refusal lives apart from the row edits' messages: the banner shows the last action's message if there is one, else the refusal. Discarding the row takes the message with it. The add row's bin button publishes the scenario handle param:new:delete.

Eight asserts read the banner through ParamsDialog::banner() instead of the error field. Regression tests: correcting_a_refused_new_parameter_clears_its_error and seven more in params_dialog.rs cover the corrected draft, a discarded row, a refusal on leaving the row, a design change, a row's own message beside the refusal, a selection, a unit or comment edit, and the banner's order.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Design Properties: a refused OK's message goes once the fields change

In File, Design Properties, a value the design refuses (a part number with a control character, for example) keeps the window open with the reason. The message stayed after the field was corrected, until the next OK.

The window now keeps a snapshot of the fields at the refused OK, as a command dialog does for its inputs (#641). Each frame it compares the fields with the snapshot; once they differ, the message goes, also on an edit to another field, and the next OK checks again. The regression test the_window_edits_properties_in_one_undo_step_and_keeps_other_tools_data covers the corrected field, an edit to another field and the repeated refusal.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* BOM panel: a refused Apply's message goes once the row or its fields change

In the Bill of Materials window, edit a part number to one the design refuses and click Apply: the message shows. Correct the field, or pick another row: the message stayed until the next Apply. BomPanel::apply set the error on a refusal and nothing cleared it while the window was open.

The panel now keeps the design revision, the row and the fields Apply ran with and drops the message as soon as any of them differs, as a command dialog's refused OK does (#641); the next Apply checks again. The revision is part of the snapshot because the row index counts the design's rows, which an undo or a part added elsewhere can change under the message. The regression test a_refused_apply_keeps_its_message_until_the_row_or_its_fields_change covers the corrected field, another field, another row, an undo and the repeated refusal.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Material editor: a refused Save's message goes once the draft changes

New material, name it Steel (a built-in), Save: refused with the reason. Rename it: the message stayed until the next Save. The editor set the error when save was refused and cleared it only on a successful save, Discard or a new draft.

The editor now keeps the design revision and the command Save ran with and drops the message as soon as the draft would run another, or the design changes under it, as a command dialog's refused OK does (#641); the next Save checks again. A Save refused while the draft fails its own validation (not reachable with a mouse, Save is disabled then) is kept the same way. The regression tests physical_material_refused_save_keeps_its_message_until_the_draft_changes and physical_material_refused_save_goes_when_the_design_changes in manage_dialogs_tests.rs cover the rename, an undo and the repeated refusal.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Add agent: a refused Add's message goes once the fields change

Add agent, custom, click Add with the command empty: the dialog asks for a command. Type the command: the message stayed until Add was clicked again, the same for a missing model, a key without https or a bad URL. The dialog cleared the message only on a preset, kind or model change.

The dialog now keeps what the fields said when Add was refused and drops the message as soon as any of them changes, as a command dialog's refused OK does (#641); the next Add checks again. The snapshot holds whether a key is typed, not the key itself. The regression test a_refused_add_keeps_its_message_until_the_fields_change covers a program without a command and an endpoint without a model.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-07 17:14:18 +00:00

190 lines
9.3 KiB
Rust

//! Enter submits only the focused Parameters editor row.
use egui::{Event, Key, Modifiers};
use serde_json::json;
use solidcraft_ui_egui::scenario::Harness;
fn open() -> Harness {
open_in_units("mm")
}
fn open_in_units(units: &str) -> Harness {
let mut h = Harness::new();
h.app.run("document.units", json!({"units": units})).unwrap();
// The Browser's Parameters row starts this command; the ribbon shows only the active tab, so no
// Parameters button is on screen to click.
h.call("ui.start", json!({"command":"parameters.change"}));
h.frames(3);
assert!(h.app.params.is_some());
h.step(&json!({"click":{"handle":"dialog:table:user:add"}})).unwrap();
h
}
fn fill(h: &mut Harness, field: &str, text: &str) {
h.run(&json!([
{"click":{"handle":format!("param:new:{field}")}},
{"key":"A","cmd":true}, {"text":text}
]))
.unwrap();
}
fn enter(h: &mut Harness, repeat: bool) {
h.app.synthetic.push(Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat, modifiers: Modifiers::NONE });
h.frame();
h.app.synthetic.push(Event::Key { key: Key::Enter, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE });
h.frames(3);
}
#[test]
fn parameter_enter_adds_once_and_focuses_next_name() {
let mut h = open();
fill(&mut h, "expression", "25 mm");
fill(&mut h, "name", "width");
let undo = h.app.session.undo.len();
enter(&mut h, false);
assert_eq!(h.app.session.undo.len(), undo + 1);
assert!(h.app.params.as_ref().unwrap().new_name.is_empty());
assert!(h.app.params.as_ref().unwrap().banner().is_none());
let saved = h.app.session.doc.to_json();
enter(&mut h, true);
assert_eq!(h.app.session.doc.to_json(), saved);
assert_eq!(h.app.session.undo.len(), undo + 1);
// Name has focus after successful creation; no mouse needed for the next name.
h.run(&json!([{"text":"height"}])).unwrap();
assert_eq!(h.app.params.as_ref().unwrap().new_name, "height");
// The new row's expression starts blank: Enter after the name goes on to it, and Enter
// there adds the parameter.
enter(&mut h, false);
assert_eq!(h.app.session.undo.len(), undo + 1, "nothing added without an expression");
h.run(&json!([{"text":"10 mm"}])).unwrap();
assert_eq!(h.app.params.as_ref().unwrap().new_expr, "10 mm");
enter(&mut h, false);
assert_eq!(h.app.session.undo.len(), undo + 2);
let result = h.app.session.execute("parameters.list", &json!({})).unwrap();
assert!(result["parameters"].as_array().unwrap().iter().any(|p| p["name"] == "height"));
}
#[test]
fn parameter_enter_invalid_preserves_draft_and_other_rows() {
let mut h = open();
h.app.session.execute("parameters.add", &json!({"name":"existing","expression":"10 mm"})).unwrap();
h.frames(3);
fill(&mut h, "expression", "missing + 1 mm");
fill(&mut h, "name", "bad");
let saved = h.app.session.doc.to_json();
let undo = h.app.session.undo.len();
enter(&mut h, false);
assert_eq!(h.app.session.doc.to_json(), saved);
assert_eq!(h.app.session.undo.len(), undo);
let d = h.app.params.as_ref().unwrap();
assert_eq!(d.new_name, "bad");
assert_eq!(d.new_expr, "missing + 1 mm");
assert!(d.banner().is_some());
fill(&mut h, "name", "existing");
fill(&mut h, "expression", "15 mm");
enter(&mut h, false);
assert_eq!(h.app.session.doc.to_json(), saved);
assert!(h.app.params.as_ref().unwrap().banner().is_some());
}
#[test]
fn parameter_enter_edits_expression_without_adding_draft() {
let mut h = open();
h.app.session.execute("parameters.add", &json!({"name":"width","expression":"10 mm"})).unwrap();
h.frames(3);
// A named draft with an invalid expression stays pending on focus loss.
fill(&mut h, "expression", "missing + 1 mm");
fill(&mut h, "name", "unsubmitted");
let undo = h.app.session.undo.len();
h.run(&json!([{"click":{"handle":"param:width"}}, {"key":"A","cmd":true}, {"text":"20 mm"}])).unwrap();
enter(&mut h, false);
assert_eq!(h.app.session.undo.len(), undo + 1);
assert_eq!(h.app.params.as_ref().unwrap().new_name, "unsubmitted");
assert!(h.app.params.as_ref().unwrap().banner().is_none(), "Enter in the existing row never attempts the invalid draft");
let result = h.app.session.execute("parameters.list", &json!({})).unwrap();
let ps = result["parameters"].as_array().unwrap();
assert!(ps.iter().any(|p| p["name"] == "width" && p["expression"] == "20 mm"));
assert!(!ps.iter().any(|p| p["name"] == "unsubmitted"));
let saved = h.app.session.doc.to_json();
h.run(&json!([{"click":{"handle":"param:width"}}, {"key":"A","cmd":true}, {"text":"missing + 2 mm"}])).unwrap();
enter(&mut h, false);
assert_eq!(h.app.session.doc.to_json(), saved);
assert!(h.app.params.as_ref().unwrap().error.is_some());
assert_eq!(h.app.params.as_ref().unwrap().editing.get("width").map(String::as_str), Some("missing + 2 mm"));
}
#[test]
fn parameter_enter_comment_uses_selected_unit_and_unrelated_focus_does_not_submit() {
let mut h = open();
h.run(&json!([{"click":{"handle":"param:new:unit"}}, {"widget":"cm"}])).unwrap();
fill(&mut h, "expression", "2 cm");
fill(&mut h, "comment", "Overall length");
fill(&mut h, "expression", "missing + 1 mm");
fill(&mut h, "name", "length");
let undo = h.app.session.undo.len();
h.run(&json!([{"click":{"handle":"params:title"}}])).unwrap();
// Make the pending expression valid while unrelated focus is retained, without firing
// the newly approved focus-loss commit route. Unrelated Enter must still do nothing.
h.app.params.as_mut().unwrap().new_expr = "2 cm".into();
enter(&mut h, false);
assert_eq!(h.app.session.undo.len(), undo);
assert!(h.app.params.as_ref().unwrap().banner().is_none(), "unrelated Enter never attempts the pending draft");
h.run(&json!([{"click":{"handle":"param:new:comment"}}])).unwrap();
enter(&mut h, false);
let result = h.app.session.execute("parameters.list", &json!({})).unwrap();
assert!(result["parameters"].as_array().unwrap().iter().any(|p| p["name"] == "length" && p["unit"] == "cm" && p["comment"] == "Overall length"));
assert_eq!(h.app.session.undo.len(), undo + 1);
}
#[test]
fn parameter_rename_arrow_keys_move_cursor_and_keep_editing() {
let mut h = open();
h.app.session.execute("parameters.add", &json!({"name":"width","expression":"10 mm"})).unwrap();
// Let the click that opened the dialog age, or egui counts the double-click as a triple.
h.frames(30);
h.run(&json!([{"click":{"handle":"param:name:width"},"double":true}])).unwrap();
h.frames(2);
let renaming = |h: &Harness| h.app.params.as_ref().unwrap().renaming.clone();
assert_eq!(renaming(&h), Some(("width".to_string(), "width".to_string())));
// Move the cursor to the start and type there; every arrow key keeps the rename open.
h.run(&json!([{"key":"ArrowLeft"}, {"key":"ArrowUp"}, {"key":"ArrowDown"}, {"key":"ArrowRight"}, {"key":"Home"}, {"text":"plate_"}])).unwrap();
assert_eq!(renaming(&h), Some(("width".to_string(), "plate_width".to_string())));
enter(&mut h, false);
assert_eq!(renaming(&h), None);
let result = h.app.session.execute("parameters.list", &json!({})).unwrap();
assert!(result["parameters"].as_array().unwrap().iter().any(|p| p["name"] == "plate_width"));
}
/// #3965: leaving an invalid name and clicking back into it must edit the name.
/// Refusing the add used to put focus back on the field being left, so the
/// correction was typed into the expression instead.
#[test]
fn invalid_parameter_name_stays_editable_after_leaving_it() {
let mut h = open();
fill(&mut h, "name", "bad name");
// A new row's expression starts blank. Type one, then leave it and come back to the name.
fill(&mut h, "expression", "10 mm");
h.run(&json!([
{"click":{"handle":"param:new:expression"}},
{"click":{"handle":"param:new:name"}},
{"key":"A","cmd":true},
{"text":"width"}
]))
.unwrap();
let d = h.app.params.as_ref().unwrap();
assert_eq!(d.new_name, "width", "typing after returning to Name edits the name");
assert_eq!(d.new_expr, "10 mm", "the expression keeps the draft");
let result = h.app.session.execute("parameters.list", &json!({})).unwrap();
assert!(!result["parameters"].as_array().unwrap().iter().any(|p| p["name"] == "bad name" || p["name"] == "width"));
}
#[test]
fn parameter_add_resets_expression_for_next_row() {
for (units, selected) in [("mm", 0), ("cm", 1), ("m", 2), ("in", 3), ("ft", 4)] {
let mut h = open_in_units(units);
let d = h.app.params.as_ref().unwrap();
assert!(d.new_expr.is_empty(), "{units}: the initial draft is blank");
assert_eq!(d.new_unit, selected, "{units}: the document unit is selected");
fill(&mut h, "expression", "25 mm");
fill(&mut h, "name", "width");
enter(&mut h, false);
let result = h.app.session.execute("parameters.list", &json!({})).unwrap();
assert!(result["parameters"].as_array().unwrap().iter().any(|p| p["name"] == "width" && p["expression"] == "25 mm"));
let d = h.app.params.as_ref().unwrap();
assert!(d.new_name.is_empty());
assert!(d.new_expr.is_empty(), "{units}: adding a parameter clears the next draft");
assert_eq!(d.new_unit, selected, "{units}: adding preserves the selected unit");
}
}