Dialogs: a new graphics context cancels the held dialogs of the old one

Each egui context has its own dialog host, and a host numbers its
openings from 1. When the app adopts a new context after losing its
graphics, the held records of the old host stayed in the list: their
Cancel never ran, so an effect or 3D preview kept its interaction open,
and a record's serial could match the first opening of the same key in
the new host, whose OK then ran the old opening's callback. Reopening
the key in the new context dropped the old record without its Cancel.

Each held record now keeps the tag of the context it was opened in.
`open_held_with` and the per-frame pump cancel the records of any other
context first, so their rollbacks run before a new opening takes the
dialog's slot.
This commit is contained in:
dotCooCoo committed 2026-11-30 07:46:28 -08:00
1 parent c99d4615d1
commit f24e2ebb24
3 files changed
+122 -2

No files matched your search

+23
View File
@@ -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();
+75 -2
View File
@@ -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<dyn FnMut(&mut VectorcraftApp) -> Option<bool>>;
struct Record {
id: DialogId,
/// The egui context whose dialog host shows the dialog ([`context_tag`]).
context: u64,
drain: Drain,
cancel: Option<Cancel>,
}
@@ -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<Record>, Vec<Record>) = 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<D: HoldsOk>(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<D: HoldsOk>(
ok: impl FnMut(&mut VectorcraftApp, &D::Answer) -> Result<(), String> + 'static,
options: HeldOptions,
) -> Option<DialogId> {
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<D: HoldsOk>(
})?;
// 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::<D>(id, serial, ok), cancel: options.cancel });
app.held.0.push(Record { id, context: context_tag(&ctx), drain: drain::<D>(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<RefCell<Vec<String>>>, 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));
}
}
+24
View File
@@ -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();