pdf: add route screenshot captures and scaffold search
- Commit
d83fc0009160dee56743958640f2cd66f6b9a41e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-02 03:43 UTC (4 days ago)
- Parents
e75e64f
Give every Route leaf a deterministic screenshot capture in MetadataPreviews: rename the outline/pages/home/expanded previews to 2-viewer/6-cut-glue/1-home/7-viewer-expanded and add 3-capture, 4-capture-page and 5-crop captures built from literal pages. The capture capture renders CaptureContent, a new stateless slice split out of CapturePdfScreen for the no-camera-permission landing, which CapturePdfScreen now uses; the crop capture renders CropOverlay over a fixed quadrilateral. Migrate viewer text search in SafePdfViewerChrome from a hand-rolled TextField in the AppScaffold title slot to the scaffold's search parameter (ScaffoldSearch with the search focus requester), fixing the ScaffoldSearch lint error. The case-sensitive Aa toggle moves to the scaffold actions beside match navigation so no behaviour is lost. Update pdf/AGENTS.md for CaptureContent, scaffold search and route captures. Verified: ./gradlew :pdf:lint :pdf:lintVitalRelease :pdf:testDevUnitTest :pdf:compileDevScreenshotTestKotlin pass.
Diff
pdf/AGENTS.md
diff --git a/pdf/AGENTS.md b/pdf/AGENTS.mdindex 00e4d1ee66..0494bebc8f 100644--- a/pdf/AGENTS.md+++ b/pdf/AGENTS.md@@ -27,6 +27,7 @@ _Install: ./install pdf (dev by default)._ - Wire format 2 carries raw rasters as little-endian ARGB *words* (`0xAARRGGBB`); Kotlin must read them as words (int-based Bitmap APIs), never `Bitmap.copyPixelsFromBuffer` — on a real device that reads ARGB_8888 memory as R,G,B,A bytes and swaps R/B (captured scans are FlateDecode, so they all hit this arm; JPEG pages pass through untouched), while Robolectric reads the buffer the other way and cannot catch the swap. The device round-trip is pinned in `PdfExporterDeviceTest.capturedPdfReopensWithFaithfulColours` - Wire format is v11: the Image arm carries a per-image `/Interpolate` byte decided in Rust (`image_should_interpolate` — bilevel art is never smoothed, contone and explicit `/Interpolate true` are), and Kotlin applies it as `isFilterBitmap`; bumping the wire version without writing the byte desyncs every later primitive - 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+- Viewer text search is declared through `SafePdfViewerChrome`'s `AppScaffold(search = ScaffoldSearch(...))` while `state.searching`; the case-sensitive (`Aa`) toggle and match navigation stay in the scaffold actions. Every `Route` leaf has a deterministic capture in `src/screenshotTest` (`<n>-<route-kebab>` in `MetadataPreviews`) - Opening hands native a file descriptor (`SafePdfDocument.acquireDocumentFd` -> `PdfNative.openDocumentFd`): Rust dups and mmaps it, so no whole-file copy exists in the Kotlin heap or across JNI and file size alone never refuses an open; that one attempt classifies Opened / NeedsPassword / Failed (incl. not-a-PDF). Providers without a descriptor fall back to spooling the stream to a cache file in chunks (O(chunk) memory) and opening its descriptor. Cut-and-glue appends take the same descriptor path (`appendPdfFd`). The byte[] native API remains for small in-memory documents and keeps its own native size guard. Redaction presence is probed lazily once editing starts, not at open ## Key files@@ -71,6 +72,7 @@ _Install: ./install pdf (dev by default)._ - com/vayunmathur/pdf/ui/SafePdfLaunchers.kt ### ui/ — capture + assembly screens - com/vayunmathur/pdf/ui/CapturePdfScreen.kt+- com/vayunmathur/pdf/ui/CaptureContent.kt — stateless no-camera-permission capture landing (screenshot slice) - com/vayunmathur/pdf/ui/CropScreen.kt - com/vayunmathur/pdf/ui/CropScreenSection.kt - com/vayunmathur/pdf/ui/CutGlueScreen.kt
pdf/src/main/java/com/vayunmathur/pdf/ui/CaptureContent.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/ui/CaptureContent.kt b/pdf/src/main/java/com/vayunmathur/pdf/ui/CaptureContent.ktnew file mode 100644index 0000000000..8a7009ed07--- /dev/null+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/CaptureContent.kt@@ -0,0 +1,38 @@+package com.vayunmathur.pdf.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.padding+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.Button+import com.vayunmathur.library.ui.Text+import com.vayunmathur.pdf.R++/**+ * Stateless capture landing shown when camera permission is not granted:+ * request permission or upload an image instead. Split out of+ * [CapturePdfScreen] so screenshot captures render it from literals.+ */+@Composable+fun CaptureContent(+ onRequestPermission: () -> Unit,+ onUploadImage: () -> Unit,+) {+ Column(+ modifier = Modifier.fillMaxSize(),+ verticalArrangement = Arrangement.Center,+ horizontalAlignment = Alignment.CenterHorizontally,+ ) {+ Button(onClick = onRequestPermission) {+ Text(stringResource(R.string.request_camera_permission))+ }+ Button(onClick = onUploadImage, Modifier.padding(top = 8.dp)) {+ Text(stringResource(R.string.upload_image))+ }+ }+}
pdf/src/main/java/com/vayunmathur/pdf/ui/CapturePdfScreen.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/ui/CapturePdfScreen.kt b/pdf/src/main/java/com/vayunmathur/pdf/ui/CapturePdfScreen.ktindex 65a0337755..e4c63cd1a0 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/ui/CapturePdfScreen.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/CapturePdfScreen.kt@@ -10,7 +10,6 @@ import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box-import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding@@ -22,7 +21,6 @@ import androidx.compose.foundation.shape.RoundedCornerShape import com.vayunmathur.library.util.AppMessages import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.BottomAppBar-import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.Icon import com.vayunmathur.library.ui.IconButton@@ -256,18 +254,10 @@ fun CapturePdfScreen( } ) } else {- Column(- modifier = Modifier.fillMaxSize(),- verticalArrangement = Arrangement.Center,- horizontalAlignment = Alignment.CenterHorizontally- ) {- Button(onClick = cameraPermissionRequest) {- Text(stringResource(R.string.request_camera_permission))- }- Button(onClick = { galleryLauncher.launch("image/*") }, Modifier.padding(top = 8.dp)) {- Text(stringResource(R.string.upload_image))- }- }+ CaptureContent(+ onRequestPermission = cameraPermissionRequest,+ onUploadImage = { galleryLauncher.launch("image/*") },+ ) } } }
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 7131e13186..cc145b72b7 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerChrome.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerChrome.kt@@ -2,10 +2,8 @@ package com.vayunmathur.pdf.ui import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize-import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue@@ -13,7 +11,6 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue-import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester@@ -21,6 +18,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Checkbox+import com.vayunmathur.library.ui.ScaffoldSearch import com.vayunmathur.library.ui.DrawerState import com.vayunmathur.library.ui.DropdownMenu import com.vayunmathur.library.ui.DropdownMenuItem@@ -45,7 +43,6 @@ import com.vayunmathur.library.ui.IconVisible import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.SmallFloatingActionButton import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.TextField import com.vayunmathur.library.ui.isExpandedWidth import com.vayunmathur.pdf.R import com.vayunmathur.pdf.platform.SafePdfDocument@@ -117,20 +114,16 @@ internal fun SafePdfViewerChrome( ) { AppScaffold( modifier = Modifier.fillMaxSize(),- title = {- if (state.searching) {- Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {- TextField(- value = state.query,- onValueChange = { state.query = it },- modifier = Modifier.weight(1f).focusRequester(searchFocus),- placeholder = { Text(stringResource(R.string.search_label)) },- singleLine = true,- )- Checkbox(checked = state.caseSensitive, onCheckedChange = { state.caseSensitive = it })- Text(stringResource(R.string.aa), style = MaterialTheme.typography.labelSmall)- }- }+ title = {},+ search = if (state.searching) {+ ScaffoldSearch(+ query = state.query,+ onQueryChange = { state.query = it },+ placeholder = stringResource(R.string.search_label),+ modifier = Modifier.focusRequester(searchFocus),+ )+ } else {+ null }, navigationIcon = { if (state.searching) {@@ -144,6 +137,8 @@ internal fun SafePdfViewerChrome( }, actions = { if (state.searching) {+ Checkbox(checked = state.caseSensitive, onCheckedChange = { state.caseSensitive = it })+ Text(stringResource(R.string.aa), style = MaterialTheme.typography.labelSmall) if (state.matches.isNotEmpty()) { Text( "${state.matchIndex + 1}/${state.matches.size}",
pdf/src/screenshotTest/kotlin/com/vayunmathur/pdf/ui/MetadataPreviews.kt
diff --git a/pdf/src/screenshotTest/kotlin/com/vayunmathur/pdf/ui/MetadataPreviews.kt b/pdf/src/screenshotTest/kotlin/com/vayunmathur/pdf/ui/MetadataPreviews.ktindex 84ffeeae0b..8841d9d860 100644--- a/pdf/src/screenshotTest/kotlin/com/vayunmathur/pdf/ui/MetadataPreviews.kt+++ b/pdf/src/screenshotTest/kotlin/com/vayunmathur/pdf/ui/MetadataPreviews.kt@@ -1,18 +1,24 @@ package com.vayunmathur.pdf.ui import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview+import androidx.compose.ui.unit.dp import com.android.tools.screenshot.PreviewTest+import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.DrawerValue import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.rememberDrawerState+import com.vayunmathur.pdf.data.Quadrilateral import com.vayunmathur.pdf.ui.InitialScreen import com.vayunmathur.pdf.platform.PdfPrimitive import com.vayunmathur.pdf.platform.SafeOutlineItem@@ -189,8 +195,10 @@ private val SampleOutline = listOf( * through the native `pdf_render` library, which Layoutlib cannot load, and its ~40 pieces * of edit state are welded to that handle. What it is built from can be previewed, so these * images use the pieces that were split out of it — [PdfOutlineDrawer] and- * [SafePdfPageCanvas] — over the literal document above, plus the two screens that were- * already free of the native handle.+ * [SafePdfPageCanvas] — over the literal document above, plus the screens and+ * stateless slices that are free of the native handle ([InitialScreen],+ * [CaptureContent], [CropOverlay], [CutGlueContent]). Every `Route` leaf has a+ * capture named `<n>-<route-kebab>`; the wide viewer is a `viewer` variant. * * Each preview needs @PreviewTest as well as @Preview: @Preview alone renders in Studio but * is not collected as a screenshot test. Previews must also be class members, not top-level@@ -199,9 +207,18 @@ private val SampleOutline = listOf( class MetadataPreviews { @PreviewTest- @Preview(name = "1-outline", device = PHONE, group = "phone", showSystemUi = true)+ @Preview(name = "1-home", device = PHONE, group = "phone", showSystemUi = true) @Composable- fun Preview1Outline() {+ fun Preview1Home() {+ DynamicTheme(darkTheme = true) {+ InitialScreen(onOpenPdf = {}, onCapturePdf = {}, onCutGlue = {})+ }+ }++ @PreviewTest+ @Preview(name = "2-viewer", device = PHONE, group = "phone", showSystemUi = true)+ @Composable+ fun Preview2Viewer() { DynamicTheme(darkTheme = true) { PdfOutlineDrawer( outline = SampleOutline,@@ -218,32 +235,79 @@ class MetadataPreviews { } @PreviewTest- @Preview(name = "2-pages", device = PHONE, group = "phone", showSystemUi = true)+ @Preview(name = "3-capture", device = PHONE, group = "phone", showSystemUi = true) @Composable- fun Preview2Pages() {+ fun Preview3Capture() { DynamicTheme(darkTheme = true) {- val pages = listOf(Page1, Page2, Page3, Page4, Page5, Page6)- CutGlueContent(- pageKeys = pages.indices.map { it.toLong() },- renderPage = { pages.getOrNull(it) },- initialPages = pages.withIndex().associate { (i, page) -> i.toLong() to page },- )+ AppScaffold(title = "Capture PDF", onNavigateBack = {}) { padding ->+ Box(Modifier.fillMaxSize().padding(padding)) {+ CaptureContent(onRequestPermission = {}, onUploadImage = {})+ }+ } } } @PreviewTest- @Preview(name = "3-home", device = PHONE, group = "phone", showSystemUi = true)+ @Preview(name = "4-capture-page", device = PHONE, group = "phone", showSystemUi = true) @Composable- fun Preview3Home() {+ fun Preview4CapturePage() { DynamicTheme(darkTheme = true) {- InitialScreen(onOpenPdf = {}, onCapturePdf = {}, onCutGlue = {})+ AppScaffold(title = "Capture PDF", onNavigateBack = {}) { padding ->+ // A captured page is a photo; the literal decoded page stands+ // in for it, as in the viewer capture above.+ LazyColumn(+ Modifier.fillMaxSize().padding(padding)+ .background(MaterialTheme.colorScheme.surface),+ ) {+ items(listOf(Page1)) { SafePdfPageCanvas(it) }+ }+ }+ }+ }++ @PreviewTest+ @Preview(name = "5-crop", device = PHONE, group = "phone", showSystemUi = true)+ @Composable+ fun Preview5Crop() {+ DynamicTheme(darkTheme = true) {+ AppScaffold(title = "Crop image", onNavigateBack = {}) { padding ->+ Box(+ Modifier.fillMaxSize().padding(padding).padding(24.dp)+ .background(Color.White),+ ) {+ CropOverlay(+ quadrilateral = Quadrilateral(+ topLeft = Offset(0.08f, 0.08f),+ topRight = Offset(0.92f, 0.08f),+ bottomRight = Offset(0.92f, 0.92f),+ bottomLeft = Offset(0.08f, 0.92f),+ ),+ onQuadrilateralChange = {},+ bitmap = null,+ )+ }+ }+ }+ }++ @PreviewTest+ @Preview(name = "6-cut-glue", device = PHONE, group = "phone", showSystemUi = true)+ @Composable+ fun Preview6CutGlue() {+ DynamicTheme(darkTheme = true) {+ val pages = listOf(Page1, Page2, Page3, Page4, Page5, Page6)+ CutGlueContent(+ pageKeys = pages.indices.map { it.toLong() },+ renderPage = { pages.getOrNull(it) },+ initialPages = pages.withIndex().associate { (i, page) -> i.toLong() to page },+ ) } } @PreviewTest- @Preview(name = "4-expanded", device = EXPANDED, group = "large-tablet", showSystemUi = true)+ @Preview(name = "7-viewer-expanded", device = EXPANDED, group = "large-tablet", showSystemUi = true) @Composable- fun Preview4Expanded() {+ fun Preview7ViewerExpanded() { DynamicTheme(darkTheme = true) { PdfViewerWideLayout( viewer = {