@onekeyfe/react-native-pager-view 6.7.0-4 → 6.7.0-41

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 (52) hide show
  1. package/README.md +21 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/androidTest/AndroidManifest.xml +7 -0
  4. package/android/src/androidTest/java/com/reactnativepagerview/CollapsiblePagerTabReservationTest.kt +476 -0
  5. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerHost.kt +233 -43
  6. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerNativeHeaders.kt +596 -27
  7. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerViewManager.kt +40 -0
  8. package/android/src/main/java/com/reactnativepagerview/NativeScrollerScrollAwayInsets.kt +19 -0
  9. package/android/src/main/java/com/reactnativepagerview/NativeScrollerView.kt +50 -6
  10. package/android/src/main/java/com/reactnativepagerview/NestedScrollableHost.kt +8 -2
  11. package/android/src/main/java/com/reactnativepagerview/event/NativeHeaderPressEvent.kt +8 -0
  12. package/android/src/test/java/com/reactnativepagerview/NativeScrollerScrollAwayInsetsTest.kt +40 -0
  13. package/docs/SPEC.md +700 -10
  14. package/ios/RNCCollapsiblePagerViewComponentView.mm +2221 -94
  15. package/ios/RNCNativeScrollerComponentView.h +2 -0
  16. package/ios/RNCNativeScrollerComponentView.mm +42 -0
  17. package/ios/tests/RNCCollapsiblePagerFirstFrameTests.mm +562 -1
  18. package/ios/tests/RNCCollapsiblePagerHitTestTests.mm +1116 -0
  19. package/ios/tests/RNCCollapsiblePagerPressCancellationTests.mm +74 -9
  20. package/ios/tests/RNCCollapsiblePagerRefreshControlTests.mm +50 -0
  21. package/lib/module/CollapsiblePagerView.js +75 -11
  22. package/lib/module/CollapsiblePagerView.web.js +653 -8
  23. package/lib/module/CollapsiblePagerViewNativeComponent.ts +57 -42
  24. package/lib/module/NativeScroller.web.js +3 -0
  25. package/lib/module/NativeScrollerNativeComponent.ts +42 -46
  26. package/lib/module/PagerViewNativeComponent.ts +20 -25
  27. package/lib/module/WebCollapsiblePagerPage.js +7 -2
  28. package/lib/module/index.native.js +9 -0
  29. package/lib/typescript/src/CollapsiblePagerView.d.ts +62 -4
  30. package/lib/typescript/src/CollapsiblePagerView.web.d.ts +56 -2
  31. package/lib/typescript/src/CollapsiblePagerViewNativeComponent.d.ts +54 -38
  32. package/lib/typescript/src/NativeScroller.d.ts +5 -3
  33. package/lib/typescript/src/NativeScroller.web.d.ts +1 -1
  34. package/lib/typescript/src/NativeScrollerNativeComponent.d.ts +47 -48
  35. package/lib/typescript/src/PagerView.d.ts +1 -1
  36. package/lib/typescript/src/PagerView.web.d.ts +1 -1
  37. package/lib/typescript/src/PagerViewNativeComponent.d.ts +20 -21
  38. package/lib/typescript/src/WebCollapsiblePagerPage.d.ts +1 -1
  39. package/lib/typescript/src/index.native.d.ts +13 -0
  40. package/lib/typescript/src/usePagerView.d.ts +2 -2
  41. package/lib/typescript/src/utils.d.ts +2 -2
  42. package/package.json +22 -7
  43. package/react-native-pager-view.podspec +1 -0
  44. package/src/CollapsiblePagerView.tsx +208 -12
  45. package/src/CollapsiblePagerView.web.tsx +587 -7
  46. package/src/CollapsiblePagerViewNativeComponent.ts +57 -42
  47. package/src/NativeScroller.tsx +14 -4
  48. package/src/NativeScroller.web.tsx +15 -1
  49. package/src/NativeScrollerNativeComponent.ts +42 -46
  50. package/src/PagerViewNativeComponent.ts +20 -25
  51. package/src/WebCollapsiblePagerPage.tsx +7 -2
  52. package/src/index.native.tsx +30 -0
@@ -87,6 +87,8 @@ internal class CollapsiblePagerAdapter : RecyclerView.Adapter<ViewPagerViewHolde
87
87
  */
88
88
  // OneKey patch: Match the standard pager's nested horizontal gesture host.
89
89
  // Original: class CollapsiblePagerHost(context: Context) : FrameLayout(context), NestedScrollingParent3 {
90
+ // Kept for react-native-bottom-tabs, which calls `tryScrollToTop` by name on a tab double press.
91
+ @androidx.annotation.Keep
90
92
  class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), NestedScrollingParent3 {
91
93
  private enum class HeaderGestureOwner {
92
94
  NONE,
@@ -115,6 +117,16 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
115
117
 
116
118
  val pager = ViewPager2(context)
117
119
  internal val adapter = CollapsiblePagerAdapter()
120
+ var onNativeToolbarPress: ((String, DoubleArray) -> Unit)? = null
121
+ fun updateNativeTabBarConfig(value: String?) {
122
+ toolbarSettledStamp = null
123
+ nativeTabBarView.updateConfigJSON(value)
124
+ settleNativeTabProgressIfReady()
125
+ }
126
+
127
+ private var toolbarTransitionGeneration = 0
128
+ private var toolbarSettlePostedGeneration: Int? = null
129
+ private var toolbarSettledStamp: Pair<Int, Int>? = null
118
130
  private val nativeTabBarView = CollapsiblePagerNativeTabBarView(context)
119
131
  private val nativeSubHeaderView = CollapsiblePagerNativeSubHeaderView(context)
120
132
  private val logicalChildren = ArrayList<View>()
@@ -142,7 +154,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
142
154
  private var transitionSourceRecyclerAnchor: RecyclerReadingAnchor? = null
143
155
  private val commandBodyKeys = HashSet<String>()
144
156
  private var explicitTopCommand: Pair<String, PageRestoreRequest>? = null
145
- private class PagerTopRequest(view: View, val key: String, val pageIndex: Int, val generation: Int, val target: Int, val token: Int) {
157
+ private class PagerTopRequest(view: View, val key: String, val pageIndex: Int, val generation: Int, val logicalOffset: Int, val token: Int) {
146
158
  val view = WeakReference(view)
147
159
  }
148
160
  private var pagerTopRequest: PagerTopRequest? = null
@@ -293,7 +305,23 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
293
305
  var onNativeSubHeaderPress: ((Int, String) -> Unit)? = null
294
306
 
295
307
  var nativeTabBarHeight = 44.0
308
+ var nativeTabBarVariant = "underline"
309
+ var nativeTabBarTrailingReservedWidth = 0.0
310
+ set(value) {
311
+ field = if (value.isFinite() && value >= 0.0) value else 0.0
312
+ bringNativeHeadersToFront()
313
+ requestLayout()
314
+ }
315
+
316
+ private fun nativeTabReservationPx(width: Int): Int {
317
+ val reserved = nativeTabBarTrailingReservedWidth
318
+ val density = resources.displayMetrics.density
319
+ return if (reserved.isFinite() && reserved > 0.0 && width > 0 && density > 0f) {
320
+ (reserved.coerceAtMost(width / density.toDouble()) * density).roundToInt()
321
+ } else 0
322
+ }
296
323
  var nativeTabBarContentPaddingHorizontal = 20.0
324
+ var nativeTabBarContentPaddingVertical = 0.0
297
325
  var nativeTabBarItemSpacing = 8.0
298
326
  var nativeTabBarFontSize = 16.0
299
327
  var nativeTabBarFontFamily: String? = null
@@ -311,10 +339,12 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
311
339
  pager.id = View.generateViewId()
312
340
  pager.adapter = adapter
313
341
  pager.isSaveEnabled = false
342
+ (pager.getChildAt(0) as? RecyclerView)?.itemAnimator = null
314
343
  super.addView(
315
344
  pager,
316
345
  LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT),
317
346
  )
347
+ nativeTabBarView.onToolbarPress = { key, rect -> onNativeToolbarPress?.invoke(key, rect) }
318
348
  nativeTabBarView.onItemPress = { index, key -> onNativeTabPress?.invoke(index, key) }
319
349
  nativeSubHeaderView.onItemPress = { index, key -> onNativeSubHeaderPress?.invoke(index, key) }
320
350
  super.addView(nativeTabBarView)
@@ -382,6 +412,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
382
412
  }
383
413
 
384
414
  fun updateNativeTabBarItems(value: String?) {
415
+ toolbarSettledStamp = null
385
416
  nativeTabBarView.updateItemsJSON(value)
386
417
  bringNativeHeadersToFront()
387
418
  updateHeaderLayout()
@@ -394,10 +425,44 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
394
425
  updateHeaderLayout()
395
426
  }
396
427
 
397
- fun updateNativeTabProgress(position: Int, offset: Float) {
428
+ fun updateNativeTabProgress(position: Int, offset: Float, fromActualPosition: Boolean = false) {
429
+ // ViewPager2 selects the target before a smooth scroll reaches it.
430
+ if (!fromActualPosition && nativeTabBarView.usesControlledTextProgress()) return
431
+ if (fromActualPosition) toolbarSettledStamp = null
398
432
  nativeTabBarView.setProgress(position + offset)
399
433
  }
400
434
 
435
+ private fun settledPagerPage(): Int? {
436
+ if (!isAttachedToWindow || !isLaidOut || width <= 0 || pager.width <= 0 ||
437
+ pager.isLayoutRequested || isPagerRecyclerLayoutSuppressed() || pager.isFakeDragging ||
438
+ pager.scrollState != ViewPager2.SCROLL_STATE_IDLE || pager.currentItem !in 0 until adapter.itemCount) return null
439
+ val recycler = pager.getChildAt(0) as? RecyclerView ?: return null
440
+ if (recycler.isLayoutRequested || recycler.isComputingLayout || recycler.width <= 0 || recycler.height <= 0) return null
441
+ return pager.currentItem
442
+ }
443
+
444
+ private fun suspendNativeToolbar() {
445
+ toolbarTransitionGeneration++
446
+ toolbarSettledStamp = null
447
+ nativeTabBarView.updateToolbarPagerState(-1, true)
448
+ }
449
+
450
+ private fun settleNativeTabProgressIfReady() {
451
+ val page = settledPagerPage() ?: return
452
+ val generation = toolbarTransitionGeneration
453
+ if (toolbarSettledStamp == (generation to page) && !nativeTabBarView.needsToolbarPagerSettlement(page)) return
454
+ if (toolbarSettlePostedGeneration == generation) return
455
+ toolbarSettlePostedGeneration = generation
456
+ postForCurrentAttachment {
457
+ if (toolbarTransitionGeneration != generation || toolbarSettlePostedGeneration != generation) return@postForCurrentAttachment
458
+ toolbarSettlePostedGeneration = null
459
+ val settledPage = settledPagerPage() ?: return@postForCurrentAttachment
460
+ if (nativeTabBarView.usesControlledTextProgress()) updateNativeTabProgress(settledPage, 0f, fromActualPosition = true)
461
+ nativeTabBarView.updateToolbarPagerState(settledPage, false)
462
+ toolbarSettledStamp = generation to settledPage
463
+ }
464
+ }
465
+
401
466
  fun logPagerState(reason: String) {
402
467
  log(
403
468
  "pager-state reason=$reason inner=$selectedPage outer=${outerPagerIndex()} " +
@@ -416,7 +481,9 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
416
481
  fun applyNativeHeaderStyle() {
417
482
  nativeTabBarView.updateStyle(
418
483
  nativeTabBarHeight,
484
+ nativeTabBarVariant,
419
485
  nativeTabBarContentPaddingHorizontal,
486
+ nativeTabBarContentPaddingVertical,
420
487
  nativeTabBarItemSpacing,
421
488
  nativeTabBarFontSize,
422
489
  nativeTabBarFontFamily,
@@ -442,6 +509,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
442
509
  private fun bringNativeHeadersToFront() {
443
510
  nativeTabBarView.bringToFront()
444
511
  nativeSubHeaderView.bringToFront()
512
+ if (nativeTabBarTrailingReservedWidth > 0.0) stickyHeaderView?.bringToFront()
445
513
  }
446
514
 
447
515
  fun addReactChild(child: View, index: Int) {
@@ -568,12 +636,33 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
568
636
  }
569
637
 
570
638
  internal fun beginPageTransition(target: Int? = null) {
639
+ suspendNativeToolbar()
571
640
  cancelPagerTop()
572
641
  if (adapter.itemCount == 0 || !nativeSmoothHeaderScrollEnabled || headerHeightPx <= 0) return
573
642
  if (transitionHeaderOffsets == null) {
574
- transitionSourceDisplayBody = primaryScrollableForPage(selectedPage)?.let(::verticalScrollOffset) ?: 0
575
- transitionSourceRecyclerAnchor = (primaryScrollableForPage(selectedPage) as? RecyclerView)?.let(::captureRecyclerAnchor)
576
- pageHeaderOffsets[currentPageKey()] = headerOffsetPx
643
+ val sourceKey = currentPageKey()
644
+ val sourceView = primaryScrollableForPage(selectedPage)
645
+ val topInset = headerHeightPx + stickyHeaderHeightPx
646
+ val sourceRestored = sourceKey !in pendingPageRestores && when (sourceView) {
647
+ is RecyclerView -> restoredRecyclerKeys[sourceView] == sourceKey &&
648
+ originalRecyclerPadding[sourceView]?.appliedTopInset == topInset
649
+ is ScrollView -> restoredScrollerKeys[sourceView] == sourceKey &&
650
+ originalScrollerPadding[sourceView]?.appliedTopInset == topInset
651
+ is NativeScrollerViewport -> ownedScrollerKeys[sourceView] == sourceKey
652
+ else -> false
653
+ }
654
+ transitionSourceDisplayBody = if (sourceRestored && sourceView != null) {
655
+ verticalScrollOffset(sourceView)
656
+ } else {
657
+ displayBodyOverrides[sourceKey] ?: pageOffsets[sourceKey] ?: 0
658
+ }
659
+ transitionSourceRecyclerAnchor = if (sourceRestored && sourceView is RecyclerView) {
660
+ captureRecyclerAnchor(sourceView)
661
+ } else {
662
+ (displayRecyclerAnchors[sourceKey] ?: recyclerReadingAnchors[sourceKey])
663
+ ?.takeIf { sourceView !is RecyclerView || (it.view.get() === sourceView && it.adapter === sourceView.adapter) }
664
+ }
665
+ pageHeaderOffsets[sourceKey] = headerOffsetPx
577
666
  detachPrimaryScrollObserver()
578
667
  transitionSourcePage = selectedPage
579
668
  transitionHeaderOffsets = pageHeaderOffsets.toMap()
@@ -587,10 +676,12 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
587
676
  }
588
677
 
589
678
  internal fun onPagerScrolled(position: Int, offset: Float) {
679
+ val controlledTextProgress = nativeTabBarView.usesControlledTextProgress()
680
+ if (controlledTextProgress) updateNativeTabProgress(position, offset, fromActualPosition = true)
590
681
  if (transitionHeaderOffsets == null) return
591
682
  alignPageBody(position)
592
683
  if (position + 1 < adapter.itemCount) alignPageBody(position + 1)
593
- updateNativeTabProgress(position, offset)
684
+ if (!controlledTextProgress) updateNativeTabProgress(position, offset)
594
685
  }
595
686
 
596
687
  internal fun onPagerScrollStateChanged(state: Int) {
@@ -600,6 +691,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
600
691
  } else if (state == ViewPager2.SCROLL_STATE_IDLE && transitionHeaderOffsets != null) {
601
692
  finishPageTransition(pager.currentItem)
602
693
  }
694
+ if (state == ViewPager2.SCROLL_STATE_IDLE) settleNativeTabProgressIfReady()
603
695
  }
604
696
 
605
697
  private fun finishPageTransition(position: Int) {
@@ -689,14 +781,44 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
689
781
 
690
782
  private fun layoutPagerIfRequested() {
691
783
  val recycler = pager.getChildAt(0) as? RecyclerView ?: return
692
- if (!recycler.isLayoutRequested || recycler.isComputingLayout || recycler.width <= 0 || recycler.height <= 0) return
693
- // OneKey patch: a non-animated page command queues RecyclerView layout, but RN
694
- // can stop that request after navigation. Drain it before showing the page.
695
- recycler.measure(
696
- MeasureSpec.makeMeasureSpec(recycler.width, MeasureSpec.EXACTLY),
697
- MeasureSpec.makeMeasureSpec(recycler.height, MeasureSpec.EXACTLY),
698
- )
699
- recycler.layout(recycler.left, recycler.top, recycler.right, recycler.bottom)
784
+ val controlledSettlement = nativeTabBarView.usesControlledTextProgress() || nativeTabBarView.usesSettledToolbarOwner()
785
+ if (controlledSettlement && !isLaidOut && isAttachedToWindow && isShown &&
786
+ width > 0 && height > 0 && pager.width > 0 && pager.height > 0 &&
787
+ pager.scrollState == ViewPager2.SCROLL_STATE_IDLE && !pager.isFakeDragging &&
788
+ pager.currentItem in 0 until adapter.itemCount && !isPagerRecyclerLayoutSuppressed() &&
789
+ !recycler.isLayoutSuppressed && !recycler.isComputingLayout) {
790
+ // A retained Fabric host can reattach with bounds but no completed layout.
791
+ updateHeaderLayout()
792
+ }
793
+ if (controlledSettlement && pager.isLayoutRequested && isAttachedToWindow && isLaidOut &&
794
+ width > 0 && height > 0 && pager.width > 0 && pager.height > 0 &&
795
+ pager.scrollState == ViewPager2.SCROLL_STATE_IDLE && !pager.isFakeDragging &&
796
+ pager.currentItem in 0 until adapter.itemCount && !isPagerRecyclerLayoutSuppressed() &&
797
+ !recycler.isLayoutSuppressed && !recycler.isComputingLayout) {
798
+ // RN can stop the parent request after the recycler has already laid out.
799
+ pager.measure(
800
+ MeasureSpec.makeMeasureSpec(pager.width, MeasureSpec.EXACTLY),
801
+ MeasureSpec.makeMeasureSpec(pager.height, MeasureSpec.EXACTLY),
802
+ )
803
+ pager.layout(pager.left, pager.top, pager.right, pager.bottom)
804
+ }
805
+ if (shouldDrainCollapsiblePagerLayout(
806
+ recycler.isLayoutRequested,
807
+ recycler.isLayoutSuppressed,
808
+ recycler.isComputingLayout,
809
+ recycler.width,
810
+ recycler.height,
811
+ adapter.itemCount,
812
+ )) {
813
+ // OneKey patch: a non-animated page command queues RecyclerView layout, but RN
814
+ // can stop that request after navigation. Drain it before showing the page.
815
+ recycler.measure(
816
+ MeasureSpec.makeMeasureSpec(recycler.width, MeasureSpec.EXACTLY),
817
+ MeasureSpec.makeMeasureSpec(recycler.height, MeasureSpec.EXACTLY),
818
+ )
819
+ recycler.layout(recycler.left, recycler.top, recycler.right, recycler.bottom)
820
+ }
821
+ if (controlledSettlement) settleNativeTabProgressIfReady()
700
822
  }
701
823
 
702
824
  private fun updateHeaderLayout() {
@@ -712,6 +834,13 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
712
834
  layout(left, top, right, bottom)
713
835
  }
714
836
 
837
+ private fun isPagerRecyclerLayoutSuppressed(): Boolean =
838
+ shouldSuppressCollapsiblePagerRecyclerLayout(
839
+ (pager.getChildAt(0) as? RecyclerView)?.isLayoutSuppressed == true,
840
+ adapter.itemCount,
841
+ attachedPageCount(),
842
+ )
843
+
715
844
  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
716
845
  val width = MeasureSpec.getSize(widthMeasureSpec)
717
846
  val height = MeasureSpec.getSize(heightMeasureSpec)
@@ -722,10 +851,12 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
722
851
  return
723
852
  }
724
853
  val pagerHeight = height + headerHeightPx
725
- pager.measure(
726
- MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
727
- MeasureSpec.makeMeasureSpec(pagerHeight, MeasureSpec.EXACTLY),
728
- )
854
+ if (!isPagerRecyclerLayoutSuppressed()) {
855
+ pager.measure(
856
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
857
+ MeasureSpec.makeMeasureSpec(pagerHeight, MeasureSpec.EXACTLY),
858
+ )
859
+ }
729
860
  headerView?.measure(
730
861
  MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
731
862
  MeasureSpec.makeMeasureSpec(headerHeightPx, MeasureSpec.EXACTLY),
@@ -737,8 +868,9 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
737
868
  val nativeTabHeight = if (nativeTabBarView.visibility == View.VISIBLE) {
738
869
  nativeTabBarView.preferredHeightPx()
739
870
  } else 0
871
+ val tabReservedWidth = nativeTabReservationPx(width)
740
872
  nativeTabBarView.measure(
741
- MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
873
+ MeasureSpec.makeMeasureSpec(width - tabReservedWidth, MeasureSpec.EXACTLY),
742
874
  MeasureSpec.makeMeasureSpec(nativeTabHeight, MeasureSpec.EXACTLY),
743
875
  )
744
876
  val nativeSubHeaderHeight = if (nativeSubHeaderView.visibility == View.VISIBLE) {
@@ -757,7 +889,9 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
757
889
  // RN ancestors allow overflow; keep translated header content inside this viewport.
758
890
  clipBounds = Rect(0, 0, width, height)
759
891
  if (width <= 0 || height <= 0) return
760
- pager.layout(0, 0, width, height + headerHeightPx)
892
+ if (!isPagerRecyclerLayoutSuppressed()) {
893
+ pager.layout(0, 0, width, height + headerHeightPx)
894
+ }
761
895
  headerView?.layout(0, 0, width, headerHeightPx)
762
896
  stickyHeaderView?.layout(
763
897
  0,
@@ -766,7 +900,12 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
766
900
  headerHeightPx + stickyHeaderHeightPx,
767
901
  )
768
902
  val nativeTabHeight = nativeTabBarView.measuredHeight
769
- nativeTabBarView.layout(0, headerHeightPx, width, headerHeightPx + nativeTabHeight)
903
+ val tabReservedWidth = nativeTabReservationPx(width)
904
+ val tabLeft = if (nativeTabBarView.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
905
+ tabReservedWidth
906
+ } else 0
907
+ nativeTabBarView.layout(tabLeft, headerHeightPx, tabLeft + width - tabReservedWidth,
908
+ headerHeightPx + nativeTabHeight)
770
909
  nativeSubHeaderView.layout(
771
910
  0,
772
911
  headerHeightPx + nativeTabHeight,
@@ -777,6 +916,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
777
916
  postForCurrentAttachment {
778
917
  prepareAdjacentPages()
779
918
  attachPrimaryScrollObserver()
919
+ settleNativeTabProgressIfReady()
780
920
  }
781
921
  }
782
922
 
@@ -828,7 +968,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
828
968
  }
829
969
  if (!reached()) {
830
970
  if (!isAttachedToWindow || !view.isAttachedToWindow || width <= 0 || height <= 0 ||
831
- view.width <= 0 || view.height <= 0 || isInLayout || view.isInLayout || view.isComputingLayout ||
971
+ view.width <= 0 || view.height <= 0 || isInLayout || view.isInLayout || view.isLayoutSuppressed || view.isComputingLayout ||
832
972
  pageKey(index) != key || primaryScrollableForPage(index) !== view || !isCurrentPageRestore(key, request)) return false
833
973
  val previousSynthetic = syntheticPageRestore
834
974
  syntheticPageRestore = true
@@ -987,18 +1127,49 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
987
1127
  if (!settled) return
988
1128
  pageOffsets[request.key] = 0
989
1129
  if (view is RecyclerView) saveRecyclerReadingAnchor(request.key, view)
990
- setHeaderOffset(if (request.target == 1) headerHeightPx else 0)
991
- offsetDiag("pager-top-complete-target=${request.target}-token=${request.token}", view, request.key)
1130
+ setHeaderOffset(request.logicalOffset.coerceIn(0, headerHeightPx))
1131
+ offsetDiag("pager-top-complete-offset=${request.logicalOffset}-token=${request.token}", view, request.key)
992
1132
  cancelPagerTop()
993
1133
  }
994
1134
 
995
- internal fun scrollToTop(target: Int, animated: Boolean) {
996
- if (target !in 0..1 || pager.scrollState != ViewPager2.SCROLL_STATE_IDLE || transitionHeaderOffsets != null ||
997
- syntheticPageRestore || selectedPage !in 0 until adapter.itemCount || !isAttachedToWindow) return
998
- val view = observedRecyclerView ?: observedOwnedScroller ?: observedNativeScroller ?: return
1135
+ @androidx.annotation.Keep
1136
+ fun scrollToTop(target: Int, animated: Boolean) {
1137
+ tryScrollToTop(target, animated)
1138
+ }
1139
+
1140
+ @androidx.annotation.Keep
1141
+ fun tryScrollToTop(target: Int, animated: Boolean): Boolean {
1142
+ if (target !in 0..1) return false
1143
+ return scrollHeaderToOffset(if (target == 1) headerHeightPx else 0, animated)
1144
+ }
1145
+
1146
+ // The header offset is stored apart from the body, so a collapsed header can sit
1147
+ // above a body that is already at its top.
1148
+ @androidx.annotation.Keep
1149
+ fun isAwayFromPageTop(): Boolean = headerOffsetPx > 0
1150
+
1151
+ internal fun scrollHeaderBy(delta: Double) {
1152
+ if (!delta.isFinite()) return
1153
+ val density = resources.displayMetrics.density.toDouble()
1154
+ val maxDip = headerHeightPx / density
1155
+ val deltaPx = (delta.coerceIn(-maxDip, maxDip) * density).roundToInt()
1156
+ scrollHeaderToOffset((headerOffsetPx.toLong() + deltaPx).coerceIn(0, headerHeightPx.toLong()).toInt(), false)
1157
+ }
1158
+
1159
+ internal fun scrollHeaderToOffset(offset: Double) {
1160
+ if (!offset.isFinite()) return
1161
+ val density = resources.displayMetrics.density.toDouble()
1162
+ val targetPx = (offset.coerceIn(0.0, headerHeightPx / density) * density).roundToInt()
1163
+ scrollHeaderToOffset(targetPx, false)
1164
+ }
1165
+
1166
+ private fun scrollHeaderToOffset(logicalOffset: Int, animated: Boolean): Boolean {
1167
+ if (pager.scrollState != ViewPager2.SCROLL_STATE_IDLE || transitionHeaderOffsets != null ||
1168
+ syntheticPageRestore || selectedPage !in 0 until adapter.itemCount || !isAttachedToWindow) return false
1169
+ val view = observedRecyclerView ?: observedOwnedScroller ?: observedNativeScroller ?: return false
999
1170
  val key = currentPageKey()
1000
1171
  val ownerKey = observedRecyclerKey ?: observedOwnedScrollerKey ?: observedNativeScrollerKey
1001
- if (ownerKey != key || !view.isAttachedToWindow || primaryScrollableForPage(selectedPage) !== view) return
1172
+ if (ownerKey != key || !view.isAttachedToWindow || primaryScrollableForPage(selectedPage) !== view) return false
1002
1173
  // Finish old child notifications before installing the new Pager-owned target.
1003
1174
  cancelPagerTop()
1004
1175
  if (view is RecyclerView) {
@@ -1020,11 +1191,12 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
1020
1191
  if (view is RecyclerView) deferredRecyclerGeometry.remove(view)
1021
1192
  if (view is ScrollView) deferredScrollerGeometry.remove(view)
1022
1193
  pageOffsets[key] = 0
1023
- val request = PagerTopRequest(view, key, selectedPage, attachmentGeneration, target, ++pagerTopToken)
1194
+ val targetOffset = logicalOffset.coerceIn(0, headerHeightPx)
1195
+ val request = PagerTopRequest(view, key, selectedPage, attachmentGeneration, targetOffset, ++pagerTopToken)
1024
1196
  pagerTopRequest = request
1025
1197
  commandBodyKeys.add(key)
1026
- setHeaderOffset(if (target == 1) headerHeightPx else 0)
1027
- offsetDiag("pager-top-begin-target=$target-animated=$animated-token=${request.token}", view, key)
1198
+ setHeaderOffset(targetOffset)
1199
+ offsetDiag("pager-top-begin-offset=$targetOffset-animated=$animated-token=${request.token}", view, key)
1028
1200
  dispatchingPagerTop = true
1029
1201
  try {
1030
1202
  when (view) {
@@ -1033,7 +1205,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
1033
1205
  if (animated && !recyclerIsAtBodyTop(view)) view.smoothScrollToPosition(0)
1034
1206
  else if (manager != null && manager.orientation == RecyclerView.VERTICAL) {
1035
1207
  manager.scrollToPositionWithOffset(0, 0)
1036
- if (view.width > 0 && view.height > 0 && !isInLayout && !view.isInLayout && !view.isComputingLayout) {
1208
+ if (view.width > 0 && view.height > 0 && !isInLayout && !view.isInLayout && !view.isLayoutSuppressed && !view.isComputingLayout) {
1037
1209
  view.measure(MeasureSpec.makeMeasureSpec(view.width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(view.height, MeasureSpec.EXACTLY))
1038
1210
  view.layout(view.left, view.top, view.right, view.bottom)
1039
1211
  }
@@ -1044,6 +1216,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
1044
1216
  }
1045
1217
  } finally { dispatchingPagerTop = false }
1046
1218
  finishPagerTopIfSettled()
1219
+ return true
1047
1220
  }
1048
1221
 
1049
1222
  private fun hasExplicitTopCommand(view: View, key: String): Boolean {
@@ -1617,6 +1790,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
1617
1790
  )
1618
1791
  }
1619
1792
  updateRecyclerPaddingOwnership(recycler, original)
1793
+ val key = pageKey(pageIndex)
1620
1794
  val topInset = headerHeightPx + stickyHeaderHeightPx
1621
1795
  recycler.clipToPadding = false
1622
1796
  val top = original.top + topInset
@@ -1628,17 +1802,28 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
1628
1802
  // OneKey patch: LinearLayoutManager otherwise anchors children at their old
1629
1803
  // screen coordinates when a page changes the sticky header height.
1630
1804
  val manager = recycler.layoutManager as? LinearLayoutManager
1631
- val first = manager?.takeIf { it.orientation == RecyclerView.VERTICAL }
1632
- ?.findFirstVisibleItemPosition() ?: RecyclerView.NO_POSITION
1633
- val anchorOffset = if (first != RecyclerView.NO_POSITION) {
1805
+ val savedAnchor = if (nativeSmoothHeaderScrollEnabled && original.appliedTopInset == 0) {
1806
+ if (headerOffsetPx < headerHeightPx) {
1807
+ recycler.adapter?.let { RecyclerReadingAnchor(recycler, it, 0, 0) }
1808
+ } else {
1809
+ (if (transitionPageOffsets != null && pageIndex == transitionSourcePage) transitionSourceRecyclerAnchor
1810
+ else displayRecyclerAnchors[key] ?: recyclerReadingAnchors[key])
1811
+ ?.takeIf { it.view.get() === recycler && it.adapter === recycler.adapter }
1812
+ }
1813
+ } else null
1814
+ val first = savedAnchor?.position
1815
+ ?: manager?.takeIf { it.orientation == RecyclerView.VERTICAL }
1816
+ ?.findFirstVisibleItemPosition()
1817
+ ?: RecyclerView.NO_POSITION
1818
+ val anchorOffset = savedAnchor?.top ?: if (first != RecyclerView.NO_POSITION) {
1634
1819
  manager?.findViewByPosition(first)?.let { manager.getDecoratedTop(it) - recycler.paddingTop }
1635
1820
  } else null
1636
- offsetDiag("recycler-inset-before-nextTop=$top-anchorPosition=$first-anchorOffset=$anchorOffset", recycler, pageKey(pageIndex), pageIndex)
1821
+ offsetDiag("recycler-inset-before-nextTop=$top-anchorPosition=$first-anchorOffset=$anchorOffset", recycler, key, pageIndex)
1637
1822
  recycler.setPadding(original.left, top, original.right, bottom)
1638
1823
  if (anchorOffset != null) manager?.scrollToPositionWithOffset(first, anchorOffset)
1639
1824
  // React Native can stop this nested requestLayout at its React-owned parent.
1640
1825
  // Commit the changed inset before drawing instead of waiting for a data update.
1641
- if (recycler.width > 0 && recycler.height > 0 && !recycler.isComputingLayout) {
1826
+ if (recycler.width > 0 && recycler.height > 0 && !recycler.isLayoutSuppressed && !recycler.isComputingLayout) {
1642
1827
  recycler.forceLayout()
1643
1828
  recycler.measure(
1644
1829
  MeasureSpec.makeMeasureSpec(recycler.width, MeasureSpec.EXACTLY),
@@ -1647,9 +1832,8 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
1647
1832
  recycler.layout(recycler.left, recycler.top, recycler.right, recycler.bottom)
1648
1833
  }
1649
1834
  }
1650
- if (original.appliedTopInset != topInset) offsetDiag("recycler-inset-after-old=${original.appliedTopInset}-new=$topInset", recycler, pageKey(pageIndex), pageIndex)
1835
+ if (original.appliedTopInset != topInset) offsetDiag("recycler-inset-after-old=${original.appliedTopInset}-new=$topInset", recycler, key, pageIndex)
1651
1836
  original.appliedTopInset = topInset
1652
- val key = pageKey(pageIndex)
1653
1837
  val geometry = Triple(recycler.computeVerticalScrollRange(), recycler.computeVerticalScrollExtent(), recycler.childCount)
1654
1838
  deferredRecyclerGeometry[recycler]?.let { deferred ->
1655
1839
  if (geometry != deferred) {
@@ -2012,14 +2196,16 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
2012
2196
  }
2013
2197
  }
2014
2198
 
2015
- private fun headerRegionAt(y: Float): String? {
2199
+ private fun headerRegionAt(x: Float, y: Float): String? {
2016
2200
  val headerBottom = headerHeightPx - headerOffsetPx
2017
2201
  val stickyBottom = headerHeightPx + stickyHeaderHeightPx - headerOffsetPx
2018
2202
  return when {
2019
2203
  y < 0 || y >= stickyBottom -> null
2020
2204
  y < headerBottom -> "header"
2021
2205
  y < headerBottom + nativeTabBarView.measuredHeight &&
2022
- nativeTabBarView.visibility == View.VISIBLE -> "primary-tab"
2206
+ nativeTabBarView.visibility == View.VISIBLE ->
2207
+ if (x >= nativeTabBarView.left && x < nativeTabBarView.right) "primary-tab"
2208
+ else "sticky-controls"
2023
2209
  y < headerBottom + nativeTabBarView.measuredHeight + nativeSubHeaderView.measuredHeight &&
2024
2210
  nativeSubHeaderView.visibility == View.VISIBLE -> "secondary-header"
2025
2211
  else -> "sticky-controls"
@@ -2128,7 +2314,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
2128
2314
  pressCancelled = false
2129
2315
  nativeGestureStarted = false
2130
2316
  nestedNativeGestureStarted = false
2131
- headerTouchRegion = headerRegionAt(event.y) ?: "none"
2317
+ headerTouchRegion = headerRegionAt(event.x, event.y) ?: "none"
2132
2318
  headerTouchActive = headerTouchRegion != "none"
2133
2319
  if (!headerTouchActive) return super.dispatchTouchEvent(event)
2134
2320
  headerDownX = event.x
@@ -2351,6 +2537,9 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
2351
2537
  override fun onAttachedToWindow() {
2352
2538
  super.onAttachedToWindow()
2353
2539
  attachmentGeneration += 1
2540
+ toolbarSettledStamp = null
2541
+ toolbarSettlePostedGeneration = null
2542
+ postForCurrentAttachment { settleNativeTabProgressIfReady() }
2354
2543
  restoredRecyclerKeys.clear()
2355
2544
  restoredScrollerKeys.clear()
2356
2545
  viewTreeObserver.addOnPreDrawListener(pageContentLayoutListener)
@@ -2368,6 +2557,7 @@ class CollapsiblePagerHost(context: Context) : NestedScrollableHost(context), Ne
2368
2557
  }
2369
2558
 
2370
2559
  override fun onDetachedFromWindow() {
2560
+ suspendNativeToolbar()
2371
2561
  cancelHeaderSettledCheck()
2372
2562
  headerCommandOwner = null
2373
2563
  publishHeaderSettledState(false)