SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Refusals: five windows drop a refused action's message once its inputs change (#12576)
Daniel0e83a0e49484
diff --git a/crates/ui-egui/src/agent_chat/settings.rs b/crates/ui-egui/src/agent_chat/settings.rsindex f87ad489a3..5cca64622b 100644--- a/crates/ui-egui/src/agent_chat/settings.rs+++ b/crates/ui-egui/src/agent_chat/settings.rs@@ -34,6 +34,8 @@ pub struct AddState {/// Optional reasoning effort sent as `reasoning_effort`; empty sends nothing.pub reasoning_effort: String,pub error: Option<String>,+ /// What the fields said when Add was refused: `error` stays only while they still say it.+ refused: Option<AddInputs>,/// Editing this added agent (its id) instead of adding a new one.pub editing: Option<String>,/// Editing an endpoint whose key is in the keychain: an empty key field keeps it.@@ -48,7 +50,32 @@ pub struct AddState {list_again: bool,}+/// What the Add dialog's fields say, as far as a refusal can be about them: of the key only+/// whether one is typed (`add_agent` reads nothing else of it, and no second copy is kept).+type AddInputs = (AddKind, Protocol, usize, String, String, String, String, bool, u64, String);+impl AddState {+ fn inputs(&self) -> AddInputs {+ (+ self.kind,+ self.protocol,+ self.preset,+ self.name.clone(),+ self.command.clone(),+ self.base_url.clone(),+ self.model.clone(),+ !self.key.trim().is_empty(),+ self.request_timeout_s,+ self.reasoning_effort.clone(),+ )+ }++ /// Show why Add was refused, for as long as the fields stay as they are.+ fn refuse(&mut self, error: String) {+ self.refused = Some(self.inputs());+ self.error = Some(error);+ }+fn insecure_http_endpoint(&self) -> Option<String> {let url = agents::normalize_url(&self.base_url).ok()?;(self.kind == AddKind::Api && self.protocol == Protocol::OpenAi && !agents::key_safe_for(&url)).then_some(url)@@ -1037,6 +1064,13 @@ pub fn dialog(app: &mut SolveApp, ctx: &egui::Context) {if app.agent_chat.settings.add.is_none() {return;}+ // A refused Add's message is about the fields it ran with (#641): once they change, it goes.+ if let Some(s) = app.agent_chat.settings.add.as_mut()+ && s.refused.as_ref().is_some_and(|was| *was != s.inputs())+ {+ s.error = None;+ s.refused = None;+ }if let Some(url) = app.agent_chat.settings.add.as_ref().and_then(|s| s.confirm_insecure_http.clone()) {let (closed, action) = ui_kit::Confirm::new(egui::Id::unique("sc_ai_http_consent"), "status_warning", tr("ai.http.allow")).tone(ui_kit::Tone::Warning)@@ -1304,7 +1338,7 @@ pub fn dialog(app: &mut SolveApp, ctx: &egui::Context) {Ok(()) => open = false,Err(e) => {if let Some(s) = app.agent_chat.settings.add.as_mut() {- s.error = Some(e);+ s.refuse(e);}}}@@ -1803,6 +1837,48 @@ mod card_tests {assert_eq!(capability_warning("local:4b", None), None);}+ #[test]+ fn a_refused_add_keeps_its_message_until_the_fields_change() {+ let mut h = Harness::new();+ h.app.home.open = false;+ h.app.preferences.ai_features = true;+ h.app.agent_chat.detection = Some(Default::default());+ open_add(&mut h.app);+ h.frames(4);+ h.step(&json!({"click":{"handle":"ai_add:ui.ai_add_custom"}})).unwrap();+ h.step(&json!({"click":{"handle":"ai_add:add"}})).unwrap();+ let refused = h.app.agent_chat.settings.add.as_ref().unwrap().error.clone();+ assert!(refused.is_some(), "an empty command is refused");+ h.frames(3);+ assert_eq!(h.app.agent_chat.settings.add.as_ref().unwrap().error, refused, "the message stays while the fields do");+ // Typing the command takes the refusal away before Add is clicked again.+ h.app.agent_chat.settings.add.as_mut().unwrap().command = "solidcraft-fixture-agent --acp".into();+ h.frames(3);+ assert_eq!(h.app.agent_chat.settings.add.as_ref().unwrap().error, None, "the message was about the empty command, which is gone");+ // The message is about the fields Add ran with (#641): the same fields again are refused again.+ h.app.agent_chat.settings.add.as_mut().unwrap().command.clear();+ h.frames(3);+ h.step(&json!({"click":{"handle":"ai_add:add"}})).unwrap();+ assert_eq!(h.app.agent_chat.settings.add.as_ref().unwrap().error, refused, "the same reason again");+ assert!(h.app.agent_chat.settings.add.is_some(), "nothing added");+ // An endpoint without a model: the same for its fields.+ h.step(&json!({"click":{"handle":"ai_add:ui.ai_add_endpoint"}})).unwrap();+ h.step(&json!({"click":{"handle":"ai_add:protocol:anthropic"}})).unwrap();+ h.step(&json!({"click":{"handle":"ai_add:add"}})).unwrap();+ let refused = h.app.agent_chat.settings.add.as_ref().unwrap().error.clone();+ assert!(refused.is_some(), "an endpoint without a model is refused");+ h.frames(3);+ assert_eq!(h.app.agent_chat.settings.add.as_ref().unwrap().error, refused);+ h.app.agent_chat.settings.add.as_mut().unwrap().model = "claude-opus-5-5".into();+ h.frames(3);+ assert_eq!(h.app.agent_chat.settings.add.as_ref().unwrap().error, None, "the message was about the missing model, which is typed");+ h.app.agent_chat.settings.add.as_mut().unwrap().model.clear();+ h.frames(3);+ h.step(&json!({"click":{"handle":"ai_add:add"}})).unwrap();+ assert_eq!(h.app.agent_chat.settings.add.as_ref().unwrap().error, refused, "the same reason again");+ assert!(h.app.agent_chat.settings.add.is_some(), "nothing added");+ }+#[test]fn add_agent_card_retains_kind_handles_draft_and_validation() {let mut h = Harness::new();diff --git a/crates/ui-egui/src/bom_panel.rs b/crates/ui-egui/src/bom_panel.rsindex d61fcfd101..b8267bc8dd 100644--- a/crates/ui-egui/src/bom_panel.rs+++ b/crates/ui-egui/src/bom_panel.rs@@ -27,9 +27,18 @@ pub struct BomPanel {pub fields: Vec<String>,/// The last refusal.pub error: Option<String>,+ /// The design revision, the picked row and its fields when Apply was refused: `error` stays+ /// only while they still are.+ refused: Option<(u64, Option<usize>, Vec<String>)>,}impl BomPanel {+ /// Show why Apply was refused, for as long as the design, the row and its fields stay as they are.+ fn refuse(&mut self, revision: u64, error: String) {+ self.refused = Some((revision, self.picked, self.fields.clone()));+ self.error = Some(error);+ }+fn params(&self) -> Value {json!({"levels": if self.top { "top" } else { "flat" }, "group_by": [if self.by_definition { "definition" } else { "part_number" }],"group_children": self.group_children})@@ -116,16 +125,23 @@ fn apply(app: &mut SolveApp, d: &mut BomPanel, row: &Value) {params.insert("component".into(), c.clone());let r = app.run("component.metadata.set", Value::Object(params.clone()));if let Err(e) = r {- d.error = Some(e);+ d.refuse(app.session.revision, e);return;}}d.error = None;+ d.refused = None;}/// Draw the window when it is open. Called once per frame.pub fn show(app: &mut SolveApp, ctx: &egui::Context) {let Some(mut d) = app.bom.take() else { return };+ // A refused Apply's message is about the design, row and fields it ran with (#641): once they+ // change, it goes.+ if d.refused.as_ref().is_some_and(|(rev, row, fields)| (*rev, *row, fields) != (app.session.revision, d.picked, &d.fields)) {+ d.error = None;+ d.refused = None;+ }let t = Tokens::get();let bom = d.rows(app);let rows: Vec<Value> = bom["rows"].as_array().cloned().unwrap_or_default();@@ -263,6 +279,61 @@ mod tests {assert!(h.widget("PL-100").is_some(), "the list follows the change");}+ #[test]+ fn a_refused_apply_keeps_its_message_until_the_row_or_its_fields_change() {+ let mut h = Harness::new();+ h.app.run("component.create", json!({"name": "Plate"})).unwrap();+ h.app.run("solid.box", json!({"length": 40, "width": 20, "height": 3})).unwrap();+ h.app.run("component.activate", json!({"component": 0})).unwrap();+ h.app.run("component.create", json!({"name": "Bracket"})).unwrap();+ h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10})).unwrap();+ h.app.run("component.activate", json!({"component": 0})).unwrap();+ // Plate placed three times and Bracket once, so no Qty cell reads "1" before row 1's item+ // number or "2" before row 2's.+ h.app.run("occurrence.copy", json!({"component": "Plate", "translate": [50, 0, 0]})).unwrap();+ h.app.run("occurrence.copy", json!({"component": "Plate", "translate": [100, 0, 0]})).unwrap();+ h.step(&json!({"start": "assembly.bom"})).unwrap();+ h.frames(2);+ let undo = h.app.session.undo.len();+ h.step(&json!({"widget": "1"})).unwrap();+ h.app.bom.as_mut().unwrap().fields[0] = "PL\u{7}".into();+ h.frames(2);+ h.step(&json!({"widget": "Apply"})).unwrap();+ let refused = h.app.bom.as_ref().unwrap().error.clone();+ assert!(refused.as_deref().is_some_and(|e| e.contains("control character")), "{refused:?}");+ assert_eq!(h.app.session.undo.len(), undo, "nothing applied");+ // Correcting the field takes the refusal away before Apply is clicked again.+ h.app.bom.as_mut().unwrap().fields[0] = "PL-100".into();+ h.frames(2);+ assert_eq!(h.app.bom.as_ref().unwrap().error, None, "the message was about the control character, which is gone");+ // The message is about the row and fields Apply ran with (#641): a change to another+ // field takes it away too, and the next Apply refuses again.+ h.app.bom.as_mut().unwrap().fields[0] = "PL\u{7}".into();+ h.frames(2);+ h.step(&json!({"widget": "Apply"})).unwrap();+ assert_eq!(h.app.bom.as_ref().unwrap().error, refused);+ h.app.bom.as_mut().unwrap().fields[1] = "Plate, 3 mm".into();+ h.frames(2);+ assert_eq!(h.app.bom.as_ref().unwrap().error, None, "the fields differ from the refused ones");+ h.step(&json!({"widget": "Apply"})).unwrap();+ assert_eq!(h.app.bom.as_ref().unwrap().error, refused, "the same reason again");+ assert_eq!(h.app.session.undo.len(), undo, "nothing applied");+ // Picking another row takes it away: the message was about the Plate row.+ h.step(&json!({"widget": "2"})).unwrap();+ assert_eq!(h.app.bom.as_mut().unwrap().picked, Some(1));+ assert_eq!(h.app.bom.as_ref().unwrap().error, None, "another row is picked");+ h.step(&json!({"widget": "1"})).unwrap();+ h.app.bom.as_mut().unwrap().fields[0] = "PL\u{7}".into();+ h.frames(2);+ h.step(&json!({"widget": "Apply"})).unwrap();+ assert_eq!(h.app.bom.as_ref().unwrap().error, refused);+ // So does a change to the design, whose rows the picked index counts: the message was+ // about the design Apply ran on.+ h.app.run("edit.undo", json!({})).unwrap();+ h.frames(2);+ assert_eq!(h.app.bom.as_ref().unwrap().error, None, "the design changed under the row");+ }+#[test]fn a_picked_row_edits_each_bill_of_materials_key_under_its_label() {let mut h = Harness::new();diff --git a/crates/ui-egui/src/design_properties.rs b/crates/ui-egui/src/design_properties.rsindex cd813a6c76..8a168547f3 100644--- a/crates/ui-egui/src/design_properties.rs+++ b/crates/ui-egui/src/design_properties.rs@@ -36,13 +36,21 @@ pub struct DesignProperties {pub fields: Vec<String>,/// The last refusal from `document.metadata.set`.pub error: Option<String>,+ /// What the fields were when OK was refused: `error` stays only while they still are.+ refused: Option<Vec<String>>,}impl DesignProperties {pub fn new(app: &SolveApp) -> Self {let m = &app.session.doc.metadata;let opened: Vec<String> = keys(Owner::Design).map(|k| m.text(k.name).unwrap_or_default().to_string()).collect();- DesignProperties { fields: opened.clone(), opened, error: None }+ DesignProperties { fields: opened.clone(), opened, error: None, refused: None }+ }++ /// Show why OK was refused, for as long as the fields stay as they are.+ fn refuse(&mut self, error: String) {+ self.refused = Some(self.fields.clone());+ self.error = Some(error);}/// The fields that differ from what the window opened with, as `document.metadata.set`@@ -70,7 +78,7 @@ fn apply(app: &mut SolveApp, d: &mut DesignProperties) -> bool {match app.run("document.metadata.set", serde_json::json!({ "values": values })) {Ok(_) => true,Err(e) => {- d.error = Some(e);+ d.refuse(e);false}}@@ -85,6 +93,11 @@ fn preview(v: &Value) -> String {/// Draw the window when it is open. Called once per frame.pub fn show(app: &mut SolveApp, ctx: &egui::Context) {let Some(mut d) = app.design_properties.take() else { return };+ // A refused OK's message is about the fields it ran with (#641): once they change, it goes.+ if d.refused.as_ref().is_some_and(|was| *was != d.fields) {+ d.error = None;+ d.refused = None;+ }let metadata = app.session.doc.metadata.clone();let title = crate::i18n::tr_label("Design Properties");let (closed, action) = ui_kit::Window::new(egui::Id::unique("sc_design_properties"), "settings", title).fields().show(ctx, true, |ui| {@@ -553,6 +566,26 @@ mod tests {let d = h.app.design_properties.as_ref().expect("still open");assert!(d.error.as_deref().is_some_and(|e| e.contains("control character")), "{:?}", d.error);assert_eq!(h.app.session.undo.len(), undo + 1);+ // Correcting the field takes the refusal away before OK is clicked again.+ h.app.design_properties.as_mut().unwrap().fields[1] = "PN-1".into();+ h.frames(2);+ let d = h.app.design_properties.as_ref().expect("still open");+ assert_eq!(d.error, None, "the message was about the control character, which is gone");+ // The message is about the fields OK ran with, as a dialog's is about its inputs (#641):+ // a change to another field takes it away too, and the next OK refuses again.+ h.app.design_properties.as_mut().unwrap().fields[1] = "bad\u{7}".into();+ h.frames(2);+ h.step(&json!({"widget": "OK"})).unwrap();+ let refused = h.app.design_properties.as_ref().expect("still open").error.clone();+ assert!(refused.as_deref().is_some_and(|e| e.contains("control character")), "{refused:?}");+ h.app.design_properties.as_mut().unwrap().fields[0] = "ACME".into();+ h.frames(2);+ let d = h.app.design_properties.as_ref().expect("still open");+ assert_eq!(d.error, None, "the fields differ from the refused ones");+ h.step(&json!({"widget": "OK"})).unwrap();+ let d = h.app.design_properties.as_ref().expect("still open: the part number is still refused");+ assert_eq!(d.error, refused, "the same reason again");+ assert_eq!(h.app.session.undo.len(), undo + 1, "nothing applied");}#[test]diff --git a/crates/ui-egui/src/manage_dialogs_tests.rs b/crates/ui-egui/src/manage_dialogs_tests.rsindex dfebca1f18..9c70907f54 100644--- a/crates/ui-egui/src/manage_dialogs_tests.rs+++ b/crates/ui-egui/src/manage_dialogs_tests.rs@@ -418,7 +418,7 @@ fn parameter_inventory_remains_reachable_with_regular_draft_rows() {h.step(&json!({"text":"missing_parameter"})).unwrap();h.step(&json!({"key":"Enter"})).unwrap();assert_eq!(scenario::count_of("param:new:valid"), Some(0.0));- assert!(h.app.params.as_ref().unwrap().error.is_some());+ assert!(h.app.params.as_ref().unwrap().banner().is_some());assert_eq!(h.app.session.doc.params.iter().filter(|p| !p.model).count(), 2);h.step(&json!({"click":{"handle":"dialog:Filter"}})).unwrap();assert!(!h.app.params.as_ref().unwrap().adding, "an abandoned nameless draft disappears");@@ -801,6 +801,53 @@ fn physical_material_discard_and_invalid_save_leave_document_and_history_unchangassert_eq!(revision, h.app.session.revision);}+#[test]+fn physical_material_refused_save_keeps_its_message_until_the_draft_changes() {+ let mut h = fixture();+ h.app.start("material.assign");+ h.frames(3);+ h.step(&json!({"click":{"handle":"material:new"}})).unwrap();+ physical_type(&mut h, "name", "Steel");+ physical_type(&mut h, "density", "7.8");+ h.step(&json!({"click":{"handle":"material:save"}})).unwrap();+ assert!(physical_editor(&mut h).editing(), "a built-in name is refused");+ let refused = physical_editor(&mut h).error().map(str::to_owned);+ assert!(refused.as_deref().is_some_and(|e| e.contains("Steel")), "{refused:?}");+ h.frames(3);+ assert_eq!(physical_editor(&mut h).error().map(str::to_owned), refused, "the message stays while the draft does");+ // Renaming the draft takes the refusal away before Save is clicked again.+ physical_type(&mut h, "name", "Steel 2");+ h.frames(2);+ assert_eq!(physical_editor(&mut h).error(), None, "the message was about the name Steel, which is gone");+ // The message is about the command Save ran (#641): the same draft again is refused again.+ physical_type(&mut h, "name", "Steel");+ h.frames(2);+ h.step(&json!({"click":{"handle":"material:save"}})).unwrap();+ assert!(physical_editor(&mut h).editing());+ assert_eq!(physical_editor(&mut h).error().map(str::to_owned), refused, "the same reason again");+}++/// A refusal that depends on the design (the name is taken) goes once the design changes under+/// the same draft (#641): after an undo, Save would succeed.+#[test]+fn physical_material_refused_save_goes_when_the_design_changes() {+ let mut h = fixture();+ h.app.run("material.create", json!({"name":"Alu","density_g_cm3":2.7})).unwrap();+ h.app.start("material.assign");+ h.frames(3);+ h.step(&json!({"click":{"handle":"material:new"}})).unwrap();+ physical_type(&mut h, "name", "Alu");+ physical_type(&mut h, "density", "2.7");+ h.step(&json!({"click":{"handle":"material:save"}})).unwrap();+ assert!(physical_editor(&mut h).editing(), "a taken name is refused");+ let refused = physical_editor(&mut h).error().map(str::to_owned);+ assert!(refused.as_deref().is_some_and(|e| e.contains("Alu")), "{refused:?}");+ h.app.run("edit.undo", json!({})).unwrap();+ h.frames(2);+ assert!(physical_editor(&mut h).editing(), "the draft stays open");+ assert_eq!(physical_editor(&mut h).error(), None, "the design no longer has an Alu, so the message is stale");+}+fn complete_physical_card() -> serde_json::Value {let mut data = vec![json!({"kind":"density","kg_m3":750.1234567890123}),diff --git a/crates/ui-egui/src/material_editor.rs b/crates/ui-egui/src/material_editor.rsindex 3f480fb7e1..7d1ad7af30 100644--- a/crates/ui-egui/src/material_editor.rs+++ b/crates/ui-egui/src/material_editor.rs@@ -26,6 +26,9 @@ const SOURCE_KINDS: [SourceKind; 4] = [SourceKind::UserSupplied, SourceKind::Meapub struct Editor {pub(crate) draft: Option<Draft>,error: Option<String>,+ /// The design revision and the command Save was refused with (none when the draft did not+ /// validate): `error` stays only while the draft would run it again on the same design.+ refused: Option<(u64, Option<(&'static str, Value)>)>,note: Option<String>,chosen_material: Option<String>,observed_index: Option<usize>,@@ -62,9 +65,22 @@ impl Editor {self.draft.is_some()}+ #[cfg(test)]+ pub(crate) fn error(&self) -> Option<&str> {+ self.error.as_deref()+ }++ /// Show why Save was refused, for as long as the draft would run the same command on the+ /// same design.+ fn refuse(&mut self, error: String, revision: u64, doc: &Document) {+ self.refused = Some((revision, self.draft.as_ref().and_then(|d| d.command(doc).ok().flatten())));+ self.error = Some(error);+ }+pub(crate) fn begin(&mut self, doc: &Document, source: Option<&str>, duplicate: bool) {self.draft = Some(Draft::new(doc, source, duplicate));self.error = None;+ self.refused = None;self.note = None;}@@ -79,6 +95,7 @@ impl Editor {self.observed_index = Some(*index);self.draft = None;self.error = None;+ self.refused = None;self.note = Some(tr("material.editor.saved").to_string());Ok(())}@@ -500,6 +517,16 @@ pub(crate) fn rows(app: &mut SolveApp, ui: &mut egui::Ui, index: &mut usize, ediui_kit::text(ui, tr("material.editor.unknown_card"), ui_kit::Text::Note("unknown_card"));}let validation = draft.command(&app.session.doc);+ // A refused Save's message is about the command it ran and the design it ran on (#641):+ // once the draft would run another, or the design changed, it goes.+ if editor+ .refused+ .as_ref()+ .is_some_and(|(revision, was)| (*revision, was.as_ref()) != (app.session.revision, validation.as_ref().ok().and_then(|c| c.as_ref())))+ {+ editor.error = None;+ editor.refused = None;+ }if let Err(error) = &validation {ui_kit::text(ui, error, ui_kit::Text::Report("physical_validation", true));}@@ -515,9 +542,10 @@ pub(crate) fn rows(app: &mut SolveApp, ui: &mut egui::Ui, index: &mut usize, ediif discard {editor.draft = None;editor.error = None;+ editor.refused = None;}if save && let Err(error) = editor.save(app, index) {- editor.error = Some(error);+ editor.refuse(error, app.session.revision, &app.session.doc);}if let Some(error) = &editor.error {ui_kit::text(ui, error, ui_kit::Text::Report("physical_save_error", true));diff --git a/crates/ui-egui/src/params_dialog.rs b/crates/ui-egui/src/params_dialog.rsindex 85c77428db..f50573b1f2 100644--- a/crates/ui-egui/src/params_dialog.rs+++ b/crates/ui-egui/src/params_dialog.rs@@ -52,6 +52,9 @@ pub struct ParamsDialog {pub new_comment: String,/// The last error from an action in the dialog.pub error: Option<String>,+ /// Why adding the add row's draft was refused, kept while the draft and the design document+ /// stay.+ new_refused: Option<NewRefused>,/// The expression edit to preview: (name, expression).pub preview: Option<(String, String)>,/// The rows as the engine listed them at one revision of the design (#2723): listing@@ -87,6 +90,12 @@ impl ParamsDialog {pub fn new() -> Self {ParamsDialog { reset_scroll: true, ..Default::default() }}++ /// What the window's banner says: the last action's message, else why the add row's draft+ /// was refused.+ pub fn banner(&self) -> Option<&str> {+ self.error.as_deref().or(self.new_refused.as_ref().map(|r| r.error.as_str()))+ }/// The command the live preview should run (a valid expression being edited).pub fn preview_commands(&self) -> Option<Vec<(String, Value)>> {let (n, e) = self.preview.as_ref()?;@@ -209,6 +218,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {publish(app, ctx);}let Some(mut d) = app.params.take() else { return };+ forget_refused_add(app, &mut d);d.sort = ctx.data_mut(|m| m.get_persisted(sort_id())).unwrap_or_default();let sort = d.sort;ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));@@ -287,7 +297,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {}}});- if let Some(error) = &d.error {+ if let Some(error) = d.banner() {ui_kit::text(ui, error, ui_kit::Text::Error("parameters_error"));}let filter = d.filter.to_ascii_lowercase();@@ -710,6 +720,50 @@ fn apply_live_checkbox(app: &mut SolveApp, ui: &mut egui::Ui, d: &mut ParamsDial}}+/// The add row's draft (name, unit, expression, comment) an add was refused with, the design+/// document the refusal was about, and the message. The message is about that draft in that+/// design, as a dialog's refused OK is about its inputs and the design (#641): once either+/// changes, the message goes and the expression cell's own live check says what is wrong now, if+/// anything. A design change replaces the session's document (`Session::execute` keeps the one a+/// command started from, so an edit lands at a new address, and a refused command puts the old+/// one back); a selection or display change keeps it, though it moves the session's revision.+/// The row edits' messages live in `ParamsDialog::error`, apart from this one, so neither can+/// be taken for the other.+#[derive(Debug)]+struct NewRefused {+ draft: (String, usize, String, String),+ doc: Arc<solidcraft_engine::doc::Document>,+ error: String,+}++impl NewRefused {+ /// Whether the add row still holds the draft the refusal was about.+ fn same_draft(&self, d: &ParamsDialog) -> bool {+ self.draft.0 == d.new_name && self.draft.1 == d.new_unit && self.draft.2 == d.new_expr && self.draft.3 == d.new_comment+ }+}++fn new_draft(d: &ParamsDialog) -> (String, usize, String, String) {+ (d.new_name.clone(), d.new_unit, d.new_expr.clone(), d.new_comment.clone())+}++/// Show why the add was refused, for as long as the draft and the design stay as they are. It+/// takes the banner from an earlier action's message, as every action's result does.+fn refuse_add(app: &SolveApp, d: &mut ParamsDialog, error: String) {+ d.new_refused = Some(NewRefused { draft: new_draft(d), doc: app.session.doc.clone(), error });+ d.error = None;+}++/// A refused add's message gives way once its draft changes or the design document is another+/// one (the design document the refusal was about; a selection or display change keeps it).+fn forget_refused_add(app: &SolveApp, d: &mut ParamsDialog) {+ if let Some(was) = &d.new_refused+ && (!Arc::ptr_eq(&was.doc, &app.session.doc) || !was.same_draft(d))+ {+ d.new_refused = None;+ }+}+/// The add row, one cell per column of the user table it ends.fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>, d: &mut ParamsDialog, right: f32) {let name_id = ui.make_persistent_id("pnew_name");@@ -769,6 +823,7 @@ fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>,row.cell(|ui| {let response = ui_kit::bin_button(ui, "Delete");row_ids.push(response.id);+ crate::scenario::publish_handle("param:new:delete", response.rect.center());left |= response.lost_focus();discard = response.clicked();});@@ -806,7 +861,7 @@ fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>,return;}if let Err(error) = live {- d.error = Some(error);+ refuse_add(app, d, error);if let Some(id) = focused_id {ui.memory_mut(|m| m.request_focus(id));}@@ -823,7 +878,7 @@ fn add_row(app: &mut SolveApp, ui: &mut egui::Ui, table: &mut ui_kit::Table<'_>,d.focus_new = submit;}Err(error) => {- d.error = Some(error);+ refuse_add(app, d, error);// Enter stays in the field that was submitted. A click into another cell// has to keep that cell, or an invalid name cannot be corrected (#3965).if submit && let Some(id) = focused_id {@@ -1450,7 +1505,242 @@ mod tests {assert_eq!(h.app.params.as_ref().unwrap().new_unit, 1);h.step(&json!({"click": {"handle": "dialog:Filter"}})).unwrap();assert_eq!(h.app.session.doc.param("width").unwrap().unit, "cm");- assert!(h.app.params.as_ref().unwrap().error.is_none());+ assert!(h.app.params.as_ref().unwrap().banner().is_none());+ }++ /// A refused add's message is about the draft it ran with: correcting the draft takes the+ /// message away before any second Enter, for a refused `parameters.add` and for the+ /// expression's own live check alike.+ #[test]+ fn correcting_a_refused_new_parameter_clears_its_error() {+ let mut h = crate::scenario::Harness::new();+ h.app.home.open = false;+ h.app.run("parameters.add", json!({"name": "width", "expression": "10 mm"})).unwrap();+ h.step(&json!({"start": "parameters.change"})).unwrap();+ h.step(&json!({"click": {"handle": "dialog:table:user:add"}})).unwrap();+ h.step(&json!({"text": "width"})).unwrap();+ h.step(&json!({"click": {"handle": "param:new:expression"}})).unwrap();+ h.step(&json!({"text": "25 mm"})).unwrap();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ let error = h.app.params.as_ref().unwrap().banner().map(str::to_owned);+ assert!(error.as_deref().is_some_and(|e| e.contains("already a parameter")), "a taken name is refused: {error:?}");+ h.step(&json!({"click": {"handle": "param:new:name"}})).unwrap();+ h.step(&json!({"text": "2"})).unwrap();+ h.frames(2);+ let d = h.app.params.as_ref().unwrap();+ assert_eq!(d.new_name, "width2");+ assert_eq!(d.banner(), None, "the message was about the name `width`, which is gone");+ // The expression's live check: Enter on an unfinished expression shows it in the banner,+ // and correcting the expression takes it away.+ h.step(&json!({"click": {"handle": "param:new:expression"}})).unwrap();+ h.step(&json!({"key": "A", "cmd": true})).unwrap();+ h.step(&json!({"text": "10 +"})).unwrap();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ let error = h.app.params.as_ref().unwrap().banner().map(str::to_owned);+ assert!(error.is_some(), "an unfinished expression is refused: {error:?}");+ assert!(h.app.session.doc.param("width2").is_none());+ h.step(&json!({"click": {"handle": "param:new:expression"}})).unwrap();+ h.step(&json!({"key": "A", "cmd": true})).unwrap();+ h.step(&json!({"text": "10 + 5"})).unwrap();+ h.frames(2);+ assert_eq!(h.app.params.as_ref().unwrap().banner(), None, "the message was about `10 +`, which is gone");+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert_eq!(h.app.session.doc.param("width2").unwrap().expr, "10 + 5");+ }++ /// The dialog with `width` and `depth` saved, and the add row holding the draft `width`,+ /// `25 mm`, not yet submitted.+ fn taken_name_draft() -> crate::scenario::Harness {+ let mut h = crate::scenario::Harness::new();+ h.app.home.open = false;+ h.app.run("parameters.add", json!({"name": "width", "expression": "10 mm"})).unwrap();+ h.app.run("parameters.add", json!({"name": "depth", "expression": "5 mm"})).unwrap();+ h.step(&json!({"start": "parameters.change"})).unwrap();+ h.step(&json!({"click": {"handle": "dialog:table:user:add"}})).unwrap();+ h.step(&json!({"text": "width"})).unwrap();+ h.step(&json!({"click": {"handle": "param:new:expression"}})).unwrap();+ h.step(&json!({"text": "25 mm"})).unwrap();+ h+ }++ fn banner(h: &crate::scenario::Harness) -> Option<String> {+ h.app.params.as_ref().unwrap().banner().map(str::to_owned)+ }++ #[test]+ fn discarding_a_refused_new_parameter_takes_its_message_away() {+ let mut h = taken_name_draft();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert!(banner(&h).is_some_and(|e| e.contains("already a parameter")), "{:?}", banner(&h));+ h.step(&json!({"click": {"handle": "param:new:delete"}})).unwrap();+ h.frames(2);+ let d = h.app.params.as_ref().unwrap();+ assert!(!d.adding && d.new_name.is_empty(), "the row is gone");+ assert_eq!(d.banner(), None, "and its message with it");+ }++ #[test]+ fn a_new_parameter_refused_on_leaving_the_row_clears_its_message_when_corrected() {+ let mut h = taken_name_draft();+ h.step(&json!({"click": {"handle": "dialog:Filter"}})).unwrap();+ h.frames(2);+ assert!(banner(&h).is_some_and(|e| e.contains("already a parameter")), "{:?}", banner(&h));+ assert_eq!(h.app.session.doc.param("width").unwrap().expr, "10 mm", "nothing was added or changed");+ h.step(&json!({"click": {"handle": "param:new:name"}})).unwrap();+ h.step(&json!({"text": "2"})).unwrap();+ h.frames(2);+ assert_eq!(h.app.params.as_ref().unwrap().new_name, "width2");+ assert_eq!(banner(&h), None, "the message was about the name `width`, which is gone");+ }++ /// A design change can make a refused draft fine (here `width` is removed by a command, as an+ /// agent or Undo would): the message goes with the design it was about, and Enter adds.+ #[test]+ fn a_refused_new_parameter_is_checked_again_when_the_design_changes() {+ let mut h = taken_name_draft();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert!(banner(&h).is_some_and(|e| e.contains("already a parameter")), "{:?}", banner(&h));+ let doc = h.app.session.doc.clone();+ h.app.run("parameters.delete", json!({"name": "width"})).unwrap();+ h.frames(2);+ assert!(!Arc::ptr_eq(&doc, &h.app.session.doc), "a design change replaces the session's document");+ assert_eq!(banner(&h), None, "the message was about a design that had `width`");+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert_eq!(h.app.session.doc.param("width").unwrap().expr, "25 mm");+ }++ /// A row's own refusal with the very same words (renaming `depth` to the taken `width`) is+ /// not the add row's: it leaves the add row's refusal in place (the refused command puts the+ /// document it started from back), and correcting the draft leaves the rename's message+ /// where it is. A rename's message clears only through a rename that succeeds, which changes+ /// the design, so the refusal showing again once a row's message is cleared is in+ /// `the_banner_shows_the_newer_of_a_rows_message_and_the_add_rows_refusal`.+ #[test]+ fn correcting_a_refused_new_parameter_keeps_a_rows_message() {+ let mut h = taken_name_draft();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert!(banner(&h).is_some_and(|e| e.contains("already a parameter")), "{:?}", banner(&h));+ let (doc, revision) = (h.app.session.doc.clone(), h.app.session.revision);+ // Let the clicks that filled the draft age, or egui counts the double-click as a triple.+ h.frames(30);+ h.run(&json!([{"click": {"handle": "param:name:depth"}, "double": true}, {"key": "A", "cmd": true}, {"text": "width"}])).unwrap();+ assert!(h.app.params.as_ref().unwrap().renaming.is_some(), "double click enters rename mode");+ // The rename commits when its box loses the focus, here to the add row's name box.+ h.step(&json!({"click": {"handle": "param:new:name"}})).unwrap();+ h.frames(2);+ let d = h.app.params.as_ref().unwrap();+ assert!(d.error.as_deref().is_some_and(|e| e.contains("already a parameter `width`")), "the rename is refused: {:?}", d.error);+ assert_ne!(revision, h.app.session.revision, "a refused rename restores the design and refreshes it, which moves the revision");+ assert!(Arc::ptr_eq(&doc, &h.app.session.doc), "but the restore puts the document the rename started from back");+ assert!(d.new_refused.is_some(), "so the draft's own refusal stays, under the rename's message");+ assert_eq!(h.app.session.doc.param("depth").unwrap().expr, "5 mm");+ h.step(&json!({"text": "2"})).unwrap();+ h.frames(2);+ let d = h.app.params.as_ref().unwrap();+ assert_eq!(d.new_name, "width2");+ assert!(d.new_refused.is_none(), "the draft's own refusal goes with the draft");+ assert!(d.error.as_deref().is_some_and(|e| e.contains("already a parameter `width`")), "the rename's message stays: {:?}", d.error);+ assert_eq!(d.banner(), d.error.as_deref(), "and is what the banner shows");+ }++ /// A click on a body in the viewport selects it (`select.set`), which moves the session's+ /// revision but not the design: the message is about the draft in the design, so it stays.+ #[test]+ fn selecting_a_body_keeps_a_refused_new_parameters_message() {+ let mut h = taken_name_draft();+ h.app.run("solid.box", json!({"length": 10, "width": 20, "height": 30})).unwrap();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some("there is already a parameter `width`"));+ let (doc, revision) = (h.app.session.doc.clone(), h.app.session.revision);+ h.app.run("select.set", json!({"items": [solidcraft_engine::Sel::Body { name: "Body1".into() }]})).unwrap();+ h.frames(2);+ assert_eq!(h.app.session.selection.len(), 1, "the body is selected");+ assert_ne!(revision, h.app.session.revision, "a selection moves the revision");+ assert!(Arc::ptr_eq(&doc, &h.app.session.doc), "and leaves the document alone");+ assert_eq!(banner(&h).as_deref(), Some("there is already a parameter `width`"), "so the message stays");+ assert!(h.app.params.as_ref().unwrap().new_refused.is_some());+ }++ /// A unit or comment edit changes the draft the message was about, so the message goes with+ /// it, although neither cures a taken name: Enter checks again and says so again.+ #[test]+ fn a_unit_or_comment_edit_on_a_refused_new_parameter_clears_its_message() {+ let mut h = taken_name_draft();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some("there is already a parameter `width`"));+ h.step(&json!({"click": {"handle": "param:new:unit"}})).unwrap();+ h.step(&json!({"click": {"handle": "dialog:Unit:cm"}})).unwrap();+ h.frames(2);+ assert_eq!(h.app.params.as_ref().unwrap().new_unit, 1);+ assert_eq!(banner(&h), None, "the message was about the draft in mm");+ // The unit menu holds the focus after a pick; Enter submits from one of the row's boxes.+ h.step(&json!({"click": {"handle": "param:new:expression"}})).unwrap();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some("there is already a parameter `width`"), "Enter checks the draft again");+ h.step(&json!({"click": {"handle": "param:new:comment"}})).unwrap();+ h.step(&json!({"text": "Overall width"})).unwrap();+ h.frames(2);+ assert_eq!(h.app.params.as_ref().unwrap().new_comment, "Overall width");+ assert_eq!(banner(&h), None, "the message was about the draft without a comment");+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some("there is already a parameter `width`"), "Enter checks the draft again");+ assert_eq!(h.app.session.doc.param("width").unwrap().expr, "10 mm", "nothing was added");+ }++ /// With a row's message and the add row's refusal both alive, the banner shows the newer one.+ /// A row's expression that fails its check runs no design-changing command, so the document+ /// stays and the add row's refusal stays with it, under the row's message; once the row is+ /// corrected, the refusal shows again with no new Enter; and a refusal after a row's message+ /// takes the banner back, as any action's result did. The edits are held (#9279): in the live+ /// mode a row's message clears only through a `parameters.change` that succeeds, which changes+ /// the design and drops the refusal too. The draft's expression is unfinished, so leaving the+ /// add row attempts nothing (a valid draft is attempted on every click outside the row).+ #[test]+ fn the_banner_shows_the_newer_of_a_rows_message_and_the_add_rows_refusal() {+ let mut h = crate::scenario::Harness::new();+ h.app.home.open = false;+ h.app.preferences.parameters_apply_live = false;+ h.app.run("parameters.add", json!({"name": "depth", "expression": "5 mm"})).unwrap();+ h.step(&json!({"start": "parameters.change"})).unwrap();+ h.step(&json!({"click": {"handle": "dialog:table:user:add"}})).unwrap();+ h.step(&json!({"text": "height"})).unwrap();+ h.step(&json!({"click": {"handle": "param:new:expression"}})).unwrap();+ h.step(&json!({"text": "10 +"})).unwrap();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ let refusal = banner(&h).expect("an unfinished expression is refused");+ assert!(h.app.session.doc.param("height").is_none(), "nothing was added");+ let doc = h.app.session.doc.clone();+ h.run(&json!([{"click": {"handle": "param:depth"}}, {"key": "A", "cmd": true}, {"text": "missing + 1 mm"}, {"key": "Enter"}])).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some("unknown parameter `missing`"), "the row's message is the newer one");+ assert!(h.app.params.as_ref().unwrap().new_refused.is_some(), "the add row's refusal is alive under it");+ assert!(Arc::ptr_eq(&doc, &h.app.session.doc), "a failed check leaves the document alone");+ h.run(&json!([{"click": {"handle": "param:depth"}}, {"key": "A", "cmd": true}, {"text": "6 mm"}, {"key": "Enter"}])).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some(refusal.as_str()), "the correction clears the row's message and the refusal shows again, no Enter");+ assert_eq!(h.app.params.as_ref().unwrap().editing.get("depth").map(String::as_str), Some("6 mm"), "the held edit waits for Apply");+ assert!(Arc::ptr_eq(&doc, &h.app.session.doc), "and the design is as it was");+ h.run(&json!([{"click": {"handle": "param:depth"}}, {"key": "A", "cmd": true}, {"text": "missing + 1 mm"}, {"key": "Enter"}])).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some("unknown parameter `missing`"), "the row's message is the newer one again");+ h.step(&json!({"click": {"handle": "param:new:expression"}})).unwrap();+ h.step(&json!({"key": "Enter"})).unwrap();+ h.frames(2);+ assert_eq!(banner(&h).as_deref(), Some(refusal.as_str()), "the add row's refusal is the newer one");+ assert_eq!(h.app.params.as_ref().unwrap().editing.get("depth").map(String::as_str), Some("missing + 1 mm"), "the row's edit is kept");+ assert_eq!(h.app.session.doc.param("depth").unwrap().expr, "5 mm");}/// #8563: sorting orders each section, and each model group, by itself.@@ -1711,7 +2001,7 @@ mod tests {h.step(&json!({"key": "Enter"})).unwrap();h.frames(2);assert!(h.app.session.doc.params.iter().all(|p| p.name != "width"), "no expression: nothing added");- assert_eq!(h.app.params.as_ref().unwrap().error, None, "and nothing wrong is said");+ assert_eq!(h.app.params.as_ref().unwrap().banner(), None, "and nothing wrong is said");h.step(&json!({"text": "25 mm"})).unwrap();assert_eq!(h.app.params.as_ref().unwrap().new_expr, "25 mm", "Enter went on to the expression");h.step(&json!({"key": "Enter"})).unwrap();diff --git a/crates/ui-egui/tests/parameter_enter.rs b/crates/ui-egui/tests/parameter_enter.rsindex fed8dfad2e..6b751b2072 100644--- a/crates/ui-egui/tests/parameter_enter.rs+++ b/crates/ui-egui/tests/parameter_enter.rs@@ -39,7 +39,7 @@ fn parameter_enter_adds_once_and_focuses_next_name() {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().error.is_none());+ 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);@@ -73,12 +73,12 @@ fn parameter_enter_invalid_preserves_draft_and_other_rows() {let d = h.app.params.as_ref().unwrap();assert_eq!(d.new_name, "bad");assert_eq!(d.new_expr, "missing + 1 mm");- assert!(d.error.is_some());+ 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().error.is_some());+ assert!(h.app.params.as_ref().unwrap().banner().is_some());}#[test]fn parameter_enter_edits_expression_without_adding_draft() {@@ -93,7 +93,7 @@ fn parameter_enter_edits_expression_without_adding_draft() {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().error.is_none(), "Enter in the existing row never attempts the invalid draft");+ 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"));@@ -121,7 +121,7 @@ fn parameter_enter_comment_uses_selected_unit_and_unrelated_focus_does_not_submih.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().error.is_none(), "unrelated Enter never attempts the pending draft");+ 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();diff --git a/docs/ui-kit.md b/docs/ui-kit.mdindex b1a9a8f01a..6959692567 100644--- a/docs/ui-kit.md+++ b/docs/ui-kit.md@@ -605,7 +605,8 @@ The natural scrolling body height is published as `dialog:body:content_height`,so geometry tests distinguish content size from the viewport cap.After a successful parameter add, a fresh empty draft retains Name focus forcontinuous keyboard entry; leaving that empty row drops it.-Parameter draft fields retain `param:new:name/unit/expression/comment`.+Parameter draft fields retain `param:new:name/unit/expression/comment`, and the draft's+bin button publishes `param:new:delete`.Configuration drafts publish `config:new:name`; existing `config:cell:ROW:COLUMN`,activation, rule and preview handles remain. The header plus retains`dialog:menu:Add Column`, including the existing menu nesting. The old separate