@momo-kits/native-kits 0.168.1-beta.32-debug → 0.168.1-beta.33

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 (151) hide show
  1. package/CLAUDE.md +1 -0
  2. package/ios/Input/Input.swift +3 -1
  3. package/ios/Input/InputTextArea.swift +3 -1
  4. package/ios/Navigation/BottomTab/BottomTab.swift +67 -48
  5. package/ios/Navigation/BottomTab/BottomTabBar.swift +131 -281
  6. package/ios/Navigation/BottomTab/CurvedContainer.swift +47 -51
  7. package/ios/Navigation/BottomTab/DuoLiquidSelection.swift +364 -0
  8. package/ios/Navigation/BottomTab/DuoTabBar.swift +292 -72
  9. package/ios/Navigation/Component/HeaderRight.swift +1 -1
  10. package/ios/Navigation/StackScreen.swift +1 -3
  11. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/BottomTabDemo.swift +59 -272
  12. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/NavigationDemo.swift +90 -0
  13. package/package.json +1 -1
  14. package/build.gradle.kts +0 -11
  15. package/compose/build.gradle.kts +0 -198
  16. package/compose/build.gradle.kts.backup +0 -198
  17. package/compose/compose.podspec +0 -47
  18. package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +0 -20
  19. package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +0 -6
  20. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +0 -12
  21. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +0 -7
  22. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +0 -154
  23. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReservedRegions.android.kt +0 -6
  24. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +0 -110
  25. package/compose/src/commonMain/kotlin/vn/momo/kits/application/DesignSystemKits.kt +0 -62
  26. package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +0 -1071
  27. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +0 -269
  28. package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +0 -56
  29. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +0 -157
  30. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +0 -81
  31. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +0 -32
  32. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +0 -338
  33. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +0 -175
  34. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +0 -357
  35. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +0 -115
  36. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +0 -89
  37. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +0 -128
  38. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +0 -209
  39. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +0 -542
  40. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +0 -49
  41. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +0 -86
  42. package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +0 -125
  43. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +0 -231
  44. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +0 -189
  45. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +0 -478
  46. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +0 -114
  47. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +0 -265
  48. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +0 -235
  49. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +0 -223
  50. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +0 -265
  51. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +0 -185
  52. package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +0 -343
  53. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +0 -108
  54. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +0 -56
  55. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +0 -41
  56. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +0 -84
  57. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +0 -40
  58. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +0 -370
  59. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +0 -108
  60. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +0 -308
  61. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +0 -59
  62. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +0 -87
  63. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +0 -21
  64. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +0 -90
  65. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +0 -330
  66. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +0 -220
  67. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +0 -473
  68. package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +0 -122
  69. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +0 -207
  70. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +0 -92
  71. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +0 -560
  72. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +0 -86
  73. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +0 -125
  74. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +0 -207
  75. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +0 -501
  76. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +0 -176
  77. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +0 -182
  78. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +0 -193
  79. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +0 -29
  80. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +0 -236
  81. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +0 -180
  82. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +0 -325
  83. package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +0 -17
  84. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +0 -12
  85. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +0 -16
  86. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +0 -194
  87. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +0 -287
  88. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +0 -54
  89. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +0 -50
  90. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ScreenLayout.kt +0 -321
  91. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +0 -41
  92. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +0 -52
  93. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +0 -66
  94. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +0 -11
  95. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +0 -104
  96. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +0 -48
  97. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +0 -51
  98. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +0 -502
  99. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/HostBackGestureBridge.kt +0 -58
  100. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +0 -148
  101. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +0 -116
  102. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +0 -205
  103. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +0 -422
  104. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +0 -8
  105. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +0 -732
  106. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +0 -148
  107. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +0 -35
  108. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +0 -240
  109. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +0 -389
  110. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +0 -94
  111. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoBarCompression.kt +0 -97
  112. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoTabBar.kt +0 -203
  113. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/TabBarMetrics.kt +0 -106
  114. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +0 -169
  115. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +0 -455
  116. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +0 -105
  117. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderJourney.kt +0 -106
  118. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderLocation.kt +0 -74
  119. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +0 -399
  120. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +0 -39
  121. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +0 -363
  122. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SideBar.kt +0 -192
  123. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +0 -133
  124. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/tracking/ScreenTracker.kt +0 -127
  125. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/BackGesture.kt +0 -12
  126. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +0 -92
  127. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +0 -9
  128. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +0 -76
  129. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReservedRegions.kt +0 -20
  130. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +0 -1329
  131. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +0 -55
  132. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +0 -15
  133. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +0 -118
  134. package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +0 -42
  135. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/HostBackGesture.ios.kt +0 -45
  136. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/ScrollToTop.ios.kt +0 -32
  137. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/BackGesture.ios.kt +0 -38
  138. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +0 -66
  139. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/Platform.ios.kt +0 -219
  140. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReservedRegions.ios.kt +0 -138
  141. package/gradle/libs.versions.toml +0 -69
  142. package/gradle/wrapper/gradle-wrapper.jar +0 -0
  143. package/gradle/wrapper/gradle-wrapper.properties +0 -8
  144. package/gradle.properties +0 -26
  145. package/gradlew +0 -252
  146. package/gradlew.bat +0 -94
  147. package/ios/Navigation/BottomTab/TabBarMetrics.swift +0 -88
  148. package/ios-demo/README.md +0 -120
  149. package/ios-demo/Resources/tabbar_scan_qr_button.json +0 -1
  150. package/ios-demo/Resources/tabbar_scan_qr_label.json +0 -1
  151. package/settings.gradle.kts +0 -64
@@ -0,0 +1,364 @@
1
+ //
2
+ // DuoLiquidSelection.swift
3
+ // MoMoUIKits
4
+ //
5
+ // iOS 26 tab bar selection motion without the glass material, port of RN
6
+ // `BottomTab/useLiquidSelection.ts` (Compose `DuoLiquidSelection.kt`).
7
+ //
8
+
9
+ import SwiftUI
10
+ import UIKit
11
+
12
+ enum LiquidAxis {
13
+ case x, y
14
+ }
15
+
16
+ // RN reanimated { damping: 18, stiffness: 220, mass: 0.8 }
17
+ let liquidSpring = Animation.interpolatingSpring(mass: 0.8, stiffness: 220, damping: 18)
18
+
19
+ private struct SpringParams {
20
+ let mass: CGFloat
21
+ let stiffness: CGFloat
22
+ let damping: CGFloat
23
+
24
+ static let standard = SpringParams(mass: 0.8, stiffness: 220, damping: 18)
25
+ // the lens trails the finger a little while dragged
26
+ static let drag = SpringParams(mass: 0.6, stiffness: 600, damping: 24)
27
+ }
28
+
29
+ private let reducedDuration: CFTimeInterval = 0.2
30
+ // while pressed the selection bubble grows into a lens past the bar's edges (80x54 to about 96x70)
31
+ let liquidLensScaleX: CGFloat = 1.2
32
+ let liquidLensScaleY: CGFloat = 1.3
33
+ // a moving lens stretches along its path and thins across it, eased per frame
34
+ private let stretchPerPoint: CGFloat = 0.012
35
+ private let maxStretch: CGFloat = 0.25
36
+ private let stretchEasing: CGFloat = 0.25
37
+ private let restStretch: CGFloat = 0.002
38
+ // a tap is shorter than the lift: the lens finishes rising, then settles, one smooth pulse
39
+ private let tapLiftDuration: Double = 0.12
40
+ private let barSwell: CGFloat = 0.04
41
+ private let iconPop: CGFloat = 1.15
42
+ private let iconPopDuration: Double = 0.12
43
+ // below it a press is a tap
44
+ private let dragSlop: CGFloat = 10
45
+ // UIScrollView rubber band constant
46
+ private let rubberBandConstant: CGFloat = 0.55
47
+ // a vetoed switch: the lens returns to the selected tab after this
48
+ private let resyncDelay: Double = 0.1
49
+ // RN TouchableOpacity delayLongPress
50
+ private let longPressDelay: Double = 0.5
51
+
52
+ /// Resistance past the first and last tab, the further the finger goes the less the lens follows.
53
+ private func rubberBand(_ value: CGFloat, max: CGFloat, dimension: CGFloat) -> CGFloat {
54
+ func band(_ overshoot: CGFloat) -> CGFloat {
55
+ (1 - 1 / (overshoot * rubberBandConstant / dimension + 1)) * dimension
56
+ }
57
+ if value < 0 { return -band(-value) }
58
+ if value > max { return max + band(value - max) }
59
+ return value
60
+ }
61
+
62
+ /// Owns the lens: its offset is integrated on a display link (SwiftUI can't expose an animation's
63
+ /// in-flight value, and the stretch and the tabs' active tint follow it every frame); lift and pop
64
+ /// are plain SwiftUI springs. Input is one gesture over the whole bar, so a press is either a tap,
65
+ /// a long press or a drag.
66
+ final class DuoLiquidSelection: ObservableObject {
67
+ @Published private(set) var offset: CGFloat = 0
68
+ @Published private(set) var stretch: CGFloat = 0
69
+ @Published private(set) var lift: CGFloat = 0
70
+ @Published private(set) var selectedIndex = 0
71
+ @Published private(set) var popIndex: Int?
72
+ @Published private(set) var pop: CGFloat = 1
73
+
74
+ var axis: LiquidAxis = .x
75
+ var count = 0
76
+ /// Distance between two neighbouring tab centers.
77
+ var pitch: CGFloat = 0
78
+ /// The first tab's center in the gesture's coordinate space.
79
+ var firstCenter: CGFloat = 0
80
+ var reduceMotion = false
81
+ var onSelect: (Int) -> Void = { _ in }
82
+ var onLongPress: ((Int) -> Void)?
83
+ var onTick: () -> Void = {}
84
+
85
+ private var velocity: CGFloat = 0
86
+ // where the lens is heading, so a re-render doesn't restart a spring already going there
87
+ private var destination: CGFloat = 0
88
+ private var spring = SpringParams.standard
89
+ private var timed: (from: CGFloat, start: CFTimeInterval?)?
90
+ private var displayLink: CADisplayLink?
91
+ private var lastTimestamp: CFTimeInterval?
92
+ private var measured = false
93
+
94
+ private var touchIndex: Int?
95
+ private var dragging = false
96
+ private var hovered = 0
97
+ private var longPressWork: DispatchWorkItem?
98
+ private var resyncWork: DispatchWorkItem?
99
+ private var lastSample: (position: CGFloat, time: Date)?
100
+ private var dragVelocity: CGFloat = 0
101
+ private var liftGeneration = 0
102
+ private var popGeneration = 0
103
+
104
+ deinit {
105
+ displayLink?.invalidate()
106
+ }
107
+
108
+ func sync(index: Int) {
109
+ let changed = index != selectedIndex
110
+ selectedIndex = index
111
+ let target = CGFloat(index) * pitch
112
+ // snap on first layout so the lens doesn't slide in from the leading edge
113
+ if measured {
114
+ move(to: target)
115
+ if changed { popIcon(index) }
116
+ } else {
117
+ stop()
118
+ destination = target
119
+ offset = target
120
+ }
121
+ measured = pitch > 0
122
+ }
123
+
124
+ func activeOpacity(_ index: Int) -> Double {
125
+ guard pitch > 0 else { return selectedIndex == index ? 1 : 0 }
126
+ return Double(min(max(1 - abs(offset - CGFloat(index) * pitch) / pitch, 0), 1))
127
+ }
128
+
129
+ var barScale: CGFloat { 1 + lift * barSwell }
130
+
131
+ var lensScale: (along: CGFloat, across: CGFloat, x: CGFloat, y: CGFloat) {
132
+ (1 + stretch, 1 - stretch * 0.5, 1 + lift * (liquidLensScaleX - 1), 1 + lift * (liquidLensScaleY - 1))
133
+ }
134
+
135
+ // MARK: Input
136
+
137
+ func touchChanged(location: CGPoint, translation: CGSize, time: Date) {
138
+ let value = position(location)
139
+ defer { lastSample = (value, time) }
140
+ guard let touchIndex else {
141
+ let index = nearest(value)
142
+ touchIndex = index
143
+ dragging = false
144
+ dragVelocity = 0
145
+ pressIn(index)
146
+ let work = DispatchWorkItem { [weak self] in self?.onLongPress?(index) }
147
+ longPressWork = work
148
+ DispatchQueue.main.asyncAfter(deadline: .now() + longPressDelay, execute: work)
149
+ return
150
+ }
151
+ if !dragging, hypot(translation.width, translation.height) > dragSlop {
152
+ dragging = true
153
+ longPressWork?.cancel()
154
+ hovered = touchIndex
155
+ animateLift(1)
156
+ }
157
+ guard dragging else { return }
158
+ if let lastSample {
159
+ let dt = time.timeIntervalSince(lastSample.time)
160
+ if dt > 0 { dragVelocity = (value - lastSample.position) / CGFloat(dt) }
161
+ }
162
+ let target = nearest(value)
163
+ if target != hovered {
164
+ hovered = target
165
+ onTick()
166
+ }
167
+ let follow = rubberBand(value, max: CGFloat(count - 1) * pitch, dimension: pitch)
168
+ if reduceMotion {
169
+ stop()
170
+ destination = follow
171
+ offset = follow
172
+ } else {
173
+ move(to: follow, spring: .drag)
174
+ }
175
+ }
176
+
177
+ func touchEnded(location: CGPoint) {
178
+ longPressWork?.cancel()
179
+ longPressWork = nil
180
+ defer {
181
+ touchIndex = nil
182
+ lastSample = nil
183
+ }
184
+ guard let touchIndex else { return }
185
+ if dragging {
186
+ dragging = false
187
+ animateLift(0)
188
+ let target = nearest(position(location))
189
+ move(to: CGFloat(target) * pitch, velocity: dragVelocity)
190
+ select(target)
191
+ } else {
192
+ pulseLift()
193
+ // a tap or a held tab, both pick the pressed tab like the system tab bar
194
+ select(touchIndex)
195
+ }
196
+ }
197
+
198
+ /// The system took the touch: nothing is selected, the lens returns to the selected tab.
199
+ func touchCancelled() {
200
+ guard touchIndex != nil else { return }
201
+ longPressWork?.cancel()
202
+ longPressWork = nil
203
+ touchIndex = nil
204
+ lastSample = nil
205
+ dragging = false
206
+ animateLift(0)
207
+ move(to: CGFloat(selectedIndex) * pitch, velocity: 0)
208
+ }
209
+
210
+ // MARK: Motion
211
+
212
+ private func position(_ point: CGPoint) -> CGFloat {
213
+ (axis == .x ? point.x : point.y) - firstCenter
214
+ }
215
+
216
+ private func nearest(_ value: CGFloat) -> Int {
217
+ guard pitch > 0 else { return 0 }
218
+ return min(max(Int((value / pitch).rounded()), 0), max(count - 1, 0))
219
+ }
220
+
221
+ private func pressIn(_ index: Int) {
222
+ resyncWork?.cancel()
223
+ // the lens heads to the pressed tab, not only the selected one
224
+ move(to: CGFloat(index) * pitch)
225
+ animateLift(1)
226
+ }
227
+
228
+ private func select(_ index: Int) {
229
+ onSelect(index)
230
+ resync()
231
+ }
232
+
233
+ /// Returns the lens to the selected tab unless a switch to where it is heading happened.
234
+ private func resync() {
235
+ resyncWork?.cancel()
236
+ let work = DispatchWorkItem { [weak self] in
237
+ guard let self, !self.dragging else { return }
238
+ let target = CGFloat(self.selectedIndex) * self.pitch
239
+ if self.destination != target { self.move(to: target) }
240
+ }
241
+ resyncWork = work
242
+ DispatchQueue.main.asyncAfter(deadline: .now() + resyncDelay, execute: work)
243
+ }
244
+
245
+ private func animateLift(_ target: CGFloat) {
246
+ liftGeneration += 1
247
+ if reduceMotion {
248
+ lift = 0
249
+ return
250
+ }
251
+ withAnimation(liquidSpring) { lift = target }
252
+ }
253
+
254
+ private func pulseLift() {
255
+ guard !reduceMotion else { return animateLift(0) }
256
+ liftGeneration += 1
257
+ let generation = liftGeneration
258
+ withAnimation(.linear(duration: tapLiftDuration)) { lift = 1 }
259
+ DispatchQueue.main.asyncAfter(deadline: .now() + tapLiftDuration) { [weak self] in
260
+ guard let self, self.liftGeneration == generation else { return }
261
+ withAnimation(liquidSpring) { self.lift = 0 }
262
+ }
263
+ }
264
+
265
+ private func popIcon(_ index: Int) {
266
+ guard !reduceMotion else { return }
267
+ popGeneration += 1
268
+ let generation = popGeneration
269
+ popIndex = index
270
+ pop = 1
271
+ withAnimation(.linear(duration: iconPopDuration)) { pop = iconPop }
272
+ DispatchQueue.main.asyncAfter(deadline: .now() + iconPopDuration) { [weak self] in
273
+ guard let self, self.popGeneration == generation else { return }
274
+ withAnimation(liquidSpring) { self.pop = 1 }
275
+ }
276
+ }
277
+
278
+ private func move(to target: CGFloat, velocity: CGFloat? = nil, spring: SpringParams = .standard) {
279
+ if destination == target, velocity == nil { return }
280
+ destination = target
281
+ self.spring = spring
282
+ if let velocity { self.velocity = velocity }
283
+ timed = reduceMotion ? (from: offset, start: nil) : nil
284
+ startDisplayLink()
285
+ }
286
+
287
+ private func stop() {
288
+ velocity = 0
289
+ timed = nil
290
+ }
291
+
292
+ fileprivate func tick(_ link: CADisplayLink) {
293
+ let now = link.targetTimestamp
294
+ let dt = min(now - (lastTimestamp ?? link.timestamp), 1.0 / 30)
295
+ lastTimestamp = now
296
+ let previous = offset
297
+ var next = offset
298
+ var settled = false
299
+
300
+ if var timed {
301
+ let start = timed.start ?? link.timestamp
302
+ timed.start = start
303
+ self.timed = timed
304
+ let fraction = min(max((now - start) / reducedDuration, 0), 1)
305
+ // reanimated withTiming default: ease in-out quad
306
+ let eased = fraction < 0.5 ? 2 * fraction * fraction : 1 - 2 * (1 - fraction) * (1 - fraction)
307
+ next = timed.from + (destination - timed.from) * CGFloat(eased)
308
+ settled = fraction >= 1
309
+ } else {
310
+ // a few sub steps keep the stiff drag spring stable
311
+ let steps = 4
312
+ let h = CGFloat(dt) / CGFloat(steps)
313
+ for _ in 0..<steps {
314
+ let force = -spring.stiffness * (next - destination) - spring.damping * velocity
315
+ velocity += force / spring.mass * h
316
+ next += velocity * h
317
+ }
318
+ settled = abs(next - destination) < 0.1 && abs(velocity) < 1
319
+ }
320
+ if settled {
321
+ next = destination
322
+ stop()
323
+ }
324
+ offset = next
325
+
326
+ if reduceMotion {
327
+ stretch = 0
328
+ } else {
329
+ let target = min(abs(next - previous) * stretchPerPoint, maxStretch)
330
+ // eased both ways: a slow frame (the spring turning at its overshoot) must not snap the shape
331
+ let eased = stretch + (target - stretch) * stretchEasing
332
+ stretch = eased < restStretch ? 0 : eased
333
+ }
334
+ if settled, stretch == 0 { stopDisplayLink() }
335
+ }
336
+
337
+ private func startDisplayLink() {
338
+ guard displayLink == nil else { return }
339
+ let link = CADisplayLink(target: DisplayLinkProxy(owner: self), selector: #selector(DisplayLinkProxy.tick))
340
+ link.preferredFrameRateRange = CAFrameRateRange(minimum: 60, maximum: 120, preferred: 120)
341
+ link.add(to: .main, forMode: .common)
342
+ displayLink = link
343
+ lastTimestamp = nil
344
+ }
345
+
346
+ private func stopDisplayLink() {
347
+ displayLink?.invalidate()
348
+ displayLink = nil
349
+ }
350
+ }
351
+
352
+ /// CADisplayLink retains its target; the proxy keeps the selection free to deinit mid motion.
353
+ private final class DisplayLinkProxy: NSObject {
354
+ weak var owner: DuoLiquidSelection?
355
+
356
+ init(owner: DuoLiquidSelection) {
357
+ self.owner = owner
358
+ }
359
+
360
+ @objc func tick(_ link: CADisplayLink) {
361
+ guard let owner else { return link.invalidate() }
362
+ owner.tick(link)
363
+ }
364
+ }