diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index b7696e2458..30133aec89 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -7926,10 +7926,10 @@ impl RustGisApp { inuse::follow(self, &ctx); chrome::shortcuts(self, &ctx); panes::follow_layer_filter(self); - // A Zarr summary or point series, a trend, a workflow, an export or a read running on its thread lands - // whatever is in view, the File menu's backstage too, so a project saved there holds it - // (all are looked at: `|`, not `||`), and the frames go on while one runs so it's taken - // when it ends. + // A Zarr summary or point series, a trend, a workflow, an export, a read or a preview the + // Layout Wizard left, running on its thread, lands whatever is in view, the File menu's + // backstage too, so a project saved there holds it (all are looked at: `|`, not `||`), and + // the frames go on while one runs so it's taken when it ends. #[cfg(not(target_arch = "wasm32"))] if workspacepanes::land_summary(self) | workspacepanes::land_series(self) @@ -7937,6 +7937,7 @@ impl RustGisApp { | analysispane::land_job(self) | exportjob::land(self) | readjob::land(self) + | publicationwizard::land_left(self) { ctx.request_repaint_after(std::time::Duration::from_millis(200)); } diff --git a/crates/ui-egui/src/publicationwizard/mod.rs b/crates/ui-egui/src/publicationwizard/mod.rs index 1b233feeeb..7f42c857f0 100644 --- a/crates/ui-egui/src/publicationwizard/mod.rs +++ b/crates/ui-egui/src/publicationwizard/mod.rs @@ -17,6 +17,8 @@ use accent::accent_step; use arrange::{Arrangement, arrange_step}; use elements::{Elements, elements_step}; use map::{Area, Coords, area_extent, map_step}; +#[cfg(not(target_arch = "wasm32"))] +pub(crate) use review::land_left; use review::{Preview, drop_stale_preview, review_step}; use rustgis_engine::Session; use rustgis_engine::cmd::layout::{ diff --git a/crates/ui-egui/src/publicationwizard/review.rs b/crates/ui-egui/src/publicationwizard/review.rs index 331ed5659b..2b1adc699c 100644 --- a/crates/ui-egui/src/publicationwizard/review.rs +++ b/crates/ui-egui/src/publicationwizard/review.rs @@ -5,6 +5,7 @@ use super::*; use rustgis_engine::analysis::control::Control; use rustgis_engine::cmd::layout::{PublicationPreview, publication_preview_work}; use rustgis_engine::map::Camera; +use std::cell::RefCell; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{Receiver, TryRecvError, channel}; @@ -143,9 +144,21 @@ impl Drawing { impl Drop for Drawing { fn drop(&mut self) { self.stop(); + // What it met is still to be heard: the wizard goes in many places, none with the app at + // hand, so its answer is kept for [`land_left`]. + let made = std::mem::replace(&mut self.made, channel().1); + // Not kept while the thread itself is ending, with the app, or while the list is being + // read: a destructor never panics. + let _ = LEFT.try_with(|left| left.try_borrow_mut().map(|mut left| left.push(made))); } } +thread_local! { + /// The answers of drawings whose [`Drawing`] was dropped, on the interface's thread. One + /// already read has nothing more to say. + static LEFT: RefCell>>> = const { RefCell::new(Vec::new()) }; +} + struct Picture { texture: egui::TextureHandle, warnings: Vec, @@ -185,7 +198,7 @@ fn draw(ctx: &egui::Context, s: &Session, d: &Draft, off_frame: bool) -> Shown { let spawned = std::thread::Builder::new().name("Layout preview".into()).spawn(move || { let _frames = rustgis_engine::render::cancel::install(stop_frames); let drawn = rustgis_engine::analysis::run_under("Layout preview", &stop, work); - // Nothing waits for a dropped one. + // A dropped one's answer is read by [`land_left`]. let _ = send.send(drawn); repaint.request_repaint(); }); @@ -195,6 +208,30 @@ fn draw(ctx: &egui::Context, s: &Session, d: &Draft, off_frame: bool) -> Shown { } } +/// Says in Messages a bug met by a drawing the wizard left before hearing of it: called every +/// frame, whatever dialog is open. `true` while one is still drawing. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn land_left(app: &mut RustGisApp) -> bool { + LEFT.with(|left| { + let mut left = left.borrow_mut(); + left.retain(|made| match made.try_recv() { + Err(TryRecvError::Empty) => true, + answer => { + if let Some(bug) = answer.ok().and_then(bug_met) { + app.session.warn(bug); + } + false + } + }); + !left.is_empty() + }) +} + +/// The bug a drawing nothing is shown of met, for Messages. +fn bug_met(made: Result) -> Option { + made.err().filter(|e| rustgis_engine::data::incident::named(e).is_some()) +} + /// A preview the system refused a thread for. fn refused(e: &std::io::Error) -> Shown { Shown::Drawn(Err(crate::thread_refused(e))) @@ -224,7 +261,7 @@ fn preview_part(ui: &mut egui::Ui, s: &Session, d: &mut Draft, off_frame: bool) if let Some(made) = made { if matches!(preview.shown, Shown::Stopping(_)) { // Cancelled, so nothing of it is shown; a bug it met is still said. - bug = made.err().filter(|e| rustgis_engine::data::incident::named(e).is_some()); + bug = bug_met(made); d.preview = None; } else { preview.shown = Shown::Drawn(made.map(|made| picture(ui.ctx(), made))); @@ -744,15 +781,98 @@ mod tests { assert!(stops.told()); } + /// What a bug caught in a drawing says. + fn a_bug() -> String { + let caught = std::panic::catch_unwind(|| panic!("a drawing's bug")).unwrap_err(); + rustgis_engine::data::incident::bug("The preview", caught.as_ref()) + } + + fn times_said(h: &Wizard, text: &str) -> usize { + h.state().session.notices.iter().filter(|n| n.text == text).count() + } + + /// A frame's landing of the drawings the wizard left; whether one is still awaited. + fn land(h: &mut Wizard) -> bool { + land_left(h.state_mut()) + } + #[test] fn a_bug_in_the_drawing_is_said_in_messages_once() { let (mut h, send, _) = held(); - let caught = std::panic::catch_unwind(|| panic!("a drawing's bug")).unwrap_err(); - let said = rustgis_engine::data::incident::bug("The preview", caught.as_ref()); + let said = a_bug(); send.send(Err(said.clone())).unwrap(); h.run_steps(8); - assert_eq!(h.state().session.notices.iter().filter(|n| n.text == said).count(), 1); + assert_eq!(times_said(&h, &said), 1); assert!(h.query_by_label(&said).is_some(), "and in the step"); + drop(send); + click(&mut h, "Cancel"); + assert!(!land(&mut h)); + assert_eq!(times_said(&h, &said), 1, "not again once the wizard closes"); + } + + /// The ways the wizard goes while it draws: closed, or another dialog taking its place. + #[test] + fn a_bug_met_after_the_wizard_goes_is_still_said() { + let goes: [fn(&mut Wizard); 2] = [|h| click(h, "Cancel"), |h| h.state_mut().dialogs = Default::default()]; + for go in goes { + let (mut h, send, _) = held(); + go(&mut h); + assert!(land(&mut h), "still awaited"); + let said = a_bug(); + send.send(Err(said.clone())).unwrap(); + drop(send); + assert!(!land(&mut h)); + assert!(!land(&mut h)); + assert_eq!(times_said(&h, &said), 1); + } + } + + /// Only the review step reads the drawing, so one that ends on another step waits there. + #[test] + fn a_bug_unread_when_the_wizard_closes_on_another_step_is_still_said() { + let (mut h, send, _) = held(); + click(&mut h, "Back"); + let said = a_bug(); + send.send(Err(said.clone())).unwrap(); + drop(send); + h.run_steps(4); + click(&mut h, "Cancel"); + assert!(!land(&mut h)); + assert_eq!(times_said(&h, &said), 1); + } + + /// A destructor never panics, so one met while the left drawings are being read gives up its + /// answer instead. + #[test] + fn a_drawing_dropped_while_the_left_ones_are_read_does_not_panic() { + let (_send, made) = channel(); + let drawing = Drawing { made, control: Control::stoppable(), frames: Default::default() }; + LEFT.with(|left| { + let _reading = left.borrow_mut(); + drop(drawing); + }); + } + + #[test] + fn a_drawing_left_that_met_no_bug_says_nothing() { + let ends: [fn(&Wizard) -> Option>; 3] = [ + |h| Some(publication_preview_work(&h.state().session, json!({"profile": "a4-portrait"}))()), + |_| Some(Err("Layout preview was cancelled".into())), + // The thread ends without answering. + |_| None, + ]; + for end in ends { + let (mut h, send, _) = held(); + click(&mut h, "Cancel"); + assert!(land(&mut h), "still awaited"); + let notices = h.state().session.notices.len(); + if let Some(made) = end(&h) { + send.send(made).unwrap(); + } + drop(send); + assert!(!land(&mut h), "and no longer"); + assert_eq!(h.state().session.notices.len(), notices); + } } #[test]