From 31850e2a3f371ea6f81227dcd6020c735b6173e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Thu, 26 Nov 2026 00:14:09 -0500 Subject: [PATCH 1/6] feat(layout): Insert Dynamic Text offers a layer's count, sum, mean, least and greatest (#2853) --- crates/ui-egui/src/graphicformat.rs | 5 +- crates/ui-egui/src/layoutview.rs | 6 +- crates/ui-egui/src/textform.rs | 136 +++++++++++++++++++++++++++- 3 files changed, 141 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/graphicformat.rs b/crates/ui-egui/src/graphicformat.rs index 36a73baae2..6b6f4ba167 100644 --- a/crates/ui-egui/src/graphicformat.rs +++ b/crates/ui-egui/src/graphicformat.rs @@ -135,8 +135,9 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) { named_fonts: false, select, }; - let settled = crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("graphic_format", k), &[graphics::TOKENS], fields)) - .unwrap_or_default(); + let settled = + crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("graphic_format", k), &[graphics::TOKENS], &[], fields)) + .unwrap_or_default(); let save = |app: &mut RustGisApp, change: serde_json::Value| { let mut p = json!({"layer": layer, "row": row}); if let (Some(p), Some(c)) = (p.as_object_mut(), change.as_object()) { diff --git a/crates/ui-egui/src/layoutview.rs b/crates/ui-egui/src/layoutview.rs index c032cb5bb0..2a1d6b8f68 100644 --- a/crates/ui-egui/src/layoutview.rs +++ b/crates/ui-egui/src/layoutview.rs @@ -2717,6 +2717,7 @@ fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: usize) { let (mut it, mut al, mut ls, mut fam, mut named) = (*italic, *align, *letter_spacing, *font, font_name.clone()); let (mut wr, mut va, mut line_sp, mut cs) = (*wrap, valign.kept(), *line_spacing, case.kept()); let tokens = [rustgis_engine::map::graphics::TOKENS, rustgis_engine::map::layout::PAGE_TOKENS]; + let layers = crate::textform::StatLayer::of(layout, &app.session.project.maps); let warning = app .layout .text_losses @@ -2742,8 +2743,9 @@ fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: usize) { named_fonts: true, select: false, }; - let settled = crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("layout_text", li, id), &tokens, fields)) - .unwrap_or_default(); + let settled = + crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("layout_text", li, id), &tokens, &layers, fields)) + .unwrap_or_default(); if settled.typing { app.layout.typing.insert(key, buf); } else if settled.text && buf != *text { diff --git a/crates/ui-egui/src/textform.rs b/crates/ui-egui/src/textform.rs index 881657a47e..daa9461ace 100644 --- a/crates/ui-egui/src/textform.rs +++ b/crates/ui-egui/src/textform.rs @@ -17,6 +17,36 @@ use crate::widgets::Dragged; /// found by typing part of a name. const MAX_FONTS_SHOWN: usize = 200; +/// A layer a layout text can show a figure of (#2853): its name and its numeric fields, which the +/// Dynamic Text menu offers as tokens (`{count:Name}`, `{sum:Name:field}`…). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct StatLayer { + pub name: String, + pub fields: Vec, +} + +impl StatLayer { + /// The layers a text of `layout` can show figures of: those of the map its first frame shows + /// (as the page reads them), without a raster's or a graphics layer's, each with its numeric + /// fields. A name a token can't hold (`{`, `}`, or a field's `:`) is left out. + pub fn of(layout: &rustgis_engine::map::layout::Layout, maps: &[rustgis_engine::map::Map]) -> Vec { + use rustgis_engine::map::layout::ElementKind; + let Some(ElementKind::MapFrame { map, .. }) = layout.first_frame().and_then(|f| layout.element(f)).map(|e| &e.kind) else { + return Vec::new(); + }; + let Some(map) = maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)) else { return Vec::new() }; + let holds = |name: &str, colon: bool| !name.is_empty() && !name.contains(['{', '}']) && !(colon && name.contains(':')); + map.layers + .iter() + .filter(|l| !l.graphics && l.raster.is_none() && holds(&l.name, false)) + .map(|l| StatLayer { + name: l.name.clone(), + fields: l.dataset.fields.iter().filter(|f| f.ty.is_numeric() && holds(&f.name, true)).map(|f| f.name.clone()).collect(), + }) + .collect() + } +} + /// What the form edits. A style is left out where the text doesn't have it. pub struct Fields<'a> { pub text: &'a mut String, @@ -90,8 +120,15 @@ pub fn section(ui: &mut Ui, title: &str, add: impl FnOnce(&mut Ui) -> R) -> O pub const LABEL_W: f32 = 96.0; /// The form, its fields told apart by `id` (an element, a dialog). `tokens` are the dynamic text -/// lists the text expands where it is shown, each a section of the Dynamic Text menu. -pub fn show(ui: &mut Ui, id: impl std::hash::Hash + std::fmt::Debug, tokens: &[&[(&str, &str)]], f: Fields<'_>) -> Settled { +/// lists the text expands where it is shown, each a section of the Dynamic Text menu; `layers` are +/// the layers whose figures it can show, a section of their own (none for a text graphic). +pub fn show( + ui: &mut Ui, + id: impl std::hash::Hash + std::fmt::Debug, + tokens: &[&[(&str, &str)]], + layers: &[StatLayer], + f: Fields<'_>, +) -> Settled { let id = egui::Id::unique(id); let mut out = Settled::default(); let hint = tl!("Shift+Enter starts a new line."); @@ -125,6 +162,34 @@ pub fn show(ui: &mut Ui, id: impl std::hash::Hash + std::fmt::Debug, tokens: &[& } } } + // The figures of a layer (#2853): a submenu per layer, a count and, for each numeric field, + // its sum, mean, least and greatest. + if !layers.is_empty() { + ui.separator(); + crate::widgets::submenu(ui, &tl!("Layer statistic"), |ui| { + for layer in layers { + crate::widgets::submenu(ui, &layer.name, |ui| { + let mut add = |ui: &mut Ui, what: &str, token: String| { + if ui.button(format!("{what} {token}")).clicked() { + f.text.push_str(&token); + out.text = true; + ui.close(); + } + }; + add(ui, &tl!("Count"), format!("{{count:{}}}", layer.name)); + for field in &layer.fields { + crate::widgets::submenu(ui, field, |ui| { + for (code, what) in + [("sum", tl!("Sum")), ("mean", tl!("Mean")), ("min", tl!("Least")), ("max", tl!("Greatest"))] + { + add(ui, &what, format!("{{{code}:{}:{field}}}", layer.name)); + } + }); + } + }); + } + }); + } }); out.typing = r.has_focus() && !out.text; egui::Grid::new(id.with("style")).num_columns(2).min_col_width(LABEL_W).spacing(vec2(8.0, 6.0)).show(ui, |ui| { @@ -411,6 +476,8 @@ mod tests { /// Whether the vertical alignment, line spacing and case are offered, as a layout text's /// form offers them. blocks: bool, + /// The layers whose figures the text can show. + layers: Vec, settled: Vec<&'static str>, } @@ -423,6 +490,7 @@ mod tests { ui, "form", &[rustgis_engine::map::graphics::TOKENS, rustgis_engine::map::layout::PAGE_TOKENS], + &f.layers, Fields { text: &mut f.text, size: &mut size, @@ -490,6 +558,70 @@ mod tests { assert!(h.query_by_label("Page number {page}").is_none(), "the menu closes"); } + /// The layers offered are those of the first frame's map, with their numeric fields; a raster + /// layer, a graphics layer and a name a token can't hold are left out (#2853). + #[test] + fn the_layers_offered_are_the_first_frames_with_their_numeric_fields() { + use rustgis_engine::data::{Dataset, Field, FieldType, GeometryType}; + use rustgis_engine::map::{ + Map, + layout::{ElementKind, Layout}, + }; + let layer = |name: &str, fields: Vec<(&str, FieldType)>| { + Dataset::new(name, GeometryType::None, fields.into_iter().map(|(n, t)| Field::new(n, t)).collect()) + }; + let mut map = Map::new("Main"); + map.add_dataset(layer("Cities", vec![("pop", FieldType::Double), ("name", FieldType::Text), ("a:b", FieldType::Double)])); + map.add_dataset(layer("Bad {name}", vec![("x", FieldType::Double)])); + let mut other = Map::new("Other"); + other.add_dataset(layer("Rivers", vec![("km", FieldType::Double)])); + let mut layout = Layout::new("L", 100.0, 100.0); + assert_eq!(StatLayer::of(&layout, &[map.clone(), other.clone()]), Vec::new(), "no frame, no layers"); + let frame = |m: &str| -> ElementKind { + serde_json::from_value(serde_json::json!({"MapFrame": {"map": m, "center": {"x": 0, "y": 0}, "scale": 1000000}})) + .unwrap_or_else(|e| panic!("{e}")) + }; + layout.add("Frame", (0.0, 0.0, 50.0, 50.0), frame("main")); + assert_eq!( + StatLayer::of(&layout, &[map.clone(), other.clone()]), + [StatLayer { name: "Cities".into(), fields: vec!["pop".into()] }] + ); + let mut second = Layout::new("L2", 100.0, 100.0); + second.add("Frame", (0.0, 0.0, 50.0, 50.0), frame("Other")); + assert_eq!(StatLayer::of(&second, &[map, other]), [StatLayer { name: "Rivers".into(), fields: vec!["km".into()] }]); + } + + /// A layer statistic is added from a submenu per layer, a count and the four figures of each + /// numeric field (#2853); a text with no layers to read has no such section. + #[test] + fn a_layer_statistic_is_added_from_its_layers_submenu() { + let cities = StatLayer { name: "Cities".into(), fields: vec!["pop".into(), "area".into()] }; + let mut h = harness(Form { text: "".into(), size: 12.0, layers: vec![cities], ..Form::default() }); + h.run(); + for (path, text) in [ + (&["Cities", "Count {count:Cities}"][..], "{count:Cities}"), + (&["Cities", "pop", "Sum {sum:Cities:pop}"][..], "{count:Cities}{sum:Cities:pop}"), + (&["Cities", "area", "Greatest {max:Cities:area}"][..], "{count:Cities}{sum:Cities:pop}{max:Cities:area}"), + ] { + h.get_by_label("Insert Dynamic Text").click(); + h.run(); + h.get_by_label("Layer statistic").click(); + h.run(); + for step in path { + h.get_by_label(step).click(); + h.run(); + } + assert_eq!(h.state().text, text); + } + assert_eq!(h.state().settled, ["text", "text", "text"]); + assert!(h.query_by_label("Layer statistic").is_none(), "the menu closes"); + let mut none = harness(Form { text: "T".into(), size: 12.0, ..Form::default() }); + none.run(); + none.get_by_label("Insert Dynamic Text").click(); + none.run(); + assert!(none.query_by_label("Layer statistic").is_none(), "no layers, no section"); + } + /// Colour and boldness show only where the text has them; the size stays in the one range /// text is drawn at. #[test] From f2deb1e70cf11ce9bf3ba64685724d2cc9ba892b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Thu, 26 Nov 2026 00:35:16 -0500 Subject: [PATCH 2/6] fix(layout): the Layer statistic menu borrows nothing it doesn't need (#2853) --- crates/ui-egui/src/textform.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/textform.rs b/crates/ui-egui/src/textform.rs index daa9461ace..86eac57c20 100644 --- a/crates/ui-egui/src/textform.rs +++ b/crates/ui-egui/src/textform.rs @@ -166,7 +166,7 @@ pub fn show( // its sum, mean, least and greatest. if !layers.is_empty() { ui.separator(); - crate::widgets::submenu(ui, &tl!("Layer statistic"), |ui| { + crate::widgets::submenu(ui, tl!("Layer statistic"), |ui| { for layer in layers { crate::widgets::submenu(ui, &layer.name, |ui| { let mut add = |ui: &mut Ui, what: &str, token: String| { @@ -176,13 +176,13 @@ pub fn show( ui.close(); } }; - add(ui, &tl!("Count"), format!("{{count:{}}}", layer.name)); + add(ui, tl!("Count"), format!("{{count:{}}}", layer.name)); for field in &layer.fields { crate::widgets::submenu(ui, field, |ui| { for (code, what) in [("sum", tl!("Sum")), ("mean", tl!("Mean")), ("min", tl!("Least")), ("max", tl!("Greatest"))] { - add(ui, &what, format!("{{{code}:{}:{field}}}", layer.name)); + add(ui, what, format!("{{{code}:{}:{field}}}", layer.name)); } }); } From 82bbbdbf1e408f470661d0c9cb49dc21ee3bc701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Thu, 26 Nov 2026 12:25:49 -0500 Subject: [PATCH 3/6] =?UTF-8?q?feat(layout):=20Publish=20=E2=80=BA=20Dynam?= =?UTF-8?q?ic=20Text=20offers=20a=20layer=20statistic,=20from=20the=20subm?= =?UTF-8?q?enu=20the=20Format=20pane=20uses=20(#3036)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ui-egui/src/ribbon.rs | 31 ++++++++++++++++ crates/ui-egui/src/textform.rs | 68 ++++++++++++++++++---------------- docs/user-guide/layouts.md | 6 ++- 3 files changed, 73 insertions(+), 32 deletions(-) diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 081d4e3e01..1332c0d7c3 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3635,6 +3635,7 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) { if big(ui, app, "g_text", "Text", "Add a text element.", "ui.menu", Value::Null, false, false).clicked() { add(app, json!({"kind": "text"})); } + let layers = crate::textform::StatLayer::of(&layout, &app.session.project.maps); big_menu(ui, app, "dynamic_text", "Dynamic\nText", "Add text that updates: page, date, scale, map and more.", |ui, app| { for (token, what) in [ ("{date}", "Current date"), @@ -3651,6 +3652,7 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) { ui.close(); } } + crate::textform::stat_menu(ui, &layers, |token| add(app, json!({"kind": "dynamicText", "text": token}))); }); if big(ui, app, "picture", "Picture", "Add a PNG or JPEG picture to the page.", "ui.layoutPicture", Value::Null, false, false) .clicked() @@ -6884,6 +6886,35 @@ mod tests { } } + /// Publish › Text › Dynamic Text › Layer statistic adds a text holding the token picked, from + /// the layers of the first frame's map (#3036); a layout with no frame has no such section. + #[test] + fn publish_dynamic_text_adds_a_layer_statistic() { + use rustgis_engine::map::layout::ElementKind; + let mut app = RustGisApp::with_sample(); + app.run("layout.new", json!({"name": "Figures"})).unwrap(); + let li = app.session.active_layout.unwrap(); + let before = app.session.project.layouts[li].elements.len(); + let mut h = tab_harness(app, layout_groups); + for step in ["Dynamic Text", "Layer statistic", "Cities", "Count {count:Cities}"] { + h.get_by_label(step).click(); + h.run(); + } + let elements = &h.state().session.project.layouts[li].elements; + assert_eq!(elements.len(), before + 1); + assert!(matches!(&elements.last().unwrap().kind, ElementKind::Text { text, .. } if text == "{count:Cities}")); + assert_eq!(h.state().layout.selected, Some(elements.last().unwrap().id)); + assert!(h.query_by_label("Layer statistic").is_none(), "the menu closes"); + + let frame = h.state().session.project.layouts[li].first_frame().unwrap(); + h.state_mut().run("layout.removeElement", json!({"layout": li, "element": frame})).unwrap(); + h.run(); + h.get_by_label("Dynamic Text").click(); + h.run(); + assert!(h.query_by_label_contains("Map scale").is_some(), "the menu is open"); + assert!(h.query_by_label("Layer statistic").is_none(), "no frame, no layers"); + } + /// New Layout › Publication Layout › a sheet › a style makes that sheet in that style, titled /// with the map's name; a figure has no styles to pick and is made at once. #[test] diff --git a/crates/ui-egui/src/textform.rs b/crates/ui-egui/src/textform.rs index 86eac57c20..ee350a4975 100644 --- a/crates/ui-egui/src/textform.rs +++ b/crates/ui-egui/src/textform.rs @@ -17,7 +17,7 @@ use crate::widgets::Dragged; /// found by typing part of a name. const MAX_FONTS_SHOWN: usize = 200; -/// A layer a layout text can show a figure of (#2853): its name and its numeric fields, which the +/// A layer a layout text can show a figure of (#3036): its name and its numeric fields, which the /// Dynamic Text menu offers as tokens (`{count:Name}`, `{sum:Name:field}`…). #[derive(Clone, Debug, PartialEq, Eq)] pub struct StatLayer { @@ -47,6 +47,36 @@ impl StatLayer { } } +/// The "Layer statistic" section of a Dynamic Text menu (#3036), the Format pane's and the +/// ribbon's: a submenu per layer, a count and, for each numeric field, its sum, mean, least and +/// greatest. `pick` takes the token chosen; with no layers there is no section. +pub fn stat_menu(ui: &mut Ui, layers: &[StatLayer], mut pick: impl FnMut(String)) { + if layers.is_empty() { + return; + } + ui.separator(); + crate::widgets::submenu(ui, tl!("Layer statistic"), |ui| { + for layer in layers { + crate::widgets::submenu(ui, &layer.name, |ui| { + let mut add = |ui: &mut Ui, what: &str, token: String| { + if ui.button(format!("{what} {token}")).clicked() { + pick(token); + ui.close(); + } + }; + add(ui, tl!("Count"), format!("{{count:{}}}", layer.name)); + for field in &layer.fields { + crate::widgets::submenu(ui, field, |ui| { + for (code, what) in [("sum", tl!("Sum")), ("mean", tl!("Mean")), ("min", tl!("Least")), ("max", tl!("Greatest"))] { + add(ui, what, format!("{{{code}:{}:{field}}}", layer.name)); + } + }); + } + }); + } + }); +} + /// What the form edits. A style is left out where the text doesn't have it. pub struct Fields<'a> { pub text: &'a mut String, @@ -162,34 +192,10 @@ pub fn show( } } } - // The figures of a layer (#2853): a submenu per layer, a count and, for each numeric field, - // its sum, mean, least and greatest. - if !layers.is_empty() { - ui.separator(); - crate::widgets::submenu(ui, tl!("Layer statistic"), |ui| { - for layer in layers { - crate::widgets::submenu(ui, &layer.name, |ui| { - let mut add = |ui: &mut Ui, what: &str, token: String| { - if ui.button(format!("{what} {token}")).clicked() { - f.text.push_str(&token); - out.text = true; - ui.close(); - } - }; - add(ui, tl!("Count"), format!("{{count:{}}}", layer.name)); - for field in &layer.fields { - crate::widgets::submenu(ui, field, |ui| { - for (code, what) in - [("sum", tl!("Sum")), ("mean", tl!("Mean")), ("min", tl!("Least")), ("max", tl!("Greatest"))] - { - add(ui, what, format!("{{{code}:{}:{field}}}", layer.name)); - } - }); - } - }); - } - }); - } + stat_menu(ui, layers, |token| { + f.text.push_str(&token); + out.text = true; + }); }); out.typing = r.has_focus() && !out.text; egui::Grid::new(id.with("style")).num_columns(2).min_col_width(LABEL_W).spacing(vec2(8.0, 6.0)).show(ui, |ui| { @@ -559,7 +565,7 @@ mod tests { } /// The layers offered are those of the first frame's map, with their numeric fields; a raster - /// layer, a graphics layer and a name a token can't hold are left out (#2853). + /// layer, a graphics layer and a name a token can't hold are left out (#3036). #[test] fn the_layers_offered_are_the_first_frames_with_their_numeric_fields() { use rustgis_engine::data::{Dataset, Field, FieldType, GeometryType}; @@ -592,7 +598,7 @@ mod tests { } /// A layer statistic is added from a submenu per layer, a count and the four figures of each - /// numeric field (#2853); a text with no layers to read has no such section. + /// numeric field (#3036); a text with no layers to read has no such section. #[test] fn a_layer_statistic_is_added_from_its_layers_submenu() { let cities = StatLayer { name: "Cities".into(), fields: vec!["pop".into(), "area".into()] }; diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index d114f629a3..13c643c609 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -490,7 +490,11 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu **Insert Dynamic Text** menu, under the text, lists each with what it shows. A layer's figures fill in too (#2853): `{count:Cities}` is how many features the layer has, and `{sum:Cities:pop}`, `{mean:Cities:pop}`, `{min:Cities:pop}` and `{max:Cities:pop}` add up, average or pick the least - or greatest number of a field, read as a chart reads its values. They follow the layer's + or greatest number of a field, read as a chart reads its values. **Layer statistic** writes them, + in a layout text's **Insert Dynamic Text** menu and in **Publish › Text › Dynamic Text** (which + adds a text holding one): a submenu for each layer of the first map frame's map, with **Count** and, under each numeric field, **Sum**, **Mean**, + **Least** and **Greatest** (raster layers, graphics layers and names a token can't hold are not + listed). They follow the layer's definition query (one that no longer compiles keeps every feature, as the map draws it), take the map of the layout's first map frame (in a series, the whole layer, not the page's part of it, so a definition query that reads the series page's values keeps nothing there), and are written as a From 52ae9c79f78ca39bc3e2ba9b93d9ed0484d34650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Thu, 26 Nov 2026 12:30:02 -0500 Subject: [PATCH 4/6] fix(layout): a layer under another of its name is not offered, and the ribbon lists layers only with its menu open (#3036) --- crates/ui-egui/src/ribbon.rs | 2 +- crates/ui-egui/src/textform.rs | 8 +++++++- docs/user-guide/layouts.md | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 1332c0d7c3..17cad00f04 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3635,7 +3635,6 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) { if big(ui, app, "g_text", "Text", "Add a text element.", "ui.menu", Value::Null, false, false).clicked() { add(app, json!({"kind": "text"})); } - let layers = crate::textform::StatLayer::of(&layout, &app.session.project.maps); big_menu(ui, app, "dynamic_text", "Dynamic\nText", "Add text that updates: page, date, scale, map and more.", |ui, app| { for (token, what) in [ ("{date}", "Current date"), @@ -3652,6 +3651,7 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) { ui.close(); } } + let layers = crate::textform::StatLayer::of(&layout, &app.session.project.maps); crate::textform::stat_menu(ui, &layers, |token| add(app, json!({"kind": "dynamicText", "text": token}))); }); if big(ui, app, "picture", "Picture", "Add a PNG or JPEG picture to the page.", "ui.layoutPicture", Value::Null, false, false) diff --git a/crates/ui-egui/src/textform.rs b/crates/ui-egui/src/textform.rs index ee350a4975..0c55f87915 100644 --- a/crates/ui-egui/src/textform.rs +++ b/crates/ui-egui/src/textform.rs @@ -28,7 +28,8 @@ pub struct StatLayer { impl StatLayer { /// The layers a text of `layout` can show figures of: those of the map its first frame shows /// (as the page reads them), without a raster's or a graphics layer's, each with its numeric - /// fields. A name a token can't hold (`{`, `}`, or a field's `:`) is left out. + /// fields. A name a token can't hold (`{`, `}`, or a field's `:`) is left out, and so is a layer + /// under another of its name, which no token reads. pub fn of(layout: &rustgis_engine::map::layout::Layout, maps: &[rustgis_engine::map::Map]) -> Vec { use rustgis_engine::map::layout::ElementKind; let Some(ElementKind::MapFrame { map, .. }) = layout.first_frame().and_then(|f| layout.element(f)).map(|e| &e.kind) else { @@ -39,6 +40,7 @@ impl StatLayer { map.layers .iter() .filter(|l| !l.graphics && l.raster.is_none() && holds(&l.name, false)) + .filter(|l| map.layer_by_name(&l.name).is_some_and(|first| first.id == l.id)) .map(|l| StatLayer { name: l.name.clone(), fields: l.dataset.fields.iter().filter(|f| f.ty.is_numeric() && holds(&f.name, true)).map(|f| f.name.clone()).collect(), @@ -577,8 +579,12 @@ mod tests { Dataset::new(name, GeometryType::None, fields.into_iter().map(|(n, t)| Field::new(n, t)).collect()) }; let mut map = Map::new("Main"); + // A token reads the first layer of a name, case aside, so one under it is not offered + // (a layer is added above the others). + map.add_dataset(layer("Under", vec![("other", FieldType::Double)])); map.add_dataset(layer("Cities", vec![("pop", FieldType::Double), ("name", FieldType::Text), ("a:b", FieldType::Double)])); map.add_dataset(layer("Bad {name}", vec![("x", FieldType::Double)])); + map.layers.last_mut().unwrap_or_else(|| panic!("a layer")).name = "cities".into(); let mut other = Map::new("Other"); other.add_dataset(layer("Rivers", vec![("km", FieldType::Double)])); let mut layout = Layout::new("L", 100.0, 100.0); diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 13c643c609..c4814329f5 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -492,9 +492,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu `{mean:Cities:pop}`, `{min:Cities:pop}` and `{max:Cities:pop}` add up, average or pick the least or greatest number of a field, read as a chart reads its values. **Layer statistic** writes them, in a layout text's **Insert Dynamic Text** menu and in **Publish › Text › Dynamic Text** (which - adds a text holding one): a submenu for each layer of the first map frame's map, with **Count** and, under each numeric field, **Sum**, **Mean**, - **Least** and **Greatest** (raster layers, graphics layers and names a token can't hold are not - listed). They follow the layer's + adds a text holding one): a submenu for each layer of the first map frame's map, with **Count** + and, under each numeric field, **Sum**, **Mean**, **Least** and **Greatest** (raster layers, + graphics layers and names a token can't hold are not listed). They follow the layer's definition query (one that no longer compiles keeps every feature, as the map draws it), take the map of the layout's first map frame (in a series, the whole layer, not the page's part of it, so a definition query that reads the series page's values keeps nothing there), and are written as a From ff806c2341f8e86f4e106cf8c7b1edb60a743d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Thu, 26 Nov 2026 12:56:21 -0500 Subject: [PATCH 5/6] fix(layout): a field after another of its name is not offered, since a token reads the first (#3036) --- crates/ui-egui/src/textform.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/textform.rs b/crates/ui-egui/src/textform.rs index 0c55f87915..11a399dbf6 100644 --- a/crates/ui-egui/src/textform.rs +++ b/crates/ui-egui/src/textform.rs @@ -29,7 +29,8 @@ impl StatLayer { /// The layers a text of `layout` can show figures of: those of the map its first frame shows /// (as the page reads them), without a raster's or a graphics layer's, each with its numeric /// fields. A name a token can't hold (`{`, `}`, or a field's `:`) is left out, and so is a layer - /// under another of its name, which no token reads. + /// under another of its name, and a field after another of its name (case aside), which no token + /// reads. pub fn of(layout: &rustgis_engine::map::layout::Layout, maps: &[rustgis_engine::map::Map]) -> Vec { use rustgis_engine::map::layout::ElementKind; let Some(ElementKind::MapFrame { map, .. }) = layout.first_frame().and_then(|f| layout.element(f)).map(|e| &e.kind) else { @@ -43,7 +44,14 @@ impl StatLayer { .filter(|l| map.layer_by_name(&l.name).is_some_and(|first| first.id == l.id)) .map(|l| StatLayer { name: l.name.clone(), - fields: l.dataset.fields.iter().filter(|f| f.ty.is_numeric() && holds(&f.name, true)).map(|f| f.name.clone()).collect(), + fields: l + .dataset + .fields + .iter() + .enumerate() + .filter(|(i, f)| f.ty.is_numeric() && holds(&f.name, true) && l.dataset.field_index(&f.name) == Some(*i)) + .map(|(_, f)| f.name.clone()) + .collect(), }) .collect() } @@ -582,7 +590,17 @@ mod tests { // A token reads the first layer of a name, case aside, so one under it is not offered // (a layer is added above the others). map.add_dataset(layer("Under", vec![("other", FieldType::Double)])); - map.add_dataset(layer("Cities", vec![("pop", FieldType::Double), ("name", FieldType::Text), ("a:b", FieldType::Double)])); + map.add_dataset(layer( + "Cities", + vec![ + ("pop", FieldType::Double), + ("name", FieldType::Text), + ("a:b", FieldType::Double), + // A token reads the first field of a name, case aside: neither of these is offered. + ("Pop", FieldType::Double), + ("Name", FieldType::Double), + ], + )); map.add_dataset(layer("Bad {name}", vec![("x", FieldType::Double)])); map.layers.last_mut().unwrap_or_else(|| panic!("a layer")).name = "cities".into(); let mut other = Map::new("Other"); From 22890a53641412d1fbe581ecf13e8bf8a31454f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Thu, 26 Nov 2026 12:59:28 -0500 Subject: [PATCH 6/6] fix(layout): the layers for the statistic menu are listed only while it is open (#3036) --- crates/ui-egui/src/graphicformat.rs | 2 +- crates/ui-egui/src/layoutview.rs | 4 ++-- crates/ui-egui/src/textform.rs | 13 +++++++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/graphicformat.rs b/crates/ui-egui/src/graphicformat.rs index 6b6f4ba167..d2deadc340 100644 --- a/crates/ui-egui/src/graphicformat.rs +++ b/crates/ui-egui/src/graphicformat.rs @@ -136,7 +136,7 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) { select, }; let settled = - crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("graphic_format", k), &[graphics::TOKENS], &[], fields)) + crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("graphic_format", k), &[graphics::TOKENS], Vec::new, fields)) .unwrap_or_default(); let save = |app: &mut RustGisApp, change: serde_json::Value| { let mut p = json!({"layer": layer, "row": row}); diff --git a/crates/ui-egui/src/layoutview.rs b/crates/ui-egui/src/layoutview.rs index 2a1d6b8f68..f8605eb9cf 100644 --- a/crates/ui-egui/src/layoutview.rs +++ b/crates/ui-egui/src/layoutview.rs @@ -2717,7 +2717,7 @@ fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: usize) { let (mut it, mut al, mut ls, mut fam, mut named) = (*italic, *align, *letter_spacing, *font, font_name.clone()); let (mut wr, mut va, mut line_sp, mut cs) = (*wrap, valign.kept(), *line_spacing, case.kept()); let tokens = [rustgis_engine::map::graphics::TOKENS, rustgis_engine::map::layout::PAGE_TOKENS]; - let layers = crate::textform::StatLayer::of(layout, &app.session.project.maps); + let layers = || crate::textform::StatLayer::of(layout, &app.session.project.maps); let warning = app .layout .text_losses @@ -2744,7 +2744,7 @@ fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: usize) { select: false, }; let settled = - crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("layout_text", li, id), &tokens, &layers, fields)) + crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("layout_text", li, id), &tokens, layers, fields)) .unwrap_or_default(); if settled.typing { app.layout.typing.insert(key, buf); diff --git a/crates/ui-egui/src/textform.rs b/crates/ui-egui/src/textform.rs index 11a399dbf6..201e50990d 100644 --- a/crates/ui-egui/src/textform.rs +++ b/crates/ui-egui/src/textform.rs @@ -160,13 +160,14 @@ pub fn section(ui: &mut Ui, title: &str, add: impl FnOnce(&mut Ui) -> R) -> O pub const LABEL_W: f32 = 96.0; /// The form, its fields told apart by `id` (an element, a dialog). `tokens` are the dynamic text -/// lists the text expands where it is shown, each a section of the Dynamic Text menu; `layers` are -/// the layers whose figures it can show, a section of their own (none for a text graphic). +/// lists the text expands where it is shown, each a section of the Dynamic Text menu; `layers` +/// gives the layers whose figures it can show, a section of their own (none for a text graphic), +/// and is asked only while the menu is open. pub fn show( ui: &mut Ui, id: impl std::hash::Hash + std::fmt::Debug, tokens: &[&[(&str, &str)]], - layers: &[StatLayer], + layers: impl FnOnce() -> Vec, f: Fields<'_>, ) -> Settled { let id = egui::Id::unique(id); @@ -202,7 +203,7 @@ pub fn show( } } } - stat_menu(ui, layers, |token| { + stat_menu(ui, &layers(), |token| { f.text.push_str(&token); out.text = true; }); @@ -506,7 +507,7 @@ mod tests { ui, "form", &[rustgis_engine::map::graphics::TOKENS, rustgis_engine::map::layout::PAGE_TOKENS], - &f.layers, + || f.layers.clone(), Fields { text: &mut f.text, size: &mut size, @@ -602,7 +603,7 @@ mod tests { ], )); map.add_dataset(layer("Bad {name}", vec![("x", FieldType::Double)])); - map.layers.last_mut().unwrap_or_else(|| panic!("a layer")).name = "cities".into(); + map.layers.iter_mut().find(|l| l.name == "Under").unwrap_or_else(|| panic!("a layer")).name = "cities".into(); let mut other = Map::new("Other"); other.add_dataset(layer("Rivers", vec![("km", FieldType::Double)])); let mut layout = Layout::new("L", 100.0, 100.0);