diff --git a/crates/ui-egui/src/apply_off_thread_tests.rs b/crates/ui-egui/src/apply_off_thread_tests.rs index 4ac5dd4f0..b177fef68 100644 --- a/crates/ui-egui/src/apply_off_thread_tests.rs +++ b/crates/ui-egui/src/apply_off_thread_tests.rs @@ -103,6 +103,28 @@ fn cancel_while_computing_applies_nothing() { assert_eq!(h.app.session.doc.features.len(), features); } +#[test] +fn removing_the_edited_feature_discards_its_pending_result() { + let mut h = Harness::new(); + h.app.home.open = false; + h.app.run("solid.box", json!({"length":10,"width":10,"height":10,"name":"Block"})).unwrap(); + h.call("ui.editFeature", json!({"feature":"Block"})); + h.frames(2); + let prepared = result(&h); + let finish = hold_result(&mut h, false); + let progress = h.app.applying.as_ref().unwrap().progress.clone(); + h.app.run("edit.undo", json!({})).unwrap(); + let document = h.app.session.doc.clone(); + let history = (h.app.session.undo.len(), h.app.session.redo.len()); + h.frames(2); + assert!(h.app.dialog.is_none() && h.app.applying.is_none() && h.app.editing_state.is_none()); + assert!(progress.is_cancelled()); + assert!(finish.send(prepared).is_err(), "the removed editor cannot adopt a late result"); + h.frames(2); + assert!(std::sync::Arc::ptr_eq(&document, &h.app.session.doc)); + assert_eq!((h.app.session.undo.len(), h.app.session.redo.len()), history); +} + #[test] fn a_ready_transaction_waits_one_frame_before_adoption() { let mut h = square_on_a_block(); @@ -323,7 +345,7 @@ fn cancelling_an_edit_restores_pinned_geometry_without_evaluating_an_evicted_suf let history = (h.app.session.undo.len(), h.app.session.redo.len()); let visibility = h.app.session.visibility.clone(); h.call("ui.editFeature", json!({"feature":target})); - crate::dialogs::sync_feature_edit_sketch(&mut h.app); + crate::dialogs::sync_feature_edit(&mut h.app); let sketch = h.app.session.feature_edit_sketch.expect("edited Extrude exposes its sketch"); assert!(solidcraft_engine::view::sketch_shown(&h.app.session, sketch)); let prepared = result(&h); diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 1960574b3..d376dd622 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -3076,10 +3076,28 @@ fn picked_side(app: &SolveApp, d: &Dialog, first: &Sel) -> Option { Some(crate::smart_extrude::picked_direction(crate::smart_extrude::view_ray(&app.cam, at), normal, false)) } -/// Show the sketch of the Extrude, Revolve, sheet metal base or contour flange, or Hole at sketch +/// Discard an editor whose target was removed, before its preview can draw again. Show the sketch +/// of the Extrude, Revolve, sheet metal base or contour flange, or Hole at sketch /// points the open dialog edits, and only while it is open: whichever way the dialog closed (OK, /// Cancel, another tool, another design), the sketch returns to its own visibility. -pub(crate) fn sync_feature_edit_sketch(app: &mut SolveApp) { +pub(crate) fn sync_feature_edit(app: &mut SolveApp) { + if app.dialog.as_ref().and_then(|d| d.editing).is_some_and(|(id, _)| app.session.doc.feature(id).is_none()) { + // Undo or deletion already chose the current document and history. Cancel's rollback + // would try to restore the obsolete editing session; just discard its work instead. + app.dialog = None; + app.applying = None; + if let Some(state) = app.editing_state.take() { + // Undo can save the editor's temporary rollback in Redo (deletion saves it in + // Undo). Those exact snapshots belong to the complete design, with its old marker. + for snapshot in app.session.undo.iter_mut().chain(&mut app.session.redo) { + if std::sync::Arc::ptr_eq(&snapshot.doc, &state.source.doc) { + snapshot.doc = state.original.doc.clone(); + } + } + } + app.preview.reset(); + app.op_check.abandon(); + } app.session.feature_edit_sketch = app.dialog.as_ref().and_then(|d| d.editing).and_then(|(id, _)| match &app.session.doc.feature(id)?.kind { FeatureKind::Extrude { sketch, .. } | FeatureKind::Revolve { sketch, .. } @@ -3135,7 +3153,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { // A click in the view's corner is for the dialog open this frame, or for none. let corner = app.viewport.corner.take(); app.viewport.corner_ok = false; - sync_feature_edit_sketch(app); + sync_feature_edit(app); app.op_check.poll(); if app.tree.redefine.is_none() { app.viewport.dialog_rect = None; diff --git a/crates/ui-egui/src/extrude_edit_tests.rs b/crates/ui-egui/src/extrude_edit_tests.rs index 47fead196..b2a4c4bfb 100644 --- a/crates/ui-egui/src/extrude_edit_tests.rs +++ b/crates/ui-egui/src/extrude_edit_tests.rs @@ -25,6 +25,69 @@ fn open(h: &mut Harness) { h.frames(8); } +#[test] +fn undoing_the_edited_feature_closes_its_editor_and_preserves_redo() { + let mut h = make(json!({})); + let feature = h.app.session.doc.find_feature("Depth").unwrap().id; + open(&mut h); + assert!(h.app.dialog.is_some()); + + h.app.start("edit.undo"); + h.frames(8); + assert!(h.app.session.doc.feature(feature).is_none()); + assert!(h.app.dialog.is_none(), "a removed feature must not keep an Edit Extrude dialog"); + assert!(h.app.editing_state.is_none()); + assert!(h.app.applying.is_none()); + assert!(!h.app.preview.active); + assert!(h.app.session.feature_edit_sketch.is_none()); + assert_eq!(h.app.session.redo.len(), 1); + assert_eq!(h.app.session.doc.features.len(), 1); + let revision = h.app.session.revision; + h.frames(8); + assert_eq!(h.app.session.revision, revision, "closing the stale editor does not change the design"); + + h.app.run("edit.redo", json!({})).unwrap(); + assert!(h.app.session.doc.feature(feature).is_some()); + assert!(h.app.dialog.is_none(), "redo does not resurrect the old editor"); + assert_eq!(h.app.session.world_state().body("Main").unwrap().mesh().bounds().max.z, 10.0); + open(&mut h); + h.step(&json!({"widget":"OK"})).unwrap(); + assert!(h.app.dialog.is_none(), "the restored feature can be edited normally"); +} + +#[test] +fn undo_that_keeps_the_edited_feature_keeps_its_editor() { + let mut h = make(json!({})); + let feature = h.app.session.doc.find_feature("Depth").unwrap().id; + h.app.run("solid.box", json!({"corner":[30,0,0],"length":2,"width":2,"height":2})).unwrap(); + open(&mut h); + h.app.start("edit.undo"); + h.frames(8); + assert!(h.app.session.doc.feature(feature).is_some()); + assert_eq!(h.app.dialog.as_ref().unwrap().editing.map(|(id, _)| id), Some(feature)); + assert!(h.app.editing_state.is_some()); + assert!(h.app.session.feature_edit_sketch.is_some()); + assert_eq!(h.app.session.redo.len(), 1); +} + +#[test] +fn deleting_the_edited_feature_closes_its_editor_and_undo_restores_the_design() { + let mut h = make(json!({})); + let original = h.app.session.doc.clone(); + open(&mut h); + h.app.run("timeline.delete", json!({"feature":"Depth"})).unwrap(); + let deleted = h.app.session.doc.clone(); + let history = (h.app.session.undo.len(), h.app.session.redo.len()); + h.frames(8); + assert!(h.app.dialog.is_none() && h.app.editing_state.is_none()); + assert!(!h.app.preview.active); + assert!(std::sync::Arc::ptr_eq(&deleted, &h.app.session.doc), "closing preserves the current document and marker"); + assert_eq!((h.app.session.undo.len(), h.app.session.redo.len()), history); + h.app.run("edit.undo", json!({})).unwrap(); + assert!(std::sync::Arc::ptr_eq(&original, &h.app.session.doc), "Undo restores the document before the temporary editor rollback"); + assert_eq!(h.app.session.world_state().body("Main").unwrap().mesh().bounds().max.z, 10.0); +} + #[test] fn unchanged_all_profiles_includes_profiles_added_after_edit() { let mut h = make(json!({})); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 7798a61c9..a73479ff8 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2860,7 +2860,7 @@ impl SolveApp { self.shell.history = None; } // The model draws next: a dialog the toolbar closed last frame no longer shows its sketch. - dialogs::sync_feature_edit_sketch(self); + dialogs::sync_feature_edit(self); let full = ui.available_rect_before_wrap(); if floating { let cards = toolbar::shell_cards(self, full);