Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 65 additions & 16 deletions crates/ui-egui/src/layerchooser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,8 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result<Value, String> {
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<Result<Value, String>> = 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(),
Expand All @@ -133,6 +122,37 @@ pub fn add_file(app: &mut RustGisApp, path: &str) -> Result<Value, String> {
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<std::sync::atomic::AtomicBool>,
start: impl FnOnce(&crate::Slot<Result<Value, String>>, Box<dyn FnOnce() + Send>),
) -> crate::Slot<Result<Value, String>> {
let slot: crate::Slot<Result<Value, String>> = 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<std::sync::atomic::AtomicBool>) -> Result<Value, String> {
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<Value, String> {
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -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<Value, String>) -> Chooser {
let slot: crate::Slot<Result<Value, String>> = crate::Slot::default();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Result<Value, String>>, work: Box<dyn FnOnce() + Send>| work();
let landed = |slot: crate::Slot<Result<Value, String>>| 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]
Expand Down
Loading