@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
@@ -1,16 +1,22 @@
1
1
  package com.onekey.bottomtabs
2
2
 
3
- import android.animation.Animator
4
- import android.animation.AnimatorListenerAdapter
3
+ import android.animation.TimeAnimator
5
4
  import android.animation.ValueAnimator
6
5
  import android.app.Activity
7
6
  import android.content.Context
8
7
  import android.content.ContextWrapper
8
+ import android.content.res.Configuration
9
9
  import android.graphics.Canvas
10
10
  import android.graphics.Color
11
+ import android.graphics.LinearGradient
11
12
  import android.graphics.Matrix
13
+ import android.graphics.Paint
12
14
  import android.graphics.Path
15
+ import android.graphics.RadialGradient
13
16
  import android.graphics.RectF
17
+ import android.graphics.Shader
18
+ import android.os.Build
19
+ import android.provider.Settings
14
20
  import android.view.HapticFeedbackConstants
15
21
  import android.view.MotionEvent
16
22
  import android.view.VelocityTracker
@@ -19,7 +25,6 @@ import android.view.ViewConfiguration
19
25
  import android.view.ViewGroup
20
26
  import android.view.SurfaceView
21
27
  import android.view.TextureView
22
- import android.view.animation.LinearInterpolator
23
28
  import android.widget.FrameLayout
24
29
  import android.widget.AbsSeekBar
25
30
  import android.widget.EditText
@@ -35,18 +40,38 @@ import com.facebook.react.uimanager.PointerEvents
35
40
  import com.facebook.react.uimanager.ReactPointerEventsView
36
41
  import com.facebook.react.uimanager.events.NativeGestureUtil
37
42
  import kotlin.math.abs
43
+ import kotlin.math.cos
38
44
  import kotlin.math.exp
45
+ import kotlin.math.max
46
+ import kotlin.math.min
39
47
  import kotlin.math.roundToInt
48
+ import kotlin.math.sin
49
+ import kotlin.math.sqrt
40
50
  import java.lang.ref.WeakReference
41
51
 
52
+ // Shared with iOS: measured frame by frame from the reference recording.
53
+ private const val MENU_WIDTH_FRACTION = .8f
54
+ private const val PAGE_CORNER_RADIUS_DP = 62f
55
+ private const val MENU_MIN_SCALE = .97f
56
+ private const val SPRING_FREQUENCY = 25.5f
57
+ private const val SPRING_DAMPING = .84f
58
+ private const val PAGE_SHADOW_SIGMA_DP = 18f
59
+ private val PAGE_HAIRLINE_COLOR = Color.argb(43, 255, 255, 255)
60
+
42
61
  private class DrawerContent(context: Context) : FrameLayout(context), ReactPointerEventsView {
43
62
  override var pointerEvents = PointerEvents.AUTO
44
63
  }
45
64
 
46
65
  private class DrawerForeground(context: Context) : FrameLayout(context) {
47
66
  var cornerRadius = 0f
67
+ var hairlineColor = Color.TRANSPARENT
48
68
  private val path = Path()
49
69
  private val rect = RectF()
70
+ // Centered on the clip edge, so one physical pixel shows inside, like a layer border.
71
+ private val hairline = Paint(Paint.ANTI_ALIAS_FLAG).apply {
72
+ style = Paint.Style.STROKE
73
+ strokeWidth = 2f
74
+ }
50
75
  override fun dispatchDraw(canvas: Canvas) {
51
76
  val save = canvas.save()
52
77
  rect.set(0f, 0f, width.toFloat(), height.toFloat())
@@ -55,6 +80,10 @@ private class DrawerForeground(context: Context) : FrameLayout(context) {
55
80
  path.addRoundRect(rect, floatArrayOf(r, r, 0f, 0f, 0f, 0f, r, r), Path.Direction.CW)
56
81
  canvas.clipPath(path)
57
82
  super.dispatchDraw(canvas)
83
+ if (Color.alpha(hairlineColor) > 0) {
84
+ hairline.color = hairlineColor
85
+ canvas.drawPath(path, hairline)
86
+ }
58
87
  canvas.restoreToCount(save)
59
88
  }
60
89
  }
@@ -62,22 +91,60 @@ private class DrawerForeground(context: Context) : FrameLayout(context) {
62
91
  /** React owns two stable slots; translation, input and settling stay on the UI thread. */
63
92
  class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
64
93
  private val menu = DrawerContent(context)
65
- private val menuShade = View(context)
66
94
  private val foreground = DrawerForeground(context)
67
95
  private val content = DrawerContent(context)
68
96
  private val dim = View(context)
69
97
  private val slots = mutableListOf<View>()
70
98
  var onDrawerOpenChange: ((Boolean) -> Unit)? = null
99
+ var onDrawerProgress: ((Float) -> Unit)? = null
71
100
  var swipeEnabled = false
72
101
  var drawerHapticFeedbackEnabled = false
73
102
  var bottomBarHeight = 0f
103
+ /** "light" or "dark" forces that palette; anything else follows the configuration's night mode. */
104
+ var colorScheme = "system"
105
+ set(value) {
106
+ if (field == value) return
107
+ field = value
108
+ applyAppearance()
109
+ setProgress(appliedValue, appliedOvershoot)
110
+ }
111
+ var progressEventsEnabled = false
112
+ set(value) {
113
+ if (field == value) return
114
+ field = value
115
+ // A new subscriber starts from the current position.
116
+ reportedProgress = -1f
117
+ if (value) reportProgress()
118
+ }
74
119
  private var drawerEnabled = false
75
120
  private var requestedOpen = false
76
121
  private var stableOpen = false
77
122
  private var progress = 0f
78
- private var animator: ValueAnimator? = null
123
+ // The last requested position, kept so a layout pass mid-settle does not
124
+ // clip the spring's overshoot back to fully open.
125
+ private var appliedValue = 0f
126
+ private var appliedOvershoot = false
127
+ private var reportedProgress = -1f
128
+ private var animator: TimeAnimator? = null
79
129
  private var animationTarget = false
80
130
  private var animationUser = false
131
+ private var animationFrom = 0f
132
+ private var animationVelocity = 0f
133
+ private var animationDuration = 0f
134
+ // Running speed of the current animation, for handing off to a retarget.
135
+ private var trackedValue = 0f
136
+ private var trackedTime = 0f
137
+ private var trackedVelocity = 0f
138
+ private var dark = false
139
+ private var scrimMaxAlpha = .58f
140
+ private var pageShadowOpacity = 0f
141
+ private val shadowPaint = Paint()
142
+ private var shadowEdge: Shader? = null
143
+ private var shadowTopCorner: Shader? = null
144
+ private var shadowBottomCorner: Shader? = null
145
+ private var shadowRadius = -1f
146
+ private var shadowHeight = -1
147
+ private var shadowOpacity = -1f
81
148
  private var downX = 0f
82
149
  private var downY = 0f
83
150
  private var downProgress = 0f
@@ -97,25 +164,41 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
97
164
  private val backCallback = object : OnBackPressedCallback(false) {
98
165
  override fun handleOnBackPressed() { closeFromUser() }
99
166
  }
167
+ private val menuWidth get() = width * MENU_WIDTH_FRACTION
100
168
 
101
169
  init {
102
170
  clipChildren = true
103
- menuShade.setBackgroundColor(Color.BLACK)
104
- menuShade.isEnabled = false
105
- menuShade.importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
106
- dim.setBackgroundColor(Color.rgb(28, 28, 28))
107
171
  dim.contentDescription = "Close menu"
108
172
  dim.importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_YES
109
173
  dim.setOnClickListener { closeFromUser() }
110
- menu.addView(menuShade, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
111
174
  foreground.addView(content, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
112
175
  foreground.addView(dim, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
113
176
  super.addView(menu, LayoutParams(0, LayoutParams.MATCH_PARENT))
114
177
  super.addView(foreground, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
115
178
  initialized = true
179
+ applyAppearance()
116
180
  setProgress(0f)
117
181
  }
118
182
 
183
+ private fun applyAppearance() {
184
+ dark = when (colorScheme) {
185
+ "dark" -> true
186
+ "light" -> false
187
+ else -> (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
188
+ }
189
+ // The page washes toward white in light mode and toward gray in dark mode.
190
+ dim.setBackgroundColor(if (dark) Color.rgb(36, 36, 36) else Color.WHITE)
191
+ scrimMaxAlpha = if (dark) .58f else .5f
192
+ pageShadowOpacity = if (dark) 0f else .16f
193
+ }
194
+
195
+ override fun onConfigurationChanged(newConfig: Configuration?) {
196
+ super.onConfigurationChanged(newConfig)
197
+ if (colorScheme == "light" || colorScheme == "dark") return
198
+ applyAppearance()
199
+ setProgress(appliedValue, appliedOvershoot)
200
+ }
201
+
119
202
  fun setDrawerEnabled(value: Boolean) {
120
203
  if (drawerEnabled == value) return
121
204
  cancelTouch()
@@ -160,18 +243,18 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
160
243
 
161
244
  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
162
245
  super.onMeasure(widthMeasureSpec, heightMeasureSpec)
163
- menu.measure(MeasureSpec.makeMeasureSpec((measuredWidth * .78f).roundToInt(), MeasureSpec.EXACTLY),
246
+ menu.measure(MeasureSpec.makeMeasureSpec((measuredWidth * MENU_WIDTH_FRACTION).roundToInt(), MeasureSpec.EXACTLY),
164
247
  MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY))
165
248
  }
166
249
 
167
250
  override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
168
251
  layoutSlots()
169
- setProgress(progress)
252
+ setProgress(appliedValue, appliedOvershoot)
170
253
  }
171
254
 
172
255
  private fun layoutSlots() {
173
256
  val exactHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
174
- val menuWidth = (width * .78f).roundToInt()
257
+ val menuWidth = (width * MENU_WIDTH_FRACTION).roundToInt()
175
258
  menu.measure(MeasureSpec.makeMeasureSpec(menuWidth, MeasureSpec.EXACTLY), exactHeight)
176
259
  foreground.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), exactHeight)
177
260
  menu.layout(0, 0, menuWidth, height)
@@ -205,7 +288,7 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
205
288
  val systemLeft = ViewCompat.getRootWindowInsets(this)
206
289
  ?.getInsets(WindowInsetsCompat.Type.systemGestures())?.left ?: 0
207
290
  eligible = drawerEnabled && event.y < height - bottomBarHeight * density &&
208
- (if (progress > 0f) event.x >= width * .78f * progress
291
+ (if (progress > 0f) event.x >= menuWidth * progress
209
292
  else swipeEnabled && event.x >= systemLeft && !touchPathHasExclusiveControl())
210
293
  if (eligible) tracker = VelocityTracker.obtain().also { it.addMovement(event) }
211
294
  }
@@ -328,14 +411,14 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
328
411
  rememberNativeEvent(event)
329
412
  tracker?.addMovement(event)
330
413
  when (event.actionMasked) {
331
- MotionEvent.ACTION_MOVE -> setProgress(downProgress + (event.x - downX) / maxOf(1f, width * .78f))
414
+ MotionEvent.ACTION_MOVE -> setProgress(downProgress + (event.x - downX) / maxOf(1f, menuWidth))
332
415
  MotionEvent.ACTION_UP -> {
333
- setProgress(downProgress + (event.x - downX) / maxOf(1f, width * .78f))
416
+ setProgress(downProgress + (event.x - downX) / maxOf(1f, menuWidth))
334
417
  tracker?.computeCurrentVelocity(1000)
335
- val velocity = ((tracker?.xVelocity ?: 0f) / maxOf(1f, width * .78f)).coerceIn(-8f, 8f)
418
+ val velocity = ((tracker?.xVelocity ?: 0f) / maxOf(1f, menuWidth)).coerceIn(-12f, 12f)
336
419
  val target = progress + velocity * .12f >= .5f
337
420
  cancelTouch()
338
- settle(target, velocity, true)
421
+ settle(target, velocity, true, tween = false)
339
422
  }
340
423
  MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_POINTER_DOWN -> {
341
424
  cancelTouch()
@@ -369,61 +452,201 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
369
452
  settle(false, 0f, true)
370
453
  }
371
454
 
372
- private fun setProgress(value: Float) {
373
- progress = (if (drawerEnabled) value else 0f).coerceIn(0f, 1f)
374
- foreground.translationX = width * .78f * progress
375
- foreground.cornerRadius = 64f * density * progress
455
+ private fun setProgress(value: Float, overshoot: Boolean = false) {
456
+ appliedValue = value
457
+ appliedOvershoot = overshoot
458
+ val raw = if (drawerEnabled) value else 0f
459
+ progress = raw.coerceIn(0f, 1f)
460
+ val visible = progress > 0f
461
+ // Only the settle spring may carry the page a few pixels past open.
462
+ val travel = if (overshoot) raw.coerceIn(0f, 1.03f) else progress
463
+ foreground.translationX = menuWidth * travel
464
+ // Rounds in over the first 10% so screens with other corner radii never pop.
465
+ foreground.cornerRadius = PAGE_CORNER_RADIUS_DP * density * min(1f, progress / .1f)
466
+ foreground.hairlineColor = if (visible && dark) PAGE_HAIRLINE_COLOR else Color.TRANSPARENT
376
467
  foreground.invalidate()
377
- dim.alpha = .7f * progress
378
- dim.visibility = if (progress > 0f) VISIBLE else INVISIBLE
379
- dim.isEnabled = progress > 0f
380
- content.pointerEvents = if (progress > 0f) PointerEvents.NONE else PointerEvents.AUTO
381
- content.importantForAccessibility = if (progress > 0f) IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
468
+ // The page shadow is drawn by this host, beneath the foreground.
469
+ invalidate()
470
+ dim.alpha = scrimMaxAlpha * progress
471
+ dim.visibility = if (visible) VISIBLE else INVISIBLE
472
+ dim.isEnabled = visible
473
+ content.pointerEvents = if (visible) PointerEvents.NONE else PointerEvents.AUTO
474
+ content.importantForAccessibility = if (visible) IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
382
475
  else IMPORTANT_FOR_ACCESSIBILITY_AUTO
383
- menu.translationX = -.12f * width * .78f * (1f - progress)
384
- menuShade.alpha = .12f * (1f - progress)
385
- menu.visibility = if (progress > 0f) VISIBLE else INVISIBLE
386
- menu.pointerEvents = if (progress > 0f) PointerEvents.AUTO else PointerEvents.NONE
387
- menu.importantForAccessibility = if (progress > 0f) IMPORTANT_FOR_ACCESSIBILITY_AUTO
476
+ // The menu grows from its left-center edge and fades in over whatever is
477
+ // behind the host, fully visible by 60% open.
478
+ val minScale = if (animationsEnabled()) MENU_MIN_SCALE else 1f
479
+ val scale = minScale + (1f - minScale) * progress
480
+ menu.pivotX = 0f
481
+ menu.pivotY = menu.height / 2f
482
+ menu.scaleX = scale
483
+ menu.scaleY = scale
484
+ menu.alpha = min(1f, progress / .6f)
485
+ // A partially transparent menu composites from one cached layer per frame.
486
+ val layer = if (menu.alpha > 0f && menu.alpha < 1f) LAYER_TYPE_HARDWARE else LAYER_TYPE_NONE
487
+ if (menu.layerType != layer) menu.setLayerType(layer, null)
488
+ menu.visibility = if (visible) VISIBLE else INVISIBLE
489
+ menu.pointerEvents = if (visible) PointerEvents.AUTO else PointerEvents.NONE
490
+ menu.importantForAccessibility = if (visible) IMPORTANT_FOR_ACCESSIBILITY_AUTO
388
491
  else IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
389
492
  updateBackCallback()
493
+ reportProgress()
390
494
  }
391
495
 
392
- private fun settle(open: Boolean, velocity: Float, user: Boolean) {
393
- stopAnimation()
496
+ private fun reportProgress() {
497
+ if (!progressEventsEnabled) return
498
+ if (progress != reportedProgress && (abs(progress - reportedProgress) >= .002f || progress == 0f || progress == 1f)) {
499
+ reportedProgress = progress
500
+ onDrawerProgress?.invoke(progress)
501
+ }
502
+ }
503
+
504
+ override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
505
+ if (child === foreground && progress > 0f && pageShadowOpacity > 0f) drawPageShadow(canvas)
506
+ return super.drawChild(canvas, child, drawingTime)
507
+ }
508
+
509
+ // A Gaussian shadow (no offset) around the page's left edge and corners,
510
+ // matching the iOS layer shadow without an offscreen blur.
511
+ private fun drawPageShadow(canvas: Canvas) {
512
+ val sigma = PAGE_SHADOW_SIGMA_DP * density
513
+ val extent = sigma * 3f
514
+ val r = foreground.cornerRadius
515
+ if (r != shadowRadius || height != shadowHeight || pageShadowOpacity != shadowOpacity) {
516
+ shadowRadius = r
517
+ shadowHeight = height
518
+ shadowOpacity = pageShadowOpacity
519
+ val stops = 8
520
+ val positions = FloatArray(stops) { it / (stops - 1f) }
521
+ // Outermost stop first: alpha at distance d outside the edge.
522
+ val edgeColors = IntArray(stops) { shadowColor(extent * (1f - positions[it]), sigma) }
523
+ shadowEdge = LinearGradient(-extent, 0f, 0f, 0f, edgeColors, positions, Shader.TileMode.CLAMP)
524
+ val outer = r + extent
525
+ val cornerPositions = FloatArray(stops) { (r + extent * it / (stops - 1f)) / outer }
526
+ val cornerColors = IntArray(stops) { shadowColor(extent * it / (stops - 1f), sigma) }
527
+ shadowTopCorner = RadialGradient(r, r, outer, cornerColors, cornerPositions, Shader.TileMode.CLAMP)
528
+ shadowBottomCorner = RadialGradient(r, height - r, outer, cornerColors, cornerPositions, Shader.TileMode.CLAMP)
529
+ }
530
+ val save = canvas.save()
531
+ canvas.translate(foreground.left + foreground.translationX, foreground.top.toFloat())
532
+ shadowPaint.shader = shadowEdge
533
+ canvas.drawRect(-extent, r, 0f, height - r, shadowPaint)
534
+ if (r > 0f) {
535
+ shadowPaint.shader = shadowTopCorner
536
+ canvas.drawRect(-extent, -extent, r, r, shadowPaint)
537
+ shadowPaint.shader = shadowBottomCorner
538
+ canvas.drawRect(-extent, height - r, r, height + extent, shadowPaint)
539
+ }
540
+ canvas.restoreToCount(save)
541
+ }
542
+
543
+ private fun shadowColor(distance: Float, sigma: Float): Int {
544
+ val z = distance / sigma
545
+ // 1 - Φ(z), the share of a Gaussian blur that spills past an edge.
546
+ val t = 1f / (1f + .2316419f * z)
547
+ val tail = .3989423f * exp(-z * z / 2f) *
548
+ t * (.31938153f + t * (-.356563782f + t * (1.781477937f + t * (-1.821255978f + t * 1.330274429f))))
549
+ return Color.argb((pageShadowOpacity * tail * 255f).roundToInt().coerceIn(0, 255), 0, 0, 0)
550
+ }
551
+
552
+ private fun animationsEnabled(): Boolean =
553
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ValueAnimator.areAnimatorsEnabled()
554
+ else Settings.Global.getFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f) != 0f
555
+
556
+ // cubic-bezier(0.55, 0.6, 0.2, 1), fitted to the reference tap open and close.
557
+ private fun pageCurve(x: Float): Float {
558
+ fun bezier(t: Float, p1: Float, p2: Float): Float {
559
+ val u = 1f - t
560
+ return 3f * u * u * t * p1 + 3f * u * t * t * p2 + t * t * t
561
+ }
562
+ var low = 0f
563
+ var high = 1f
564
+ repeat(24) {
565
+ val mid = (low + high) / 2f
566
+ if (bezier(mid, .55f, .2f) < x) low = mid else high = mid
567
+ }
568
+ return bezier((low + high) / 2f, .6f, 1f)
569
+ }
570
+
571
+ // Taps and programmatic changes ease over a fixed time; a released drag
572
+ // keeps its finger velocity through a slightly underdamped spring.
573
+ private fun settle(open: Boolean, velocity: Float, user: Boolean, tween: Boolean = true) {
394
574
  val target = drawerEnabled && open
575
+ // An animation already heading there keeps running; the newer request
576
+ // still decides whether its completion is a user event.
577
+ if (animator != null && animationTarget == target) {
578
+ animationUser = user
579
+ return
580
+ }
581
+ // Reversing mid-flight continues from the page's on-screen position and
582
+ // speed, so the motion bends instead of stopping dead.
583
+ val retarget = animator != null
584
+ val from = if (retarget) appliedValue else progress
585
+ val carried = if (retarget) trackedVelocity else velocity
586
+ stopAnimation()
395
587
  if (width == 0 || !isAttachedToWindow) {
396
588
  stableOpen = target
397
589
  setProgress(if (target) 1f else 0f)
398
590
  return
399
591
  }
400
- if (progress == if (target) 1f else 0f) {
592
+ if (!retarget && progress == if (target) 1f else 0f) {
401
593
  finish(target, user)
402
594
  return
403
595
  }
404
- val end = if (target) 1f else 0f
405
- val a = progress - end
406
- val b = velocity.coerceIn(-8f, 8f) + 22f * a
596
+ // With system animations removed, the drawer lands without moving.
597
+ if (!animationsEnabled()) {
598
+ finish(target, user)
599
+ return
600
+ }
601
+ animationFrom = from
602
+ animationVelocity = carried.coerceIn(-12f, 12f)
407
603
  animationTarget = target
408
604
  animationUser = user
409
- val next = ValueAnimator.ofFloat(0f, .5f)
410
- next.duration = 500
411
- next.interpolator = LinearInterpolator()
412
- next.addUpdateListener {
413
- val time = it.animatedValue as Float
414
- setProgress(end + (a + b * time) * exp(-22f * time))
415
- }
416
- next.addListener(object : AnimatorListenerAdapter() {
417
- override fun onAnimationEnd(animation: Animator) {
418
- if (animator !== animation) return
419
- animator = null
420
- finish(target, user)
421
- }
422
- })
605
+ // Closing runs about 20% shorter than opening.
606
+ val fullDuration = if (target) .36f else .29f
607
+ animationDuration = if (tween && !retarget) max(.12f, fullDuration * abs((if (target) 1f else 0f) - from)) else 0f
608
+ trackedValue = from
609
+ trackedTime = 0f
610
+ trackedVelocity = animationVelocity
611
+ val next = TimeAnimator()
612
+ next.setTimeListener { animation, totalTime, _ -> tick(animation, totalTime / 1000f) }
423
613
  animator = next
424
614
  next.start()
425
615
  }
426
616
 
617
+ private fun tick(animation: TimeAnimator, elapsed: Float) {
618
+ if (animator !== animation) return
619
+ val end = if (animationTarget) 1f else 0f
620
+ val spring = animationDuration == 0f
621
+ val value: Float
622
+ var tweenDone = false
623
+ if (!spring) {
624
+ val x = min(1f, elapsed / animationDuration)
625
+ value = animationFrom + (end - animationFrom) * pageCurve(x)
626
+ tweenDone = x >= 1f
627
+ } else {
628
+ val w = SPRING_FREQUENCY
629
+ val z = SPRING_DAMPING
630
+ val a = animationFrom - end
631
+ val wd = w * sqrt(1f - z * z)
632
+ val decay = exp(-z * w * elapsed)
633
+ value = end + decay * (a * cos(wd * elapsed) + (animationVelocity + z * w * a) / wd * sin(wd * elapsed))
634
+ }
635
+ if (elapsed > trackedTime) trackedVelocity = (value - trackedValue) / (elapsed - trackedTime)
636
+ trackedValue = value
637
+ trackedTime = elapsed
638
+ setProgress(value, spring)
639
+ // A spring ends once it is visually at rest, not after a fixed time.
640
+ val done = if (spring) (abs(value - end) < .0005f && abs(trackedVelocity) < .02f) || elapsed >= .6f
641
+ else tweenDone
642
+ if (done) {
643
+ val target = animationTarget
644
+ val user = animationUser
645
+ stopAnimation()
646
+ finish(target, user)
647
+ }
648
+ }
649
+
427
650
  private fun finish(target: Boolean, user: Boolean) {
428
651
  val changed = stableOpen != target
429
652
  stableOpen = target
@@ -435,8 +658,7 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
435
658
  }
436
659
 
437
660
  private fun stopAnimation() {
438
- animator?.removeAllListeners()
439
- animator?.removeAllUpdateListeners()
661
+ animator?.setTimeListener(null)
440
662
  animator?.cancel()
441
663
  animator = null
442
664
  }