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
56 changes: 55 additions & 1 deletion crates/ui-egui/src/publicationwizard/accent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The wizard's accent step.
use super::*;

/// The accent step: the style's own colour, or one picked.
/// The accent step: the style's own colour, or one picked, and an edge sheet's paper.
pub(super) fn accent_step(ui: &mut egui::Ui, d: &mut Draft, app_accent: egui::Color32) {
let mut ticked = d.accent.is_some();
if ui
Expand All @@ -26,6 +26,26 @@ pub(super) fn accent_step(ui: &mut egui::Ui, d: &mut Draft, app_accent: egui::Co
} else {
ui.label(egui::RichText::new(tl!("The style's own colour.")).small().weak());
}
if d.edge() {
paper_row(ui, d);
}
}

/// An edge sheet's paper, asked here and not left to Page Color: the command picks the texts' ink
/// from it as it makes the sheet.
fn paper_row(ui: &mut egui::Ui, d: &mut Draft) {
ui.separator();
ui.horizontal(|ui| {
let caption = ui.label(tl!("Paper"));
let mut c = d.paper.unwrap_or(egui::Color32::WHITE);
let swatch = egui::color_picker::color_edit_button_srgba(ui, &mut c, egui::color_picker::Alpha::Opaque).labelled_by(caption.id);
if swatch.changed() {
d.paper = Some(c);
}
if ui.add_enabled(d.paper_sent().is_some(), egui::Button::new(tl!("White"))).clicked() {
d.paper = None;
}
});
}

#[cfg(test)]
Expand Down Expand Up @@ -154,4 +174,38 @@ mod tests {
h.run_steps(2);
assert_eq!(h.state().dialogs.publication.as_ref().unwrap().accent, Some(red), "custom colour kept");
}

/// An edge sheet is asked its paper, white until one is picked; White goes back to it. No
/// other style is asked.
#[test]
fn only_an_edge_sheet_is_asked_its_paper() {
use egui::accesskit::Role;
let dark = egui::Color32::from_rgb(0x1B, 0x24, 0x30);
let mut app = app();
open(&mut app);
{
let d = app.dialogs.publication.as_mut().unwrap();
d.step = Step::Accent;
d.style = "edge".into();
}
let mut h = harness(app);
let d = |h: &egui_kittest::Harness<'static, RustGisApp>| h.state().dialogs.publication.as_ref().unwrap().params();
assert!(h.query_by_role_and_label(Role::ColorWell, "Paper").is_some());
assert!(d(&h).get("paper").is_none(), "white");
h.state_mut().dialogs.publication.as_mut().unwrap().paper = Some(dark);
h.run_steps(4);
assert_eq!(d(&h)["paper"], "#1B2430");
click(&mut h, "White");
assert!(d(&h).get("paper").is_none());
h.state_mut().dialogs.publication.as_mut().unwrap().paper = Some(egui::Color32::WHITE);
h.run_steps(4);
assert!(d(&h).get("paper").is_none(), "white picked by hand");
h.state_mut().dialogs.publication.as_mut().unwrap().paper = Some(dark);
for style in ["classic", "banner", "panel", "editorial", "modular", "poster"] {
h.state_mut().dialogs.publication.as_mut().unwrap().style = style.into();
h.run_steps(4);
assert!(h.query_by_role_and_label(Role::ColorWell, "Paper").is_none(), "{style}");
assert!(d(&h).get("paper").is_none(), "{style}");
}
}
}
53 changes: 52 additions & 1 deletion crates/ui-egui/src/publicationwizard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ enum LeftOut {
NoSources,
NoReference,
NotEditorial,
NotEdge,
NoFigures,
}

/// A text field of the wizard that takes a layer statistic.
Expand All @@ -76,13 +78,18 @@ enum Field {
Author,
Reference,
Heading(usize),
Kicker,
/// A headline figure's value and its label, by the figure's place.
FigureValue(usize),
FigureLabel(usize),
}

impl Field {
/// The sheet's step that asks for it.
fn step(self) -> Step {
match self {
Field::Title | Field::Subtitle | Field::Note | Field::Author | Field::Heading(2) => Step::Texts,
Field::Kicker | Field::FigureValue(_) | Field::FigureLabel(_) => Step::Texts,
Field::Sources | Field::Reference | Field::Heading(_) => Step::Sources,
}
}
Expand All @@ -109,6 +116,12 @@ pub struct Draft {
pub title: String,
/// Whether the title was typed; until then it is the active map's name, as the menu's.
pub title_typed: bool,
/// An edge sheet's short line over its title.
pub kicker: String,
/// The headline figures of an edge or panel sheet, each a value and its label.
pub figures: [(String, String); 3],
/// An edge sheet's paper, or `None` for white, as `layout.page` keeps it.
pub paper: Option<egui::Color32>,
pub subtitle: String,
pub note: String,
pub sources: String,
Expand Down Expand Up @@ -156,6 +169,9 @@ impl Draft {
style: "classic".into(),
title: s.map().name.clone(),
title_typed: false,
kicker: String::new(),
figures: Default::default(),
paper: None,
subtitle: String::new(),
note: String::new(),
sources: String::new(),
Expand Down Expand Up @@ -245,6 +261,19 @@ impl Draft {
p[key] = json!(text.trim());
}
}
if self.left_out(Field::Kicker).is_none() && !self.kicker.trim().is_empty() {
p["kicker"] = json!(self.kicker.trim());
}
let figures: Vec<Value> = self
.figures_sent()
.map(|(value, label)| if label.is_empty() { json!({"value": value}) } else { json!({"value": value, "label": label}) })
.collect();
if !figures.is_empty() {
p["figures"] = json!(figures);
}
if let Some(c) = self.paper_sent() {
p["paper"] = json!(hex(c));
}
if self.no_author {
p["author"] = json!("");
}
Expand Down Expand Up @@ -277,21 +306,40 @@ impl Draft {
self.style == "editorial"
}

fn edge(&self) -> bool {
self.style == "edge"
}

/// The headline figures sent, in their order: those with a value, each trimmed, none for a
/// sheet that draws none.
fn figures_sent(&self) -> impl Iterator<Item = (&str, &str)> {
let drawn = self.left_out(Field::FigureValue(0)).is_none();
self.figures.iter().map(|(value, label)| (value.trim(), label.trim())).filter(move |(value, _)| drawn && !value.is_empty())
}

/// The paper sent: the one picked, on an edge sheet; white is left to the command.
fn paper_sent(&self) -> Option<egui::Color32> {
self.paper.filter(|&c| self.edge() && c != egui::Color32::WHITE)
}

/// Why the sheet has no place for `field`'s text, if it hasn't: what is sent, what the review
/// says isn't used and which boxes are off all follow from this.
fn left_out(&self, field: Field) -> Option<LeftOut> {
match field {
Field::Heading(0) if lines(&self.sources).next().is_none() => Some(LeftOut::NoSources),
Field::Reference | Field::Heading(1) if self.no_reference => Some(LeftOut::NoReference),
Field::Heading(2) if !self.editorial() => Some(LeftOut::NotEditorial),
Field::Kicker if !self.edge() => Some(LeftOut::NotEdge),
Field::FigureValue(_) | Field::FigureLabel(_) if !self.edge() && self.style != "panel" => Some(LeftOut::NoFigures),
_ => None,
}
}

/// Whether `field`'s box is switched off or not shown. The sources' heading stays on with no
/// sources, which may be typed after it.
fn off(&self, field: Field) -> bool {
field == Field::Author && self.no_author || matches!(self.left_out(field), Some(LeftOut::NoReference | LeftOut::NotEditorial))
field == Field::Author && self.no_author
|| matches!(self.left_out(field), Some(LeftOut::NoReference | LeftOut::NotEditorial | LeftOut::NotEdge | LeftOut::NoFigures))
}

fn field_mut(&mut self, field: Field) -> &mut String {
Expand All @@ -306,6 +354,9 @@ impl Draft {
Field::Heading(0) => sources,
Field::Heading(1) => reference,
Field::Heading(_) => column,
Field::Kicker => &mut self.kicker,
Field::FigureValue(at) => &mut self.figures[at].0,
Field::FigureLabel(at) => &mut self.figures[at].1,
}
}

Expand Down
111 changes: 108 additions & 3 deletions crates/ui-egui/src/publicationwizard/review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ pub(super) fn review_step(ui: &mut egui::Ui, s: &Session, d: &Draft) {
row(tl!("Style"), style);
row(tl!("Elements"), elements_value(s, d));
placement_rows(&mut row, s, d);
kicker_row(&mut row, d);
row(tl!("Title"), or_none(&d.title));
row(tl!("Subtitle"), or_none(&d.subtitle));
row(tl!("Note"), or_none(&d.note));
figures_row(&mut row, d);
let sources = lines(&d.sources).collect::<Vec<_>>().join("\n");
row(tl!("Sources"), or_none(&sources));
heading_row(&mut row, d, 0, tl!("Sources heading"), PUBLICATION_HEADINGS[0]);
Expand All @@ -45,6 +47,7 @@ pub(super) fn review_step(ui: &mut egui::Ui, s: &Session, d: &Draft) {
row(tl!("Credit line"), credit_value(d));
let accent = d.accent.map(hex).unwrap_or_else(|| tl!("The style's own").to_string());
row(tl!("Accent"), accent);
paper_review_row(&mut row, d);
});
if d.figure() {
figure_note(ui);
Expand Down Expand Up @@ -94,13 +97,75 @@ fn heading_row(row: &mut impl FnMut(&str, String), d: &Draft, at: usize, label:
None if typed.is_empty() => or.to_string(),
None => typed.to_string(),
Some(_) if typed.is_empty() => return,
Some(LeftOut::NoSources) => i18n::fmt("{heading} (not used: no sources)", &[("heading", typed)]),
Some(LeftOut::NoReference) => i18n::fmt("{heading} (not used: no reference)", &[("heading", typed)]),
Some(LeftOut::NotEditorial) => i18n::fmt("{heading} (not used: not an editorial sheet)", &[("heading", typed)]),
Some(why) => not_used(why, typed),
};
row(label, value);
}

/// `typed`, said not to be used and why, rather than dropped unseen.
fn not_used(why: LeftOut, typed: &str) -> String {
let text = [("text", typed)];
// Each template at its own call, where catalog extraction sees it.
match why {
LeftOut::NoSources => i18n::fmt("{text} (not used: no sources)", &text),
LeftOut::NoReference => i18n::fmt("{text} (not used: no reference)", &text),
LeftOut::NotEditorial => i18n::fmt("{text} (not used: not an editorial sheet)", &text),
LeftOut::NotEdge => i18n::fmt("{text} (not used: not an edge sheet)", &text),
LeftOut::NoFigures => i18n::fmt("{text} (not used: not an edge or panel sheet)", &text),
}
}

/// The kicker: its text on an edge sheet; on another, said not to be used, and no row with nothing
/// typed.
fn kicker_row(row: &mut impl FnMut(&str, String), d: &Draft) {
let typed = d.kicker.trim();
let value = match d.left_out(Field::Kicker) {
None if typed.is_empty() => tl!("(none)").to_string(),
None => typed.to_string(),
Some(_) if typed.is_empty() => return,
Some(why) => not_used(why, typed),
};
row(tl!("Kicker"), value);
}

/// The headline figures, one per line, as `kicker_row` says the kicker; a label typed with no value
/// is said not to be used.
fn figures_row(row: &mut impl FnMut(&str, String), d: &Draft) {
let left_out = d.left_out(Field::FigureValue(0));
let typed: Vec<String> = d
.figures
.iter()
.map(|(value, label)| (value.trim(), label.trim()))
.filter_map(|(value, label)| match (value.is_empty(), label.is_empty()) {
(true, true) => None,
(false, true) => Some(value.to_string()),
(false, false) => Some(format!("{value} · {label}")),
(true, false) if left_out.is_some() => Some(label.to_string()),
(true, false) => Some(i18n::fmt("{label} (label without a value isn't used)", &[("label", label)])),
})
.collect();
let typed = typed.join("\n");
let value = match left_out {
None if typed.is_empty() => tl!("(none)").to_string(),
None => typed,
Some(_) if typed.is_empty() => return,
// Each line says it: said once, it would read as the last figure's alone.
Some(why) => typed.lines().map(|line| not_used(why, line)).collect::<Vec<_>>().join("\n"),
};
row(tl!("Figures"), value);
}

/// An edge sheet's paper; one picked for another style is said not to be used.
fn paper_review_row(row: &mut impl FnMut(&str, String), d: &Draft) {
let value = match (d.paper_sent(), d.paper.filter(|&c| c != egui::Color32::WHITE)) {
(Some(c), _) => hex(c),
_ if d.edge() => tl!("White").to_string(),
(None, Some(c)) => not_used(LeftOut::NotEdge, &hex(c)),
(None, None) => return,
};
row(tl!("Paper"), value);
}

#[cfg(test)]
mod tests {
use super::harness::*;
Expand Down Expand Up @@ -152,4 +217,44 @@ mod tests {
assert_eq!(none, others + usize::from(listed == "(none)"), "(none): {author:?}, off {no_author}");
}
}

/// The review lists the kicker, figures and paper a sheet draws, says those typed that its
/// style doesn't, and lists none of them where there is nothing to say.
#[test]
fn the_review_lists_the_kicker_figures_and_paper() {
let mut app = app();
open(&mut app);
app.dialogs.publication.as_mut().unwrap().step = Step::Review;
let mut h = harness(app);
for key in ["Kicker", "Figures", "Paper"] {
assert!(h.query_by_label(key).is_none(), "{key} on an empty classic sheet");
}
{
let d = h.state_mut().dialogs.publication.as_mut().unwrap();
d.style = "edge".into();
}
h.run_steps(4);
for key in ["Kicker", "Figures", "Paper", "White"] {
assert!(h.query_by_label(key).is_some(), "{key} on an empty edge sheet");
}
{
let d = h.state_mut().dialogs.publication.as_mut().unwrap();
d.kicker = " Census ".into();
d.figures = [(" 12 ".into(), "towns".into()), (String::new(), "lost".into()), ("40".into(), String::new())];
d.paper = Some(egui::Color32::from_rgb(0x1B, 0x24, 0x30));
}
h.run_steps(4);
for text in ["Census", "12 · towns\nlost (label without a value isn't used)\n40", "#1B2430"] {
assert!(h.query_by_label(text).is_some(), "{text}");
}
h.state_mut().dialogs.publication.as_mut().unwrap().style = "classic".into();
h.run_steps(4);
for text in [
"Census (not used: not an edge sheet)",
"12 · towns (not used: not an edge or panel sheet)\nlost (not used: not an edge or panel sheet)\n40 (not used: not an edge or panel sheet)",
"#1B2430 (not used: not an edge sheet)",
] {
assert!(h.query_by_label(text).is_some(), "{text}");
}
}
}
12 changes: 6 additions & 6 deletions crates/ui-egui/src/publicationwizard/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ fn heading(ui: &mut egui::Ui, d: &mut Draft, label: &str, at: usize) {
}

#[cfg(test)]
mod tests {
pub(super) mod tests {
use super::harness::*;
use super::*;
use egui::accesskit::Role;
use egui_kittest::kittest::{NodeT, Queryable};
use rustgis_engine::map::layout::ElementKind;

type Wizard = egui_kittest::Harness<'static, RustGisApp>;
pub(in crate::publicationwizard) type Wizard = egui_kittest::Harness<'static, RustGisApp>;

fn draft() -> Draft {
Draft::new(&app().session)
}

/// The wizard on `step`, over the sample project (its map has vector layers).
fn at(step: Step, set: impl FnOnce(&mut Draft)) -> Wizard {
pub(in crate::publicationwizard) fn at(step: Step, set: impl FnOnce(&mut Draft)) -> Wizard {
let mut app = app();
open(&mut app);
let d = app.dialogs.publication.as_mut().unwrap();
Expand All @@ -92,12 +92,12 @@ mod tests {
harness(app)
}

fn wizard(h: &Wizard) -> &Draft {
pub(in crate::publicationwizard) fn wizard(h: &Wizard) -> &Draft {
h.state().dialogs.publication.as_ref().expect("the wizard is open")
}

/// The first vector layer of the active map, which the statistic menu lists.
fn layer(h: &Wizard) -> String {
pub(in crate::publicationwizard) fn layer(h: &Wizard) -> String {
crate::textform::StatLayer::of_map(h.state().session.map()).first().expect("a vector layer").name.clone()
}

Expand All @@ -107,7 +107,7 @@ mod tests {
}

/// Insert Statistic › the first layer › Count.
fn insert_count(h: &mut Wizard) -> String {
pub(in crate::publicationwizard) fn insert_count(h: &mut Wizard) -> String {
let name = layer(h);
let token = format!("{{count:{name}}}");
for label in ["Insert Statistic", &name, &format!("Count {token}")] {
Expand Down
Loading
Loading