Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rgba> {
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;

Expand All @@ -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 {
Expand Down Expand Up @@ -658,6 +667,18 @@ enum Style {
}

impl Style {
fn parse(code: &str) -> Option<Self> {
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",
Expand Down Expand Up @@ -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();
Expand All @@ -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))?;
Expand Down
36 changes: 36 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
126 changes: 111 additions & 15 deletions crates/ui-egui/src/publicationwizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -194,43 +196,75 @@ 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.
fn accent_step(ui: &mut egui::Ui, d: &mut Draft, app_accent: egui::Color32) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The credit line's English template is no longer a wrapped literal.

The old code was tl!("Made with RustGIS · today's date"), so the whole line could be translated. Now "Made with RustGIS · {date}" comes from a const in the engine and reaches i18n::fmt unwrapped. The catalog extraction (see docs/i18n.md) only sees tl!/tl_raw! literals, so this key never gets a catalog entry and the line stays English in every language. The wizard should keep the template wrapped in its own tl_raw!, with a test (the existing default_credit_matches_the_engines_constant) tying it to PUBLICATION_DEFAULT_CREDIT, so the translator can see the wording.

Category: agents-md

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale colour remembered after a style change while the box is ticked (about 70% confident).

style_step changes d.style but leaves d.accent alone. So with Banner selected, ticking the box sets accent to Banner's brown. If the user then picks Poster while the box is still ticked, accent stays Banner's brown. Unticking then runs d.picked = d.accent.filter(|&c| c != default) with Poster's default, which keeps Banner's brown as a "custom" colour. The next time the box is ticked, d.picked.unwrap_or(default) gives Banner's colour rather than the new style's accent. That is the stale-colour case this PR says it fixes, and the new test accent_starts_from_the_chosen_styles_own_colour does not cover a style change while ticked.

A possible fix is to re-seed accent when the style changes, for example if accent equals the previous style's default, set it to the new style's default. Also add a test for that path.

Category: bug

}
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);
Expand Down Expand Up @@ -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()
};
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
Loading