From a4d98a780477d66f939e6d4863539c6044bb478c Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Thu, 12 Nov 2026 15:26:43 -0300 Subject: [PATCH 1/3] fix(layout): follow-ups to #1826 and #1829 (#1379) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A publication's scale bar box is measured again on a frame's new scale or a new text under its bar only while its width is still the automatic one from what it was measured from before; a width set by hand is kept. - layout.arrangement declares "arrangement" as a parameter of its own, so {"save": …, "arrangement": …} isn't said to ignore it. - Saving, replacing or removing an arrangement drops the usable unread entries under the same name but for case; unreadable ones stay. Part of #1379. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layout.rs | 78 ++++++++++++++++++++---- crates/engine/src/tests.rs | 25 +++++++- crates/ui-egui/src/lib.rs | 9 +-- crates/ui-egui/tests/layout_favourite.rs | 20 ++++++ 4 files changed, 116 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index d761b76408..9b3dc2d2dc 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -35,7 +35,7 @@ pub fn commands() -> Vec { r#"{"profile"?: "a4" (the default) | "a3" | "a2" (a sheet turned to portrait or landscape, whichever the area's map comes out larger on: its extent east to west against north to south, on the map's projection) | "a4-portrait"|"a4-landscape"|"a3-portrait"|"a3-landscape"|"a2-portrait"|"a2-landscape" (a sheet: frame, title, subtitle, the map, a boxed footer)|"figure-1col" (90 mm)|"figure-1.5col" (140 mm)|"figure-2col" (190 mm)|"thesis-full" (160 × 225 mm)|"thesis-half" (160 × 110 mm) (a figure: the map edge to edge, its title and sources left to the caption), "extent"?: [west, south, east, north] in degrees that must fit (default: the map view), "name"?: string, "title"?: text, "subtitle"?: text, "sources"?: text | [texts], "reference"?: text | [texts] (default: the projection, datum and graticule), "note"?: text, "author"?: text (default: "Made with RustGIS · {date}"; "" or [] for none), "headings"?: [the sources' heading, the reference's heading, an editorial column's heading], "locator"?: "globe" (a globe with the area outlined; the default on sheets) | "inset" (the surrounding region, the area outlined) | "none" (the default on figures), "decimal"?: "." (the default) | "," (graticule, scale bar and colorbar numbers and {scale} with a decimal comma: 0,05 and 1:6.300.000), "colorbar"?: "auto" (the default: the top visible raster with a colour scale, not drawn by classes, gets one, under the legend in its box; a raster drawn by classes has them in the legend's rows instead) | "none" (no colorbar, and the legend's box has no room for one), "veil"?: true (the default: when no shape of the legend fits, a map that runs on past the extent, with a long legend, has what is outside the extent veiled and its surrounds grouped there, at up to half the scale on a sheet) | false (never veiled, and drawn at its scale; the map may still move aside), "style"?: "classic" (the default: the title over the map, the footer under it) | "banner" (the title and subtitle in a band of the accent colour across the top) | "panel" (the map at the left, a panel at the right with the title, locator, legend, sources and reference) | "editorial" (the title large in serif, a column at the right with the note, the locator and the legend) | "modular" (cards on a light page: the map, the locator and the legend each in one, the sources and reference in tinted ones) | "poster" (the page in the accent colour, the title in light text, the map in a light card, the footer in darker blocks) (a sheet's arrangement; figures leave it to the caption), "accent"?: a colour as #RRGGBB (the style's colour: its band, panel tint, headings and rules, a poster's page), "arrangement"?: "best" (the default: the favourite, each arrangement by name and each saved with layout.arrangement laid out, the one moving the least area off its corners kept, then the one leaving the least over the data, the favourite on a tie) | "favourite" (layout.arrangement's) | an arrangement by name (built in or saved) or corner by corner, as layout.arrangement takes (the corners the locator, north arrow, legend and scale bar are at home in; one covering what the map draws there still moves), "coverage"?: "*.png" (writes the coverage grid the placement searched, as it settled on it, three pixels a cell: what the map draws dark, the free cells white, both tinted blue in the strips the graticule's labels keep, the surrounds placed over the map outlined in red; the answer gives its path)} → a new layout of the active map at a round scale, its texts wrapped to their boxes; {projection}, {datum} and {grid} in the texts are filled from the map"#, ), C::new("layout.arrangement", "Favourite Arrangement", "Publish › Layout", arrangement).roles(&[]).effects(crate::policy::Effects::NONE).view().params( - r#"{"favourite"?: "classic" (the locator top left, the north arrow top right, the legend bottom left, the scale bar bottom right) | "mirrored" | "legendRight" | "legendTop" | {"locator", "northArrow", "legend", "scaleBar": "top-left"|"top-right"|"bottom-left"|"bottom-right", each a corner of its own} | a saved arrangement's name, "save"?: a name (1 to 40 characters, not a built-in one nor "best", "favourite", "custom" or "veil"; one saved under it already, case aside, is replaced) to save "arrangement" under: a name, or a corner for each surround (at most 20 saved), "remove"?: a saved arrangement's name (a favourite named after it keeps its corners, unless "favourite" gives another)} → the favourite arrangement of publication layouts and those saved by name (kept with the app's preferences), the favourite's corners, the arrangements by name and the saved ones' corners; a removal, then a save, then the favourite; with none of them given, they are only shown"#, + r#"{"favourite"?: "classic" (the locator top left, the north arrow top right, the legend bottom left, the scale bar bottom right) | "mirrored" | "legendRight" | "legendTop" | {"locator", "northArrow", "legend", "scaleBar": "top-left"|"top-right"|"bottom-left"|"bottom-right", each a corner of its own} | a saved arrangement's name, "save"?: a name (1 to 40 characters, not a built-in one nor "best", "favourite", "custom" or "veil"; one saved under it already, case aside, is replaced) to save "arrangement" under (at most 20 saved), "arrangement"?: what "save" saves: an arrangement by name, or a corner for each surround as "favourite" takes them (refused without "save"), "remove"?: a saved arrangement's name (a favourite named after it keeps its corners, unless "favourite" gives another)} → the favourite arrangement of publication layouts and those saved by name (kept with the app's preferences), the favourite's corners, the arrangements by name and the saved ones' corners; a removal, then a save, then the favourite; with none of them given, they are only shown"#, ), C::new("layout.activate", "Open Layout", "Browser", |s, p| { let i = index(s, &by_name(p))?; @@ -3956,6 +3956,8 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { } } let li = index(s, p)?; + // What a publication's scale bar box was measured from, before this change. + let basis = scale_box_basis(s, li); // Locked, it takes no change but being unlocked, which may come with other changes. // "dash": null makes a line solid, so it is a change too. let changes = p @@ -4330,10 +4332,13 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { if let Some(text) = p.get("grid").and_then(|grid| grid.get("crs")).and_then(Value::as_str) { super::say_crs_note(s, text); } - // A frame drawn at another scale or width: a publication's scale bar box measured again. - let frame = s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind); - if matches!(frame, Some(ElementKind::MapFrame { .. })) && ["scale", "w"].iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) { - refit_scale_box(s, li, id); + // Its frame drawn at another scale or width, or the text under its bar changed: a + // publication's scale bar box measured again. + if let Some(basis) = basis { + let given = |keys: &[&str]| keys.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())); + if (id == basis.frame && given(&["scale", "w"])) || (id == basis.text_id && given(&["text", "size", "bold"])) { + refit_scale_box(s, li, &basis); + } } if let Some(w) = font_told { return Ok(json!({"warning": w})); @@ -4365,6 +4370,42 @@ fn scale_text_mm(text: &str, text_pt: f64, bold: bool) -> f64 { /// What a publication places over its map, which a scale bar box measured again must stay off. const PLACED_OVER_MAP: [&str; 3] = ["Legend Box", "Locator", "North Arrow"]; +/// What a publication's scale bar box is measured from: its frame (id, centre, scale, width) and +/// the text under its bar (id, text, size, boldness). +#[derive(Clone)] +struct BoxBasis { + frame: u32, + center: Coord, + scale: f64, + frame_w: f64, + text_id: u32, + text: String, + size: f64, + bold: bool, +} + +/// The basis of a layout's publication scale bar box as it is now (#1379); `None` without one. +fn scale_box_basis(s: &Session, li: usize) -> Option { + let l = s.project.layouts.get(li)?; + let named = |name: &str| l.elements.iter().find(|e| e.name == name); + let (bar, written) = (named("Scale Bar")?, named("Scale")?); + let (ElementKind::ScaleBar { frame, .. }, ElementKind::Text { text, size, bold, .. }) = (&bar.kind, &written.kind) else { + return None; + }; + let fr = l.element(*frame)?; + let ElementKind::MapFrame { center, scale, .. } = &fr.kind else { return None }; + Some(BoxBasis { + frame: *frame, + center: *center, + scale: *scale, + frame_w: fr.w, + text_id: written.id, + text: text.clone(), + size: *size, + bold: *bold, + }) +} + /// A publication's scale bar box measured again after its frame's scale or width changed (#1379): /// the "Scale Box", the "Scale Bar" of `frame` and the "Scale" under it, as a publication lays them /// out (one place, one width, none locked), take the width that bar and the text under it (its @@ -4372,11 +4413,13 @@ const PLACED_OVER_MAP: [&str; 3] = ["Legend Box", "Locator", "North Arrow"]; /// frame they are nearer. A layout without them, with them moved apart, with that text spaced out /// or in another font, naming a map series page (which differs from page to page), or of a frame /// other than the first (whose map the page fills the text from) is left as it is; so is a text -/// longer than the widest box a publication gives (a box widened by hand for it keeps its width). -/// A change to the text or the bar alone doesn't measure the box again; the next change of the -/// frame's scale or width does. A box that would then cover the legend, locator or north arrow, -/// which it didn't, stays as it was, which is said. -fn refit_scale_box(s: &mut Session, li: usize, frame: u32) { +/// longer than the widest box a publication gives. Only a box as wide as `before` (what it was +/// measured from before the change: its frame and text) makes it is measured again: one set by +/// hand keeps its width. A change to the text under the bar measures it again too; a restyled +/// bar alone doesn't. A box that would then cover the legend, locator or north arrow, which it +/// didn't, stays as it was, which is said. +fn refit_scale_box(s: &mut Session, li: usize, before: &BoxBasis) { + let frame = before.frame; let Some(l) = s.project.layouts.get(li) else { return }; let Some(fr) = l.element(frame) else { return }; let ElementKind::MapFrame { map, center, scale, .. } = &fr.kind else { return }; @@ -4411,6 +4454,16 @@ fn refit_scale_box(s: &mut Session, li: usize, frame: u32) { if scale_text_mm(&filled, *size, *bold) > (fr.w * 0.3).max(25.0) { return; } + // The width the box had from its basis before the change: another one was set by hand. + let was = { + let cam = rustgis_map::layout::frame_camera(m, before.center, before.scale, 0.0, 0.0); + let filled = crate::render::layout::expand(&before.text, l, &info, Some((m, cam))); + let bar = (*units, *divisions); + scale_box_width(m.ground_res_at(&cam), before.scale, before.frame_w, bar, l.decimal_comma, (&filled, before.size, before.bold)) + }; + if (boxed.w - was).abs() > 0.01 { + return; + } let w = scale_box_width(ground_per_pt, *scale, fr.w, (*units, *divisions), l.decimal_comma, (&filled, *size, *bold)); let x = if boxed.x + boxed.w / 2.0 > fr.x + fr.w / 2.0 { boxed.x + boxed.w - w } else { boxed.x }; let covers = |(x, w): (f64, f64), e: &rustgis_map::layout::Element| { @@ -4458,6 +4511,7 @@ fn set_frame(s: &mut Session, li: usize, frame: u32, e: &Extent) -> Result<(), S fn frame_zoom(s: &mut Session, p: &Value) -> CmdResult { let li = index(s, p)?; + let basis = scale_box_basis(s, li); let l = s.project.layouts.get(li).ok_or("no layout")?; let frame = zoom_frame(l, p)?; unlocked(l, frame)?; @@ -4494,7 +4548,9 @@ fn frame_zoom(s: &mut Session, p: &Value) -> CmdResult { } o => return Err(format!("unknown zoom '{o}'")), } - refit_scale_box(s, li, frame); + if let Some(basis) = basis.filter(|b| b.frame == frame) { + refit_scale_box(s, li, &basis); + } Ok(json!({"element": frame})) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 8a66bdacc4..15a0c50622 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7660,8 +7660,8 @@ fn publication_layouts_fit_their_pages() { // Beside it on the side it grows to: the left one when it keeps its right edge. let x = if boxed.x + boxed.w / 2.0 > frame_x_mid(&s, li, frame) { boxed.x - 5.5 } else { boxed.x + boxed.w + 0.5 }; s.run("layout.update", &json!({"layout": li, "element": arrow, "x": x, "y": boxed.y, "w": 5.0, "h": 5.0})).unwrap(); - s.run("layout.update", &json!({"layout": li, "element": scale_text, "text": "Escala aproximada da folha {scale}"})).unwrap(); let mark = s.notices.len(); + s.run("layout.update", &json!({"layout": li, "element": scale_text, "text": "Escala aproximada da folha {scale}"})).unwrap(); s.run("layout.update", &json!({"layout": li, "element": frame, "scale": 30_000_000.0})).unwrap(); let after = s.project.layouts[li].element(b).cloned().unwrap(); assert_eq!((after.x, after.w), (boxed.x, boxed.w), "the box stays"); @@ -7675,6 +7675,20 @@ fn publication_layouts_fit_their_pages() { } s.run("layout.update", &json!({"layout": li, "element": frame, "scale": 12_000_000.0})).unwrap(); assert_eq!(s.project.layouts[li].element(b).map(|e| e.w), Some(120.0), "the width set by hand"); + // So does one set by hand within the widest box a publication gives. + s.run("layout.publication", &json!({"profile": "thesis-half", "extent": piaui})).unwrap(); + let li = s.project.layouts.len() - 1; + let frame = s.project.layouts[li].elements.iter().find(|e| e.kind.code() == "mapFrame").map(|e| e.id).unwrap(); + let id_in = |s: &Session, n: &str| s.project.layouts[li].elements.iter().find(|e| e.name == n).map(|e| e.id).unwrap(); + let (b, widest) = (id_in(&s, "Scale Box"), s.project.layouts[li].element(frame).map(|e| (e.w * 0.3).max(25.0)).unwrap()); + let auto = s.project.layouts[li].element(b).map(|e| e.w).unwrap(); + let set = if auto + 2.0 < widest { auto + 2.0 } else { auto - 2.0 }; + for name in ["Scale Box", "Scale Bar", "Scale"] { + s.run("layout.update", &json!({"layout": li, "element": id_in(&s, name), "w": set})).unwrap(); + } + s.run("layout.update", &json!({"layout": li, "element": frame, "scale": 9_000_000.0})).unwrap(); + s.run("layout.frameZoom", &json!({"layout": li, "element": frame, "to": "full"})).unwrap(); + assert_eq!(s.project.layouts[li].element(b).map(|e| e.w), Some(set), "the width set by hand, {auto} mm automatic"); // A long title wraps within the frame, and pushes the map down; the result names every // element for layout.update. let r = s.run("layout.publication", &json!({"extent": piaui, "title": long})).unwrap(); @@ -43528,6 +43542,15 @@ fn arrangements_are_saved_by_name() { // A name copies that arrangement's corners. let r = s.run("layout.arrangement", &json!({"save": "Copy", "arrangement": "mirrored"})).unwrap(); assert_eq!(r["saved"]["Copy"]["locator"], "top-right", "{r}"); + // "arrangement" with "save" is read, not said to be ignored. + let mark = s.notices.len(); + s.run("layout.arrangement", &json!({"save": "Print", "arrangement": "classic"})).unwrap(); + assert!( + !s.notices[mark..].iter().any(|n| n.text.contains("ignores")), + "{:?}", + s.notices[mark..].iter().map(|n| &n.text).collect::>() + ); + s.run("layout.arrangement", &json!({"remove": "Print"})).unwrap(); // Taken by name, case aside, and named in the answer as it is kept. let homes = |v: &Value| crate::cmd::layout::parse_arrangement(v, &Default::default()).unwrap(); assert_eq!(arrangements_to_try(&s, &json!("atlas")).unwrap(), vec![("Atlas".to_string(), homes(&atlas))]); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index f96db30a16..348957caa7 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2909,11 +2909,12 @@ impl RustGisApp { /// `layout.arrangement` changed them. fn save_layout_favourite(&mut self) { if self.session.layout_arrangements != self.ui.layout_arrangements_saved && !self.ui.layout_arrangements_unreadable { - // An arrangement removed here takes with it the entries passed over beside it under - // the same name but for case, so it doesn't come back at the next start. - let kept = &self.session.layout_arrangements; + // An arrangement removed, saved or replaced here takes with it the entries passed over + // beside it under the same name but for case: one removed doesn't come back at the next + // start, and one saved isn't passed over then for the older entry. + let (kept, before) = (&self.session.layout_arrangements, &self.ui.layout_arrangements_saved); let removed: Vec = - self.ui.layout_arrangements_saved.keys().filter(|k| !kept.contains_key(*k)).map(|k| k.to_lowercase()).collect(); + before.keys().chain(kept.keys()).filter(|k| before.get(*k) != kept.get(*k)).map(|k| k.to_lowercase()).collect(); // Only those this version would use: one it can't read stays as it was. self.ui.layout_arrangements_unread.retain(|name, corners| { !removed.contains(&name.to_lowercase()) || !rustgis_engine::cmd::layout::usable_saved_arrangement(name, corners) diff --git a/crates/ui-egui/tests/layout_favourite.rs b/crates/ui-egui/tests/layout_favourite.rs index 9daf5a7817..14cde15b48 100644 --- a/crates/ui-egui/tests/layout_favourite.rs +++ b/crates/ui-egui/tests/layout_favourite.rs @@ -124,4 +124,24 @@ fn saved_arrangements_are_kept_and_restored() { let written: serde_json::Value = serde_json::from_str(&last(SAVED)).unwrap(); // One this version can't read stays as it was. assert_eq!(written, json!({"Keep": mirrored, "ATLAS": {"locator": "middle"}})); + // A name saved here takes the place of a usable entry passed over under it but for case, so + // the next start keeps the new one; one this version can't read stays. + let text = json!({"Atlas": atlas, "atlas": mirrored, "zed": {"locator": "middle"}}).to_string(); + let writes = saved.clone(); + let mut restored = RustGisApp::new(Session::new(Project::default())).with_services(Services { + load_pref: Some(Box::new(move |k| (k == SAVED).then(|| text.clone()))), + save_pref: Some(Box::new(move |k, v| writes.lock().unwrap().push((k.to_string(), v.to_string())))), + ..Services::default() + }); + restored.run("layout.arrangement", json!({"remove": "Atlas", "save": "ATLAS", "arrangement": "legendTop"})).unwrap(); + restored.run("layout.arrangement", json!({"save": "Zed", "arrangement": "classic"})).unwrap(); + let written: serde_json::Value = serde_json::from_str(&last(SAVED)).unwrap(); + let names: Vec<&String> = written.as_object().unwrap().keys().collect(); + assert_eq!(names.len(), 3, "{written}"); + assert_eq!( + (written["ATLAS"]["locator"].clone(), written["zed"].clone()), + (json!("bottom-left"), json!({"locator": "middle"})), + "{written}" + ); + assert_eq!(written["Zed"]["locator"], "top-left", "{written}"); } From 6db57f072bc6ffaadb4a0a8a4a80769c10c6a6ee Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Thu, 12 Nov 2026 16:33:31 -0300 Subject: [PATCH 2/3] fix(layout): one measure for a scale box's automatic width; its bar in its basis (#1379) Reviews on #1867: layout.publication and the check of an untouched box now measure the same text ({scale} filled as the publication fills it, the other tokens as drawn), so a box never touched follows a new scale; the basis keeps the bar's units and divisions, so a restyled bar is measured again too, and a change of units or divisions refits the box. A save or removal that drops unread entries passed over says how many. Tests: an untouched box follows a scale, a restyle and another scale; a name replaced in place takes the place of its duplicate. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layout.rs | 79 ++++++++++++++++-------- crates/engine/src/tests.rs | 23 +++++++ crates/ui-egui/src/lib.rs | 11 ++++ crates/ui-egui/tests/layout_favourite.rs | 16 +++++ 4 files changed, 102 insertions(+), 27 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 9b3dc2d2dc..4c8a4c076d 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -4336,7 +4336,8 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { // publication's scale bar box measured again. if let Some(basis) = basis { let given = |keys: &[&str]| keys.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())); - if (id == basis.frame && given(&["scale", "w"])) || (id == basis.text_id && given(&["text", "size", "bold"])) { + let restyled = id == basis.bar_id && given(&["units", "divisions"]); + if (id == basis.frame && given(&["scale", "w"])) || (id == basis.text_id && given(&["text", "size", "bold"])) || restyled { refit_scale_box(s, li, &basis); } } @@ -4370,26 +4371,57 @@ fn scale_text_mm(text: &str, text_pt: f64, bold: bool) -> f64 { /// What a publication places over its map, which a scale bar box measured again must stay off. const PLACED_OVER_MAP: [&str; 3] = ["Legend Box", "Locator", "North Arrow"]; -/// What a publication's scale bar box is measured from: its frame (id, centre, scale, width) and -/// the text under its bar (id, text, size, boldness). +/// What a publication's scale bar box is measured from: its frame (id, centre, scale, width), its +/// bar (id, units, divisions) and the text under the bar (id, text, size, boldness). #[derive(Clone)] struct BoxBasis { frame: u32, center: Coord, scale: f64, frame_w: f64, + bar_id: u32, + units: ScaleUnits, + divisions: usize, text_id: u32, text: String, size: f64, bold: bool, } +/// The text under a publication's scale bar as the page draws it from `b`: `{scale}` filled as +/// `layout.publication` fills it, the other tokens from the frame's map. +fn basis_text(l: &Layout, m: &rustgis_map::Map, b: &BoxBasis) -> String { + let info = crate::render::layout::PageInfo { page: 1, pages: 1, page_name: String::new() }; + let cam = rustgis_map::layout::frame_camera(m, b.center, b.scale, 0.0, 0.0); + let text = b.text.replace("{scale}", &crate::render::layout::scale_ratio_text(b.scale, l.decimal_comma)); + crate::render::layout::expand(&text, l, &info, Some((m, cam))) +} + +/// The automatic width (mm) of a publication's scale bar box measured from `b`: the one +/// `layout.publication` gives it for that frame, bar and text. +fn basis_width(l: &Layout, m: &rustgis_map::Map, b: &BoxBasis) -> f64 { + let ground_per_pt = m.ground_res_at(&rustgis_map::layout::frame_camera(m, b.center, b.scale, 0.0, 0.0)); + let filled = basis_text(l, m, b); + scale_box_width(ground_per_pt, b.scale, b.frame_w, (b.units, b.divisions), l.decimal_comma, (&filled, b.size, b.bold)) +} + +/// The automatic width (mm) of layout `li`'s publication scale bar box as things are now. +#[cfg(test)] +pub(crate) fn scale_box_auto(s: &Session, li: usize) -> Option { + let b = scale_box_basis(s, li)?; + let l = s.project.layouts.get(li)?; + let ElementKind::MapFrame { map, .. } = &l.element(b.frame)?.kind else { return None }; + let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map))?; + Some(basis_width(l, m, &b)) +} + /// The basis of a layout's publication scale bar box as it is now (#1379); `None` without one. fn scale_box_basis(s: &Session, li: usize) -> Option { let l = s.project.layouts.get(li)?; let named = |name: &str| l.elements.iter().find(|e| e.name == name); let (bar, written) = (named("Scale Bar")?, named("Scale")?); - let (ElementKind::ScaleBar { frame, .. }, ElementKind::Text { text, size, bold, .. }) = (&bar.kind, &written.kind) else { + let (ElementKind::ScaleBar { frame, units, divisions, .. }, ElementKind::Text { text, size, bold, .. }) = (&bar.kind, &written.kind) + else { return None; }; let fr = l.element(*frame)?; @@ -4399,6 +4431,9 @@ fn scale_box_basis(s: &Session, li: usize) -> Option { center: *center, scale: *scale, frame_w: fr.w, + bar_id: bar.id, + units: *units, + divisions: *divisions, text_id: written.id, text: text.clone(), size: *size, @@ -4414,23 +4449,21 @@ fn scale_box_basis(s: &Session, li: usize) -> Option { /// or in another font, naming a map series page (which differs from page to page), or of a frame /// other than the first (whose map the page fills the text from) is left as it is; so is a text /// longer than the widest box a publication gives. Only a box as wide as `before` (what it was -/// measured from before the change: its frame and text) makes it is measured again: one set by -/// hand keeps its width. A change to the text under the bar measures it again too; a restyled -/// bar alone doesn't. A box that would then cover the legend, locator or north arrow, which it -/// didn't, stays as it was, which is said. +/// measured from before the change: its frame, bar and text) makes it is measured again: one set +/// by hand keeps its width. A change to the text under the bar or to the bar's units or divisions +/// measures it again too. A box that would then cover the legend, locator or north arrow, which +/// it didn't, stays as it was, which is said. fn refit_scale_box(s: &mut Session, li: usize, before: &BoxBasis) { let frame = before.frame; + let Some(now) = scale_box_basis(s, li).filter(|b| b.frame == frame) else { return }; let Some(l) = s.project.layouts.get(li) else { return }; let Some(fr) = l.element(frame) else { return }; - let ElementKind::MapFrame { map, center, scale, .. } = &fr.kind else { return }; + let ElementKind::MapFrame { map, .. } = &fr.kind else { return }; let Some(m) = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)) else { return }; - let ground_per_pt = m.ground_res_at(&rustgis_map::layout::frame_camera(m, *center, *scale, 0.0, 0.0)); let named = |name: &str| l.elements.iter().find(|e| e.name == name); let (Some(boxed), Some(bar), Some(text)) = (named("Scale Box"), named("Scale Bar"), named("Scale")) else { return }; - let ( - ElementKind::ScaleBar { frame: of, units, divisions, .. }, - ElementKind::Text { text: written, size, bold, letter_spacing, font, font_name, .. }, - ) = (&bar.kind, &text.kind) + let (ElementKind::ScaleBar { frame: of, .. }, ElementKind::Text { text: written, letter_spacing, font, font_name, .. }) = + (&bar.kind, &text.kind) else { return; }; @@ -4442,29 +4475,21 @@ fn refit_scale_box(s: &mut Session, li: usize, before: &BoxBasis) { { return; } - // Its tokens filled as the page draws them, `{scale}` from the frame. - let info = crate::render::layout::PageInfo { page: 1, pages: 1, page_name: String::new() }; - let cam = rustgis_map::layout::frame_camera(m, *center, *scale, 0.0, 0.0); - let filled = crate::render::layout::expand(written, l, &info, Some((m, cam))); + // Its tokens filled as the page draws them. + let filled = basis_text(l, m, &now); let together = [bar, text].iter().all(|e| (e.x - boxed.x).abs() < 1e-6 && (e.w - boxed.w).abs() < 1e-6 && !e.locked); if *of != frame || !together || boxed.locked { return; } // A text longer than the widest box a publication gives: a width set by hand for it is kept. - if scale_text_mm(&filled, *size, *bold) > (fr.w * 0.3).max(25.0) { + if scale_text_mm(&filled, now.size, now.bold) > (fr.w * 0.3).max(25.0) { return; } // The width the box had from its basis before the change: another one was set by hand. - let was = { - let cam = rustgis_map::layout::frame_camera(m, before.center, before.scale, 0.0, 0.0); - let filled = crate::render::layout::expand(&before.text, l, &info, Some((m, cam))); - let bar = (*units, *divisions); - scale_box_width(m.ground_res_at(&cam), before.scale, before.frame_w, bar, l.decimal_comma, (&filled, before.size, before.bold)) - }; - if (boxed.w - was).abs() > 0.01 { + if (boxed.w - basis_width(l, m, before)).abs() > 0.01 { return; } - let w = scale_box_width(ground_per_pt, *scale, fr.w, (*units, *divisions), l.decimal_comma, (&filled, *size, *bold)); + let w = basis_width(l, m, &now); let x = if boxed.x + boxed.w / 2.0 > fr.x + fr.w / 2.0 { boxed.x + boxed.w - w } else { boxed.x }; let covers = |(x, w): (f64, f64), e: &rustgis_map::layout::Element| { x < e.x + e.w && e.x < x + w && boxed.y < e.y + e.h && e.y < boxed.y + boxed.h diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 15a0c50622..0a4096921a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7689,6 +7689,29 @@ fn publication_layouts_fit_their_pages() { s.run("layout.update", &json!({"layout": li, "element": frame, "scale": 9_000_000.0})).unwrap(); s.run("layout.frameZoom", &json!({"layout": li, "element": frame, "to": "full"})).unwrap(); assert_eq!(s.project.layouts[li].element(b).map(|e| e.w), Some(set), "the width set by hand, {auto} mm automatic"); + // An untouched box follows: a new scale, the bar restyled, then another scale, each leave it + // as wide as it is automatically then. + s.run("layout.publication", &json!({"profile": "thesis-half", "extent": piaui})).unwrap(); + let li = s.project.layouts.len() - 1; + let id_in = |s: &Session, n: &str| s.project.layouts[li].elements.iter().find(|e| e.name == n).map(|e| e.id).unwrap(); + let frame = s.project.layouts[li].elements.iter().find(|e| e.kind.code() == "mapFrame").map(|e| e.id).unwrap(); + let (b, bar) = (id_in(&s, "Scale Box"), id_in(&s, "Scale Bar")); + let width = |s: &Session| s.project.layouts[li].element(b).map(|e| e.w).unwrap(); + let first = width(&s); + let mut widths = Vec::new(); + for change in [ + json!({"element": frame, "scale": 20_000_000.0}), + json!({"element": bar, "units": "miles"}), + json!({"element": frame, "scale": 7_000_000.0}), + ] { + let mut change = change; + change["layout"] = json!(li); + s.run("layout.update", &change).unwrap(); + let auto = crate::cmd::layout::scale_box_auto(&s, li).unwrap(); + assert!((width(&s) - auto).abs() < 1e-9, "{change}: {} mm, {auto} mm automatic", width(&s)); + widths.push(width(&s)); + } + assert!(widths.iter().any(|w| (w - first).abs() > 0.5), "the box changed: {first} mm, then {widths:?}"); // A long title wraps within the frame, and pushes the map down; the result names every // element for layout.update. let r = s.run("layout.publication", &json!({"extent": piaui, "title": long})).unwrap(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 348957caa7..0aa3f8d2dd 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2916,9 +2916,20 @@ impl RustGisApp { let removed: Vec = before.keys().chain(kept.keys()).filter(|k| before.get(*k) != kept.get(*k)).map(|k| k.to_lowercase()).collect(); // Only those this version would use: one it can't read stays as it was. + let passed = self.ui.layout_arrangements_unread.len(); self.ui.layout_arrangements_unread.retain(|name, corners| { !removed.contains(&name.to_lowercase()) || !rustgis_engine::cmd::layout::usable_saved_arrangement(name, corners) }); + let dropped = passed - self.ui.layout_arrangements_unread.len(); + if dropped > 0 { + self.session.notify( + crate::i18n::fmt( + "Saved arrangements of publication layouts passed over under a name saved or removed now are dropped: {count}.", + &[("count", &dropped.to_string())], + ), + false, + ); + } self.ui.layout_arrangements_saved = self.session.layout_arrangements.clone(); // Those this version couldn't use go back as they were, but where one saved here took // their very name. diff --git a/crates/ui-egui/tests/layout_favourite.rs b/crates/ui-egui/tests/layout_favourite.rs index 14cde15b48..0c06739635 100644 --- a/crates/ui-egui/tests/layout_favourite.rs +++ b/crates/ui-egui/tests/layout_favourite.rs @@ -133,7 +133,23 @@ fn saved_arrangements_are_kept_and_restored() { save_pref: Some(Box::new(move |k, v| writes.lock().unwrap().push((k.to_string(), v.to_string())))), ..Services::default() }); + // Replaced under its very name, it takes the place of the usable entry passed over beside it. + let replaced = json!({"Atlas": atlas, "atlas": mirrored}).to_string(); + let writes = saved.clone(); + let mut kept = RustGisApp::new(Session::new(Project::default())).with_services(Services { + load_pref: Some(Box::new(move |k| (k == SAVED).then(|| replaced.clone()))), + save_pref: Some(Box::new(move |k, v| writes.lock().unwrap().push((k.to_string(), v.to_string())))), + ..Services::default() + }); + kept.run("layout.arrangement", json!({"save": "Atlas", "arrangement": "legendTop"})).unwrap(); + let written: serde_json::Value = serde_json::from_str(&last(SAVED)).unwrap(); + assert_eq!( + written, + json!({"Atlas": {"locator": "bottom-left", "northArrow": "top-right", "legend": "top-left", "scaleBar": "bottom-right"}}) + ); restored.run("layout.arrangement", json!({"remove": "Atlas", "save": "ATLAS", "arrangement": "legendTop"})).unwrap(); + // The entry passed over that goes with it is said. + assert!(restored.session.notices.iter().any(|n| n.text.ends_with("are dropped: 1.")), "said"); restored.run("layout.arrangement", json!({"save": "Zed", "arrangement": "classic"})).unwrap(); let written: serde_json::Value = serde_json::from_str(&last(SAVED)).unwrap(); let names: Vec<&String> = written.as_object().unwrap().keys().collect(); From 916288743c6d1b6d4cc6373a22d8503153db6666 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Thu, 12 Nov 2026 16:52:57 -0300 Subject: [PATCH 3/3] test(layout): a save with nothing passed over says nothing of dropping (#1379) Review on #1867: the drop notice is tested both ways. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/tests/layout_favourite.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/ui-egui/tests/layout_favourite.rs b/crates/ui-egui/tests/layout_favourite.rs index 0c06739635..893a98200e 100644 --- a/crates/ui-egui/tests/layout_favourite.rs +++ b/crates/ui-egui/tests/layout_favourite.rs @@ -61,6 +61,8 @@ fn saved_arrangements_are_kept_and_restored() { }); let atlas = json!({"locator": "bottom-left", "northArrow": "top-left", "legend": "top-right", "scaleBar": "bottom-right"}); app.run("layout.arrangement", json!({"save": "Atlas", "arrangement": atlas, "favourite": "Atlas"})).unwrap(); + // With nothing passed over, a save drops nothing and says nothing of it. + assert!(!app.session.notices.iter().any(|n| n.text.contains("are dropped"))); let last = |key: &str| saved.lock().unwrap().iter().rev().find(|(k, _)| k == key).map(|(_, v)| v.clone()).unwrap(); let (arrangements, favourite) = (last(SAVED), last(KEY)); assert_eq!(serde_json::from_str::(&arrangements).unwrap(), json!({"Atlas": atlas}));