expo-android-glass-view 0.1.0

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 (140) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +55 -0
  3. package/README.md +260 -0
  4. package/THIRD_PARTY_NOTICES.md +44 -0
  5. package/android/build.gradle +39 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/expo/modules/androidglassview/AndroidGlassBottomTabs.kt +162 -0
  8. package/android/src/main/java/expo/modules/androidglassview/AndroidGlassButton.kt +45 -0
  9. package/android/src/main/java/expo/modules/androidglassview/AndroidGlassSlider.kt +84 -0
  10. package/android/src/main/java/expo/modules/androidglassview/AndroidGlassToggle.kt +59 -0
  11. package/android/src/main/java/expo/modules/androidglassview/AndroidGlassView.kt +21 -0
  12. package/android/src/main/java/expo/modules/androidglassview/ControlledProp.kt +30 -0
  13. package/android/src/main/java/expo/modules/androidglassview/ExpoAndroidGlassViewModule.kt +210 -0
  14. package/android/src/main/java/expo/modules/androidglassview/GlassHostView.kt +346 -0
  15. package/android/src/main/java/expo/modules/androidglassview/GlassRegistry.kt +31 -0
  16. package/android/src/main/java/expo/modules/androidglassview/GlassSurface.kt +96 -0
  17. package/android/src/main/java/expo/modules/androidglassview/ViewBackdrop.kt +151 -0
  18. package/android/src/main/java/expo/modules/androidglassview/backdrop/Backdrop.kt +25 -0
  19. package/android/src/main/java/expo/modules/androidglassview/backdrop/BackdropEffectScope.kt +82 -0
  20. package/android/src/main/java/expo/modules/androidglassview/backdrop/DrawBackdropModifier.kt +397 -0
  21. package/android/src/main/java/expo/modules/androidglassview/backdrop/Platform.kt +18 -0
  22. package/android/src/main/java/expo/modules/androidglassview/backdrop/RuntimeShader.kt +94 -0
  23. package/android/src/main/java/expo/modules/androidglassview/backdrop/RuntimeShaderCache.kt +28 -0
  24. package/android/src/main/java/expo/modules/androidglassview/backdrop/backdrops/Backdrop.kt +45 -0
  25. package/android/src/main/java/expo/modules/androidglassview/backdrop/backdrops/CanvasBackdrop.kt +43 -0
  26. package/android/src/main/java/expo/modules/androidglassview/backdrop/backdrops/CombinedBackdrop.kt +107 -0
  27. package/android/src/main/java/expo/modules/androidglassview/backdrop/backdrops/EmptyBackdrop.kt +33 -0
  28. package/android/src/main/java/expo/modules/androidglassview/backdrop/backdrops/LayerBackdrop.kt +82 -0
  29. package/android/src/main/java/expo/modules/androidglassview/backdrop/backdrops/LayerBackdropModifier.kt +77 -0
  30. package/android/src/main/java/expo/modules/androidglassview/backdrop/effects/Blur.kt +37 -0
  31. package/android/src/main/java/expo/modules/androidglassview/backdrop/effects/ColorFilter.kt +83 -0
  32. package/android/src/main/java/expo/modules/androidglassview/backdrop/effects/Lens.kt +117 -0
  33. package/android/src/main/java/expo/modules/androidglassview/backdrop/effects/RenderEffect.kt +41 -0
  34. package/android/src/main/java/expo/modules/androidglassview/backdrop/highlight/Highlight.kt +36 -0
  35. package/android/src/main/java/expo/modules/androidglassview/backdrop/highlight/HighlightModifier.kt +174 -0
  36. package/android/src/main/java/expo/modules/androidglassview/backdrop/highlight/HighlightStyle.kt +146 -0
  37. package/android/src/main/java/expo/modules/androidglassview/backdrop/internal/InverseLayerScope.kt +138 -0
  38. package/android/src/main/java/expo/modules/androidglassview/backdrop/internal/LayerRecorder.kt +34 -0
  39. package/android/src/main/java/expo/modules/androidglassview/backdrop/internal/Outline.kt +26 -0
  40. package/android/src/main/java/expo/modules/androidglassview/backdrop/internal/Paint.kt +24 -0
  41. package/android/src/main/java/expo/modules/androidglassview/backdrop/internal/RenderEffect.kt +58 -0
  42. package/android/src/main/java/expo/modules/androidglassview/backdrop/internal/Shaders.kt +206 -0
  43. package/android/src/main/java/expo/modules/androidglassview/backdrop/internal/ShapeProvider.kt +52 -0
  44. package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/InnerShadow.kt +49 -0
  45. package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/InnerShadowModifier.kt +166 -0
  46. package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/Shadow.kt +35 -0
  47. package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/ShadowModifier.kt +145 -0
  48. package/android/src/main/java/expo/modules/androidglassview/capture/ViewBackdropCapture.kt +176 -0
  49. package/android/src/main/java/expo/modules/androidglassview/components/CapsuleShape.kt +10 -0
  50. package/android/src/main/java/expo/modules/androidglassview/components/DampedDragAnimation.kt +152 -0
  51. package/android/src/main/java/expo/modules/androidglassview/components/DragGestureInspector.kt +91 -0
  52. package/android/src/main/java/expo/modules/androidglassview/components/InteractiveHighlight.kt +127 -0
  53. package/android/src/main/java/expo/modules/androidglassview/components/LiquidBottomTabs.kt +471 -0
  54. package/android/src/main/java/expo/modules/androidglassview/components/LiquidButton.kt +111 -0
  55. package/android/src/main/java/expo/modules/androidglassview/components/LiquidSlider.kt +227 -0
  56. package/android/src/main/java/expo/modules/androidglassview/components/LiquidToggle.kt +220 -0
  57. package/android/src/main/java/expo/modules/androidglassview/components/TabsFrame.kt +18 -0
  58. package/build/AndroidGlassBottomTabs.android.d.ts +4 -0
  59. package/build/AndroidGlassBottomTabs.android.d.ts.map +1 -0
  60. package/build/AndroidGlassBottomTabs.android.js +42 -0
  61. package/build/AndroidGlassBottomTabs.android.js.map +1 -0
  62. package/build/AndroidGlassBottomTabs.d.ts +5 -0
  63. package/build/AndroidGlassBottomTabs.d.ts.map +1 -0
  64. package/build/AndroidGlassBottomTabs.js +34 -0
  65. package/build/AndroidGlassBottomTabs.js.map +1 -0
  66. package/build/AndroidGlassButton.android.d.ts +4 -0
  67. package/build/AndroidGlassButton.android.d.ts.map +1 -0
  68. package/build/AndroidGlassButton.android.js +32 -0
  69. package/build/AndroidGlassButton.android.js.map +1 -0
  70. package/build/AndroidGlassButton.d.ts +5 -0
  71. package/build/AndroidGlassButton.d.ts.map +1 -0
  72. package/build/AndroidGlassButton.js +38 -0
  73. package/build/AndroidGlassButton.js.map +1 -0
  74. package/build/AndroidGlassComponents.types.d.ts +71 -0
  75. package/build/AndroidGlassComponents.types.d.ts.map +1 -0
  76. package/build/AndroidGlassComponents.types.js +2 -0
  77. package/build/AndroidGlassComponents.types.js.map +1 -0
  78. package/build/AndroidGlassSlider.android.d.ts +4 -0
  79. package/build/AndroidGlassSlider.android.d.ts.map +1 -0
  80. package/build/AndroidGlassSlider.android.js +27 -0
  81. package/build/AndroidGlassSlider.android.js.map +1 -0
  82. package/build/AndroidGlassSlider.d.ts +5 -0
  83. package/build/AndroidGlassSlider.d.ts.map +1 -0
  84. package/build/AndroidGlassSlider.js +47 -0
  85. package/build/AndroidGlassSlider.js.map +1 -0
  86. package/build/AndroidGlassTab.d.ts +9 -0
  87. package/build/AndroidGlassTab.d.ts.map +1 -0
  88. package/build/AndroidGlassTab.js +22 -0
  89. package/build/AndroidGlassTab.js.map +1 -0
  90. package/build/AndroidGlassToggle.android.d.ts +4 -0
  91. package/build/AndroidGlassToggle.android.d.ts.map +1 -0
  92. package/build/AndroidGlassToggle.android.js +27 -0
  93. package/build/AndroidGlassToggle.android.js.map +1 -0
  94. package/build/AndroidGlassToggle.d.ts +5 -0
  95. package/build/AndroidGlassToggle.d.ts.map +1 -0
  96. package/build/AndroidGlassToggle.js +7 -0
  97. package/build/AndroidGlassToggle.js.map +1 -0
  98. package/build/AndroidGlassView.android.d.ts +4 -0
  99. package/build/AndroidGlassView.android.d.ts.map +1 -0
  100. package/build/AndroidGlassView.android.js +10 -0
  101. package/build/AndroidGlassView.android.js.map +1 -0
  102. package/build/AndroidGlassView.d.ts +8 -0
  103. package/build/AndroidGlassView.d.ts.map +1 -0
  104. package/build/AndroidGlassView.js +18 -0
  105. package/build/AndroidGlassView.js.map +1 -0
  106. package/build/AndroidGlassView.types.d.ts +34 -0
  107. package/build/AndroidGlassView.types.d.ts.map +1 -0
  108. package/build/AndroidGlassView.types.js +2 -0
  109. package/build/AndroidGlassView.types.js.map +1 -0
  110. package/build/index.d.ts +10 -0
  111. package/build/index.d.ts.map +1 -0
  112. package/build/index.js +12 -0
  113. package/build/index.js.map +1 -0
  114. package/build/useMinimizeOnScroll.d.ts +17 -0
  115. package/build/useMinimizeOnScroll.d.ts.map +1 -0
  116. package/build/useMinimizeOnScroll.js +39 -0
  117. package/build/useMinimizeOnScroll.js.map +1 -0
  118. package/build/utils.d.ts +5 -0
  119. package/build/utils.d.ts.map +1 -0
  120. package/build/utils.js +9 -0
  121. package/build/utils.js.map +1 -0
  122. package/expo-module.config.json +6 -0
  123. package/package.json +58 -0
  124. package/src/AndroidGlassBottomTabs.android.tsx +93 -0
  125. package/src/AndroidGlassBottomTabs.tsx +59 -0
  126. package/src/AndroidGlassButton.android.tsx +70 -0
  127. package/src/AndroidGlassButton.tsx +70 -0
  128. package/src/AndroidGlassComponents.types.ts +76 -0
  129. package/src/AndroidGlassSlider.android.tsx +70 -0
  130. package/src/AndroidGlassSlider.tsx +78 -0
  131. package/src/AndroidGlassTab.tsx +29 -0
  132. package/src/AndroidGlassToggle.android.tsx +64 -0
  133. package/src/AndroidGlassToggle.tsx +22 -0
  134. package/src/AndroidGlassView.android.tsx +41 -0
  135. package/src/AndroidGlassView.tsx +41 -0
  136. package/src/AndroidGlassView.types.ts +34 -0
  137. package/src/index.ts +11 -0
  138. package/src/useMinimizeOnScroll.ts +53 -0
  139. package/src/utils.ts +10 -0
  140. package/tsconfig.json +28 -0
@@ -0,0 +1,471 @@
1
+ /*
2
+ * Copyright 2025 Kyant (https://github.com/Kyant0/AndroidLiquidGlass)
3
+ * Licensed under the Apache License, Version 2.0. See THIRD_PARTY_NOTICES.md.
4
+ *
5
+ * From the Backdrop Catalog sample app (LiquidBottomTabs + LiquidBottomTab). Changes from
6
+ * upstream: package relocated, made internal, configurable colours, sized by React Native, the
7
+ * Compose tab content slot became a draw callback (the React Native tab views), the bar itself
8
+ * takes the touches (a tap selects the tab under the finger, like upstream's tab click; a
9
+ * horizontal drag from anywhere on the bar scrubs the droplet between tabs, iOS-style), and the
10
+ * bar can minimize (shorter and narrower, with the labels fading out).
11
+ */
12
+ package expo.modules.androidglassview.components
13
+
14
+ import androidx.compose.animation.core.Animatable
15
+ import androidx.compose.animation.core.EaseOut
16
+ import androidx.compose.animation.core.spring
17
+ import androidx.compose.foundation.isSystemInDarkTheme
18
+ import androidx.compose.foundation.layout.Box
19
+ import androidx.compose.foundation.layout.BoxWithConstraints
20
+ import androidx.compose.runtime.Composable
21
+ import androidx.compose.runtime.LaunchedEffect
22
+ import androidx.compose.runtime.derivedStateOf
23
+ import androidx.compose.runtime.getValue
24
+ import androidx.compose.runtime.mutableIntStateOf
25
+ import androidx.compose.runtime.remember
26
+ import androidx.compose.runtime.rememberCoroutineScope
27
+ import androidx.compose.runtime.rememberUpdatedState
28
+ import androidx.compose.runtime.setValue
29
+ import androidx.compose.runtime.snapshotFlow
30
+ import androidx.compose.ui.Modifier
31
+ import androidx.compose.ui.draw.alpha
32
+ import androidx.compose.ui.geometry.Offset
33
+ import androidx.compose.ui.geometry.Rect
34
+ import androidx.compose.ui.graphics.Color
35
+ import androidx.compose.ui.graphics.drawscope.DrawScope
36
+ import androidx.compose.ui.graphics.graphicsLayer
37
+ import androidx.compose.ui.input.pointer.pointerInput
38
+ import androidx.compose.ui.layout.layout
39
+ import androidx.compose.ui.platform.LocalDensity
40
+ import androidx.compose.ui.platform.LocalLayoutDirection
41
+ import androidx.compose.ui.semantics.clearAndSetSemantics
42
+ import androidx.compose.ui.unit.Constraints
43
+ import androidx.compose.ui.unit.Density
44
+ import androidx.compose.ui.unit.LayoutDirection
45
+ import androidx.compose.ui.unit.dp
46
+ import androidx.compose.ui.util.fastCoerceIn
47
+ import androidx.compose.ui.util.fastRoundToInt
48
+ import androidx.compose.ui.util.lerp
49
+ import expo.modules.androidglassview.backdrop.Backdrop
50
+ import expo.modules.androidglassview.backdrop.backdrops.layerBackdrop
51
+ import expo.modules.androidglassview.backdrop.backdrops.rememberCombinedBackdrop
52
+ import expo.modules.androidglassview.backdrop.backdrops.rememberLayerBackdrop
53
+ import expo.modules.androidglassview.backdrop.drawBackdrop
54
+ import expo.modules.androidglassview.backdrop.effects.blur
55
+ import expo.modules.androidglassview.backdrop.effects.lens
56
+ import expo.modules.androidglassview.backdrop.effects.vibrancy
57
+ import expo.modules.androidglassview.backdrop.highlight.Highlight
58
+ import expo.modules.androidglassview.backdrop.shadow.InnerShadow
59
+ import expo.modules.androidglassview.backdrop.shadow.Shadow
60
+ import kotlinx.coroutines.flow.collectLatest
61
+ import kotlinx.coroutines.flow.drop
62
+ import kotlinx.coroutines.launch
63
+ import kotlin.math.abs
64
+ import kotlin.math.min
65
+ import kotlin.math.sign
66
+
67
+ /**
68
+ * @param drawTabs draws the tab content. `origin` is the position of the current draw scope in
69
+ * the bar, `frame` where the tab slots are right now, `tint` (when set) turns the content into
70
+ * a silhouette of that colour and `scale` magnifies every tab.
71
+ * @param minimized whether the bar should be minimized: shorter and narrower, labels gone. The
72
+ * change is animated.
73
+ * @param onExpand called when the user touches the bar (a tap, a scrub or the droplet).
74
+ */
75
+ @Composable
76
+ internal fun LiquidBottomTabs(
77
+ selectedTabIndex: () -> Int,
78
+ onTabSelected: (index: Int) -> Unit,
79
+ backdrop: Backdrop,
80
+ tabsCount: Int,
81
+ drawTabs: DrawScope.(origin: Offset, frame: TabsFrame, tint: Color?, scale: Float) -> Unit,
82
+ modifier: Modifier = Modifier,
83
+ minimized: () -> Boolean = { false },
84
+ onExpand: () -> Unit = {},
85
+ accentColor: Color? = null,
86
+ containerColor: Color? = null
87
+ ) {
88
+ val isLightTheme = !isSystemInDarkTheme()
89
+ val accent = accentColor
90
+ ?: if (isLightTheme) Color(0xFF0088FF)
91
+ else Color(0xFF0091FF)
92
+ val container = containerColor
93
+ ?: if (isLightTheme) Color(0xFFFAFAFA).copy(0.4f)
94
+ else Color(0xFF121212).copy(0.4f)
95
+
96
+ val tabsBackdrop = rememberLayerBackdrop()
97
+ val currentOnExpand by rememberUpdatedState(onExpand)
98
+
99
+ BoxWithConstraints(modifier) {
100
+ val density = LocalDensity.current
101
+ val maxWidth = constraints.maxWidth
102
+ val maxHeight = constraints.maxHeight
103
+ val geometry = remember(density, maxWidth, maxHeight, tabsCount) {
104
+ TabsGeometry(density, maxWidth.toFloat(), maxHeight.toFloat(), tabsCount)
105
+ }
106
+
107
+ // 0 = expanded, 1 = minimized. Only read while laying out and drawing, so the animation
108
+ // neither recomposes nor restarts the gestures.
109
+ val minimizeAnimation = remember { Animatable(if (minimized()) 1f else 0f) }
110
+ LaunchedEffect(minimizeAnimation) {
111
+ snapshotFlow { minimized() }
112
+ .collectLatest { isMinimized ->
113
+ minimizeAnimation.animateTo(if (isMinimized) 1f else 0f, MinimizeAnimationSpec)
114
+ }
115
+ }
116
+
117
+ val offsetAnimation = remember { Animatable(0f) }
118
+ val panelOffset by remember(density, maxWidth) {
119
+ derivedStateOf {
120
+ val fraction = (offsetAnimation.value / maxWidth).fastCoerceIn(-1f, 1f)
121
+ with(density) {
122
+ 4f.dp.toPx() * fraction.sign * EaseOut.transform(abs(fraction))
123
+ }
124
+ }
125
+ }
126
+
127
+ val isLtr = LocalLayoutDirection.current == LayoutDirection.Ltr
128
+ val animationScope = rememberCoroutineScope()
129
+ var currentIndex by remember(selectedTabIndex) {
130
+ mutableIntStateOf(selectedTabIndex())
131
+ }
132
+ val dampedDragAnimation = remember(animationScope, tabsCount, geometry) {
133
+ DampedDragAnimation(
134
+ animationScope = animationScope,
135
+ initialValue = currentIndex.toFloat(),
136
+ valueRange = 0f..(tabsCount - 1).toFloat(),
137
+ visibilityThreshold = 0.001f,
138
+ initialScale = 1f,
139
+ pressedScale = 78f / 56f,
140
+ onDragStarted = { currentOnExpand() },
141
+ onDragStopped = {
142
+ val targetIndex = targetValue.fastRoundToInt().fastCoerceIn(0, tabsCount - 1)
143
+ currentIndex = targetIndex
144
+ animateToValue(targetIndex.toFloat())
145
+ animationScope.launch {
146
+ offsetAnimation.animateTo(
147
+ 0f,
148
+ spring(1f, 300f, 0.5f)
149
+ )
150
+ }
151
+ },
152
+ onDrag = { _, dragAmount ->
153
+ val tabWidth = geometry.tabWidth(minimizeAnimation.value)
154
+ if (tabWidth > 0f) {
155
+ updateValue(
156
+ (targetValue + dragAmount.x / tabWidth * if (isLtr) 1f else -1f)
157
+ .fastCoerceIn(0f, (tabsCount - 1).toFloat())
158
+ )
159
+ }
160
+ animationScope.launch {
161
+ offsetAnimation.snapTo(offsetAnimation.value + dragAmount.x)
162
+ }
163
+ }
164
+ )
165
+ }
166
+ LaunchedEffect(selectedTabIndex) {
167
+ snapshotFlow { selectedTabIndex() }
168
+ .collectLatest { index ->
169
+ currentIndex = index
170
+ }
171
+ }
172
+ LaunchedEffect(dampedDragAnimation) {
173
+ snapshotFlow { currentIndex }
174
+ .drop(1)
175
+ .collectLatest { index ->
176
+ dampedDragAnimation.animateToValue(index.toFloat())
177
+ onTabSelected(index)
178
+ }
179
+ }
180
+
181
+ val interactiveHighlight = remember(animationScope, dampedDragAnimation) {
182
+ InteractiveHighlight(
183
+ animationScope = animationScope,
184
+ position = { size, _ ->
185
+ val tabWidth = geometry.tabWidth(minimizeAnimation.value)
186
+ Offset(
187
+ if (isLtr) (dampedDragAnimation.value + 0.5f) * tabWidth + panelOffset
188
+ else size.width - (dampedDragAnimation.value + 0.5f) * tabWidth + panelOffset,
189
+ size.height / 2f
190
+ )
191
+ }
192
+ )
193
+ }
194
+
195
+ // The bar: glass + the tab content.
196
+ Box(
197
+ Modifier
198
+ .placeAt { geometry.barBounds(minimizeAnimation.value) }
199
+ .graphicsLayer {
200
+ translationX = panelOffset
201
+ }
202
+ .drawBackdrop(
203
+ backdrop = backdrop,
204
+ shape = { CapsuleShape },
205
+ effects = {
206
+ val sizeScale = geometry.sizeScale(minimizeAnimation.value)
207
+ vibrancy()
208
+ blur(8f.dp.toPx())
209
+ lens(24f.dp.toPx() * sizeScale, 24f.dp.toPx() * sizeScale)
210
+ },
211
+ layerBlock = {
212
+ val progress = dampedDragAnimation.pressProgress
213
+ val scale = lerp(1f, 1f + 16f.dp.toPx() / size.width, progress)
214
+ scaleX = scale
215
+ scaleY = scale
216
+ },
217
+ onDrawSurface = { drawRect(container) },
218
+ onDrawFront = {
219
+ val progress = minimizeAnimation.value
220
+ drawTabs(
221
+ Offset(geometry.inset(progress), geometry.barTop(progress)),
222
+ geometry.frame(progress),
223
+ null,
224
+ 1f
225
+ )
226
+ }
227
+ )
228
+ .then(interactiveHighlight.modifier)
229
+ // A tap selects the tab under the finger, exactly like upstream's tab click. Unlike
230
+ // upstream (where only the droplet can be dragged), a horizontal drag that starts
231
+ // anywhere on the bar pulls the droplet under the finger and scrubs between tabs;
232
+ // releasing selects the nearest one.
233
+ .pointerInput(dampedDragAnimation, tabsCount, isLtr, geometry) {
234
+ val lastIndex = (tabsCount - 1).toFloat()
235
+ // x is in the bar's own coordinates, which follow the minimize animation.
236
+ val valueAt: (Float) -> Float = { x ->
237
+ val tabWidth = geometry.tabWidth(minimizeAnimation.value)
238
+ val slot =
239
+ if (tabWidth > 0f) ((x - geometry.padding) / tabWidth - 0.5f).fastCoerceIn(0f, lastIndex)
240
+ else 0f
241
+ if (isLtr) slot else lastIndex - slot
242
+ }
243
+ var startX = 0f
244
+ var scrubbing = false
245
+ var scrubValue = 0f
246
+ val settle: () -> Unit = {
247
+ val target = scrubValue.fastRoundToInt().fastCoerceIn(0, tabsCount - 1)
248
+ if (target != currentIndex) {
249
+ currentIndex = target
250
+ } else {
251
+ dampedDragAnimation.animateToValue(target.toFloat())
252
+ }
253
+ }
254
+ inspectDragGestures(
255
+ onDragStart = { down ->
256
+ startX = down.position.x
257
+ scrubbing = false
258
+ },
259
+ onDragEnd = { up ->
260
+ if (scrubbing) {
261
+ settle()
262
+ } else {
263
+ currentIndex = valueAt(up.position.x).fastRoundToInt()
264
+ .fastCoerceIn(0, tabsCount - 1)
265
+ currentOnExpand()
266
+ }
267
+ scrubbing = false
268
+ },
269
+ onDragCancel = {
270
+ if (scrubbing) settle()
271
+ scrubbing = false
272
+ }
273
+ ) { change, _ ->
274
+ if (scrubbing) {
275
+ scrubValue = valueAt(change.position.x)
276
+ dampedDragAnimation.updateValue(scrubValue)
277
+ } else if (abs(change.position.x - startX) > viewConfiguration.touchSlop) {
278
+ scrubbing = true
279
+ currentOnExpand()
280
+ scrubValue = valueAt(change.position.x)
281
+ dampedDragAnimation.press()
282
+ // Jump to the finger without feeding the velocity (no stretch).
283
+ dampedDragAnimation.moveToValue(scrubValue)
284
+ }
285
+ }
286
+ }
287
+ )
288
+
289
+ // Invisible copy of the bar with the content tinted in the accent colour. It is only
290
+ // recorded into `tabsBackdrop`, which the droplet refracts: under the droplet the tabs
291
+ // show up in the accent colour, magnified while it is pressed.
292
+ Box(
293
+ Modifier
294
+ .placeAt { geometry.innerBounds(minimizeAnimation.value) }
295
+ .clearAndSetSemantics {}
296
+ .alpha(0f)
297
+ .layerBackdrop(tabsBackdrop)
298
+ .graphicsLayer {
299
+ translationX = panelOffset
300
+ }
301
+ .drawBackdrop(
302
+ backdrop = backdrop,
303
+ shape = { CapsuleShape },
304
+ effects = {
305
+ val progress = dampedDragAnimation.pressProgress
306
+ val sizeScale = geometry.sizeScale(minimizeAnimation.value)
307
+ vibrancy()
308
+ blur(8f.dp.toPx())
309
+ lens(
310
+ 24f.dp.toPx() * progress * sizeScale,
311
+ 24f.dp.toPx() * progress * sizeScale
312
+ )
313
+ },
314
+ highlight = {
315
+ val progress = dampedDragAnimation.pressProgress
316
+ Highlight.Default.copy(alpha = progress)
317
+ },
318
+ onDrawSurface = { drawRect(container) },
319
+ onDrawFront = {
320
+ val progress = minimizeAnimation.value
321
+ drawTabs(
322
+ Offset(geometry.inset(progress), geometry.barTop(progress) + geometry.padding),
323
+ geometry.frame(progress),
324
+ accent,
325
+ lerp(1f, 1.2f, dampedDragAnimation.pressProgress)
326
+ )
327
+ }
328
+ )
329
+ .then(interactiveHighlight.modifier)
330
+ )
331
+
332
+ // The droplet: selection indicator, draggable.
333
+ Box(
334
+ Modifier
335
+ .placeAt { geometry.dropletBounds(minimizeAnimation.value) }
336
+ .graphicsLayer {
337
+ val tabWidth = geometry.tabWidth(minimizeAnimation.value)
338
+ translationX =
339
+ if (isLtr) dampedDragAnimation.value * tabWidth + panelOffset
340
+ else (tabsCount - 1 - dampedDragAnimation.value) * tabWidth + panelOffset
341
+ }
342
+ .then(interactiveHighlight.gestureModifier)
343
+ .then(dampedDragAnimation.modifier)
344
+ .drawBackdrop(
345
+ backdrop = rememberCombinedBackdrop(backdrop, tabsBackdrop),
346
+ shape = { CapsuleShape },
347
+ effects = {
348
+ val progress = dampedDragAnimation.pressProgress
349
+ val sizeScale = geometry.sizeScale(minimizeAnimation.value)
350
+ lens(
351
+ 10f.dp.toPx() * progress * sizeScale,
352
+ 14f.dp.toPx() * progress * sizeScale,
353
+ chromaticAberration = true
354
+ )
355
+ },
356
+ highlight = {
357
+ val progress = dampedDragAnimation.pressProgress
358
+ Highlight.Default.copy(alpha = progress)
359
+ },
360
+ shadow = {
361
+ val progress = dampedDragAnimation.pressProgress
362
+ Shadow(alpha = progress)
363
+ },
364
+ innerShadow = {
365
+ val progress = dampedDragAnimation.pressProgress
366
+ InnerShadow(
367
+ radius = 8f.dp * progress,
368
+ alpha = progress
369
+ )
370
+ },
371
+ layerBlock = {
372
+ scaleX = dampedDragAnimation.scaleX
373
+ scaleY = dampedDragAnimation.scaleY
374
+ val velocity = dampedDragAnimation.velocity / 10f
375
+ scaleX /= 1f - (velocity * 0.75f).fastCoerceIn(-0.2f, 0.2f)
376
+ scaleY *= 1f - (velocity * 0.25f).fastCoerceIn(-0.2f, 0.2f)
377
+ },
378
+ onDrawSurface = {
379
+ val progress = dampedDragAnimation.pressProgress
380
+ drawRect(
381
+ if (isLightTheme) Color.Black.copy(0.1f)
382
+ else Color.White.copy(0.1f),
383
+ alpha = 1f - progress
384
+ )
385
+ drawRect(Color.Black.copy(alpha = 0.03f * progress))
386
+ }
387
+ )
388
+ )
389
+ }
390
+ }
391
+
392
+ private val BarPadding = 4.dp
393
+
394
+ /** Minimized bar, like iOS 26 tab bars on scroll: shorter and narrower. */
395
+ private val MinimizedHeight = 48.dp
396
+ private val MinimizedInset = 34.dp
397
+
398
+ /** The minimized bar never squeezes a tab below this width. */
399
+ private val MinimizedMinTabWidth = 48.dp
400
+
401
+ /** Critically damped: a direction change mid-animation retargets smoothly, without overshoot. */
402
+ private val MinimizeAnimationSpec = spring(dampingRatio = 1f, stiffness = 600f, visibilityThreshold = 0.001f)
403
+
404
+ /** The bar's layout as a function of the minimize progress, in px and host coordinates. */
405
+ private class TabsGeometry(density: Density, val width: Float, val height: Float, val tabsCount: Int) {
406
+
407
+ val padding: Float
408
+ private val minimizedHeight: Float
409
+ private val minimizedInset: Float
410
+
411
+ init {
412
+ with(density) {
413
+ padding = BarPadding.toPx()
414
+ minimizedHeight = min(height, MinimizedHeight.toPx())
415
+ minimizedInset = MinimizedInset.toPx()
416
+ .coerceAtMost(((width - tabsCount * MinimizedMinTabWidth.toPx()) / 2f).coerceAtLeast(0f))
417
+ }
418
+ }
419
+
420
+ fun barHeight(progress: Float): Float = lerp(height, minimizedHeight, progress)
421
+
422
+ fun inset(progress: Float): Float = lerp(0f, minimizedInset, progress)
423
+
424
+ // Bottom-anchored: a minimizing bar sinks towards the edge of the screen.
425
+ fun barTop(progress: Float): Float = height - barHeight(progress)
426
+
427
+ fun tabWidth(progress: Float): Float =
428
+ ((width - inset(progress) * 2f - padding * 2f) / tabsCount).coerceAtLeast(0f)
429
+
430
+ fun slotsLeft(progress: Float): Float = inset(progress) + padding
431
+
432
+ /** Refraction sizes shrink with the bar. */
433
+ fun sizeScale(progress: Float): Float = if (height > 0f) barHeight(progress) / height else 1f
434
+
435
+ fun barBounds(progress: Float): Rect =
436
+ Rect(inset(progress), barTop(progress), width - inset(progress), height)
437
+
438
+ fun innerBounds(progress: Float): Rect =
439
+ Rect(inset(progress), barTop(progress) + padding, width - inset(progress), height - padding)
440
+
441
+ fun dropletBounds(progress: Float): Rect {
442
+ val left = slotsLeft(progress)
443
+ return Rect(left, barTop(progress) + padding, left + tabWidth(progress), height - padding)
444
+ }
445
+
446
+ fun frame(progress: Float): TabsFrame {
447
+ val expandedTabWidth = tabWidth(0f)
448
+ return TabsFrame(
449
+ minimizeProgress = progress,
450
+ expandedSlotsLeft = slotsLeft(0f),
451
+ slotsLeft = slotsLeft(progress),
452
+ slotsScale = if (expandedTabWidth > 0f) tabWidth(progress) / expandedTabWidth else 1f,
453
+ centerY = barTop(progress) + barHeight(progress) / 2f
454
+ )
455
+ }
456
+ }
457
+
458
+ /** Lays the node out at [bounds]; read while laying out, so animating them only re-lays it out. */
459
+ private fun Modifier.placeAt(bounds: () -> Rect): Modifier =
460
+ layout { measurable, constraints ->
461
+ val rect = bounds()
462
+ val placeable = measurable.measure(
463
+ Constraints.fixed(
464
+ rect.width.fastRoundToInt().coerceAtLeast(0),
465
+ rect.height.fastRoundToInt().coerceAtLeast(0)
466
+ )
467
+ )
468
+ layout(constraints.maxWidth, constraints.maxHeight) {
469
+ placeable.place(rect.left.fastRoundToInt(), rect.top.fastRoundToInt())
470
+ }
471
+ }
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Copyright 2025 Kyant (https://github.com/Kyant0/AndroidLiquidGlass)
3
+ * Licensed under the Apache License, Version 2.0. See THIRD_PARTY_NOTICES.md.
4
+ *
5
+ * From the Backdrop Catalog sample app. Changes from upstream: package relocated, made internal,
6
+ * glass parameters come from GlassState, the Compose content slot became a draw callback (the
7
+ * React Native children), sizing is left to React Native.
8
+ */
9
+ package expo.modules.androidglassview.components
10
+
11
+ import androidx.compose.foundation.LocalIndication
12
+ import androidx.compose.foundation.clickable
13
+ import androidx.compose.foundation.layout.Box
14
+ import androidx.compose.foundation.shape.RoundedCornerShape
15
+ import androidx.compose.runtime.Composable
16
+ import androidx.compose.runtime.remember
17
+ import androidx.compose.runtime.rememberCoroutineScope
18
+ import androidx.compose.ui.Modifier
19
+ import androidx.compose.ui.graphics.drawscope.DrawScope
20
+ import androidx.compose.ui.semantics.Role
21
+ import androidx.compose.ui.unit.dp
22
+ import androidx.compose.ui.util.fastCoerceAtMost
23
+ import androidx.compose.ui.util.lerp
24
+ import expo.modules.androidglassview.GlassState
25
+ import expo.modules.androidglassview.backdrop.Backdrop
26
+ import expo.modules.androidglassview.backdrop.drawBackdrop
27
+ import expo.modules.androidglassview.backdrop.highlight.Highlight
28
+ import expo.modules.androidglassview.backdrop.isRenderEffectSupported
29
+ import expo.modules.androidglassview.backdrop.shadow.Shadow
30
+ import expo.modules.androidglassview.glassEffects
31
+ import expo.modules.androidglassview.glassSurface
32
+ import kotlin.math.abs
33
+ import kotlin.math.atan2
34
+ import kotlin.math.cos
35
+ import kotlin.math.sin
36
+ import kotlin.math.tanh
37
+
38
+ @Composable
39
+ internal fun LiquidButton(
40
+ onClick: () -> Unit,
41
+ backdrop: Backdrop,
42
+ state: GlassState,
43
+ isInteractive: Boolean,
44
+ drawContent: DrawScope.() -> Unit,
45
+ modifier: Modifier = Modifier
46
+ ) {
47
+ val animationScope = rememberCoroutineScope()
48
+ val effectsSupported = isRenderEffectSupported()
49
+
50
+ val interactiveHighlight = remember(animationScope) {
51
+ InteractiveHighlight(
52
+ animationScope = animationScope
53
+ )
54
+ }
55
+
56
+ Box(
57
+ modifier
58
+ .drawBackdrop(
59
+ backdrop = backdrop,
60
+ shape = { RoundedCornerShape(state.cornerRadius.dp) },
61
+ effects = { glassEffects(state) },
62
+ highlight = { if (state.highlight) Highlight.Default else null },
63
+ shadow = { if (state.shadow) Shadow.Default else null },
64
+ layerBlock = if (isInteractive) {
65
+ {
66
+ val width = size.width
67
+ val height = size.height
68
+
69
+ val progress = interactiveHighlight.pressProgress
70
+ val scale = lerp(1f, 1f + 4f.dp.toPx() / size.height, progress)
71
+
72
+ val maxOffset = size.minDimension
73
+ val initialDerivative = 0.05f
74
+ val offset = interactiveHighlight.offset
75
+ translationX = maxOffset * tanh(initialDerivative * offset.x / maxOffset)
76
+ translationY = maxOffset * tanh(initialDerivative * offset.y / maxOffset)
77
+
78
+ val maxDragScale = 4f.dp.toPx() / size.height
79
+ val offsetAngle = atan2(offset.y, offset.x)
80
+ scaleX =
81
+ scale +
82
+ maxDragScale * abs(cos(offsetAngle) * offset.x / size.maxDimension) *
83
+ (width / height).fastCoerceAtMost(1f)
84
+ scaleY =
85
+ scale +
86
+ maxDragScale * abs(sin(offsetAngle) * offset.y / size.maxDimension) *
87
+ (height / width).fastCoerceAtMost(1f)
88
+ }
89
+ } else {
90
+ null
91
+ },
92
+ onDrawSurface = { glassSurface(state, effectsSupported) },
93
+ onDrawFront = drawContent
94
+ )
95
+ .clickable(
96
+ interactionSource = null,
97
+ indication = if (isInteractive) null else LocalIndication.current,
98
+ role = Role.Button,
99
+ onClick = onClick
100
+ )
101
+ .then(
102
+ if (isInteractive) {
103
+ Modifier
104
+ .then(interactiveHighlight.modifier)
105
+ .then(interactiveHighlight.gestureModifier)
106
+ } else {
107
+ Modifier
108
+ }
109
+ )
110
+ )
111
+ }