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/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ fn reset(s: &mut Session, p: Project) {
s.active_layer = s.map().layers.first().map(|l| l.id);
s.table_layer = None;
s.table_standalone = None;
s.active_layout = None;
// The layout open when it was saved; one the file doesn't have is the map view (#3446).
s.active_layout = s.project.saved_open_layout();
s.tool = Tool::Explore;
s.clear_history();
s.file_identity = s.state();
Expand Down
17 changes: 14 additions & 3 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ impl Session {
web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0)
});
let label_notices = project.take_read_notices(&crate::project::ALL_PARTS);
let open_layout = project.saved_open_layout();
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let mut session = Session {
project,
path: None,
Expand Down Expand Up @@ -1567,7 +1568,7 @@ impl Session {
clipboard: None,
layer_clipboard: None,
keep_tool: false,
active_layout: None,
active_layout: open_layout,
policy: Default::default(),
document_assets: Default::default(),
browse_origins: Default::default(),
Expand Down Expand Up @@ -2566,6 +2567,7 @@ impl Session {
let kept = !spec.atomic || matches!(res, Ok(Ok(_)));
self.shared_steps.requested |= unchanged;
self.shared_steps.edited |= moved || (spec.mutates && kept && !unchanged);
self.keep_open_layout();
res.unwrap_or_else(|panic| std::panic::resume_unwind(panic))
}

Expand Down Expand Up @@ -4374,11 +4376,20 @@ impl Session {
self.layer_selection_of = active;
}

/// Keeps `active_layer` pointing at a layer of the active map.
pub(crate) fn fix_active_layer(&mut self) {
/// Writes the open layout into the project, so a save carries it (#3446), after each command
/// and each step of one: a step may save. An index the project has no layout for is let go.
/// Writing it isn't an edit: [`Session::state`] doesn't change.
fn keep_open_layout(&mut self) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if self.active_layout.is_some_and(|i| i >= self.project.layouts.len()) {
self.active_layout = None;
}
self.project.open_layout = self.active_layout;
}

/// Keeps `active_layer` pointing at a layer of the active map and the other open items at
/// items the project has.
pub(crate) fn fix_active_layer(&mut self) {
self.keep_open_layout();
if self.active_report.is_some_and(|i| i >= self.project.reports.len()) {
self.active_report = None;
}
Expand Down
16 changes: 16 additions & 0 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub const CAPABILITIES: &[&str] = &[
"shaded-relief-flats",
"layout-publication-crs",
"shaded-relief-generalize",
PROJECT_OPEN_LAYOUT,
];
/// An offline area keeping the versions that lost a sync or a Bring Back
/// (`OfflineArea::conflicts`, #2468): an older reader would save it again without them.
Expand All @@ -163,6 +164,9 @@ pub(crate) const NATIVE_COORDINATES: &str = "native-coordinates";
/// (`Project::coordinate_system`, `Project::crs_from_first_layer`, #2032 C3a): an older reader
/// would save it again without them.
pub(crate) const PROJECT_CRS: &str = "project-crs";
/// A project saved with a layout open (`Project::open_layout`, #3446): an older reader would
/// save it again without it.
pub(crate) const PROJECT_OPEN_LAYOUT: &str = "project-open-layout";
/// Independent marker sizes alongside classified point colours (`Layer::size_by`).
pub(crate) const SIZE_BY_CAPABILITY: &str = "classified-point-size";
/// The most entries a document's `requires` may list: past it the document is refused without
Expand Down Expand Up @@ -475,6 +479,10 @@ pub struct Project {
#[serde(default)]
#[cfg_attr(feature = "schema", schemars(with = "Vec<crate::schema::Open>"))]
pub layouts: Vec<rustgis_map::layout::Layout>,
/// The layout shown when the project was saved, an index into `layouts`; none is the map
/// view. Kept as the session's open layout after each command, and opened with the project.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub open_layout: Option<usize>,
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
/// Folder connections shown in the Browser.
#[serde(default)]
pub folders: Vec<String>,
Expand Down Expand Up @@ -1353,6 +1361,7 @@ impl Project {
next_table_id: 1,
gp_history: Vec::new(),
layouts: Vec::new(),
open_layout: None,
folders: Vec::new(),
styles: Vec::new(),
ramps: Vec::new(),
Expand Down Expand Up @@ -1861,6 +1870,12 @@ impl Project {
notices
}

/// The layout the project was saved with open, as a session opens it; an index the project
/// has no layout for is the map view.
pub(crate) fn saved_open_layout(&self) -> Option<usize> {
self.open_layout.filter(|i| *i < self.layouts.len())
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}

pub fn map(&self) -> &Map {
// `normalize` guarantees a map; fall back to the first for safety.
self.maps.get(self.active_map).or_else(|| self.maps.first()).unwrap_or_else(|| empty_map())
Expand Down Expand Up @@ -2927,6 +2942,7 @@ impl Durable for Project {
next_table_id: _,
gp_history,
layouts: _,
open_layout: _,
folders: _,
styles: _,
ramps: _,
Expand Down
4 changes: 4 additions & 0 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ impl Project {
if self.coordinate_system.is_some() || self.crs_from_first_layer {
required.push(super::PROJECT_CRS);
}
if self.saved_open_layout().is_some() {
required.push(super::PROJECT_OPEN_LAYOUT);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Saved symbology keeps its images too.
if !required.contains(&"picture-markers") && self.styles.iter().any(|s| s.renderer.has_picture_markers()) {
required.push("picture-markers");
Expand Down Expand Up @@ -1604,6 +1607,7 @@ fn project_needs_wkt(project: &Project) -> bool {
next_table_id: _,
gp_history: _,
layouts: _,
open_layout: _,
folders: _,
styles: _,
ramps: _,
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 @@ -185,6 +185,9 @@ mod layout_table;
#[path = "tests/layout_extents.rs"]
mod layout_extents;

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

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

Expand Down
2 changes: 2 additions & 0 deletions crates/engine/src/tests/layout_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ fn a_layout_file_requires_nothing_for_other_layouts_or_saved_commands() {
document["tasks"] = json!([{"name": "Task", "steps": [{"title": "Wrap", "command": "layout.addElement", "params": {"kind": "text", "text": "A", "wrap": true}}]}]);
let mut s = Session::new(Project::from_json(&document.to_string()).unwrap());
s.run("layout.new", &json!({"page": "a4", "template": "map"})).unwrap();
// On the map view: an open layout is the project's own requirement (#3446).
s.run("layout.close", &Value::Null).unwrap();
let project = serde_json::to_value(&s.project).unwrap();
assert_eq!(project["requires"], json!(["layout-element-wrap", "layout-element-background"]));
let (_, background) = saved_file(&mut s, 0, "other-background");
Expand Down
219 changes: 219 additions & 0 deletions crates/engine/src/tests/layout_open.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
//! A project reopens on the layout that was open when it was saved (#3446).

use crate::{Project, Session};
use serde_json::{Value, json};
use std::path::PathBuf;

/// A folder of this test's own, removed when it ends.
struct Dir(PathBuf);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved

impl Dir {
fn new(name: &str) -> Self {
let dir = std::env::temp_dir().join(format!("rustgis-open-layout-{name}-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
Dir(dir)
}
}

impl Drop for Dir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}

/// A session with the layouts A and B, B open.
fn two_layouts() -> Session {
let mut s = Session::new(Project::default());
for name in ["A", "B"] {
s.run("layout.new", &json!({"page": "a4", "template": "map", "name": name})).unwrap();
}
assert_eq!(s.active_layout, Some(1));
s
}

/// The project as a save writes it.
fn saved(s: &mut Session) -> String {
s.project.saved_text(None).unwrap().0
}

/// What the saved text says is open.
fn written(text: &str) -> Value {
serde_json::from_str::<Value>(text).unwrap().get("open_layout").cloned().unwrap_or(Value::Null)
}

/// `text` opened in a new session.
fn opened(text: &str) -> Session {
let mut s = Session::new(Project::default());
s.run("project.openText", &json!({"text": text})).unwrap();
s
}

/// Whether opening said a key was left out.
fn said_unread(s: &Session) -> bool {
s.notices_since(0).iter().any(|n| n.text.contains("this version doesn't read"))
}

#[test]
fn a_saved_project_reopens_on_the_layout_that_was_open() {
let dir = Dir::new("save");
let path = dir.0.join("atlas.rgis");
let mut s = two_layouts();
s.run("project.saveAs", &json!({"path": path})).unwrap();
let mut o = Session::new(Project::default());
o.run("project.open", &json!({"path": path})).unwrap();
assert_eq!(o.active_layout, Some(1));
assert!(!said_unread(&o));
}

#[test]
fn text_a_package_and_a_recovery_copy_reopen_on_the_layout() {
let dir = Dir::new("package");
let package = dir.0.join("atlas.rgispkg");
let mut s = two_layouts();
let text = saved(&mut s);
assert_eq!(written(&text), json!(1));
assert_eq!(opened(&text).active_layout, Some(1));
assert_eq!(opened(&s.project.clone().recovery_text().unwrap()).active_layout, Some(1));
s.run("project.package", &json!({"path": package})).unwrap();
let mut o = Session::new(Project::default());
o.run("project.open", &json!({"path": package})).unwrap();
assert_eq!(o.active_layout, Some(1));
}

#[test]
fn a_project_saved_on_the_map_view_reopens_on_it() {
let mut s = two_layouts();
s.run("layout.close", &Value::Null).unwrap();
let text = saved(&mut s);
assert_eq!(written(&text), Value::Null);
assert_eq!(opened(&text).active_layout, None);
}

#[test]
fn a_moved_open_layout_reopens_at_its_new_place() {
let mut s = two_layouts();
s.run("layout.move", &json!({"name": "B", "to": 0})).unwrap();
let o = opened(&saved(&mut s));
assert_eq!(o.active_layout, Some(0));
assert_eq!(o.project.layouts[0].name, "B");
}

#[test]
fn another_layout_moved_past_the_open_one_shifts_what_is_written() {
let mut s = two_layouts();
s.run("layout.move", &json!({"name": "A", "to": 1})).unwrap();
let text = saved(&mut s);
assert_eq!(written(&text), json!(0));
assert_eq!(opened(&text).project.layouts[0].name, "B");
}

#[test]
fn removing_the_open_layout_leaves_none_to_reopen() {
let mut s = two_layouts();
s.run("layout.remove", &json!({"name": "B"})).unwrap();
assert_eq!(s.active_layout, None);
assert_eq!(written(&saved(&mut s)), Value::Null);
}

#[test]
fn undo_writes_the_layout_then_open() {
let mut s = two_layouts();
s.run("layout.remove", &json!({"name": "A"})).unwrap();
assert_eq!(written(&saved(&mut s)), json!(0));
s.run("edit.undo", &Value::Null).unwrap();
assert_eq!(written(&saved(&mut s)), json!(1));
}

#[test]
fn a_file_without_the_key_or_with_one_past_the_layouts_opens_on_the_map() {
let mut s = two_layouts();
let mut v: Value = serde_json::from_str(&saved(&mut s)).unwrap();
v["open_layout"] = json!(2);
let o = opened(&v.to_string());
assert_eq!(o.active_layout, None);
assert_eq!(o.project.open_layout, None);
v.as_object_mut().unwrap().remove("open_layout");
let o = opened(&v.to_string());
assert_eq!(o.active_layout, None);
assert!(!said_unread(&o));
}

#[test]
fn a_new_or_sample_project_opens_on_the_map() {
for command in ["project.new", "project.sample"] {
let mut s = two_layouts();
s.run(command, &Value::Null).unwrap();
assert_eq!((s.active_layout, s.project.open_layout), (None, None), "{command}");
}
}

#[test]
fn a_failed_command_keeps_the_layout_open_before_it() {
let mut s = two_layouts();
let steps = json!([
{"title": "Open A", "command": "layout.activate", "params": {"name": "A"}},
{"title": "Fail", "command": "layout.activate", "params": {"name": "none"}},
]);
s.run("task.new", &json!({"name": "Fails", "steps": steps})).unwrap();
assert!(s.run("task.run", &json!({"task": "Fails"})).is_err());
assert_eq!((s.active_layout, s.project.open_layout), (Some(1), Some(1)));
}

#[test]
fn switching_layouts_leaves_the_project_saved() {
let mut s = two_layouts();
s.mark_saved();
let state = s.state();
s.run("layout.activate", &json!({"name": "A"})).unwrap();
assert_eq!((s.state(), s.project.open_layout), (state, Some(0)));
s.run("layout.close", &Value::Null).unwrap();
assert_eq!((s.state(), s.project.open_layout), (state, None));
}

#[test]
fn a_session_made_from_a_project_keeps_its_open_layout() {
let s = two_layouts();
let mut copy = Session::new(s.project.clone());
assert_eq!(copy.active_layout, Some(1));
copy.run("project.rename", &json!({"name": "Copy"})).unwrap();
assert_eq!(copy.project.open_layout, Some(1));
}

#[test]
fn the_open_layout_fixture_opens_on_its_second_layout() {
let o = opened(include_str!("../../../../schema/fixtures/open_layout.rgis"));
assert_eq!(o.active_layout, Some(1));
assert!(!said_unread(&o));
}

/// Whether the saved text requires the capability an older reader refuses it by.
fn required(text: &str) -> bool {
let v: Value = serde_json::from_str(text).unwrap();
v["requires"].as_array().is_some_and(|r| r.contains(&json!("project-open-layout")))
}

#[test]
fn only_a_project_saved_with_a_layout_open_requires_the_capability() {
let mut s = two_layouts();
assert!(required(&saved(&mut s)));
assert!(required(&s.project.clone().recovery_text().unwrap()));
s.run("layout.close", &Value::Null).unwrap();
assert!(!required(&saved(&mut s)));
assert!(required(include_str!("../../../../schema/fixtures/open_layout.rgis")));
}

#[test]
fn a_task_that_opens_another_layout_and_saves_writes_it() {
let dir = Dir::new("task");
let path = dir.0.join("atlas.rgis");
let mut s = two_layouts();
for (name, then, open) in [
("To A", json!({"title": "Open A", "command": "layout.activate", "params": {"name": "A"}}), json!(0)),
("To the map", json!({"title": "Map", "command": "layout.close", "params": {}}), Value::Null),
] {
let steps = json!([then, {"title": "Save", "command": "project.saveAs", "params": {"path": path}}]);
s.run("task.new", &json!({"name": name, "steps": steps})).unwrap();
s.run("task.run", &json!({"task": name})).unwrap();
assert_eq!(written(&std::fs::read_to_string(&path).unwrap()), open, "{name}");
}
}
5 changes: 4 additions & 1 deletion crates/engine/src/tests/layout_pictures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ fn pictures_save_and_say_as_before() {
let li = s.project.layouts.len() - 1;
let name = s.project.layouts[li].name.clone();
assert_eq!((s.project.layouts.len(), name.as_str(), s.project.layouts[li].elements.len()), (1, "Pictures", 4));
let requires = serde_json::to_value(&s.project).unwrap()["requires"].to_string();
// As saved on the map view: an open layout adds a requirement of the project's own (#3446).
let mut on_map = s.project.clone();
on_map.open_layout = None;
let requires = serde_json::to_value(&on_map).unwrap()["requires"].to_string();
let saves = [
("layouts".to_string(), super::fnv_bytes(serde_json::to_string(&s.project.layouts).unwrap().as_bytes())),
(
Expand Down
4 changes: 3 additions & 1 deletion crates/engine/src/tests/layout_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ fn refused(s: &mut Session, command: &str, params: Value, said: &str) {

#[test]
fn a_table_without_the_new_options_saves_none_of_them() {
let (s, id) = tabled();
let (mut s, id) = tabled();
let stored = table(&s, id);
for key in ["fields", "text_size", "header"] {
assert!(stored.get(key).is_none(), "{key}: {stored}");
}
// On the map view: an open layout is the project's own requirement (#3446).
s.run("layout.close", &Value::Null).unwrap();
assert!(serde_json::to_value(&s.project).unwrap().get("requires").is_none());
}

Expand Down
Loading
Loading