diff --git a/crates/ui-egui/src/layerchooser.rs b/crates/ui-egui/src/layerchooser.rs index e6d52efdaf..12df77197a 100644 --- a/crates/ui-egui/src/layerchooser.rs +++ b/crates/ui-egui/src/layerchooser.rs @@ -106,19 +106,8 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result { if !pick && !links(path) { return add(app, json!({"path": path})); } - // Listed off the frame: a geodatabase's or GDAL's listing can take a while. - let slot: crate::Slot> = crate::Slot::default(); - let out = slot.clone(); - let listed = path.to_string(); let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - let stopped = stop.clone(); - crate::work_off_frame("rustgis-list-layers", &slot, move || { - // A chooser cancelled or closed stops it between a GeoPackage's tables. - let _stop = rustgis_engine::data::progress::also_stop_when(stopped); - let mut session = rustgis_engine::Session::new(rustgis_engine::Project::default()); - let result = session.run("info.fileLayers", &json!({"path": listed, "link": true})); - *out.lock().unwrap_or_else(|e| e.into_inner()) = Some(result); - }); + let slot = list_in_background(path, stop.clone(), |slot, work| crate::work_off_frame("rustgis-list-layers", slot, work)); let opened = app.session.opened; app.ui.choosing.push(Chooser { path: path.to_string(), @@ -133,6 +122,37 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result { Ok(json!({"choosing": path})) } +/// Lists `path`'s layers (and Add Data's question about it) off the frame: a geodatabase's or +/// GDAL's listing can take a while. A `stop` set, by a chooser cancelled or closed, ends the +/// question between a GeoPackage's tables ([`list_layers`]). `start` starts the thread with the +/// work (a test runs the work where it stands). +fn list_in_background( + path: &str, + stop: std::sync::Arc, + start: impl FnOnce(&crate::Slot>, Box), +) -> crate::Slot> { + let slot: crate::Slot> = crate::Slot::default(); + let out = slot.clone(); + let listed = path.to_string(); + start( + &slot, + Box::new(move || { + let result = list_layers(&listed, stop); + *out.lock().unwrap_or_else(|e| e.into_inner()) = Some(result); + }), + ); + slot +} + +/// What [`list_in_background`] runs on its thread: `info.fileLayers` with Add Data's question, +/// which a `stop` already set (or set while it runs) leaves unmade. The listing itself still runs to +/// the end. +fn list_layers(path: &str, stop: std::sync::Arc) -> Result { + let _stop = rustgis_engine::data::progress::also_stop_when(stop); + let mut session = rustgis_engine::Session::new(rustgis_engine::Project::default()); + session.run("info.fileLayers", &json!({"path": path, "link": true})) +} + /// Adds a file, read on a thread of its own when the desktop app reads it so ([`crate::readjob`]). fn add(app: &mut RustGisApp, p: Value) -> Result { #[cfg(not(target_arch = "wasm32"))] @@ -348,6 +368,14 @@ mod tests { h } + /// Writes `ds` to a GeoPackage at `path` as two tables, `a` and `b`. + fn two_table_gpkg(ds: &rustgis_engine::data::Dataset, path: &std::path::Path) { + for table in ["a", "b"] { + let add = rustgis_engine::data::io::GpkgTable { mode: rustgis_engine::data::io::GpkgMode::AddTable, name: Some(table.into()) }; + rustgis_engine::data::io::write_path_to(ds, path, rustgis_engine::data::raster::Crs::LonLat, &add).unwrap(); + } + } + /// A chooser whose listing has already landed, for the project `opened`. fn listed(opened: u64, path: &str, result: Result) -> Chooser { let slot: crate::Slot> = crate::Slot::default(); @@ -489,10 +517,7 @@ mod tests { let fgb = dir.path().join("one.fgb"); rustgis_engine::data::io::write_path(&ds, &fgb).unwrap(); let gpkg = dir.path().join("two.gpkg"); - for table in ["a", "b"] { - let add = rustgis_engine::data::io::GpkgTable { mode: rustgis_engine::data::io::GpkgMode::AddTable, name: Some(table.into()) }; - rustgis_engine::data::io::write_path_to(&ds, &gpkg, rustgis_engine::data::raster::Crs::LonLat, &add).unwrap(); - } + two_table_gpkg(&ds, &gpkg); let (fgb, gpkg) = (fgb.display().to_string(), gpkg.display().to_string()); let mut app = RustGisApp::new(Session::new(Project::default())); // Dropped, picked or double-clicked, it waits for the answer. @@ -622,6 +647,30 @@ mod tests { assert!(stop.load(std::sync::atomic::Ordering::Relaxed), "the listing is told to stop"); } + /// The listing Add Data starts stops when Cancel sets its flag (#2827): through + /// [`list_in_background`], a flag already set on a GeoPackage of two tables lands a listing + /// with no question made, one still clear lands it with one. Deleting `also_stop_when` from + /// [`list_layers`], or `list_in_background` passing it another flag, fails the first. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_listing_told_to_stop_makes_no_question() { + use rustgis_engine::data::{Coord, Dataset, Field, FieldType, Geometry, GeometryType, Value as V}; + use std::sync::{Arc, atomic::AtomicBool}; + let dir = tempfile::tempdir().unwrap(); + let mut ds = Dataset::new("points", GeometryType::Point, vec![Field::new("name", FieldType::Text)]); + ds.push(Some(Geometry::Point(Coord::new(0.0, 1.0))), vec![V::Text("p".into())]); + let gpkg = dir.path().join("two.gpkg"); + two_table_gpkg(&ds, &gpkg); + let gpkg = gpkg.display().to_string(); + let inline = |_: &crate::Slot>, work: Box| work(); + let landed = |slot: crate::Slot>| slot.lock().unwrap().take().expect("the work ran").unwrap(); + let told = landed(list_in_background(&gpkg, Arc::new(AtomicBool::new(true)), inline)); + assert_eq!(told["layers"], json!(["a", "b"])); + assert!(told.get("link").is_none(), "stopped: no question made: {told}"); + let free = landed(list_in_background(&gpkg, Arc::new(AtomicBool::new(false)), inline)); + assert_eq!(free["link"], json!({"linked": false, "copy": true}), "not stopped: a question: {free}"); + } + /// Another project opened while the listing is on its way drops the chooser, and the listing /// stops as on Cancel (#2827). #[test]