@onekeyfe/react-native-bottom-tabs 6.7.0-alpha.475 → 6.8.0-3

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 (27) hide show
  1. package/README.md +139 -48
  2. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsDrawerManager.kt +15 -0
  3. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsDrawerView.kt +276 -54
  4. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsView.kt +426 -53
  5. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsViewManager.kt +6 -5
  6. package/android/src/main/java/com/onekey/bottomtabs/FloatingTabBarStyle.kt +22 -6
  7. package/android/src/main/java/com/onekey/bottomtabs/events/TabDoublePressEvent.kt +7 -1
  8. package/docs/SPEC.md +212 -25
  9. package/ios/RCTBottomTabsComponentView.mm +4 -1
  10. package/ios/RCTBottomTabsContainerView.swift +263 -15
  11. package/ios/RCTBottomTabsDrawerComponentView.mm +24 -0
  12. package/ios/RCTBottomTabsDrawerContainerView.swift +314 -48
  13. package/ios/tests/BottomTabsDrawerReduceMotionTests.swift +231 -0
  14. package/lib/module/BottomTabs.js +85 -58
  15. package/lib/module/BottomTabsDrawerNativeComponent.ts +3 -0
  16. package/lib/module/BottomTabsNativeComponent.ts +6 -1
  17. package/lib/module/index.js +1 -0
  18. package/lib/typescript/src/BottomTabs.d.ts +23 -2
  19. package/lib/typescript/src/BottomTabsDrawerNativeComponent.d.ts +5 -0
  20. package/lib/typescript/src/BottomTabsNativeComponent.d.ts +5 -1
  21. package/lib/typescript/src/index.d.ts +1 -0
  22. package/package.json +1 -1
  23. package/react-native-bottom-tabs.podspec +6 -0
  24. package/src/BottomTabs.tsx +94 -44
  25. package/src/BottomTabsDrawerNativeComponent.ts +3 -0
  26. package/src/BottomTabsNativeComponent.ts +6 -1
  27. package/src/index.tsx +1 -0
@@ -9,6 +9,7 @@ import androidx.appcompat.view.ContextThemeWrapper
9
9
  import android.graphics.Color
10
10
  import android.graphics.Canvas
11
11
  import android.graphics.Outline
12
+ import android.graphics.Rect
12
13
  import android.graphics.drawable.BitmapDrawable
13
14
  import android.graphics.drawable.ColorDrawable
14
15
  import android.graphics.drawable.Drawable
@@ -31,12 +32,16 @@ import android.view.ViewOutlineProvider
31
32
  import android.view.animation.DecelerateInterpolator
32
33
  import android.widget.FrameLayout
33
34
  import android.widget.LinearLayout
35
+ import android.widget.ScrollView
34
36
  import android.widget.TextView
35
37
  import androidx.core.animation.doOnEnd
36
38
  import androidx.core.graphics.ColorUtils
37
39
  import androidx.core.view.ViewCompat
38
40
  import androidx.core.view.WindowInsetsCompat
41
+ import androidx.core.view.forEach
39
42
  import androidx.core.view.forEachIndexed
43
+ import androidx.core.widget.NestedScrollView
44
+ import androidx.recyclerview.widget.RecyclerView
40
45
  import coil3.ImageLoader
41
46
  import coil3.asDrawable
42
47
  import coil3.request.ImageRequest
@@ -157,12 +162,13 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
157
162
  private val floatingRoot = object : FrameLayout(context) {
158
163
  override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
159
164
  if (child === floatingBar && usesBottomBlur) {
165
+ val translationY = floatingBar.translationY.roundToInt()
160
166
  drawBlurBackdrop(
161
167
  canvas,
162
168
  floatingBar.left,
163
- floatingBar.top,
169
+ floatingBar.top + translationY,
164
170
  floatingBar.right,
165
- floatingBar.bottom,
171
+ floatingBar.bottom + translationY,
166
172
  floatingBar.height / 2f,
167
173
  ) { recording ->
168
174
  super.drawChild(recording, layoutHolder, drawingTime)
@@ -177,7 +183,7 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
177
183
  private val floatingIndicatorDrawable = GradientDrawable()
178
184
 
179
185
  var onTabSelectedListener: ((key: String) -> Unit)? = null
180
- var onTabDoublePressedListener: ((key: String) -> Unit)? = null
186
+ var onTabDoublePressedListener: ((key: String, didScrollToTop: Boolean) -> Unit)? = null
181
187
  var onNativeLayoutListener: ((width: Double, height: Double) -> Unit)? = null
182
188
  var onTabBarMeasuredListener: ((height: Int) -> Unit)? = null
183
189
  var disablePageAnimations = false
@@ -195,8 +201,10 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
195
201
  }
196
202
  var items: MutableList<TabInfo> = mutableListOf()
197
203
  private val iconSources: MutableMap<Int, ImageSource> = mutableMapOf()
204
+ private val selectedIconSources: MutableMap<Int, ImageSource> = mutableMapOf()
198
205
  private val drawableCache: MutableMap<ImageSource, Drawable> = mutableMapOf()
199
206
  private val iconRequestTokens: MutableMap<Int, Any> = mutableMapOf()
207
+ private val selectedIconRequestTokens: MutableMap<Int, Any> = mutableMapOf()
200
208
 
201
209
  private var isLayoutEnqueued = false
202
210
  private var selectedItem: String? = null
@@ -227,6 +235,8 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
227
235
  private var floatingStretch = 0f
228
236
  private var floatingLiftAnimator: ValueAnimator? = null
229
237
  private var floatingSlideAnimator: ValueAnimator? = null
238
+ private var floatingSlideTargetX = Float.NaN
239
+ private var floatingVisibilityCallback: Choreographer.FrameCallback? = null
230
240
  private var floatingTouchView: View? = null
231
241
  private var floatingTouchItemId = View.NO_ID
232
242
  private var floatingCandidateItemId = View.NO_ID
@@ -242,6 +252,10 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
242
252
  private val floatingBarLocationOnScreen = IntArray(2)
243
253
  private var lastPressedItemId = View.NO_ID
244
254
  private var lastPressedTime = 0L
255
+ // Looked up by name: the pager lives in a package this one does not depend on.
256
+ private val collapsiblePagerClass: Class<*>? by lazy {
257
+ runCatching { Class.forName("com.reactnativepagerview.CollapsiblePagerHost") }.getOrNull()
258
+ }
245
259
  private var defaultLabelTextSizes: Pair<Float, Float>? = null
246
260
  private var barTintColor: Int? = null
247
261
  private var activeIndicatorColor: ColorStateList? = null
@@ -279,7 +293,7 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
279
293
 
280
294
  private val usesBottomBlur: Boolean
281
295
  get() = androidBottomBlurEnabled && Build.VERSION.SDK_INT >= 31 &&
282
- isHardwareAccelerated && !isTabBarHidden
296
+ isHardwareAccelerated && (!isTabBarHidden || floatingVisibilityCallback != null)
283
297
 
284
298
  init {
285
299
  orientation = VERTICAL
@@ -333,7 +347,70 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
333
347
  (view.parent as? ViewGroup)?.removeView(view)
334
348
  }
335
349
 
350
+ private fun cancelFloatingVisibilityAnimation() {
351
+ val callback = floatingVisibilityCallback ?: return
352
+ floatingVisibilityCallback = null
353
+ Choreographer.getInstance().removeFrameCallback(callback)
354
+ }
355
+
356
+ private fun getFloatingBarSlideOffset(): Float {
357
+ val params = floatingBar.layoutParams as? FrameLayout.LayoutParams
358
+ val barHeightPx = floatingBar.height.takeIf { it > 0 } ?: dp(FloatingTabBarMetrics.BAR_HEIGHT)
359
+ val bottomMarginPx = params?.bottomMargin
360
+ ?: (maxOf(floatingBottomInset, dp(FloatingTabBarMetrics.BAR_MIN_BOTTOM_GAP)) +
361
+ dp(FloatingTabBarMetrics.BAR_BOTTOM_EXTRA))
362
+ return (barHeightPx + bottomMarginPx + dp(4f)).toFloat()
363
+ }
364
+
365
+ private fun animateFloatingVisibility(fromY: Float, toY: Float, hiding: Boolean) {
366
+ cancelFloatingVisibilityAnimation()
367
+ if (Build.VERSION.SDK_INT >= 26 && !ValueAnimator.areAnimatorsEnabled()) {
368
+ floatingBar.translationY = 0f
369
+ floatingBar.visibility = if (hiding) GONE else VISIBLE
370
+ updateBlurAppearance()
371
+ floatingRoot.invalidate()
372
+ return
373
+ }
374
+ val durationMs = 140f
375
+ val maxFrameStepMs = 24f
376
+ val interpolator = DecelerateInterpolator(if (hiding) 1.2f else 1.4f)
377
+ var elapsedMs = 0f
378
+ var lastFrameTimeNanos = -1L
379
+ val callback = object : Choreographer.FrameCallback {
380
+ override fun doFrame(frameTimeNanos: Long) {
381
+ if (floatingVisibilityCallback !== this) return
382
+ val deltaMs = if (lastFrameTimeNanos < 0L) {
383
+ 16f
384
+ } else {
385
+ ((frameTimeNanos - lastFrameTimeNanos) / 1_000_000f).coerceIn(1f, maxFrameStepMs)
386
+ }
387
+ lastFrameTimeNanos = frameTimeNanos
388
+ elapsedMs = (elapsedMs + deltaMs).coerceAtMost(durationMs)
389
+ val fraction = interpolator.getInterpolation((elapsedMs / durationMs).coerceIn(0f, 1f))
390
+ floatingBar.translationY = fromY + (toY - fromY) * fraction
391
+ floatingRoot.invalidate()
392
+ if (elapsedMs < durationMs) {
393
+ Choreographer.getInstance().postFrameCallback(this)
394
+ } else {
395
+ floatingVisibilityCallback = null
396
+ if (hiding && isTabBarHidden) {
397
+ floatingBar.translationY = 0f
398
+ floatingBar.visibility = GONE
399
+ updateBlurAppearance()
400
+ } else if (!hiding && !isTabBarHidden) {
401
+ floatingBar.translationY = 0f
402
+ }
403
+ floatingRoot.invalidate()
404
+ }
405
+ }
406
+ }
407
+ floatingVisibilityCallback = callback
408
+ Choreographer.getInstance().postFrameCallback(callback)
409
+ }
410
+
336
411
  private fun detachTabBarViews() {
412
+ cancelFloatingVisibilityAnimation()
413
+ floatingBar.translationY = 0f
337
414
  releaseBottomBlur()
338
415
  removeFromParent(layoutHolder)
339
416
  removeFromParent(bottomNavigation)
@@ -466,11 +543,16 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
466
543
  return
467
544
  }
468
545
  bottomNavigation.itemIconSize = size
469
- // Icons are rasterized at the icon size, so reload them for the new size.
546
+ // Icons are rasterized at the icon size: drop cached and in-flight requests,
547
+ // then reload each item's currently active source at the new size.
470
548
  drawableCache.clear()
471
- iconSources.forEach { (index, source) ->
549
+ iconRequestTokens.values.forEach { OneKeyNamedSVG.cancel(it) }
550
+ iconRequestTokens.clear()
551
+ selectedIconRequestTokens.values.forEach { OneKeyNamedSVG.cancel(it) }
552
+ selectedIconRequestTokens.clear()
553
+ items.indices.forEach { index ->
472
554
  bottomNavigation.menu.findItem(index)?.let { menuItem ->
473
- getDrawable(index, source) { menuItem.icon = it }
555
+ applyMenuItemIcon(index, menuItem)
474
556
  }
475
557
  }
476
558
  }
@@ -579,7 +661,20 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
579
661
  1f + (FloatingTabBarMetrics.LENS_SCALE_Y - 1f) * floatingLift - floatingStretch * 0.4f
580
662
  }
581
663
 
664
+ private val floatingLiftSettle = Runnable {
665
+ if (floatingTouchItemId == View.NO_ID) {
666
+ animateFloatingLift(0f)
667
+ }
668
+ }
669
+
670
+ private fun cancelFloatingSlideAnimator() {
671
+ floatingSlideAnimator?.cancel()
672
+ floatingSlideAnimator = null
673
+ floatingSlideTargetX = Float.NaN
674
+ }
675
+
582
676
  private fun animateFloatingLift(target: Float) {
677
+ floatingBar.removeCallbacks(floatingLiftSettle)
583
678
  floatingLiftAnimator?.cancel()
584
679
  val start = floatingLift
585
680
  if (start == target) {
@@ -588,8 +683,15 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
588
683
  }
589
684
  val lifting = target > start
590
685
  floatingLiftAnimator = ValueAnimator.ofFloat(start, target).apply {
591
- duration = if (lifting) 320L else 460L
592
- interpolator = SpringInterpolator(dampingRatio = if (lifting) 0.6f else 0.72f)
686
+ duration = if (lifting) {
687
+ FloatingTabBarMetrics.LIFT_UP_DURATION_MS
688
+ } else {
689
+ FloatingTabBarMetrics.LIFT_SETTLE_DURATION_MS
690
+ }
691
+ interpolator = SpringInterpolator(
692
+ dampingRatio = if (lifting) 0.72f else 0.8f,
693
+ angularFrequency = if (lifting) 9f else 7.8f,
694
+ )
593
695
  addUpdateListener {
594
696
  floatingLift = it.animatedValue as Float
595
697
  applyFloatingIndicatorState()
@@ -598,8 +700,40 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
598
700
  }
599
701
  }
600
702
 
601
- private fun animateFloatingIndicatorTo(targetX: Float, durationMs: Long = 440L) {
602
- floatingSlideAnimator?.cancel()
703
+ private fun scheduleFloatingLiftSettle(cancelled: Boolean) {
704
+ floatingBar.removeCallbacks(floatingLiftSettle)
705
+ if (cancelled) {
706
+ animateFloatingLift(0f)
707
+ return
708
+ }
709
+ val slideAnimator = floatingSlideAnimator
710
+ if (slideAnimator != null && slideAnimator.isRunning) {
711
+ val remainingFraction = (1f - slideAnimator.animatedFraction).coerceIn(0f, 1f)
712
+ val delayMs = (FloatingTabBarMetrics.LIFT_HOLD_DURING_SLIDE_MS * remainingFraction)
713
+ .roundToInt()
714
+ .toLong()
715
+ if (delayMs > 16L) {
716
+ floatingBar.postDelayed(floatingLiftSettle, delayMs)
717
+ return
718
+ }
719
+ }
720
+ animateFloatingLift(0f)
721
+ }
722
+
723
+ private fun animateFloatingIndicatorTo(
724
+ targetX: Float,
725
+ durationMs: Long = FloatingTabBarMetrics.SLIDE_DURATION_MS,
726
+ ) {
727
+ val activeAnimator = floatingSlideAnimator
728
+ if (
729
+ activeAnimator != null &&
730
+ activeAnimator.isRunning &&
731
+ !floatingSlideTargetX.isNaN() &&
732
+ abs(floatingSlideTargetX - targetX) < 0.5f
733
+ ) {
734
+ return
735
+ }
736
+ cancelFloatingSlideAnimator()
603
737
  val startX = floatingIndicator.x
604
738
  if (abs(targetX - startX) < 0.5f) {
605
739
  floatingIndicator.x = targetX
@@ -608,23 +742,39 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
608
742
  return
609
743
  }
610
744
  var previousX = startX
611
- floatingSlideAnimator = ValueAnimator.ofFloat(startX, targetX).apply {
745
+ var previousTimeMs = SystemClock.uptimeMillis()
746
+ floatingSlideTargetX = targetX
747
+ val animator = ValueAnimator.ofFloat(startX, targetX)
748
+ floatingSlideAnimator = animator
749
+ animator.apply {
612
750
  duration = durationMs
613
- interpolator = SpringInterpolator(dampingRatio = 0.74f)
751
+ interpolator = SpringInterpolator(
752
+ dampingRatio = FloatingTabBarMetrics.SLIDE_SPRING_DAMPING,
753
+ angularFrequency = FloatingTabBarMetrics.SLIDE_SPRING_FREQUENCY,
754
+ )
614
755
  addUpdateListener {
756
+ val nowMs = SystemClock.uptimeMillis()
757
+ val elapsedMs = (nowMs - previousTimeMs).coerceIn(4L, 32L).toFloat()
758
+ previousTimeMs = nowMs
615
759
  val x = it.animatedValue as Float
616
760
  val frameDelta = abs(x - previousX)
617
761
  previousX = x
618
762
  floatingIndicator.x = x
619
- // The capsule stretches along the travel direction, like a liquid drop.
620
- val rawStretch = (frameDelta / floatingIndicator.width.coerceAtLeast(1) * 1.6f)
763
+ // Normalize velocity to a 16ms reference step so 60Hz and 120Hz displays
764
+ // produce identical liquid stretch during the spring slide.
765
+ val normalizedStepDelta = frameDelta * (16f / elapsedMs)
766
+ val rawStretch = (normalizedStepDelta / floatingIndicator.width.coerceAtLeast(1) * 1.4f)
621
767
  .coerceAtMost(FloatingTabBarMetrics.LENS_MAX_STRETCH)
622
- floatingStretch = floatingStretch * 0.5f + rawStretch * 0.5f
768
+ floatingStretch = floatingStretch * 0.62f + rawStretch * 0.38f
623
769
  applyFloatingIndicatorState()
624
770
  }
625
771
  doOnEnd {
626
- floatingStretch = 0f
627
- applyFloatingIndicatorState()
772
+ if (floatingSlideAnimator === animator) {
773
+ floatingSlideAnimator = null
774
+ floatingSlideTargetX = Float.NaN
775
+ floatingStretch = 0f
776
+ applyFloatingIndicatorState()
777
+ }
628
778
  }
629
779
  start()
630
780
  }
@@ -653,14 +803,32 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
653
803
  val targetX = getFloatingItemX(selectedIndex) ?: return
654
804
  floatingIndicator.visibility = VISIBLE
655
805
 
656
- if (!animate || !floatingIndicatorPositioned) {
657
- floatingSlideAnimator?.cancel()
806
+ if (!floatingIndicatorPositioned) {
807
+ cancelFloatingSlideAnimator()
658
808
  floatingIndicator.x = targetX
659
809
  floatingStretch = 0f
660
810
  floatingIndicatorPositioned = true
661
811
  applyFloatingIndicatorState()
662
812
  return
663
813
  }
814
+ val activeAnimator = floatingSlideAnimator
815
+ if (
816
+ activeAnimator != null &&
817
+ activeAnimator.isRunning &&
818
+ !floatingSlideTargetX.isNaN() &&
819
+ abs(floatingSlideTargetX - targetX) < 0.5f
820
+ ) {
821
+ // Keep an in-flight slide to the same target running smoothly even when
822
+ // a late layout/item sync arrives with animate = false.
823
+ return
824
+ }
825
+ if (!animate) {
826
+ cancelFloatingSlideAnimator()
827
+ floatingIndicator.x = targetX
828
+ floatingStretch = 0f
829
+ applyFloatingIndicatorState()
830
+ return
831
+ }
664
832
  animateFloatingIndicatorTo(targetX)
665
833
  }
666
834
 
@@ -791,7 +959,7 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
791
959
  updateFloatingIndicatorWidth(itemView.width)
792
960
  floatingIndicator.visibility = VISIBLE
793
961
  if (floatingIndicatorPositioned) {
794
- animateFloatingIndicatorTo(targetX, durationMs = 320L)
962
+ animateFloatingIndicatorTo(targetX, durationMs = FloatingTabBarMetrics.SLIDE_DURATION_MS)
795
963
  } else {
796
964
  floatingIndicator.x = targetX
797
965
  floatingIndicatorPositioned = true
@@ -827,7 +995,7 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
827
995
  val targetStretch = (velocityDpPerMs * 0.06f).coerceAtMost(FloatingTabBarMetrics.LENS_MAX_STRETCH)
828
996
  floatingStretch = floatingStretch * 0.6f + targetStretch * 0.4f
829
997
 
830
- floatingSlideAnimator?.cancel()
998
+ cancelFloatingSlideAnimator()
831
999
  floatingIndicator.x = centerX - halfWidth
832
1000
  applyFloatingIndicatorState()
833
1001
  floatingBar.removeCallbacks(floatingStretchDecay)
@@ -864,7 +1032,6 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
864
1032
  val wasDragging = floatingDragging
865
1033
  clearFloatingTouchTracking()
866
1034
  setFloatingItemMagnified(View.NO_ID)
867
- animateFloatingLift(0f)
868
1035
 
869
1036
  if (!cancelled && requestedItemId != View.NO_ID) {
870
1037
  if (!wasDragging) {
@@ -878,6 +1045,7 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
878
1045
  // Settles on the selected tab, which is the old one for cancelled or
879
1046
  // preventsDefault presses.
880
1047
  updateFloatingIndicator(animate = true)
1048
+ scheduleFloatingLiftSettle(cancelled = cancelled || wasDragging)
881
1049
  }
882
1050
 
883
1051
  private fun clearFloatingTouchTracking() {
@@ -892,8 +1060,9 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
892
1060
 
893
1061
  private fun resetFloatingGesture() {
894
1062
  clearFloatingTouchTracking()
1063
+ floatingBar.removeCallbacks(floatingLiftSettle)
895
1064
  floatingLiftAnimator?.cancel()
896
- floatingSlideAnimator?.cancel()
1065
+ cancelFloatingSlideAnimator()
897
1066
  floatingLift = 0f
898
1067
  floatingStretch = 0f
899
1068
  items.indices.forEach { itemId ->
@@ -1042,6 +1211,11 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1042
1211
  }
1043
1212
 
1044
1213
  override fun onDetachedFromWindow() {
1214
+ cancelFloatingVisibilityAnimation()
1215
+ floatingBar.translationY = 0f
1216
+ if (androidTabBarStyle == AndroidTabBarStyle.FLOATING) {
1217
+ floatingBar.visibility = if (isTabBarHidden) GONE else VISIBLE
1218
+ }
1045
1219
  releaseBottomBlur()
1046
1220
  super.onDetachedFromWindow()
1047
1221
  }
@@ -1109,7 +1283,13 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1109
1283
  private fun setSelectedIndex(itemId: Int) {
1110
1284
  val selectionChanged = bottomNavigation.selectedItemId != itemId
1111
1285
  bottomNavigation.selectedItemId = itemId
1112
- if (androidTabBarStyle == AndroidTabBarStyle.FLOATING) {
1286
+ if (selectionChanged) {
1287
+ updateMenuItemIconsForSelection()
1288
+ }
1289
+ if (
1290
+ androidTabBarStyle == AndroidTabBarStyle.FLOATING &&
1291
+ (selectionChanged || !floatingIndicatorPositioned)
1292
+ ) {
1113
1293
  post { updateFloatingIndicator(animate = selectionChanged) }
1114
1294
  }
1115
1295
  if (disablePageAnimations) {
@@ -1177,13 +1357,90 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1177
1357
  now - lastPressedTime <= ViewConfiguration.getDoubleTapTimeout()
1178
1358
  ) {
1179
1359
  lastPressedItemId = View.NO_ID
1180
- onTabDoublePressedListener?.invoke(key)
1360
+ val listener = onTabDoublePressedListener ?: return
1361
+ val didScrollToTop = scrollSelectedTabToTop(key) ?: return
1362
+ listener(key, didScrollToTop)
1181
1363
  return
1182
1364
  }
1183
1365
  lastPressedItemId = itemId
1184
1366
  lastPressedTime = now
1185
1367
  }
1186
1368
 
1369
+ /**
1370
+ * Animates every visible vertical scroller of the selected tab back to its
1371
+ * top. Returns false when all of them were already there, which tells JS the
1372
+ * double press may leave the page instead. A rejected pager request with no
1373
+ * accepted scroll returns null instead of implying that the page is at top.
1374
+ */
1375
+ private fun scrollSelectedTabToTop(pressedKey: String): Boolean? {
1376
+ if (pressedKey != selectedItem) return false
1377
+ val scrollers = mutableListOf<View>()
1378
+ val pagers = mutableListOf<View>()
1379
+ collectScrolledViews(layoutHolder, scrollers, pagers)
1380
+ scrollers.forEach { view ->
1381
+ when (view) {
1382
+ is RecyclerView -> view.smoothScrollToPosition(0)
1383
+ is NestedScrollView -> view.smoothScrollTo(view.scrollX, 0)
1384
+ is ScrollView -> view.smoothScrollTo(view.scrollX, 0)
1385
+ }
1386
+ }
1387
+ var didScrollToTop = scrollers.isNotEmpty()
1388
+ var rejectedPagerRequest = false
1389
+ pagers.forEach { pager ->
1390
+ if (scrollPagerToTop(pager)) didScrollToTop = true
1391
+ else rejectedPagerRequest = true
1392
+ }
1393
+ return when {
1394
+ didScrollToTop -> true
1395
+ rejectedPagerRequest -> null
1396
+ else -> false
1397
+ }
1398
+ }
1399
+
1400
+ private fun collectScrolledViews(
1401
+ view: View,
1402
+ scrollers: MutableList<View>,
1403
+ pagers: MutableList<View>,
1404
+ ) {
1405
+ // Hidden tabs and pager siblings stay attached without being on screen.
1406
+ if (!view.isShown || !view.getGlobalVisibleRect(Rect())) return
1407
+ if (collapsiblePagerClass?.isInstance(view) == true) {
1408
+ // The pager keeps header state for its pages, so moving one of its
1409
+ // scrollers directly leaves the header out of sync. It scrolls itself.
1410
+ val pagerScrollers = mutableListOf<View>()
1411
+ val nestedPagers = mutableListOf<View>()
1412
+ (view as ViewGroup).forEach { child ->
1413
+ collectScrolledViews(child, pagerScrollers, nestedPagers)
1414
+ }
1415
+ if (pagerScrollers.isNotEmpty() || nestedPagers.isNotEmpty() || isPagerAwayFromTop(view)) {
1416
+ pagers.add(view)
1417
+ }
1418
+ return
1419
+ }
1420
+ if (
1421
+ (view is RecyclerView || view is NestedScrollView || view is ScrollView) &&
1422
+ view.canScrollVertically(-1)
1423
+ ) {
1424
+ scrollers.add(view)
1425
+ }
1426
+ if (view is ViewGroup) {
1427
+ view.forEach { child -> collectScrolledViews(child, scrollers, pagers) }
1428
+ }
1429
+ }
1430
+
1431
+ private fun isPagerAwayFromTop(pager: View): Boolean =
1432
+ runCatching { pager.javaClass.getMethod("isAwayFromPageTop").invoke(pager) as Boolean }
1433
+ .onFailure { Log.w("BottomTabs", "Unable to read the collapsible pager top state", it) }
1434
+ .getOrDefault(false)
1435
+
1436
+ private fun scrollPagerToTop(pager: View): Boolean =
1437
+ runCatching {
1438
+ pager.javaClass
1439
+ .getMethod("tryScrollToTop", Int::class.javaPrimitiveType, Boolean::class.javaPrimitiveType)
1440
+ .invoke(pager, 0, true) as Boolean
1441
+ }.onFailure { Log.w("BottomTabs", "Unable to scroll the collapsible pager to top", it) }
1442
+ .getOrDefault(false)
1443
+
1187
1444
  fun setIgnoreBottomInsets(ignore: Boolean) {
1188
1445
  ignoreBottomInsets = ignore
1189
1446
  bottomNavigation.setIgnoreBottomInsets(
@@ -1224,23 +1481,61 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1224
1481
  }
1225
1482
 
1226
1483
  fun setTabBarHidden(isHidden: Boolean) {
1484
+ if (isTabBarHidden == isHidden) {
1485
+ return
1486
+ }
1227
1487
  isTabBarHidden = isHidden
1228
1488
  if (isHidden) {
1229
1489
  resetFloatingGesture()
1230
1490
  }
1231
- if (androidTabBarStyle == AndroidTabBarStyle.FLOATING) {
1232
- floatingBar.visibility = if (isHidden) GONE else VISIBLE
1233
- bottomNavigation.visibility = VISIBLE
1234
- } else {
1235
- bottomNavigation.visibility = if (isHidden) GONE else VISIBLE
1491
+ val shouldAnimateFloating =
1492
+ androidTabBarStyle == AndroidTabBarStyle.FLOATING &&
1493
+ isAttachedToWindow &&
1494
+ width > 0 &&
1495
+ height > 0
1496
+
1497
+ cancelFloatingVisibilityAnimation()
1498
+
1499
+ if (!shouldAnimateFloating) {
1500
+ floatingBar.translationY = 0f
1501
+ if (androidTabBarStyle == AndroidTabBarStyle.FLOATING) {
1502
+ floatingBar.visibility = if (isHidden) GONE else VISIBLE
1503
+ bottomNavigation.visibility = VISIBLE
1504
+ } else {
1505
+ bottomNavigation.visibility = if (isHidden) GONE else VISIBLE
1506
+ }
1507
+ updateBlurAppearance()
1508
+ requestLayout()
1509
+ floatingRoot.invalidate()
1510
+ invalidate()
1511
+ if (!isHidden) post { updateFloatingIndicator(animate = false) }
1512
+ return
1513
+ }
1514
+
1515
+ val slideOffset = getFloatingBarSlideOffset()
1516
+ bottomNavigation.visibility = VISIBLE
1517
+ if (isHidden) {
1518
+ val startY = if (floatingBar.visibility == VISIBLE) floatingBar.translationY else slideOffset
1519
+ floatingBar.visibility = VISIBLE
1520
+ floatingBar.translationY = startY
1521
+ animateFloatingVisibility(startY, slideOffset, hiding = true)
1522
+ updateBlurAppearance()
1523
+ requestLayout()
1524
+ floatingRoot.invalidate()
1525
+ invalidate()
1526
+ return
1236
1527
  }
1237
1528
 
1529
+ val wasGone = floatingBar.visibility != VISIBLE
1530
+ val startY = if (wasGone) slideOffset else floatingBar.translationY
1531
+ floatingBar.visibility = VISIBLE
1532
+ floatingBar.translationY = startY
1533
+ animateFloatingVisibility(startY, 0f, hiding = false)
1238
1534
  updateBlurAppearance()
1239
1535
  requestLayout()
1240
1536
  floatingRoot.invalidate()
1241
1537
  invalidate()
1242
- if (!isHidden) post { updateFloatingIndicator(animate = false) }
1243
-
1538
+ post { updateFloatingIndicator(animate = false) }
1244
1539
  }
1245
1540
 
1246
1541
  fun updateItems(items: MutableList<TabInfo>) {
@@ -1256,11 +1551,7 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1256
1551
  }
1257
1552
 
1258
1553
  menuItem.isVisible = !item.hidden
1259
- if (iconSources.containsKey(index)) {
1260
- getDrawable(index, iconSources[index]!!) {
1261
- menuItem.icon = it
1262
- }
1263
- }
1554
+ applyMenuItemIcon(index, menuItem)
1264
1555
 
1265
1556
  if (item.badge?.isNotEmpty() == true) {
1266
1557
  val badge = bottomNavigation.getOrCreateBadge(index)
@@ -1280,7 +1571,9 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1280
1571
  val itemView = bottomNavigation.findViewById<View>(menuItem.itemId)
1281
1572
  itemView?.let { view ->
1282
1573
  view.setOnClickListener {
1283
- onTabSelected(menuItem)
1574
+ if (!isTabBarHidden) {
1575
+ onTabSelected(menuItem)
1576
+ }
1284
1577
  }
1285
1578
  view.setOnTouchListener { touchedView, event ->
1286
1579
  handleFloatingTabTouch(touchedView, menuItem, event)
@@ -1310,6 +1603,49 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1310
1603
  return bottomNavigation.menu.findItem(index) ?: bottomNavigation.menu.add(0, index, 0, title)
1311
1604
  }
1312
1605
 
1606
+ private fun isItemSelected(index: Int): Boolean {
1607
+ val selectedKey = selectedItem
1608
+ return if (selectedKey != null) {
1609
+ items.getOrNull(index)?.key == selectedKey
1610
+ } else {
1611
+ bottomNavigation.selectedItemId == index
1612
+ }
1613
+ }
1614
+
1615
+ private fun activeIconSourceForIndex(index: Int): Pair<ImageSource, Boolean>? {
1616
+ val selected = isItemSelected(index)
1617
+ if (selected) {
1618
+ selectedIconSources[index]?.let { return it to true }
1619
+ }
1620
+ iconSources[index]?.let { return it to false }
1621
+ selectedIconSources[index]?.let { return it to true }
1622
+ return null
1623
+ }
1624
+
1625
+ private fun applyMenuItemIcon(index: Int, menuItem: MenuItem) {
1626
+ val (source, isSelectedSource) = activeIconSourceForIndex(index) ?: return
1627
+ drawableCache[source]?.let { cached ->
1628
+ if (menuItem.icon !== cached) {
1629
+ menuItem.icon = cached
1630
+ }
1631
+ return
1632
+ }
1633
+ loadDrawable(index, source, isSelectedSource) { drawable ->
1634
+ val currentSource = activeIconSourceForIndex(index)?.first
1635
+ if (currentSource == source && drawable != null) {
1636
+ menuItem.icon = drawable
1637
+ }
1638
+ }
1639
+ }
1640
+
1641
+ private fun updateMenuItemIconsForSelection() {
1642
+ items.indices.forEach { index ->
1643
+ bottomNavigation.menu.findItem(index)?.let { menuItem ->
1644
+ applyMenuItemIcon(index, menuItem)
1645
+ }
1646
+ }
1647
+ }
1648
+
1313
1649
  fun setIcons(icons: ReadableArray?) {
1314
1650
  if (icons == null || icons.size() == 0) {
1315
1651
  return
@@ -1325,10 +1661,38 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1325
1661
  val imageSource = ImageSource(context, uri)
1326
1662
  this.iconSources[idx] = imageSource
1327
1663
 
1328
- // Update existing item if exists.
1329
- bottomNavigation.menu.findItem(idx)?.let { menuItem ->
1330
- getDrawable(idx, imageSource) {
1331
- menuItem.icon = it
1664
+ loadDrawable(idx, imageSource, isSelectedIcon = false) {
1665
+ bottomNavigation.menu.findItem(idx)?.let { menuItem ->
1666
+ applyMenuItemIcon(idx, menuItem)
1667
+ }
1668
+ }
1669
+ }
1670
+ }
1671
+
1672
+ fun setSelectedIcons(icons: ReadableArray?) {
1673
+ if (icons == null || icons.size() == 0) {
1674
+ selectedIconRequestTokens.values.forEach { OneKeyNamedSVG.cancel(it) }
1675
+ selectedIconRequestTokens.clear()
1676
+ selectedIconSources.clear()
1677
+ updateMenuItemIconsForSelection()
1678
+ return
1679
+ }
1680
+
1681
+ for (idx in 0 until icons.size()) {
1682
+ val source = icons.getMap(idx)
1683
+ val uri = source?.getString("uri")
1684
+ if (uri.isNullOrEmpty()) {
1685
+ selectedIconSources.remove(idx)
1686
+ continue
1687
+ }
1688
+
1689
+ val imageSource = ImageSource(context, uri)
1690
+ this.selectedIconSources[idx] = imageSource
1691
+
1692
+ // Pre-warm the selected icon into drawableCache so tab switches swap synchronously.
1693
+ loadDrawable(idx, imageSource, isSelectedIcon = true) {
1694
+ bottomNavigation.menu.findItem(idx)?.let { menuItem ->
1695
+ applyMenuItemIcon(idx, menuItem)
1332
1696
  }
1333
1697
  }
1334
1698
  }
@@ -1359,8 +1723,15 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1359
1723
  }
1360
1724
 
1361
1725
  @SuppressLint("CheckResult")
1362
- private fun getDrawable(index: Int, imageSource: ImageSource, onDrawableReady: (Drawable?) -> Unit) {
1363
- iconRequestTokens.remove(index)?.let { OneKeyNamedSVG.cancel(it) }
1726
+ private fun loadDrawable(
1727
+ index: Int,
1728
+ imageSource: ImageSource,
1729
+ isSelectedIcon: Boolean,
1730
+ onDrawableReady: (Drawable?) -> Unit,
1731
+ ) {
1732
+ val tokenMap = if (isSelectedIcon) selectedIconRequestTokens else iconRequestTokens
1733
+ val sourceMap = if (isSelectedIcon) selectedIconSources else iconSources
1734
+ tokenMap.remove(index)?.let { OneKeyNamedSVG.cancel(it) }
1364
1735
  drawableCache[imageSource]?.let {
1365
1736
  onDrawableReady(it)
1366
1737
  return
@@ -1373,7 +1744,7 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1373
1744
  return
1374
1745
  }
1375
1746
  val token = Any()
1376
- iconRequestTokens[index] = token
1747
+ tokenMap[index] = token
1377
1748
  val iconSizePx = bottomNavigation.itemIconSize.coerceAtLeast(1)
1378
1749
  val density = resources.displayMetrics.density
1379
1750
  OneKeyNamedSVG.request(
@@ -1385,24 +1756,24 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1385
1756
  height = iconSizePx,
1386
1757
  scale = density,
1387
1758
  ) { bitmap ->
1388
- if (iconRequestTokens[index] !== token) {
1759
+ if (tokenMap[index] !== token) {
1389
1760
  return@request
1390
1761
  }
1391
- iconRequestTokens.remove(index)
1762
+ tokenMap.remove(index)
1392
1763
  if (bitmap == null) {
1393
1764
  onDrawableReady(null)
1394
1765
  return@request
1395
1766
  }
1396
1767
  val stateDrawable = BitmapDrawable(context.resources, bitmap)
1397
1768
  drawableCache[imageSource] = stateDrawable
1398
- if (iconSources[index] == imageSource) {
1769
+ if (sourceMap[index] == imageSource) {
1399
1770
  onDrawableReady(stateDrawable)
1400
1771
  }
1401
1772
  }
1402
1773
  return
1403
1774
  }
1404
1775
  val token = Any()
1405
- iconRequestTokens[index] = token
1776
+ tokenMap[index] = token
1406
1777
  val iconSizePx = bottomNavigation.itemIconSize
1407
1778
  val request = ImageRequest.Builder(context)
1408
1779
  .data(imageSource.getUri(context))
@@ -1411,13 +1782,13 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1411
1782
  .precision(Precision.EXACT)
1412
1783
  .target { drawable ->
1413
1784
  post {
1414
- if (iconRequestTokens[index] !== token) {
1785
+ if (tokenMap[index] !== token) {
1415
1786
  return@post
1416
1787
  }
1417
- iconRequestTokens.remove(index)
1788
+ tokenMap.remove(index)
1418
1789
  val stateDrawable = drawable.asDrawable(context.resources)
1419
1790
  drawableCache[imageSource] = stateDrawable
1420
- if (iconSources[index] == imageSource) {
1791
+ if (sourceMap[index] == imageSource) {
1421
1792
  onDrawableReady(stateDrawable)
1422
1793
  }
1423
1794
  }
@@ -1491,6 +1862,8 @@ class BottomTabsView(context: Context) : LinearLayout(context) {
1491
1862
  fun onDropViewInstance() {
1492
1863
  iconRequestTokens.values.forEach { OneKeyNamedSVG.cancel(it) }
1493
1864
  iconRequestTokens.clear()
1865
+ selectedIconRequestTokens.values.forEach { OneKeyNamedSVG.cancel(it) }
1866
+ selectedIconRequestTokens.clear()
1494
1867
  androidBottomBlurEnabled = false
1495
1868
  updateBlurAppearance()
1496
1869
  releaseBottomBlur()