diff --git a/crates/ui-egui/src/graphicformat.rs b/crates/ui-egui/src/graphicformat.rs index d2deadc340..fa9febc6db 100644 --- a/crates/ui-egui/src/graphicformat.rs +++ b/crates/ui-egui/src/graphicformat.rs @@ -14,6 +14,14 @@ use crate::theme::Tokens; /// id and row: layer ids and rows are a map's own. type Key = (u64, u32, usize); +/// A refused edit stays in its box while the same graphic still holds the text it was typed over. +struct RefusedText { + graphic: Key, + opened: u64, + original: String, + text: String, +} + #[derive(Default)] pub struct GraphicFormat { /// The text the pane last opened for: a new one opens it again, so closing the pane keeps the @@ -21,6 +29,7 @@ pub struct GraphicFormat { formatted: Option, /// The text being typed: its graphic, the project it is for (`Session::opened`), and the text. draft: Option<(Key, u64, String)>, + refused: Option, /// The graphic the pane drew the form of this frame: a draft for any other is saved at the /// end of the frame (`save_draft`). drawn: Option, @@ -105,12 +114,13 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) { let k = key(app, (layer, row)); let opened = app.session.opened; app.graphic_format.drawn = Some(k); + let refused = app.graphic_format.refused.take().filter(|r| r.graphic == k && r.opened == opened && r.original == current); // A draft for another text is left for `save_draft`. let mut text = match app.graphic_format.draft.take() { Some((dk, p, t)) if dk == k && p == opened => t, other => { app.graphic_format.draft = other; - current.clone() + refused.as_ref().map_or(¤t, |r| &r.text).clone() } }; let heading = crate::i18n::fmt("Map: {kind}", &[("kind", tl_raw!("Text"))]); @@ -135,9 +145,11 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) { named_fonts: false, select, }; - let settled = + let Some(settled) = crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("graphic_format", k), &[graphics::TOKENS], Vec::new, fields)) - .unwrap_or_default(); + else { + return; + }; let save = |app: &mut RustGisApp, change: serde_json::Value| { let mut p = json!({"layer": layer, "row": row}); if let (Some(p), Some(c)) = (p.as_object_mut(), change.as_object()) { @@ -145,10 +157,12 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) { } let _ = app.run("graphics.edit", p); }; - if settled.typing && text != current { + if refused.as_ref().is_some_and(|r| r.text == text) { + app.graphic_format.refused = refused; + } else if settled.typing && text != current { app.graphic_format.draft = Some((k, opened, text)); - } else if settled.text && text != current { - save(app, json!({"text": text})); + } else if settled.text && text != current && app.run("graphics.edit", json!({"layer": layer, "row": row, "text": text})).is_err() { + app.graphic_format.refused = Some(RefusedText { graphic: k, opened, original: current, text }); } if settled.size { save(app, json!({"fontSize": size})); @@ -178,6 +192,9 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) { /// a frame, after the panes and the view. pub(crate) fn save_draft(app: &mut RustGisApp) { let drawn = app.graphic_format.drawn.take(); + if drawn.is_none() { + app.graphic_format.refused = None; + } let Some((k, opened, text)) = app.graphic_format.draft.take() else { return }; if Some(k) == drawn && opened == app.session.opened { app.graphic_format.draft = Some((k, opened, text)); @@ -290,6 +307,33 @@ mod tests { assert!(edit(h.state_mut(), layer, row + 99).is_err()); } + #[test] + fn a_refused_graphic_text_stays_in_the_box_until_corrected() { + let mut app = RustGisApp::with_sample(); + place(&mut app, 10.0, 50.0, Some("Old")).unwrap(); + let (layer, row) = selected(&app).unwrap(); + let mut h = harness(app); + h.run_steps(3); + let long = "x".repeat(2001); + let mark = h.state().session.notice_mark(); + h.get_by_role(Role::MultilineTextInput).type_text(&long); + h.run_steps(2); + h.get_by_role_and_label(Role::CheckBox, "Bold").focus(); + h.run_steps(3); + let saved = |app: &RustGisApp| graphics::label(&app.session.map().layer(layer).unwrap().dataset, row).unwrap().to_owned(); + assert_eq!(saved(h.state()), "Old"); + assert_eq!(h.get_by_role(Role::MultilineTextInput).value().as_deref(), Some(long.as_str())); + assert_eq!(h.state().session.notices_since(mark).iter().filter(|n| n.error).count(), 1); + h.get_by_role(Role::MultilineTextInput).focus(); + h.run_steps(2); + h.key_press(egui::Key::End); + h.key_press(egui::Key::Backspace); + h.run_steps(2); + h.get_by_role_and_label(Role::CheckBox, "Bold").focus(); + h.run_steps(3); + assert_eq!(saved(h.state()), "x".repeat(2000)); + } + /// 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]