pdf: outline drawer opens only on explicit user action (#952)
- Commit
f8582027d297909a793704edb6e555707df21abd- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 18:52 UTC (5 days ago)
- Parents
d2a238b
After rotating portrait -> landscape -> portrait, the outline drawer opened by itself. The Material DrawerState is an anchored-draggable state whose anchors are recomputed from the measured drawer width on every window size change, and the host activity handles rotation via configChanges, so the same state instance lived across the rotation and a width round trip could settle it open with no user action. Open intent now lives in the new saveable ui/PdfOutlineState, written only by explicit user actions (top-bar outline button, page select, back press, scrim/swipe dismissal fed back through the binding). BindPdfOutlineDrawer re-asserts the intent onto the draggable state on every width change, forcing a spuriously settled-open drawer back to closed, while an explicit open survives the same round trip. The wide layout's side pane is derived from the width class purely as layout and never reads or writes drawer intent. New PdfOutlineStateTest covers the portrait->landscape->portrait round trip against the real binding and drawer (stays closed unless opened, explicit open/close and scrim dismissal survive width changes), and ChromeAndFormsTest call sites updated for the new chrome parameter. Verified: :pdf:testDevUnitTest passes; lint findings are pre-existing (RouteScreenshotCoverage on Route.kt, ScaffoldSearch in untouched search UI).
Diff
pdf/AGENTS.md
diff --git a/pdf/AGENTS.md b/pdf/AGENTS.mdindex da30044f0f..9c81274929 100644--- a/pdf/AGENTS.md+++ b/pdf/AGENTS.md@@ -24,6 +24,7 @@ _Install: ./install pdf (dev by default)._ - `util/` holds only `PdfNative` (the Rust engine bridge) — don't add more files there; the capture model is in `data/` - Search-index prewarm at open is page-count gated (`SafePdfDocument.MAX_PREWARM_PAGES`, 256): opening a document never eagerly interprets the whole book; larger documents build the same index lazily on first search - Adobe YCCK (APP14 transform 2) CMYK JPEGs reach the Rust decoder with all four samples in inverted-ink polarity; the CMYK32 arm inverts all four before converting (transform 0 samples are already true ink)+- Outline drawer intent is owned by `ui/PdfOutlineState` (saveable, explicit user action only) and drives the Material drawer state; width changes are layout only and never open or close it, and the wide layout's side pane is derived from the width class, not from drawer state - Opening reads the document bytes once through a chunked, capped read (`SafePdfDocument.MAX_PDF_BYTES`, 256 MiB): oversized or unreadable inputs fail with the viewer error state instead of loading whole into the heap, the same read classifies encryption for the password prompt, and cut-and-glue appends use the same bounded read; redaction presence is probed lazily once editing starts, not at open ## Key files@@ -45,6 +46,7 @@ _Install: ./install pdf (dev by default)._ - com/vayunmathur/pdf/ui/SafePdfPageItem.kt - com/vayunmathur/pdf/ui/PdfViewerWideLayout.kt - com/vayunmathur/pdf/ui/PdfOutlineDrawer.kt+- com/vayunmathur/pdf/ui/PdfOutlineState.kt - com/vayunmathur/pdf/ui/SafePdfTextSelection.kt - com/vayunmathur/pdf/ui/SafePdfFormFields.kt ### ui/ — safe-draw rendering kit
pdf/src/main/java/com/vayunmathur/pdf/ui/PdfOutlineState.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/ui/PdfOutlineState.kt b/pdf/src/main/java/com/vayunmathur/pdf/ui/PdfOutlineState.ktnew file mode 100644index 0000000000..20b17f2b5b--- /dev/null+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/PdfOutlineState.kt@@ -0,0 +1,109 @@+package com.vayunmathur.pdf.ui++import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.saveable.Saver+import androidx.compose.runtime.saveable.rememberSaveable+import androidx.compose.runtime.setValue+import androidx.compose.runtime.snapshotFlow+import com.vayunmathur.library.ui.DrawerState+import com.vayunmathur.library.ui.DrawerValue+import com.vayunmathur.library.ui.WindowWidthClass++/**+ * Owns the outline drawer's open intent for the viewer.+ *+ * The modal drawer may open only as the result of an explicit user action+ * (the top-bar outline button). Its backing [DrawerState] is an+ * anchored-draggable state whose anchors are recomputed from the measured+ * drawer width whenever the window changes size; the host activity handles+ * rotation via `configChanges`, so that state instance lives across+ * portrait/landscape changes and a width round trip can settle it open with+ * no user action at all. Keeping the user's intent in this saveable holder+ * (independent of the draggable value and of layout) is what stops the+ * drawer from opening by itself: layout changes never write [drawerOpen].+ *+ * The wide layout's side pane is pure layout derived from [widthClass]:+ * showing or hiding it never changes [drawerOpen], and returning to a+ * compact width restores exactly the portrait intent (closed unless the user+ * opened the drawer, in which case it stays open).+ */+internal class PdfOutlineState(initialDrawerOpen: Boolean = false) {++ /** Explicit user intent: is the modal outline drawer open? */+ var drawerOpen by mutableStateOf(initialDrawerOpen)+ private set++ /** Current width bucket, tracked for layout decisions only. */+ var widthClass by mutableStateOf(WindowWidthClass.Compact)+ private set++ /** Whether the wide layout shows its side pane at the current width. */+ val showsSidePane: Boolean+ get() = widthClass == WindowWidthClass.Expanded++ /** Explicit user action: open the outline drawer. */+ fun openDrawer() {+ drawerOpen = true+ }++ /** Explicit user action: close the outline drawer. */+ fun closeDrawer() {+ drawerOpen = false+ }++ /**+ * Layout changed (rotation, resize, fold). Records the new bucket for+ * [showsSidePane] only; deliberately never touches [drawerOpen].+ */+ fun onWidthClassChanged(newWidthClass: WindowWidthClass) {+ widthClass = newWidthClass+ }++ /** The drawer reached Closed without [closeDrawer] (scrim tap, swipe). */+ fun onDrawerDismissed() {+ drawerOpen = false+ }++ companion object {+ /** Saveable form: only the user's open intent survives recreation. */+ val Saver: Saver<PdfOutlineState, Boolean> = Saver(+ save = { it.drawerOpen },+ restore = { PdfOutlineState(it) },+ )+ }+}++/** The viewer's outline state, restored across recreation. */+@Composable+internal fun rememberPdfOutlineState(): PdfOutlineState =+ rememberSaveable(saver = PdfOutlineState.Saver) { PdfOutlineState() }++/**+ * Drives [drawerState] (the Material anchored-draggable value) from+ * [state]'s explicit intent, and feeds genuine dismissals back.+ *+ * The assert effect re-runs on every width change, so a width round trip+ * that left the draggable settled at Open is forced back to Closed unless+ * the user explicitly opened the drawer; an explicit open survives the same+ * round trip untouched. Dismissals observed from the drawer (scrim/swipe)+ * clear the intent so a later width change cannot resurrect the drawer.+ */+@Composable+internal fun BindPdfOutlineDrawer(+ state: PdfOutlineState,+ drawerState: DrawerState,+ widthClass: WindowWidthClass,+) {+ LaunchedEffect(widthClass) { state.onWidthClassChanged(widthClass) }+ LaunchedEffect(state.drawerOpen, widthClass) {+ if (state.drawerOpen) drawerState.open() else drawerState.close()+ }+ LaunchedEffect(drawerState) {+ snapshotFlow { drawerState.currentValue }.collect { value ->+ if (value == DrawerValue.Closed) state.onDrawerDismissed()+ }+ }+}
pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerChrome.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerChrome.kt b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerChrome.ktindex 2d4d4b2969..7131e13186 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerChrome.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerChrome.kt@@ -90,6 +90,7 @@ internal fun SafePdfViewerChrome( scroll: SafePdfScrollState, zoom: SafePdfZoomState, drawerState: DrawerState,+ outlineState: PdfOutlineState, pageViewer: @Composable (Modifier) -> Unit, searchFocus: FocusRequester, onBack: () -> Unit,@@ -110,7 +111,7 @@ internal fun SafePdfViewerChrome( onSelectPage = { page -> scope.launch { if (page >= 0) scroll.scrollToPage(page, zoom.zoom)- drawerState.close()+ outlineState.closeDrawer() } }, ) {@@ -135,7 +136,8 @@ internal fun SafePdfViewerChrome( if (state.searching) { IconNavigation { state.searching = false; state.query = "" } } else if (bundle.outline.isNotEmpty()) {- IconButton({ scope.launch { drawerState.open() } }) { IconMenu() }+ // The only place the drawer opens: explicit user action.+ IconButton({ outlineState.openDrawer() }) { IconMenu() } } else { IconNavigation { onBack() } }
pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.ktindex 32a000bc70..4c305d8668 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt@@ -13,6 +13,7 @@ import androidx.compose.ui.res.stringResource import com.vayunmathur.library.ui.DrawerValue import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.ExternalIntents+import com.vayunmathur.library.ui.currentWindowWidthClass import com.vayunmathur.library.ui.rememberDrawerState import com.vayunmathur.library.util.AppMessages import com.vayunmathur.pdf.R@@ -46,7 +47,11 @@ fun SafePdfViewerScreen(uri: Uri, onBack: () -> Unit) { val document = bundle.document val scroll = rememberSafePdfScrollState()+ // Outline open intent is owned by [outlineState] (explicit user action+ // only); [drawerState] is just the draggable rendering of that intent.+ val outlineState = rememberPdfOutlineState() val drawerState = rememberDrawerState(DrawerValue.Closed)+ BindPdfOutlineDrawer(outlineState, drawerState, currentWindowWidthClass()) val (zoom, transform) = rememberSafePdfZoomState(scroll) val launchers = rememberSafePdfLaunchers(@@ -93,7 +98,7 @@ fun SafePdfViewerScreen(uri: Uri, onBack: () -> Unit) { BackHandler { when {- drawerState.isOpen -> scope.launch { drawerState.close() }+ drawerState.isOpen -> outlineState.closeDrawer() state.searching -> { state.searching = false; state.query = "" } state.editMode -> { state.editMode = false; state.selected = null } else -> onBack()@@ -129,6 +134,7 @@ fun SafePdfViewerScreen(uri: Uri, onBack: () -> Unit) { scroll = scroll, zoom = zoom, drawerState = drawerState,+ outlineState = outlineState, pageViewer = { modifier -> SafePdfPageViewer( modifier = modifier,
pdf/src/test/java/com/vayunmathur/pdf/ui/ChromeAndFormsTest.kt
diff --git a/pdf/src/test/java/com/vayunmathur/pdf/ui/ChromeAndFormsTest.kt b/pdf/src/test/java/com/vayunmathur/pdf/ui/ChromeAndFormsTest.ktindex 9d4154b9e8..d98ea61dda 100644--- a/pdf/src/test/java/com/vayunmathur/pdf/ui/ChromeAndFormsTest.kt+++ b/pdf/src/test/java/com/vayunmathur/pdf/ui/ChromeAndFormsTest.kt@@ -92,7 +92,7 @@ class ChromeAndFormsTest { SafePdfViewerChrome( state = state, bundle = bundle(null, LoadState.Loading), scroll = SafePdfScrollState(), zoom = SafePdfZoomState(),- drawerState = rememberDrawerState(DrawerValue.Closed),+ drawerState = rememberDrawerState(DrawerValue.Closed), outlineState = rememberPdfOutlineState(), pageViewer = { androidx.compose.foundation.layout.Box(it) { com.vayunmathur.library.ui.Text("PagesHere") } }, searchFocus = remember { FocusRequester() }, onBack = {}, onShare = {}, onSaveInPlace = {}, onSaveCopyName = {}, )@@ -112,7 +112,7 @@ class ChromeAndFormsTest { SafePdfViewerChrome( state = state, bundle = bundle(null, LoadState.Loading), scroll = SafePdfScrollState(), zoom = SafePdfZoomState(),- drawerState = rememberDrawerState(DrawerValue.Closed),+ drawerState = rememberDrawerState(DrawerValue.Closed), outlineState = rememberPdfOutlineState(), pageViewer = { androidx.compose.foundation.layout.Box(it) {} }, searchFocus = remember { FocusRequester() }, onBack = {}, onShare = {}, onSaveInPlace = {}, onSaveCopyName = {}, )@@ -132,7 +132,7 @@ class ChromeAndFormsTest { SafePdfViewerChrome( state = state, bundle = bundle(null, LoadState.Loading), scroll = SafePdfScrollState(), zoom = SafePdfZoomState(),- drawerState = rememberDrawerState(DrawerValue.Closed),+ drawerState = rememberDrawerState(DrawerValue.Closed), outlineState = rememberPdfOutlineState(), pageViewer = { androidx.compose.foundation.layout.Box(it) {} }, searchFocus = remember { FocusRequester() }, onBack = {}, onShare = {}, onSaveInPlace = {}, onSaveCopyName = {}, )
pdf/src/test/java/com/vayunmathur/pdf/ui/PdfOutlineStateTest.kt
diff --git a/pdf/src/test/java/com/vayunmathur/pdf/ui/PdfOutlineStateTest.kt b/pdf/src/test/java/com/vayunmathur/pdf/ui/PdfOutlineStateTest.ktnew file mode 100644index 0000000000..09ee7910e7--- /dev/null+++ b/pdf/src/test/java/com/vayunmathur/pdf/ui/PdfOutlineStateTest.kt@@ -0,0 +1,160 @@+package com.vayunmathur.pdf.ui++import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue+import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createAndroidComposeRule+import androidx.compose.ui.test.onNodeWithText+import com.vayunmathur.library.ui.DrawerValue+import com.vayunmathur.library.ui.DynamicTheme+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.WindowWidthClass+import com.vayunmathur.library.ui.rememberDrawerState+import com.vayunmathur.pdf.platform.SafeOutlineItem+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.GraphicsMode++/**+ * Issue #952: the outline drawer opened by itself after a+ * portrait -> landscape -> portrait rotation. The drawer's open intent is+ * owned explicitly by [PdfOutlineState]; width changes are layout only and+ * must never create or destroy that intent.+ */+@RunWith(RobolectricTestRunner::class)+@GraphicsMode(GraphicsMode.Mode.NATIVE)+class PdfOutlineStateTest {++ @get:Rule val rule = createAndroidComposeRule<androidx.activity.ComponentActivity>()++ private val rotationRoundTrip = listOf(+ WindowWidthClass.Compact,+ WindowWidthClass.Medium,+ WindowWidthClass.Expanded,+ WindowWidthClass.Medium,+ WindowWidthClass.Compact,+ )++ @Test fun drawerStartsClosedAndWidthChangesNeverOpenIt() {+ val state = PdfOutlineState()+ assertFalse(state.drawerOpen)+ for (width in rotationRoundTrip) {+ state.onWidthClassChanged(width)+ assertFalse(state.drawerOpen, "width $width must not open the drawer")+ }+ assertEquals(WindowWidthClass.Compact, state.widthClass)+ assertFalse(state.showsSidePane)+ }++ @Test fun widePaneIsLayoutOnlyAndLeavesDrawerIntentAlone() {+ val state = PdfOutlineState()+ state.onWidthClassChanged(WindowWidthClass.Expanded)+ assertTrue(state.showsSidePane)+ assertFalse(state.drawerOpen)+ state.onWidthClassChanged(WindowWidthClass.Compact)+ assertFalse(state.showsSidePane)+ assertFalse(state.drawerOpen)+ }++ @Test fun explicitOpenInPortraitSurvivesRotationRoundTrip() {+ val state = PdfOutlineState()+ state.openDrawer()+ for (width in rotationRoundTrip) {+ state.onWidthClassChanged(width)+ assertTrue(state.drawerOpen, "explicit open must survive width $width")+ }+ }++ @Test fun scrimDismissalClearsIntentAndStaysClosedAcrossWidths() {+ val state = PdfOutlineState()+ state.openDrawer()+ state.onDrawerDismissed()+ assertFalse(state.drawerOpen)+ for (width in rotationRoundTrip) {+ state.onWidthClassChanged(width)+ assertFalse(state.drawerOpen)+ }+ }++ @Test fun savedIntentRestoresThroughTheSaverConstructor() {+ // The Saver persists only the boolean intent; restoring is the+ // constructor the Saver's restore lambda calls.+ assertTrue(PdfOutlineState(true).drawerOpen)+ assertFalse(PdfOutlineState(false).drawerOpen)+ val state = PdfOutlineState()+ state.openDrawer()+ assertTrue(state.drawerOpen)+ state.closeDrawer()+ assertFalse(state.drawerOpen)+ }++ /**+ * Full binding regression: the real [BindPdfOutlineDrawer] ++ * [PdfOutlineDrawer] across a simulated width round trip. Width is the+ * state the binding consumes, so driving it stands in for rotation+ * (the host activity handles rotation via configChanges and never+ * recreates, which is exactly why the draggable value used to drift).+ */+ @Test fun boundDrawerFollowsIntentAcrossWidthRoundTrip() {+ var width by mutableStateOf(WindowWidthClass.Compact)+ lateinit var outlineState: PdfOutlineState+ lateinit var drawerState: com.vayunmathur.library.ui.DrawerState+ rule.setContent {+ DynamicTheme {+ val os = rememberPdfOutlineState()+ val ds = rememberDrawerState(DrawerValue.Closed)+ outlineState = os+ drawerState = ds+ BindPdfOutlineDrawer(os, ds, width)+ PdfOutlineDrawer(+ outline = listOf(SafeOutlineItem(0, 0, "Cover")),+ drawerState = ds,+ onSelectPage = {},+ ) { Text("Body") }+ }+ }+ rule.onNodeWithText("Body").assertIsDisplayed()+ rule.runOnIdle {+ assertFalse(outlineState.drawerOpen)+ assertEquals(DrawerValue.Closed, drawerState.currentValue)+ }++ // portrait -> landscape -> portrait: drawer must stay closed.+ for (w in rotationRoundTrip.drop(1)) {+ width = w+ rule.waitForIdle()+ rule.runOnIdle {+ assertFalse(outlineState.drawerOpen, "width $w opened the drawer by itself")+ assertEquals(DrawerValue.Closed, drawerState.currentValue, "width $w")+ }+ }++ // Explicit open in portrait survives the same round trip.+ rule.runOnIdle { outlineState.openDrawer() }+ rule.waitForIdle()+ rule.runOnIdle { assertEquals(DrawerValue.Open, drawerState.currentValue) }+ for (w in rotationRoundTrip.drop(1)) {+ width = w+ rule.waitForIdle()+ rule.runOnIdle {+ assertTrue(outlineState.drawerOpen)+ assertEquals(DrawerValue.Open, drawerState.currentValue, "width $w")+ }+ }++ // Explicit close in portrait also survives.+ rule.runOnIdle { outlineState.closeDrawer() }+ rule.waitForIdle()+ for (w in rotationRoundTrip.drop(1)) {+ width = w+ rule.waitForIdle()+ rule.runOnIdle { assertEquals(DrawerValue.Closed, drawerState.currentValue, "width $w") }+ }+ }+}