@tobyt/expo-pdf-markup 1.1.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookPager.kt +66 -0
  3. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookTurnPolicy.kt +117 -0
  4. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +370 -57
  5. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +96 -5
  7. package/build/ExpoPdfMarkup.types.d.ts +12 -0
  8. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  9. package/build/ExpoPdfMarkup.types.js.map +1 -1
  10. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  11. package/build/ExpoPdfMarkupView.web.js +369 -44
  12. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  13. package/build/web/bookMode.d.ts +77 -0
  14. package/build/web/bookMode.d.ts.map +1 -0
  15. package/build/web/bookMode.js +169 -0
  16. package/build/web/bookMode.js.map +1 -0
  17. package/build/web/layout.d.ts +8 -0
  18. package/build/web/layout.d.ts.map +1 -0
  19. package/build/web/layout.js +22 -0
  20. package/build/web/layout.js.map +1 -0
  21. package/build/web/types.d.ts +2 -0
  22. package/build/web/types.d.ts.map +1 -1
  23. package/build/web/types.js.map +1 -1
  24. package/docs/agents/domain.md +51 -0
  25. package/docs/agents/issue-tracker.md +46 -0
  26. package/docs/agents/triage-labels.md +15 -0
  27. package/ios/ExpoPdfMarkupModule.swift +4 -0
  28. package/ios/ExpoPdfMarkupView+Book.swift +455 -0
  29. package/ios/ExpoPdfMarkupView.swift +46 -2
  30. package/ios/Tests/BookModeTests.swift +534 -0
  31. package/ios/Tests/book-sample.pdf +207 -0
  32. package/ios/Tests/single-page.pdf +52 -0
  33. package/package.json +3 -2
  34. package/src/ExpoPdfMarkup.types.ts +13 -0
  35. package/src/ExpoPdfMarkupView.web.tsx +443 -52
  36. package/src/web/bookMode.ts +186 -0
  37. package/src/web/layout.ts +30 -0
  38. package/src/web/types.ts +3 -0
  39. package/tsconfig.test.json +10 -0
@@ -1,5 +1,8 @@
1
1
  package dev.terhoeven.expopdfmarkup
2
2
 
3
+ import android.animation.Animator
4
+ import android.animation.AnimatorListenerAdapter
5
+ import android.animation.ValueAnimator
3
6
  import android.content.Context
4
7
  import android.graphics.Bitmap
5
8
  import android.graphics.Canvas
@@ -12,19 +15,50 @@ import android.view.GestureDetector
12
15
  import android.view.MotionEvent
13
16
  import android.view.ScaleGestureDetector
14
17
  import android.view.View
18
+ import android.view.animation.DecelerateInterpolator
15
19
  import android.widget.OverScroller
16
20
  import java.util.UUID
17
21
 
22
+ /**
23
+ * Draws the PDF and handles touches in both display modes:
24
+ * - continuous: every page stacked in one vertical scroll (the default).
25
+ * - book ([isBookMode]): one page at a time, fitted to the view, turned
26
+ * sideways. All pages share one content slot; only [BookPager.currentPage]
27
+ * (plus its neighbour while sliding) is drawn. Swipe-or-pan and edge-tap
28
+ * decisions come from [BookTurnPolicy]; every turn goes through [turnPage].
29
+ */
18
30
  class ContinuousPdfView(context: Context) : View(context) {
19
- data class ScrollState(val panX: Float, val panY: Float, val scale: Float)
31
+ data class ScrollState(val panX: Float, val panY: Float, val scale: Float, val page: Int)
20
32
 
21
33
  var pendingScrollRestore: ScrollState? = null
22
34
 
23
35
  fun captureScrollState(): ScrollState? {
24
36
  if (pageBitmaps.isEmpty()) return null
25
- return ScrollState(panX, panY, currentScale)
37
+ return ScrollState(panX, panY, currentScale, currentPage)
26
38
  }
39
+
40
+ /** Book mode lays pages out one at a time. Set before [loadPages]. */
41
+ var isBookMode = false
42
+
43
+ /** The page in view: the one shown in book mode, the one at mid-height in continuous. */
44
+ val currentPage: Int get() = getVisiblePage()
45
+
46
+ private val bookPager = BookPager { notifyPageChange() }
47
+ private val density = resources.displayMetrics.density
48
+
49
+ /** Screen-space horizontal offset of the shown page while dragging or sliding. */
50
+ private var slideOffset = 0f
51
+
52
+ /** The page drawn beside the shown page while dragging or sliding, or -1. */
53
+ private var slideNeighbour = -1
54
+ private var slideAnimator: ValueAnimator? = null
55
+ private var bookSwipeAction = BookTurnPolicy.SwipeAction.PAN
56
+ private var bookSwipeDownX = 0f
57
+ private var isBookSwiping = false
58
+ private var bookSwipeVelocityX = 0f
59
+
27
60
  private val pageBitmaps = mutableListOf<Bitmap>()
61
+ private val pageXOffsets = mutableListOf<Float>()
28
62
  private val pageYOffsets = mutableListOf<Float>()
29
63
  private val pageWidths = mutableListOf<Int>()
30
64
  private val pageHeights = mutableListOf<Int>()
@@ -102,22 +136,33 @@ class ContinuousPdfView(context: Context) : View(context) {
102
136
  (page: Int, point: AnnotationPoint, annotation: AnnotationModel?) -> Unit
103
137
  )? = null
104
138
 
105
- fun loadPages(renderer: PdfRenderer, viewWidth: Int) {
139
+ /**
140
+ * Renders every page for the current display mode. With [startPage] (a
141
+ * mode switch, rotation or resize) the view stays on that page and does
142
+ * not report it again; without it the view starts at the top.
143
+ */
144
+ fun loadPages(renderer: PdfRenderer, viewWidth: Int, viewHeight: Int, startPage: Int? = null) {
145
+ val keptNotifiedPage = lastNotifiedPage
106
146
  recycle()
107
- if (viewWidth <= 0) return
147
+ if (viewWidth <= 0 || (isBookMode && viewHeight <= 0)) return
108
148
  setBackgroundColor(pageBackgroundColor)
109
149
 
110
150
  var yOffset = 0f
111
151
  for (i in 0 until renderer.pageCount) {
112
- if (i > 0) yOffset += pageGap
152
+ if (!isBookMode && i > 0) yOffset += pageGap
113
153
 
114
154
  val page = renderer.openPage(i)
115
155
  val pdfW = page.width
116
156
  val pdfH = page.height
117
- val scale = viewWidth.toFloat() / pdfW.toFloat()
118
- val bmpH = (pdfH * scale).toInt()
157
+ val scale = if (isBookMode) {
158
+ minOf(viewWidth.toFloat() / pdfW, viewHeight.toFloat() / pdfH)
159
+ } else {
160
+ viewWidth.toFloat() / pdfW.toFloat()
161
+ }
162
+ val bmpW = if (isBookMode) maxOf(1, (pdfW * scale).toInt()) else viewWidth
163
+ val bmpH = if (isBookMode) maxOf(1, (pdfH * scale).toInt()) else (pdfH * scale).toInt()
119
164
 
120
- val bmp = Bitmap.createBitmap(viewWidth, bmpH, Bitmap.Config.ARGB_8888)
165
+ val bmp = Bitmap.createBitmap(bmpW, bmpH, Bitmap.Config.ARGB_8888)
121
166
  Canvas(bmp).drawColor(Color.WHITE)
122
167
 
123
168
  val m = Matrix()
@@ -125,28 +170,45 @@ class ContinuousPdfView(context: Context) : View(context) {
125
170
  page.render(bmp, null, m, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
126
171
  page.close()
127
172
 
128
- pageYOffsets.add(yOffset)
173
+ if (isBookMode) {
174
+ // Every page sits centred in the same view-sized slot.
175
+ pageXOffsets.add((viewWidth - bmpW) / 2f)
176
+ pageYOffsets.add((viewHeight - bmpH) / 2f)
177
+ } else {
178
+ pageXOffsets.add(0f)
179
+ pageYOffsets.add(yOffset)
180
+ yOffset += bmpH
181
+ }
129
182
  pageBitmaps.add(bmp)
130
183
  pageWidths.add(pdfW)
131
184
  pageHeights.add(pdfH)
132
185
  renderScales.add(scale)
133
- yOffset += bmpH
134
186
  }
135
187
 
136
- totalHeight = yOffset
188
+ totalHeight = if (isBookMode) viewHeight.toFloat() else yOffset
137
189
  contentWidth = viewWidth.toFloat()
138
190
  currentScale = 1f
139
191
  minScale = 1f
140
192
  maxScale = 5f
141
193
  panX = 0f
142
194
  panY = 0f
143
- lastNotifiedPage = -1
195
+ lastNotifiedPage = if (startPage != null) keptNotifiedPage else -1
196
+ bookPager.reset(renderer.pageCount, startPage ?: 0)
144
197
 
145
- pendingScrollRestore?.let { state ->
146
- currentScale = state.scale.coerceIn(minScale, maxScale)
147
- panX = state.panX
148
- panY = state.panY
198
+ if (startPage != null && !isBookMode && startPage in pageYOffsets.indices) {
199
+ panY = pageYOffsets[startPage]
149
200
  constrainPan()
201
+ }
202
+
203
+ pendingScrollRestore?.let { state ->
204
+ if (isBookMode) {
205
+ bookPager.reset(renderer.pageCount, state.page)
206
+ } else {
207
+ currentScale = state.scale.coerceIn(minScale, maxScale)
208
+ panX = state.panX
209
+ panY = state.panY
210
+ constrainPan()
211
+ }
150
212
  pendingScrollRestore = null
151
213
  }
152
214
 
@@ -173,43 +235,27 @@ class ContinuousPdfView(context: Context) : View(context) {
173
235
  annotations
174
236
  }
175
237
 
176
- canvas.save()
177
- canvas.translate(-panX, -panY)
178
- canvas.scale(currentScale, currentScale)
179
-
180
- for (i in pageBitmaps.indices) {
181
- val pageTop = pageYOffsets[i] * currentScale
182
- val pageBottom = pageTop + pageBitmaps[i].height * currentScale
183
- val visibleTop = panY
184
- val visibleBottom = panY + height
185
-
186
- // Only draw pages that are visible
187
- if (pageBottom >= visibleTop && pageTop <= visibleBottom) {
188
- canvas.drawBitmap(pageBitmaps[i], 0f, pageYOffsets[i], null)
189
- AnnotationRenderer.drawAnnotations(
190
- canvas,
191
- renderedAnnotations,
192
- i,
193
- pageYOffsets[i],
194
- renderScales[i],
195
- pageHeights[i],
196
- context
197
- )
198
- if (annotationMode == "move" || annotationMode == "eraser") {
199
- AnnotationRenderer.drawEditingAffordances(
200
- canvas,
201
- renderedAnnotations,
202
- i,
203
- pageYOffsets[i],
204
- renderScales[i],
205
- pageHeights[i],
206
- currentScale
207
- )
238
+ if (isBookMode) {
239
+ drawBookPages(canvas, renderedAnnotations)
240
+ } else {
241
+ canvas.save()
242
+ canvas.translate(-panX, -panY)
243
+ canvas.scale(currentScale, currentScale)
244
+
245
+ for (i in pageBitmaps.indices) {
246
+ val pageTop = pageYOffsets[i] * currentScale
247
+ val pageBottom = pageTop + pageBitmaps[i].height * currentScale
248
+ val visibleTop = panY
249
+ val visibleBottom = panY + height
250
+
251
+ // Only draw pages that are visible
252
+ if (pageBottom >= visibleTop && pageTop <= visibleBottom) {
253
+ drawPage(canvas, i, renderedAnnotations)
208
254
  }
209
255
  }
210
- }
211
256
 
212
- canvas.restore()
257
+ canvas.restore()
258
+ }
213
259
 
214
260
  // Draw live ink preview (in screen coordinates)
215
261
  if (isDrawingInk && currentInkPoints.size > 1) {
@@ -247,6 +293,55 @@ class ContinuousPdfView(context: Context) : View(context) {
247
293
  }
248
294
  }
249
295
 
296
+ /** Draws page [i] and its markup in content space, at its layout offset. */
297
+ private fun drawPage(canvas: Canvas, i: Int, renderedAnnotations: List<AnnotationModel>) {
298
+ canvas.save()
299
+ canvas.translate(pageXOffsets[i], 0f)
300
+ canvas.drawBitmap(pageBitmaps[i], 0f, pageYOffsets[i], null)
301
+ AnnotationRenderer.drawAnnotations(
302
+ canvas,
303
+ renderedAnnotations,
304
+ i,
305
+ pageYOffsets[i],
306
+ renderScales[i],
307
+ pageHeights[i],
308
+ context
309
+ )
310
+ if (annotationMode == "move" || annotationMode == "eraser") {
311
+ AnnotationRenderer.drawEditingAffordances(
312
+ canvas,
313
+ renderedAnnotations,
314
+ i,
315
+ pageYOffsets[i],
316
+ renderScales[i],
317
+ pageHeights[i],
318
+ currentScale
319
+ )
320
+ }
321
+ canvas.restore()
322
+ }
323
+
324
+ /** Book mode: the shown page, plus its neighbour beside it while sliding. */
325
+ private fun drawBookPages(canvas: Canvas, renderedAnnotations: List<AnnotationModel>) {
326
+ val shown = bookPager.currentPage
327
+ if (shown !in pageBitmaps.indices) return
328
+
329
+ canvas.save()
330
+ canvas.translate(slideOffset - panX, -panY)
331
+ canvas.scale(currentScale, currentScale)
332
+ drawPage(canvas, shown, renderedAnnotations)
333
+ canvas.restore()
334
+
335
+ if (slideNeighbour in pageBitmaps.indices && slideNeighbour != shown) {
336
+ // The next page waits off the right edge, the previous off the left.
337
+ val side = if (slideNeighbour > shown) 1f else -1f
338
+ canvas.save()
339
+ canvas.translate(slideOffset + side * width, 0f)
340
+ drawPage(canvas, slideNeighbour, renderedAnnotations)
341
+ canvas.restore()
342
+ }
343
+ }
344
+
250
345
  override fun onTouchEvent(event: MotionEvent): Boolean {
251
346
  // Hold off parent interception (e.g. gesture-handler root, scroll
252
347
  // containers) for the duration of each gesture only; leaving it set
@@ -255,11 +350,20 @@ class ContinuousPdfView(context: Context) : View(context) {
255
350
  parent?.requestDisallowInterceptTouchEvent(it)
256
351
  }
257
352
 
353
+ // A new touch in book mode lands any slide still running, so markup
354
+ // and taps always act on the page that is fully in view.
355
+ if (isBookMode && event.actionMasked == MotionEvent.ACTION_DOWN) {
356
+ slideAnimator?.end()
357
+ }
358
+
258
359
  // Always let scale detector handle pinch zoom
259
360
  scaleDetector.onTouchEvent(event)
260
361
 
261
362
  // If scaling with two fingers, don't process as annotation gesture
262
- if (scaleDetector.isInProgress) return true
363
+ if (scaleDetector.isInProgress) {
364
+ if (isBookSwiping) settleBookSwipe()
365
+ return true
366
+ }
263
367
 
264
368
  when (annotationMode) {
265
369
  "ink" -> {
@@ -308,8 +412,13 @@ class ContinuousPdfView(context: Context) : View(context) {
308
412
  }
309
413
 
310
414
  else -> {
311
- // "none" mode — normal scroll/zoom
415
+ // "none" mode — normal scroll/zoom (book mode: swipe turns at fit)
312
416
  gestureDetector.onTouchEvent(event)
417
+ if (isBookMode) {
418
+ val action = event.actionMasked
419
+ if (action == MotionEvent.ACTION_UP) releaseBookSwipe()
420
+ if (action == MotionEvent.ACTION_CANCEL) settleBookSwipe()
421
+ }
313
422
  }
314
423
  }
315
424
 
@@ -333,6 +442,10 @@ class ContinuousPdfView(context: Context) : View(context) {
333
442
  dragCurrentPoint = null
334
443
  clearMovePreview()
335
444
  scroller.forceFinished(true)
445
+ slideAnimator?.end()
446
+ isBookSwiping = false
447
+ slideOffset = 0f
448
+ slideNeighbour = -1
336
449
  parent?.requestDisallowInterceptTouchEvent(false)
337
450
  invalidate()
338
451
  }
@@ -347,6 +460,129 @@ class ContinuousPdfView(context: Context) : View(context) {
347
460
  super.onDetachedFromWindow()
348
461
  }
349
462
 
463
+ // --- Book mode turning ---
464
+
465
+ /**
466
+ * Turns one page in book mode: slides to the neighbouring page, or
467
+ * bounces at either end. The new page starts at fit, and onPageChanged
468
+ * fires once when the slide lands. Swipes and edge taps call this after
469
+ * [BookTurnPolicy] allows them; hardware keys can call it directly.
470
+ * Returns false when nothing could turn (not book mode, nothing loaded).
471
+ */
472
+ fun turnPage(direction: BookTurnPolicy.Direction): Boolean {
473
+ if (!isBookMode || pageBitmaps.isEmpty()) return false
474
+ // Land a slide still running first, so rapid turns each count.
475
+ val wasSliding = slideAnimator != null
476
+ slideAnimator?.end()
477
+ if (wasSliding) slideOffset = 0f
478
+ isBookSwiping = false
479
+ resetToFit()
480
+
481
+ when (val turn = bookPager.turn(direction)) {
482
+ is BookPager.Turn.Slide -> {
483
+ slideNeighbour = turn.to
484
+ // Next slides the page off to the left, previous to the right.
485
+ animateSlideOffset(slideOffset, -direction.delta * width.toFloat()) {
486
+ slideOffset = 0f
487
+ slideNeighbour = -1
488
+ bookPager.finishSlide()
489
+ }
490
+ }
491
+
492
+ is BookPager.Turn.Bounce -> {
493
+ slideNeighbour = -1
494
+ if (slideOffset != 0f) {
495
+ // Released a rubber-banded drag at an end: spring back.
496
+ animateSlideOffset(slideOffset, 0f) {}
497
+ } else {
498
+ val nudge = -direction.delta * width * BOUNCE_FRACTION
499
+ animateSlideOffset(0f, nudge, BOUNCE_DURATION_MS) {
500
+ animateSlideOffset(nudge, 0f, BOUNCE_DURATION_MS) {}
501
+ }
502
+ }
503
+ }
504
+ }
505
+ return true
506
+ }
507
+
508
+ private fun bookEdgeTapTurn(e: MotionEvent): BookTurnPolicy.Direction? =
509
+ BookTurnPolicy.edgeTapTurnFor(
510
+ annotationMode,
511
+ currentScale,
512
+ e.x,
513
+ width.toFloat(),
514
+ density
515
+ )
516
+
517
+ private fun resetToFit() {
518
+ scroller.forceFinished(true)
519
+ currentScale = minScale
520
+ panX = 0f
521
+ panY = 0f
522
+ }
523
+
524
+ private fun animateSlideOffset(
525
+ from: Float,
526
+ to: Float,
527
+ durationMs: Long = SLIDE_DURATION_MS,
528
+ onEnd: () -> Unit
529
+ ) {
530
+ slideAnimator?.removeAllListeners()
531
+ slideAnimator?.cancel()
532
+ slideOffset = from
533
+ val animator = ValueAnimator.ofFloat(from, to).apply {
534
+ duration = durationMs
535
+ interpolator = DecelerateInterpolator()
536
+ addUpdateListener {
537
+ slideOffset = it.animatedValue as Float
538
+ invalidate()
539
+ }
540
+ }
541
+ animator.addListener(object : AnimatorListenerAdapter() {
542
+ override fun onAnimationEnd(a: Animator) {
543
+ if (slideAnimator === animator) slideAnimator = null
544
+ slideOffset = to
545
+ onEnd()
546
+ invalidate()
547
+ }
548
+ })
549
+ slideAnimator = animator
550
+ animator.start()
551
+ }
552
+
553
+ /** Book mode swipe that is following the finger, at fit. */
554
+ private fun dragBookSwipe(totalDx: Float) {
555
+ isBookSwiping = true
556
+ val neighbour = bookPager.currentPage + if (totalDx < 0f) 1 else -1
557
+ if (neighbour in pageBitmaps.indices) {
558
+ slideNeighbour = neighbour
559
+ slideOffset = totalDx
560
+ } else {
561
+ // Nothing that way: rubber-band so the bounce is felt.
562
+ slideNeighbour = -1
563
+ slideOffset = totalDx * RUBBER_BAND
564
+ }
565
+ invalidate()
566
+ }
567
+
568
+ private fun releaseBookSwipe() {
569
+ if (!isBookSwiping) return
570
+ isBookSwiping = false
571
+ val totalDx = if (slideNeighbour >= 0) slideOffset else slideOffset / RUBBER_BAND
572
+ val direction = BookTurnPolicy.swipeReleaseTurnFor(
573
+ dragX = totalDx,
574
+ velocityX = bookSwipeVelocityX,
575
+ viewWidth = width.toFloat(),
576
+ flingVelocity = FLING_VELOCITY_DP * density
577
+ )
578
+ if (direction != null) turnPage(direction) else settleBookSwipe()
579
+ }
580
+
581
+ private fun settleBookSwipe() {
582
+ isBookSwiping = false
583
+ animateSlideOffset(slideOffset, 0f) { slideNeighbour = -1 }
584
+ }
585
+
350
586
  // --- Ink mode ---
351
587
 
352
588
  private fun handleInkTouch(event: MotionEvent) {
@@ -393,7 +629,7 @@ class ContinuousPdfView(context: Context) : View(context) {
393
629
  if (pi != pageIndex || pi < 0) continue
394
630
 
395
631
  val localY = contentY - pageYOffsets[pi]
396
- val pdfX = contentX / renderScales[pi]
632
+ val pdfX = (contentX - pageXOffsets[pi]) / renderScales[pi]
397
633
  val pdfY = pageHeights[pi] - (localY / renderScales[pi])
398
634
  pdfPoints.add(AnnotationPoint(pdfX, pdfY))
399
635
  }
@@ -461,9 +697,10 @@ class ContinuousPdfView(context: Context) : View(context) {
461
697
  val localEndY = endCY - pageYOffsets[pageIndex]
462
698
 
463
699
  // Convert to PDF coordinates
464
- val pdfX1 = startCX / rs
700
+ val pageX = pageXOffsets[pageIndex]
701
+ val pdfX1 = (startCX - pageX) / rs
465
702
  val pdfY1 = ph - (localStartY / rs)
466
- val pdfX2 = endCX / rs
703
+ val pdfX2 = (endCX - pageX) / rs
467
704
  val pdfY2 = ph - (localEndY / rs)
468
705
 
469
706
  val left = minOf(pdfX1, pdfX2)
@@ -645,6 +882,8 @@ class ContinuousPdfView(context: Context) : View(context) {
645
882
  // --- Helpers ---
646
883
 
647
884
  private fun pageIndexAtContentY(contentY: Float): Int {
885
+ // Book mode: every page shares one slot, so a touch is on the page shown.
886
+ if (isBookMode) return bookPager.currentPage
648
887
  for (i in pageYOffsets.indices.reversed()) {
649
888
  if (pageYOffsets[i] <= contentY) return i
650
889
  }
@@ -670,7 +909,7 @@ class ContinuousPdfView(context: Context) : View(context) {
670
909
  if (pageIndex < 0 || pageIndex >= pageYOffsets.size) return null
671
910
 
672
911
  val localY = contentY - pageYOffsets[pageIndex]
673
- val pdfX = contentX / renderScales[pageIndex]
912
+ val pdfX = (contentX - pageXOffsets[pageIndex]) / renderScales[pageIndex]
674
913
  val pdfY = pageHeights[pageIndex] - (localY / renderScales[pageIndex])
675
914
  return PdfTouchPoint(pageIndex, AnnotationPoint(pdfX, pdfY))
676
915
  }
@@ -693,6 +932,7 @@ class ContinuousPdfView(context: Context) : View(context) {
693
932
  }
694
933
 
695
934
  private fun getVisiblePage(): Int {
935
+ if (isBookMode) return bookPager.currentPage
696
936
  val centerY = (panY + height / 2f) / currentScale
697
937
  for (i in pageYOffsets.indices.reversed()) {
698
938
  if (pageYOffsets[i] <= centerY) return i
@@ -702,6 +942,19 @@ class ContinuousPdfView(context: Context) : View(context) {
702
942
 
703
943
  fun scrollToPage(pageIndex: Int) {
704
944
  if (pageIndex < 0 || pageIndex >= pageYOffsets.size) return
945
+ if (isBookMode) {
946
+ // The page prop jumps straight there, at fit, with no slide.
947
+ slideAnimator?.removeAllListeners()
948
+ slideAnimator?.cancel()
949
+ slideAnimator = null
950
+ isBookSwiping = false
951
+ slideOffset = 0f
952
+ slideNeighbour = -1
953
+ resetToFit()
954
+ bookPager.jumpTo(pageIndex) // reports through notifyPageChange
955
+ invalidate()
956
+ return
957
+ }
705
958
  panY = pageYOffsets[pageIndex] * currentScale
706
959
  constrainPan()
707
960
  invalidate()
@@ -721,7 +974,14 @@ class ContinuousPdfView(context: Context) : View(context) {
721
974
  fun recycle() {
722
975
  pageBitmaps.forEach { it.recycle() }
723
976
  pageBitmaps.clear()
977
+ pageXOffsets.clear()
724
978
  pageYOffsets.clear()
979
+ slideAnimator?.removeAllListeners()
980
+ slideAnimator?.cancel()
981
+ slideAnimator = null
982
+ isBookSwiping = false
983
+ slideOffset = 0f
984
+ slideNeighbour = -1
725
985
  pageWidths.clear()
726
986
  pageHeights.clear()
727
987
  renderScales.clear()
@@ -736,6 +996,20 @@ class ContinuousPdfView(context: Context) : View(context) {
736
996
  lastNotifiedPage = -1
737
997
  }
738
998
 
999
+ private companion object {
1000
+ const val SLIDE_DURATION_MS = 250L
1001
+ const val BOUNCE_DURATION_MS = 120L
1002
+
1003
+ /** How far an edge tap or key nudges the page at either end. */
1004
+ const val BOUNCE_FRACTION = 0.06f
1005
+
1006
+ /** How much of the finger's travel the page follows at either end. */
1007
+ const val RUBBER_BAND = 0.3f
1008
+
1009
+ /** A release faster than this turns the page even after a short drag. */
1010
+ const val FLING_VELOCITY_DP = 500f
1011
+ }
1012
+
739
1013
  private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
740
1014
  override fun onScale(detector: ScaleGestureDetector): Boolean {
741
1015
  val oldScale = currentScale
@@ -752,9 +1026,30 @@ class ContinuousPdfView(context: Context) : View(context) {
752
1026
  }
753
1027
 
754
1028
  private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
755
- override fun onDown(e: MotionEvent): Boolean = true
1029
+ override fun onDown(e: MotionEvent): Boolean {
1030
+ // Turn or pan is decided once per gesture, at touch down.
1031
+ bookSwipeAction = if (isBookMode) {
1032
+ BookTurnPolicy.swipeActionFor(annotationMode, currentScale)
1033
+ } else {
1034
+ BookTurnPolicy.SwipeAction.PAN
1035
+ }
1036
+ bookSwipeDownX = e.x
1037
+ bookSwipeVelocityX = 0f
1038
+ return true
1039
+ }
1040
+
1041
+ override fun onSingleTapUp(e: MotionEvent): Boolean {
1042
+ if (!isBookMode) return false
1043
+ val direction = bookEdgeTapTurn(e) ?: return false
1044
+ turnPage(direction)
1045
+ return true
1046
+ }
756
1047
 
757
1048
  override fun onScroll(e1: MotionEvent?, e2: MotionEvent, dx: Float, dy: Float): Boolean {
1049
+ if (isBookMode && bookSwipeAction == BookTurnPolicy.SwipeAction.TURN) {
1050
+ dragBookSwipe(e2.x - (e1?.x ?: bookSwipeDownX))
1051
+ return true
1052
+ }
758
1053
  panX += dx
759
1054
  panY += dy
760
1055
  constrainPan()
@@ -764,6 +1059,11 @@ class ContinuousPdfView(context: Context) : View(context) {
764
1059
  }
765
1060
 
766
1061
  override fun onFling(e1: MotionEvent?, e2: MotionEvent, vx: Float, vy: Float): Boolean {
1062
+ if (isBookMode && bookSwipeAction == BookTurnPolicy.SwipeAction.TURN) {
1063
+ // Picked up when the finger lifts, in releaseBookSwipe.
1064
+ bookSwipeVelocityX = vx
1065
+ return true
1066
+ }
767
1067
  scroller.fling(
768
1068
  panX.toInt(),
769
1069
  panY.toInt(),
@@ -779,6 +1079,19 @@ class ContinuousPdfView(context: Context) : View(context) {
779
1079
  }
780
1080
 
781
1081
  override fun onDoubleTap(e: MotionEvent): Boolean {
1082
+ // In book mode the first tap on an edge has already turned the page, so
1083
+ // the second is swallowed: a double-tap zooms only in the middle region.
1084
+ if (isBookMode &&
1085
+ !BookTurnPolicy.doubleTapZooms(
1086
+ annotationMode,
1087
+ currentScale,
1088
+ e.x,
1089
+ width.toFloat(),
1090
+ density
1091
+ )
1092
+ ) {
1093
+ return true
1094
+ }
782
1095
  val targetScale = if (currentScale > minScale * 1.5f) minScale else minScale * 2.5f
783
1096
  val oldScale = currentScale
784
1097
  currentScale = targetScale
@@ -68,6 +68,10 @@ class ExpoPdfMarkupModule : Module() {
68
68
  view.loadAnnotations(json)
69
69
  }
70
70
 
71
+ Prop("displayMode") { view: ExpoPdfMarkupView, mode: String? ->
72
+ view.setDisplayMode(mode)
73
+ }
74
+
71
75
  Prop("annotationMode") { view: ExpoPdfMarkupView, mode: String? ->
72
76
  view.setAnnotationMode(mode)
73
77
  }