diff --git a/crates/engine/src/cmd/layout/publication/arrangement.rs b/crates/engine/src/cmd/layout/publication/arrangement.rs index d0a77a4e2f..3bf9beff2e 100644 --- a/crates/engine/src/cmd/layout/publication/arrangement.rs +++ b/crates/engine/src/cmd/layout/publication/arrangement.rs @@ -141,7 +141,7 @@ pub(in crate::cmd::layout) fn arrangement_label(v: &Value, homes: Homes, saved: } /// The corners of an arrangement, by surround. -pub(in crate::cmd::layout) fn corners_of(homes: Homes) -> Value { +pub fn corners_of(homes: Homes) -> Value { Value::Object(ARRANGEMENT_KEYS.iter().zip(homes).map(|(k, h)| ((*k).to_string(), json!(CORNER_NAMES[h]))).collect()) } diff --git a/crates/engine/src/cmd/layout/publication/mod.rs b/crates/engine/src/cmd/layout/publication/mod.rs index 9acd033c4f..ac19e98700 100644 --- a/crates/engine/src/cmd/layout/publication/mod.rs +++ b/crates/engine/src/cmd/layout/publication/mod.rs @@ -62,7 +62,7 @@ pub(in crate::cmd::layout) fn landscape_suits(aspect: f64, short: f64, long: f64 // An edge sheet's map takes the page's whole width. let sides = if style == Style::Edge { 0.0 } else { 30.0 * k }; let (iw, ih) = (w - sides, h - texts); - let fw = if matches!(style, Style::Panel | Style::Editorial | Style::Modular) { 0.68 * iw } else { iw }; + let fw = if style.has_column() { 0.68 * iw } else { iw }; let (fw, fh) = (fw - 2.0 * LABEL_ROOM_SIDE * k, ih - 2.0 * LABEL_ROOM_END * k); fw.min(fh / aspect.max(1e-6)) }; diff --git a/crates/engine/src/cmd/layout/publication/sheet.rs b/crates/engine/src/cmd/layout/publication/sheet.rs index a07ee14a6d..e49a7bbe3f 100644 --- a/crates/engine/src/cmd/layout/publication/sheet.rs +++ b/crates/engine/src/cmd/layout/publication/sheet.rs @@ -79,6 +79,12 @@ pub fn publication_style_accent(style: &str) -> Option { Style::parse(style).map(Style::accent) } +/// Whether a [`PUBLICATION_STYLES`] style id keeps the locator and legend in a column beside the +/// map, leaving only the north arrow and scale bar to the frame's corners. +pub fn publication_style_has_column(style: &str) -> bool { + Style::parse(style).is_some_and(Style::has_column) +} + /// How a publication sheet is arranged around its map. #[derive(Clone, Copy, PartialEq, Debug)] pub(in crate::cmd::layout) enum Style { @@ -144,6 +150,11 @@ impl Style { } } + /// Whether it has a column beside the map for the locator and legend. + pub(in crate::cmd::layout) fn has_column(self) -> bool { + matches!(self, Style::Panel | Style::Editorial | Style::Modular) + } + /// Whether its surrounds sit on cards with rounded corners. pub(in crate::cmd::layout) fn cards(self) -> bool { matches!(self, Style::Modular | Style::Poster) diff --git a/crates/ui-egui/src/publicationwizard/accent.rs b/crates/ui-egui/src/publicationwizard/accent.rs index 10bb5c2b01..a1b1c5680f 100644 --- a/crates/ui-egui/src/publicationwizard/accent.rs +++ b/crates/ui-egui/src/publicationwizard/accent.rs @@ -113,6 +113,7 @@ mod tests { click(&mut h, "Back"); click(&mut h, "Back"); click(&mut h, "Back"); + click(&mut h, "Back"); h.get_by_label("Poster").click(); h.run_steps(2); assert_eq!(h.state().dialogs.publication.as_ref().unwrap().accent, Some(poster), "re-seeded to Poster"); @@ -120,17 +121,20 @@ mod tests { click(&mut h, "Next"); click(&mut h, "Next"); click(&mut h, "Next"); + click(&mut h, "Next"); 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); click(&mut h, "Back"); click(&mut h, "Back"); click(&mut h, "Back"); + click(&mut h, "Back"); h.get_by_label("Editorial").click(); h.run_steps(2); click(&mut h, "Next"); click(&mut h, "Next"); click(&mut h, "Next"); + click(&mut h, "Next"); 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(editorial), "starts from Editorial"); @@ -140,6 +144,7 @@ mod tests { click(&mut h, "Back"); click(&mut h, "Back"); click(&mut h, "Back"); + click(&mut h, "Back"); h.get_by_label("Banner").click(); h.run_steps(2); assert_eq!(h.state().dialogs.publication.as_ref().unwrap().accent, Some(red), "custom colour kept"); diff --git a/crates/ui-egui/src/publicationwizard/arrange.rs b/crates/ui-egui/src/publicationwizard/arrange.rs new file mode 100644 index 0000000000..688366712d --- /dev/null +++ b/crates/ui-egui/src/publicationwizard/arrange.rs @@ -0,0 +1,506 @@ +//! The wizard's placement step: which corner each surround takes. +use super::*; +use rustgis_engine::cmd::layout::{ + ARRANGEMENTS, Homes, corners_of, parse_arrangement, publication_style_has_column, usable_saved_arrangement, +}; + +/// The arrangement `layout.publication` is asked for. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Arrangement { + /// The command's default. + Best, + Favourite, + /// A built-in or saved arrangement, by name. + Named(String), + /// A corner for each surround. + Custom(Homes), +} + +impl Arrangement { + /// The command's `"arrangement"`, or `None` to leave it to the command. + pub(super) fn value(&self) -> Option { + match self { + Arrangement::Best => None, + Arrangement::Favourite => Some(json!("favourite")), + Arrangement::Named(name) => Some(json!(name)), + Arrangement::Custom(homes) => Some(corners_of(*homes)), + } + } + + /// What making this the favourite saves: an arrangement chosen by name or corner by corner, + /// one that still holds; `best` and the favourite itself aren't one. + pub(super) fn as_favourite(&self, s: &Session) -> Option { + let chosen = matches!(self, Arrangement::Named(_) | Arrangement::Custom(_)) && self.held(s).is_ok(); + chosen.then(|| self.value()).flatten() + } + + /// Its name, as the review says it: none for one that no longer holds. + pub(super) fn label(&self, s: &Session) -> String { + match self { + _ if self.held(s).is_err() => tl!("(none)").to_string(), + Arrangement::Best => tl!("Best").to_string(), + Arrangement::Favourite => tl!("Favourite").to_string(), + Arrangement::Named(name) => i18n::display(built_in(name).unwrap_or(name)), + Arrangement::Custom(_) => tl!("Custom").to_string(), + } + } + + /// The corners it gives now, `None` for `best`, which the command picks against the map; or + /// why it no longer holds (a saved one removed meanwhile): the one check the step, the review + /// and Create share. + pub(super) fn held(&self, s: &Session) -> Result, &'static str> { + match self { + Arrangement::Best => Ok(None), + Arrangement::Favourite => Ok(Some(favourite_homes(s))), + Arrangement::Named(name) => parse_arrangement(&json!(name), &s.layout_arrangements) + .map(Some) + .map_err(|_| tl!("The arrangement picked is no longer saved: pick the placement again.")), + Arrangement::Custom(homes) => Ok(Some(*homes)), + } + } +} + +/// A built-in arrangement's name as the step shows it, in logical order. +fn built_in(name: &str) -> Option<&'static str> { + let labels = [tl_raw!("Classic"), tl_raw!("Mirrored"), tl_raw!("Legend right"), tl_raw!("Legend top")]; + ARRANGEMENTS.iter().zip(labels).find(|((n, _), _)| n.eq_ignore_ascii_case(name.trim())).map(|(_, label)| label) +} + +/// The favourite's corners; one that can't be read is `classic`. +fn favourite_homes(s: &Session) -> Homes { + parse_arrangement(&s.layout_favourite, &s.layout_arrangements).unwrap_or(ARRANGEMENTS[0].1) +} + +/// The favourite as its option names it. +fn favourite_label(s: &Session) -> String { + let name = s.layout_favourite.as_str().map_or(tl_raw!("custom corners"), |n| built_in(n).unwrap_or(n)); + i18n::fmt("Favourite: {name}", &[("name", name)]) +} + +/// The corners' names, in [`Homes`]'s numbering. +fn corner_labels() -> [&'static str; 4] { + [tl_raw!("Top left"), tl_raw!("Top right"), tl_raw!("Bottom left"), tl_raw!("Bottom right")] +} + +/// The locator's place in [`Homes`]. +const LOCATOR: usize = 0; + +/// The locator and the legend, by their place in [`Homes`]: what a column style keeps in its +/// column. +const IN_COLUMN: [usize; 2] = [LOCATOR, 2]; + +/// The surrounds' names, in [`Homes`]'s order. +fn surround_labels() -> [&'static str; 4] { + [tl_raw!("Locator"), tl_raw!("North arrow"), tl_raw!("Legend"), tl_raw!("Scale bar")] +} + +/// The placement step: the arrangements to pick from, and what goes in each corner. +pub(super) fn arrange_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { + let mut option = |ui: &mut egui::Ui, choice: Arrangement, label: &str| { + let response = ui.radio(d.arrangement == choice, label); + if response.clicked() { + d.arrangement = choice; + } + response + }; + ui.horizontal_wrapped(|ui| { + option(ui, Arrangement::Best, tl!("Best")) + .on_hover_text(tl!("The arrangement that moves the surrounds least off their corners and covers the map least.")); + option(ui, Arrangement::Favourite, &favourite_label(s)); + }); + ui.horizontal_wrapped(|ui| { + for (name, _) in ARRANGEMENTS { + let label = i18n::display(built_in(name).unwrap_or(name)); + option(ui, Arrangement::Named(name.into()), &label); + } + // Those saved with `layout.arrangement` that this version reads, under their own names. + for (name, _) in s.layout_arrangements.iter().filter(|(n, c)| usable_saved_arrangement(n, c)) { + option(ui, Arrangement::Named(name.clone()), &i18n::display(name)); + } + }); + let custom = matches!(d.arrangement, Arrangement::Custom(_)); + if ui.radio(custom, tl!("Custom")).clicked() && !custom { + let from = d.arrangement.held(s).ok().flatten().unwrap_or_else(|| favourite_homes(s)); + d.arrangement = Arrangement::Custom(from); + } + // What `cmd/layout/publication` puts in no corner, whatever the arrangement: the locator and + // legend of a style with a column, which sit in it, and a figure's locator, which it doesn't + // draw unless asked (the wizard doesn't ask). + let figure = d.figure(); + let column = !figure && publication_style_has_column(&d.style); + let cornered = |surround: usize| !(column && IN_COLUMN.contains(&surround) || figure && surround == LOCATOR); + if let Arrangement::Custom(homes) = &mut d.arrangement { + corner_pickers(ui, homes, cornered); + } + if let Ok(Some(homes)) = d.arrangement.held(s) { + ui.add_space(4.0); + egui::Grid::new("publication_wizard_corners").num_columns(2).spacing(egui::vec2(24.0, 6.0)).show(ui, |ui| { + for (corner, name) in corner_labels().into_iter().enumerate() { + match homes.iter().position(|&h| h == corner).filter(|&s| cornered(s)) { + Some(surround) => { + let values = [("corner", name), ("surround", surround_labels()[surround])]; + ui.group(|ui| ui.label(i18n::fmt("{corner}: {surround}", &values))); + } + // Its cell is kept, so the others stay at their side. + None => { + ui.label(""); + } + } + if corner % 2 == 1 { + ui.end_row(); + } + } + }); + } + let note = if figure { + Some(tl!("A figure draws no locator.")) + } else { + column.then(|| tl!("The locator and legend sit in this style's column: only the north arrow and scale bar take these corners.")) + }; + if let Some(note) = note { + ui.label(egui::RichText::new(note).small().weak()); + } + ui.add_space(4.0); + ui.add_enabled_ui(d.arrangement.as_favourite(s).is_some(), |ui| { + ui.checkbox(&mut d.make_favourite, tl!("Make this my favourite")) + .on_hover_text(tl!("Create also keeps it with the app's preferences as the favourite arrangement.")); + }); +} + +/// A corner for each surround; one picked that another has swaps the two, so each keeps a corner +/// of its own. A surround that takes no corner on this sheet has its picker off, still showing the +/// one it holds for the sheets and the favourite where it does. +fn corner_pickers(ui: &mut egui::Ui, homes: &mut Homes, cornered: impl Fn(usize) -> bool) { + egui::Grid::new("publication_wizard_custom").num_columns(2).spacing(egui::vec2(8.0, 6.0)).show(ui, |ui| { + for (surround, name) in surround_labels().into_iter().enumerate() { + let label = ui.label(i18n::display(name)); + let corners = corner_labels(); + ui.add_enabled_ui(cornered(surround), |ui| { + egui::ComboBox::from_id_salt(("publication_wizard_corner", surround)) + .selected_text(i18n::display(corners[homes[surround]])) + .width(field_width(ui)) + .show_ui(ui, |ui| { + for (corner, name) in corners.into_iter().enumerate() { + if ui.selectable_label(homes[surround] == corner, i18n::display(name)).clicked() + && let Some(other) = homes.iter().position(|&h| h == corner) + { + homes.swap(surround, other); + } + } + }) + .response + .labelled_by(label.id); + }); + ui.end_row(); + } + }); +} + +#[cfg(test)] +mod tests { + use super::super::harness::*; + use super::*; + use egui::accesskit::Role; + use egui_kittest::kittest::{NodeT, Queryable}; + use rustgis_engine::cmd::layout::{ARRANGEMENTS, parse_arrangement}; + + type Wizard = egui_kittest::Harness<'static, RustGisApp>; + + /// The wizard on its placement step. + fn placement(prepare: impl FnOnce(&mut RustGisApp)) -> Wizard { + let mut app = app(); + open(&mut app); + app.dialogs.publication.as_mut().unwrap().step = Step::Placement; + prepare(&mut app); + harness(app) + } + + fn draft(h: &Wizard) -> &Draft { + h.state().dialogs.publication.as_ref().expect("the wizard is open") + } + + fn pick(h: &mut Wizard, label: &str) { + h.get_by_role_and_label(Role::RadioButton, label).click(); + h.run_steps(4); + } + + /// What the 2 × 2 grid says, corner by corner. + fn shown(h: &Wizard, text: &str) -> bool { + h.query_by_label(text).is_some() + } + + /// A new draft asks for no arrangement, as before the step, and makes no favourite. + #[test] + fn a_new_draft_leaves_the_arrangement_to_the_command() { + let app = app(); + let d = Draft::new(&app.session); + assert_eq!((d.arrangement.clone(), d.make_favourite), (Arrangement::Best, false)); + assert!(d.params().get("arrangement").is_none()); + } + + /// A sheet and a figure both send the arrangement chosen. + #[test] + fn the_arrangement_chosen_is_sent_for_a_sheet_and_a_figure() { + let app = app(); + for size in ["a4", "figure-1col"] { + let mut d = Draft::new(&app.session); + d.size = size.into(); + d.arrangement = Arrangement::Named("mirrored".into()); + assert_eq!(d.params()["arrangement"], "mirrored", "{size}"); + d.arrangement = Arrangement::Favourite; + assert_eq!(d.params()["arrangement"], "favourite", "{size}"); + } + } + + /// Each arrangement by name sends its name and shows its corners. + #[test] + fn each_built_in_arrangement_sends_its_name_and_shows_its_corners() { + let mut h = placement(|_| {}); + for ((name, homes), label) in ARRANGEMENTS.iter().zip(["Classic", "Mirrored", "Legend right", "Legend top"]) { + pick(&mut h, label); + assert_eq!(draft(&h).params()["arrangement"], *name); + for (surround, corner) in ["Locator", "North arrow", "Legend", "Scale bar"].iter().zip(homes) { + let corner = ["Top left", "Top right", "Bottom left", "Bottom right"][*corner]; + assert!(shown(&h, &format!("{corner}: {surround}")), "{name}: {surround} at {corner}"); + } + } + } + + /// The favourite is named: classic unset, a name, a saved one, or corners of its own. + #[test] + fn the_favourite_is_listed_with_its_name() { + let h = placement(|_| {}); + assert!(h.query_by_role_and_label(Role::RadioButton, "Favourite: Classic").is_some()); + let own = json!({"locator": "bottom-right", "northArrow": "top-left", "legend": "top-right", "scaleBar": "bottom-left"}); + for (favourite, label) in [(json!("mirrored"), "Favourite: Mirrored"), (own, "Favourite: custom corners")] { + let mut h = placement(|app| app.session.layout_favourite = favourite); + pick(&mut h, label); + assert_eq!(draft(&h).params()["arrangement"], "favourite"); + } + } + + /// Each saved arrangement is listed under its name; with none saved, there is nothing more. + #[test] + fn saved_arrangements_are_listed_by_name() { + let corners = json!({"locator": "bottom-right", "northArrow": "top-left", "legend": "top-right", "scaleBar": "bottom-left"}); + let h = placement(|_| {}); + assert!(h.query_by_role_and_label(Role::RadioButton, "Atlas").is_none()); + let mut h = placement(|app| { + app.session.layout_arrangements.insert("Atlas".into(), corners.clone()); + }); + pick(&mut h, "Atlas"); + assert_eq!(draft(&h).params()["arrangement"], "Atlas"); + assert!(shown(&h, "Top right: Legend")); + } + + /// Custom starts from the corners of what was selected: the favourite's for Best and + /// Favourite. + #[test] + fn custom_starts_from_the_corners_selected() { + let mut h = placement(|app| app.session.layout_favourite = json!("mirrored")); + pick(&mut h, "Custom"); + assert_eq!(draft(&h).arrangement, Arrangement::Custom(ARRANGEMENTS[1].1), "from Best: the favourite's"); + pick(&mut h, "Legend top"); + pick(&mut h, "Custom"); + assert_eq!(draft(&h).arrangement, Arrangement::Custom(ARRANGEMENTS[3].1)); + let params = draft(&h).params(); + assert_eq!(parse_arrangement(¶ms["arrangement"], &Default::default()), Ok(ARRANGEMENTS[3].1)); + } + + /// Giving a surround a corner another has swaps the two, whichever corner it is. + #[test] + fn picking_a_taken_corner_swaps_the_two_surrounds() { + for (corner, name) in ["Top left", "Top right", "Bottom left", "Bottom right"].iter().enumerate() { + let mut h = placement(|app| app.dialogs.publication.as_mut().unwrap().arrangement = Arrangement::Custom([0, 1, 2, 3])); + // The legend (at 2) is given each corner in turn. + h.get_by_role_and_label(Role::ComboBox, "Legend").click(); + h.run_steps(4); + h.get_by_role_and_label(Role::Button, name).click(); + h.run_steps(4); + let mut want = [0, 1, 2, 3]; + want.swap(2, corner); + assert_eq!(draft(&h).arrangement, Arrangement::Custom(want), "{name}"); + } + } + + /// The styles with a column say that the locator and legend sit in it. + #[test] + fn column_styles_say_which_surrounds_take_the_corners() { + for (style, column) in + [("classic", false), ("banner", false), ("panel", true), ("editorial", true), ("modular", true), ("poster", false)] + { + let h = placement(|app| app.dialogs.publication.as_mut().unwrap().style = style.into()); + assert_eq!(h.query_by_label_contains("sit in this style's column").is_some(), column, "{style}"); + } + let h = placement(|app| { + let d = app.dialogs.publication.as_mut().unwrap(); + (d.style, d.size) = ("panel".into(), "figure-1col".into()); + }); + assert!(h.query_by_label_contains("sit in this style's column").is_none(), "a figure has no column"); + } + + /// Only a chosen arrangement can be made the favourite. + #[test] + fn make_favourite_is_off_for_best_and_favourite() { + let mut h = placement(|_| {}); + let off = |h: &Wizard| h.get_by_role_and_label(Role::CheckBox, "Make this my favourite").accesskit_node().is_disabled(); + assert!(off(&h), "Best"); + pick(&mut h, "Favourite: Classic"); + assert!(off(&h), "Favourite"); + pick(&mut h, "Mirrored"); + assert!(!off(&h)); + pick(&mut h, "Custom"); + assert!(!off(&h)); + } + + /// The wizard on its review, with `arrangement` to be made the favourite. + fn creating(arrangement: Arrangement, prepare: impl FnOnce(&mut Draft)) -> Wizard { + let mut app = app(); + open(&mut app); + let d = app.dialogs.publication.as_mut().unwrap(); + (d.step, d.arrangement, d.make_favourite) = (Step::Review, arrangement, true); + prepare(d); + harness(app) + } + + /// Create sets the favourite, by name or by corners; Undo takes the layout, not the + /// favourite. + #[test] + fn create_makes_the_chosen_arrangement_the_favourite() { + let custom = [3, 0, 1, 2]; + for (arrangement, want) in [ + (Arrangement::Named("mirrored".into()), json!("mirrored")), + (Arrangement::Custom(custom), rustgis_engine::cmd::layout::corners_of(custom)), + ] { + let mut h = creating(arrangement, |_| {}); + let before = h.state().session.project.layouts.len(); + click(&mut h, "Create"); + assert_eq!(h.state().session.project.layouts.len(), before + 1); + assert_eq!(h.state().session.layout_favourite, want); + assert_eq!(h.state().ui.tab, "Publish"); + h.state_mut().run("edit.undo", Value::Null).unwrap(); + assert_eq!(h.state().session.project.layouts.len(), before); + assert_eq!(h.state().session.layout_favourite, want, "a preference, not undone"); + } + } + + /// A flag left ticked behind Best or Favourite sets nothing. + #[test] + fn create_leaves_the_favourite_for_best_and_favourite() { + for arrangement in [Arrangement::Best, Arrangement::Favourite] { + let mut h = creating(arrangement, |_| {}); + click(&mut h, "Create"); + assert_eq!(h.state().session.layout_favourite, json!("classic")); + } + } + + /// A layout the command refuses changes no favourite and keeps the wizard open: an unknown + /// style, or a saved arrangement removed meanwhile. + #[test] + fn a_refused_layout_leaves_the_favourite() { + for (arrangement, style) in [(Arrangement::Named("mirrored".into()), "unknown"), (Arrangement::Named("Gone".into()), "classic")] { + let mut h = creating(arrangement, |d| d.style = style.into()); + let before = h.state().session.project.layouts.len(); + click(&mut h, "Create"); + assert_eq!(h.state().session.project.layouts.len(), before, "nothing made"); + assert_eq!(h.state().session.layout_favourite, json!("classic")); + assert_eq!(h.state().dialogs.open, Some("publicationWizard")); + } + } + + /// The review says the placement, of a sheet and of a figure. + #[test] + fn the_review_says_the_placement() { + for size in ["a4", "figure-1col"] { + let h = creating(Arrangement::Named("legendTop".into()), |d| d.size = size.into()); + assert!(h.query_by_label("Legend top").is_some(), "{size}"); + } + } + + /// A saved arrangement removed after it was picked is none in the step, the review and Create, + /// each by the same check: nothing marked or ticked, and nothing made. + #[test] + fn a_saved_arrangement_removed_after_it_was_picked_holds_nowhere() { + let gone = || Arrangement::Named("Gone".into()); + let h = placement(|app| app.dialogs.publication.as_mut().unwrap().arrangement = gone()); + assert!(h.get_by_role_and_label(Role::CheckBox, "Make this my favourite").accesskit_node().is_disabled()); + let mut h = creating(gone(), |_| {}); + assert!(h.query_by_label("Gone").is_none(), "the review doesn't name it"); + let before = h.state().session.project.layouts.len(); + let said = h.state().session.notices.len(); + click(&mut h, "Create"); + assert_eq!(h.state().session.project.layouts.len(), before, "nothing made"); + assert!(h.state().session.notices[said..].iter().any(|n| n.text.contains("pick the placement again")), "said"); + } + + /// The longest names saved, one of them the favourite, don't take the step off a narrow screen. + #[test] + fn long_saved_names_stay_on_a_narrow_screen() { + let mut app = app(); + open(&mut app); + let name = "W".repeat(40); + let corners = rustgis_engine::cmd::layout::corners_of(ARRANGEMENTS[1].1); + app.session.layout_arrangements.insert(name.clone(), corners); + app.session.layout_favourite = json!(name); + app.dialogs.publication.as_mut().unwrap().step = Step::Placement; + let h = harness_sized(app, egui::vec2(320.0, 900.0)); + for label in [name.clone(), format!("Favourite: {name}")] { + let rect = h.get_by_role_and_label(Role::RadioButton, &label).rect(); + assert!(rect.right() <= 320.0, "{label} at {rect:?} on a screen 320 wide"); + } + let next = h.get_by_role_and_label(Role::Button, "Next").rect(); + assert!(next.right() <= 320.0, "Next at {next:?} on a screen 320 wide"); + } + + /// The review says Create makes the arrangement the favourite only where it will: ticked, and + /// on an arrangement by name or Custom. + #[test] + fn the_review_says_when_create_sets_the_favourite() { + let said = |h: &Wizard| h.query_by_label("Create makes this arrangement the favourite").is_some(); + for size in ["a4", "figure-1col"] { + for (arrangement, will) in [ + (Arrangement::Named("mirrored".into()), true), + (Arrangement::Custom([3, 0, 1, 2]), true), + (Arrangement::Best, false), + (Arrangement::Favourite, false), + (Arrangement::Named("Gone".into()), false), + ] { + let h = creating(arrangement.clone(), |d| d.size = size.into()); + assert_eq!(said(&h), will, "{size}: {arrangement:?}"); + } + } + let h = creating(Arrangement::Named("mirrored".into()), |d| d.make_favourite = false); + assert!(!said(&h), "unticked"); + } + + /// A surround with no corner on the sheet isn't listed in the grid and has its picker off: on a + /// column style the locator and legend, on a figure (which draws none) the locator. The rest, + /// and all four elsewhere, keep theirs; a figure says why. + #[test] + fn surrounds_without_a_corner_are_not_given_one() { + for (style, size, column) in [ + ("classic", "a4", false), + ("panel", "a4", true), + ("editorial", "a4", true), + ("modular", "a4", true), + ("panel", "figure-1col", false), + ] { + let h = placement(|app| { + let d = app.dialogs.publication.as_mut().unwrap(); + (d.style, d.size, d.arrangement) = (style.into(), size.into(), Arrangement::Custom([0, 1, 2, 3])); + }); + for (surround, corner, free) in [ + ("Locator", "Top left", !column && size == "a4"), + ("North arrow", "Top right", true), + ("Legend", "Bottom left", !column), + ("Scale bar", "Bottom right", true), + ] { + assert_eq!(shown(&h, &format!("{corner}: {surround}")), free, "{style} {size}: {surround} in the grid"); + let off = h.get_by_role_and_label(Role::ComboBox, surround).accesskit_node().is_disabled(); + assert_eq!(off, !free, "{style} {size}: {surround}'s picker"); + } + let said = h.query_by_label("A figure draws no locator.").is_some(); + assert_eq!(said, size != "a4", "{style} {size}: the figure's note"); + } + } +} diff --git a/crates/ui-egui/src/publicationwizard/mod.rs b/crates/ui-egui/src/publicationwizard/mod.rs index a2a13bc740..9d69832b92 100644 --- a/crates/ui-egui/src/publicationwizard/mod.rs +++ b/crates/ui-egui/src/publicationwizard/mod.rs @@ -1,7 +1,8 @@ -//! Publication Layout Wizard: `layout.publication` asked step by step (size, map, style, texts, -//! sources and reference, accent), then made at once, as the New Layout › Publication Layout menu -//! makes it (#2850). +//! Publication Layout Wizard: `layout.publication` asked step by step (size, map, style, placement, +//! texts, sources and reference, accent), then made at once, as the New Layout › Publication Layout +//! menu makes it (#2850). mod accent; +mod arrange; mod map; mod review; mod size; @@ -12,6 +13,7 @@ mod texts; use crate::textform::StatLayer; use crate::{RustGisApp, i18n, theme::Tokens}; use accent::accent_step; +use arrange::{Arrangement, arrange_step}; use map::{Area, area_extent, map_step}; use review::review_step; use rustgis_engine::Session; @@ -34,6 +36,7 @@ pub enum Step { Size, Map, Style, + Placement, Texts, Sources, /// A figure's cartographic reference, the one text it draws. @@ -124,6 +127,10 @@ pub struct Draft { pub accent: Option, /// The colour last picked, which the swatch comes back with when the box is ticked again. picked: Option, + /// The corners the surrounds take. + pub arrangement: Arrangement, + /// Whether Create also sets a chosen arrangement as the favourite. + pub make_favourite: bool, } impl Draft { @@ -152,6 +159,8 @@ impl Draft { refused: [false; 2], accent: None, picked: None, + arrangement: Arrangement::Best, + make_favourite: false, } } @@ -172,9 +181,15 @@ impl Draft { /// The steps this draft goes through, in order. fn steps(&self) -> &'static [Step] { if self.figure() { - return &[Step::Size, Step::Map, Step::Reference, Step::Review]; + return &[Step::Size, Step::Map, Step::Placement, Step::Reference, Step::Review]; } - &[Step::Size, Step::Map, Step::Style, Step::Texts, Step::Sources, Step::Accent, Step::Review] + &[Step::Size, Step::Map, Step::Style, Step::Placement, Step::Texts, Step::Sources, Step::Accent, Step::Review] + } + + /// The favourite Create sets once the layout is made, if any: what the review says and what + /// Create does. + fn favourite_to_set(&self, s: &Session) -> Option { + self.arrangement.as_favourite(s).filter(|_| self.make_favourite) } /// The step `by` places from this one, if there is one. @@ -193,6 +208,9 @@ impl Draft { (false, Orientation::Landscape) => format!("{}-landscape", self.size), }; let mut p = json!({"profile": profile}); + if let Some(arrangement) = self.arrangement.value() { + p["arrangement"] = arrangement; + } if !self.name.trim().is_empty() { p["name"] = json!(self.name.trim()); } @@ -353,6 +371,7 @@ fn step_name(step: Step) -> &'static str { Step::Size => tl_raw!("Size"), Step::Map => tl_raw!("Map"), Step::Style => tl_raw!("Style"), + Step::Placement => tl_raw!("Placement"), Step::Texts => tl_raw!("Texts"), Step::Sources => tl_raw!("Sources and reference"), Step::Reference => tl_raw!("Reference"), @@ -367,6 +386,7 @@ fn step_label(step: Step) -> &'static str { Step::Size => tl!("Size"), Step::Map => tl!("Map"), Step::Style => tl!("Style"), + Step::Placement => tl!("Placement"), Step::Texts => tl!("Texts"), Step::Sources => tl!("Sources"), Step::Reference => tl!("Reference"), @@ -492,6 +512,7 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { Step::Size => size_step(ui, &mut d), Step::Map => map_step(ui, &app.session, &mut d), Step::Style => style_step(ui, &mut d, t.accent), + Step::Placement => arrange_step(ui, &app.session, &mut d), Step::Texts => texts_step(ui, &mut d, app.session.map()), Step::Sources => sources_step(ui, &mut d, app.session.map()), Step::Reference => reference_step(ui, &mut d), @@ -523,13 +544,22 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { }); // A layout the command refuses is said (a map with no extent), and the wizard stays. if create { - match area_extent(&app.session, &d.area) { + let held = d.arrangement.held(&app.session).map_err(String::from); + match held.and_then(|_| area_extent(&app.session, &d.area)) { Ok(extent) => { let mut params = d.params(); if let Some(extent) = extent { params["extent"] = json!(extent); } - close |= crate::ribbon::make_publication(app, params); + if crate::ribbon::make_publication(app, params) { + close = true; + // Only once the layout is made: a refused one changes no preference. + if let Some(favourite) = d.favourite_to_set(&app.session) + && let Err(e) = app.run("layout.arrangement", json!({"favourite": favourite})) + { + app.session.notify(e, true); + } + } } Err(e) => app.session.notify(e, true), } @@ -651,32 +681,32 @@ mod tests { assert_eq!(d.params(), json!({"profile": "figure-1col"})); } - /// A sheet goes through the seven steps; a figure goes from its size to its map, its reference - /// and the review, the steps it takes nothing from skipped both ways. Back is off on the first - /// step; Next becomes Create on the last. + /// A sheet goes through the eight steps; a figure goes from its size to its map, its placement, + /// its reference and the review, the steps it takes nothing from skipped both ways. Back is off + /// on the first step; Next becomes Create on the last. #[test] fn a_sheet_takes_every_step_and_a_figure_skips_what_it_doesnt_draw() { let mut app = app(); open(&mut app); let mut h = harness(app); assert!(h.get_by_role_and_label(Role::Button, "Back").accesskit_node().is_disabled(), "Back is off"); - for want in [Step::Map, Step::Style, Step::Texts, Step::Sources, Step::Accent, Step::Review] { + for want in [Step::Map, Step::Style, Step::Placement, Step::Texts, Step::Sources, Step::Accent, Step::Review] { click(&mut h, "Next"); assert_eq!(step(&h), want); } assert!(h.query_by_role_and_label(Role::Button, "Next").is_none()); assert!(h.query_by_role_and_label(Role::Button, "Create").is_some()); - for want in [Step::Accent, Step::Sources, Step::Texts, Step::Style, Step::Map, Step::Size] { + for want in [Step::Accent, Step::Sources, Step::Texts, Step::Placement, Step::Style, Step::Map, Step::Size] { click(&mut h, "Back"); assert_eq!(step(&h), want); } h.state_mut().dialogs.publication.as_mut().unwrap().size = "figure-2col".into(); h.run_steps(2); - for want in [Step::Map, Step::Reference, Step::Review] { + for want in [Step::Map, Step::Placement, Step::Reference, Step::Review] { click(&mut h, "Next"); assert_eq!(step(&h), want); } - for want in [Step::Reference, Step::Map, Step::Size] { + for want in [Step::Reference, Step::Placement, Step::Map, Step::Size] { click(&mut h, "Back"); assert_eq!(step(&h), want); } @@ -737,7 +767,7 @@ mod tests { /// The names the step list holds, in order, and the one it marks as the current step. fn listed(h: &egui_kittest::Harness<'static, RustGisApp>) -> (Vec<&'static str>, Vec<&'static str>) { let (mut names, mut current) = (Vec::new(), Vec::new()); - for name in ["Size", "Map", "Style", "Texts", "Sources", "Reference", "Accent", "Review"] { + for name in ["Size", "Map", "Style", "Placement", "Texts", "Sources", "Reference", "Accent", "Review"] { if let Some(n) = h.query_by_role_and_label(Role::Button, name) { names.push(name); if n.accesskit_node().toggled() == Some(egui::accesskit::Toggled::True) { @@ -772,13 +802,13 @@ mod tests { let mut app = app(); open(&mut app); let mut h = harness(app); - assert_eq!(listed(&h), (vec!["Size", "Map", "Style", "Texts", "Sources", "Accent", "Review"], vec!["Size"])); + assert_eq!(listed(&h), (vec!["Size", "Map", "Style", "Placement", "Texts", "Sources", "Accent", "Review"], vec!["Size"])); click(&mut h, "Next"); assert_eq!(listed(&h).1, ["Map"]); click(&mut h, "Back"); h.state_mut().dialogs.publication.as_mut().unwrap().size = "figure-1col".into(); h.run_steps(2); - assert_eq!(listed(&h), (vec!["Size", "Map", "Reference", "Review"], vec!["Size"])); + assert_eq!(listed(&h), (vec!["Size", "Map", "Placement", "Reference", "Review"], vec!["Size"])); } /// A step clicked in the list is the one shown, first, middle or last, keeping what was @@ -809,13 +839,13 @@ mod tests { for _ in 0..30 { h.key_press(egui::Key::Tab); h.run_steps(2); - for name in ["Size", "Map", "Style", "Texts", "Sources", "Accent", "Review"] { + for name in ["Size", "Map", "Style", "Placement", "Texts", "Sources", "Accent", "Review"] { if h.query_by_role_and_label(Role::Button, name).is_some_and(|n| n.accesskit_node().is_focused()) { reached.push(name); } } } - for name in ["Size", "Map", "Style", "Texts", "Sources", "Accent", "Review"] { + for name in ["Size", "Map", "Style", "Placement", "Texts", "Sources", "Accent", "Review"] { assert!(reached.contains(&name), "{name} not reached: {reached:?}"); } } @@ -836,9 +866,9 @@ mod tests { assert!((415.0..=460.0).contains(&small.width()) && (355.0..=420.0).contains(&small.height()), "smallest {small:?}"); let next = h.get_by_role_and_label(Role::Button, "Next").rect(); assert!(small.contains_rect(next), "Next at {next:?} outside {small:?}"); - assert_eq!(listed(&h).0.len(), 7, "the list stays"); + assert_eq!(listed(&h).0.len(), 8, "the list stays"); // Each step wraps or scrolls inside that size: none makes the window wider or taller. - for _ in 0..6 { + for _ in 0..7 { click(&mut h, "Next"); // Within the point a high-density screen's rounding of a wrapped line may add. let now = window(&h).size(); @@ -861,7 +891,7 @@ mod tests { let larger = at(&h); let moved = larger.right_bottom() - before.right_bottom(); assert!(moved.y > 50.0, "Next moved by {moved:?}"); - for _ in 0..5 { + for _ in 0..6 { click(&mut h, "Next"); assert_eq!(at(&h), larger, "{:?}", step(&h)); } @@ -875,7 +905,7 @@ mod tests { let mut h = harness(app); let at = |h: &egui_kittest::Harness<'static, RustGisApp>| h.get_by_role_and_label(Role::Button, "Next").rect(); let first = at(&h); - for _ in 0..5 { + for _ in 0..6 { click(&mut h, "Next"); assert_eq!(at(&h), first, "{:?}", step(&h)); } @@ -896,7 +926,7 @@ mod tests { assert_ne!(next, "Next", "{code}: the language changed"); let at = |h: &egui_kittest::Harness<'static, RustGisApp>| h.get_by_role_and_label(Role::Button, &next).rect(); let first = at(&h); - for _ in 0..5 { + for _ in 0..6 { h.get_by_role_and_label(Role::Button, &next).click(); h.run_steps(4); assert_eq!(at(&h), first, "{code}: {:?}", step(&h)); @@ -927,7 +957,7 @@ mod tests { let text = h.get_by_label(about).rect(); assert!(text.right() <= 320.0, "{text:?} runs off a screen 320 wide"); assert_eq!(listed(&h).0, Vec::<&str>::new(), "the list is hidden"); - assert!(h.query_by_label("Step 3 of 7: Style").is_some(), "the heading still says the step"); + assert!(h.query_by_label("Step 3 of 8: Style").is_some(), "the heading still says the step"); } /// A screen that narrows while the wizard is open (a phone turned, a browser resized) keeps diff --git a/crates/ui-egui/src/publicationwizard/review.rs b/crates/ui-egui/src/publicationwizard/review.rs index 326d25e91b..c8f77db0b8 100644 --- a/crates/ui-egui/src/publicationwizard/review.rs +++ b/crates/ui-egui/src/publicationwizard/review.rs @@ -17,6 +17,7 @@ pub(super) fn review_step(ui: &mut egui::Ui, s: &Session, d: &Draft) { row(tl!("Map extent"), area_label(s, &d.area)); row(tl!("Name"), name_label(s, &d.name)); if d.figure() { + placement_rows(&mut row, s, d); row(tl!("Reference"), reference_value(d)); return; } @@ -28,6 +29,7 @@ pub(super) fn review_step(ui: &mut egui::Ui, s: &Session, d: &Draft) { row(tl!("Orientation"), turned.to_string()); let style = crate::ribbon::publication_style(&d.style).map(|s| s.0.to_string()).unwrap_or_else(|| d.style.clone()); row(tl!("Style"), style); + placement_rows(&mut row, s, d); row(tl!("Title"), or_none(&d.title)); row(tl!("Subtitle"), or_none(&d.subtitle)); row(tl!("Note"), or_none(&d.note)); @@ -53,6 +55,14 @@ pub(super) fn review_step(ui: &mut egui::Ui, s: &Session, d: &Draft) { } } +/// The placement's row, and one more when Create also makes it the favourite. +fn placement_rows(row: &mut impl FnMut(&str, String), s: &Session, d: &Draft) { + row(tl!("Placement"), d.arrangement.label(s)); + if d.favourite_to_set(s).is_some() { + row(tl!("Favourite"), tl!("Create makes this arrangement the favourite").to_string()); + } +} + /// The reference as it will be drawn: the lines typed, or the command's default; left out, none, /// but lines typed then are said not to be used. fn reference_value(d: &Draft) -> String { diff --git a/crates/ui-egui/src/publicationwizard/texts.rs b/crates/ui-egui/src/publicationwizard/texts.rs index 006f0c2456..85d1c83529 100644 --- a/crates/ui-egui/src/publicationwizard/texts.rs +++ b/crates/ui-egui/src/publicationwizard/texts.rs @@ -69,7 +69,7 @@ mod tests { h.state_mut().run("map.new", json!({"name": "Rivers"})).unwrap(); h.run_steps(2); assert_eq!(h.state().dialogs.publication.as_ref().unwrap().title, "Rivers"); - for _ in 0..3 { + for _ in 0..4 { click(&mut h, "Next"); } h.get_by_label("Title").focus(); diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index f1e76c3e16..2543526a5c 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -43,24 +43,30 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu (a sheet turned Auto, Portrait or Landscape, or a figure), the map extent and the layout's name, the style (each beside an outline of where its title, map and footer go, and its column or panel if it has one, with the accent colour picked, or else its own, where the sheet takes it, which - Classic's doesn't; a click on the outline picks the style), the texts (title, the active map's - name until one is typed; subtitle, note and credit line, or none; an Editorial sheet's column - heading), the sources and reference (each one per line, with its heading; the reference can be - left out) and the accent colour, then shows what was picked; **Create** makes the layout as the - menu does. **Insert Statistic**, on the texts and sources steps, adds a layer figure of the - active map to the field the cursor was last in there (the note or the sources, where it was in - none). The map extent is the view, the features one layer shows (hidden or not, in their - longitude/latitude box) or the selected features (a point or a straight line is widened to - 0.01°, and what lies past ±180° or ±90° is left out of the frame; an area with nothing to frame, - one gone by Create, or one picked before the map or the project was switched or the map renamed, - is said and nothing is made); without a name the layout is `Publication`; the review says when a - layout has that name, so the new one is numbered. A figure goes from its size to its map, its - reference (each line typed; with no heading, and it can be left out) and the review, as it takes - no style, accent or other text. A field left empty is left out (for the credit - line, the reference and the headings, the command's default applies); the title is the - exception: empty, it means no title. A layout the command refuses is said, and the wizard stays - open. The window can be resized. Its steps are listed on the left, and a click goes to one; a - narrow screen leaves the list out, and the heading still says which step this is. + Classic's doesn't; a click on the outline picks the style), the placement, the texts (title, the + active map's name until one is typed; subtitle, note and credit line, or none; an Editorial + sheet's column heading), the sources and reference (each one per line, with its heading; the + reference can be left out) and the accent colour, then shows what was picked; **Create** makes + the layout as the menu does. **Insert Statistic**, on the texts and sources steps, adds a layer + figure of the active map to the field the cursor was last in there (the note or the sources, + where it was in none). The map extent is the view, the features one layer shows (hidden or not, + in their longitude/latitude box) or the selected features (a point or a straight line is widened + to 0.01°, and what lies past ±180° or ±90° is left out of the frame; an area with nothing to + frame, one gone by Create, or one picked before the map or the project was switched or the map + renamed, is said and nothing is made); without a name the layout is `Publication`; the review + says when a layout has that name, so the new one is numbered. The **placement** is the + arrangement (below): `best`, the favourite, one by name or one saved, or **Custom**, a corner + for each surround (a corner another has swaps the two; on Panel, Editorial and Modular only the + north arrow and scale bar take one, the locator and legend sitting in the column, and a figure + gives the locator none, as it draws none); a chosen one can be made the favourite, once the + layout is made. A saved one removed after it was picked is said at Create and nothing is made. A + figure goes from its size to its map, its placement, its reference (each line typed; with no + heading, and it can be left out) and the review, as it takes no style, accent or other text. A + field left empty is left out (for the credit line, the reference and the headings, the command's + default applies); the title is the exception: empty, it means no title. A layout the command + refuses is said, and the wizard stays open. The window can be resized. Its steps are listed on + the left, and a click goes to one; a narrow screen leaves the list out, and the heading still + says which step this is. - **The map**: the `extent` given (or the view) fitted north-up, without tilt, at a round scale, two significant figures, rounded up so it still fits within the strips the graticule's labels take inside the frame. A graticule's step follows the extent (2° for a state, 15′ for a city),