@papyrus-sdk/engine-native 0.2.20 → 0.2.21

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.
@@ -426,7 +426,7 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
426
426
  }
427
427
 
428
428
  @ReactMethod
429
- public void selectText(String engineId, int pageIndex, double x, double y, double width, double height, Promise promise) {
429
+ public void selectText(String engineId, int pageIndex, double x, double y, double width, double height, double startX, double startY, double endX, double endY, Promise promise) {
430
430
  executor.execute(() -> {
431
431
  PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
432
432
  if (state == null || state.document == null || pageIndex < 0) {
@@ -443,7 +443,7 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
443
443
  try {
444
444
  if (state.sourcePath != null && !state.sourcePath.isEmpty()) {
445
445
  synchronized (state.pdfiumLock) {
446
- selection = PapyrusTextSelect.nativeSelectTextFile(state.sourcePath, pageIndex, (float) x, (float) y, (float) width, (float) height);
446
+ selection = PapyrusTextSelect.nativeSelectTextFile(state.sourcePath, pageIndex, (float) x, (float) y, (float) width, (float) height, (float) startX, (float) startY, (float) endX, (float) endY);
447
447
  }
448
448
  } else {
449
449
  long docPtr;
@@ -452,7 +452,7 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
452
452
  }
453
453
  if (docPtr != 0) {
454
454
  synchronized (state.pdfiumLock) {
455
- selection = PapyrusTextSelect.nativeSelectText(docPtr, pageIndex, (float) x, (float) y, (float) width, (float) height);
455
+ selection = PapyrusTextSelect.nativeSelectText(docPtr, pageIndex, (float) x, (float) y, (float) width, (float) height, (float) startX, (float) startY, (float) endX, (float) endY);
456
456
  }
457
457
  }
458
458
  }
@@ -333,7 +333,7 @@ class PapyrusNativeEngineModule : Module() {
333
333
  }
334
334
  }
335
335
 
336
- AsyncFunction("selectText") { engineId: String, pageIndex: Int, x: Double, y: Double, width: Double, height: Double, promise: Promise ->
336
+ AsyncFunction("selectText") { engineId: String, pageIndex: Int, x: Double, y: Double, width: Double, height: Double, startX: Double, startY: Double, endX: Double, endY: Double, promise: Promise ->
337
337
  executor.execute {
338
338
  val state = PapyrusEngineStore.getEngine(engineId)
339
339
  if (state == null || state.document == null || pageIndex < 0) {
@@ -356,7 +356,11 @@ class PapyrusNativeEngineModule : Module() {
356
356
  x.toFloat(),
357
357
  y.toFloat(),
358
358
  width.toFloat(),
359
- height.toFloat()
359
+ height.toFloat(),
360
+ startX.toFloat(),
361
+ startY.toFloat(),
362
+ endX.toFloat(),
363
+ endY.toFloat()
360
364
  )
361
365
  }
362
366
  } else {
@@ -369,7 +373,11 @@ class PapyrusNativeEngineModule : Module() {
369
373
  x.toFloat(),
370
374
  y.toFloat(),
371
375
  width.toFloat(),
372
- height.toFloat()
376
+ height.toFloat(),
377
+ startX.toFloat(),
378
+ startY.toFloat(),
379
+ endX.toFloat(),
380
+ endY.toFloat()
373
381
  )
374
382
  }
375
383
  }
@@ -19,6 +19,7 @@ import android.view.MotionEvent;
19
19
  import android.view.ScaleGestureDetector;
20
20
  import android.view.VelocityTracker;
21
21
  import android.view.View;
22
+ import android.view.ViewConfiguration;
22
23
  import android.view.ViewParent;
23
24
  import android.widget.OverScroller;
24
25
 
@@ -135,15 +136,36 @@ public class PapyrusPdfViewerView extends View {
135
136
  private long lastTapTime = 0;
136
137
  private float lastTapX = 0;
137
138
  private float lastTapY = 0;
138
- private static final long DOUBLE_TAP_MAX_DELTA_MS = 250;
139
139
  private static final float DOUBLE_TAP_MAX_DISTANCE_DP = 20;
140
+ private final long doubleTapTimeoutMs;
140
141
  private Runnable pendingSingleTap;
142
+ private boolean suppressPageTapForGesture = false;
143
+ private boolean insideSelectionAtDown = false;
141
144
 
142
145
  // Selection handles
143
146
  private static final float HANDLE_RADIUS_DP = 10;
147
+ private static final float HANDLE_TOUCH_RADIUS_DP = 24;
144
148
  private static final int HANDLE_COLOR = Color.parseColor("#4285F4");
145
149
  private int draggedHandle = 0; // 0 = none, -1 = start handle, 1 = end handle
146
150
 
151
+ // Coalesced text selection requests ("latest request wins")
152
+ private final Object selectionLock = new Object();
153
+ private boolean selectionRequestInFlight = false;
154
+ private boolean selectionRequestPending = false;
155
+ private boolean selectionRequestEmit = false;
156
+ private long selectionRequestVersion = 0;
157
+ private long selectionRequestQueuedVersion = 0;
158
+ private String selectionRequestEngineId = null;
159
+ private int selectionRequestPage = -1;
160
+ private float selectionRequestRectX = 0f;
161
+ private float selectionRequestRectY = 0f;
162
+ private float selectionRequestRectW = 0f;
163
+ private float selectionRequestRectH = 0f;
164
+ private float selectionRequestStartX = 0f;
165
+ private float selectionRequestStartY = 0f;
166
+ private float selectionRequestEndX = 0f;
167
+ private float selectionRequestEndY = 0f;
168
+
147
169
  private final ReactContext reactContext;
148
170
  private final Handler eventThrottleHandler = new Handler(Looper.getMainLooper());
149
171
  private Runnable pendingZoomEvent;
@@ -190,6 +212,7 @@ public class PapyrusPdfViewerView extends View {
190
212
  super(context);
191
213
  this.reactContext = context;
192
214
  scaleDetector = createScaleDetector(context);
215
+ doubleTapTimeoutMs = ViewConfiguration.get(context).getDoubleTapTimeout();
193
216
  flingScroller = new OverScroller(context);
194
217
  }
195
218
 
@@ -197,6 +220,7 @@ public class PapyrusPdfViewerView extends View {
197
220
  super(context, attrs);
198
221
  this.reactContext = context;
199
222
  scaleDetector = createScaleDetector(context);
223
+ doubleTapTimeoutMs = ViewConfiguration.get(context).getDoubleTapTimeout();
200
224
  flingScroller = new OverScroller(context);
201
225
  }
202
226
 
@@ -527,7 +551,7 @@ public class PapyrusPdfViewerView extends View {
527
551
  private boolean handleSelectTouch(MotionEvent event) {
528
552
  float density = getResources().getDisplayMetrics().density;
529
553
  switch (event.getActionMasked()) {
530
- case MotionEvent.ACTION_DOWN:
554
+ case MotionEvent.ACTION_DOWN: {
531
555
  if (event.getPointerCount() > 1) {
532
556
  return true; // Multi-touch: ignore, let pinch zoom handle it
533
557
  }
@@ -535,9 +559,14 @@ public class PapyrusPdfViewerView extends View {
535
559
  touchDownX = event.getX();
536
560
  touchDownY = event.getY();
537
561
  draggedHandle = 0;
562
+ insideSelectionAtDown = false;
538
563
 
539
- // Check if touching a selection handle
540
- if (isSelectingText && selectPageIndex >= 0 && !selectedRects.isEmpty()) {
564
+ boolean hasSelection =
565
+ isSelectingText && selectPageIndex >= 0 && !selectedRects.isEmpty();
566
+ suppressPageTapForGesture =
567
+ PapyrusTextSelectionGesture.shouldSuppressPageTap(hasSelection);
568
+
569
+ if (hasSelection) {
541
570
  int handleHit = hitTestHandle(event.getX(), event.getY(), density);
542
571
  if (handleHit != 0) {
543
572
  draggedHandle = handleHit;
@@ -547,14 +576,14 @@ public class PapyrusPdfViewerView extends View {
547
576
  }
548
577
  return true;
549
578
  }
550
- // Any new touch that is not on a selection handle starts normal
551
- // scrolling. Keeping the selection body "active" here made a
552
- // regular finger drag look like a stuck scroll after double-tap.
553
- if (PapyrusTextSelectionGesture.shouldContinueAfterSelectionTouch(handleHit != 0)) {
554
- isSelectingText = false;
555
- selectPageIndex = -1;
556
- selectedRects.clear();
557
- selectedText = "";
579
+ boolean insideSelection =
580
+ isPointInsideSelection(event.getX(), event.getY(), density);
581
+ if (PapyrusTextSelectionGesture.shouldDismissSelectionOnTouch(
582
+ true, false, insideSelection)) {
583
+ clearSelectionState();
584
+ emitSelectionCleared();
585
+ } else {
586
+ insideSelectionAtDown = insideSelection;
558
587
  }
559
588
  }
560
589
 
@@ -568,45 +597,50 @@ public class PapyrusPdfViewerView extends View {
568
597
  removeCallbacks(flingRunnable);
569
598
  lastTouchX = event.getX();
570
599
  lastTouchY = event.getY();
571
-
572
600
  return true;
601
+ }
602
+
603
+ case MotionEvent.ACTION_MOVE: {
604
+ if (velocityTracker != null) {
605
+ velocityTracker.addMovement(event);
606
+ }
607
+
608
+ float moveDx = event.getX() - touchDownX;
609
+ float moveDy = event.getY() - touchDownY;
610
+ float moveDistance = (float) Math.hypot(moveDx, moveDy);
573
611
 
574
- case MotionEvent.ACTION_MOVE:
575
612
  if (draggedHandle != 0 && isSelectingText && selectPageIndex >= 0) {
576
613
  ensureLayout();
577
- float moveDocX = event.getX() + offsetX;
578
- float moveDocY = event.getY() + offsetY;
579
- PageFrame moveFrame = null;
580
- for (PageFrame f : pageFrames) {
581
- if (f.index == selectPageIndex) {
582
- moveFrame = f;
583
- break;
584
- }
585
- }
614
+ PageFrame moveFrame = findSelectFrame();
586
615
  if (moveFrame != null) {
587
- float nx = clamp01((moveDocX - moveFrame.left) / moveFrame.width);
588
- float ny = clamp01((moveDocY - moveFrame.top) / moveFrame.height);
616
+ float nx = clamp01((event.getX() + offsetX - moveFrame.left) / moveFrame.width);
617
+ float ny = clamp01((event.getY() + offsetY - moveFrame.top) / moveFrame.height);
589
618
  if (draggedHandle < 0) {
590
- // Preserve the actual horizontal position instead of expanding
591
- // the selection to the whole line when crossing line breaks.
592
- selectStartX = PapyrusTextSelectionGesture.resolveSelectionEndpoint(nx);
619
+ selectStartX = nx;
593
620
  selectStartY = ny;
594
621
  } else {
595
- // Preserve the actual horizontal position for the end handle too.
596
- selectEndX = PapyrusTextSelectionGesture.resolveSelectionEndpoint(nx);
622
+ selectEndX = nx;
597
623
  selectEndY = ny;
598
624
  }
599
- performTextSelection();
625
+ performTextSelection(false);
600
626
  }
627
+ lastTouchX = event.getX();
628
+ lastTouchY = event.getY();
601
629
  return true;
602
630
  }
603
631
 
604
- if (velocityTracker != null) {
605
- velocityTracker.addMovement(event);
632
+ if (isSelectingText && selectPageIndex >= 0 && insideSelectionAtDown) {
633
+ if (!PapyrusTextSelectionGesture.shouldStartScrollFromSelection(
634
+ true, moveDistance, TAP_MAX_DISTANCE_DP * density)) {
635
+ lastTouchX = event.getX();
636
+ lastTouchY = event.getY();
637
+ return true;
638
+ }
639
+ clearSelectionState();
640
+ emitSelectionCleared();
641
+ insideSelectionAtDown = false;
606
642
  }
607
- float moveDx = event.getX() - touchDownX;
608
- float moveDy = event.getY() - touchDownY;
609
- float moveDistance = (float) Math.hypot(moveDx, moveDy);
643
+
610
644
  if (!isSelectingText && moveDistance > TAP_MAX_DISTANCE_DP * density) {
611
645
  if (!scaleDetector.isInProgress() && event.getPointerCount() == 1) {
612
646
  offsetX += lastTouchX - event.getX();
@@ -615,37 +649,27 @@ public class PapyrusPdfViewerView extends View {
615
649
  emitScrollEvent(offsetY);
616
650
  invalidate();
617
651
  }
618
- lastTouchX = event.getX();
619
- lastTouchY = event.getY();
620
- return true;
621
- }
622
- if (isSelectingText && selectPageIndex >= 0) {
623
- ensureLayout();
624
- float moveDocX = event.getX() + offsetX;
625
- float moveDocY = event.getY() + offsetY;
626
- PageFrame moveFrame = null;
627
- for (PageFrame f : pageFrames) {
628
- if (f.index == selectPageIndex) {
629
- moveFrame = f;
630
- break;
631
- }
632
- }
633
- if (moveFrame != null) {
634
- float nx = clamp01((moveDocX - moveFrame.left) / moveFrame.width);
635
- float ny = clamp01((moveDocY - moveFrame.top) / moveFrame.height);
636
- // Keep the endpoint under the finger while dragging across lines.
637
- selectEndX = PapyrusTextSelectionGesture.resolveSelectionEndpoint(nx);
638
- selectEndY = ny;
639
- performTextSelection();
640
- }
641
652
  }
642
653
  lastTouchX = event.getX();
643
654
  lastTouchY = event.getY();
644
655
  return true;
656
+ }
645
657
 
646
658
  case MotionEvent.ACTION_UP:
647
- case MotionEvent.ACTION_CANCEL:
659
+ case MotionEvent.ACTION_CANCEL: {
660
+ boolean wasHandleDrag = draggedHandle != 0;
648
661
  draggedHandle = 0;
662
+ insideSelectionAtDown = false;
663
+
664
+ if (event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
665
+ suppressPageTapForGesture = false;
666
+ return true;
667
+ }
668
+
669
+ if (wasHandleDrag && selectPageIndex >= 0) {
670
+ performTextSelection(true);
671
+ return true;
672
+ }
649
673
 
650
674
  if (!scaleDetector.isInProgress() && velocityTracker != null) {
651
675
  velocityTracker.addMovement(event);
@@ -691,17 +715,18 @@ public class PapyrusPdfViewerView extends View {
691
715
  float upDistance = (float) Math.hypot(upDx, upDy);
692
716
 
693
717
  if (duration > TAP_MAX_DURATION_MS || upDistance > TAP_MAX_DISTANCE_DP * density) {
694
- if (isSelectingText) {
695
- emitTextSelected();
696
- }
697
718
  return true;
698
719
  }
699
720
 
700
721
  long now = System.currentTimeMillis();
701
- float tapDx = event.getX() - lastTapX;
702
- float tapDy = event.getY() - lastTapY;
703
- float tapDistance = (float) Math.hypot(tapDx, tapDy);
704
- boolean isDoubleTap = (now - lastTapTime) < DOUBLE_TAP_MAX_DELTA_MS && tapDistance < DOUBLE_TAP_MAX_DISTANCE_DP * density;
722
+ boolean isDoubleTap = PapyrusTextSelectionGesture.resolveDoubleTap(
723
+ now,
724
+ lastTapTime,
725
+ event.getX() - lastTapX,
726
+ event.getY() - lastTapY,
727
+ doubleTapTimeoutMs,
728
+ DOUBLE_TAP_MAX_DISTANCE_DP * density
729
+ );
705
730
 
706
731
  if (pendingSingleTap != null) {
707
732
  mainHandler.removeCallbacks(pendingSingleTap);
@@ -714,15 +739,10 @@ public class PapyrusPdfViewerView extends View {
714
739
  lastTapY = event.getY();
715
740
  final float tapX = event.getX();
716
741
  final float tapY = event.getY();
742
+ final boolean allowPageTap = !suppressPageTapForGesture;
743
+ suppressPageTapForGesture = false;
717
744
  pendingSingleTap = () -> {
718
745
  pendingSingleTap = null;
719
- if (isSelectingText) {
720
- isSelectingText = false;
721
- selectPageIndex = -1;
722
- selectedRects.clear();
723
- selectedText = "";
724
- invalidate();
725
- }
726
746
  ensureLayout();
727
747
  float docX = tapX + offsetX;
728
748
  float docY = tapY + offsetY;
@@ -733,49 +753,88 @@ public class PapyrusPdfViewerView extends View {
733
753
  float ny = (docY - frame.top) / frame.height;
734
754
  Annotation hit = findAnnotationAt(pageIdx, nx, ny);
735
755
  if (hit != null) {
756
+ // O tap de anotação continua funcionando mesmo ao desselecionar.
736
757
  emitAnnotationTap(hit);
737
- } else {
758
+ } else if (allowPageTap) {
738
759
  emitTap(pageIdx, clamp01(nx), clamp01(ny));
739
760
  }
740
761
  };
741
- mainHandler.postDelayed(pendingSingleTap, DOUBLE_TAP_MAX_DELTA_MS + 50);
762
+ mainHandler.postDelayed(pendingSingleTap, doubleTapTimeoutMs + 50);
742
763
  } else {
743
764
  lastTapTime = 0;
744
- ensureLayout();
745
- float docX = event.getX() + offsetX;
746
- float docY = event.getY() + offsetY;
747
- int pageIdx = findPageIndexAt(docX, docY);
748
- if (pageIdx >= 0) {
749
- PageFrame frame = pageFrames.get(pageIdx);
750
- float nx = clamp01((docX - frame.left) / frame.width);
751
- float ny = clamp01((docY - frame.top) / frame.height);
752
- isSelectingText = true;
753
- selectPageIndex = pageIdx;
754
- selectStartX = nx;
755
- selectStartY = ny;
756
- selectEndX = nx;
757
- selectEndY = ny;
758
- performTextSelection();
759
- }
765
+ suppressPageTapForGesture = false;
766
+ beginWordSelection(event.getX(), event.getY());
760
767
  }
761
768
  return true;
769
+ }
770
+
762
771
  default:
763
772
  return true;
764
773
  }
765
774
  }
766
775
 
767
- private int hitTestHandle(float screenX, float screenY, float density) {
768
- if (selectedRects.isEmpty() || selectPageIndex < 0) return 0;
769
- PageFrame frame = null;
770
- for (PageFrame f : pageFrames) {
771
- if (f.index == selectPageIndex) {
772
- frame = f;
773
- break;
776
+ private PageFrame findSelectFrame() {
777
+ if (selectPageIndex < 0) return null;
778
+ for (PageFrame frame : pageFrames) {
779
+ if (frame.index == selectPageIndex) return frame;
780
+ }
781
+ return null;
782
+ }
783
+
784
+ private void clearSelectionState() {
785
+ isSelectingText = false;
786
+ selectPageIndex = -1;
787
+ selectedRects.clear();
788
+ selectedText = "";
789
+ draggedHandle = 0;
790
+ invalidate();
791
+ }
792
+
793
+ private boolean isPointInsideSelection(float screenX, float screenY, float density) {
794
+ if (selectedRects.isEmpty()) return false;
795
+ PageFrame frame = findSelectFrame();
796
+ if (frame == null) return false;
797
+ float padding = 8f * density;
798
+ for (NormalizedRect rect : selectedRects) {
799
+ float left = frame.left - offsetX + rect.x * frame.width;
800
+ float top = frame.top - offsetY + rect.y * frame.height;
801
+ float right = left + rect.width * frame.width;
802
+ float bottom = top + rect.height * frame.height;
803
+ if (screenX >= left - padding && screenX <= right + padding &&
804
+ screenY >= top - padding && screenY <= bottom + padding) {
805
+ return true;
774
806
  }
775
807
  }
808
+ return false;
809
+ }
810
+
811
+ private void beginWordSelection(float screenX, float screenY) {
812
+ if (!PapyrusTextSelect.AVAILABLE) return;
813
+ PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
814
+ if (state == null || state.document == null) return;
815
+ ensureLayout();
816
+ float docX = screenX + offsetX;
817
+ float docY = screenY + offsetY;
818
+ int pageIdx = findPageIndexAt(docX, docY);
819
+ if (pageIdx < 0) return;
820
+ PageFrame frame = pageFrames.get(pageIdx);
821
+ float nx = clamp01((docX - frame.left) / frame.width);
822
+ float ny = clamp01((docY - frame.top) / frame.height);
823
+ isSelectingText = true;
824
+ selectPageIndex = pageIdx;
825
+ selectStartX = nx;
826
+ selectStartY = ny;
827
+ selectEndX = nx;
828
+ selectEndY = ny;
829
+ performTextSelection(true);
830
+ }
831
+
832
+ private int hitTestHandle(float screenX, float screenY, float density) {
833
+ if (selectedRects.isEmpty() || selectPageIndex < 0) return 0;
834
+ PageFrame frame = findSelectFrame();
776
835
  if (frame == null) return 0;
777
836
 
778
- float radius = HANDLE_RADIUS_DP * density * 1.8f;
837
+ float radius = HANDLE_TOUCH_RADIUS_DP * density;
779
838
 
780
839
  // Start handle: top-left of first rect
781
840
  NormalizedRect first = selectedRects.get(0);
@@ -792,32 +851,76 @@ public class PapyrusPdfViewerView extends View {
792
851
  return 0;
793
852
  }
794
853
 
795
- private void performTextSelection() {
854
+ private void performTextSelection(boolean emitWhenReady) {
796
855
  if (selectPageIndex < 0) return;
797
856
  PapyrusEngineStore.EngineState state = PapyrusEngineStore.getEngine(engineId);
798
857
  if (state == null || state.document == null) return;
799
858
  if (!PapyrusTextSelect.AVAILABLE) return;
800
859
 
801
- float minX = Math.min(selectStartX, selectEndX);
802
- float minY = Math.min(selectStartY, selectEndY);
803
- float maxX = Math.max(selectStartX, selectEndX);
804
- float maxY = Math.max(selectStartY, selectEndY);
805
- float width = Math.max(0.001f, maxX - minX);
806
- float height = Math.max(0.001f, maxY - minY);
807
-
808
- final int pageIdx = selectPageIndex;
809
- final float selX = minX;
810
- final float selY = minY;
811
- final float selW = width;
812
- final float selH = height;
813
-
860
+ synchronized (selectionLock) {
861
+ selectionRequestVersion += 1;
862
+ selectionRequestQueuedVersion = selectionRequestVersion;
863
+ selectionRequestEngineId = engineId;
864
+ selectionRequestPage = selectPageIndex;
865
+ selectionRequestRectX = Math.min(selectStartX, selectEndX);
866
+ selectionRequestRectY = Math.min(selectStartY, selectEndY);
867
+ selectionRequestRectW = Math.max(0.001f, Math.abs(selectEndX - selectStartX));
868
+ selectionRequestRectH = Math.max(0.001f, Math.abs(selectEndY - selectStartY));
869
+ selectionRequestStartX = selectStartX;
870
+ selectionRequestStartY = selectStartY;
871
+ selectionRequestEndX = selectEndX;
872
+ selectionRequestEndY = selectEndY;
873
+ selectionRequestEmit = selectionRequestEmit || emitWhenReady;
874
+ selectionRequestPending = true;
875
+ if (selectionRequestInFlight) return;
876
+ selectionRequestInFlight = true;
877
+ }
878
+
879
+ final PapyrusEngineStore.EngineState requestState = state;
814
880
  final String sourcePath = state.sourcePath;
815
- SELECT_EXECUTOR.execute(() -> {
881
+ SELECT_EXECUTOR.execute(() -> drainTextSelection(requestState, sourcePath));
882
+ }
883
+
884
+ private void drainTextSelection(PapyrusEngineStore.EngineState state, String sourcePath) {
885
+ while (true) {
886
+ int pageIdx;
887
+ float x;
888
+ float y;
889
+ float w;
890
+ float h;
891
+ float startX;
892
+ float startY;
893
+ float endX;
894
+ float endY;
895
+ boolean emit;
896
+ long version;
897
+ String requestEngineId;
898
+ synchronized (selectionLock) {
899
+ if (!selectionRequestPending) {
900
+ selectionRequestInFlight = false;
901
+ return;
902
+ }
903
+ selectionRequestPending = false;
904
+ pageIdx = selectionRequestPage;
905
+ x = selectionRequestRectX;
906
+ y = selectionRequestRectY;
907
+ w = selectionRequestRectW;
908
+ h = selectionRequestRectH;
909
+ startX = selectionRequestStartX;
910
+ startY = selectionRequestStartY;
911
+ endX = selectionRequestEndX;
912
+ endY = selectionRequestEndY;
913
+ emit = selectionRequestEmit;
914
+ selectionRequestEmit = false;
915
+ version = selectionRequestQueuedVersion;
916
+ requestEngineId = selectionRequestEngineId;
917
+ }
918
+
816
919
  PapyrusTextSelection selection = null;
817
920
  try {
818
921
  if (sourcePath != null && !sourcePath.isEmpty()) {
819
922
  synchronized (state.pdfiumLock) {
820
- selection = PapyrusTextSelect.nativeSelectTextFile(sourcePath, pageIdx, selX, selY, selW, selH);
923
+ selection = PapyrusTextSelect.nativeSelectTextFile(sourcePath, pageIdx, x, y, w, h, startX, startY, endX, endY);
821
924
  }
822
925
  } else {
823
926
  long docPtr;
@@ -826,7 +929,7 @@ public class PapyrusPdfViewerView extends View {
826
929
  }
827
930
  if (docPtr != 0) {
828
931
  synchronized (state.pdfiumLock) {
829
- selection = PapyrusTextSelect.nativeSelectText(docPtr, pageIdx, selX, selY, selW, selH);
932
+ selection = PapyrusTextSelect.nativeSelectText(docPtr, pageIdx, x, y, w, h, startX, startY, endX, endY);
830
933
  }
831
934
  }
832
935
  }
@@ -834,30 +937,56 @@ public class PapyrusPdfViewerView extends View {
834
937
  }
835
938
 
836
939
  final PapyrusTextSelection finalSelection = selection;
837
- mainHandler.post(() -> {
838
- if (!isSelectingText && selectPageIndex != pageIdx) return;
839
- selectedRects.clear();
840
- if (finalSelection != null && finalSelection.rects != null && finalSelection.rects.length >= 4) {
841
- selectedText = finalSelection.text != null ? finalSelection.text : "";
842
- for (int i = 0; i + 3 < finalSelection.rects.length; i += 4) {
843
- selectedRects.add(new NormalizedRect(
844
- finalSelection.rects[i],
845
- finalSelection.rects[i + 1],
846
- finalSelection.rects[i + 2],
847
- finalSelection.rects[i + 3]
848
- ));
849
- }
850
- } else {
851
- selectedText = "";
852
- }
853
- invalidate();
854
- });
855
- });
940
+ final long finalVersion = version;
941
+ final String finalEngineId = requestEngineId;
942
+ final int finalPage = pageIdx;
943
+ final boolean finalEmit = emit;
944
+ mainHandler.post(() -> applyTextSelection(
945
+ finalVersion, finalEngineId, finalPage, finalSelection, finalEmit));
946
+ }
947
+ }
948
+
949
+ private void applyTextSelection(
950
+ long version,
951
+ String requestEngineId,
952
+ int pageIdx,
953
+ PapyrusTextSelection selection,
954
+ boolean emit
955
+ ) {
956
+ synchronized (selectionLock) {
957
+ if (version != selectionRequestVersion) return;
958
+ }
959
+ if (requestEngineId == null || !requestEngineId.equals(engineId)) return;
960
+ if (!isSelectingText || selectPageIndex != pageIdx) return;
961
+
962
+ selectedRects.clear();
963
+ if (selection != null && selection.rects != null && selection.rects.length >= 4) {
964
+ selectedText = selection.text != null ? selection.text : "";
965
+ for (int i = 0; i + 3 < selection.rects.length; i += 4) {
966
+ selectedRects.add(new NormalizedRect(
967
+ selection.rects[i],
968
+ selection.rects[i + 1],
969
+ selection.rects[i + 2],
970
+ selection.rects[i + 3]
971
+ ));
972
+ }
973
+ } else {
974
+ selectedText = "";
975
+ // Movimentos de alça (emit == false) mantêm o gesto vivo para o UP
976
+ // pedir a extração final e emitir o estado vazio.
977
+ if (emit) {
978
+ isSelectingText = false;
979
+ selectPageIndex = -1;
980
+ }
981
+ }
982
+ invalidate();
983
+ if (emit) {
984
+ emitTextSelected();
985
+ }
856
986
  }
857
987
 
858
988
  private void emitTextSelected() {
859
989
  try {
860
- if (selectedRects.isEmpty()) return;
861
990
  WritableMap event = Arguments.createMap();
862
991
  event.putString("text", selectedText);
863
992
  event.putInt("pageIndex", selectPageIndex);
@@ -877,6 +1006,18 @@ public class PapyrusPdfViewerView extends View {
877
1006
  }
878
1007
  }
879
1008
 
1009
+ private void emitSelectionCleared() {
1010
+ try {
1011
+ WritableMap event = Arguments.createMap();
1012
+ event.putString("text", "");
1013
+ event.putInt("pageIndex", -1);
1014
+ event.putArray("rects", Arguments.createArray());
1015
+ reactContext.getJSModule(RCTEventEmitter.class)
1016
+ .receiveEvent(getId(), "onTextSelected", event);
1017
+ } catch (Throwable ignored) {
1018
+ }
1019
+ }
1020
+
880
1021
  private int findPageIndexAt(float docX, float docY) {
881
1022
  for (PageFrame frame : pageFrames) {
882
1023
  if (docX >= frame.left && docX <= frame.left + frame.width &&
@@ -1533,7 +1674,7 @@ public class PapyrusPdfViewerView extends View {
1533
1674
  WritableMap event = Arguments.createMap();
1534
1675
  event.putDouble("offsetY", offsetYValue);
1535
1676
  reactContext.getJSModule(RCTEventEmitter.class)
1536
- .receiveEvent(getId(), "onScroll", event);
1677
+ .receiveEvent(getId(), "topScroll", event);
1537
1678
  emitVisiblePagesChanged();
1538
1679
  pendingScrollEvent = null;
1539
1680
  };