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
3 changes: 2 additions & 1 deletion crates/engine/src/cmd/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub fn commands() -> Vec<C> {
}
let i = required_map_at(s,p)?;
let named = super::layout::maps_layouts_name(&s.project);
s.project.maps.remove(i);
let gone = s.project.maps.remove(i).name;
// Deleting another map keeps the one in view (a map tab's close button).
let active = s.project.active_map;
if i < active {
Expand All @@ -226,6 +226,7 @@ pub fn commands() -> Vec<C> {
s.table_layer = None;
}
super::layout::say_map_texts(s, &named);
super::presentation::say_pages_without_their_map(s, super::presentation::PagesOf::Map(&gone));
Ok(Value::Null)
}).transition(unmodelled_transition).inputs(required_map_inputs).roles(&[]).effects(crate::policy::Effects::NONE)
.params(r#"{"name": string}"#),
Expand Down
98 changes: 98 additions & 0 deletions crates/engine/src/cmd/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn commands() -> Vec<C> {
if let Some(cut) = cut {
s.notify(cut, false);
}
say_pages_without_their_map(s, PagesOf::Presentation(i));
// Pages often share a title's figure: each warning once, with its count of pages.
let mut said: Vec<(String, usize)> = Vec::new();
for k in 0..s.project.presentations[i].pages.len() {
Expand Down Expand Up @@ -631,6 +632,103 @@ fn page_map<'a>(input: &'a impl super::InputSource, name: &str) -> &'a rustgis_m
let s = &context;
s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(name)).unwrap_or_else(|| s.map())
}

/// Most map names, presentations and page numbers [`say_pages_without_their_map`] lists: an
/// imported file may hold hundreds of pages, each naming another map.
const MISSING_MAPS_NAMED: usize = 3;
const PRESENTATIONS_LISTED: usize = 3;
const PAGES_LISTED: usize = 5;

/// The map pages [`say_pages_without_their_map`] looks at.
pub(crate) enum PagesOf<'a> {
/// Those naming this map, in every presentation: it has just gone.
Map(&'a str),
/// Those of the presentation at this index, whatever they name: it has just come in.
Presentation(usize),
}

/// Says, once, which of the map pages `which` name a map the project doesn't have, which
/// [`page_map`] draws over the active map (#3047). Pages are numbered from 1, as the Presentation
/// pane shows them. The pages keep the name.
pub(crate) fn say_pages_without_their_map(s: &mut Session, which: PagesOf<'_>) {
use crate::notice_text::NoticeText;

/// A presentation listed: where it is, its name, its first pages and how many it has.
struct Place<'a> {
at: usize,
name: &'a str,
numbers: Vec<String>,
pages: usize,
}

let (mut pages, mut places) = (0usize, Vec::<Place<'_>>::new());
let (mut named, mut more_maps, mut more_places) = (Vec::<&str>::new(), false, false);
for (i, pres) in s.project.presentations.iter().enumerate() {
for (k, page) in pres.pages.iter().enumerate() {
let PageKind::Map { map, .. } = &page.kind else { continue };
let looked_at = match which {
PagesOf::Map(gone) => map.eq_ignore_ascii_case(gone),
PagesOf::Presentation(own) => i == own,
};
if !looked_at || s.project.maps.iter().any(|m| m.name.eq_ignore_ascii_case(map)) {
continue;
}
pages += 1;
if !named.iter().any(|n| n.eq_ignore_ascii_case(map)) {
if named.len() < MISSING_MAPS_NAMED {
named.push(map);
} else {
more_maps = true;
}
}
if places.last().is_none_or(|place| place.at != i) {
if places.len() == PRESENTATIONS_LISTED {
more_places = true;
continue;
}
places.push(Place { at: i, name: &pres.name, numbers: Vec::new(), pages: 0 });
}
if let Some(place) = places.last_mut() {
place.pages += 1;
if place.numbers.len() < PAGES_LISTED {
place.numbers.push((k + 1).to_string());
}
}
}
}
let listed = places.into_iter().map(|place| {
let key = if place.pages == 1 { "presentation '{name}', page {numbers}" } else { "presentation '{name}', pages {numbers}" };
let mut numbers = place.numbers.join(", ");
if place.pages > PAGES_LISTED {
numbers.push_str(", …");
}
NoticeText::format(key, [("name", place.name.to_string().into()), ("numbers", numbers.into())])
});
let Some(mut places) = listed.reduce(|first, next| NoticeText::format("{first}; {next}", [("first", first), ("next", next)])) else {
return;
};
if more_places {
places = NoticeText::format("{first}; {next}", [("first", places), ("next", "…".to_string().into())]);
}
let key = match (pages, named.len()) {
(1, _) => {
"{count} map page shows {names}, which the project doesn't have, so it is drawn over the active map until a map of that name comes back: {places}"
}
(_, 1) => {
"{count} map pages show {names}, which the project doesn't have, so they are drawn over the active map until a map of that name comes back: {places}"
}
_ => {
"{count} map pages show {names}, which the project doesn't have, so they are drawn over the active map until maps of those names come back: {places}"
}
};
let mut names = named.iter().map(|n| format!("'{n}'")).collect::<Vec<_>>().join(", ");
if more_maps {
names.push_str(", …");
}
let notice = NoticeText::format(key, [("count", pages.to_string().into()), ("names", names.into()), ("places", places)]);
s.notify_text(notice, false);
}

/// Binds the map whose current view initializes the page.
fn map_page_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::Inputs, String> {
let map = map_page_source(input, p)?;
Expand Down
6 changes: 5 additions & 1 deletion crates/engine/src/cmd/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,11 @@ pub fn commands() -> Vec<C> {
m.offline_area_id = None;
}
}
let mut gone = None;
if let Some(i) = kept
&& s.project.maps.len() > 1
{
s.project.maps.remove(i);
gone = Some(s.project.maps.remove(i).name);
// The active map stays the same map; if it was the removed one, a neighbour.
let a = s.project.active_map;
s.project.active_map = if a > i { a - 1 } else { a.min(s.project.maps.len().saturating_sub(1)) };
Expand All @@ -317,6 +318,9 @@ pub fn commands() -> Vec<C> {
s.notify(format!("The offline area's map was the only one, so it was kept as '{kept}'"), false);
}
super::layout::say_map_texts(s, &named);
if let Some(gone) = gone {
super::presentation::say_pages_without_their_map(s, super::presentation::PagesOf::Map(&gone));
}
Ok(Value::Null)
}).roles(&[]).effects(crate::policy::Effects::NONE)
.params(r#"{"name": offline area}"#),
Expand Down
3 changes: 3 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ mod layout_title;
#[path = "tests/layout_removed_layer.rs"]
mod layout_removed_layer;

#[path = "tests/layout_removed_map.rs"]
mod layout_removed_map;

#[path = "tests/layout_edge.rs"]
mod layout_edge;

Expand Down
212 changes: 212 additions & 0 deletions crates/engine/src/tests/layout_removed_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
//! A presentation map page whose map the project doesn't have is drawn over the active map:
//! deleting the map, or importing such a presentation, says which pages (#3047). All keep the name.

use super::*;
use rustgis_map::presentation::{Page, PageKind, Presentation};

/// The notices `command` gives about map pages that show a map the project doesn't have.
fn missing(s: &mut Session, command: &str, params: Value) -> Vec<String> {
let mark = s.notice_mark();
s.run(command, &params).unwrap();
s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("which the project doesn't have")).collect()
}

/// The sample world with a map `Other`, the world in view, and a presentation `Tour`.
fn with_tour() -> Session {
let mut s = world();
let home = s.map().name.clone();
s.run("map.new", &json!({"name": "Other"})).unwrap();
s.run("map.activate", &json!({"name": home})).unwrap();
s.run("presentation.new", &json!({"name": "Tour"})).unwrap();
s
}

fn page(s: &mut Session, map: &str) {
s.run("presentation.mapPage", &json!({"map": map, "title": "Sites"})).unwrap();
}

/// The map the frame of page `k` of the first presentation is drawn over.
fn drawn_over(s: &Session, k: usize) -> String {
let pres = &s.project.presentations[0];
let layout = crate::cmd::presentation::page_layout(s, pres, &pres.pages[k]);
layout
.elements
.iter()
.find_map(|e| match &e.kind {
rustgis_map::layout::ElementKind::MapFrame { map, .. } => Some(map.clone()),
_ => None,
})
.unwrap()
}

/// A presentation file with one map page for each of `maps`.
fn file_showing(maps: &[&str]) -> Value {
let pages = maps
.iter()
.map(|map| Page {
title: "Sites".into(),
kind: PageKind::Map { map: (*map).into(), center: rustgis_geom::Coord::new(0.0, 0.0), width_m: 1000.0 },
notes: String::new(),
seconds: 5.0,
})
.collect();
let pres = Presentation { name: "Filed".into(), pages, ..Presentation::default() };
json!({"text": serde_json::to_string(&pres).unwrap()})
}

#[test]
fn deleting_a_map_a_page_shows_says_so_and_the_page_keeps_the_name() {
let mut s = with_tour();
page(&mut s, "Other");
assert_eq!(
missing(&mut s, "map.remove", json!({"name": "Other"})),
[
"1 map page shows 'Other', which the project doesn't have, so it is drawn over the active map until a map of that name comes back: presentation 'Tour', page 2"
]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
);
assert_eq!(drawn_over(&s, 1), s.map().name);
// Exporting draws it as before, with nothing more said.
let mark = s.notice_mark();
crate::cmd::presentation::export_bytes(&mut s, 0, "html", 120.0).unwrap();
assert!(s.notices_since(mark).iter().all(|n| !n.text.contains("which the project doesn't have")));
// A map of that name brings the page back to it.
s.run("map.new", &json!({"name": "Other"})).unwrap();
assert_eq!(drawn_over(&s, 1), "Other");
}

#[test]
fn undoing_the_deletion_draws_the_page_over_its_map_again() {
let mut s = with_tour();
page(&mut s, "Other");
s.run("map.remove", &json!({"name": "Other"})).unwrap();
assert_eq!(missing(&mut s, "edit.undo", Value::Null), Vec::<String>::new());
assert_eq!(drawn_over(&s, 1), "Other");
}

#[test]
fn pages_of_two_presentations_are_said_once() {
let mut s = with_tour();
page(&mut s, "Other");
s.run("presentation.new", &json!({"name": "Second"})).unwrap();
page(&mut s, "Other");
assert_eq!(
missing(&mut s, "map.remove", json!({"name": "Other"})),
[
"2 map pages show 'Other', which the project doesn't have, so they are drawn over the active map until a map of that name comes back: presentation 'Tour', page 2; presentation 'Second', page 2"
]
);
}

#[test]
fn a_page_that_spells_its_map_in_another_case_is_counted() {
let mut s = with_tour();
page(&mut s, "Other");
let PageKind::Map { map, .. } = &mut s.project.presentations[0].pages[1].kind else { panic!("a map page") };
*map = "oTHER".into();
assert_eq!(
missing(&mut s, "map.remove", json!({"name": "Other"})),
[
"1 map page shows 'oTHER', which the project doesn't have, so it is drawn over the active map until a map of that name comes back: presentation 'Tour', page 2"
]
);
}

#[test]
fn deleting_a_map_no_page_shows_says_nothing() {
let mut s = with_tour();
s.run("map.new", &json!({"name": "Spare"})).unwrap();
page(&mut s, "Other");
// A page already without its map isn't said again when another map goes.
let PageKind::Map { map, .. } = &mut s.project.presentations[0].pages[1].kind else { panic!("a map page") };
*map = "Gone".into();
assert_eq!(missing(&mut s, "map.remove", json!({"name": "Spare"})), Vec::<String>::new());
}

#[test]
fn deleting_a_map_with_no_presentation_says_nothing() {
let mut s = world();
s.run("map.new", &json!({"name": "Other"})).unwrap();
assert_eq!(missing(&mut s, "map.remove", json!({"name": "Other"})), Vec::<String>::new());
}

/// Adds the offline area `Crew`, made from the map `source`.
fn offline_area(s: &mut Session, source: &str) {
let extent = rustgis_geom::Extent::new(0.0, 0.0, 1.0, 1.0);
let area = json!({"name": "Crew", "map": source, "extent": extent, "created": 0.0, "data": s.project.maps[0]});
s.project.offline_areas.push(serde_json::from_value(area).unwrap());
}

#[test]
fn removing_an_offline_area_says_the_pages_that_showed_its_map() {
let mut s = with_tour();
let home = s.map().name.clone();
s.run("map.new", &json!({"name": "Crew (offline)"})).unwrap();
offline_area(&mut s, &home);
page(&mut s, "Crew (offline)");
assert_eq!(
missing(&mut s, "offline.remove", json!({"name": "Crew"})),
[
"1 map page shows 'Crew (offline)', which the project doesn't have, so it is drawn over the active map until a map of that name comes back: presentation 'Tour', page 2"
]
);
}

#[test]
fn removing_an_offline_area_whose_map_is_kept_says_nothing() {
let mut s = world();
s.run("map.rename", &json!({"name": "Crew (offline)"})).unwrap();
s.run("presentation.new", &json!({"name": "Tour"})).unwrap();
page(&mut s, "Crew (offline)");
offline_area(&mut s, "Source");
assert_eq!(missing(&mut s, "offline.remove", json!({"name": "Crew"})), Vec::<String>::new());
assert_eq!(drawn_over(&s, 1), "Crew (kept)");
}

#[test]
fn importing_a_presentation_says_the_pages_whose_map_is_missing() {
let mut s = world();
let home = s.map().name.clone();
assert_eq!(
missing(&mut s, "presentation.import", file_showing(&["Missing", &home, "missing"])),
[
"2 map pages show 'Missing', which the project doesn't have, so they are drawn over the active map until a map of that name comes back: presentation 'Filed', pages 1, 3"
]
);
}

#[test]
fn importing_a_presentation_whose_maps_are_all_there_says_nothing() {
let mut s = world();
let home = s.map().name.clone();
assert_eq!(missing(&mut s, "presentation.import", file_showing(&[&home])), Vec::<String>::new());
}

#[test]
fn many_missing_maps_are_named_up_to_three() {
let mut s = world();
assert_eq!(
missing(&mut s, "presentation.import", file_showing(&["A", "B", "A", "C", "D", "E"])),
[
"6 map pages show 'A', 'B', 'C', …, which the project doesn't have, so they are drawn over the active map until maps of those names come back: presentation 'Filed', pages 1, 2, 3, 4, 5, …"
]
);
}

#[test]
fn pages_past_three_presentations_are_counted_and_not_listed() {
let mut s = with_tour();
// Five pages in the first presentation, all listed; one in each of three more.
for _ in 0..5 {
page(&mut s, "Other");
}
for name in ["Second", "Third", "Fourth"] {
s.run("presentation.new", &json!({"name": name})).unwrap();
page(&mut s, "Other");
}
assert_eq!(
missing(&mut s, "map.remove", json!({"name": "Other"})),
[
"8 map pages show 'Other', which the project doesn't have, so they are drawn over the active map until a map of that name comes back: presentation 'Tour', pages 2, 3, 4, 5, 6; presentation 'Second', page 2; presentation 'Third', page 2; …"
]
);
}
3 changes: 3 additions & 0 deletions docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,9 @@ previews, edits and plays it.
coloured page, an **Image** page a picture with a caption, and a **Video** page a video file or
URL with an optional poster picture.
- Each page has a title, speaker notes and a duration (for **Play**), and can move up or down.
- A **Map** page follows its map by name. While the project has no map of that name, the page is
drawn over the active map; deleting the map (or the offline area that goes with it), or
importing a presentation that names it, says which pages.
- **Publish › Output › Presentation › Export Presentation** writes an HTML slideshow (arrow
keys, `N` for notes, `P` to play; videos play in place), a PDF (kept as vectors as a layout's
is), or the first page as PNG.
Expand Down
Loading