Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M6: Field Properties refuses a notation it couldn't store, and stays open with what was typed

Grzegorz Martens46382092f1cd
diff --git a/crates/forms/src/author/edit.rs b/crates/forms/src/author/edit.rs
index 54a05f3db..4f1a1a863 100644
--- a/crates/forms/src/author/edit.rs
+++ b/crates/forms/src/author/edit.rs
@@ -88,6 +88,12 @@ pub fn set_props(doc: &mut Document, name: &str, props: &FieldProps) -> Result<S
}
patch.validate()?;
}
+ // Refused here, before anything is written, rather than stored as a different formula.
+ if let Some(crate::af::Calculate::Notation(n)) = &props.calculate
+ && !crate::af::notation_storable(n)
+ {
+ return invalid(crate::af::NOTATION_NOT_STORABLE);
+ }
let all = fields(doc);
let f = all.iter().find(|f| f.name == name).ok_or_else(|| FormError::NoSuchField(name.into()))?.clone();
// Refuse a wrong type/index before any other requested property is written.
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index e57299fdc..4e0ba2137 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -1733,9 +1733,13 @@ fn finish_redactions_and_fields(app: &mut PdfCraftApp, req: &mut Pending) {
// The widget being edited, by object: a radio button that joins a group by its new name
// is appended after the group's own buttons, so its index changes.
let widget = before.as_ref().and_then(|all| all.iter().find(|f| f.name == d.field)?.widgets.get(d.widget).map(|w| w.obj));
- if app.apply_edit(Edit::SetFieldProps { name: d.field.clone(), props: Box::new(props) })
- && let Some(i) = app.active
- {
+ let applied = app.apply_edit(Edit::SetFieldProps { name: d.field.clone(), props: Box::new(props) });
+ if !applied && app.active_ids().is_some() {
+ // A refused edit (its reason is in the notice) leaves the dialog open with what was
+ // typed into it, as the several-fields dialog does, so nothing is lost meanwhile.
+ app.field_props = Some(d);
+ req.close = false;
+ } else if applied && let Some(i) = app.active {
// Keep the (possibly renamed) field selected.
// A dotted name is the full name; a plain one renames the field within its parent.
let typed = d.name.trim();
diff --git a/crates/ui-egui/tests/it/prepare.rs b/crates/ui-egui/tests/it/prepare.rs
index 3fad521e6..99858e3af 100644
--- a/crates/ui-egui/tests/it/prepare.rs
+++ b/crates/ui-egui/tests/it/prepare.rs
@@ -1404,6 +1404,42 @@ fn fields_sharing_a_name_each_show_their_own_formula() {
}
}
+/// Field Properties refuses a notation it couldn't store as written, and stays open with what was
+/// typed into it: the field keeps its formula and nothing in the dialog is lost. Once the notation
+/// is fixed, OK applies it and closes.
+#[test]
+fn a_notation_that_cannot_be_stored_keeps_field_properties_open() {
+ use pdfcraft_engine::form_scripts::Calculate;
+ use pdfcraft_engine::{Edit, FieldProps, NewField};
+ let mut h = harness();
+ for (i, name) in ["qty", "sub"].into_iter().enumerate() {
+ let y = 200.0 + 30.0 * i as f64;
+ let kind = NewField::Text { multiline: false };
+ h.state_mut().apply_edit(Edit::AddField { page: 0, rect: [20.0, y, 120.0, y + 20.0], kind, name: Some(name.into()) });
+ }
+ let props = Box::new(FieldProps { calculate: Some(Calculate::Notation("qty * 2".into())), ..Default::default() });
+ h.state_mut().apply_edit(Edit::SetFieldProps { name: "sub".into(), props });
+ let formula = |h: &Harness<'static, PdfCraftApp>| {
+ let s = h.state();
+ s.session.get(s.views[0].id).unwrap().form.iter().find(|f| f.name == "sub").unwrap().actions.calculate.clone()
+ };
+ h.state_mut().open_field_props("sub", 0);
+ h.run_steps(3);
+ let d = h.state_mut().field_props.as_mut().expect("Field Properties is open");
+ d.calculate = Calculate::Notation("qty EVCALC 2".into());
+ d.tooltip = "Subtotal".into();
+ h.get_by_label("OK").click();
+ h.run_steps(3);
+ let kept = h.state().field_props.as_ref().map(|d| (d.calculate.clone(), d.tooltip.clone()));
+ assert_eq!(kept, Some((Calculate::Notation("qty EVCALC 2".into()), "Subtotal".to_owned())), "the dialog stays open as typed");
+ assert_eq!(formula(&h), Calculate::Notation("qty * 2".into()), "the field keeps its formula");
+ h.state_mut().field_props.as_mut().expect("still open").calculate = Calculate::Notation("qty * 3".into());
+ h.get_by_label("OK").click();
+ h.run_steps(3);
+ assert!(h.state().field_props.is_none(), "applied and closed");
+ assert_eq!(formula(&h), Calculate::Notation("qty * 3".into()));
+}
+
/// A formula that reads a field the form doesn't have gets a card of its own, in place of "every
/// field calculates after the fields it reads", and its link opens that formula. A script that
/// builds the names it reads gets a note.