expo-android-glass-view 0.1.4 → 0.1.6

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 (74) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +24 -14
  3. package/android/src/main/java/expo/modules/androidglassview/GlassHostView.kt +11 -17
  4. package/android/src/main/java/expo/modules/androidglassview/ViewBackdrop.kt +7 -0
  5. package/android/src/main/java/expo/modules/androidglassview/backdrop/highlight/HighlightModifier.kt +46 -24
  6. package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/InnerShadowModifier.kt +33 -12
  7. package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/ShadowModifier.kt +42 -16
  8. package/android/src/main/java/expo/modules/androidglassview/capture/ViewBackdropCapture.kt +167 -6
  9. package/build/AndroidGlassBottomTabs.android.d.ts +4 -0
  10. package/build/AndroidGlassBottomTabs.android.d.ts.map +1 -0
  11. package/build/AndroidGlassBottomTabs.android.js +51 -0
  12. package/build/AndroidGlassBottomTabs.android.js.map +1 -0
  13. package/build/AndroidGlassBottomTabs.d.ts +5 -0
  14. package/build/AndroidGlassBottomTabs.d.ts.map +1 -0
  15. package/build/AndroidGlassBottomTabs.js +34 -0
  16. package/build/AndroidGlassBottomTabs.js.map +1 -0
  17. package/build/AndroidGlassButton.android.d.ts +4 -0
  18. package/build/AndroidGlassButton.android.d.ts.map +1 -0
  19. package/build/AndroidGlassButton.android.js +32 -0
  20. package/build/AndroidGlassButton.android.js.map +1 -0
  21. package/build/AndroidGlassButton.d.ts +5 -0
  22. package/build/AndroidGlassButton.d.ts.map +1 -0
  23. package/build/AndroidGlassButton.js +38 -0
  24. package/build/AndroidGlassButton.js.map +1 -0
  25. package/build/AndroidGlassComponents.types.d.ts +72 -0
  26. package/build/AndroidGlassComponents.types.d.ts.map +1 -0
  27. package/build/AndroidGlassComponents.types.js +2 -0
  28. package/build/AndroidGlassComponents.types.js.map +1 -0
  29. package/build/AndroidGlassSlider.android.d.ts +4 -0
  30. package/build/AndroidGlassSlider.android.d.ts.map +1 -0
  31. package/build/AndroidGlassSlider.android.js +27 -0
  32. package/build/AndroidGlassSlider.android.js.map +1 -0
  33. package/build/AndroidGlassSlider.d.ts +5 -0
  34. package/build/AndroidGlassSlider.d.ts.map +1 -0
  35. package/build/AndroidGlassSlider.js +47 -0
  36. package/build/AndroidGlassSlider.js.map +1 -0
  37. package/build/AndroidGlassTab.d.ts +9 -0
  38. package/build/AndroidGlassTab.d.ts.map +1 -0
  39. package/build/AndroidGlassTab.js +22 -0
  40. package/build/AndroidGlassTab.js.map +1 -0
  41. package/build/AndroidGlassToggle.android.d.ts +4 -0
  42. package/build/AndroidGlassToggle.android.d.ts.map +1 -0
  43. package/build/AndroidGlassToggle.android.js +27 -0
  44. package/build/AndroidGlassToggle.android.js.map +1 -0
  45. package/build/AndroidGlassToggle.d.ts +5 -0
  46. package/build/AndroidGlassToggle.d.ts.map +1 -0
  47. package/build/AndroidGlassToggle.js +7 -0
  48. package/build/AndroidGlassToggle.js.map +1 -0
  49. package/build/AndroidGlassView.android.d.ts +4 -0
  50. package/build/AndroidGlassView.android.d.ts.map +1 -0
  51. package/build/AndroidGlassView.android.js +10 -0
  52. package/build/AndroidGlassView.android.js.map +1 -0
  53. package/build/AndroidGlassView.d.ts +8 -0
  54. package/build/AndroidGlassView.d.ts.map +1 -0
  55. package/build/AndroidGlassView.js +18 -0
  56. package/build/AndroidGlassView.js.map +1 -0
  57. package/build/AndroidGlassView.types.d.ts +34 -0
  58. package/build/AndroidGlassView.types.d.ts.map +1 -0
  59. package/build/AndroidGlassView.types.js +2 -0
  60. package/build/AndroidGlassView.types.js.map +1 -0
  61. package/build/MinimizeOnScroll.d.ts +41 -0
  62. package/build/MinimizeOnScroll.d.ts.map +1 -0
  63. package/build/MinimizeOnScroll.js +76 -0
  64. package/build/MinimizeOnScroll.js.map +1 -0
  65. package/build/index.d.ts +10 -0
  66. package/build/index.d.ts.map +1 -0
  67. package/build/index.js +12 -0
  68. package/build/index.js.map +1 -0
  69. package/build/utils.d.ts +5 -0
  70. package/build/utils.d.ts.map +1 -0
  71. package/build/utils.js +9 -0
  72. package/build/utils.js.map +1 -0
  73. package/package.json +1 -1
  74. package/tsconfig.json +3 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.6 — 2026-09-12
4
+
5
+ - Fix: 0.1.4 and 0.1.5 were published without their compiled JavaScript (`build/`), so Metro
6
+ failed with `Unable to resolve "expo-android-glass-view"`. The native code is the same as in
7
+ 0.1.5: an app whose native build already has 0.1.5 only needs the new JavaScript, no rebuild.
8
+ - `prepare` now fails when TypeScript emits nothing, so a package without its JavaScript can't be
9
+ published again.
10
+
11
+ ## 0.1.5 — 2026-09-12
12
+
13
+ - Performance: a glass view's capture leaves out everything that cannot reach it. Before, an
14
+ animation anywhere on screen made every glass view re-render its blur and refraction on every
15
+ frame; now only the glass views it can be seen through do. On a screen with 17 glass views over
16
+ an animated backdrop (Helio G99, release build) this took the frame rate from about 20 to about
17
+ 70 fps. A left-out view that moves, resizes or scrolls triggers a new capture.
18
+
19
+ - Performance: scrolling no longer re-captures every glass view in the window. A glass view
20
+ captures again only when a container whose scroll offset its capture baked in has scrolled;
21
+ lists it merely references (e.g. a screen's list under a tab bar) scroll live for free.
22
+ - Performance: the drop shadow, inner shadow and rim highlight are only re-recorded when their
23
+ shape or parameters change (not when they fade, and not on every redraw), and are skipped at
24
+ zero alpha.
25
+
3
26
  ## 0.1.3 — 2026-09-11
4
27
 
5
28
  - Fix a crash when navigating to or from a screen that has a glass view (Expo Router and React
package/README.md CHANGED
@@ -342,6 +342,9 @@ view tree of the window — your React Native screen — captured with an approa
342
342
  when content behind the glass animates, the glass shows it without capturing again.
343
343
  4. Other glass views are left out of the capture, except the glass's own ancestors: a toggle on
344
344
  a glass card sees the card. This rules out cycles (glass A → glass B → glass A).
345
+ 5. So is everything that cannot reach the glass (its bounds plus a small margin, and those of its
346
+ descendants): a live reference to it would make the glass re-render — capture, blur and
347
+ refraction — whenever it changes, even though none of it shows through the glass.
345
348
 
346
349
  ### The effects
347
350
 
@@ -356,7 +359,10 @@ The capture is cached in one RenderNode with its own GPU layer, and every glass
356
359
  (the tab bar has three) samples that same texture. It is recorded again only when:
357
360
 
358
361
  - the glass moves on screen (layout, transform, dragging),
359
- - a parent scrolls,
362
+ - a container it draws inline scrolls: one of its own ancestors, one of their direct children,
363
+ or a container that holds other glass views. A list that is simply behind the glass (deeper in
364
+ another branch, with no glass inside) is referenced, so it scrolls live without a new capture,
365
+ - a view it left out because it was too far away moves, resizes or scrolls,
360
366
  - a glass view is added or removed.
361
367
 
362
368
  A glass view that is off screen waits until it is visible again. Pressing or dragging a glass
@@ -376,25 +382,26 @@ screen.
376
382
 
377
383
  ## Performance
378
384
 
379
- The glass costs nothing while nothing moves. The cost comes when many glass views have to be
380
- redrawn in the same frame: every one of them runs its blur and refraction on the GPU.
385
+ The glass costs nothing while nothing moves. It costs when what is behind a glass view changes:
386
+ that glass view then runs its blur and refraction again on the GPU. Changes elsewhere on the
387
+ screen don't count, because each glass view only captures what can reach it (see _Seeing the
388
+ React Native screen behind it_).
381
389
 
382
- Measured with `adb shell dumpsys gfxinfo` in the example app on a mid-range phone: Xiaomi
383
- 24117RN76E, MediaTek Helio G99, Android 16, 120 Hz. The app was a debuggable development build.
384
- That mostly slows the UI thread; the glass effects run on Android's RenderThread and GPU,
385
- which a release build does not speed up much.
390
+ Measured with `adb shell dumpsys gfxinfo` and `atrace` on a release build, on a mid-range phone:
391
+ Xiaomi 24117RN76E, MediaTek Helio G99, Android 16, 120 Hz.
386
392
 
387
- | Screen | Glass views visible | Result |
388
- | -------------------------------------------------------------------------- | ------------------- | --------------------------------------------------------------------------------- |
389
- | Static screen, nothing moving | 2 | 0 frames rendered while idle |
390
- | Scrolling a list under a glass header, a glass button and the tab bar | 3 | 94% of frames on time |
391
- | 6 glass tiles over an animated backdrop, plus glass panels and controls | 15+ | about 20–24 fps; the RenderThread spends about 40 ms per frame issuing GPU work |
393
+ | Screen | Glass views | Result |
394
+ | -------------------------------------------------------------------------- | ----------- | ----------------------------------------------------------------------- |
395
+ | Static screen, nothing moving | 3 | 0 frames rendered while idle |
396
+ | Scrolling a list under a glass header, a glass button and the tab bar | 3 | no missed frames; 90% of frames within 14 ms |
397
+ | 6 glass tiles over an animated backdrop, plus glass panels and controls | 17 | about 70 fps, no missed frames; the RenderThread needs ~14 ms per frame |
392
398
 
393
399
  In practice:
394
400
 
395
401
  - A glass header, a tab bar and a few glass controls on a scrolling screen stay smooth.
396
- - Many glass views redrawing at once — over an animation, or all scrolling together — will
397
- drop frames on mid-range GPUs. Keep those to a handful.
402
+ - What adds up is the number of glass views whose backdrop changes in the same frame — glass
403
+ over an animation, or glass that scrolls. On mid-range GPUs, keep those to a handful; glass
404
+ elsewhere on the screen doesn't add to it.
398
405
  - Don't put a glass view inside every item of a long list: each visible item captures and
399
406
  redraws on every scroll frame.
400
407
  - Glass views scrolled out of view don't capture again until they are visible.
@@ -405,6 +412,9 @@ In practice:
405
412
  - **Glass does not show other glass views** behind it, unless it sits inside them.
406
413
  - Inside a container that holds a glass view, branches containing glass are drawn after their
407
414
  siblings in the backdrop, so z-order can differ from the screen in that case.
415
+ - Content drawn well outside its view's bounds (more than about 8 dp, e.g. a large box shadow)
416
+ may be missing behind glass that the view itself is away from, and so may content that a
417
+ distant view animates towards the glass without moving itself.
408
418
  - `SurfaceView`-based content (most video players, camera previews, some maps) is not part of the
409
419
  view tree's drawing and shows up empty behind the glass. `TextureView` content works.
410
420
  - A React Native `Modal` is a separate window, so glass inside it only sees the modal's content.
@@ -82,26 +82,26 @@ abstract class GlassHostView(context: Context, appContext: AppContext) : ExpoVie
82
82
  private var downY = 0f
83
83
  private var dragClaimed = false
84
84
 
85
- // Re-sample when the glass moves on screen (layout, an ancestor scrolling, transforms).
85
+ // Re-sample when the glass moves on screen (layout, an ancestor scrolling, transforms), when a
86
+ // container the capture drew inline (not by reference) scrolled — its offset is baked into the
87
+ // capture — or when a view the capture left out (too far away to be seen through the glass)
88
+ // moved and may now be. A list that is only referenced scrolls live, so scrolling it costs no
89
+ // capture. There is deliberately no global-layout listener: React Native lays out on every
90
+ // state change (e.g. each slider tick updating a label), and re-sampling every glass view each
91
+ // time made interactions stutter. Everything else behind the glass is referenced live (see
92
+ // ViewBackdropCapture).
86
93
  private val preDrawListener = ViewTreeObserver.OnPreDrawListener {
87
94
  getLocationOnScreen(location)
88
95
  if (location[0] != lastLocation[0] || location[1] != lastLocation[1]) {
89
96
  lastLocation[0] = location[0]
90
97
  lastLocation[1] = location[1]
91
98
  resampleIfVisible()
92
- } else if (resamplePending) {
99
+ } else if (resamplePending || backdrop.changedSinceCapture()) {
93
100
  resampleIfVisible()
94
101
  }
95
102
  true
96
103
  }
97
104
 
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
105
  /**
106
106
  * Re-samples now if any part of this view is on screen. Sampling costs a capture plus the
107
107
  * glass effects on the GPU, so glass scrolled out of view waits until it is visible again.
@@ -288,18 +288,12 @@ abstract class GlassHostView(context: Context, appContext: AppContext) : ExpoVie
288
288
  override fun onAttachedToWindow() {
289
289
  ensureViewTreeOwners()
290
290
  super.onAttachedToWindow()
291
- observer = viewTreeObserver.also {
292
- it.addOnPreDrawListener(preDrawListener)
293
- it.addOnScrollChangedListener(scrollListener)
294
- }
291
+ observer = viewTreeObserver.also { it.addOnPreDrawListener(preDrawListener) }
295
292
  GlassRegistry.add(this)
296
293
  }
297
294
 
298
295
  override fun onDetachedFromWindow() {
299
- observer?.takeIf { it.isAlive }?.let {
300
- it.removeOnPreDrawListener(preDrawListener)
301
- it.removeOnScrollChangedListener(scrollListener)
302
- }
296
+ observer?.takeIf { it.isAlive }?.removeOnPreDrawListener(preDrawListener)
303
297
  observer = null
304
298
  lastLocation[0] = Int.MIN_VALUE
305
299
  lastLocation[1] = Int.MIN_VALUE
@@ -66,9 +66,16 @@ internal class ViewBackdrop(private val host: GlassHostView) : Backdrop {
66
66
  Snapshot.sendApplyNotifications()
67
67
  }
68
68
 
69
+ /**
70
+ * Whether the capture is out of date although the host didn't move: something it drew inline
71
+ * (not by reference) scrolled, or a view it left out moved (see ViewBackdropCapture).
72
+ */
73
+ fun changedSinceCapture(): Boolean = capture?.changedSinceCapture() ?: false
74
+
69
75
  /** Frees the cached capture; the next draw records a fresh one. */
70
76
  fun release() {
71
77
  captureNode?.discardDisplayList()
78
+ capture?.clear()
72
79
  recordedGeneration = -1
73
80
  }
74
81
 
@@ -4,7 +4,9 @@
4
4
  *
5
5
  * Vendored into expo-android-glass-view. Changes from upstream: package relocated
6
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.
7
+ * code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax,
8
+ * the highlight layer is only re-recorded when its shape, size, width, blur or style change,
9
+ * and nothing is drawn at zero alpha.
8
10
  */
9
11
  package expo.modules.androidglassview.backdrop.highlight
10
12
 
@@ -25,6 +27,7 @@ import androidx.compose.ui.node.invalidateDraw
25
27
  import androidx.compose.ui.node.requireGraphicsContext
26
28
  import androidx.compose.ui.platform.InspectorInfo
27
29
  import androidx.compose.ui.unit.Density
30
+ import androidx.compose.ui.unit.Dp
28
31
  import androidx.compose.ui.unit.IntSize
29
32
  import androidx.compose.ui.util.fastCoerceAtMost
30
33
  import expo.modules.androidglassview.backdrop.RuntimeShaderCacheImpl
@@ -92,9 +95,17 @@ internal class HighlightNode(
92
95
 
93
96
  private var prevStyle: HighlightStyle? = null
94
97
 
98
+ // What the layer was last recorded with. Alpha and blend mode are layer properties, so
99
+ // animating them (e.g. a highlight fading in on press) doesn't re-record the rim, and neither
100
+ // does a redraw of the node for any other reason.
101
+ private var recordedOutline: Outline? = null
102
+ private var recordedWidth = Dp.Unspecified
103
+ private var recordedBlurRadius = Dp.Unspecified
104
+ private var recordedStyle: HighlightStyle? = null
105
+
95
106
  override fun ContentDrawScope.draw() {
96
107
  val highlight = highlight()
97
- if (highlight == null || highlight.width.value <= 0f) {
108
+ if (highlight == null || highlight.width.value <= 0f || highlight.alpha <= 0f) {
98
109
  return drawContent()
99
110
  }
100
111
 
@@ -106,33 +117,43 @@ internal class HighlightNode(
106
117
  val density: Density = this
107
118
  val layoutDirection = layoutDirection
108
119
 
109
- val safeSize =
110
- IntSize(
111
- ceil(size.width).toInt() + 2,
112
- ceil(size.height).toInt() + 2
113
- )
114
-
120
+ // ShapeProvider hands out the same outline while shape, size and density stay.
115
121
  val outline = shapeProvider.shape.createOutline(size, layoutDirection, density)
116
- val clipPath =
117
- if (outline is Outline.Rounded) {
118
- clipPath ?: Path().also { clipPath = it }
119
- } else {
120
- null
121
- }
122
122
 
123
- configurePaint(highlight)
123
+ if (outline !== recordedOutline || highlight.width != recordedWidth ||
124
+ highlight.blurRadius != recordedBlurRadius || highlight.style != recordedStyle
125
+ ) {
126
+ val safeSize =
127
+ IntSize(
128
+ ceil(size.width).toInt() + 2,
129
+ ceil(size.height).toInt() + 2
130
+ )
131
+ val clipPath =
132
+ if (outline is Outline.Rounded) {
133
+ clipPath ?: Path().also { clipPath = it }
134
+ } else {
135
+ null
136
+ }
137
+
138
+ configurePaint(highlight)
139
+
140
+ highlightLayer.record(safeSize) {
141
+ translate(1f, 1f) {
142
+ val canvas = drawContext.canvas
143
+ canvas.save()
144
+ canvas.clipOutline(outline, clipPath)
145
+ canvas.drawOutline(outline, paint)
146
+ canvas.restore()
147
+ }
148
+ }
149
+ recordedOutline = outline
150
+ recordedWidth = highlight.width
151
+ recordedBlurRadius = highlight.blurRadius
152
+ recordedStyle = highlight.style
153
+ }
124
154
 
125
155
  highlightLayer.alpha = highlight.alpha
126
156
  highlightLayer.blendMode = highlight.style.blendMode
127
- highlightLayer.record(safeSize) {
128
- translate(1f, 1f) {
129
- val canvas = drawContext.canvas
130
- canvas.save()
131
- canvas.clipOutline(outline, clipPath)
132
- canvas.drawOutline(outline, paint)
133
- canvas.restore()
134
- }
135
- }
136
157
 
137
158
  translate(-1f, -1f) {
138
159
  drawLayer(highlightLayer)
@@ -154,6 +175,7 @@ internal class HighlightNode(
154
175
  clipPath = null
155
176
  runtimeShaderCache.clear()
156
177
  prevStyle = null
178
+ recordedOutline = null
157
179
  }
158
180
 
159
181
  private fun DrawScope.configurePaint(highlight: Highlight) {
@@ -4,13 +4,16 @@
4
4
  *
5
5
  * Vendored into expo-android-glass-view. Changes from upstream: package relocated
6
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.
7
+ * code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax,
8
+ * the shadow layer is only re-recorded when its shape, size, offset or colour change, and
9
+ * nothing is drawn at zero alpha.
8
10
  */
9
11
  package expo.modules.androidglassview.backdrop.shadow
10
12
 
11
13
  import androidx.compose.ui.Modifier
12
14
  import androidx.compose.ui.graphics.BlendMode
13
15
  import androidx.compose.ui.graphics.BlurEffect
16
+ import androidx.compose.ui.graphics.Color
14
17
  import androidx.compose.ui.graphics.Outline
15
18
  import androidx.compose.ui.graphics.Paint
16
19
  import androidx.compose.ui.graphics.Path
@@ -83,12 +86,20 @@ internal class InnerShadowNode(
83
86
 
84
87
  private var prevRadius = Float.NaN
85
88
 
89
+ // What the layer was last recorded with. Radius (a render effect), alpha and blend mode are
90
+ // layer properties, so animating them doesn't re-record the shape.
91
+ private var recordedOutline: Outline? = null
92
+ private var recordedOffsetX = Float.NaN
93
+ private var recordedOffsetY = Float.NaN
94
+ private var recordedColor = Color.Unspecified
95
+
86
96
  override fun ContentDrawScope.draw() {
87
97
  drawContent()
88
98
 
89
99
  if (!isRenderEffectSupported()) return
90
100
 
91
101
  val shadow = shadow() ?: return
102
+ if (shadow.alpha <= 0f) return
92
103
 
93
104
  val shadowLayer = shadowLayer
94
105
  if (shadowLayer != null) {
@@ -108,8 +119,6 @@ internal class InnerShadowNode(
108
119
  null
109
120
  }
110
121
 
111
- configurePaint(shadow)
112
-
113
122
  shadowLayer.alpha = shadow.alpha
114
123
  shadowLayer.blendMode = shadow.blendMode
115
124
  if (prevRadius != radius) {
@@ -121,15 +130,25 @@ internal class InnerShadowNode(
121
130
  }
122
131
  prevRadius = radius
123
132
  }
124
- shadowLayer.record {
125
- val canvas = drawContext.canvas
126
- canvas.save()
127
- canvas.clipOutline(outline, clipPath)
128
- canvas.drawOutline(outline, paint)
129
- canvas.translate(offsetX, offsetY)
130
- canvas.drawOutline(outline, ShadowMaskPaint)
131
- canvas.translate(-offsetX, -offsetY)
132
- canvas.restore()
133
+ // ShapeProvider hands out the same outline while shape, size and density stay.
134
+ if (outline !== recordedOutline || offsetX != recordedOffsetX ||
135
+ offsetY != recordedOffsetY || shadow.color != recordedColor
136
+ ) {
137
+ configurePaint(shadow)
138
+ shadowLayer.record {
139
+ val canvas = drawContext.canvas
140
+ canvas.save()
141
+ canvas.clipOutline(outline, clipPath)
142
+ canvas.drawOutline(outline, paint)
143
+ canvas.translate(offsetX, offsetY)
144
+ canvas.drawOutline(outline, ShadowMaskPaint)
145
+ canvas.translate(-offsetX, -offsetY)
146
+ canvas.restore()
147
+ }
148
+ recordedOutline = outline
149
+ recordedOffsetX = offsetX
150
+ recordedOffsetY = offsetY
151
+ recordedColor = shadow.color
133
152
  }
134
153
 
135
154
  val canvas = drawContext.canvas
@@ -154,6 +173,8 @@ internal class InnerShadowNode(
154
173
  graphicsContext.releaseGraphicsLayer(layer)
155
174
  shadowLayer = null
156
175
  }
176
+ prevRadius = Float.NaN
177
+ recordedOutline = null
157
178
  }
158
179
 
159
180
  private fun DrawScope.configurePaint(shadow: InnerShadow) {
@@ -4,12 +4,16 @@
4
4
  *
5
5
  * Vendored into expo-android-glass-view. Changes from upstream: package relocated
6
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.
7
+ * code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax,
8
+ * the shadow layer is only re-recorded when its shape, size, radius, offset or colour change,
9
+ * and nothing is drawn at zero alpha.
8
10
  */
9
11
  package expo.modules.androidglassview.backdrop.shadow
10
12
 
11
13
  import androidx.compose.ui.Modifier
12
14
  import androidx.compose.ui.graphics.BlendMode
15
+ import androidx.compose.ui.graphics.Color
16
+ import androidx.compose.ui.graphics.Outline
13
17
  import androidx.compose.ui.graphics.Paint
14
18
  import androidx.compose.ui.graphics.drawOutline
15
19
  import androidx.compose.ui.graphics.drawscope.ContentDrawScope
@@ -78,8 +82,18 @@ internal class ShadowNode(
78
82
 
79
83
  private val paint = Paint()
80
84
 
85
+ // What the layer was last recorded with. Alpha and blend mode are layer properties, so
86
+ // animating them (e.g. a shadow fading in on press) doesn't re-record the blurred shape, and
87
+ // neither does a redraw of the node for any other reason.
88
+ private var recordedOutline: Outline? = null
89
+ private var recordedRadius = Float.NaN
90
+ private var recordedOffsetX = Float.NaN
91
+ private var recordedOffsetY = Float.NaN
92
+ private var recordedColor = Color.Unspecified
93
+
81
94
  override fun ContentDrawScope.draw() {
82
- val shadow = shadow() ?: return drawContent()
95
+ val shadow = shadow()
96
+ if (shadow == null || shadow.alpha <= 0f) return drawContent()
83
97
 
84
98
  val shadowLayer = shadowLayer
85
99
  if (shadowLayer != null) {
@@ -90,25 +104,36 @@ internal class ShadowNode(
90
104
  val radius = shadow.radius.toPx()
91
105
  val offsetX = shadow.offset.x.toPx()
92
106
  val offsetY = shadow.offset.y.toPx()
93
- val shadowSize = IntSize(
94
- ceil(size.width + radius * 4f + offsetX).toInt(),
95
- ceil(size.height + radius * 4f + offsetY).toInt()
96
- )
107
+ // ShapeProvider hands out the same outline while shape, size and density stay.
97
108
  val outline = shapeProvider.shape.createOutline(size, layoutDirection, density)
98
109
 
99
- configurePaint(shadow)
110
+ if (outline !== recordedOutline || radius != recordedRadius ||
111
+ offsetX != recordedOffsetX || offsetY != recordedOffsetY ||
112
+ shadow.color != recordedColor
113
+ ) {
114
+ val shadowSize = IntSize(
115
+ ceil(size.width + radius * 4f + offsetX).toInt(),
116
+ ceil(size.height + radius * 4f + offsetY).toInt()
117
+ )
118
+ configurePaint(shadow)
119
+ shadowLayer.record(shadowSize) {
120
+ translate(radius * 2f + offsetX, radius * 2f + offsetY) {
121
+ val canvas = drawContext.canvas
122
+ canvas.drawOutline(outline, paint)
123
+ canvas.translate(-offsetX, -offsetY)
124
+ canvas.drawOutline(outline, ShadowMaskPaint)
125
+ canvas.translate(offsetX, offsetY)
126
+ }
127
+ }
128
+ recordedOutline = outline
129
+ recordedRadius = radius
130
+ recordedOffsetX = offsetX
131
+ recordedOffsetY = offsetY
132
+ recordedColor = shadow.color
133
+ }
100
134
 
101
135
  shadowLayer.alpha = shadow.alpha
102
136
  shadowLayer.blendMode = shadow.blendMode
103
- shadowLayer.record(shadowSize) {
104
- translate(radius * 2f + offsetX, radius * 2f + offsetY) {
105
- val canvas = drawContext.canvas
106
- canvas.drawOutline(outline, paint)
107
- canvas.translate(-offsetX, -offsetY)
108
- canvas.drawOutline(outline, ShadowMaskPaint)
109
- canvas.translate(offsetX, offsetY)
110
- }
111
- }
112
137
 
113
138
  translate(-radius * 2f, -radius * 2f) {
114
139
  drawLayer(shadowLayer)
@@ -132,6 +157,7 @@ internal class ShadowNode(
132
157
  graphicsContext.releaseGraphicsLayer(layer)
133
158
  shadowLayer = null
134
159
  }
160
+ recordedOutline = null
135
161
  }
136
162
 
137
163
  private fun DrawScope.configurePaint(shadow: Shadow) {