Skip to content
64 changes: 52 additions & 12 deletions crates/ui-egui/src/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@ use std::hash::{Hash, Hasher};
use crate::theme::{Tokens, semibold};
use crate::{RustGisApp, i18n};

/// A live map by what it is (`Map::instance_id`, whatever its place among the project's maps, so
/// moving it is not another map) and its name, which a rename changes.
pub(crate) type MapKey = (u64, String);

/// The map in view, as a dialog, an import or a form remembers it to find it again.
pub(crate) fn map_key(s: &Session) -> MapKey {
let map = s.map();
(map.instance_id(), map.name.clone())
}

/// The project (`Session::opened`) and the map in view a dialog or a form was opened on: layer ids
/// are a map's own, so what it captured is only good while both are still in view.
#[derive(Clone, Debug, PartialEq)]
pub struct MapOwner {
project: u64,
map: MapKey,
}

impl MapOwner {
pub(crate) fn of(s: &Session) -> Self {
Self { project: s.opened, map: map_key(s) }
}

/// Whether `s` still has that project open with that map in view (a moved map is the same one).
pub(crate) fn holds(&self, s: &Session) -> bool {
let map = s.map();
self.project == s.opened && self.map.0 == map.instance_id() && self.map.1 == map.name
}
}

#[derive(Default)]
pub struct Dialogs {
/// Project Properties: the system text last read, and what was said of it (and whether that is
Expand All @@ -20,7 +50,7 @@ pub struct Dialogs {
/// The layer captured when Define Projection opens; changing selection must not retarget it.
pub projection_layer: Option<u32>,
/// Map-local layer IDs require the dialog to retain its original project and map as well.
pub projection_map: Option<(u64, usize, String)>,
pub projection_map: Option<MapOwner>,
pub geometry: Option<crate::geometrycalc::Form>,
/// Publication Layout Wizard: what it has asked so far (#2850).
pub publication: Option<crate::publicationwizard::Draft>,
Expand All @@ -39,7 +69,7 @@ pub struct Dialogs {
/// The live instance captured with the map-local ID; Undo can make that ID reusable.
pub gpkg_instance: Option<u64>,
/// Layer IDs are map-local; source writes must stay in the captured project and map.
pub gpkg_map: Option<(u64, usize, String)>,
pub gpkg_map: Option<MapOwner>,
/// The source-backed layer last counted: its name for PostGIS, numeric ID for GeoPackage.
pub counted: String,
/// Revision after the last count command, which itself advances the session revision.
Expand Down Expand Up @@ -217,7 +247,7 @@ impl Dialogs {
match name {
"defineProjection" => {
self.projection_layer = active.map(|l| l.id);
self.projection_map = Some((s.opened, s.project.active_map, s.map().name.clone()));
self.projection_map = Some(MapOwner::of(s));
self.text = active.and_then(|l| l.dataset.crs).map(|c| c.definition_text()).unwrap_or_else(|| "EPSG:4326".into());
}
"coordinateSystem" => {
Expand Down Expand Up @@ -276,7 +306,7 @@ impl Dialogs {
let layer = active.filter(gpkg).or_else(|| s.map().layers.iter().find(gpkg));
self.gpkg_layer = layer.map(|l| l.id);
self.gpkg_instance = layer.map(|l| l.instance_id());
self.gpkg_map = Some((s.opened, s.project.active_map, s.map().name.clone()));
self.gpkg_map = Some(MapOwner::of(s));
}
// A table of addresses is what's usually geocoded: picked by its id (`flag` says the
// choice is a table), since names can repeat.
Expand All @@ -291,11 +321,7 @@ impl Dialogs {
}

fn gpkg_owner(&self, s: &Session) -> Result<(), String> {
if !self
.gpkg_map
.as_ref()
.is_some_and(|(project, index, name)| *project == s.opened && *index == s.project.active_map && *name == s.map().name)
{
if !self.gpkg_map.as_ref().is_some_and(|owner| owner.holds(s)) {
return Err(tl!("The active project or map changed; reopen the GeoPackage dialog for the intended layer.").into());
}
Ok(())
Expand Down Expand Up @@ -618,9 +644,7 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) {
}
}
close = buttons(app, ui, "Apply", |app| {
if !app.dialogs.projection_map.as_ref().is_some_and(|(project, index, name)| {
*project == app.session.opened && *index == app.session.project.active_map && *name == app.session.map().name
}) {
if !app.dialogs.projection_map.as_ref().is_some_and(|owner| owner.holds(&app.session)) {
return Err(tl!("The active project or map changed; reopen Define Projection for the intended layer.").into());
}
let id = app.dialogs.projection_layer.ok_or(tl!("No layer selected"))?;
Expand Down Expand Up @@ -3473,6 +3497,22 @@ fn calculation_preview(rows: &[rustgis_engine::cmd::edit::CalculationPreview]) -
mod tests {
use super::*;

/// The GeoPackage prompt belongs to the map it opened on, wherever that map goes among the maps;
/// another map or project refuses it (#2959).
#[test]
fn the_gpkg_prompt_follows_its_map_when_the_map_moves() {
let mut app = RustGisApp::with_sample();
let first = app.session.map().name.clone();
let dialogs = Dialogs { gpkg_map: Some(MapOwner::of(&app.session)), ..Dialogs::default() };
assert!(dialogs.gpkg_owner(&app.session).is_ok());
app.run("map.new", json!({"name": "Second"})).unwrap();
assert!(dialogs.gpkg_owner(&app.session).is_err(), "another map is in view");
app.run("map.activate", json!({"name": first})).unwrap();
app.run("map.move", json!({"name": first, "to": 1})).unwrap();
assert_eq!(app.session.project.active_map, 1, "the map in view moved to the end");
assert!(dialogs.gpkg_owner(&app.session).is_ok(), "the same map, moved");
}

#[test]
fn narrow_desktop_dialog_remembers_its_dragged_position() {
use egui::accesskit::Role;
Expand Down
38 changes: 33 additions & 5 deletions crates/ui-egui/src/graphicformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use rustgis_engine::map::graphics;
use crate::RustGisApp;
use crate::theme::Tokens;

/// A text graphic, by map (its index in the project), layer id and row: layer ids and rows are a
/// map's own.
type Key = (usize, u32, usize);
/// A text graphic, by map (`Map::instance_id`, whatever its place among the project's maps), layer
/// id and row: layer ids and rows are a map's own.
type Key = (u64, u32, usize);

#[derive(Default)]
pub struct GraphicFormat {
Expand Down Expand Up @@ -40,7 +40,7 @@ pub(crate) fn selected(app: &RustGisApp) -> Option<(u32, usize)> {
}

fn key(app: &RustGisApp, (layer, row): (u32, usize)) -> Key {
(app.session.project.active_map, layer, row)
(app.session.map().instance_id(), layer, row)
}

/// Opens the Format pane when another text is selected on the map (run each frame the map shows).
Expand Down Expand Up @@ -179,7 +179,7 @@ pub(crate) fn save_draft(app: &mut RustGisApp) {
return;
}
let (map, layer, row) = k;
let here = opened == app.session.opened && map == app.session.project.active_map;
let here = opened == app.session.opened && map == app.session.map().instance_id();
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let current = app.session.map().layer(layer).and_then(|l| graphics::label(&l.dataset, row)).map(str::to_string);
// A blank text goes to `graphics.edit` too, which refuses it and says so, as when Enter ends it.
if here && current.is_some_and(|c| c != text) {
Expand Down Expand Up @@ -270,6 +270,34 @@ mod tests {
assert!(edit(h.state_mut(), layer, row + 99).is_err());
}

/// The map with the text being typed moves (its tab, Move Up, `map.move`) and stays in view: the
/// pane keeps the text and saves it when it ends, instead of dropping it for "another map" (#2959).
#[test]
fn moving_the_map_keeps_the_text_being_typed() {
let mut app = RustGisApp::with_sample();
let added = app.run("graphics.add", json!({"shape": "text", "points": [[10, 50]], "text": "Old"})).unwrap();
let (layer, row) = (added["layer"].as_u64().unwrap() as u32, added["row"].as_u64().unwrap() as usize);
let first = app.session.map().name.clone();
let mut h = harness(app);
h.run_steps(2);
h.state_mut().run("select.rows", json!({"layer": layer, "rows": [row]})).unwrap();
h.run_steps(3);
h.query_all_by_role(Role::MultilineTextInput).find(|n| n.value().as_deref() == Some("Old")).unwrap().focus();
h.run_steps(2);
h.query_all_by_role(Role::MultilineTextInput).find(|n| n.is_focused()).unwrap().type_text("New");
h.run_steps(2);
h.state_mut().run("map.new", json!({"name": "Second"})).unwrap();
h.state_mut().run("map.activate", json!({"name": first})).unwrap();
h.state_mut().run("map.move", json!({"name": first, "to": 1})).unwrap();
assert_eq!(h.state().session.project.active_map, 1, "the map in view moved to the end");
h.state_mut().run("select.rows", json!({"layer": layer, "rows": [row]})).unwrap();
h.run_steps(3);
h.state_mut().run("ui.pane.format", json!({})).unwrap();
h.run_steps(2);
let label = h.state().session.map().layer(layer).and_then(|l| graphics::label(&l.dataset, row)).map(str::to_string);
assert!(label.as_deref().is_some_and(|t| t.contains("New")), "{label:?}");
}

/// A text cleared in the pane is refused, as `graphics.edit` refuses one: when Enter ends it and
/// when the pane closes over it alike. The text keeps what it said, and stays on the map.
#[test]
Expand Down
31 changes: 28 additions & 3 deletions crates/ui-egui/src/labelpane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ fn shown(c: &Class) -> String {
}

/// `l` as a layer of this map of this project: another map, or another project, may reuse its id.
/// A map goes by its place and its name, as Define Projection's does: deleting a map moves the
/// next one into its place, and a copy keeps its layer ids.
/// A map goes by what it is (`Map::instance_id`) and its name, as Define Projection's does: moving
/// it keeps its typed text, a map deleted before it doesn't hand its state to the next one, and a
/// copy keeps its layer ids and is another map. Renaming the map starts its layers' state afresh,
/// as it always did.
pub(crate) fn layer_key(app: &RustGisApp, l: &Layer) -> egui::Id {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
egui::Id::unique((app.session.opened, app.session.project.active_map, app.session.map().name.as_str(), l.id))
let map = app.session.map();
egui::Id::unique((app.session.opened, map.instance_id(), map.name.as_str(), l.id))
}

/// Where the class chosen on a layer is kept.
Expand Down Expand Up @@ -1433,6 +1436,28 @@ mod tests {
app.session.map().layers.iter().find(|l| l.name == name).unwrap().id
}

/// What the pane and the Unique Values box keep for a layer (the class chosen, a search, the
/// typed expression) is found again when its map moves among the maps; another map's layer of the
/// same id is another key (#2959).
#[test]
fn a_layers_state_follows_its_map_when_the_map_moves() {
let mut app = with_cities();
let id = layer_id(&app, "Cities");
let first = app.session.map().name.clone();
let key = |app: &RustGisApp| super::layer_key(app, app.session.map().layer(id).unwrap());
let before = key(&app);
app.run("map.new", json!({"name": "Second"})).unwrap();
app.run("map.activate", json!({"name": first})).unwrap();
app.run("map.move", json!({"name": first, "to": 1})).unwrap();
assert_eq!(app.session.project.active_map, 1, "the map in view moved to the end");
assert_eq!(key(&app), before, "the same map, moved");
let original = app.session.map().instance_id();
app.run("map.duplicate", json!({"name": first})).unwrap();
// The copy is named "<map> copy", which the key tells apart anyway: it is the identity that
// `map.duplicate` renews, and the key is built from it.
assert_ne!(app.session.map().instance_id(), original, "a copy keeps the layer ids and is another map");
}

fn with_cities() -> RustGisApp {
// Keep the font picker ready before the harness checks its role or settles the layout.
super::families::installed();
Expand Down
53 changes: 37 additions & 16 deletions crates/ui-egui/src/repairsource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pub enum Target {
#[cfg_attr(target_arch = "wasm32", expect(dead_code, reason = "the browser can't pick a replacement file, so it reads less of the form"))]
pub struct Form {
target: Target,
opened: u64,
map: (usize, String),
owner: crate::dialogs::MapOwner,
/// An index is not an identity: closing on a different archive prevents accidental retargeting.
archive: Option<(String, rustgis_engine::map::TileKind)>,
name: String,
Expand Down Expand Up @@ -57,8 +56,7 @@ impl Form {
};
Ok(Self {
target,
opened: s.opened,
map: (s.project.active_map, s.map().name.clone()),
owner: crate::dialogs::MapOwner::of(s),
archive: match target {
Target::Layer(_) => None,
Target::Tiles(index) => s.map().tiles.get(index).map(|t| (t.id.clone(), t.kind)),
Expand Down Expand Up @@ -132,22 +130,25 @@ impl Form {
}
}

/// Shows the captured source and runs the appropriate undoable repair command.
pub fn show(app: &mut RustGisApp, ctx: &egui::Context) {
let Some(f) = app.dialogs.repair.take() else {
app.dialogs.open = None;
return;
};
if app.session.opened != f.opened
|| app.session.project.active_map != f.map.0
|| app.session.map().name != f.map.1
/// The form's item is no longer where it was opened on: another project or map (a moved map is the
/// same one), or its layer or tile layer gone or replaced.
fn gone(app: &RustGisApp, f: &Form) -> bool {
!f.owner.holds(&app.session)
|| match f.target {
Target::Layer(id) => app.session.map().layer(id).is_none(),
Target::Tiles(index) => app.session.map().tiles.get(index).is_none_or(|t| {
t.archive_file() != Some(f.saved.as_str()) || f.archive.as_ref().is_none_or(|(id, kind)| t.id != *id || t.kind != *kind)
}),
}
{
}

/// Shows the captured source and runs the appropriate undoable repair command.
pub fn show(app: &mut RustGisApp, ctx: &egui::Context) {
let Some(f) = app.dialogs.repair.take() else {
app.dialogs.open = None;
return;
};
if gone(app, &f) {
app.dialogs.open = None;
return;
}
Expand Down Expand Up @@ -283,8 +284,7 @@ mod tests {
fn form() -> Form {
Form {
target: Target::Layer(1),
opened: 0,
map: (0, String::new()),
owner: crate::dialogs::MapOwner::of(&rustgis_engine::Session::new(rustgis_engine::Project::default())),
archive: None,
name: "Cities".into(),
saved: "old.gpkg".into(),
Expand All @@ -304,6 +304,27 @@ mod tests {
slot
}

/// The form stays for its map when that map moves among the maps; another map, or a layer that
/// is gone, closes it (#2959).
#[test]
fn the_form_follows_its_map_when_the_map_moves() {
let mut app = RustGisApp::with_sample();
let layer = app.session.map().layers[0].id;
let first = app.session.map().name.clone();
let mut f = form();
f.target = Target::Layer(layer);
f.owner = crate::dialogs::MapOwner::of(&app.session);
assert!(!gone(&app, &f));
app.run("map.new", json!({"name": "Second"})).unwrap();
assert!(gone(&app, &f), "another map is in view");
app.run("map.activate", json!({"name": first})).unwrap();
app.run("map.move", json!({"name": first, "to": 1})).unwrap();
assert_eq!(app.session.project.active_map, 1, "the map in view moved to the end");
assert!(!gone(&app, &f), "the same map, moved");
app.run("project.new", json!({})).unwrap();
assert!(gone(&app, &f), "another project");
}

#[test]
fn editing_only_invalidates_discovery_and_keeps_the_table_default() {
let mut f = form();
Expand Down
Loading
Loading