Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Parameters: an option to apply edits together, when you choose (#9279) (#9400)

PaddleStroke0096e694ddd4
diff --git a/assets/locales/pending/parameters-apply-later.json b/assets/locales/pending/parameters-apply-later.json
new file mode 100644
index 0000000000..aab1be39ba
--- /dev/null
+++ b/assets/locales/pending/parameters-apply-later.json
@@ -0,0 +1,58 @@
+{
+ "en": {
+ "parameters.held_hint": "Edit expressions and units. Changes wait for Apply or OK.",
+ "parameters.apply_live": "Apply each change as you leave its field",
+ "parameters.apply_live_tip": "Off: changes wait until Apply or OK, then the design rebuilds once for all of them. Cancel or Escape drops them.",
+ "parameters.held": "{count} changes waiting for Apply",
+ "parameters.held_one": "1 change waiting for Apply"
+ },
+ "ar": {
+ "parameters.held_hint": "عدّل التعابير والوحدات. تنتظر التغييرات «تطبيق» أو «موافق».",
+ "parameters.apply_live": "طبّق كل تغيير عند مغادرة حقله",
+ "parameters.apply_live_tip": "عند الإيقاف: تنتظر التغييرات حتى «تطبيق» أو «موافق»، ثم يُعاد بناء التصميم مرة واحدة لها جميعًا. «إلغاء» أو Escape يتجاهلها.",
+ "parameters.held": "{count} تغييرات بانتظار «تطبيق»",
+ "parameters.held_one": "تغيير واحد بانتظار «تطبيق»"
+ },
+ "de": {
+ "parameters.held_hint": "Bearbeiten Sie Ausdrücke und Einheiten. Änderungen warten auf Übernehmen oder OK.",
+ "parameters.apply_live": "Jede Änderung beim Verlassen des Feldes übernehmen",
+ "parameters.apply_live_tip": "Aus: Änderungen warten auf Übernehmen oder OK, dann wird das Modell einmal für alle neu aufgebaut. Abbrechen oder Esc verwirft sie.",
+ "parameters.held": "{count} Änderungen warten auf Übernehmen",
+ "parameters.held_one": "1 Änderung wartet auf Übernehmen"
+ },
+ "fr": {
+ "parameters.held_hint": "Modifiez les expressions et les unités. Les modifications attendent Appliquer ou OK.",
+ "parameters.apply_live": "Appliquer chaque modification en quittant son champ",
+ "parameters.apply_live_tip": "Désactivé : les modifications attendent Appliquer ou OK, puis le modèle est reconstruit une seule fois pour toutes. Annuler ou Échap les abandonne.",
+ "parameters.held": "{count} modifications en attente d’Appliquer",
+ "parameters.held_one": "1 modification en attente d’Appliquer"
+ },
+ "he": {
+ "parameters.held_hint": "עריכת ביטויים ויחידות. השינויים ממתינים ל„החל” או ל„אישור”.",
+ "parameters.apply_live": "להחיל כל שינוי ביציאה מהשדה שלו",
+ "parameters.apply_live_tip": "כבוי: השינויים ממתינים עד ל„החל” או „אישור”, ואז התכן נבנה מחדש פעם אחת עבור כולם. „ביטול” או Escape משליכים אותם.",
+ "parameters.held": "{count} שינויים ממתינים ל„החל”",
+ "parameters.held_one": "שינוי אחד ממתין ל„החל”"
+ },
+ "ja": {
+ "parameters.held_hint": "式と単位を編集してください。変更は「適用」または「OK」まで保留されます。",
+ "parameters.apply_live": "フィールドを離れるたびに変更を適用",
+ "parameters.apply_live_tip": "オフ: 変更は「適用」または「OK」まで保留され、その後すべてをまとめて一度だけ再構築します。「キャンセル」または Esc で破棄します。",
+ "parameters.held": "{count} 件の変更が適用待ちです",
+ "parameters.held_one": "1 件の変更が適用待ちです"
+ },
+ "vi": {
+ "parameters.held_hint": "Sửa biểu thức và đơn vị. Thay đổi chờ đến khi nhấn Áp dụng hoặc OK.",
+ "parameters.apply_live": "Áp dụng mỗi thay đổi khi rời khỏi ô của nó",
+ "parameters.apply_live_tip": "Tắt: các thay đổi chờ đến khi nhấn Áp dụng hoặc OK, rồi thiết kế được dựng lại một lần cho tất cả. Hủy hoặc Escape sẽ bỏ chúng.",
+ "parameters.held": "{count} thay đổi đang chờ Áp dụng",
+ "parameters.held_one": "1 thay đổi đang chờ Áp dụng"
+ },
+ "zh": {
+ "parameters.held_hint": "编辑表达式和单位。更改将等到“应用”或“确定”时生效。",
+ "parameters.apply_live": "离开字段时立即应用每项更改",
+ "parameters.apply_live_tip": "关闭时:更改会等到“应用”或“确定”,然后设计只为所有更改重建一次。“取消”或 Esc 会放弃这些更改。",
+ "parameters.held": "{count} 项更改等待应用",
+ "parameters.held_one": "1 项更改等待应用"
+ }
+}
diff --git a/crates/ui-egui/src/params_dialog.rs b/crates/ui-egui/src/params_dialog.rs
index 1ea2299659..2b652ebed4 100644
--- a/crates/ui-egui/src/params_dialog.rs
+++ b/crates/ui-egui/src/params_dialog.rs
@@ -5,6 +5,8 @@
//! the sketch or feature they drive (editable). The Name heading sorts each section, and each
//! model group, by name in natural order (#8563). Expressions validate as you type
//! (`expr.evaluate`) with a live model preview; Enter or leaving the field applies them.
+//! With "Apply each change as you leave its field" off (a remembered preference, #9279), they
+//! wait instead, without a preview, until Apply or OK sets them in one `parameters.change`.
//! Escape closes the window and discards pending edits and their preview.
//! Import and export go through `parameters.import` / `parameters.export`.
//!
@@ -231,9 +233,11 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
d.lists += 1;
rows
};
- let mut close = false;
+ let mut outcome = (false, ui_kit::Action::None);
+ let live = app.preferences.parameters_apply_live;
+ let held_status = if live { None } else { held_status(held(&d, &all).len()) };
panel.window(ctx).show(ctx, |ui| {
- let (closed, action) = panel.feature(
+ outcome = panel.feature(
ui,
ui_kit::FeatureOptions {
icon: solidcraft_engine::find_command("parameters.change").map_or("", |c| c.icon),
@@ -241,12 +245,13 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
drag_key: "PARAMETERS",
moved,
docked,
- apply_label: None,
+ apply_label: (!live).then_some("Apply"),
},
false,
true,
|ui| {
- ui_kit::text(ui, crate::i18n::tr("manage.parameters_hint"), ui_kit::Text::Hint("manage"));
+ ui_kit::text(ui, hint(live), ui_kit::Text::Hint("manage"));
+ apply_live_checkbox(app, ui, &mut d, &all);
ui_kit::row(ui, "Filter", |ui| {
let id = ui.make_persistent_id("parameter_filter");
ui_kit::search_field(ui, id, &mut d.filter, "Filter");
@@ -321,12 +326,15 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
});
});
});
- ui_kit::ComputeStatus { text: None, busy: false, applying: false }
+ ui_kit::ComputeStatus { text: held_status, busy: false, applying: false }
},
);
d.reset_scroll = false;
- close = closed || matches!(action, ui_kit::Action::Ok | ui_kit::Action::Cancel);
});
+ // Held edits (#9279) are set by Apply and OK; Cancel, the close button and Escape drop them.
+ let (closed, action) = outcome;
+ let applied = live || !matches!(action, ui_kit::Action::Ok | ui_kit::Action::Apply) || apply_held(app, &mut d, &all);
+ let close = closed || action == ui_kit::Action::Cancel || (action == ui_kit::Action::Ok && applied);
// Each cell owns fresh Enter from raw events. The panel itself must not submit on
// Enter in the filter or with unrelated focus (the legacy editor left those alone), but
// the filter, rename and comment fields still see Enter first to give up focus.
@@ -562,13 +570,18 @@ fn table(app: &mut SolveApp, ui: &mut egui::Ui, d: &mut ParamsDialog, rows: &[&R
if response.changed() || completed {
d.editing.insert(r.name.clone(), expression.clone());
}
- if response.has_focus() && matches!(live, Some(Ok(_))) {
+ // Held edits (#9279) preview nothing: the preview is a rebuild too.
+ let apply_live = app.preferences.parameters_apply_live;
+ if apply_live && response.has_focus() && matches!(live, Some(Ok(_))) {
d.preview = Some((r.name.clone(), expression.clone()));
}
let entered = !completed && enter_for(ui, focused);
if !completed && (response.lost_focus() || entered) {
d.preview = None;
- if expression != r.expression {
+ if expression != r.expression && !apply_live {
+ // Kept in `editing` for Apply or OK, checked now.
+ d.error = check(app, &expression, &r.unit).err();
+ } else if expression != r.expression {
d.error = check(app, &expression, &r.unit)
.and_then(|_| app.run("parameters.change", json!({"name":r.name,"expression":expression})))
.err();
@@ -637,6 +650,56 @@ fn enter_for(ui: &mut egui::Ui, focused: bool) -> bool {
})
}
+/// The expression edits waiting for Apply (#9279): each draft that differs from the design.
+fn held(d: &ParamsDialog, rows: &[Row]) -> serde_json::Map<String, Value> {
+ rows.iter().filter_map(|r| d.editing.get(&r.name).filter(|e| **e != r.expression).map(|e| (r.name.clone(), json!(e)))).collect()
+}
+
+/// The hint at the top of the dialog: when edits apply, as the check box under it says.
+fn hint(live: bool) -> &'static str {
+ crate::i18n::tr(if live { "manage.parameters_hint" } else { "parameters.held_hint" })
+}
+
+/// The footer's note of the edits waiting for Apply, if any.
+fn held_status(count: usize) -> Option<String> {
+ match count {
+ 0 => None,
+ 1 => Some(crate::i18n::tr("parameters.held_one").to_string()),
+ n => Some(crate::i18n::format("parameters.held", &[("count", &n.to_string())])),
+ }
+}
+
+/// Set the edits waiting for Apply together: one `parameters.change`, so the design rebuilds once
+/// and one undo takes them all back. A single edit goes as `name, expression`, which can also
+/// rename (`new = …`). False, with the error shown and the edits kept, when it is refused.
+fn apply_held(app: &mut SolveApp, d: &mut ParamsDialog, rows: &[Row]) -> bool {
+ let changes = held(d, rows);
+ let params = match changes.iter().next() {
+ None => return true,
+ Some((name, expression)) if changes.len() == 1 => json!({"name": name, "expression": expression}),
+ Some(_) => json!({"changes": changes}),
+ };
+ d.error = app.run("parameters.change", params).err();
+ if d.error.is_some() {
+ return false;
+ }
+ for name in changes.keys() {
+ d.editing.remove(name);
+ }
+ true
+}
+
+/// "Apply each change as you leave its field", remembered across sessions (#9279). Turning it
+/// back on applies what was waiting.
+fn apply_live_checkbox(app: &mut SolveApp, ui: &mut egui::Ui, d: &mut ParamsDialog, rows: &[Row]) {
+ let response = ui_kit::checkbox(ui, crate::i18n::tr("parameters.apply_live"), &mut app.preferences.parameters_apply_live)
+ .on_hover_text(crate::i18n::tr("parameters.apply_live_tip"));
+ crate::scenario::publish_handle("params:apply_live", response.rect.center());
+ if response.changed() && app.preferences.parameters_apply_live {
+ apply_held(app, d, rows);
+ }
+}
+
/// 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");
@@ -1083,6 +1146,106 @@ mod tests {
assert_eq!(escape_state(&h), before);
}
+ /// #9279: a box driven by `w` and `h`, the Parameters dialog open with live updates off.
+ fn held_dialog() -> crate::scenario::Harness {
+ 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":"w","expression":"10 mm"})).unwrap();
+ h.app.run("parameters.add", json!({"name":"h","expression":"20 mm"})).unwrap();
+ h.app.run("solid.box", json!({"length":"w","width":"w","height":"h"})).unwrap();
+ h.step(&json!({"start":"parameters.change"})).unwrap();
+ h.frames(3);
+ h
+ }
+
+ fn expression(h: &crate::scenario::Harness, name: &str) -> String {
+ h.app.session.doc.param(name).map(|p| p.expr.clone()).unwrap_or_default()
+ }
+
+ /// Type into a parameter's expression and leave the field, as the user moves on.
+ fn hold(h: &mut crate::scenario::Harness, name: &str, text: &str) {
+ escape_type(h, &format!("param:{name}"), text);
+ h.step(&json!({"click":{"handle":"dialog:Filter"}})).unwrap();
+ }
+
+ #[test]
+ fn held_edits_wait_for_apply_and_go_in_together_as_one_undo_step() {
+ let mut h = held_dialog();
+ let (revision, undo) = (h.app.session.revision, h.app.session.undo.len());
+ hold(&mut h, "w", "30 mm");
+ hold(&mut h, "h", "w * 2");
+ assert_eq!((expression(&h, "w").as_str(), expression(&h, "h").as_str()), ("10 mm", "20 mm"), "leaving a field changes nothing yet");
+ assert_eq!((h.app.session.revision, h.app.session.undo.len()), (revision, undo), "no rebuild and no undo step while held");
+ let d = h.app.params.as_ref().unwrap();
+ assert!(d.preview.is_none() && d.error.is_none());
+ assert_eq!(d.editing.len(), 2);
+ h.step(&json!({"click":{"handle":"dialog:Apply"}})).unwrap();
+ assert_eq!((expression(&h, "w").as_str(), expression(&h, "h").as_str()), ("30 mm", "w * 2"));
+ assert_eq!(h.app.session.undo.len(), undo + 1, "one undo step for both");
+ let d = h.app.params.as_ref().expect("Apply keeps the dialog open");
+ assert!(d.editing.is_empty() && d.error.is_none());
+ h.app.run("edit.undo", json!({})).unwrap();
+ assert_eq!((expression(&h, "w").as_str(), expression(&h, "h").as_str()), ("10 mm", "20 mm"), "one undo takes both back");
+ }
+
+ #[test]
+ fn held_edits_go_in_with_ok_and_are_dropped_by_cancel_or_escape() {
+ let mut h = held_dialog();
+ let before = escape_state(&h);
+ hold(&mut h, "w", "30 mm");
+ h.step(&json!({"click":{"handle":"dialog:Cancel"}})).unwrap();
+ assert!(h.app.params.is_none());
+ assert_eq!(escape_state(&h), before, "Cancel drops held edits");
+ h.step(&json!({"start":"parameters.change"})).unwrap();
+ h.frames(2);
+ hold(&mut h, "w", "30 mm");
+ escape_key(&mut h, true);
+ assert!(h.app.params.is_none());
+ assert_eq!(escape_state(&h), before, "Escape drops held edits");
+ escape_key(&mut h, false);
+ h.step(&json!({"start":"parameters.change"})).unwrap();
+ h.frames(2);
+ hold(&mut h, "w", "30 mm");
+ h.step(&json!({"click":{"handle":"dialog:OK"}})).unwrap();
+ assert!(h.app.params.is_none());
+ assert_eq!(expression(&h, "w"), "30 mm", "OK applies held edits");
+ }
+
+ #[test]
+ fn a_refused_held_edit_keeps_the_dialog_open_with_its_error() {
+ let mut h = held_dialog();
+ hold(&mut h, "w", "30 mm");
+ hold(&mut h, "h", "nonexistent * 2");
+ assert!(h.app.params.as_ref().unwrap().error.is_some(), "a held edit is checked when its field is left");
+ h.step(&json!({"click":{"handle":"dialog:OK"}})).unwrap();
+ let d = h.app.params.as_ref().expect("OK keeps the dialog open when the edits are refused");
+ assert!(d.error.is_some());
+ assert_eq!(d.editing.len(), 2, "the held edits are kept to correct");
+ assert_eq!(expression(&h, "w"), "10 mm", "all or nothing");
+ }
+
+ #[test]
+ fn the_apply_live_choice_is_remembered_and_turning_it_on_applies_what_waits() {
+ assert!(crate::prefs::Prefs::default().parameters_apply_live, "live updates stay the default");
+ // The hint above the check box says when edits apply, whichever way it is set.
+ assert_eq!(hint(true), crate::i18n::tr("manage.parameters_hint"));
+ assert_ne!(hint(false), hint(true));
+ assert!(hint(false).contains("Apply") && !hint(false).contains("leaving"), "{}", hint(false));
+ let mut h = held_dialog();
+ hold(&mut h, "w", "30 mm");
+ h.step(&json!({"click":{"handle":"params:apply_live"}})).unwrap();
+ assert!(h.app.preferences.parameters_apply_live);
+ assert_eq!(expression(&h, "w"), "30 mm");
+ assert!(h.app.params.as_ref().unwrap().editing.is_empty());
+ h.step(&json!({"click":{"handle":"params:apply_live"}})).unwrap();
+ assert!(!h.app.preferences.parameters_apply_live);
+ let saved = h.app.prefs();
+ let mut other = crate::scenario::Harness::new();
+ other.app.load_prefs(&saved);
+ assert!(!other.app.preferences.parameters_apply_live, "the choice persists with the preferences");
+ }
+
#[test]
fn words_and_suggestions() {
assert_eq!(word_before("2 * wid", 7), (4, "wid".to_string()));
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index 4c8e708475..3eaa2e09df 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -1022,6 +1022,10 @@ preference_settings! {
/// view (`canvas::boxes_shown`). Off: it is typed in the command window only, which takes the
/// keyboard after a pick instead, and nothing covers the model (#9479).
value_boxes_in_view: bool = true, PAGE_DESIGN => check_id("prefs.value_boxes_in_view");
+ /// The Parameters dialog applies an expression as soon as its field is left (#9279). Off: the
+ /// edits wait in the dialog until Apply or OK sets them together, in one rebuild and one undo
+ /// step. Its checkbox is in the dialog itself.
+ parameters_apply_live: bool = true;
/// A design coming back to its tab reads again the linked designs whose files changed since
/// it last read them. Off: they wait for Update Links, as before.
update_changed_links: bool = true, PAGE_DESIGN => check_id("settings.update_changed_links");
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index b1b6062c4f..b5d560ae05 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -452,6 +452,7 @@ One setting per line, sorted by name. A change that adds a setting adds its own
- `origin_plane_names` (`xyz` (default) or `top_front_right`: the browser, dialogs and lists call the origin planes Top, Front and Right; their ids, commands and saved designs keep XY, XZ and YZ)
- `pan_reverse_x` (default false: horizontal scrolling pans the other way)
- `pan_reverse_y` (default false: vertical scrolling pans the other way)
+- `parameters_apply_live` (default true: the Parameters dialog applies an expression when its field is left; false: edits wait for Apply or OK, which set them together in one rebuild and one undo step)
- `perspective_fov` (Perspective view's vertical field of view in radians, 0.2618…1.5708 (15°…90°), default 0.6; when it changes, the camera's distance scales so the view keeps its size)
- `placement_handles` (default false: handles on the preview of a Rest, Boss or Snap Fit that move it across the model and turn it)
- `print_face_highlight` (0…1, default 0.2: how strongly a print face is tinted and outlined in the view, #8429; 0 draws no highlight)