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
7 changes: 6 additions & 1 deletion crates/ui-egui/src/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ impl MapOwner {
Self { project: s.opened, map: map_key(s) }
}

/// Whether `s` still has the project open it was made in.
pub(crate) fn in_project(&self, s: &Session) -> bool {
self.project == s.opened
}

/// 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
self.in_project(s) && self.map.0 == map.instance_id() && self.map.1 == map.name
}
}

Expand Down
140 changes: 135 additions & 5 deletions crates/ui-egui/src/graphicformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) {
}
let _ = app.run("graphics.edit", p);
};
if settled.typing {
if settled.typing && text != current {
app.graphic_format.draft = Some((k, opened, text));
} else if settled.text && text != current {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
save(app, json!({"text": text}));
Expand Down Expand Up @@ -169,8 +169,9 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) {

/// Saves the text typed for a graphic the pane didn't draw this frame (the pane was closed or
/// hidden, or the selection changed) before its field could say it lost focus. A draft for
/// another project, or another map than the one shown, is dropped: its layer ids and rows aren't
/// this one's. Runs once a frame, after the panes and the view.
/// another project, or another map than the one shown, is dropped, and so is one whose graphic is
/// gone, and that is said (#2987): the layer ids and rows of another map aren't this one's. Runs once
/// a frame, after the panes and the view.
pub(crate) fn save_draft(app: &mut RustGisApp) {
let drawn = app.graphic_format.drawn.take();
let Some((k, opened, text)) = app.graphic_format.draft.take() else { return };
Expand All @@ -180,9 +181,24 @@ pub(crate) fn save_draft(app: &mut RustGisApp) {
}
let (map, layer, row) = k;
let here = opened == app.session.opened && map == app.session.map().instance_id();
let current = app.session.map().layer(layer).and_then(|l| graphics::label(&l.dataset, row)).map(str::to_string);
// Typing for a map no longer in view can't be saved there: it is dropped, and said.
if !here {
app.session.notify(tl!("Text typed for a graphic on another map or project was not saved.").to_string(), false);
return;
}
let Some(current) = app
.session
.map()
.layer(layer)
.filter(|l| graphics::kind(&l.dataset, row) == "text")
.map(|l| graphics::label(&l.dataset, row).unwrap_or_default().to_string())
else {
// Its layer was removed, or its row is no longer a text (a text whose text is empty still is one).
app.session.notify(tl!("Text typed for a graphic that is no longer there was not saved.").to_string(), false);
return;
};
// 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) {
if current != text {
let _ = app.run("graphics.edit", json!({"layer": layer, "row": row, "text": text}));
}
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}
Expand Down Expand Up @@ -286,6 +302,7 @@ mod tests {
h.run_steps(2);
h.query_all_by_role(Role::MultilineTextInput).find(|n| n.is_focused()).unwrap().type_text("New");
h.run_steps(2);
let mark = h.state().session.notice_mark();
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();
Expand All @@ -296,6 +313,119 @@ mod tests {
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:?}");
assert!(h.state().session.notices_since(mark).iter().all(|n| !n.text.contains("was not saved")), "moved, not dropped");
}

/// A text typed for a map that is then replaced in view is not saved, and that is said once (#2987);
/// one moved with its map is kept without a word (`moving_the_map_keeps_the_text_being_typed`).
#[test]
fn a_text_typed_for_a_map_no_longer_in_view_says_it_was_not_saved() {
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 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);
let mark = h.state().session.notice_mark();
h.state_mut().run("map.new", json!({"name": "Second"})).unwrap();
h.run_steps(3);
let said = |h: &egui_kittest::Harness<'_, RustGisApp>| {
h.state().session.notices_since(mark).iter().filter(|n| n.text.contains("was not saved")).count()
};
assert_eq!(said(&h), 1, "{:?}", h.state().session.notices_since(mark).iter().map(|n| &n.text).collect::<Vec<_>>());
h.run_steps(3);
assert_eq!(said(&h), 1, "once, not every frame");
assert!(h.state().graphic_format.draft.is_none());
}

/// A text typed for a graphic that is then gone (its layer removed) is not saved, and that is said once.
#[test]
fn a_text_typed_for_a_graphic_that_is_gone_says_it_was_not_saved() {
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 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);
let mark = h.state().session.notice_mark();
h.state_mut().run("layer.remove", json!({"layer": layer})).unwrap();
h.run_steps(3);
let said = h.state().session.notices_since(mark).iter().filter(|n| n.text.contains("was not saved")).count();
assert_eq!(said, 1, "{:?}", h.state().session.notices_since(mark).iter().map(|n| &n.text).collect::<Vec<_>>());
assert!(h.state().graphic_format.draft.is_none());
}

/// A text graphic whose text is empty (`graphics.style` accepts one) still exists: what is typed for
/// it is saved when the pane closes, not reported as gone.
#[test]
fn a_text_typed_for_a_graphic_with_empty_text_is_saved_not_reported_gone() {
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);
app.run("select.rows", json!({"layer": layer, "rows": [row]})).unwrap();
app.run("graphics.style", json!({"text": ""})).unwrap();
let mut h = harness(app);
h.run_steps(3);
h.query_all_by_role(Role::MultilineTextInput).find(|n| n.value().as_deref() == Some("")).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);
let mark = h.state().session.notice_mark();
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:?}");
assert!(h.state().session.notices_since(mark).iter().all(|n| !n.text.contains("no longer there")));
}

/// A text too long for a graphic (`graphics.edit` takes at most 2000 characters) is refused when the
/// pane closes over it, and that is said, not dropped in silence.
#[test]
fn a_text_too_long_to_save_says_why_when_the_pane_closes() {
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 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(&"x".repeat(2001));
h.run_steps(2);
let mark = h.state().session.notice_mark();
h.state_mut().run("ui.pane.format", json!({})).unwrap();
h.run_steps(2);
let said: Vec<String> = h.state().session.notices_since(mark).iter().map(|n| n.text.clone()).collect();
assert!(said.iter().any(|t| t.contains("characters: a graphic's text takes at most 2000")), "{said:?}");
}

/// A text field focused but not edited has nothing pending: switching map says nothing of it (#2987).
#[test]
fn a_focused_text_nobody_edited_leaves_no_notice_when_the_map_changes() {
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 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);
let mark = h.state().session.notice_mark();
h.state_mut().run("map.new", json!({"name": "Second"})).unwrap();
h.run_steps(3);
assert!(h.state().session.notices_since(mark).iter().all(|n| !n.text.contains("was not saved")));
}

/// A text cleared in the pane is refused, as `graphics.edit` refuses one: when Enter ends it and
Expand Down
80 changes: 73 additions & 7 deletions crates/ui-egui/src/serviceui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ pub struct Import {
/// The saved web-service connection to record on imported layers, not a group-plan parameter.
connection: Option<Value>,
token: u64,
/// The originating map (`dialogs::map_key`), so a deleted, replaced or renamed map cancels the
/// import and a moved one doesn't.
map: crate::dialogs::MapKey,
/// The project and map it started on (`dialogs::MapOwner`), so another project, or a deleted,
/// replaced or renamed map, cancels the import and a moved one doesn't.
owner: crate::dialogs::MapOwner,
slot: Slot<Result<Vec<u8>, String>>,
document: Option<String>,
current: Option<String>,
Expand Down Expand Up @@ -342,7 +342,7 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin
refresh,
connection,
token: app.session.download_started(),
map: crate::dialogs::map_key(&app.session),
owner: crate::dialogs::MapOwner::of(&app.session),
slot: Slot::default(),
document: None,
current: None,
Expand Down Expand Up @@ -466,8 +466,16 @@ impl Import {

pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) {
let Some(mut task) = app.ui.service_import.take() else { return };
if !app.session.download_is_pending(task.token) || crate::dialogs::map_key(&app.session) != task.map {
let pending = app.session.download_is_pending(task.token);
let moved = !task.owner.holds(&app.session);
if !pending || moved {
app.session.download_failed(task.token);
// Said unless the download was already over before the map moved (it failed elsewhere,
// say). Opening another project drops or puts aside its downloads, so they are never
// pending here.
if moved && (pending || !task.owner.in_project(&app.session)) {
app.session.notify(tl!("The service import was cancelled because the map or project in view changed.").to_string(), false);
}
return;
}
let landed = task.slot.lock().unwrap_or_else(|e| e.into_inner()).take();
Expand Down Expand Up @@ -682,6 +690,64 @@ fn noted(app: &mut RustGisApp, landed: &Landed, note: Option<String>) {
mod tests {
use super::*;

/// An import in flight that is given up because the map in view changed says so, once; one whose
/// download is already over says nothing (#2987).
#[test]
fn an_import_given_up_for_another_map_says_so() {
let mut app = RustGisApp::with_sample();
let first = app.session.map().name.clone();
let task = |app: &mut RustGisApp| Import {
params: json!({}),
refresh: false,
connection: None,
token: app.session.download_started(),
owner: crate::dialogs::MapOwner::of(&app.session),
slot: Slot::default(),
document: Some("https://example.test/capabilities".into()),
current: None,
remaining: VecDeque::new(),
responses: Vec::new(),
bytes: 0,
};
let ctx = egui::Context::default();
let said = |app: &RustGisApp, mark: u64| {
app.session.notices_since(mark).iter().filter(|n| n.text.contains("service import was cancelled")).count()
};
// Another map in view: said once.
let mark = app.session.notice_mark();
let import = task(&mut app);
app.ui.service_import = Some(import);
app.run("map.new", json!({"name": "Second"})).unwrap();
poll(&mut app, &ctx);
assert_eq!(said(&app, mark), 1);
assert!(app.ui.service_import.is_none());
// Its map renamed: the name is part of what it finds its map by.
app.run("map.activate", json!({"name": first})).unwrap();
let mark = app.session.notice_mark();
let import = task(&mut app);
app.ui.service_import = Some(import);
app.run("map.rename", json!({"name": "Renamed"})).unwrap();
poll(&mut app, &ctx);
assert_eq!(said(&app, mark), 1);
// A download already over (failed elsewhere, say): nothing more to say.
let mark = app.session.notice_mark();
let import = task(&mut app);
app.session.download_failed(import.token);
app.ui.service_import = Some(import);
app.run("map.activate", json!({"name": "Second"})).unwrap();
poll(&mut app, &ctx);
assert_eq!(said(&app, mark), 0);
// Another project opened: its download is put aside with the project, so it is no longer
// pending here, and that is said all the same.
app.run("map.activate", json!({"name": "Second"})).unwrap();
let mark = app.session.notice_mark();
let import = task(&mut app);
app.ui.service_import = Some(import);
app.run("project.new", json!({})).unwrap();
poll(&mut app, &ctx);
assert_eq!(said(&app, mark), 1);
}

/// An import in flight goes on when its map moves among the maps, and is given up when another
/// map is in view (#2959).
#[test]
Expand All @@ -694,7 +760,7 @@ mod tests {
refresh: false,
connection: None,
token,
map: crate::dialogs::map_key(&app.session),
owner: crate::dialogs::MapOwner::of(&app.session),
slot: Slot::default(),
document: Some("https://example.test/capabilities".into()),
current: None,
Expand Down Expand Up @@ -1017,7 +1083,7 @@ mod tests {
refresh: false,
connection: None,
token: 0,
map: (0, String::new()),
owner: crate::dialogs::MapOwner::of(&rustgis_engine::Session::new(rustgis_engine::Project::default())),
slot: Slot::default(),
document: None,
current: None,
Expand Down
Loading