pdf: text and choice form fields were committed through each other's setters
- Commit
bb354fcea049b50946f04a695aee077e26ccebe2- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 03:39 UTC (5 days ago)
- Parents
449006c
The two DisposableEffect commit blocks in the form-field
overlay were transposed when the editable-combo UI was
added. A choice field disposed into set_text_field, which
writes the display string ('United States') verbatim as /V —
but /V must hold the export value ('US') with /I naming the
selected index, so consumers matching /Opt exports stopped
matching and /V//I were mutually inconsistent. A text field
disposed into set_choice_field, which builds a single-line
appearance for only the clicked widget and touches /I:
multiline/comb text rendered clipped and sibling widgets
kept stale appearances. Each Rust setter is individually
correct, which is why no test saw the swap.
Both branches now commit through a pure formFieldCommit
dispatch (0->TEXT, 2->CHOICE, else NONE), pinned by three
new tests. Rendering, annotations, page ops, search and
passwords were audited and cleared (a list of follow-up
findings was reported, strongest: non-ASCII field values
written as UTF-8 PDF text strings). Verified: cargo test
667 passed; :pdf:testDevUnitTest 305 tests pass; lint
errors are pre-existing on untouched files.Diff
pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfFormFields.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfFormFields.kt b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfFormFields.ktindex 1e21835426..fc322e320f 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfFormFields.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfFormFields.kt@@ -30,6 +30,16 @@ import com.vayunmathur.pdf.platform.setTextField import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch +/** Which native setter a field type commits through on dispose. */+internal enum class FormFieldCommit { TEXT, CHOICE, NONE }++/** Type 0 is a text field and type 2 a choice field (see SafeFormField). */+internal fun formFieldCommit(fieldType: Int): FormFieldCommit = when (fieldType) {+ 0 -> FormFieldCommit.TEXT+ 2 -> FormFieldCommit.CHOICE+ else -> FormFieldCommit.NONE+}+ /** Interactive form fields positioned over the page. */ @Composable internal fun FormFieldOverlay(@@ -63,7 +73,14 @@ internal fun FormFieldOverlay( DisposableEffect(field.id) { onDispose { if (text != field.value) {- scope.launch { document.setChoiceField(index, field.id, text); onEdited() }+ scope.launch {+ when (formFieldCommit(field.type)) {+ FormFieldCommit.TEXT -> document.setTextField(index, field.id, text)+ FormFieldCommit.CHOICE -> document.setChoiceField(index, field.id, text)+ FormFieldCommit.NONE -> Unit+ }+ onEdited()+ } } } }@@ -101,7 +118,14 @@ internal fun FormFieldOverlay( DisposableEffect(field.id) { onDispose { if (text != field.value) {- scope.launch { document.setTextField(index, field.id, text); onEdited() }+ scope.launch {+ when (formFieldCommit(field.type)) {+ FormFieldCommit.TEXT -> document.setTextField(index, field.id, text)+ FormFieldCommit.CHOICE -> document.setChoiceField(index, field.id, text)+ FormFieldCommit.NONE -> Unit+ }+ onEdited()+ } } } }
pdf/src/test/java/com/vayunmathur/pdf/ui/FormFieldCommitTest.kt
diff --git a/pdf/src/test/java/com/vayunmathur/pdf/ui/FormFieldCommitTest.kt b/pdf/src/test/java/com/vayunmathur/pdf/ui/FormFieldCommitTest.ktnew file mode 100644index 0000000000..3cc02e999d--- /dev/null+++ b/pdf/src/test/java/com/vayunmathur/pdf/ui/FormFieldCommitTest.kt@@ -0,0 +1,30 @@+package com.vayunmathur.pdf.ui++import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Pins the field-type → native-setter dispatch in [FormFieldOverlay].+ *+ * Regression: type 0 (text) committed through setChoiceField and type 2+ * (choice) through setTextField. A choice edit then wrote the display string+ * verbatim into /V (never the /Opt export value) and left /I stale, while a+ * text edit lost multiline/comb formatting and refreshed only one widget.+ */+class FormFieldCommitTest {++ @Test fun textFieldCommitsThroughTextSetter() {+ assertEquals(FormFieldCommit.TEXT, formFieldCommit(0))+ }++ @Test fun choiceFieldCommitsThroughChoiceSetter() {+ assertEquals(FormFieldCommit.CHOICE, formFieldCommit(2))+ }++ @Test fun nonTextTypesDoNotCommitText() {+ assertEquals(FormFieldCommit.NONE, formFieldCommit(1)) // checkbox / button+ assertEquals(FormFieldCommit.NONE, formFieldCommit(3)) // signature / other+ assertEquals(FormFieldCommit.NONE, formFieldCommit(-1))+ assertEquals(FormFieldCommit.NONE, formFieldCommit(99))+ }+}