From d80c6a91fb494a87a06e979e78aaf1a5f833805c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 25 Nov 2026 21:02:19 -0500 Subject: [PATCH 1/3] test(ui): a listing told to stop makes no question (#2978) --- crates/ui-egui/src/layerchooser.rs | 59 ++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/layerchooser.rs b/crates/ui-egui/src/layerchooser.rs index e6d52efdaf..da0cd55198 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()); let opened = app.session.opened; app.ui.choosing.push(Chooser { path: path.to_string(), @@ -133,6 +122,28 @@ 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, stops it +/// between a GeoPackage's tables ([`list_layers`]). +fn list_in_background(path: &str, stop: std::sync::Arc) -> crate::Slot> { + let slot: crate::Slot> = crate::Slot::default(); + let out = slot.clone(); + let listed = path.to_string(); + crate::work_off_frame("rustgis-list-layers", &slot, 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. +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"))] @@ -622,6 +633,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): 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`] 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"); + 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(); + } + let gpkg = gpkg.display().to_string(); + let told = list_layers(&gpkg, Arc::new(AtomicBool::new(true))).unwrap(); + assert_eq!(told["layers"], json!(["a", "b"])); + assert!(told.get("link").is_none(), "stopped: no question made: {told}"); + let free = list_layers(&gpkg, Arc::new(AtomicBool::new(false))).unwrap(); + 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] From 21853a7a34293183e21f092f64cf87768bc36c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 25 Nov 2026 21:24:02 -0500 Subject: [PATCH 2/3] test(ui): the listing's test goes through list_in_background (#2978) --- crates/ui-egui/src/layerchooser.rs | 52 +++++++++++++++++++----------- crates/ui-egui/src/lib.rs | 8 ++++- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/crates/ui-egui/src/layerchooser.rs b/crates/ui-egui/src/layerchooser.rs index da0cd55198..ead2039880 100644 --- a/crates/ui-egui/src/layerchooser.rs +++ b/crates/ui-egui/src/layerchooser.rs @@ -107,7 +107,7 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result { return add(app, json!({"path": path})); } let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - let slot = list_in_background(path, stop.clone()); + let slot = list_in_background(path, stop.clone(), crate::named_thread("rustgis-list-layers")); let opened = app.session.opened; app.ui.choosing.push(Chooser { path: path.to_string(), @@ -123,13 +123,18 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result { } /// 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, stops it -/// between a GeoPackage's tables ([`list_layers`]). -fn list_in_background(path: &str, stop: std::sync::Arc) -> crate::Slot> { +/// 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`]). `spawn` starts the thread (a test +/// runs the work where it stands). +fn list_in_background( + path: &str, + stop: std::sync::Arc, + spawn: impl FnOnce(Box) -> std::io::Result<()>, +) -> crate::Slot> { let slot: crate::Slot> = crate::Slot::default(); let out = slot.clone(); let listed = path.to_string(); - crate::work_off_frame("rustgis-list-layers", &slot, move || { + crate::work_off_frame_with(spawn, &slot, move || { let result = list_layers(&listed, stop); *out.lock().unwrap_or_else(|e| e.into_inner()) = Some(result); }); @@ -137,7 +142,8 @@ fn list_in_background(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()); @@ -359,6 +365,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(); @@ -500,10 +514,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. @@ -633,9 +644,10 @@ 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): 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`] fails the first. + /// 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() { @@ -645,15 +657,17 @@ mod tests { 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"); - 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 gpkg = gpkg.display().to_string(); - let told = list_layers(&gpkg, Arc::new(AtomicBool::new(true))).unwrap(); + let inline = |work: Box| { + work(); + Ok(()) + }; + 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 = list_layers(&gpkg, Arc::new(AtomicBool::new(false))).unwrap(); + 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}"); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index b5f8fa0d35..b73a82a769 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -259,7 +259,13 @@ pub type Slot = std::sync::Arc>>; /// an error rather than the app (AGENTS.md › Use the threads). In the browser, which has no /// threads, that is every time. pub(crate) fn work_off_frame>(name: &str, slot: &Slot>, work: impl FnOnce() + Send + 'static) { - work_off_frame_with(|work| std::thread::Builder::new().name(name.into()).spawn(work).map(drop), slot, work); + work_off_frame_with(named_thread(name), slot, work); +} + +/// The spawner [`work_off_frame`] uses: a thread named `name`. +pub(crate) fn named_thread(name: &str) -> impl FnOnce(Box) -> std::io::Result<()> { + let name = name.to_string(); + move |work| std::thread::Builder::new().name(name).spawn(work).map(drop) } /// [`work_off_frame`], its thread started by `spawn`, which a test can make refuse. From 70628b06ea72c2d37925a2fd77221149e9b14c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 25 Nov 2026 22:45:34 -0500 Subject: [PATCH 3/3] fix(ui): Add Data's listing starts through work_off_frame, which the browser build uses (#2978) --- crates/ui-egui/src/layerchooser.rs | 24 ++++++++++++------------ crates/ui-egui/src/lib.rs | 8 +------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/crates/ui-egui/src/layerchooser.rs b/crates/ui-egui/src/layerchooser.rs index ead2039880..12df77197a 100644 --- a/crates/ui-egui/src/layerchooser.rs +++ b/crates/ui-egui/src/layerchooser.rs @@ -107,7 +107,7 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result { return add(app, json!({"path": path})); } let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - let slot = list_in_background(path, stop.clone(), crate::named_thread("rustgis-list-layers")); + 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(), @@ -124,20 +124,23 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result { /// 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`]). `spawn` starts the thread (a test -/// runs the work where it stands). +/// 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, - spawn: impl FnOnce(Box) -> std::io::Result<()>, + start: impl FnOnce(&crate::Slot>, Box), ) -> crate::Slot> { let slot: crate::Slot> = crate::Slot::default(); let out = slot.clone(); let listed = path.to_string(); - crate::work_off_frame_with(spawn, &slot, move || { - let result = list_layers(&listed, stop); - *out.lock().unwrap_or_else(|e| e.into_inner()) = Some(result); - }); + start( + &slot, + Box::new(move || { + let result = list_layers(&listed, stop); + *out.lock().unwrap_or_else(|e| e.into_inner()) = Some(result); + }), + ); slot } @@ -659,10 +662,7 @@ mod tests { let gpkg = dir.path().join("two.gpkg"); two_table_gpkg(&ds, &gpkg); let gpkg = gpkg.display().to_string(); - let inline = |work: Box| { - work(); - Ok(()) - }; + 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"])); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index b73a82a769..b5f8fa0d35 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -259,13 +259,7 @@ pub type Slot = std::sync::Arc>>; /// an error rather than the app (AGENTS.md › Use the threads). In the browser, which has no /// threads, that is every time. pub(crate) fn work_off_frame>(name: &str, slot: &Slot>, work: impl FnOnce() + Send + 'static) { - work_off_frame_with(named_thread(name), slot, work); -} - -/// The spawner [`work_off_frame`] uses: a thread named `name`. -pub(crate) fn named_thread(name: &str) -> impl FnOnce(Box) -> std::io::Result<()> { - let name = name.to_string(); - move |work| std::thread::Builder::new().name(name).spawn(work).map(drop) + work_off_frame_with(|work| std::thread::Builder::new().name(name.into()).spawn(work).map(drop), slot, work); } /// [`work_off_frame`], its thread started by `spawn`, which a test can make refuse.