Skip to content
356 changes: 296 additions & 60 deletions crates/engine/src/cmd/gp.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/engine/src/cmd/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ fn import_maps(s: &mut Session, p: &Value) -> CmdResult {
m.name = format!("{base} ({i})");
}
names.push(m.name.clone());
m.renew_instance_id();
s.project.maps.push(m);
}
// Repair what was read (rasters reopen, cameras clamp); the sources that read S3 wait for
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/cmd/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ fn duplicate_map(s: &mut Session, p: &Value) -> CmdResult {
let mut m = s.project.maps.get(i).ok_or("no map")?.clone();
m.name = super::copy_name(ps(p, "name"), &m.name, |n| s.project.maps.iter().any(|x| x.name.eq_ignore_ascii_case(n)));
m.label_lock = None;
m.renew_instance_id();
for l in &mut m.layers {
l.renew_instance_id();
l.selection.clear();
Expand Down
13 changes: 11 additions & 2 deletions crates/engine/src/cmd/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub fn commands() -> Vec<C> {
} else {
let mut m = a.data.clone();
m.name = map_name;
m.renew_instance_id();
// Reading a project checks only maps' layers, not offline snapshots', so their
// selections are put right here.
for l in &mut m.layers {
Expand Down Expand Up @@ -340,8 +341,11 @@ pub(crate) fn fill_cache(s: &mut Session) -> usize {

/// The longitude/latitude extent the flat map's view shows.
pub fn view_extent(s: &Session) -> Extent {
let m = s.map();
let (w, h) = s.view_size;
view_extent_of(s.map(), s.view_size)
}

/// The longitude/latitude extent a `size` view of map `m` shows.
pub fn view_extent_of(m: &rustgis_map::Map, (w, h): (f64, f64)) -> Extent {
let e = m.camera.extent(w, h);
let mut ll = Extent::EMPTY;
for i in 0..=8 {
Expand Down Expand Up @@ -727,6 +731,11 @@ fn offline_sync(s: &mut Session, p: &Value) -> CmdResult {
let name = m.name.clone();
*m = fresh.clone();
m.name = name;
// A copy of the source map: a map, and layers, of its own.
m.renew_instance_id();
for l in &mut m.layers {
l.renew_instance_id();
}
}
if let Some(a) = s.project.offline_areas.get_mut(ai) {
a.data = fresh;
Expand Down
283 changes: 283 additions & 0 deletions crates/engine/src/tests.rs

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions crates/map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,9 @@ pub struct Map {
pub body: String,
#[serde(default)]
next_layer_id: u32,
/// Process-local identity, kept by undo snapshots but never stored in a project.
#[serde(skip, default = "next_layer_instance")]
instance_id: u64,
}

fn earth() -> String {
Expand Down Expand Up @@ -1350,9 +1353,20 @@ impl Map {
time: None,
body: earth(),
next_layer_id: 1,
instance_id: next_layer_instance(),
}
}

/// Identifies this live map, whatever its name or place among the project's maps.
pub fn instance_id(&self) -> u64 {
self.instance_id
}

/// Starts a distinct live map after copying one. Undo snapshots keep the original identity.
pub fn renew_instance_id(&mut self) {
self.instance_id = next_layer_instance();
}

/// Every address the map keeps to read its data again (service groups and their layers'
/// sources, tile layers and their dated addresses, terrain tiles, layers' dataset, streamed and
/// raster sources, time-following Zarr stores). Local paths come too: callers judge each one.
Expand Down
40 changes: 40 additions & 0 deletions crates/ui-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,20 @@ impl RustGisApp {
/// made.
/// `active` is the active layer's name from [`Self::layer_acted_on`], read before it ran.
fn record_step(&mut self, id: &str, params: &Value, active: Option<(String, bool)>) {
if self.ws.recording.is_none() {
return;
}
// A tool run in the background is applied by `gp.finish`, whose run id means nothing to a
// replay: the step is the run itself, `gp.run` with its tool and parameters, from the
// history entry it has just added.
let finished;
let (id, params) = if id == "gp.finish" {
let Some(h) = self.session.project.gp_history.last().filter(|h| !h.id.is_empty()) else { return };
finished = json!({"tool": h.id, "params": h.params});
("gp.run", &finished)
} else {
(id, params)
};
let Some(steps) = self.ws.recording.as_mut() else { return };
// Undo doesn't take back a recorded step (the undo history and the recording don't line
// up step for step): say how to.
Expand Down Expand Up @@ -3764,6 +3778,8 @@ impl RustGisApp {
rustgis_engine::plugins::set_language(self.lang().code());
panes::poll_geolibre(self, ctx);
panes::poll_plugin_run(self, ctx);
#[cfg(not(target_arch = "wasm32"))]
panes::poll_tool_run(self, ctx);
self.poll_downloads();
let _ = self.deliver_gp_files(Ok(Value::Null));
// A value picked under Dynamic Text lasts while the text tool does, however the tool
Expand Down Expand Up @@ -4273,6 +4289,30 @@ mod record_tests {
assert_eq!(app.ws.recording.as_ref().map(Vec::len), Some(1));
}

/// A background run's `gp.finish` is recorded as the `gp.run` it finished.
#[test]
fn a_tool_finished_in_the_background_is_recorded_as_its_run() {
let mut app = RustGisApp::with_sample();
app.ws.recording = Some(Vec::new());
app.session.project.gp_history.push(rustgis_engine::GpRecord {
tool: "Buffer".into(),
id: "buffer".into(),
params: json!({"input": "Cities", "distance": 50}),
command: None,
at: 0,
ok: true,
messages: Vec::new(),
millis: 0,
outputs: Vec::new(),
});
app.record_step("gp.finish", &json!({"id": 7}), None);
let steps = app.ws.recording.as_deref().unwrap_or_default();
assert_eq!(
steps.iter().map(|s| (s.command.as_str(), s.params.clone())).collect::<Vec<_>>(),
[("gp.run", json!({"tool": "buffer", "params": {"input": "Cities", "distance": 50}}))]
);
}

/// A step keeping a plain `key` is recorded, and that is said once a recording.
#[test]
fn a_kept_key_is_said_once() {
Expand Down
Loading
Loading