diff --git a/crates/engine/src/cmd/blocks/attedit.rs b/crates/engine/src/cmd/blocks/attedit.rs index 9ab95c55f..2212082ee 100644 --- a/crates/engine/src/cmd/blocks/attedit.rs +++ b/crates/engine/src/cmd/blocks/attedit.rs @@ -3,7 +3,7 @@ //! time or by replacing a string in all of them. Attribute tags match in any case, as in AutoCAD. use cadcraft_doc::color::Color; -use cadcraft_doc::{AttribProps, Common, EntityKind, HAlign, Handle, Text, VAlign}; +use cadcraft_doc::{Attrib, AttribProps, Common, EntityKind, HAlign, Handle, Text, VAlign}; use cadcraft_geom::Vec3; use serde_json::{Map, Value, json}; @@ -124,13 +124,18 @@ pub struct AttributeRow { pub prompt: String, pub value: String, /// Where the attribute's text is inserted: two attributes can share a tag and a value, and a - /// reorder of the block's definitions moves them, so this is what tells them apart. + /// reorder of the block's definitions moves them. pub at: Vec3, /// Not constant: the value can be changed. pub editable: bool, pub invisible: bool, /// A multiline attribute: its value may hold line breaks. pub multiline: bool, + /// The whole stored attribute this row was made from. The fields above are what a list shows; + /// this is what a pending edit is bound to, so a row is stale when any stored property differs + /// (text height, rotation, alignment point, justification, style, its own layer/colour/ + /// linetype/lineweight overrides, its modes, …) and not only when the shown ones do. + pub record: Attrib, } /// The attributes of block reference `h`, in order (empty for other objects). The prompt comes @@ -156,6 +161,7 @@ pub fn attributes(d: &cadcraft_doc::Drawing, h: Handle) -> Vec { editable: !a.constant, invisible: a.invisible, multiline: a.multiline.is_some(), + record: a.clone(), }) .collect() } diff --git a/crates/ui-egui/src/attedit.rs b/crates/ui-egui/src/attedit.rs index 2fc831b94..4304b2b4c 100644 --- a/crates/ui-egui/src/attedit.rs +++ b/crates/ui-egui/src/attedit.rs @@ -5,7 +5,6 @@ //! JSON calls never open the dialog. use cadcraft_engine::doc::{Drawing, EntityKind, Handle}; -use cadcraft_geom::Vec3; use egui::{RichText, vec2}; use serde_json::{Map, Value, json}; @@ -18,25 +17,23 @@ pub struct Row { pub tag: String, pub prompt: String, pub value: String, - /// Where the attribute's text is inserted (see `cadcraft_engine::AttributeRow`): what tells - /// two attributes sharing a tag and a value apart. - pub at: Vec3, pub editable: bool, pub invisible: bool, pub multiline: bool, + /// The whole attribute the row was loaded from (see `cadcraft_engine::AttributeRow`): a row + /// index is only the attribute's place in the reference, and a reorder of the block's + /// definitions moves a different attribute — the same tag, value and insertion point is not + /// enough to tell them apart. + pub record: cadcraft_engine::doc::Attrib, pub typed: String, } impl Row { - /// Whether `a` is still the attribute this row was loaded from, as the row shows it. + /// Whether `a` is still the attribute this row was loaded from: the whole stored attribute must + /// be equal, so a property the dialog doesn't show (text height, rotation, justification, + /// style, the attribute's own overrides) tells a reordered one apart too. fn is(&self, a: &cadcraft_engine::AttributeRow) -> bool { - self.tag == a.tag - && self.prompt == a.prompt - && self.value == a.value - && self.at == a.at - && self.editable == a.editable - && self.invisible == a.invisible - && self.multiline == a.multiline + self.record == a.record } } @@ -90,10 +87,10 @@ fn load(app: &mut CadApp, h: Handle) { tag: a.tag, prompt: a.prompt, value: a.value, - at: a.at, editable: a.editable, invisible: a.invisible, multiline: a.multiline, + record: a.record, }) .collect(); let current = app.attedit.current; @@ -144,9 +141,9 @@ fn changed(rows: &[Row]) -> Map { rows.iter().enumerate().filter(|(_, r)| r.editable && r.typed != r.value).map(|(i, r)| (i.to_string(), Value::String(r.typed.clone()))).collect() } -/// True while the reference's attributes are still the ones the dialog listed: every property a -/// row shows, its place included (two attributes can share a tag and a value, and a reorder of the -/// block's definitions swaps those). +/// True while the reference's attributes are still the ones the dialog listed: every stored +/// property of every row's attribute, its place included (two attributes can share a tag and a +/// value, and a reorder of the block's definitions swaps those). fn rows_unchanged(app: &CadApp, h: Handle) -> bool { let Ok(st) = app.session.state() else { return false }; let now = cadcraft_engine::attributes(&st.doc, h); @@ -270,16 +267,37 @@ pub fn dialog(app: &mut CadApp, ctx: &egui::Context, open: &mut bool) { #[cfg(test)] mod tests { use super::*; + use cadcraft_geom::Vec3; fn row(tag: &str, value: &str, typed: &str, editable: bool) -> Row { Row { tag: tag.into(), prompt: String::new(), value: value.into(), - at: Vec3::default(), editable, invisible: false, multiline: false, + record: cadcraft_engine::doc::Attrib { + tag: tag.into(), + text: cadcraft_engine::doc::Text { + insert: Vec3::ZERO, + align_pt: None, + height: 1.0, + value: value.into(), + rotation: 0.0, + width_factor: 1.0, + oblique: 0.0, + style: "Standard".into(), + halign: cadcraft_engine::doc::HAlign::Left, + valign: cadcraft_engine::doc::VAlign::Baseline, + }, + invisible: false, + constant: !editable, + prompt: String::new(), + props: cadcraft_engine::doc::AttribProps::default(), + multiline: None, + modes: cadcraft_engine::doc::AttribModes::default(), + }, typed: typed.into(), } } diff --git a/crates/ui-egui/src/palettes.rs b/crates/ui-egui/src/palettes.rs index f36cccb2d..96293470e 100644 --- a/crates/ui-egui/src/palettes.rs +++ b/crates/ui-egui/src/palettes.rs @@ -583,8 +583,9 @@ fn edit_field(ui: &mut egui::Ui, id: egui::Id, value: String) -> Option /// Text waiting in an attribute's Properties field, kept with the attribute it was typed into: a /// reorder, an edit from elsewhere, or an undo can put another attribute in that place. The whole -/// row is kept, not just its tag and value: two attributes can share those and differ only in -/// where they are drawn, and a reorder of the block's definitions swaps those. +/// row is kept, which holds the whole stored attribute (`AttributeRow::record`): two attributes can +/// share a tag, a value and even an insertion point, and still differ in text height, rotation, +/// justification, style or their own overrides, which a reorder of the block's definitions swaps. #[derive(Clone)] struct PendingAttr { row: cadcraft_engine::AttributeRow, @@ -594,7 +595,8 @@ struct PendingAttr { /// [`edit_field`] for the value of attribute `a`, which the drawing has where this row is. The /// field is a place in the reference (addressed by index, which keeps attributes sharing a tag /// apart), so the pending text stays bound to the attribute it was typed into: when the place -/// holds another one, the text is dropped (and `stale` set) rather than written to it. +/// holds another one — any stored property differs, not only the tag, value and insertion point — +/// the text is dropped (and `stale` set) rather than written to it. fn attr_field(ui: &mut egui::Ui, id: egui::Id, a: &cadcraft_engine::AttributeRow, stale: &mut bool) -> Option { let pending = ui.data(|d| d.get_temp::(id)); if pending.as_ref().is_some_and(|p| p.row != *a) { diff --git a/crates/ui-egui/tests/attedit_dialog.rs b/crates/ui-egui/tests/attedit_dialog.rs index f742b6184..58b649a1a 100644 --- a/crates/ui-egui/tests/attedit_dialog.rs +++ b/crates/ui-egui/tests/attedit_dialog.rs @@ -394,6 +394,66 @@ fn a_stale_dialog_tells_equal_tag_and_value_attributes_apart_by_place() { assert!(error.contains("The attributes changed"), "the dialog says so: {error}"); } +/// A block "Twin" with two editable `T=v` attribute definitions at the same insertion point, their +/// texts 1 and 2 high, and one reference to it (selected): the two attributes share every field +/// the palette and the dialog show, so only a property they don't show tells them apart. +fn app_with_same_place_twins() -> (CadApp, Handle) { + let mut app = CadApp::new(Session::new(), Services::default()); + let mut hs = vec![app.run("line", json!({ "points": [[0, 0], [10, 0]] })).unwrap()["handles"][0].clone()]; + hs.push(app.run("attdef", json!({ "tag": "T", "default": "v", "at": [0, 1], "height": 1 })).unwrap()["handle"].clone()); + hs.push(app.run("attdef", json!({ "tag": "T", "default": "v", "at": [0, 1], "height": 2 })).unwrap()["handle"].clone()); + app.run("block", json!({ "name": "Twin", "base": [0, 0], "handles": hs, "keep": "delete" })).unwrap(); + let r = app.run("insert", json!({ "name": "Twin", "at": [100, 0] })).unwrap(); + let h = Handle::parse_hex(r["handle"].as_str().unwrap()).unwrap(); + app.session.set_selection(vec![h]); + (app, h) +} + +/// Every attribute's value by the height its text is drawn at, smallest first: the property that +/// tells [`app_with_same_place_twins`]'s two attributes apart after a reorder. +fn values_by_height(app: &CadApp, h: Handle) -> Vec<(f64, String)> { + match app.session.doc().unwrap().entity(h).map(|e| &e.kind) { + Some(EntityKind::Insert(i)) => { + let mut v: Vec<(f64, String)> = i.attribs.iter().map(|a| (a.text.height, a.text.value.clone())).collect(); + v.sort_by(|a, b| a.0.total_cmp(&b.0)); + v + } + other => panic!("{other:?}"), + } +} + +/// A reorder can leave every field the palette shows equal — tag, prompt, value and insertion point +/// — and still put another attribute in the place: the text heights differ. Text waiting in the +/// first field must not go to the attribute now in that place. +#[test] +fn a_pending_properties_value_does_not_follow_a_reorder_only_a_hidden_property_shows() { + let (app, h) = app_with_same_place_twins(); + let mut harness = palette(app); + harness.run(); + type_pending(&mut harness, "v", "EG"); + harness.state_mut().run("battman", json!({ "name": "Twin", "action": "down", "tag": "T" })).unwrap(); + harness.run(); + harness.key_press(egui::Key::Enter); + harness.run(); + assert_eq!(values_by_height(harness.state(), h), [(1.0, "v".to_string()), (2.0, "v".to_string())], "neither attribute changed"); + assert!(told_the_value_was_not_applied(&harness), "the user is told: {:?}", harness.state().session.log.last()); +} + +/// The Enhanced Attribute Editor draws the same line: a reorder that leaves every field it shows +/// in place, but swaps the attributes' text heights, is a stale dialog too. +#[test] +fn a_stale_dialog_tells_attributes_apart_by_a_property_it_does_not_show() { + let (mut app, h) = app_with_same_place_twins(); + menus::activate(&mut app, "attedit"); + assert_eq!(app.attedit.rows.len(), 2); + app.attedit.rows[0].typed = "EG".into(); + app.run("battman", json!({ "name": "Twin", "action": "down", "tag": "T" })).unwrap(); + assert!(!attedit::apply(&mut app), "the dialog refuses the stale row"); + assert_eq!(values_by_height(&app, h), [(1.0, "v".to_string()), (2.0, "v".to_string())], "nothing was written"); + let error = app.attedit.error.clone().unwrap_or_default(); + assert!(error.contains("The attributes changed"), "the dialog says so: {error}"); +} + /// Input queued for the frame that finds the row has another attribute belongs to the attribute /// the field had: neither its text nor its Enter may reach the one now in that place, and nothing /// may be kept for a later Enter either.