From df45c24ecf62c14bcd1ce09b5b814801a205906a Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 07:13:36 +0100 Subject: [PATCH 1/7] feat(ui): Export Map and Export Layout in the background (#1291 step 3) On the desktop, Export Map and Export Layout (PNG, PDF, SVG) now draw on a thread of their own, with the status bar's progress and Cancel that Export Features and Export Raster have (#1433). Export Map draws a copy of the map, and Export Layout a copy of the project, so moving the map or editing the layout meanwhile doesn't change the file. - Cancel also stops the map's wait for tiles (`wait_for_news_unless`); a cancelled export writes nothing. A map series PDF counts its pages in the progress bar. - A layout export that fails still says the warnings it gave first, then why it failed. - While a style added with its own view loads, Export Map runs before the window goes on, as the command waits for it. - Printing, and the exports of reports, presentations and animations, are unchanged. --- crates/data/src/progress.rs | 11 +++--- crates/engine/src/cmd/exports.rs | 32 ++++++++------- crates/engine/src/cmd/layout.rs | 67 ++++++++++++++++++++++++-------- crates/engine/src/cmd/share.rs | 55 +++++++++++++++++++------- crates/engine/src/tests.rs | 32 +++++++++++++-- crates/render/src/lib.rs | 9 ++++- crates/tiles/src/cache.rs | 9 ++++- crates/ui-egui/src/exportjob.rs | 34 +++++++++++++--- crates/ui-egui/src/lib.rs | 11 ++++++ docs/user-guide/sharing.md | 15 ++++--- 10 files changed, 207 insertions(+), 68 deletions(-) diff --git a/crates/data/src/progress.rs b/crates/data/src/progress.rs index 8602a7483a..c10b3a3a08 100644 --- a/crates/data/src/progress.rs +++ b/crates/data/src/progress.rs @@ -1,7 +1,8 @@ //! Cancelling a long read or write and following its progress (#1291 step 3). Whoever runs it //! (the app's export on a thread of its own) installs a [`Probe`] on that thread; the writers' -//! loops say how far they have come with [`begin`] and [`step`], and `step` stops them once the -//! probe was cancelled. With none installed, as for a script or the web app, they only run. +//! loops (and a layout export's pages) say how far they have come with [`begin`] and [`step`], +//! and `step` stops them once the probe was cancelled; [`check`] only stops (the map's wait for +//! its tiles). With none installed, as for a script or the web app, they only run. //! //! The analysis crate's run control is a probe, so the same Cancel and progress reach a tool and //! an export. @@ -43,7 +44,7 @@ impl Drop for Installed { } /// Starts counting a step of `total` items for the progress. -pub(crate) fn begin(total: usize) { +pub fn begin(total: usize) { CURRENT.with(|c| { if let Some(p) = c.borrow().as_ref() { p.begin(total as u64); @@ -53,7 +54,7 @@ pub(crate) fn begin(total: usize) { /// Counts `n` more items done, and stops the write once its run was asked to: call it for each /// feature, row or tile of a long loop. -pub(crate) fn step(n: usize) -> Result<()> { +pub fn step(n: usize) -> Result<()> { CURRENT.with(|c| match c.borrow().as_ref() { Some(p) if p.cancelled() => Err(DataError::Cancelled), Some(p) => { @@ -66,7 +67,7 @@ pub(crate) fn step(n: usize) -> Result<()> { /// Stops the write once its run was asked to, counting nothing: for a check after a loop that /// can't stop by itself. -pub(crate) fn check() -> Result<()> { +pub fn check() -> Result<()> { CURRENT.with(|c| match c.borrow().as_ref() { Some(p) if p.cancelled() => Err(DataError::Cancelled), _ => Ok(()), diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 8ed6f7110d..e225e5416d 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -1,5 +1,5 @@ //! Exports that write a file from what the session shares (a layer's features, a raster's -//! pixels). A script or the command line writes it where the command is called; the app writes it +//! pixels, a copy of the map or project to draw). A script or the command line writes it where the command is called; the app writes it //! on a thread of its own with progress and Cancel (#1291 step 3): [`export_start`] reads the //! session, [`ExportRun::run`] writes the file, and [`export_land`] says what was written, as the //! command would have. It lands whatever layer is active or project is open by then: the file is @@ -10,7 +10,7 @@ use serde_json::Value; use crate::{CmdResult, Session}; /// What an export wrote: the command's answer, and what it says of the file (the last says it -/// was exported). +/// was exported, unless `then` says it). pub struct Wrote { pub(crate) answer: Value, pub(crate) notes: Vec, @@ -19,20 +19,23 @@ pub struct Wrote { pub(crate) then: Option, } -/// What an export does to the session once its file is written, and adds to its answer. -pub(crate) type Then = Box; +/// What an export does to the session once its file is written, and adds to its answer; or the +/// failure it says once what came before it was said (a layout's warnings, then its error). +pub(crate) type Then = Box Result<(), String> + Send>; /// The writing of an export, its inputs read: it needs no session. pub(crate) type Work = Box Result + Send>; /// What an export reads of the session, and the work that writes its file. -type Start = fn(&Session, &Value) -> Result; +type Start = fn(&mut Session, &Value) -> Result; /// The exports the app writes on another thread ([`export_start`]), each with what it is called /// (in its progress, and a cancelled run's error) and what it reads of the session. const IN_BACKGROUND: &[(&str, &str, Start)] = &[ - ("share.exportLayer", "Export Features", super::share::layer_work), - ("share.exportRaster", "Export Raster", super::raster::export_work), + ("share.exportLayer", "Export Features", |s, p| super::share::layer_work(s, p)), + ("share.exportRaster", "Export Raster", |s, p| super::raster::export_work(s, p)), + ("share.exportMap", "Export Map", |s, p| super::share::map_work(s, p)), + ("layout.export", "Export Layout", |s, p| super::layout::export_work(s, p)), ]; /// `command` and what it is called, when the app writes its file on another thread (see @@ -47,7 +50,7 @@ pub(crate) fn write_here(s: &mut Session, p: &Value, command: &str) -> CmdResult let work = start(s, p)?; // Where a person at a terminal can stop it with Ctrl-C, as on the app's thread. let wrote = if s.stoppable { written_under(name, &rustgis_analysis::control::Control::stoppable(), work)? } else { work()? }; - Ok(said(s, wrote)) + said(s, wrote) } /// `work` under `control`, as [`rustgis_analysis::run_under`] runs it, except that a file once @@ -61,16 +64,16 @@ fn written_under(name: &str, control: &std::sync::Arc Value { +/// Says what an export wrote, and gives its answer (or the failure its `then` says). +fn said(s: &mut Session, wrote: Wrote) -> CmdResult { let Wrote { mut answer, notes, then } = wrote; for note in notes { s.notify(note, false); } if let Some(then) = then { - then(s, &mut answer); + then(s, &mut answer)?; } - answer + Ok(answer) } #[cfg(not(target_arch = "wasm32"))] @@ -107,8 +110,9 @@ pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result Value { +/// Says what [`ExportRun::run`] wrote, as its command would have, and gives its answer (or its +/// failure). +pub fn export_land(s: &mut Session, wrote: Wrote) -> CmdResult { said(s, wrote) } diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index b53ea41c09..48cb5ef688 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -121,7 +121,7 @@ pub fn commands() -> Vec { C::new("layout.seriesPage", "Map Series Page", "Publish › Map Frames", series_page).params(r#"{"layout"?, "page": number|"next"|"previous"}"#), C::new("layout.seriesRefresh", "Refresh Map Series", "Publish › Map Frames", |s, p| series_page(s, &json!({"layout": p.get("layout"), "page": "current"}))) .params(r#"{"layout"?}"#), - C::new("layout.export", "Export Layout", "Publish › Output", export) + C::new("layout.export", "Export Layout", "Publish › Output", |s, p| super::exports::write_here(s, p, "layout.export")) .view() .params(r#"{"layout"?, "path": "*.png|*.pdf|*.svg" (PNG/SVG: current page; SVG: each element a group of its own; text and shapes as SVG, map frames and surrounds as vectors, rasters and tiles in them as pictures at dpi), "dpi"?: 72..600, "allPages"?: bool (PDF only; default true: every map series page, up to 500; false: the current page)}"#), C::new("layout.save", "Save Layout File", "Publish › Save As", |s, p| { @@ -3338,8 +3338,8 @@ fn series_page(s: &mut Session, p: &Value) -> CmdResult { /// Renders a layout to PNG or PDF bytes. `all` renders every map series page (PDF only). /// What an export of a layout whose texts name installed families is told while the app is still -/// indexing the fonts (its first seconds; an export runs on its interface thread, which mustn't -/// wait): those texts are drawn in their fallbacks. A script or the command line indexes them at +/// indexing the fonts (its first seconds; the app's exports don't wait for them): those texts +/// are drawn in their fallbacks. A script or the command line indexes them at /// once. fn named_fonts_unread(layout: &Layout) -> Option<&'static str> { let named = layout.elements.iter().any(|e| matches!(&e.kind, ElementKind::Text { font_name: Some(_), .. })); @@ -3379,6 +3379,10 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) let mut out = Vec::new(); let mut png = None; let mut held = 0usize; + // How far a series' pages have come, when the export runs with progress. + if pages.len() > 1 { + rustgis_data::progress::begin(pages.len()); + } for page in &pages { if stepping { series_page(s, &json!({"layout": li, "page": page}))?; @@ -3420,6 +3424,8 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) let img = rustgis_render::layout::render(layout, &mut s.project.maps, dpi, &info).ok_or("the page is too large to draw")?; png = Some(img.encode_png().map_err(|e| e.to_string())?); } + // A cancelled export stops here, its layout put back below. + rustgis_data::progress::step(1).map_err(|e| e.to_string())?; } Ok((out, png)) }; @@ -3471,24 +3477,53 @@ pub(crate) fn pictured_notices(s: &mut Session, frames: &[(u32, String)], pages: } } -fn export(s: &mut Session, p: &Value) -> CmdResult { +/// `layout.export`: a layout to a PNG, PDF or SVG. It draws a copy of the project, so the project +/// can change meanwhile (a map series steps through its pages on the copy); a cancelled export +/// stops at the next page, or its map's wait for tiles, and writes nothing. +pub(crate) fn export_work(s: &Session, p: &Value) -> Result { let path = ps(p, "path").ok_or("path is required")?.to_string(); let li = index(s, p)?; let lower = path.to_ascii_lowercase(); - let pdf = lower.ends_with(".pdf"); - if lower.ends_with(".svg") { - let text = layout_svg(s, li, f(p, "dpi").unwrap_or(150.0))?; - rustgis_data::io::write_atomic(Path::new(&path), text.as_bytes()).map_err(|e| format!("{path}: {e}"))?; - s.notify(format!("Exported {path}"), false); - return Ok(json!({"path": path, "bytes": text.len()})); - } - if !pdf && !lower.ends_with(".png") { + let (pdf, svg) = (lower.ends_with(".pdf"), lower.ends_with(".svg")); + if !pdf && !svg && !lower.ends_with(".png") { return Err("export to a .png, .pdf or .svg file".into()); } - let bytes = layout_bytes(s, li, pdf, f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true))?; - rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; - s.notify(format!("Exported {path}"), false); - Ok(json!({"path": path, "bytes": bytes.len()})) + let (dpi, all) = (f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true)); + let project = s.project.clone(); + Ok(Box::new(move || { + let mut copy = Session::new(project); + let written = (|| { + let bytes = if svg { layout_svg(&mut copy, li, dpi)?.into_bytes() } else { layout_bytes(&mut copy, li, pdf, dpi, all)? }; + rustgis_data::progress::check().map_err(|e| e.to_string())?; + rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?; + Ok::<_, String>(bytes.len()) + })(); + // A cancelled export says only that it was cancelled. + if let (Err(e), Err(_)) = (&written, rustgis_data::progress::check()) { + return Err(e.clone()); + } + // What drawing it said (frames drawn as pictures, fonts it lacks), as the command says it + // here, then that it was written or why not: each notice whole, with the severity its + // history record has (the two are kept together, the history's text shortened). + let severities: Vec<_> = copy.diagnostics.records().into_iter().rev().map(|r| r.severity).collect(); + let mut said: Vec<_> = copy.notices.into_iter().rev().zip(severities).map(|(n, severity)| (severity, n.text)).collect(); + said.reverse(); + let answer = written.as_ref().map_or(Value::Null, |bytes| json!({"path": path, "bytes": bytes})); + let then: super::exports::Then = Box::new(move |s: &mut Session, _: &mut Value| { + use rustgis_data::diagnostics::Severity; + for (severity, text) in said { + match severity { + Severity::Error => s.notify(text, true), + Severity::Warning => s.warn(text), + Severity::Info => s.notify(text, false), + } + } + written?; + s.notify(format!("Exported {path}"), false); + Ok(()) + }); + Ok(super::exports::Wrote { answer, notes: Vec::new(), then: Some(then) }) + })) } /// The layout `li` as it is now (a map series at its current page) as SVG, kept as vectors with diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index ea126b233c..ebda602d49 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -10,12 +10,17 @@ use serde_json::{Value, json}; use super::exports::{Work, Wrote}; use super::{f, need_layer, ps}; -use crate::{CmdResult, CommandSpec as C, Session}; +use crate::{CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ - C::new("share.exportMap", "Export Map", "Publish › Output", export_map) - .view() + C::new("share.exportMap", "Export Map", "Publish › Output", |s, p| { + // A style added with its own view: the export waits for it to load, as it does for + // tiles. The app runs it here, not on a thread, while one loads. + super::tiles::follow_style_view(s, web_time::Duration::from_secs(30)); + super::exports::write_here(s, p, "share.exportMap") + }) + .view() .params(r#"{"path": "*.png", "width"?: px, "height"?: px, "dpi"?: 96..600}"#), C::new("share.exportLayer", "Export Features", "Publish › Output", |s, p| super::exports::write_here(s, p, "share.exportLayer")) .view() @@ -136,21 +141,32 @@ fn report_table(s: &mut Session, path: &str, gpkg: &GpkgTable, table: Option, height: Option, dpi: Option) -> Result, String> { - let (vw, vh) = s.view_size; + let view_size = s.view_size; + png_of(s.map_mut(), view_size, width, height, dpi) +} + +/// Renders `map` at its camera in a view of `(vw, vh)` points (or a requested size) to PNG bytes. +fn png_of( + map: &mut rustgis_map::Map, + (vw, vh): (f64, f64), + width: Option, + height: Option, + dpi: Option, +) -> Result, String> { // NaN would pass through `clamp`; non-finite values fall back to the defaults. let fin = |v: Option| v.filter(|x| x.is_finite()); let (width, height, dpi) = (fin(width), fin(height), fin(dpi)); let w = width.unwrap_or(vw).clamp(16.0, 8000.0); let h = height.unwrap_or(vh).clamp(16.0, 8000.0); let ppp = (dpi.unwrap_or(96.0) / 96.0).clamp(0.5, 6.0); - let mut camera = s.map().camera; + let mut camera = map.camera; // Keep the same ground extent when the size differs from the view. if width.is_some() || height.is_some() { camera.res *= (vw / w).max(vh / h); camera.clamp(); } let view = View { width: w, height: h, pixels_per_point: ppp, camera, sky: false }; - rustgis_render::render_png(s.map_mut(), &view) + rustgis_render::render_png(map, &view) } /// The map view on one PDF page (for printing): the page is the view's shape at 96 points per @@ -175,14 +191,19 @@ pub fn map_pdf(s: &mut Session, dpi: f64) -> Result, String> { Ok(rustgis_render::pdf::write(&[page], &s.map().name)) } -fn export_map(s: &mut Session, p: &Value) -> CmdResult { +/// `share.exportMap`: draws the active map as it is now to a PNG. It draws a copy of the map, so +/// the map can move meanwhile; a cancelled export stops waiting for tiles and writes nothing. +pub(crate) fn map_work(s: &Session, p: &Value) -> Result { let path = ps(p, "path").ok_or("path is required")?.to_string(); - // A style added with its own view: the export waits for it to load, as it does for tiles. - super::tiles::follow_style_view(s, web_time::Duration::from_secs(30)); - let png = map_png(s, f(p, "width"), f(p, "height"), f(p, "dpi"))?; - rustgis_data::io::write_atomic(std::path::Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?; - s.notify(format!("Exported {path}"), false); - Ok(json!({"path": path, "bytes": png.len()})) + let (width, height, dpi) = (f(p, "width"), f(p, "height"), f(p, "dpi")); + let (mut map, view_size) = (s.map().clone(), s.view_size); + Ok(Box::new(move || { + let png = png_of(&mut map, view_size, width, height, dpi)?; + rustgis_data::progress::check().map_err(|e| e.to_string())?; + rustgis_data::io::write_atomic(Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?; + let answer = json!({"path": path, "bytes": png.len()}); + Ok(Wrote { answer, notes: vec![format!("Exported {path}")], then: None }) + })) } /// `share.exportLayer`: writes a layer to a file by its extension. A streamed layer exports its @@ -258,8 +279,12 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk // The table written, said and put in the answer once the export lands, where the session // (layers reading a replaced table) is at hand. let path = path.to_string(); - let then: Option = table - .map(|t| Box::new(move |s: &mut Session, out: &mut Value| report_table(s, &path, &gpkg, Some(t), out)) as super::exports::Then); + let then: Option = table.map(|t| { + Box::new(move |s: &mut Session, out: &mut Value| { + report_table(s, &path, &gpkg, Some(t), out); + Ok(()) + }) as super::exports::Then + }); Ok(Wrote { answer, notes: said, then }) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 606d28eea8..70d0354194 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -28047,7 +28047,12 @@ fn exports_run_on_another_thread() { let dir = std::env::temp_dir().join(format!("rustgis-exports-thread-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let at = |name: &str| dir.join(name).display().to_string(); + s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap(); for (command, file, p) in [ + ("share.exportMap", "map.png", json!({"width": 64, "height": 48})), + ("layout.export", "layout.png", json!({"dpi": 48})), + ("layout.export", "layout.pdf", json!({"dpi": 48})), + ("layout.export", "layout.svg", json!({"dpi": 48})), ("share.exportLayer", "countries.geojson", json!({"layer": "Countries"})), ("share.exportLayer", "countries.shp", json!({"layer": "Countries"})), ("share.exportLayer", "countries.gpkg", json!({"layer": "Countries", "crs": "EPSG:3857"})), @@ -28071,7 +28076,7 @@ fn exports_run_on_another_thread() { let mark = s.notice_mark(); let wrote = std::thread::spawn(move || run.run(&Control::new())).join().unwrap().unwrap(); assert!(s.notices_since(mark).is_empty(), "{command}: nothing is said before it lands"); - let answer_there = export_land(&mut s, wrote); + let answer_there = export_land(&mut s, wrote).unwrap(); assert_eq!(said(&s, mark, &there), said_here, "{command} {file}"); // The path as it appears in JSON: on Windows its backslashes are escaped there. let unpathed = |v: &Value, path: &str| { @@ -28096,8 +28101,29 @@ fn exports_run_on_another_thread() { assert!(export_start(&mut s, "share.exportLayer", &json!({"layer": "nowhere", "path": at("x.geojson")})).is_err()); assert!(export_start(&mut s, "share.exportLayer", &json!({"layer": "Countries", "path": at("x.gpkg"), "crs": "EPSG:nope"})).is_err()); assert!(export_start(&mut s, "share.exportRaster", &json!({"layer": dem, "path": at("x.tif"), "band": 99})).is_err()); - // Map and layout exports draw the map: they still run where they're called. - assert!(runs_in_background("share.exportMap").is_none() && runs_in_background("layout.export").is_none()); + assert!(export_start(&mut s, "layout.export", &json!({"path": at("x.jpg")})).is_err(), "a format it can't write"); + // Map and layout exports draw a copy of what was there when they started: moving the map or + // removing the layout meanwhile changes nothing in the file. + let (here, there) = (at("still_here.png"), at("still_there.png")); + s.run("share.exportMap", &json!({"path": here, "width": 64, "height": 48})).unwrap(); + let run = export_start(&mut s, "share.exportMap", &json!({"path": there, "width": 64, "height": 48})).unwrap(); + s.map_mut().camera.res *= 4.0; + run.run(&Control::new()).unwrap(); + assert_eq!(std::fs::read(&there).unwrap(), std::fs::read(&here).unwrap()); + let (here, there) = (at("still_here.svg"), at("still_there.svg")); + s.map_mut().camera.res /= 4.0; + s.run("layout.export", &json!({"path": here, "dpi": 48})).unwrap(); + let run = export_start(&mut s, "layout.export", &json!({"path": there, "dpi": 48})).unwrap(); + let layouts = std::mem::take(&mut s.project.layouts); + run.run(&Control::new()).unwrap(); + assert_eq!(std::fs::read(&there).unwrap(), std::fs::read(&here).unwrap()); + s.project.layouts = layouts; + // A layout export that fails says why when it lands, as it does here. + let nowhere = dir.join("no").join("such.png").display().to_string(); + let here = s.run("layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap_err(); + let run = export_start(&mut s, "layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap(); + let wrote = run.run(&Control::new()).unwrap(); + assert_eq!(export_land(&mut s, wrote).unwrap_err(), here); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 1899969445..7e7aaa3ef3 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -5589,11 +5589,16 @@ fn complete(map: &mut Map, view: &View, record: bool) -> Option { let start = web_time::Instant::now(); let budget = web_time::Duration::from_secs(60); let mut idle = 0; - while r.tiles_pending && idle < IDLE_PASSES && start.elapsed() < budget && cfg!(not(target_arch = "wasm32")) { + // An export cancelled meanwhile (see `rustgis_data::progress`) stops waiting: it writes nothing. + let cancelled = || rustgis_data::progress::check().is_err(); + while r.tiles_pending && idle < IDLE_PASSES && start.elapsed() < budget && cfg!(not(target_arch = "wasm32")) && !cancelled() { // Each wait within what is left of the minute, until something has come: a fetch of // another's (a map open beside it, a slow server) doesn't hold it up. let left = budget.saturating_sub(start.elapsed()); - rustgis_tiles::cache::wait_for_news(drawn_at, left.min(web_time::Duration::from_secs(30))); + rustgis_tiles::cache::wait_for_news_unless(drawn_at, left.min(web_time::Duration::from_secs(30)), cancelled); + if cancelled() { + break; + } let now = rustgis_tiles::cache::generation(); if now == drawn_at { idle += 1; diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 07ecdf8ac7..31eec53255 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -349,11 +349,16 @@ const SETTLE: Duration = Duration::from_secs(1); /// a moment, or for a second at most: one drawing again with what came needn't wait for every /// fetch in the process (another map's, a slow server's, one never answered). Native only. pub fn wait_for_news(since: u64, timeout: Duration) { + wait_for_news_unless(since, timeout, || false); +} + +/// [`wait_for_news`], given up as soon as `stop` says so (an export cancelled meanwhile). +pub fn wait_for_news_unless(since: u64, timeout: Duration, stop: impl Fn() -> bool) { #[cfg(not(target_arch = "wasm32"))] { let start = Instant::now(); let (mut seen, mut changed, mut news) = (since, Instant::now(), None); - while in_flight() > 0 && start.elapsed() < timeout { + while in_flight() > 0 && start.elapsed() < timeout && !stop() { let now = generation(); if now != seen { (seen, changed) = (now, Instant::now()); @@ -366,7 +371,7 @@ pub fn wait_for_news(since: u64, timeout: Duration) { } } #[cfg(target_arch = "wasm32")] - let _ = (since, timeout); + let _ = (since, timeout, stop); } /// A waiting fetch: what to decode it as, whose diagnostics it goes to, and its expected statuses. diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index cd579745bd..af14cfc642 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -1,7 +1,7 @@ -//! Export Features and Export Raster on the desktop write their file on a thread of their own -//! (#1291 step 3), so a large layer or raster doesn't hold the window: the status bar shows the -//! export under way, how far it has come and Cancel, and [`land`] says what it wrote, as the -//! command says when it runs here. +//! Export Features, Export Raster, Export Map and Export Layout on the desktop write their file +//! on a thread of their own (#1291 step 3), so a large layer, raster or map doesn't hold the +//! window: the status bar shows the export under way, how far it has come and Cancel, and +//! [`land`] says what it wrote, as the command says when it runs here. use std::sync::Arc; @@ -57,7 +57,9 @@ pub fn land(app: &mut RustGisApp) -> bool { match done { // Its notices are the command's; the answer was for a script, which waits for its own. Ok(wrote) => { - exports::export_land(&mut app.session, wrote); + if let Err(e) = exports::export_land(&mut app.session, wrote) { + app.session.notify(e, true); + } } Err(e) => app.session.notify(e, true), } @@ -132,4 +134,26 @@ mod tests { assert!(left.iter().all(|n| !n.to_string_lossy().ends_with(".tmp")), "{left:?}"); let _ = std::fs::remove_dir_all(&dir); } + + #[test] + fn export_map_and_export_layout_draw_on_a_thread() { + let mut app = RustGisApp::with_sample(); + app.run("layout.new", json!({"page": "a4", "template": "map"})).unwrap(); + let dir = std::env::temp_dir().join(format!("rustgis-export-drawn-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + for (command, name, file) in [("ui.exportMap", "Export Map", "map.png"), ("ui.exportLayout", "Export Layout", "layout.png")] { + let path = dir.join(file); + crate::dialogs::PICKED.with(|c| *c.borrow_mut() = Some(path.display().to_string())); + assert_eq!(app.run(command, Value::Null).unwrap()["started"], name); + let end = std::time::Instant::now() + std::time::Duration::from_secs(120); + while land(&mut app) { + assert!(std::time::Instant::now() < end, "{name} never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + let exported = format!("Exported {}", path.display()); + assert!(app.session.notices.iter().any(|n| n.text == exported && !n.error), "{:?}", app.session.notices.last()); + assert!(std::fs::metadata(&path).is_ok_and(|m| m.len() > 100), "{file}"); + } + let _ = std::fs::remove_dir_all(&dir); + } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 6b11cfc67f..8804112290 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3279,6 +3279,10 @@ impl RustGisApp { let (exts, kinds): (&[&str], _) = if pdf_only { (&["pdf"], tl!("PDF")) } else { (&["pdf", "png", "svg"], tl!("PDF, PNG or SVG")) }; if let Some(path) = dialogs::pick_save(&format!("{name}.pdf"), exts, kinds) { + // Drawn on a thread of its own, with progress and Cancel in the status bar. + #[cfg(not(target_arch = "wasm32"))] + return exportjob::start(self, "layout.export", json!({"layout": li, "path": path, "dpi": 200})); + #[cfg(target_arch = "wasm32")] return self.run("layout.export", json!({"layout": li, "path": path, "dpi": 200})); } } @@ -3789,6 +3793,13 @@ impl RustGisApp { "ui.exportMap" => { let name = format!("{}.png", self.session.map().name); if let Some(path) = dialogs::pick_save(&name, &["png"], tl!("PNG image")) { + // Drawn on a thread of its own, with progress and Cancel in the status bar; + // here while a style added with its own view loads, as the command waits for + // it to move the map there first. + #[cfg(not(target_arch = "wasm32"))] + if self.session.style_view.is_none() { + return exportjob::start(self, "share.exportMap", json!({"path": path, "dpi": 192})); + } return self.run("share.exportMap", json!({"path": path, "dpi": 192})); } } diff --git a/docs/user-guide/sharing.md b/docs/user-guide/sharing.md index efdffa3637..6c9511b225 100644 --- a/docs/user-guide/sharing.md +++ b/docs/user-guide/sharing.md @@ -19,12 +19,15 @@ table. **Data › Export** also writes rasters to GeoTIFF, and **Data Interop** KML, GPX or CSV with WKT. Layouts, reports and presentations export from their own tabs and panes (see [Layouts and Reports](layouts.md)). -On the desktop, Export Features and Export Raster write their file in the background, so the map -and the rest of the window stay in use. While one runs, the status bar shows its name, a progress -bar (Export Raster's untiled GeoTIFF shows none) and **Cancel**; when it ends its messages appear -as before. One export runs at a time. A cancelled export writes nothing, and a file it was to -replace stays as it was. Scripts, models, the AI Assistant and the command line still wait for -the file. Export Map and layout exports still run before the window goes on. +On the desktop, Export Features, Export Raster, Export Map and Export Layout write their file in +the background, so the map and the rest of the window stay in use. While one runs, the status +bar shows its name, a progress bar (Export Raster's untiled GeoTIFF, Export Map and a one-page +layout show none) and **Cancel**; when it ends its messages appear as before. One export runs at +a time. Export Map and Export Layout draw the map and layout as they were when the export +started, so moving the map or editing the layout meanwhile doesn't change the file. A cancelled +export writes nothing, and a file it was to replace stays as it was. Scripts, models, the AI +Assistant and the command line still wait for the file. Printing a layout, and the exports of +reports, presentations and animations, still run before the window goes on. A GeoPackage export writes a new file by default, and refuses an existing `.gpkg` to protect its tables, even if the save dialog offers to replace the file. To put the layer into an existing From 582a8a38aa2a91427f7fcb61768f59e0b4dd972d Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 07:56:54 +0100 Subject: [PATCH 2/7] fix(exports): the drawing copy reads for itself, and its failures reach the live layers - A streamed layer's read under way stays with the live map (`Map::detached`): the copy and the map shared it, and the one that didn't take it waited for it forever. - A source that failed on the copy is marked on its live layer, unless the layer was removed or opened from elsewhere meanwhile; a failed map export also says its warnings first. - Notices keep their severity, so the copy's are said again without pairing them with the history; what a landed export says is filed under its command in the history. - Export Map on the window's thread (a style's view loading) is refused while another export runs, and the guide says it runs there. --- crates/engine/src/cmd/exports.rs | 70 ++++++++++++++++++++++++++++---- crates/engine/src/cmd/layout.rs | 34 ++++------------ crates/engine/src/cmd/share.rs | 14 ++++--- crates/engine/src/lib.rs | 6 ++- crates/engine/src/tests.rs | 26 +++++++++++- crates/map/src/lib.rs | 10 +++++ crates/map/src/source.rs | 8 ++++ crates/ui-egui/src/exportjob.rs | 20 ++++++--- crates/ui-egui/src/lib.rs | 2 + docs/user-guide/sharing.md | 17 ++++---- 10 files changed, 150 insertions(+), 57 deletions(-) diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index e225e5416d..5c64c869fb 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -76,12 +76,64 @@ fn said(s: &mut Session, wrote: Wrote) -> CmdResult { Ok(answer) } +/// Lands an export drawn on a copy of the map or project (Export Map, Export Layout): what +/// drawing it said, with each notice's severity; the streamed layers whose source failed on the +/// copy, marked on their live layers as drawing them there would have; then that it was written +/// to `path`, or why not. A cancelled one says only that it was cancelled. +pub(crate) fn drawn( + maps: &[rustgis_map::Map], + said: Vec<(rustgis_data::diagnostics::Severity, String)>, + written: Result, + path: String, +) -> Result { + if let (Err(e), Err(_)) = (&written, rustgis_data::progress::check()) { + return Err(e.clone()); + } + // Each by its live layer, with the source it failed on: one the user removed, or opened + // afresh meanwhile, is left as it is. + let failed: Vec<_> = maps + .iter() + .flat_map(|m| &m.layers) + .filter_map(|l| { + let src = l.source.as_ref()?; + Some((l.instance_id(), src.error.clone()?, src.path.clone(), src.table.clone())) + }) + .collect(); + let answer = written.as_ref().map_or(Value::Null, Value::clone); + let then: Then = Box::new(move |s: &mut Session, _: &mut Value| { + use rustgis_data::diagnostics::Severity; + for (severity, text) in said { + match severity { + Severity::Error => s.notify(text, true), + Severity::Warning => s.warn(text), + Severity::Info => s.notify(text, false), + } + } + for (instance, error, path, table) in failed { + let live = s.project.maps.iter_mut().flat_map(|m| m.layers.iter_mut()).find(|l| l.instance_id() == instance); + if let Some(src) = live.and_then(|l| l.source.as_mut()) + && src.error.is_none() + && src.path == path + && src.table == table + { + src.error = Some(error); + } + } + written?; + s.notify(format!("Exported {path}"), false); + Ok(()) + }); + Ok(Wrote { answer, notes: Vec::new(), then: Some(then) }) +} + #[cfg(not(target_arch = "wasm32"))] /// An export ([`IN_BACKGROUND`]) started to write its file on another thread ([`ExportRun::run`]) /// while the session goes on: its inputs are taken now (cheaply: the features and pixels are /// shared, and a streamed layer's source is read on the export's thread), and [`export_land`] /// says what it wrote. pub struct ExportRun { + /// The command it runs, which [`export_land`] says what it wrote as. + pub command: &'static str, /// What the export is called, for its progress and a cancelled run's error. pub name: &'static str, work: Work, @@ -93,7 +145,7 @@ impl ExportRun { /// (see [`rustgis_analysis::control`]). A cancelled export leaves no file, or the one there /// before. pub fn run(self, control: &std::sync::Arc) -> Result { - let ExportRun { name, work } = self; + let ExportRun { name, work, .. } = self; // A bug in a writer must not take the app down. std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| written_under(name, control, work))) .unwrap_or_else(|_| Err(format!("{name} failed unexpectedly"))) @@ -104,16 +156,20 @@ impl ExportRun { /// Starts `command` (one of [`IN_BACKGROUND`]'s) with `p`, to write its file on another thread: /// its parameters checked and its inputs taken now. pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result { - let &(_, name, start) = + let &(command, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?; - Ok(ExportRun { name, work: start(s, p)? }) + Ok(ExportRun { command, name, work: start(s, p)? }) } #[cfg(not(target_arch = "wasm32"))] -/// Says what [`ExportRun::run`] wrote, as its command would have, and gives its answer (or its -/// failure). -pub fn export_land(s: &mut Session, wrote: Wrote) -> CmdResult { - said(s, wrote) +/// Says what [`ExportRun::run`] wrote, as its `command` ([`ExportRun::command`]) would have, and +/// gives its answer (or its failure): the troubleshooting history files what it says under that +/// command, as when it runs here. +pub fn export_land(s: &mut Session, command: &'static str, wrote: Wrote) -> CmdResult { + let before = s.diagnostic_source.replace(command); + let landed = said(s, wrote); + s.diagnostic_source = before; + landed } #[cfg(test)] diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 48cb5ef688..7837b84fb0 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3489,40 +3489,22 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result(bytes.len()) + Ok(json!({"path": path, "bytes": bytes.len()})) })(); - // A cancelled export says only that it was cancelled. - if let (Err(e), Err(_)) = (&written, rustgis_data::progress::check()) { - return Err(e.clone()); - } // What drawing it said (frames drawn as pictures, fonts it lacks), as the command says it - // here, then that it was written or why not: each notice whole, with the severity its - // history record has (the two are kept together, the history's text shortened). - let severities: Vec<_> = copy.diagnostics.records().into_iter().rev().map(|r| r.severity).collect(); - let mut said: Vec<_> = copy.notices.into_iter().rev().zip(severities).map(|(n, severity)| (severity, n.text)).collect(); - said.reverse(); - let answer = written.as_ref().map_or(Value::Null, |bytes| json!({"path": path, "bytes": bytes})); - let then: super::exports::Then = Box::new(move |s: &mut Session, _: &mut Value| { - use rustgis_data::diagnostics::Severity; - for (severity, text) in said { - match severity { - Severity::Error => s.notify(text, true), - Severity::Warning => s.warn(text), - Severity::Info => s.notify(text, false), - } - } - written?; - s.notify(format!("Exported {path}"), false); - Ok(()) - }); - Ok(super::exports::Wrote { answer, notes: Vec::new(), then: Some(then) }) + // here. + let said = std::mem::take(&mut copy.notices).into_iter().map(|n| (n.severity, n.text)).collect(); + super::exports::drawn(©.project.maps, said, written, path) })) } diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index ebda602d49..efa96122fe 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -196,13 +196,15 @@ pub fn map_pdf(s: &mut Session, dpi: f64) -> Result, String> { pub(crate) fn map_work(s: &Session, p: &Value) -> Result { let path = ps(p, "path").ok_or("path is required")?.to_string(); let (width, height, dpi) = (f(p, "width"), f(p, "height"), f(p, "dpi")); - let (mut map, view_size) = (s.map().clone(), s.view_size); + let (mut map, view_size) = (s.map().detached(), s.view_size); Ok(Box::new(move || { - let png = png_of(&mut map, view_size, width, height, dpi)?; - rustgis_data::progress::check().map_err(|e| e.to_string())?; - rustgis_data::io::write_atomic(Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?; - let answer = json!({"path": path, "bytes": png.len()}); - Ok(Wrote { answer, notes: vec![format!("Exported {path}")], then: None }) + let written = (|| { + let png = png_of(&mut map, view_size, width, height, dpi)?; + rustgis_data::progress::check().map_err(|e| e.to_string())?; + rustgis_data::io::write_atomic(Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?; + Ok(json!({"path": path, "bytes": png.len()})) + })(); + super::exports::drawn(std::slice::from_ref(&map), Vec::new(), written, path) })) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 574c4733ed..376e9042f8 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -451,6 +451,8 @@ pub struct TopologyIssue { pub struct Notice { pub text: String, pub error: bool, + /// How the troubleshooting history files it (an error, a warning or information). + pub(crate) severity: rustgis_data::diagnostics::Severity, } const UNDO_LIMIT: usize = 40; @@ -665,7 +667,7 @@ pub struct Session { put_aside: Option>, depth: u32, /// Only the command's registered id, never its parameters. Nested commands restore this. - diagnostic_source: Option<&'static str>, + pub(crate) diagnostic_source: Option<&'static str>, /// How many of the commands running now restore the project if they fail: a replacement is /// put aside only under one of them, as only then can it be rolled back. restorable: u32, @@ -1650,7 +1652,7 @@ impl Session { fn notice_redacted(&mut self, text: rustgis_data::diagnostics::Redacted, severity: rustgis_data::diagnostics::Severity) { self.diagnostics.push_redacted(severity, self.diagnostic_source.unwrap_or("Application"), &text); - self.notices.push(Notice { text: text.into_string(), error: severity == rustgis_data::diagnostics::Severity::Error }); + self.notices.push(Notice { text: text.into_string(), error: severity == rustgis_data::diagnostics::Severity::Error, severity }); self.notices_given += 1; if self.notices.len() > 200 { self.notices.remove(0); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 70d0354194..657fecfcff 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -28076,7 +28076,9 @@ fn exports_run_on_another_thread() { let mark = s.notice_mark(); let wrote = std::thread::spawn(move || run.run(&Control::new())).join().unwrap().unwrap(); assert!(s.notices_since(mark).is_empty(), "{command}: nothing is said before it lands"); - let answer_there = export_land(&mut s, wrote).unwrap(); + let answer_there = export_land(&mut s, command, wrote).unwrap(); + let filed = s.diagnostics.records().last().map(|r| r.source.clone()); + assert_eq!(filed.as_deref(), Some(command), "the history files what it said under the command"); assert_eq!(said(&s, mark, &there), said_here, "{command} {file}"); // The path as it appears in JSON: on Windows its backslashes are escaped there. let unpathed = |v: &Value, path: &str| { @@ -28123,7 +28125,27 @@ fn exports_run_on_another_thread() { let here = s.run("layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap_err(); let run = export_start(&mut s, "layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap(); let wrote = run.run(&Control::new()).unwrap(); - assert_eq!(export_land(&mut s, wrote).unwrap_err(), here); + assert_eq!(export_land(&mut s, "layout.export", wrote).unwrap_err(), here); + // A streamed layer's source that failed on the copy is marked on its live layer, unless that + // was opened from somewhere else meanwhile. + let li = s.map().layers.iter().position(|l| l.name == "Countries").unwrap(); + let mut src = rustgis_map::source::SourceLayer::default(); + src.path = Some("gone.fgb".into()); + s.map_mut().layers[li].source = Some(src.clone()); + let mut copy = s.map().detached(); + let mut failed = src.clone(); + failed.error = Some("gone.fgb: not found".into()); + copy.layers[li].source = Some(failed); + let land = |s: &mut Session, copy: &rustgis_map::Map| { + let wrote = crate::cmd::exports::drawn(std::slice::from_ref(copy), Vec::new(), Ok(json!({})), at("drawn.png")).unwrap(); + export_land(s, "share.exportMap", wrote).unwrap(); + s.map().layers[li].source.as_ref().and_then(|src| src.error.clone()) + }; + assert_eq!(land(&mut s, ©).as_deref(), Some("gone.fgb: not found")); + s.map_mut().layers[li].source.as_mut().unwrap().error = None; + s.map_mut().layers[li].source.as_mut().unwrap().path = Some("found.fgb".into()); + assert_eq!(land(&mut s, ©), None, "a source opened afresh keeps its own state"); + s.map_mut().layers[li].source = None; let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 487c84e68c..c841b54059 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1406,6 +1406,16 @@ impl Map { } } + /// A copy to draw elsewhere (an export on a thread of its own): its layers keep this map's + /// identities, and its streamed layers read for themselves (see [`source::SourceLayer::detach_read`]). + pub fn detached(&self) -> Map { + let mut copy = self.clone(); + for source in copy.layers.iter_mut().filter_map(|l| l.source.as_mut()) { + source.detach_read(); + } + copy + } + /// Identifies this live map, whatever its name or place among the project's maps. pub fn instance_id(&self) -> u64 { self.instance_id diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 83223919a5..1b6d1880af 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -915,6 +915,14 @@ impl SourceLayer { self.open() } + /// Leaves a read under way to the layer this one was copied from, which takes it when it + /// lands: a copy drawn elsewhere (an export on a thread of its own) reads what it needs for + /// itself. The two would otherwise share the read, and the one that didn't take it would wait + /// for it forever. + pub fn detach_read(&mut self) { + self.pending = None; + } + /// Forgets the window, so the next view reads again (after the source changes). pub fn reset(&mut self) { self.render_lod = Default::default(); diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index af14cfc642..f532cafd6b 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -15,19 +15,26 @@ use crate::theme::Tokens; /// An export under way: what it is called, its Cancel and progress, and what it wrote once done. pub struct ExportJob { + command: &'static str, name: &'static str, control: Arc, slot: crate::Slot>, } +/// Refuses another export while one runs, whichever way it would run (one at a time). +pub fn idle(app: &RustGisApp) -> Result<(), String> { + match &app.export_job { + Some(job) => Err(crate::i18n::fmt("{tool} is still running: wait for it, or cancel it.", &[("tool", tl_raw!(job.name))])), + None => Ok(()), + } +} + /// Starts `command` ([`exports::runs_in_background`]) with `p` on a thread of its own. One at a /// time: a second is refused while the first runs. pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result { - if let Some(job) = &app.export_job { - return Err(crate::i18n::fmt("{tool} is still running: wait for it, or cancel it.", &[("tool", tl_raw!(job.name))])); - } + idle(app)?; let run = exports::export_start(&mut app.session, command, &p)?; - let name = run.name; + let (command, name) = (run.command, run.name); // Cancel stops it. let control = Control::stoppable(); let (ctl, slot) = (control.clone(), crate::Slot::default()); @@ -43,7 +50,7 @@ pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result bool { let Some(job) = &app.export_job else { return false }; let done = job.slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take(); let Some(done) = done else { return true }; + let command = job.command; app.export_job = None; match done { // Its notices are the command's; the answer was for a script, which waits for its own. Ok(wrote) => { - if let Err(e) = exports::export_land(&mut app.session, wrote) { + if let Err(e) = exports::export_land(&mut app.session, command, wrote) { app.session.notify(e, true); } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 8804112290..859f8aaeae 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3800,6 +3800,8 @@ impl RustGisApp { if self.session.style_view.is_none() { return exportjob::start(self, "share.exportMap", json!({"path": path, "dpi": 192})); } + #[cfg(not(target_arch = "wasm32"))] + exportjob::idle(self)?; return self.run("share.exportMap", json!({"path": path, "dpi": 192})); } } diff --git a/docs/user-guide/sharing.md b/docs/user-guide/sharing.md index 6c9511b225..803d228b0d 100644 --- a/docs/user-guide/sharing.md +++ b/docs/user-guide/sharing.md @@ -20,14 +20,15 @@ KML, GPX or CSV with WKT. Layouts, reports and presentations export from their o (see [Layouts and Reports](layouts.md)). On the desktop, Export Features, Export Raster, Export Map and Export Layout write their file in -the background, so the map and the rest of the window stay in use. While one runs, the status -bar shows its name, a progress bar (Export Raster's untiled GeoTIFF, Export Map and a one-page -layout show none) and **Cancel**; when it ends its messages appear as before. One export runs at -a time. Export Map and Export Layout draw the map and layout as they were when the export -started, so moving the map or editing the layout meanwhile doesn't change the file. A cancelled -export writes nothing, and a file it was to replace stays as it was. Scripts, models, the AI -Assistant and the command line still wait for the file. Printing a layout, and the exports of -reports, presentations and animations, still run before the window goes on. +the background, so the map and the rest of the window stay in use. While one runs, the status bar +shows its name, a progress bar (Export Raster's untiled GeoTIFF, Export Map and a one-page layout +show none) and **Cancel**; when it ends its messages appear as before. One export runs at a time. +Export Map and Export Layout draw the map and layout as they were when the export started, so +moving the map or editing the layout meanwhile doesn't change the file. A cancelled export writes +nothing, and a file it was to replace stays as it was. Scripts, models, the AI Assistant and the +command line still wait for the file. Printing a layout, and the exports of reports, presentations +and animations, still run before the window goes on. So does Export Map while a style added with +its own view is still loading: it waits for the map to move there, without progress or **Cancel**. A GeoPackage export writes a new file by default, and refuses an existing `.gpkg` to protect its tables, even if the save dialog offers to replace the file. To put the layer into an existing From 669031300e9720074685dc2479479db830dec6d7 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 08:50:56 +0100 Subject: [PATCH 3/7] fix(exports): a source opened afresh during an export keeps its own state A source error from the export's copy is marked on the live layer only when the live source is still the same open (`SourceLayer::same_opening`: the same handle or opening slot, path and table), so a layer reopened meanwhile (a replaced GeoPackage table) isn't given the old open's error. The export starts take `&Session` again. --- crates/engine/src/cmd/exports.rs | 31 +++++++++++++++---------------- crates/map/src/source.rs | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 5c64c869fb..3e4bffd8bb 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -1,8 +1,8 @@ //! Exports that write a file from what the session shares (a layer's features, a raster's -//! pixels, a copy of the map or project to draw). A script or the command line writes it where the command is called; the app writes it -//! on a thread of its own with progress and Cancel (#1291 step 3): [`export_start`] reads the -//! session, [`ExportRun::run`] writes the file, and [`export_land`] says what was written, as the -//! command would have. It lands whatever layer is active or project is open by then: the file is +//! pixels, a copy of the map or project to draw). A script or the command line writes it where +//! the command is called; the app writes it on a thread of its own with progress and Cancel +//! (#1291 step 3): [`export_start`] reads the session, [`ExportRun::run`] writes the file, and +//! [`export_land`] says what was written, as the command would have. It lands whatever layer is active or project is open by then: the file is //! written either way, and an export changes nothing in the project. use serde_json::Value; @@ -27,15 +27,15 @@ pub(crate) type Then = Box Result<(), St pub(crate) type Work = Box Result + Send>; /// What an export reads of the session, and the work that writes its file. -type Start = fn(&mut Session, &Value) -> Result; +type Start = fn(&Session, &Value) -> Result; /// The exports the app writes on another thread ([`export_start`]), each with what it is called /// (in its progress, and a cancelled run's error) and what it reads of the session. const IN_BACKGROUND: &[(&str, &str, Start)] = &[ - ("share.exportLayer", "Export Features", |s, p| super::share::layer_work(s, p)), - ("share.exportRaster", "Export Raster", |s, p| super::raster::export_work(s, p)), - ("share.exportMap", "Export Map", |s, p| super::share::map_work(s, p)), - ("layout.export", "Export Layout", |s, p| super::layout::export_work(s, p)), + ("share.exportLayer", "Export Features", super::share::layer_work), + ("share.exportRaster", "Export Raster", super::raster::export_work), + ("share.exportMap", "Export Map", super::share::map_work), + ("layout.export", "Export Layout", super::layout::export_work), ]; /// `command` and what it is called, when the app writes its file on another thread (see @@ -82,7 +82,7 @@ fn said(s: &mut Session, wrote: Wrote) -> CmdResult { /// to `path`, or why not. A cancelled one says only that it was cancelled. pub(crate) fn drawn( maps: &[rustgis_map::Map], - said: Vec<(rustgis_data::diagnostics::Severity, String)>, + notices: Vec<(rustgis_data::diagnostics::Severity, String)>, written: Result, path: String, ) -> Result { @@ -90,31 +90,30 @@ pub(crate) fn drawn( return Err(e.clone()); } // Each by its live layer, with the source it failed on: one the user removed, or opened - // afresh meanwhile, is left as it is. + // afresh meanwhile (`SourceLayer::same_opening`), is left as it is. let failed: Vec<_> = maps .iter() .flat_map(|m| &m.layers) .filter_map(|l| { let src = l.source.as_ref()?; - Some((l.instance_id(), src.error.clone()?, src.path.clone(), src.table.clone())) + Some((l.instance_id(), src.error.clone()?, src.clone())) }) .collect(); let answer = written.as_ref().map_or(Value::Null, Value::clone); let then: Then = Box::new(move |s: &mut Session, _: &mut Value| { use rustgis_data::diagnostics::Severity; - for (severity, text) in said { + for (severity, text) in notices { match severity { Severity::Error => s.notify(text, true), Severity::Warning => s.warn(text), Severity::Info => s.notify(text, false), } } - for (instance, error, path, table) in failed { + for (instance, error, copied) in failed { let live = s.project.maps.iter_mut().flat_map(|m| m.layers.iter_mut()).find(|l| l.instance_id() == instance); if let Some(src) = live.and_then(|l| l.source.as_mut()) && src.error.is_none() - && src.path == path - && src.table == table + && src.same_opening(&copied) { src.error = Some(error); } diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 1b6d1880af..7740b86122 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -923,6 +923,24 @@ impl SourceLayer { self.pending = None; } + /// Whether `other` (a copy of this one) reads the same source the same way: the same file and + /// table, opened by the same open. One opened afresh since (`reopen`, a table replaced) is + /// another, whatever its path. + pub fn same_opening(&self, other: &SourceLayer) -> bool { + let handles = match (&self.handle, &other.handle) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + (None, None) => true, + _ => false, + }; + // A source by its web address still opening: the same open shares its slot. + let openings = match (&self.opening, &other.opening) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + (None, None) => true, + _ => false, + }; + handles && openings && self.path == other.path && self.table == other.table + } + /// Forgets the window, so the next view reads again (after the source changes). pub fn reset(&mut self) { self.render_lod = Default::default(); From d159e506c6e2c8ca543e7ca6e8124bb570a59b0b Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 09:13:18 +0100 Subject: [PATCH 4/7] fix(exports): a background export goes in the session's log when it lands The app's exports on a thread of their own bypass `Session::run`, so they were missing from the session's log (Export Features since #1433). `ExportRun::started` keeps the command, its parameters and when it started, and `export_land` writes the entry `Session::run` would have, with the parameters kept as the log keeps them, failed or not. --- crates/engine/src/cmd/exports.rs | 37 +++++++++++++++++++++++++------- crates/engine/src/tests.rs | 27 +++++++++++++++++------ crates/ui-egui/src/exportjob.rs | 19 ++++++---------- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 3e4bffd8bb..734df67b0c 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -131,15 +131,30 @@ pub(crate) fn drawn( /// shared, and a streamed layer's source is read on the export's thread), and [`export_land`] /// says what it wrote. pub struct ExportRun { - /// The command it runs, which [`export_land`] says what it wrote as. - pub command: &'static str, /// What the export is called, for its progress and a cancelled run's error. pub name: &'static str, + started: Started, work: Work, } +#[cfg(not(target_arch = "wasm32"))] +/// The command an export started on another thread runs, with what and when: [`export_land`] +/// says what it wrote as that command, and puts it in the session's log as `Session::run` does. +#[derive(Clone)] +pub struct Started { + pub(crate) command: &'static str, + pub(crate) params: Value, + pub(crate) at: u64, + pub(crate) start: web_time::Instant, +} + #[cfg(not(target_arch = "wasm32"))] impl ExportRun { + /// What [`export_land`] needs of it once it has run. + pub fn started(&self) -> Started { + self.started.clone() + } + /// Writes the file, blocking until it's written; `control` stops it and counts its progress /// (see [`rustgis_analysis::control`]). A cancelled export leaves no file, or the one there /// before. @@ -157,17 +172,23 @@ impl ExportRun { pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result { let &(command, name, start) = IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?; - Ok(ExportRun { command, name, work: start(s, p)? }) + let started = Started { command, params: p.clone(), at: crate::project_log::now_ms(), start: web_time::Instant::now() }; + Ok(ExportRun { name, started, work: start(s, p)? }) } #[cfg(not(target_arch = "wasm32"))] -/// Says what [`ExportRun::run`] wrote, as its `command` ([`ExportRun::command`]) would have, and -/// gives its answer (or its failure): the troubleshooting history files what it says under that -/// command, as when it runs here. -pub fn export_land(s: &mut Session, command: &'static str, wrote: Wrote) -> CmdResult { +/// Says what [`ExportRun::run`] wrote (or why it failed), as its command would have, and gives +/// its answer: the troubleshooting history files what it says under that command, and the +/// session's log has its entry, as when it runs here. +pub fn export_land(s: &mut Session, started: Started, done: Result) -> CmdResult { + let Started { command, params, at, start } = started; let before = s.diagnostic_source.replace(command); - let landed = said(s, wrote); + let landed = done.and_then(|wrote| said(s, wrote)); s.diagnostic_source = before; + if s.log.on() && crate::project_log::is_logged(s, command, ¶ms) { + let entry = crate::project_log::entry(command, crate::project_log::kept_params(¶ms), &landed, at, start.elapsed()); + s.log.push(entry); + } landed } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 657fecfcff..1d5a0a074f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -28040,6 +28040,11 @@ fn raster_tools_run_on_another_thread() { #[test] fn exports_run_on_another_thread() { use crate::cmd::exports::{export_land, export_start, runs_in_background}; + // A path as it appears in JSON, as `PATH`: on Windows its backslashes are escaped there. + let unpathed_json = |v: &Value, path: &str| { + let escaped = json!(path).to_string(); + v.to_string().replace(escaped.trim_matches('"'), "PATH") + }; use rustgis_analysis::control::Control; let mut s = world(); s.run("layer.addDataBytes", &json!({"fileName": "dem.tif", "data": dem_b64()})).unwrap(); @@ -28072,11 +28077,17 @@ fn exports_run_on_another_thread() { let mark = s.notice_mark(); let answer_here = s.run(command, &with(&here)).unwrap(); let said_here = said(&s, mark, &here); + let logged = |s: &Session| s.log.entries().last().map(|e| (e.command.clone(), e.params.clone(), e.ok)); + let logged_here = logged(&s); + assert!(logged_here.as_ref().is_some_and(|(c, ..)| c == command), "{command}: logged where it's called"); let run = export_start(&mut s, command, &with(&there)).unwrap(); - let mark = s.notice_mark(); - let wrote = std::thread::spawn(move || run.run(&Control::new())).join().unwrap().unwrap(); + let (mark, started) = (s.notice_mark(), run.started()); + let wrote = std::thread::spawn(move || run.run(&Control::new())).join().unwrap(); assert!(s.notices_since(mark).is_empty(), "{command}: nothing is said before it lands"); - let answer_there = export_land(&mut s, command, wrote).unwrap(); + let answer_there = export_land(&mut s, started, wrote).unwrap(); + // The session's log has it as it has the export run here. + let there_params = logged(&s).map(|(c, p, ok)| (c, unpathed_json(&p, &there), ok)); + assert_eq!(there_params, logged_here.map(|(c, p, ok)| (c, unpathed_json(&p, &here), ok)), "{command} {file}"); let filed = s.diagnostics.records().last().map(|r| r.source.clone()); assert_eq!(filed.as_deref(), Some(command), "the history files what it said under the command"); assert_eq!(said(&s, mark, &there), said_here, "{command} {file}"); @@ -28124,8 +28135,9 @@ fn exports_run_on_another_thread() { let nowhere = dir.join("no").join("such.png").display().to_string(); let here = s.run("layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap_err(); let run = export_start(&mut s, "layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap(); - let wrote = run.run(&Control::new()).unwrap(); - assert_eq!(export_land(&mut s, "layout.export", wrote).unwrap_err(), here); + let started = run.started(); + assert_eq!(export_land(&mut s, started, run.run(&Control::new())).unwrap_err(), here); + assert!(s.log.entries().last().is_some_and(|e| e.command == "layout.export" && !e.ok), "a failed one is logged as failed"); // A streamed layer's source that failed on the copy is marked on its live layer, unless that // was opened from somewhere else meanwhile. let li = s.map().layers.iter().position(|l| l.name == "Countries").unwrap(); @@ -28137,8 +28149,9 @@ fn exports_run_on_another_thread() { failed.error = Some("gone.fgb: not found".into()); copy.layers[li].source = Some(failed); let land = |s: &mut Session, copy: &rustgis_map::Map| { - let wrote = crate::cmd::exports::drawn(std::slice::from_ref(copy), Vec::new(), Ok(json!({})), at("drawn.png")).unwrap(); - export_land(s, "share.exportMap", wrote).unwrap(); + let wrote = crate::cmd::exports::drawn(std::slice::from_ref(copy), Vec::new(), Ok(json!({})), at("drawn.png")); + let started = export_start(s, "share.exportMap", &json!({"path": at("drawn.png")})).unwrap().started(); + export_land(s, started, wrote).unwrap(); s.map().layers[li].source.as_ref().and_then(|src| src.error.clone()) }; assert_eq!(land(&mut s, ©).as_deref(), Some("gone.fgb: not found")); diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index f532cafd6b..5090029f24 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -15,7 +15,7 @@ use crate::theme::Tokens; /// An export under way: what it is called, its Cancel and progress, and what it wrote once done. pub struct ExportJob { - command: &'static str, + started: exports::Started, name: &'static str, control: Arc, slot: crate::Slot>, @@ -34,7 +34,7 @@ pub fn idle(app: &RustGisApp) -> Result<(), String> { pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result { idle(app)?; let run = exports::export_start(&mut app.session, command, &p)?; - let (command, name) = (run.command, run.name); + let (started, name) = (run.started(), run.name); // Cancel stops it. let control = Control::stoppable(); let (ctl, slot) = (control.clone(), crate::Slot::default()); @@ -50,7 +50,7 @@ pub fn start(app: &mut RustGisApp, command: &'static str, p: Value) -> Result bool { let Some(job) = &app.export_job else { return false }; let done = job.slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take(); let Some(done) = done else { return true }; - let command = job.command; + let started = job.started.clone(); app.export_job = None; - match done { - // Its notices are the command's; the answer was for a script, which waits for its own. - Ok(wrote) => { - if let Err(e) = exports::export_land(&mut app.session, command, wrote) { - app.session.notify(e, true); - } - } - Err(e) => app.session.notify(e, true), + // Its notices are the command's; the answer was for a script, which waits for its own. + if let Err(e) = exports::export_land(&mut app.session, started, done) { + app.session.notify(e, true); } false } From 7a8847e6adbfa6a06503a12800933343905fafa2 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 09:36:53 +0100 Subject: [PATCH 5/7] fix(exports): a landed export's failure is filed under its command, and a layer left out is said - `export_land` says a failure itself, once and under the export's command, as `Session::run` does; the app no longer says it again under Application. - A source that failed on the export's copy, where its live layer is gone or was opened afresh, is said to be missing from the file instead of left unsaid. - The module doc's first paragraph is rewrapped. --- crates/engine/src/cmd/exports.rs | 34 +++++++++++++++++++++----------- crates/engine/src/tests.rs | 8 ++++++++ crates/ui-egui/src/exportjob.rs | 7 +++---- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 734df67b0c..6a59b621d4 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -2,8 +2,9 @@ //! pixels, a copy of the map or project to draw). A script or the command line writes it where //! the command is called; the app writes it on a thread of its own with progress and Cancel //! (#1291 step 3): [`export_start`] reads the session, [`ExportRun::run`] writes the file, and -//! [`export_land`] says what was written, as the command would have. It lands whatever layer is active or project is open by then: the file is -//! written either way, and an export changes nothing in the project. +//! [`export_land`] says what was written, as the command would have. It lands whatever layer is +//! active or project is open by then: the file is written either way, and an export changes +//! nothing in the project. use serde_json::Value; @@ -78,7 +79,8 @@ fn said(s: &mut Session, wrote: Wrote) -> CmdResult { /// Lands an export drawn on a copy of the map or project (Export Map, Export Layout): what /// drawing it said, with each notice's severity; the streamed layers whose source failed on the -/// copy, marked on their live layers as drawing them there would have; then that it was written +/// copy, marked on their live layers as drawing them there would have (or, where that layer is +/// gone or opened afresh, said to be missing from the file); then that it was written /// to `path`, or why not. A cancelled one says only that it was cancelled. pub(crate) fn drawn( maps: &[rustgis_map::Map], @@ -96,7 +98,7 @@ pub(crate) fn drawn( .flat_map(|m| &m.layers) .filter_map(|l| { let src = l.source.as_ref()?; - Some((l.instance_id(), src.error.clone()?, src.clone())) + Some((l.instance_id(), l.name.clone(), src.error.clone()?, src.clone())) }) .collect(); let answer = written.as_ref().map_or(Value::Null, Value::clone); @@ -109,13 +111,13 @@ pub(crate) fn drawn( Severity::Info => s.notify(text, false), } } - for (instance, error, copied) in failed { + for (instance, name, error, copied) in failed { let live = s.project.maps.iter_mut().flat_map(|m| m.layers.iter_mut()).find(|l| l.instance_id() == instance); - if let Some(src) = live.and_then(|l| l.source.as_mut()) - && src.error.is_none() - && src.same_opening(&copied) - { - src.error = Some(error); + match live.and_then(|l| l.source.as_mut()) { + // Its failure already shows on the layer. + Some(src) if src.error.is_some() => {} + Some(src) if src.same_opening(&copied) => src.error = Some(error), + _ => s.warn(format!("{path}: {name} isn't in the file, as its source couldn't be read: {error}")), } } written?; @@ -178,12 +180,20 @@ pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result) -> CmdResult { let Started { command, params, at, start } = started; let before = s.diagnostic_source.replace(command); + let mark = s.notice_mark(); let landed = done.and_then(|wrote| said(s, wrote)); + // Its failure said once, under the command, as `Session::run` says one. + if let Err(e) = &landed { + let text = Session::redacted(e); + if !s.notices_since(mark).iter().any(|n| n.error && n.text == text.as_str()) { + s.notify_redacted(text, true); + } + } s.diagnostic_source = before; if s.log.on() && crate::project_log::is_logged(s, command, ¶ms) { let entry = crate::project_log::entry(command, crate::project_log::kept_params(¶ms), &landed, at, start.elapsed()); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 1d5a0a074f..32bd67ff72 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -28136,7 +28136,12 @@ fn exports_run_on_another_thread() { let here = s.run("layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap_err(); let run = export_start(&mut s, "layout.export", &json!({"path": nowhere, "dpi": 48})).unwrap(); let started = run.started(); + let mark = s.notice_mark(); assert_eq!(export_land(&mut s, started, run.run(&Control::new())).unwrap_err(), here); + let errors: Vec<_> = s.notices_since(mark).iter().filter(|n| n.error).map(|n| n.text.clone()).collect(); + assert_eq!(errors, std::slice::from_ref(&here), "its failure is said once"); + let filed = s.diagnostics.records().last().map(|r| (r.source.clone(), r.severity)); + assert_eq!(filed, Some(("layout.export".to_string(), rustgis_data::diagnostics::Severity::Error)), "under the command"); assert!(s.log.entries().last().is_some_and(|e| e.command == "layout.export" && !e.ok), "a failed one is logged as failed"); // A streamed layer's source that failed on the copy is marked on its live layer, unless that // was opened from somewhere else meanwhile. @@ -28157,7 +28162,10 @@ fn exports_run_on_another_thread() { assert_eq!(land(&mut s, ©).as_deref(), Some("gone.fgb: not found")); s.map_mut().layers[li].source.as_mut().unwrap().error = None; s.map_mut().layers[li].source.as_mut().unwrap().path = Some("found.fgb".into()); + let mark = s.notice_mark(); assert_eq!(land(&mut s, ©), None, "a source opened afresh keeps its own state"); + let said = s.notices_since(mark).iter().map(|n| n.text.clone()).collect::>().join("\n"); + assert!(said.contains("Countries isn't in the file") && said.contains("gone.fgb: not found"), "and the file's gap is said: {said}"); s.map_mut().layers[li].source = None; let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/ui-egui/src/exportjob.rs b/crates/ui-egui/src/exportjob.rs index 5090029f24..7c6015eccb 100644 --- a/crates/ui-egui/src/exportjob.rs +++ b/crates/ui-egui/src/exportjob.rs @@ -62,10 +62,9 @@ pub fn land(app: &mut RustGisApp) -> bool { let Some(done) = done else { return true }; let started = job.started.clone(); app.export_job = None; - // Its notices are the command's; the answer was for a script, which waits for its own. - if let Err(e) = exports::export_land(&mut app.session, started, done) { - app.session.notify(e, true); - } + // Its notices (a failure among them) are the command's; the answer was for a script, which + // waits for its own. + let _ = exports::export_land(&mut app.session, started, done); false } From db51acdb1097562ac0f87502fa751c0efc40eda9 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 10:04:59 +0100 Subject: [PATCH 6/7] fix(exports): one notice for the layers a drawn export left out, and one copy of the project - The layers whose source failed on the copy and that can't be marked on their live layer are said in one notice with a count, naming the first. The opening is checked first, so a layer reopened and failing again meanwhile is still said. - Export Layout drops the copied maps' reads under way in place (`Map::detach_reads`), so the project is copied once. --- crates/engine/src/cmd/exports.rs | 23 +++++++++++++++++------ crates/engine/src/cmd/layout.rs | 2 +- crates/engine/src/tests.rs | 16 ++++++++++++++++ crates/map/src/lib.rs | 10 ++++++++-- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/exports.rs b/crates/engine/src/cmd/exports.rs index 6a59b621d4..52f3e8365e 100644 --- a/crates/engine/src/cmd/exports.rs +++ b/crates/engine/src/cmd/exports.rs @@ -80,8 +80,8 @@ fn said(s: &mut Session, wrote: Wrote) -> CmdResult { /// Lands an export drawn on a copy of the map or project (Export Map, Export Layout): what /// drawing it said, with each notice's severity; the streamed layers whose source failed on the /// copy, marked on their live layers as drawing them there would have (or, where that layer is -/// gone or opened afresh, said to be missing from the file); then that it was written -/// to `path`, or why not. A cancelled one says only that it was cancelled. +/// gone or opened afresh, said in one notice to be missing from the file); then that it was +/// written to `path`, or why not. A cancelled one says only that it was cancelled. pub(crate) fn drawn( maps: &[rustgis_map::Map], notices: Vec<(rustgis_data::diagnostics::Severity, String)>, @@ -111,15 +111,26 @@ pub(crate) fn drawn( Severity::Info => s.notify(text, false), } } + let mut missing = Vec::new(); for (instance, name, error, copied) in failed { let live = s.project.maps.iter_mut().flat_map(|m| m.layers.iter_mut()).find(|l| l.instance_id() == instance); match live.and_then(|l| l.source.as_mut()) { - // Its failure already shows on the layer. - Some(src) if src.error.is_some() => {} - Some(src) if src.same_opening(&copied) => src.error = Some(error), - _ => s.warn(format!("{path}: {name} isn't in the file, as its source couldn't be read: {error}")), + // The same open: its failure shows on the layer (it may already). + Some(src) if src.same_opening(&copied) => { + src.error.get_or_insert(error); + } + _ => missing.push((name, error)), } } + // One notice for them all, with a count, naming the first. + if let Some((name, error)) = missing.first() { + let n = missing.len(); + s.warn(if n == 1 { + format!("{path}: {name} isn't in the file, as its source couldn't be read: {error}") + } else { + format!("{path}: {n} layers aren't in the file, as their sources couldn't be read; the first, {name}: {error}") + }); + } written?; s.notify(format!("Exported {path}"), false); Ok(()) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 7837b84fb0..4393cfb650 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3491,7 +3491,7 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result>().join("\n"); assert!(said.contains("Countries isn't in the file") && said.contains("gone.fgb: not found"), "and the file's gap is said: {said}"); + // Opened afresh and failing again: still said, as the file lacks the layer either way. + s.map_mut().layers[li].source.as_mut().unwrap().error = Some("found.fgb: not found".into()); + let mark = s.notice_mark(); + land(&mut s, ©); + assert!(s.notices_since(mark).iter().any(|n| n.text.contains("Countries isn't in the file"))); + // Several left out: one notice, with a count. + let mut two = copy.clone(); + let other = two.layers.iter().position(|l| l.name != "Countries").unwrap(); + let mut gone = rustgis_map::source::SourceLayer::default(); + gone.path = Some("gone.fgb".into()); + gone.error = Some("gone.fgb: not found".into()); + two.layers[other].source = Some(gone); + let mark = s.notice_mark(); + land(&mut s, &two); + let missing: Vec<_> = s.notices_since(mark).iter().filter(|n| n.text.contains("in the file")).map(|n| n.text.clone()).collect(); + assert!(missing.len() == 1 && missing[0].contains("2 layers aren't in the file"), "{missing:?}"); s.map_mut().layers[li].source = None; let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index c841b54059..af9910a273 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1410,10 +1410,16 @@ impl Map { /// identities, and its streamed layers read for themselves (see [`source::SourceLayer::detach_read`]). pub fn detached(&self) -> Map { let mut copy = self.clone(); - for source in copy.layers.iter_mut().filter_map(|l| l.source.as_mut()) { + copy.detach_reads(); + copy + } + + /// [`Map::detached`] for a map that is already a copy: its streamed layers leave a read under + /// way to the map it was copied from. + pub fn detach_reads(&mut self) { + for source in self.layers.iter_mut().filter_map(|l| l.source.as_mut()) { source.detach_read(); } - copy } /// Identifies this live map, whatever its name or place among the project's maps. From af25ae1f811072f7d3248ba9f1aae9cbf345437d Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 5 Nov 2026 11:11:35 +0100 Subject: [PATCH 7/7] fix(exports): count a layout export's notices past what its copy keeps, and give the copy its own thinning - Past the 200 notices a session keeps, the copy's earlier notices are said as one count. - `detach_read` gives the copy its own thinning decisions, so the map's redraws never wait on the export's. --- crates/engine/src/cmd/layout.rs | 13 +++++++++++-- crates/engine/src/lib.rs | 5 +++++ crates/map/src/source.rs | 4 +++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 4393cfb650..024758cd60 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3502,8 +3502,17 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result = Vec::new(); + if earlier > 0 { + said.push(( + rustgis_data::diagnostics::Severity::Warning, + format!("{path}: {earlier} earlier notices from drawing it aren't shown"), + )); + } + said.extend(kept.into_iter().map(|n| (n.severity, n.text))); super::exports::drawn(©.project.maps, said, written, path) })) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 376e9042f8..688bd9fd02 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1659,6 +1659,11 @@ impl Session { } } + /// How many notices were given in this session, including ones no longer kept. + pub(crate) fn notices_given(&self) -> u64 { + self.notices_given + } + /// A mark to pass to [`Session::notices_since`]. pub fn notice_mark(&self) -> u64 { self.notices_given diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 7740b86122..3882c81319 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -918,9 +918,11 @@ impl SourceLayer { /// Leaves a read under way to the layer this one was copied from, which takes it when it /// lands: a copy drawn elsewhere (an export on a thread of its own) reads what it needs for /// itself. The two would otherwise share the read, and the one that didn't take it would wait - /// for it forever. + /// for it forever. The copy decides its thinning by itself too, so the map's redraws never + /// wait for the export's. pub fn detach_read(&mut self) { self.pending = None; + self.render_lod = Default::default(); } /// Whether `other` (a copy of this one) reads the same source the same way: the same file and