diff --git a/crates/ui-egui/src/apply_off_thread_tests.rs b/crates/ui-egui/src/apply_off_thread_tests.rs index c261fe077..92d72da22 100644 --- a/crates/ui-egui/src/apply_off_thread_tests.rs +++ b/crates/ui-egui/src/apply_off_thread_tests.rs @@ -73,6 +73,23 @@ fn cancel_while_computing_applies_nothing() { assert_eq!(h.app.session.doc.features.len(), features); } +/// #2929: the feature must not appear in the frame OK starts computing in. The window thread +/// can be held up between starting the evaluation and the check that takes it up, and the +/// feature then appears where the dialog says it is computing - which is what made +/// `cancel_while_computing_applies_nothing` fail under load. +#[test] +fn ok_computing_off_the_window_thread_does_not_apply_in_the_frame_it_starts() { + let mut h = square_on_a_block(); + h.app.apply_off_thread = Some(Duration::ZERO); + let features = h.app.session.doc.features.len(); + h.step(&json!({"key": "Enter"})).unwrap(); + assert!(h.app.applying.is_some(), "the dialog is computing"); + assert_eq!(h.app.session.doc.features.len(), features, "nothing applied in the frame it started"); + // A frame later the evaluation is in and the feature is applied. + until_applied(&mut h); + assert_eq!(h.app.session.doc.features.len(), features + 1); +} + #[test] fn the_dialog_holds_its_inputs_while_ok_computes() { let mut h = square_on_a_block(); @@ -82,7 +99,7 @@ fn the_dialog_holds_its_inputs_while_ok_computes() { // OK computing, until the test lets it finish. let (finish, done) = std::sync::mpsc::channel(); let cmds = h.app.dialog.as_ref().and_then(|d| crate::dialogs::apply_commands(&h.app, d).ok()).unwrap(); - h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds }); + h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds, seen: true }); // A click on the square picked would take it out of the profiles. h.step(&json!({"click": {"world": [10, 10, 10]}})).unwrap(); h.frames(2); @@ -103,7 +120,7 @@ fn inputs_changed_while_ok_computes_are_computed_off_the_thread_too() { let features = h.app.session.doc.features.len(); // OK computing other commands than the dialog's, until the test lets it finish. let (finish, done) = std::sync::mpsc::channel(); - h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds: Vec::new() }); + h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds: Vec::new(), seen: true }); finish.send(()).unwrap(); h.frames(1); assert!(h.app.applying.is_some(), "the dialog's own commands are computing"); diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 70eefe7fd..e763a9749 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -436,6 +436,9 @@ pub struct Applying { pub(crate) started: f64, /// The commands being computed. pub(crate) cmds: Vec<(String, Value)>, + /// The job has been in a frame. A job that has just started is not taken up in the frame it + /// starts, so the dialog says it is computing before the feature appears. + pub(crate) seen: bool, } impl Applying { @@ -443,6 +446,13 @@ impl Applying { fn finished(&self) -> bool { !matches!(self.done.try_recv(), Err(std::sync::mpsc::TryRecvError::Empty)) } + + /// The evaluation is in and the job has been in a frame. A job that has just started waits + /// one frame: the window thread may be held up between starting it and the check, and the + /// feature must not appear where the dialog says it is computing. + fn ready(&self) -> bool { + self.seen && self.finished() + } } /// How long the desktop app's OK waits for its evaluation before it lets the window go on and @@ -477,7 +487,7 @@ fn begin_applying(app: &SolveApp, d: &Dialog) -> Option { }) .ok()?; match rx.recv_timeout(wait) { - Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Some(Applying { done: rx, started: crate::now_ms(), cmds }), + Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Some(Applying { done: rx, started: crate::now_ms(), cmds, seen: false }), _ => None, } } @@ -2416,7 +2426,13 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { // OK's evaluation is in: run it, the feature taken from the cache. Inputs changed meanwhile // (nothing should change them) are computed in turn, off the window thread too: run here, // they would hold the window for the whole boolean. - if app.applying.as_ref().is_some_and(Applying::finished) && !cancel { + // A job that has just started is not in yet: it waits a frame, so the dialog says it is + // computing before the feature appears. + let ready = app.applying.as_ref().is_some_and(Applying::ready); + if let Some(job) = &mut app.applying { + job.seen = true; + } + if ready && !cancel { let computed = app.applying.take().map(|job| job.cmds); let current = apply_commands(app, &d).ok(); let again = if current.is_some() && current != computed { begin_applying(app, &d) } else { None };