Repository navigation
feat(layout): preview the sheet on the Layout Wizard's review step #3480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jgregoriorincon
merged 10 commits into
opengeos:main
from
jgregoriorincon:feat/3475-wizard-preview
Dec 1, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c30e3cb
feat(layout): preview the sheet on the Layout Wizard's review step
jgregoriorincon ea46fb6
fix(layout): the preview says Create's notices; its copy builds for t…
jgregoriorincon f321e74
fix(layout): the wizard's Preview button comes before the review's rows
jgregoriorincon 7c492ff
docs(layout): say why the wizard's preview draws on the UI thread; pi…
jgregoriorincon a8bbf8a
fix(layout): draw the wizard's preview off the window's thread, with …
jgregoriorincon 370170c
fix(layout): the wizard's preview waits for a cancelled drawing to end
jgregoriorincon 64f4443
fix(layout): catch a bug in the preview's drawing, stop it with the p…
jgregoriorincon 6392d56
fix(layout): the preview says what the page drawing leaves out, and a…
jgregoriorincon 1adf0c4
fix(layout): a preview refused a thread says so in the interface's la…
jgregoriorincon 4c3667f
fix(layout): a choice changed on another step stops the preview's dra…
jgregoriorincon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| //! The sheet `layout.publication` would make, drawn without touching the project (#3475). | ||
|
|
||
| use rustgis_render::layout::PagePending; | ||
| use serde_json::Value; | ||
|
|
||
| use crate::Session; | ||
| use crate::undo_memory::ReadRows; | ||
|
|
||
| /// The resolution a preview is drawn at. | ||
| pub const PREVIEW_DPI: f64 = 36.0; | ||
|
|
||
| /// A publication sheet as it would be made. | ||
| pub struct PublicationPreview { | ||
| pub pixmap: tiny_skia::Pixmap, | ||
| /// What was still on its way when it was drawn. | ||
| pub pending: PagePending, | ||
| /// What `layout.publication` warns of and says, and what an export of the page would say it | ||
| /// leaves out. | ||
| pub warnings: Vec<String>, | ||
| } | ||
|
|
||
| /// Makes a copy of `s` to run `layout.publication` on, there or on another thread: what the | ||
| /// command reads of the session, with the logins already open (it opens none). Its streamed | ||
| /// layers are read anew ([`rustgis_map::Map::detach_reads`]). | ||
| fn copy_of(s: &Session) -> impl FnOnce() -> (ReadRows, Session) + Send + 'static { | ||
| let mut project = s.project.clone(); | ||
| for m in &mut project.maps { | ||
| m.detach_reads(); | ||
| } | ||
| let (extensions, connections, web_logins) = (s.extensions().clone(), s.connections.clone(), s.web_logins.borrowing_open()); | ||
| let selections: Vec<(String, String)> = | ||
| s.database_logins.web_selections().into_iter().map(|(origin, id)| (origin.to_string(), id.to_string())).collect(); | ||
| let requests = s.diagnostics.capture(); | ||
| let (view_size, active_layer, opened) = (s.view_size, s.active_layer, s.opened); | ||
| let (in_place_wait, raster_defaults, grids, auto_cache) = | ||
| (s.in_place_wait, s.raster_defaults.clone(), s.offers_grid_downloads, s.auto_cache); | ||
| let (favourite, arrangements) = (s.layout_favourite.clone(), s.layout_arrangements.clone()); | ||
| let readers = s.undo_memory.readers(); | ||
| // Rows the copy shares stay counted while it holds them, should this session let them go. | ||
| let retained = readers.retain(crate::undo_memory::allocations(&project)); | ||
| move || { | ||
| let mut copy = Session::new(project); | ||
| copy.set_extensions(extensions); | ||
| copy.connections = connections; | ||
| copy.web_logins = web_logins; | ||
| for (origin, id) in &selections { | ||
| copy.database_logins.select(crate::connections::WEB, origin, id); | ||
| } | ||
| copy.requests_into = Some(requests); | ||
| (copy.view_size, copy.active_layer, copy.opened) = (view_size, active_layer, opened); | ||
| (copy.in_place_wait, copy.raster_defaults, copy.offers_grid_downloads, copy.auto_cache) = | ||
| (in_place_wait, raster_defaults, grids, auto_cache); | ||
| (copy.layout_favourite, copy.layout_arrangements) = (favourite, arrangements); | ||
| // The copy shares this session's rows, which this session already counts (#2431). | ||
| copy.undo_memory.memory = None; | ||
| copy.undo_memory.use_readers(readers); | ||
| copy.sync_policy_generation(); | ||
| (retained, copy) | ||
| } | ||
| } | ||
|
|
||
| /// [`copy_of`], made here. | ||
| #[cfg(test)] | ||
| pub(crate) fn preview_copy(s: &Session) -> Session { | ||
| copy_of(s)().1 | ||
| } | ||
|
|
||
| /// What draws the sheet `layout.publication` with `params` would make, at [`PREVIEW_DPI`] without | ||
| /// waiting for what is still on its way, here or on a thread of its own; `s` doesn't change. Its | ||
| /// refusal is the error, and so is a bug caught in the drawing, with its incident (natively: the | ||
| /// browser stops at a panic). The command reads streamed layers as it does when run on `s`, and | ||
| /// what the whole drawing asks of servers goes in `s`'s Diagnostics. | ||
| pub fn publication_preview_work(s: &Session, params: Value) -> impl FnOnce() -> Result<PublicationPreview, String> + Send + 'static { | ||
| let (copy, requests) = (copy_of(s), s.diagnostics.capture()); | ||
| move || { | ||
| let _requests = requests.enter(); | ||
| std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || draw(copy, ¶ms))) | ||
| .unwrap_or_else(|payload| Err(rustgis_data::incident::bug("The preview", payload.as_ref()))) | ||
| } | ||
| } | ||
|
|
||
| /// Runs `layout.publication` with `params` on the copy `copy` makes and draws the sheet. | ||
| fn draw(copy: impl FnOnce() -> (ReadRows, Session), params: &Value) -> Result<PublicationPreview, String> { | ||
| // Bound first, so it is dropped after the copy. | ||
| let (_retained, mut copy) = copy(); | ||
| let made = copy.run("layout.publication", params)?; | ||
| let li = copy.project.layouts.len().checked_sub(1).ok_or("no layout")?; | ||
| // The page as an export draws it, but without waiting, and what it leaves out said alike. | ||
| let mut pending = PagePending::default(); | ||
| let pixmap = super::export::layout_page(&mut copy, li, PREVIEW_DPI, None, |layout, maps, info| { | ||
| let (pixmap, short) = rustgis_render::layout::render_preview(layout, maps, PREVIEW_DPI, info)?; | ||
| pending = short; | ||
| Ok(pixmap) | ||
| })?; | ||
| // What Create answers, then what it and the drawing say as notices. | ||
| let mut warnings: Vec<String> = | ||
| made["warnings"].as_array().into_iter().flatten().filter_map(|w| w.as_str().map(String::from)).collect(); | ||
| warnings.extend(copy.notices.iter().map(|n| n.text.clone())); | ||
| Ok(PublicationPreview { pixmap, pending, warnings }) | ||
|
jgregoriorincon marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /// [`publication_preview_work`], done here. | ||
| pub fn publication_preview(s: &Session, params: &Value) -> Result<PublicationPreview, String> { | ||
| publication_preview_work(s, params.clone())() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| //! `publication_preview` (#3475): the sheet `layout.publication` would make, the project left | ||
| //! as it was. | ||
|
|
||
| use super::*; | ||
| use crate::cmd::layout::{PREVIEW_DPI, PublicationPreview, layout_png, preview_copy, publication_preview}; | ||
|
|
||
| fn preview(s: &Session, params: Value) -> PublicationPreview { | ||
| publication_preview(s, ¶ms).unwrap_or_else(|e| panic!("{params}: {e}")) | ||
| } | ||
|
|
||
| /// The preview of `params` over the session `set` leaves, against the layout Create then makes, | ||
| /// drawn as an export draws it. | ||
| fn is_what_create_draws(set: impl Fn(&mut Session), params: Value) { | ||
| let mut s = world(); | ||
| set(&mut s); | ||
| let drawn = preview(&s, params.clone()).pixmap; | ||
| s.run("layout.publication", ¶ms).unwrap(); | ||
| let made = s.project.layouts.len() - 1; | ||
| let png = layout_png(&mut s, made, PREVIEW_DPI, None).unwrap(); | ||
| let want = tiny_skia::Pixmap::decode_png(&png).unwrap(); | ||
| assert_eq!((drawn.width(), drawn.height()), (want.width(), want.height()), "{params}"); | ||
| assert!(drawn.data() == want.data(), "{params}: the preview differs from the sheet made"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_preview_draws_the_page_and_leaves_the_project_alone() { | ||
| let mut s = world(); | ||
| s.mark_saved(); | ||
| let (project, undo) = (serde_json::to_value(&s.project).unwrap(), s.undo_label()); | ||
| let p = preview(&s, json!({"profile": "a4-portrait"})).pixmap; | ||
| let ratio = f64::from(p.height()) / f64::from(p.width()); | ||
| assert!((ratio - 297.0 / 210.0).abs() < 0.01, "{} x {}", p.width(), p.height()); | ||
| assert!((f64::from(p.width()) - 210.0 / 25.4 * PREVIEW_DPI).abs() <= 1.0, "{}", p.width()); | ||
| assert_eq!(serde_json::to_value(&s.project).unwrap(), project); | ||
| assert_eq!((s.undo_label(), s.dirty()), (undo, false)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_preview_is_the_sheet_create_draws() { | ||
| is_what_create_draws(|_| {}, json!({"profile": "a4-portrait"})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_preview_takes_the_saved_favourite() { | ||
| is_what_create_draws( | ||
| |s| { | ||
| s.run("layout.arrangement", &json!({"favourite": "mirrored"})).unwrap(); | ||
| }, | ||
| json!({"profile": "a4-portrait"}), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_preview_of_the_view_takes_the_views_size() { | ||
| is_what_create_draws(|s| s.view_size = (400.0, 900.0), json!({"profile": "a4-portrait"})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_figure_preview_has_the_figures_proportions() { | ||
| let p = preview(&world(), json!({"profile": "thesis-half"})).pixmap; | ||
| let ratio = f64::from(p.height()) / f64::from(p.width()); | ||
| assert!((ratio - 110.0 / 160.0).abs() < 0.01, "{} x {}", p.width(), p.height()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_refused_sheet_says_what_create_says() { | ||
| let mut s = world(); | ||
| let params = json!({"profile": "a4-portrait", "style": "unknown"}); | ||
| let said = publication_preview(&s, ¶ms).err().expect("refused"); | ||
| assert_eq!(said, s.run("layout.publication", ¶ms).unwrap_err()); | ||
| assert!(s.project.layouts.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_preview_warns_of_what_create_warns_of() { | ||
| let mut s = world(); | ||
| s.run("map.projection", &json!({"type": "globe"})).unwrap(); | ||
| let params = json!({"profile": "a4-portrait", "scaleBar": {"units": "miles"}}); | ||
| let said = preview(&s, params.clone()).warnings; | ||
| let r = s.run("layout.publication", ¶ms).unwrap(); | ||
| assert!(!said.is_empty()); | ||
| assert_eq!(json!(said), r["warnings"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn the_copy_previewed_on_keeps_the_sessions_settings() { | ||
| let mut s = world(); | ||
| s.in_place_wait = false; | ||
| s.view_size = (400.0, 900.0); | ||
| s.opened = 7; | ||
| s.active_layer = s.map().layers.first().map(|l| l.id); | ||
| s.layout_favourite = json!("mirrored"); | ||
| s.layout_arrangements.insert("mine".into(), json!({"legend": "top-left"})); | ||
| let copy = preview_copy(&s); | ||
| assert!(!copy.in_place_wait); | ||
| assert_eq!((copy.view_size, copy.opened, copy.active_layer), (s.view_size, s.opened, s.active_layer)); | ||
| assert_eq!(copy.layout_favourite, s.layout_favourite); | ||
| assert_eq!(copy.layout_arrangements, s.layout_arrangements); | ||
| assert_eq!(copy.project.maps.len(), s.project.maps.len()); | ||
| } | ||
|
|
||
| /// What the page drawing leaves out, as an export of the sheet says it, is said under the preview. | ||
| #[test] | ||
| fn a_preview_says_what_the_drawing_leaves_out() { | ||
| let mut s = world(); | ||
| let params = json!({"profile": "a4-portrait", "legend": {"title": "W".repeat(150)}}); | ||
| let warned = preview(&s, params.clone()).warnings; | ||
| s.run("layout.publication", ¶ms).unwrap(); | ||
| let seen = s.notices_since(0).len(); | ||
| layout_png(&mut s, 0, PREVIEW_DPI, None).unwrap(); | ||
| let said = s.notices_since(0)[seen..].to_vec(); | ||
| assert!(!said.is_empty(), "the export says the legend's title is cut"); | ||
| for n in said { | ||
| assert!(warned.contains(&n.text), "{}: {warned:?}", n.text); | ||
| } | ||
| } | ||
|
|
||
| /// A tile that never comes doesn't hold the preview, which says its frames are short of it. | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| #[test] | ||
| fn a_preview_does_not_wait_for_tiles() { | ||
| use rustgis_map::tiles::{TileKind, TileLayer}; | ||
| if isolate_process_test("tests::layout_preview::a_preview_does_not_wait_for_tiles") { | ||
| return; | ||
| } | ||
| isolate_plugins(); | ||
| static HELD: std::sync::Mutex<Vec<Box<dyn std::any::Any + Send>>> = std::sync::Mutex::new(Vec::new()); | ||
| crate::render::rustgis_tiles::cache::set_fetcher(|url, done| { | ||
| // As a request's failure is noted: under whoever the drawing's thread draws for. | ||
| rustgis_data::diagnostics::Capture::current().note_absence("GET", &url, 404); | ||
| HELD.lock().unwrap().push(Box::new(done)); | ||
| }); | ||
| let mut s = world(); | ||
| s.map_mut().tiles.push(TileLayer::new("Held", TileKind::Raster, "https://tiles.test/held/{z}/{x}/{y}.png")); | ||
| let start = std::time::Instant::now(); | ||
| let p = preview(&s, json!({"profile": "a4-portrait"})); | ||
| assert!(start.elapsed() < std::time::Duration::from_secs(20), "waited {:?}", start.elapsed()); | ||
| assert!(p.pending.frames); | ||
| assert!(!s.diagnostics.records().is_empty(), "the drawing's requests are the session's to show"); | ||
| } | ||
|
|
||
| /// What Create says as notices (the locator map's name cut short) is a warning of the preview too. | ||
| #[test] | ||
| fn a_preview_warns_of_what_create_says_as_notices() { | ||
| let mut s = world(); | ||
| s.run("map.rename", &json!({"name": "m".repeat(crate::cmd::MAX_NAME)})).unwrap(); | ||
| let params = json!({"profile": "a4-portrait"}); | ||
| let warned = preview(&s, params.clone()).warnings; | ||
| let seen = s.notices_since(0).len(); | ||
| s.run("layout.publication", ¶ms).unwrap(); | ||
| let said = s.notices_since(0)[seen..].to_vec(); | ||
| assert!(!said.is_empty(), "the cut is said"); | ||
| for n in said { | ||
| assert!(warned.contains(&n.text), "{}: {warned:?}", n.text); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.