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,346 @@
1
+ package expo.modules.androidglassview
2
+
3
+ import android.annotation.SuppressLint
4
+ import android.content.Context
5
+ import android.graphics.Canvas
6
+ import android.graphics.Paint
7
+ import android.graphics.PorterDuff
8
+ import android.graphics.PorterDuffColorFilter
9
+ import android.graphics.Rect
10
+ import android.view.MotionEvent
11
+ import android.view.View
12
+ import android.view.ViewConfiguration
13
+ import android.view.ViewTreeObserver
14
+ import androidx.compose.runtime.Composable
15
+ import androidx.compose.runtime.getValue
16
+ import androidx.compose.runtime.mutableIntStateOf
17
+ import androidx.compose.runtime.setValue
18
+ import androidx.compose.ui.geometry.Offset
19
+ import androidx.compose.ui.graphics.Color
20
+ import androidx.compose.ui.graphics.drawscope.DrawScope
21
+ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
22
+ import androidx.compose.ui.graphics.nativeCanvas
23
+ import androidx.compose.ui.graphics.toArgb
24
+ import androidx.compose.ui.platform.ComposeView
25
+ import androidx.lifecycle.LifecycleOwner
26
+ import androidx.lifecycle.findViewTreeLifecycleOwner
27
+ import androidx.lifecycle.setViewTreeLifecycleOwner
28
+ import androidx.savedstate.SavedStateRegistryOwner
29
+ import androidx.savedstate.findViewTreeSavedStateRegistryOwner
30
+ import androidx.savedstate.setViewTreeSavedStateRegistryOwner
31
+ import com.facebook.react.uimanager.events.NativeGestureUtil
32
+ import expo.modules.kotlin.AppContext
33
+ import expo.modules.kotlin.views.ExpoView
34
+ import kotlin.math.abs
35
+
36
+ /**
37
+ * Base class of every glass view: a React Native view whose first child is a [ComposeView]
38
+ * rendering Kyant's liquid glass, with the React Native screen behind it as the backdrop.
39
+ *
40
+ * React children come after the Compose layer. React Native positions them itself, so this view
41
+ * only ever lays out the Compose layer.
42
+ */
43
+ @SuppressLint("ViewConstructor")
44
+ abstract class GlassHostView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
45
+
46
+ /** Samples the window behind this view. One instance can serve every glass node of the view. */
47
+ internal val backdrop = ViewBackdrop(this)
48
+
49
+ /** The view the glass samples: the root of the window it is attached to. */
50
+ internal val backdropSource: View?
51
+ get() = if (isAttachedToWindow) rootView else null
52
+
53
+ /**
54
+ * When true, React children are not drawn by the view system. The Compose content draws them
55
+ * instead (see [drawReactChildren]), so they live *inside* the glass: clipped to its shape and
56
+ * transformed together with it (press / drag feedback).
57
+ */
58
+ protected open val drawsReactChildrenInCompose: Boolean = false
59
+
60
+ /** When true, a horizontal drag that starts on this view is claimed from scrolling parents. */
61
+ protected open val claimsHorizontalDrags: Boolean = false
62
+
63
+ /** Bumped whenever the React children may look different; read it while drawing them. */
64
+ internal var reactChildrenGeneration by mutableIntStateOf(0)
65
+ private set
66
+
67
+ // Not named `Content`: inside `apply`, that would resolve to ComposeView.Content(), which
68
+ // invokes this very lambda again (infinite recursion).
69
+ private val composeView = ComposeView(context).apply {
70
+ setContent { GlassContent() }
71
+ }
72
+
73
+ private val lastLocation = intArrayOf(Int.MIN_VALUE, Int.MIN_VALUE)
74
+ private val location = IntArray(2)
75
+ private val visibleRect = Rect()
76
+ private var observer: ViewTreeObserver? = null
77
+ private var layoutPending = false
78
+ private var resamplePending = false
79
+
80
+ private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
81
+ private var downX = 0f
82
+ private var downY = 0f
83
+ private var dragClaimed = false
84
+
85
+ // Re-sample when the glass moves on screen (layout, an ancestor scrolling, transforms).
86
+ private val preDrawListener = ViewTreeObserver.OnPreDrawListener {
87
+ getLocationOnScreen(location)
88
+ if (location[0] != lastLocation[0] || location[1] != lastLocation[1]) {
89
+ lastLocation[0] = location[0]
90
+ lastLocation[1] = location[1]
91
+ resampleIfVisible()
92
+ } else if (resamplePending) {
93
+ resampleIfVisible()
94
+ }
95
+ true
96
+ }
97
+
98
+ // Scrolling changes the containers that are drawn inline into the backdrop (the ones between
99
+ // the window root and a glass view), so re-sample. There is deliberately no global-layout
100
+ // listener: React Native lays out on every state change (e.g. each slider tick updating a
101
+ // label), and re-sampling every glass view each time made interactions stutter. Everything
102
+ // else behind the glass is referenced live (see ViewBackdropCapture).
103
+ private val scrollListener = ViewTreeObserver.OnScrollChangedListener { resampleIfVisible() }
104
+
105
+ /**
106
+ * Re-samples now if any part of this view is on screen. Sampling costs a capture plus the
107
+ * glass effects on the GPU, so glass scrolled out of view waits until it is visible again.
108
+ */
109
+ private fun resampleIfVisible() {
110
+ if (isShown && getGlobalVisibleRect(visibleRect)) {
111
+ resamplePending = false
112
+ backdrop.invalidate()
113
+ } else {
114
+ resamplePending = true
115
+ }
116
+ }
117
+
118
+ init {
119
+ clipChildren = false
120
+ clipToPadding = false
121
+ composeView.clipChildren = false
122
+ super.addView(composeView, 0, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
123
+ }
124
+
125
+ @Composable
126
+ protected abstract fun GlassContent()
127
+
128
+ internal fun invalidateBackdrop() {
129
+ backdrop.invalidate()
130
+ }
131
+
132
+ // region React children (offset by the Compose layer at index 0)
133
+
134
+ internal val reactChildCount: Int
135
+ get() = childCount - 1
136
+
137
+ internal fun getReactChildAt(index: Int): View? = getChildAt(index + 1)
138
+
139
+ internal fun addReactChild(child: View, index: Int) {
140
+ addView(child, index + 1)
141
+ }
142
+
143
+ internal fun removeReactChildAt(index: Int) {
144
+ removeViewAt(index + 1)
145
+ }
146
+
147
+ internal fun removeReactChild(child: View) {
148
+ removeView(child)
149
+ }
150
+
151
+ override fun onViewAdded(child: View?) {
152
+ super.onViewAdded(child)
153
+ if (child !== composeView) reactChildrenGeneration++
154
+ }
155
+
156
+ override fun onViewRemoved(child: View?) {
157
+ super.onViewRemoved(child)
158
+ if (child !== composeView) reactChildrenGeneration++
159
+ }
160
+
161
+ /**
162
+ * Draws the React children into [canvas], positioned like the view system would draw them.
163
+ * [scale] additionally scales each child around its own centre.
164
+ */
165
+ internal fun drawReactChildrenInto(canvas: Canvas, scale: Float) {
166
+ for (i in 1 until childCount) {
167
+ val child = getChildAt(i)
168
+ if (child.visibility != View.VISIBLE) continue
169
+ val width = child.width.toFloat()
170
+ val height = child.height.toFloat()
171
+ val save = canvas.save()
172
+ canvas.translate((child.left - scrollX).toFloat(), (child.top - scrollY).toFloat())
173
+ val matrix = child.matrix
174
+ if (!matrix.isIdentity) canvas.concat(matrix)
175
+ if (scale != 1f) canvas.scale(scale, scale, width / 2f, height / 2f)
176
+ val alpha = child.alpha
177
+ if (alpha < 1f) canvas.saveLayerAlpha(0f, 0f, width, height, (alpha * 255f).toInt())
178
+ child.draw(canvas)
179
+ canvas.restoreToCount(save)
180
+ }
181
+ }
182
+
183
+ override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
184
+ if (drawsReactChildrenInCompose && child !== composeView) return false
185
+ return super.drawChild(canvas, child, drawingTime)
186
+ }
187
+
188
+ override fun onDescendantInvalidated(child: View, target: View) {
189
+ super.onDescendantInvalidated(child, target)
190
+ // React children drawn by Compose are not redrawn by the view system; tell Compose instead.
191
+ if (drawsReactChildrenInCompose && child !== composeView) reactChildrenGeneration++
192
+ }
193
+
194
+ // endregion
195
+
196
+ // region Layout
197
+
198
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
199
+ val width = MeasureSpec.getSize(widthMeasureSpec)
200
+ val height = MeasureSpec.getSize(heightMeasureSpec)
201
+ setMeasuredDimension(width, height)
202
+ composeView.measure(
203
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
204
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
205
+ )
206
+ }
207
+
208
+ override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
209
+ // React Native lays out the React children; only the glass layer is ours.
210
+ composeView.layout(0, 0, right - left, bottom - top)
211
+ }
212
+
213
+ override fun requestLayout() {
214
+ super.requestLayout()
215
+ // React Native does not run Android layout passes, so honour the Compose layer's
216
+ // layout requests ourselves.
217
+ if (isAttachedToWindow && !layoutPending) {
218
+ layoutPending = true
219
+ post {
220
+ layoutPending = false
221
+ measureAndLayout()
222
+ }
223
+ }
224
+ }
225
+
226
+ // endregion
227
+
228
+ // region Touch
229
+
230
+ override fun dispatchTouchEvent(event: MotionEvent): Boolean {
231
+ if (claimsHorizontalDrags) trackHorizontalDrag(event)
232
+ return super.dispatchTouchEvent(event)
233
+ }
234
+
235
+ /**
236
+ * Once a drag on this view turns out to be horizontal, stop scrolling parents from stealing it
237
+ * and tell React Native's JS responder system that a native gesture took over.
238
+ */
239
+ private fun trackHorizontalDrag(event: MotionEvent) {
240
+ when (event.actionMasked) {
241
+ MotionEvent.ACTION_DOWN -> {
242
+ downX = event.x
243
+ downY = event.y
244
+ dragClaimed = false
245
+ }
246
+ MotionEvent.ACTION_MOVE -> if (!dragClaimed) {
247
+ val dx = abs(event.x - downX)
248
+ val dy = abs(event.y - downY)
249
+ if (dx > touchSlop && dx > dy) {
250
+ dragClaimed = true
251
+ parent?.requestDisallowInterceptTouchEvent(true)
252
+ NativeGestureUtil.notifyNativeGestureStarted(this, event)
253
+ }
254
+ }
255
+ MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
256
+ if (dragClaimed) NativeGestureUtil.notifyNativeGestureEnded(this, event)
257
+ dragClaimed = false
258
+ }
259
+ }
260
+ }
261
+
262
+ // endregion
263
+
264
+ // region Attach / detach
265
+
266
+ override fun onAttachedToWindow() {
267
+ ensureViewTreeOwners()
268
+ super.onAttachedToWindow()
269
+ observer = viewTreeObserver.also {
270
+ it.addOnPreDrawListener(preDrawListener)
271
+ it.addOnScrollChangedListener(scrollListener)
272
+ }
273
+ GlassRegistry.add(this)
274
+ }
275
+
276
+ override fun onDetachedFromWindow() {
277
+ observer?.takeIf { it.isAlive }?.let {
278
+ it.removeOnPreDrawListener(preDrawListener)
279
+ it.removeOnScrollChangedListener(scrollListener)
280
+ }
281
+ observer = null
282
+ lastLocation[0] = Int.MIN_VALUE
283
+ lastLocation[1] = Int.MIN_VALUE
284
+ GlassRegistry.remove(this)
285
+ backdrop.release()
286
+ super.onDetachedFromWindow()
287
+ }
288
+
289
+ /**
290
+ * ComposeView needs a lifecycle and saved-state owner on the view tree. Activity windows have
291
+ * them; a window without them (e.g. a plain Dialog used by React Native's Modal) would crash,
292
+ * so fall back to the current activity.
293
+ */
294
+ private fun ensureViewTreeOwners() {
295
+ val activity = appContext.currentActivity ?: return
296
+ if (findViewTreeLifecycleOwner() == null && activity is LifecycleOwner) {
297
+ setViewTreeLifecycleOwner(activity)
298
+ }
299
+ if (findViewTreeSavedStateRegistryOwner() == null && activity is SavedStateRegistryOwner) {
300
+ setViewTreeSavedStateRegistryOwner(activity)
301
+ }
302
+ }
303
+
304
+ // endregion
305
+ }
306
+
307
+ private val tintPaint = Paint()
308
+
309
+ /**
310
+ * Draws [host]'s React children from inside a Compose draw scope.
311
+ *
312
+ * @param origin position of the current draw scope inside the host, so children land where
313
+ * React Native placed them.
314
+ * @param tint when set, the children are drawn as a silhouette in this colour (SrcIn).
315
+ * @param scale scales every child around its own centre.
316
+ */
317
+ internal fun DrawScope.drawReactChildren(
318
+ host: GlassHostView,
319
+ origin: Offset = Offset.Zero,
320
+ tint: Color? = null,
321
+ scale: Float = 1f
322
+ ) {
323
+ // Reading the generation subscribes this draw to changes of the React children.
324
+ if (host.reactChildrenGeneration < 0) return
325
+ drawReactLayer(origin, tint) { canvas -> host.drawReactChildrenInto(canvas, scale) }
326
+ }
327
+
328
+ /**
329
+ * Runs [draw] on the native canvas of a Compose draw scope, in the host's coordinates.
330
+ *
331
+ * @param origin position of the current draw scope inside the host.
332
+ * @param tint when set, whatever [draw] draws becomes a silhouette in this colour (SrcIn).
333
+ */
334
+ internal fun DrawScope.drawReactLayer(origin: Offset, tint: Color?, draw: (Canvas) -> Unit) {
335
+ drawIntoCanvas { canvas ->
336
+ val nativeCanvas = canvas.nativeCanvas
337
+ val save = nativeCanvas.save()
338
+ nativeCanvas.translate(-origin.x, -origin.y)
339
+ if (tint != null) {
340
+ tintPaint.colorFilter = PorterDuffColorFilter(tint.toArgb(), PorterDuff.Mode.SRC_IN)
341
+ nativeCanvas.saveLayer(null, tintPaint)
342
+ }
343
+ draw(nativeCanvas)
344
+ nativeCanvas.restoreToCount(save)
345
+ }
346
+ }
@@ -0,0 +1,31 @@
1
+ package expo.modules.androidglassview
2
+
3
+ import android.view.View
4
+
5
+ /**
6
+ * Every glass view currently attached to a window. A backdrop leaves the others out (see
7
+ * ViewBackdropCapture), so adding or removing one changes what the others sample.
8
+ *
9
+ * Main thread only.
10
+ */
11
+ internal object GlassRegistry {
12
+
13
+ private val attached = LinkedHashSet<GlassHostView>()
14
+
15
+ val views: Collection<View>
16
+ get() = attached
17
+
18
+ fun add(view: GlassHostView) {
19
+ if (attached.add(view)) invalidateAll()
20
+ }
21
+
22
+ fun remove(view: GlassHostView) {
23
+ if (attached.remove(view)) invalidateAll()
24
+ }
25
+
26
+ private fun invalidateAll() {
27
+ for (view in attached) {
28
+ view.invalidateBackdrop()
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,96 @@
1
+ package expo.modules.androidglassview
2
+
3
+ import androidx.compose.foundation.layout.Box
4
+ import androidx.compose.foundation.layout.fillMaxSize
5
+ import androidx.compose.foundation.shape.RoundedCornerShape
6
+ import androidx.compose.runtime.Composable
7
+ import androidx.compose.runtime.getValue
8
+ import androidx.compose.runtime.mutableFloatStateOf
9
+ import androidx.compose.runtime.mutableStateOf
10
+ import androidx.compose.runtime.setValue
11
+ import androidx.compose.ui.Modifier
12
+ import androidx.compose.ui.graphics.BlendMode
13
+ import androidx.compose.ui.graphics.Color
14
+ import androidx.compose.ui.graphics.drawscope.DrawScope
15
+ import androidx.compose.ui.graphics.isSpecified
16
+ import androidx.compose.ui.unit.dp
17
+ import expo.modules.androidglassview.backdrop.Backdrop
18
+ import expo.modules.androidglassview.backdrop.BackdropEffectScope
19
+ import expo.modules.androidglassview.backdrop.drawBackdrop
20
+ import expo.modules.androidglassview.backdrop.effects.blur
21
+ import expo.modules.androidglassview.backdrop.effects.lens
22
+ import expo.modules.androidglassview.backdrop.effects.vibrancy
23
+ import expo.modules.androidglassview.backdrop.highlight.Highlight
24
+ import expo.modules.androidglassview.backdrop.isRenderEffectSupported
25
+ import expo.modules.androidglassview.backdrop.shadow.Shadow
26
+
27
+ /** Glass parameters set from React props. Lengths are in dp, like React Native styles. */
28
+ internal class GlassState {
29
+ var cornerRadius by mutableFloatStateOf(DEFAULT_CORNER_RADIUS)
30
+ var blurRadius by mutableFloatStateOf(DEFAULT_BLUR_RADIUS)
31
+ var refractionHeight by mutableFloatStateOf(DEFAULT_REFRACTION_HEIGHT)
32
+ var refractionAmount by mutableFloatStateOf(DEFAULT_REFRACTION_AMOUNT)
33
+ var chromaticAberration by mutableStateOf(false)
34
+ var depthEffect by mutableStateOf(false)
35
+ var vibrancy by mutableStateOf(true)
36
+ var highlight by mutableStateOf(true)
37
+ var shadow by mutableStateOf(true)
38
+ var tintColor by mutableStateOf(Color.Unspecified)
39
+ var surfaceColor by mutableStateOf(Color.Unspecified)
40
+ var fallbackColor by mutableStateOf(DEFAULT_FALLBACK_COLOR)
41
+
42
+ companion object {
43
+ // Same defaults as Kyant's LiquidButton sample.
44
+ const val DEFAULT_CORNER_RADIUS = 24f
45
+ const val DEFAULT_BLUR_RADIUS = 2f
46
+ const val DEFAULT_REFRACTION_HEIGHT = 12f
47
+ const val DEFAULT_REFRACTION_AMOUNT = 24f
48
+ val DEFAULT_FALLBACK_COLOR = Color(0xB3FFFFFF)
49
+ }
50
+ }
51
+
52
+ /** Vibrancy, blur and lens as configured by [state]. */
53
+ internal fun BackdropEffectScope.glassEffects(state: GlassState) {
54
+ if (state.vibrancy) vibrancy()
55
+ val blurRadius = state.blurRadius
56
+ if (blurRadius > 0f) blur(blurRadius.dp.toPx())
57
+ val refractionHeight = state.refractionHeight
58
+ val refractionAmount = state.refractionAmount
59
+ if (refractionHeight > 0f && refractionAmount > 0f) {
60
+ lens(
61
+ refractionHeight = refractionHeight.dp.toPx(),
62
+ refractionAmount = refractionAmount.dp.toPx(),
63
+ depthEffect = state.depthEffect,
64
+ chromaticAberration = state.chromaticAberration
65
+ )
66
+ }
67
+ }
68
+
69
+ /** Fallback, tint and surface colours painted over the backdrop, as configured by [state]. */
70
+ internal fun DrawScope.glassSurface(state: GlassState, effectsSupported: Boolean) {
71
+ if (!effectsSupported) drawRect(state.fallbackColor)
72
+ val tint = state.tintColor
73
+ if (tint.isSpecified) {
74
+ drawRect(tint, blendMode = BlendMode.Hue)
75
+ drawRect(tint.copy(alpha = 0.75f * tint.alpha))
76
+ }
77
+ val surface = state.surfaceColor
78
+ if (surface.isSpecified) drawRect(surface)
79
+ }
80
+
81
+ @Composable
82
+ internal fun GlassSurface(state: GlassState, backdrop: Backdrop) {
83
+ val effectsSupported = isRenderEffectSupported()
84
+ Box(
85
+ Modifier
86
+ .fillMaxSize()
87
+ .drawBackdrop(
88
+ backdrop = backdrop,
89
+ shape = { RoundedCornerShape(state.cornerRadius.dp) },
90
+ effects = { glassEffects(state) },
91
+ highlight = { if (state.highlight) Highlight.Default else null },
92
+ shadow = { if (state.shadow) Shadow.Default else null },
93
+ onDrawSurface = { glassSurface(state, effectsSupported) }
94
+ )
95
+ )
96
+ }
@@ -0,0 +1,151 @@
1
+ package expo.modules.androidglassview
2
+
3
+ import android.graphics.RenderNode
4
+ import android.os.Build
5
+ import androidx.compose.runtime.getValue
6
+ import androidx.compose.runtime.mutableIntStateOf
7
+ import androidx.compose.runtime.setValue
8
+ import androidx.compose.runtime.snapshots.Snapshot
9
+ import androidx.compose.ui.geometry.Offset
10
+ import androidx.compose.ui.graphics.GraphicsLayerScope
11
+ import androidx.compose.ui.graphics.drawscope.DrawScope
12
+ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
13
+ import androidx.compose.ui.graphics.drawscope.withTransform
14
+ import androidx.compose.ui.graphics.nativeCanvas
15
+ import androidx.compose.ui.layout.LayoutCoordinates
16
+ import androidx.compose.ui.layout.positionInRoot
17
+ import androidx.compose.ui.unit.Density
18
+ import expo.modules.androidglassview.backdrop.Backdrop
19
+ import expo.modules.androidglassview.backdrop.internal.InverseLayerScope
20
+ import expo.modules.androidglassview.capture.ViewBackdropCapture
21
+ import kotlin.math.ceil
22
+
23
+ /**
24
+ * A Kyant [Backdrop] whose content is the Android View tree behind a glass view — i.e. the
25
+ * React Native screen — instead of a Compose layer.
26
+ *
27
+ * The screen behind the host view (plus a margin for blur and scaled glass) is captured by
28
+ * [ViewBackdropCapture] into one RenderNode with its own compositing layer. It is re-recorded only
29
+ * when invalidated (the host moved, something scrolled, glass views came or went), and every glass
30
+ * node of the host samples that same cached texture. So a glass that animates — pressed, dragged,
31
+ * scaled — only re-runs its effects on the GPU, not the capture.
32
+ */
33
+ internal class ViewBackdrop(private val host: GlassHostView) : Backdrop {
34
+
35
+ // true so Kyant hands us the glass node's coordinates (and redraws when they change).
36
+ override val isCoordinatesDependent: Boolean = true
37
+
38
+ /** Bumped to make Compose redraw — and the capture re-record — this backdrop. */
39
+ private var generation by mutableIntStateOf(0)
40
+
41
+ private val capture: ViewBackdropCapture? =
42
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ViewBackdropCapture() else null
43
+
44
+ private val captureNode: RenderNode? =
45
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
46
+ RenderNode("AndroidGlassBackdrop").apply { setUseCompositingLayer(true, null) }
47
+ } else {
48
+ null
49
+ }
50
+
51
+ private val margin: Int = ceil(CAPTURE_MARGIN_DP * host.resources.displayMetrics.density).toInt()
52
+ private var recordedGeneration = -1
53
+ private var recordedWidth = 0
54
+ private var recordedHeight = 0
55
+ private var recordedOffsetX = Int.MIN_VALUE
56
+ private var recordedOffsetY = Int.MIN_VALUE
57
+
58
+ private val hostLocation = IntArray(2)
59
+ private val sourceLocation = IntArray(2)
60
+ private var inverseLayerScope: InverseLayerScope? = null
61
+
62
+ fun invalidate() {
63
+ generation++
64
+ // Deliver the change now instead of on the next frame, so an invalidation from a
65
+ // pre-draw / scroll listener lands in the frame that is about to be drawn.
66
+ Snapshot.sendApplyNotifications()
67
+ }
68
+
69
+ /** Frees the cached capture; the next draw records a fresh one. */
70
+ fun release() {
71
+ captureNode?.discardDisplayList()
72
+ recordedGeneration = -1
73
+ }
74
+
75
+ override fun DrawScope.drawBackdrop(
76
+ density: Density,
77
+ coordinates: LayoutCoordinates?,
78
+ layerBlock: (GraphicsLayerScope.() -> Unit)?
79
+ ) {
80
+ // Reading the state subscribes this draw to invalidate().
81
+ val currentGeneration = generation
82
+ // Without RenderEffect (API 31) Kyant cannot blur or bend anything; a sharp copy of the
83
+ // backdrop would just look like a hole, so draw nothing and let the fallback surface show.
84
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return
85
+ val capture = capture ?: return
86
+ val node = captureNode ?: return
87
+ val source = host.backdropSource ?: return
88
+
89
+ host.getLocationOnScreen(hostLocation)
90
+ source.getLocationOnScreen(sourceLocation)
91
+ val offsetX = sourceLocation[0] - hostLocation[0]
92
+ val offsetY = sourceLocation[1] - hostLocation[1]
93
+ val width = host.width + margin * 2
94
+ val height = host.height + margin * 2
95
+ val stale = currentGeneration != recordedGeneration ||
96
+ width != recordedWidth || height != recordedHeight ||
97
+ offsetX != recordedOffsetX || offsetY != recordedOffsetY
98
+ if (stale) {
99
+ if (ViewBackdropCapture.isCapturing) {
100
+ // Re-recorded while another glass view is sampling: capturing now would interleave the
101
+ // two captures. Keep showing the previous capture and sample again on the next frame.
102
+ host.post { invalidate() }
103
+ } else {
104
+ node.setPosition(0, 0, width, height)
105
+ val recordingCanvas = node.beginRecording(width, height)
106
+ try {
107
+ // The capture expects the source's top-left corner at the origin.
108
+ recordingCanvas.translate((margin + offsetX).toFloat(), (margin + offsetY).toFloat())
109
+ capture.draw(recordingCanvas, source, host, GlassRegistry.views)
110
+ } finally {
111
+ node.endRecording()
112
+ }
113
+ recordedGeneration = currentGeneration
114
+ recordedWidth = width
115
+ recordedHeight = height
116
+ recordedOffsetX = offsetX
117
+ recordedOffsetY = offsetY
118
+ }
119
+ }
120
+ if (!node.hasDisplayList()) return
121
+
122
+ // The Compose view fills the host, so the glass node's position inside the Compose root is
123
+ // its offset inside the host.
124
+ val nodeOffset =
125
+ if (coordinates != null && coordinates.isAttached) coordinates.positionInRoot() else Offset.Zero
126
+
127
+ withTransform({
128
+ // Keep the backdrop still while the glass itself is scaled by its layer block
129
+ // (press / drag feedback), same as Kyant's LayerBackdrop.
130
+ if (layerBlock != null) {
131
+ with(obtainInverseLayerScope()) { inverseTransform(density, layerBlock) }
132
+ }
133
+ translate(-nodeOffset.x - margin, -nodeOffset.y - margin)
134
+ }) {
135
+ drawIntoCanvas { canvas ->
136
+ val nativeCanvas = canvas.nativeCanvas
137
+ if (nativeCanvas.isHardwareAccelerated) nativeCanvas.drawRenderNode(node)
138
+ }
139
+ }
140
+ }
141
+
142
+ private fun obtainInverseLayerScope(): InverseLayerScope {
143
+ return inverseLayerScope?.apply { reset() }
144
+ ?: InverseLayerScope().also { inverseLayerScope = it }
145
+ }
146
+
147
+ companion object {
148
+ /** Room around the host for blur samples and for glass that grows while pressed. */
149
+ private const val CAPTURE_MARGIN_DP = 32f
150
+ }
151
+ }
@@ -0,0 +1,25 @@
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
+ * Vendored into expo-android-glass-view. Changes from upstream: package relocated
6
+ * from com.kyant.backdrop, Kotlin Multiplatform expect/actual merged into Android-only
7
+ * code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax.
8
+ */
9
+ package expo.modules.androidglassview.backdrop
10
+
11
+ import androidx.compose.ui.graphics.GraphicsLayerScope
12
+ import androidx.compose.ui.graphics.drawscope.DrawScope
13
+ import androidx.compose.ui.layout.LayoutCoordinates
14
+ import androidx.compose.ui.unit.Density
15
+
16
+ interface Backdrop {
17
+
18
+ val isCoordinatesDependent: Boolean
19
+
20
+ fun DrawScope.drawBackdrop(
21
+ density: Density,
22
+ coordinates: LayoutCoordinates?,
23
+ layerBlock: (GraphicsLayerScope.() -> Unit)? = null
24
+ )
25
+ }