diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index ca8da4349b..e843cae701 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -443,6 +443,18 @@ pub const PUBLICATION_STYLES: &[(&str, &str)] = &[ ("poster", "Poster"), ]; +/// The credit line `layout.publication` writes when `"author"` isn't given (`{date}` is filled +/// with today's date when the layout is drawn). +pub const PUBLICATION_DEFAULT_CREDIT: &str = "Made with RustGIS · {date}"; + +/// The longest text a publication layout takes in one field, in characters. +pub const MAX_PUBLICATION_TEXT: usize = 2000; + +/// The default accent colour of a [`PUBLICATION_STYLES`] style id, or `None` if unknown. +pub fn publication_style_accent(style: &str) -> Option { + Style::parse(style).map(Style::accent) +} + /// A figure's text size, in points at its printed size: journals ask for 6 to 8. const FIGURE_PT: f64 = 7.0; @@ -455,9 +467,6 @@ const GRID_STEPS: &[f64] = &[0.01, 0.02, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0, 10 const LABEL_ROOM_SIDE: f64 = 9.0; const LABEL_ROOM_END: f64 = 6.0; -/// The longest text a publication layout takes in one field, in characters. -const MAX_PUBLICATION_TEXT: usize = 2000; - /// A scale denominator rounded up to two significant figures (1:1,234,567 to 1:1,300,000), so the /// extent fitted at it still fits. pub(crate) fn round_scale(scale: f64) -> f64 { @@ -658,6 +667,18 @@ enum Style { } impl Style { + fn parse(code: &str) -> Option { + match code.trim() { + "classic" => Some(Style::Classic), + "banner" => Some(Style::Banner), + "panel" => Some(Style::Panel), + "editorial" => Some(Style::Editorial), + "modular" => Some(Style::Modular), + "poster" => Some(Style::Poster), + _ => None, + } + } + fn code(self) -> &'static str { match self { Style::Classic => "classic", @@ -1319,7 +1340,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { texts_of(p, "reference")?.unwrap_or_else(|| vec!["Projection: {projection}".into(), "Datum: {datum} · Graticule: {grid}".into()]); // An empty author leaves the line out; none given, the default. let author = match texts_of(p, "author")? { - None => Some("Made with RustGIS · {date}".to_string()), + None => Some(PUBLICATION_DEFAULT_CREDIT.to_string()), Some(t) => Some(t.join(" ")).filter(|t| !t.trim().is_empty()), }; let headings = texts_of(p, "headings")?.unwrap_or_default(); @@ -1336,16 +1357,12 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { return Err(format!("{k} is text: {}", if k == "style" { "a style's name, such as \"panel\"" } else { "a colour as #RRGGBB" })); } } - let style = match ps(p, "style").map(str::trim) { - None | Some("classic") => Style::Classic, - Some("banner") => Style::Banner, - Some("panel") => Style::Panel, - Some("editorial") => Style::Editorial, - Some("modular") => Style::Modular, - Some("poster") => Style::Poster, - Some(x) => { - return Err(format!("style is \"classic\", \"banner\", \"panel\", \"editorial\", \"modular\" or \"poster\", not \"{x}\"")); - } + let style = match ps(p, "style") { + None => Style::Classic, + Some(raw) => Style::parse(raw).ok_or_else(|| { + let x = raw.trim(); + format!("style is \"classic\", \"banner\", \"panel\", \"editorial\", \"modular\" or \"poster\", not \"{x}\"") + })?, }; let accent = color(p, "accent", style.accent())?; let tries = arrangements_to_try(s, p.get("arrangement").unwrap_or(&Value::Null))?; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 8a30906024..bce96d74cf 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -13596,6 +13596,42 @@ fn every_publication_style_in_the_menu_is_taken() { } } +/// The defaults `layout.publication` exposes for the UI match what the command uses: every style +/// in `PUBLICATION_STYLES` has an opaque accent (unknown styles give `None`), omitted `author` +/// writes `PUBLICATION_DEFAULT_CREDIT`, and `MAX_PUBLICATION_TEXT` is the exact character limit. +#[test] +fn publication_defaults_match_the_command() { + use crate::cmd::layout::{MAX_PUBLICATION_TEXT, PUBLICATION_DEFAULT_CREDIT, PUBLICATION_STYLES, publication_style_accent}; + use rustgis_map::layout::ElementKind; + + for (style, _) in PUBLICATION_STYLES { + let accent = publication_style_accent(style).unwrap_or_else(|| panic!("accent for {style}")); + assert_eq!(accent.a(), 255, "{style} accent is opaque"); + } + assert!(publication_style_accent("unknown").is_none()); + + let mut s = world(); + let extent = json!([-46.8, -11.6, -38.5, -2.2]); + // Banner paints its top band in the style's own accent when no accent is given. + let banner_accent = publication_style_accent("banner").unwrap(); + s.run("layout.publication", &json!({"extent": extent, "profile": "a4-portrait", "style": "banner", "title": "Check"})).unwrap(); + let l = s.project.layouts.last().unwrap(); + assert!( + l.elements.iter().any(|e| matches!(e.kind, ElementKind::Rectangle { fill, .. } if fill == banner_accent)), + "banner band uses publication_style_accent(\"banner\")" + ); + assert!( + l.elements.iter().any(|e| matches!(&e.kind, ElementKind::Text { text, .. } if text == PUBLICATION_DEFAULT_CREDIT)), + "omitted author uses PUBLICATION_DEFAULT_CREDIT" + ); + + let at_limit = "a".repeat(MAX_PUBLICATION_TEXT); + assert!(s.run("layout.publication", &json!({"extent": extent, "profile": "a4-portrait", "note": at_limit})).is_ok()); + let over_limit = "a".repeat(MAX_PUBLICATION_TEXT + 1); + let err = s.run("layout.publication", &json!({"extent": extent, "profile": "a4-portrait", "note": over_limit})).unwrap_err(); + assert!(err.contains(&MAX_PUBLICATION_TEXT.to_string()), "{err}"); +} + /// A publication sheet's style (#682): `classic` (the default) as before; `banner` the title and /// subtitle in a band of the accent colour across the top; `panel` the map at the left and a panel /// at the right holding the title, the locator, the legend, the sources and the reference; diff --git a/crates/ui-egui/src/publicationwizard.rs b/crates/ui-egui/src/publicationwizard.rs index f095e8369e..89b191dc51 100644 --- a/crates/ui-egui/src/publicationwizard.rs +++ b/crates/ui-egui/src/publicationwizard.rs @@ -2,7 +2,9 @@ //! then made at once, as the New Layout › Publication Layout menu makes it (#2850). use crate::{RustGisApp, i18n, theme::Tokens}; use rustgis_engine::Session; -use rustgis_engine::cmd::layout::{PUBLICATION_PROFILES, PUBLICATION_STYLES}; +use rustgis_engine::cmd::layout::{ + MAX_PUBLICATION_TEXT, PUBLICATION_DEFAULT_CREDIT, PUBLICATION_PROFILES, PUBLICATION_STYLES, publication_style_accent, +}; use rustgis_engine::map::layout::PAGES; use serde_json::{Value, json}; @@ -194,28 +196,56 @@ fn texts_step(ui: &mut egui::Ui, d: &mut Draft) { egui::Grid::new("publication_wizard_texts").num_columns(2).spacing(egui::vec2(8.0, 8.0)).show(ui, |ui| { let line = |ui: &mut egui::Ui, label: &str, text: &mut String| { let caption = ui.label(label); - ui.add(egui::TextEdit::singleline(text).desired_width(field_width(ui))).labelled_by(caption.id); + ui.add(egui::TextEdit::singleline(text).char_limit(MAX_PUBLICATION_TEXT).desired_width(field_width(ui))) + .labelled_by(caption.id); ui.end_row(); }; let label = ui.label(tl!("Title")); let title = ui - .add(egui::TextEdit::singleline(&mut d.title).hint_text(tl!("No title")).desired_width(field_width(ui))) + .add( + egui::TextEdit::singleline(&mut d.title) + .hint_text(tl!("No title")) + .char_limit(MAX_PUBLICATION_TEXT) + .desired_width(field_width(ui)), + ) .labelled_by(label.id); d.title_typed |= title.changed(); ui.end_row(); line(ui, tl!("Subtitle"), &mut d.subtitle); let label = ui.label(tl!("Note")); - ui.add(egui::TextEdit::multiline(&mut d.note).desired_rows(3).desired_width(field_width(ui))).labelled_by(label.id); + ui.add(egui::TextEdit::multiline(&mut d.note).desired_rows(3).char_limit(MAX_PUBLICATION_TEXT).desired_width(field_width(ui))) + .labelled_by(label.id); ui.end_row(); line(ui, tl!("Sources"), &mut d.sources); let label = ui.label(tl!("Credit line")); - ui.add_enabled(!d.no_author, egui::TextEdit::singleline(&mut d.author).hint_text(default_credit()).desired_width(field_width(ui))) - .labelled_by(label.id); + ui.add_enabled( + !d.no_author, + egui::TextEdit::singleline(&mut d.author) + .hint_text(default_credit()) + .char_limit(MAX_PUBLICATION_TEXT) + .desired_width(field_width(ui)), + ) + .labelled_by(label.id); ui.end_row(); ui.label(""); ui.checkbox(&mut d.no_author, tl!("No credit line")); ui.end_row(); }); + // The boxes take no more, and cut a longer paste to fit: said, not done unseen. + let at_limit = [&d.title, &d.subtitle, &d.note, &d.sources].iter().any(|t| t.chars().count() >= MAX_PUBLICATION_TEXT) + || (!d.no_author && d.author.chars().count() >= MAX_PUBLICATION_TEXT); + if at_limit { + ui.label( + egui::RichText::new(i18n::fmt("Each field takes at most {limit} characters.", &[("limit", &MAX_PUBLICATION_TEXT.to_string())])) + .small() + .weak(), + ); + } +} + +/// A style's default accent as an egui colour, falling back to `fallback` if the style is unknown. +fn style_accent(style: &str, fallback: egui::Color32) -> egui::Color32 { + publication_style_accent(style).map_or(fallback, |c| egui::Color32::from_rgb(c.r(), c.g(), c.b())) } /// The accent step: the style's own colour, or one picked. @@ -223,14 +253,18 @@ fn accent_step(ui: &mut egui::Ui, d: &mut Draft, app_accent: egui::Color32) { let mut ticked = d.accent.is_some(); if ui .checkbox(&mut ticked, tl!("Pick a colour of your own")) - .on_hover_text(tl!("Starts from the colour picked last, or the app's accent colour; the swatch shows the colour the layout takes.")) + .on_hover_text(tl!( + "Starts from the colour picked last, or the style's own accent colour; the swatch shows the colour the layout takes." + )) .changed() { - // Unticked, the colour picked is kept for the next tick. + // Unticked, keep a custom colour for the next tick; leave the style's own unremembered so + // switching styles starts from the new style's accent. + let default = style_accent(&d.style, app_accent); if !ticked { - d.picked = d.accent; + d.picked = d.accent.filter(|&c| c != default); } - d.accent = ticked.then(|| d.picked.unwrap_or(app_accent)); + d.accent = ticked.then(|| d.picked.unwrap_or(default)); } if let Some(c) = &mut d.accent { egui::color_picker::color_edit_button_srgba(ui, c, egui::color_picker::Alpha::Opaque); @@ -268,7 +302,7 @@ fn review_step(ui: &mut egui::Ui, d: &Draft) { let credit = if d.no_author { none.to_string() } else if d.author.trim().is_empty() { - default_credit().to_string() + default_credit() } else { d.author.trim().to_string() }; @@ -281,10 +315,10 @@ fn review_step(ui: &mut egui::Ui, d: &Draft) { } } -/// How the wizard names the credit line `layout.publication` writes when none is given -/// ("Made with RustGIS · {date}", dated when the layout is made). -fn default_credit() -> &'static str { - tl!("Made with RustGIS · today's date") +/// How the wizard names the credit line `layout.publication` writes when none is given, +/// built from the engine's [`PUBLICATION_DEFAULT_CREDIT`] with `{date}` named for display. +fn default_credit() -> String { + i18n::fmt(PUBLICATION_DEFAULT_CREDIT, &[("date", tl_raw!("today's date"))]) } /// A field's width beside its label: 260 points, or what the row leaves once a longer label @@ -697,6 +731,68 @@ mod tests { assert_eq!(h.state().dialogs.publication.as_ref().unwrap().accent, Some(red), "the colour picked before"); } + /// Ticking "Pick a colour of your own" starts from the chosen style's own accent from the + /// engine, not the app's accent; unticking without picking a custom colour lets a newly + /// chosen style start from its own accent too. + #[test] + fn accent_starts_from_the_chosen_styles_own_colour() { + let mut app = app(); + open(&mut app); + let banner = publication_style_accent("banner").unwrap(); + let banner = egui::Color32::from_rgb(banner.r(), banner.g(), banner.b()); + let poster = publication_style_accent("poster").unwrap(); + let poster = egui::Color32::from_rgb(poster.r(), poster.g(), poster.b()); + assert_ne!(banner, poster); + { + let d = app.dialogs.publication.as_mut().unwrap(); + d.style = "banner".into(); + d.step = Step::Accent; + } + let mut h = harness(app); + h.get_by_label("Pick a colour of your own").click(); + h.run_steps(2); + assert_eq!(h.state().dialogs.publication.as_ref().unwrap().accent, Some(banner), "starts from Banner's accent"); + // Unticking without editing leaves no custom colour remembered, so switching to Poster + // starts from Poster's own accent. + h.get_by_label("Pick a colour of your own").click(); + h.run_steps(2); + assert_eq!(h.state().dialogs.publication.as_ref().unwrap().accent, None); + h.state_mut().dialogs.publication.as_mut().unwrap().style = "poster".into(); + h.run_steps(2); + h.get_by_label("Pick a colour of your own").click(); + h.run_steps(2); + assert_eq!(h.state().dialogs.publication.as_ref().unwrap().accent, Some(poster), "starts from Poster's accent"); + } + + /// The text fields enforce the engine's character limit (`MAX_PUBLICATION_TEXT`) and say so + /// when a field reaches it, saying nothing on shorter text. + #[test] + fn texts_enforce_the_engines_character_limit_and_say_so() { + let mut app = app(); + open(&mut app); + app.dialogs.publication.as_mut().unwrap().step = Step::Texts; + let mut h = harness(app); + let limit_text = format!("Each field takes at most {MAX_PUBLICATION_TEXT} characters."); + assert!(h.query_by_label(&limit_text).is_none(), "clean text shows no limit notice"); + // Fill Subtitle to two characters below the limit, then type five more: stopped at the limit. + h.state_mut().dialogs.publication.as_mut().unwrap().subtitle = "x".repeat(MAX_PUBLICATION_TEXT - 2); + h.run_steps(2); + h.get_by_label("Subtitle").focus(); + h.run_steps(1); + h.get_by_label("Subtitle").type_text("abcde"); + h.run_steps(2); + let sub = &h.state().dialogs.publication.as_ref().unwrap().subtitle; + assert_eq!(sub.chars().count(), MAX_PUBLICATION_TEXT, "capped at MAX_PUBLICATION_TEXT"); + assert!(sub.ends_with("xxab"), "kept up to the limit"); + assert!(h.query_by_label(&limit_text).is_some(), "limit notice shown at the cap"); + } + + /// The review's default credit line comes from the engine's `PUBLICATION_DEFAULT_CREDIT`. + #[test] + fn default_credit_matches_the_engines_constant() { + assert_eq!(default_credit(), PUBLICATION_DEFAULT_CREDIT.replace("{date}", "today's date")); + } + /// In English the style step fits its box: the wheel over it scrolls nothing, so the last /// style's description isn't cut at the bottom. #[test]