diff --git a/crates/ui-egui/src/dialogs/effect.rs b/crates/ui-egui/src/dialogs/effect.rs index 06fa47f045..c2d2f14810 100644 --- a/crates/ui-egui/src/dialogs/effect.rs +++ b/crates/ui-egui/src/dialogs/effect.rs @@ -1167,6 +1167,29 @@ mod tests { assert_eq!(params(&app, node)["size"], 12.0); } + #[test] + fn a_new_graphics_context_rolls_back_the_old_effect_preview_and_keeps_the_new_opening() { + let mut app = app(); + let node = rect(&mut app, 10); + standalone_effect_source(&mut app); + effect_frame(&mut app); + assert_eq!(params(&app, node)["size"], 16.0); + let history = steps(&app); + + app.adopt_context(&crate::testing::ui_context()); + effect_frame(&mut app); + assert!(!app.session.in_interaction(), "the old context's effect preview is still running"); + assert!(app.effect_dialogs.effect.is_none()); + assert!(app.session.doc().unwrap().doc.node(vectorcraft_doc::NodeId(node)).unwrap().appearance.effects.is_empty()); + + standalone_effect_source(&mut app); + effect_frame(&mut app); + assert!(app.effect_dialogs.effect.is_some(), "the old opening's Cancel removed the new opening"); + ok(&mut app); + assert_eq!(params(&app, node)["size"], 16.0); + assert_eq!(steps(&app), history + 1); + } + #[test] fn standalone_effect_ok_cannot_change_the_reloaded_drawing() { let mut app = app(); diff --git a/crates/ui-egui/src/dialogs/host.rs b/crates/ui-egui/src/dialogs/host.rs index 38c572c1c9..82987ab202 100644 --- a/crates/ui-egui/src/dialogs/host.rs +++ b/crates/ui-egui/src/dialogs/host.rs @@ -7,6 +7,7 @@ use craft::ui::shell::WindowDialogs; use serde_json::Value; use crate::VectorcraftApp; +use crate::graphics::context_tag; /// The most dialogs waiting for an answer at once. pub(crate) const MAX_WAITING: usize = 16; @@ -45,6 +46,8 @@ type Drain = Box Option>; struct Record { id: DialogId, + /// The egui context whose dialog host shows the dialog ([`context_tag`]). + context: u64, drain: Drain, cancel: Option, } @@ -59,6 +62,20 @@ impl Held { } } +/// Cancels the held dialogs opened in another egui context than the app's current one, so every record left is +/// served by the current window's host. Each context has its own host, which numbers its openings from the start: a +/// record of the old host is never answered, and its serial can match an opening of the same key in the new one. +fn retire_other_contexts(app: &mut VectorcraftApp) { + let current = app.ui_ctx.as_ref().map(context_tag); + let (retired, kept): (Vec, Vec) = std::mem::take(&mut app.held.0).into_iter().partition(|record| Some(record.context) != current); + app.held.0 = kept; + for mut record in retired { + if let Some(cancel) = record.cancel.take() { + cancel(app); + } + } +} + fn drain(id: DialogId, serial: u64, mut ok: impl FnMut(&mut VectorcraftApp, &D::Answer) -> Result<(), String> + 'static) -> Drain { Box::new(move |app| { // Without a window nothing can answer: the dialog counts as cancelled so its rollback runs. @@ -106,7 +123,9 @@ pub(crate) fn open_held_with( ok: impl FnMut(&mut VectorcraftApp, &D::Answer) -> Result<(), String> + 'static, options: HeldOptions, ) -> Option { - let host = app.dialogs()?; + retire_other_contexts(app); + let ctx = app.ui_ctx.clone()?; + let host = window_dialogs(&ctx); let dialog = dialog.held_ok(); let (id, serial) = host.with(|h| { let id = match options.parent { @@ -117,7 +136,7 @@ pub(crate) fn open_held_with( })?; // A host key can be reopened before the previous result has been pumped. app.held.0.retain(|record| record.id != id); - app.held.0.push(Record { id, drain: drain::(id, serial, ok), cancel: options.cancel }); + app.held.0.push(Record { id, context: context_tag(&ctx), drain: drain::(id, serial, ok), cancel: options.cancel }); Some(id) } @@ -154,6 +173,8 @@ macro_rules! pumps { /// Runs once per frame after the shell pass: every dialog module's pump, then the held dialogs. pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { + // The dialog modules see the rollbacks of the dialogs a replaced graphics context took with it. + retire_other_contexts(app); sync(app); pumps!(app, ctx; crate::file_asks::pump, @@ -379,4 +400,56 @@ mod opening_tests { pump(&mut app, &ctx); assert_eq!(host.with(|h| h.held_state(id)), Some(HeldOk::Accepted)); } + + /// Opens the test dialog with an OK and a Cancel that write `name` to `log`. + fn open_logged(app: &mut VectorcraftApp, ctx: &egui::Context, log: &Rc>>, name: &'static str) -> DialogId { + let (answered, cancelled) = (log.clone(), log.clone()); + open_held_with( + app, + dialog(ctx), + move |_, _| { + answered.borrow_mut().push(format!("{name} ok")); + Ok(()) + }, + HeldOptions { parent: None, cancel: Some(Box::new(move |_| cancelled.borrow_mut().push(format!("{name} cancel")))) }, + ) + .unwrap() + } + + #[test] + fn a_new_graphics_context_cancels_the_old_contexts_opening() { + let (mut app, ctx) = app(); + let log = Rc::new(RefCell::new(Vec::new())); + let first = open_logged(&mut app, &ctx, &log, "old"); + let old_serial = app.dialogs().unwrap().with(|h| h.open_serial(first)); + + let fresh = crate::testing::ui_context(); + app.adopt_context(&fresh); + // The new context's host numbers its openings from the start: the same key gets the old opening's serial. + let host = app.dialogs().unwrap(); + let id = host.with(|h| h.open(dialog(&fresh).held_ok())); + assert_eq!((id, host.with(|h| h.open_serial(id))), (first, old_serial)); + assert!(!host.with(|h| h.confirm_by_id(&fresh, id))); + pump(&mut app, &fresh); + assert_eq!(*log.borrow(), ["old cancel"], "the old opening's OK answered the new context's dialog"); + pump(&mut app, &fresh); + assert_eq!(*log.borrow(), ["old cancel"]); + } + + #[test] + fn reopening_a_key_in_a_new_graphics_context_cancels_the_old_opening_first() { + let (mut app, ctx) = app(); + let log = Rc::new(RefCell::new(Vec::new())); + open_logged(&mut app, &ctx, &log, "old"); + + let fresh = crate::testing::ui_context(); + app.adopt_context(&fresh); + let id = open_logged(&mut app, &fresh, &log, "new"); + assert_eq!(*log.borrow(), ["old cancel"], "the old opening's rollback was dropped"); + let host = app.dialogs().unwrap(); + assert!(!host.with(|h| h.confirm_by_id(&fresh, id))); + pump(&mut app, &fresh); + assert_eq!(*log.borrow(), ["old cancel", "new ok"]); + assert_eq!(host.with(|h| h.held_state(id)), Some(HeldOk::Accepted)); + } } diff --git a/crates/ui-egui/src/dialogs/three_d.rs b/crates/ui-egui/src/dialogs/three_d.rs index 460d8327f2..cd1fcd4576 100644 --- a/crates/ui-egui/src/dialogs/three_d.rs +++ b/crates/ui-egui/src/dialogs/three_d.rs @@ -754,6 +754,30 @@ mod drawing_tests { assert_eq!(applied(&app, original), [vectorcraft_effects::REVOLVE]); } + #[test] + fn three_d_ownership_new_graphics_context_rolls_back_the_old_preview_and_keeps_the_new_opening() { + let (mut app, original) = drawing(); + open_effect(&mut app, true); + frame(&mut app); + assert_eq!(applied(&app, original), [EXTRUDE]); + + app.adopt_context(&crate::testing::ui_context()); + frame(&mut app); + assert!(!app.session.in_interaction(), "the old context's 3D preview is still running"); + assert!(app.three_d_dialog.is_none()); + assert!(applied(&app, original).is_empty()); + + open_effect(&mut app, true); + frame(&mut app); + assert!(app.three_d_dialog.is_some(), "the old opening's Cancel removed the new opening"); + assert_eq!(applied(&app, original), [EXTRUDE]); + let history = app.session.active().unwrap().history.undo.len(); + hold_ok(&app); + frame(&mut app); + assert_eq!(applied(&app, original), [EXTRUDE]); + assert_eq!(app.session.active().unwrap().history.undo.len(), history + 1); + } + #[test] fn three_d_ownership_ok_refuses_the_reloaded_drawing() { let (mut app, original) = drawing();