From f06875128b677e0b130c91f1f576985bf64b1a7c Mon Sep 17 00:00:00 2001 From: Colin Faulkingham Date: Sun, 25 Oct 2026 01:25:04 +0000 Subject: [PATCH] Preserve the history marker when switching sketch editors (#5435) (#6103) * claim: #5435 * fix(ui): preserve history when switching sketch editors --- .../ui-egui/src/history_off_thread_tests.rs | 59 +++++++++++++++++++ crates/ui-egui/src/lib.rs | 28 +++++---- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/history_off_thread_tests.rs b/crates/ui-egui/src/history_off_thread_tests.rs index fd4c40218..f573ccb0c 100644 --- a/crates/ui-egui/src/history_off_thread_tests.rs +++ b/crates/ui-egui/src/history_off_thread_tests.rs @@ -126,6 +126,65 @@ fn a_design_changed_while_a_move_computes_is_computed_again() { assert_eq!(marker(&h), Some(2)); } +fn sketches_with_later_bodies() -> (Harness, Vec) { + let mut h = Harness::new(); + h.app.home.open = false; + let mut sketches = Vec::new(); + for x in [0, 20] { + let sketch = h.app.run("sketch.create", json!({"plane": "XY"})).unwrap()["sketch"].as_u64().unwrap(); + sketches.push(sketch); + h.app.run("sketch.line", json!({"points": [[x, 0], [x + 10, 0]]})).unwrap(); + h.app.finish_sketch(); + h.app.run("solid.box", json!({"corner": [x, 0, 0], "length": 10, "width": 10, "height": 10})).unwrap(); + } + h.app.run("solid.box", json!({"corner": [40, 0, 0], "length": 10, "width": 10, "height": 10})).unwrap(); + h.app.apply_off_thread = Some(Duration::ZERO); + (h, sketches) +} + +/// #5435: switching editors must not leave a background restore from the first sketch that +/// moves the second editor's marker, or make Finish restore to the first sketch's rollback. +#[test] +fn switching_sketches_keeps_the_original_marker_with_background_rebuilds() { + for original_marker in [None, Some(4)] { + for cancel in [false, true] { + let (mut h, sketches) = sketches_with_later_bodies(); + h.app.run("timeline.roll_to", json!({"position": original_marker})).unwrap(); + let bodies = h.app.session.model.state().bodies.len(); + h.app.edit_sketch(sketches[0]); + h.app.edit_sketch(sketches[1]); + until_settled(&mut h); + assert_eq!(h.app.session.active_sketch, Some(sketches[1])); + assert_eq!(marker(&h), Some(3), "stay at the sketch being edited"); + if cancel { + h.app.cancel_sketch(); + } else { + h.app.finish_sketch(); + } + until_settled(&mut h); + assert_eq!(marker(&h), original_marker, "restore the marker from before either edit"); + assert_eq!(h.app.session.model.state().bodies.len(), bodies, "all later bodies return"); + } + } +} + +#[test] +fn switching_from_a_sketch_to_a_feature_keeps_the_original_marker_with_background_rebuilds() { + let (mut h, sketches) = sketches_with_later_bodies(); + let feature = h.app.session.doc.features[3].id; + let bodies = h.app.session.model.state().bodies.len(); + h.app.edit_sketch(sketches[0]); + h.app.edit_feature(feature); + until_settled(&mut h); + assert_eq!(h.app.session.active_sketch, None); + assert_eq!(marker(&h), Some(3), "stay just before the feature being edited"); + assert_eq!(h.app.dialog.as_ref().and_then(|d| d.editing), Some((feature, None))); + crate::dialogs::cancel(&mut h.app); + until_settled(&mut h); + assert_eq!(marker(&h), None); + assert_eq!(h.app.session.model.state().bodies.len(), bodies, "all later bodies return"); +} + /// A control request for the app, and where its reply lands. fn ask(tx: &std::sync::mpsc::Sender, method: &str, params: serde_json::Value) -> std::sync::mpsc::Receiver { let (req, reply) = crate::ControlRequest::new(method, params); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index cda7d7fbb..e9e017d0b 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1194,11 +1194,8 @@ impl SolveApp { self.edit_sketch(id); return; } - if self.session.active_sketch.is_some() { - self.finish_sketch(); - } let Some(idx) = self.session.doc.feature_index(id) else { return }; - let marker = self.session.doc.marker; + let marker = self.finish_sketch_for_edit(); self.tool = None; if dialogs::roll_marker(self, Some(idx)).is_err() { return; @@ -1229,24 +1226,31 @@ impl SolveApp { if self.session.active_sketch == Some(id) { return; } - if self.session.active_sketch.is_some() { - self.finish_sketch(); - } + let Some(idx) = self.session.doc.feature_index(id) else { return }; + let marker = self.finish_sketch_for_edit(); self.tool = None; self.dialog = None; - let Some(idx) = self.session.doc.feature_index(id) else { return }; - let marker = self.session.doc.marker; - let rolled = marker != Some(idx + 1) && self.run("timeline.roll_to", json!({ "position": idx + 1 })).is_ok(); + let rolled = self.session.doc.marker != Some(idx + 1) && self.run("timeline.roll_to", json!({ "position": idx + 1 })).is_ok(); if self.run("sketch.edit", json!({ "sketch": id })).is_err() { - if rolled { + if rolled || marker != self.session.doc.marker { self.restore_marker(marker); } return; } - self.pre_sketch_marker = rolled.then_some(marker); + self.pre_sketch_marker = (rolled || marker != self.session.doc.marker).then_some(marker); dialogs::look_at_sketch(self); } + /// Pass the original marker to the next editor. Restoring it between editors could finish + /// in the background after the next rollback, moving the marker out from under that edit. + fn finish_sketch_for_edit(&mut self) -> Option { + let marker = self.pre_sketch_marker.take().unwrap_or(self.session.doc.marker); + if self.session.active_sketch.is_some() { + self.finish_sketch(); + } + marker + } + /// Finish the active sketch: stop editing and restore the history marker Edit Sketch moved. /// The camera stays where it is, however the sketch is left. pub fn finish_sketch(&mut self) {