expo-android-glass-view 0.1.3 → 0.1.5

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 (75) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +164 -67
  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/package.json +1 -1
  10. package/src/AndroidGlassComponents.types.ts +1 -1
  11. package/tsconfig.json +20 -5
  12. package/build/AndroidGlassBottomTabs.android.d.ts +0 -4
  13. package/build/AndroidGlassBottomTabs.android.d.ts.map +0 -1
  14. package/build/AndroidGlassBottomTabs.android.js +0 -51
  15. package/build/AndroidGlassBottomTabs.android.js.map +0 -1
  16. package/build/AndroidGlassBottomTabs.d.ts +0 -5
  17. package/build/AndroidGlassBottomTabs.d.ts.map +0 -1
  18. package/build/AndroidGlassBottomTabs.js +0 -34
  19. package/build/AndroidGlassBottomTabs.js.map +0 -1
  20. package/build/AndroidGlassButton.android.d.ts +0 -4
  21. package/build/AndroidGlassButton.android.d.ts.map +0 -1
  22. package/build/AndroidGlassButton.android.js +0 -32
  23. package/build/AndroidGlassButton.android.js.map +0 -1
  24. package/build/AndroidGlassButton.d.ts +0 -5
  25. package/build/AndroidGlassButton.d.ts.map +0 -1
  26. package/build/AndroidGlassButton.js +0 -38
  27. package/build/AndroidGlassButton.js.map +0 -1
  28. package/build/AndroidGlassComponents.types.d.ts +0 -72
  29. package/build/AndroidGlassComponents.types.d.ts.map +0 -1
  30. package/build/AndroidGlassComponents.types.js +0 -2
  31. package/build/AndroidGlassComponents.types.js.map +0 -1
  32. package/build/AndroidGlassSlider.android.d.ts +0 -4
  33. package/build/AndroidGlassSlider.android.d.ts.map +0 -1
  34. package/build/AndroidGlassSlider.android.js +0 -27
  35. package/build/AndroidGlassSlider.android.js.map +0 -1
  36. package/build/AndroidGlassSlider.d.ts +0 -5
  37. package/build/AndroidGlassSlider.d.ts.map +0 -1
  38. package/build/AndroidGlassSlider.js +0 -47
  39. package/build/AndroidGlassSlider.js.map +0 -1
  40. package/build/AndroidGlassTab.d.ts +0 -9
  41. package/build/AndroidGlassTab.d.ts.map +0 -1
  42. package/build/AndroidGlassTab.js +0 -22
  43. package/build/AndroidGlassTab.js.map +0 -1
  44. package/build/AndroidGlassToggle.android.d.ts +0 -4
  45. package/build/AndroidGlassToggle.android.d.ts.map +0 -1
  46. package/build/AndroidGlassToggle.android.js +0 -27
  47. package/build/AndroidGlassToggle.android.js.map +0 -1
  48. package/build/AndroidGlassToggle.d.ts +0 -5
  49. package/build/AndroidGlassToggle.d.ts.map +0 -1
  50. package/build/AndroidGlassToggle.js +0 -7
  51. package/build/AndroidGlassToggle.js.map +0 -1
  52. package/build/AndroidGlassView.android.d.ts +0 -4
  53. package/build/AndroidGlassView.android.d.ts.map +0 -1
  54. package/build/AndroidGlassView.android.js +0 -10
  55. package/build/AndroidGlassView.android.js.map +0 -1
  56. package/build/AndroidGlassView.d.ts +0 -8
  57. package/build/AndroidGlassView.d.ts.map +0 -1
  58. package/build/AndroidGlassView.js +0 -18
  59. package/build/AndroidGlassView.js.map +0 -1
  60. package/build/AndroidGlassView.types.d.ts +0 -34
  61. package/build/AndroidGlassView.types.d.ts.map +0 -1
  62. package/build/AndroidGlassView.types.js +0 -2
  63. package/build/AndroidGlassView.types.js.map +0 -1
  64. package/build/MinimizeOnScroll.d.ts +0 -41
  65. package/build/MinimizeOnScroll.d.ts.map +0 -1
  66. package/build/MinimizeOnScroll.js +0 -76
  67. package/build/MinimizeOnScroll.js.map +0 -1
  68. package/build/index.d.ts +0 -10
  69. package/build/index.d.ts.map +0 -1
  70. package/build/index.js +0 -12
  71. package/build/index.js.map +0 -1
  72. package/build/utils.d.ts +0 -5
  73. package/build/utils.d.ts.map +0 -1
  74. package/build/utils.js +0 -9
  75. package/build/utils.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## Unreleased
4
+
5
+ - Performance: a glass view's capture leaves out everything that cannot reach it. Before, an
6
+ animation anywhere on screen made every glass view re-render its blur and refraction on every
7
+ frame; now only the glass views it can be seen through do. On a screen with 17 glass views over
8
+ an animated backdrop (Helio G99, release build) this took the frame rate from about 20 to about
9
+ 70 fps. A left-out view that moves, resizes or scrolls triggers a new capture.
10
+
11
+ - Performance: scrolling no longer re-captures every glass view in the window. A glass view
12
+ captures again only when a container whose scroll offset its capture baked in has scrolled;
13
+ lists it merely references (e.g. a screen's list under a tab bar) scroll live for free.
14
+ - Performance: the drop shadow, inner shadow and rim highlight are only re-recorded when their
15
+ shape or parameters change (not when they fade, and not on every redraw), and are skipped at
16
+ zero alpha.
17
+
3
18
  ## 0.1.3 — 2026-09-11
4
19
 
5
20
  - Fix a crash when navigating to or from a screen that has a glass view (Expo Router and React
package/README.md CHANGED
@@ -1,4 +1,8 @@
1
- # expo-android-glass-view
1
+ <p align="center">
2
+ <img src=".github/assets/hero.png" alt="Liquid Glass — expo-android-glass-view" width="1040" />
3
+ </p>
4
+
5
+ #
2
6
 
3
7
  Liquid Glass for React Native on **Android**: real refraction, blur, vibrancy and rim
4
8
  highlights behind any React content, rendered with Jetpack Compose — plus ready-made glass
@@ -30,12 +34,12 @@ the same window, and keeps up with scrolling lists, animations and layout change
30
34
 
31
35
  ## Platforms
32
36
 
33
- | Platform | Result |
34
- | --- | --- |
35
- | Android 13+ (API 33) | Full effect: refraction, chromatic aberration, blur, vibrancy |
37
+ | Platform | Result |
38
+ | ---------------------- | ------------------------------------------------------------- |
39
+ | Android 13+ (API 33) | Full effect: refraction, chromatic aberration, blur, vibrancy |
36
40
  | Android 12 (API 31–32) | Blur and vibrancy, no refraction (needs AGSL runtime shaders) |
37
- | Android 11 and older | Plain translucent surface (`fallbackColor`) |
38
- | iOS, web | Same components, plain fallbacks with the same API — no crash |
41
+ | Android 11 and older | Plain translucent surface (`fallbackColor`) |
42
+ | iOS, web | Same components, plain fallbacks with the same API — no crash |
39
43
 
40
44
  Requires a development build (it is a native module, so it does not run in Expo Go) and the
41
45
  React Native New Architecture. Built against Expo SDK 57 / React Native 0.86.
@@ -59,22 +63,24 @@ A container: children render on top of the glass and stay fully interactive.
59
63
  </AndroidGlassView>
60
64
  ```
61
65
 
66
+ <img src=".github/assets/glasses.gif" alt="AndroidGlassView with different props: default, frosted, lens, prism, tint, no shadow" width="480" />
67
+
62
68
  Accepts every `View` prop plus:
63
69
 
64
- | Prop | Type | Default | Description |
65
- | --- | --- | --- | --- |
66
- | `cornerRadius` | `number` | `style.borderRadius`, else `24` | Corner radius of the glass shape in dp. Use a large value (e.g. `999`) for a capsule. |
67
- | `blurRadius` | `number` | `2` | Backdrop blur in dp. `0` disables it. |
68
- | `refractionHeight` | `number` | `12` | Width of the refracting rim in dp. `0` disables refraction. |
69
- | `refractionAmount` | `number` | `24` | How far content is bent at the rim, in dp. |
70
- | `chromaticAberration` | `boolean` | `false` | Split the refraction per colour channel (prism fringe). |
71
- | `depthEffect` | `boolean` | `false` | Stronger, depth-like bending towards the rim. |
72
- | `vibrancy` | `boolean` | `true` | Boost the saturation of what is behind the glass. |
73
- | `highlight` | `boolean` | `true` | Specular highlight along the rim. |
74
- | `shadow` | `boolean` | `true` | Soft drop shadow around the shape. |
75
- | `tintColor` | `ColorValue` | — | Colour tint mixed into the glass. |
76
- | `surfaceColor` | `ColorValue` | — | Flat colour painted over the glass, on top of the refraction. |
77
- | `fallbackColor` | `ColorValue` | `rgba(255,255,255,0.7)` | Surface used where the effect cannot run (see *Platforms*). |
70
+ | Prop | Type | Default | Description |
71
+ | --------------------- | ------------ | ------------------------------- | ------------------------------------------------------------------------------------- |
72
+ | `cornerRadius` | `number` | `style.borderRadius`, else `24` | Corner radius of the glass shape in dp. Use a large value (e.g. `999`) for a capsule. |
73
+ | `blurRadius` | `number` | `2` | Backdrop blur in dp. `0` disables it. |
74
+ | `refractionHeight` | `number` | `12` | Width of the refracting rim in dp. `0` disables refraction. |
75
+ | `refractionAmount` | `number` | `24` | How far content is bent at the rim, in dp. |
76
+ | `chromaticAberration` | `boolean` | `false` | Split the refraction per colour channel (prism fringe). |
77
+ | `depthEffect` | `boolean` | `false` | Stronger, depth-like bending towards the rim. |
78
+ | `vibrancy` | `boolean` | `true` | Boost the saturation of what is behind the glass. |
79
+ | `highlight` | `boolean` | `true` | Specular highlight along the rim. |
80
+ | `shadow` | `boolean` | `true` | Soft drop shadow around the shape. |
81
+ | `tintColor` | `ColorValue` | — | Colour tint mixed into the glass. |
82
+ | `surfaceColor` | `ColorValue` | — | Flat colour painted over the glass, on top of the refraction. |
83
+ | `fallbackColor` | `ColorValue` | `rgba(255,255,255,0.7)` | Surface used where the effect cannot run (see _Platforms_). |
78
84
 
79
85
  Don't set `backgroundColor` on a glass view — it would paint over the effect. Use `tintColor`
80
86
  or `surfaceColor` instead.
@@ -84,6 +90,8 @@ or `surfaceColor` instead.
84
90
  Kyant's liquid button: while pressed, the glass swells, stretches towards the finger and a
85
91
  highlight follows it.
86
92
 
93
+ <img src=".github/assets/buttons.gif" alt="AndroidGlassButton" width="480" />
94
+
87
95
  ```tsx
88
96
  <AndroidGlassButton title="Save" onPress={save} />
89
97
  <AndroidGlassButton title="Continue" tintColor="#0088FF" onPress={next} />
@@ -95,29 +103,31 @@ highlight follows it.
95
103
 
96
104
  Takes every `AndroidGlassView` prop (`cornerRadius` defaults to a capsule) plus:
97
105
 
98
- | Prop | Type | Default | Description |
99
- | --- | --- | --- | --- |
100
- | `onPress` | `() => void` | — | Called on tap. |
101
- | `title` | `string` | — | Convenience label (15 sp; white on a tinted button). |
102
- | `titleStyle` | `TextStyle` | — | Style for `title`. |
103
- | `interactive` | `boolean` | `true` | Press and drag deformation. |
106
+ | Prop | Type | Default | Description |
107
+ | ------------- | ------------ | ------- | ---------------------------------------------------- |
108
+ | `onPress` | `() => void` | — | Called on tap. |
109
+ | `title` | `string` | — | Convenience label (15 sp; white on a tinted button). |
110
+ | `titleStyle` | `TextStyle` | — | Style for `title`. |
111
+ | `interactive` | `boolean` | `true` | Press and drag deformation. |
104
112
 
105
113
  The default size is 48 dp high with 16 dp horizontal padding; override it with `style`. The
106
- children are drawn *inside* the glass (clipped to it, deformed with it), so they are decorative:
114
+ children are drawn _inside_ the glass (clipped to it, deformed with it), so they are decorative:
107
115
  don't put interactive elements in a button.
108
116
 
109
117
  ## `AndroidGlassToggle`
110
118
 
119
+ <img src=".github/assets/toggles.gif" alt="AndroidGlassToggle" width="480" />
120
+
111
121
  ```tsx
112
122
  <AndroidGlassToggle value={enabled} onValueChange={setEnabled} />
113
123
  ```
114
124
 
115
- | Prop | Type | Default | Description |
116
- | --- | --- | --- | --- |
117
- | `value` | `boolean` | `false` | |
118
- | `onValueChange` | `(value: boolean) => void` | — | Called on tap or when the thumb is dropped on the other side. |
119
- | `accentColor` | `ColorValue` | iOS green | Track colour when on. |
120
- | `trackColor` | `ColorValue` | translucent grey | Track colour when off. |
125
+ | Prop | Type | Default | Description |
126
+ | --------------- | -------------------------- | ---------------- | ------------------------------------------------------------- |
127
+ | `value` | `boolean` | `false` | |
128
+ | `onValueChange` | `(value: boolean) => void` | — | Called on tap or when the thumb is dropped on the other side. |
129
+ | `accentColor` | `ColorValue` | iOS green | Track colour when on. |
130
+ | `trackColor` | `ColorValue` | translucent grey | Track colour when off. |
121
131
 
122
132
  The track is 64 × 28 dp; the thumb turns into refracting glass and grows past it while dragged.
123
133
 
@@ -127,18 +137,20 @@ new value, the switch returns to `value`.
127
137
 
128
138
  ## `AndroidGlassSlider`
129
139
 
140
+ <img src=".github/assets/sliders.gif" alt="AndroidGlassSlider" width="480" />
141
+
130
142
  ```tsx
131
143
  <AndroidGlassSlider value={volume} maximumValue={100} onValueChange={setVolume} />
132
144
  ```
133
145
 
134
- | Prop | Type | Default | Description |
135
- | --- | --- | --- | --- |
136
- | `value` | `number` | `0` | |
137
- | `minimumValue` / `maximumValue` | `number` | `0` / `1` | |
138
- | `onValueChange` | `(value: number) => void` | — | Called continuously while dragging. |
139
- | `onSlidingComplete` | `(value: number) => void` | — | Called when the finger is lifted, or after a tap on the track. |
140
- | `accentColor` | `ColorValue` | system blue | Filled part of the track. |
141
- | `trackColor` | `ColorValue` | translucent grey | Unfilled part of the track. |
146
+ | Prop | Type | Default | Description |
147
+ | ------------------------------- | ------------------------- | ---------------- | -------------------------------------------------------------- |
148
+ | `value` | `number` | `0` | |
149
+ | `minimumValue` / `maximumValue` | `number` | `0` / `1` | |
150
+ | `onValueChange` | `(value: number) => void` | — | Called continuously while dragging. |
151
+ | `onSlidingComplete` | `(value: number) => void` | — | Called when the finger is lifted, or after a tap on the track. |
152
+ | `accentColor` | `ColorValue` | system blue | Filled part of the track. |
153
+ | `trackColor` | `ColorValue` | translucent grey | Unfilled part of the track. |
142
154
 
143
155
  It stretches to the width of its parent and is 36 dp high by default.
144
156
 
@@ -153,6 +165,8 @@ Kyant's iOS 26 style tab bar: a glass capsule with a liquid selection droplet yo
153
165
  between tabs. Under the droplet the tab content takes the accent colour and is magnified while
154
166
  pressed.
155
167
 
168
+ <img src=".github/assets/tabbar.gif" alt="AndroidGlassBottomTabs" width="480" />
169
+
156
170
  ```tsx
157
171
  <AndroidGlassBottomTabs
158
172
  selectedIndex={tab}
@@ -164,14 +178,14 @@ pressed.
164
178
  </AndroidGlassBottomTabs>
165
179
  ```
166
180
 
167
- | Prop | Type | Default | Description |
168
- | --- | --- | --- | --- |
169
- | `selectedIndex` | `number` | `0` | |
170
- | `onTabSelected` | `(index: number) => void` | — | Called on tap, or when the droplet is dropped on a tab. |
171
- | `minimized` | `boolean` | `false` | Minimized look: shorter and narrower, labels faded out. Animated. See *Minimize on scroll*. |
172
- | `onMinimizedChange` | `(minimized: boolean) => void` | — | Called with `false` when the user expands the minimized bar by touching it. |
173
- | `accentColor` | `ColorValue` | system blue | Colour of the selected tab's content. |
174
- | `containerColor` | `ColorValue` | translucent white | Colour of the glass bar. |
181
+ | Prop | Type | Default | Description |
182
+ | ------------------- | ------------------------------ | ----------------- | ------------------------------------------------------------------------------------------- |
183
+ | `selectedIndex` | `number` | `0` | |
184
+ | `onTabSelected` | `(index: number) => void` | — | Called on tap, or when the droplet is dropped on a tab. |
185
+ | `minimized` | `boolean` | `false` | Minimized look: shorter and narrower, labels faded out. Animated. See _Minimize on scroll_. |
186
+ | `onMinimizedChange` | `(minimized: boolean) => void` | — | Called with `false` when the user expands the minimized bar by touching it. |
187
+ | `accentColor` | `ColorValue` | system blue | Colour of the selected tab's content. |
188
+ | `containerColor` | `ColorValue` | translucent white | Colour of the glass bar. |
175
189
 
176
190
  Each child is one tab (equal widths). `AndroidGlassTab` is an icon over a 12 sp label, but any
177
191
  view works — as with buttons, the tabs are drawn inside the glass, so keep them decorative. The
@@ -184,8 +198,8 @@ droplet returns to `selectedIndex`.
184
198
 
185
199
  ### Minimize on scroll
186
200
 
187
- Like iOS 26 tab bars, the bar can shrink while the user scrolls down a list, and come back when
188
- they scroll up, get back to the top or touch it. It gets shorter and narrower; every tab keeps its
201
+ Added by this package, on top of Kyant's tab bar: the bar can shrink while the user scrolls
202
+ down a list, and come back when they scroll up, get back to the top or touch it. It gets shorter and narrower; every tab keeps its
189
203
  first child (the icon) while the rest (the label) fades out.
190
204
 
191
205
  Wrap the bar and the screens in `MinimizeOnScrollProvider`, and give each list the handler from
@@ -237,7 +251,8 @@ import {
237
251
  function GlassTabBar({ state, descriptors, navigation }: BottomTabBarProps) {
238
252
  // Routes hidden with `href: null` reach a custom bar as `display: 'none'`.
239
253
  const routes = state.routes.filter(
240
- (route) => StyleSheet.flatten(descriptors[route.key].options.tabBarItemStyle)?.display !== 'none'
254
+ (route) =>
255
+ StyleSheet.flatten(descriptors[route.key].options.tabBarItemStyle)?.display !== 'none'
241
256
  );
242
257
  const focusedKey = state.routes[state.index].key;
243
258
  return (
@@ -245,7 +260,11 @@ function GlassTabBar({ state, descriptors, navigation }: BottomTabBarProps) {
245
260
  selectedIndex={routes.findIndex((route) => route.key === focusedKey)}
246
261
  onTabSelected={(index) => {
247
262
  const route = routes[index];
248
- const event = navigation.emit({ type: 'tabPress', target: route.key, canPreventDefault: true });
263
+ const event = navigation.emit({
264
+ type: 'tabPress',
265
+ target: route.key,
266
+ canPreventDefault: true,
267
+ });
249
268
  if (!event.defaultPrevented) navigation.navigate(route.name, route.params);
250
269
  }}
251
270
  style={{ position: 'absolute', left: 24, right: 24, bottom: 32 }}>
@@ -298,26 +317,104 @@ are told a native gesture started. Vertical drags still scroll.
298
317
 
299
318
  ## How it works
300
319
 
301
- Each glass view is a React Native view whose first child is a Compose view drawing Kyant's
302
- `drawBackdrop` modifier. Its backdrop is not a Compose layer but the Android view tree of the
303
- window — i.e. your React Native screen:
304
-
305
- 1. Every container on the path from the window root to a glass view is drawn with the public
306
- `View.draw()`, with glass views (and branches containing them) hidden for that call.
307
- 2. Every other subtree is recorded as a reference to its RenderNode, so when a list scrolls or a
308
- view animates, the glass picks it up without re-sampling.
309
- 3. Glass views don't sample other glass views, except their own ancestors: a toggle on a glass
310
- card sees the card. This rules out RenderNode cycles (glass A → glass B → glass A).
311
- 4. The capture is recorded once into a RenderNode with its own GPU layer, and only recorded again
312
- when the glass moves, something scrolls or glass views come and go. Every glass layer of a view
313
- (the tab bar has three) samples that same texture, so pressing or dragging only re-runs the
314
- glass effects, not the capture.
320
+ In short: every glass view takes a live snapshot of what is drawn behind it, runs it through a
321
+ GPU lens (blur, refraction, vibrancy) and draws the result in its own shape.
322
+
323
+ ### A Compose layer inside a React Native view
324
+
325
+ Each glass component is a React Native view (`GlassHostView`) whose first child is a Jetpack
326
+ Compose view drawing Kyant's `drawBackdrop` modifier. Your React children are regular React
327
+ Native views on top of that layer — except in `AndroidGlassButton`, where they are drawn _inside_
328
+ the glass so they stretch and swell with it.
329
+
330
+ ### Seeing the React Native screen behind it
331
+
332
+ Kyant's library can only see Compose content behind a glass. Here the backdrop is the Android
333
+ view tree of the window — your React Native screen — captured with an approach adapted from
334
+ [QWEA0/Liquid-Glass-Android](https://github.com/QWEA0/Liquid-Glass-Android):
335
+
336
+ 1. The glass records the area of the window around itself (plus a 32 dp margin for blur and for
337
+ glass that grows while pressed) into a `RenderNode`.
338
+ 2. Its own ancestors are being drawn at that very moment, and calling their `draw()` again would
339
+ crash, so their background and children are drawn one by one instead, in their real drawing
340
+ order (`zIndex` included).
341
+ 3. Every other subtree is recorded as a _reference_ to its RenderNode rather than as pixels, so
342
+ when content behind the glass animates, the glass shows it without capturing again.
343
+ 4. Other glass views are left out of the capture, except the glass's own ancestors: a toggle on
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.
348
+
349
+ ### The effects
350
+
351
+ The capture goes through Kyant's effect chain on the GPU: blur (`RenderEffect`, Android 12+),
352
+ refraction at the rim (an AGSL runtime shader, Android 13+) with optional chromatic aberration,
353
+ vibrancy, a rim highlight and a drop shadow. Where these APIs are missing, the glass falls back
354
+ to a flat `fallbackColor` surface.
355
+
356
+ ### When it captures again
357
+
358
+ The capture is cached in one RenderNode with its own GPU layer, and every glass layer of a view
359
+ (the tab bar has three) samples that same texture. It is recorded again only when:
360
+
361
+ - the glass moves on screen (layout, transform, dragging),
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,
366
+ - a glass view is added or removed.
367
+
368
+ A glass view that is off screen waits until it is visible again. Pressing or dragging a glass
369
+ control only re-runs the GPU effects, not the capture.
370
+
371
+ ### Controlled props
372
+
373
+ Toggles, sliders and the tab bar are dragged natively. So that a late `value` from JS never
374
+ pulls the thumb back, a prop only wins once JS has handled every event the control sent — the
375
+ handshake React Native's `TextInput` uses.
376
+
377
+ ### Minimize on scroll
378
+
379
+ JS decides the direction (`useMinimizeOnScrollHandler`) and sends the bar a single boolean; the
380
+ shrinking itself is animated natively in Compose. The provider shares that state between every
381
+ screen.
382
+
383
+ ## Performance
384
+
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_).
389
+
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.
392
+
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 |
398
+
399
+ In practice:
400
+
401
+ - A glass header, a tab bar and a few glass controls on a scrolling screen stay smooth.
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.
405
+ - Don't put a glass view inside every item of a long list: each visible item captures and
406
+ redraws on every scroll frame.
407
+ - Glass views scrolled out of view don't capture again until they are visible.
408
+ - On Android 11 and older the glass is a flat surface and costs nothing extra.
315
409
 
316
410
  ## Limitations
317
411
 
318
412
  - **Glass does not show other glass views** behind it, unless it sits inside them.
319
413
  - Inside a container that holds a glass view, branches containing glass are drawn after their
320
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.
321
418
  - `SurfaceView`-based content (most video players, camera previews, some maps) is not part of the
322
419
  view tree's drawing and shows up empty behind the glass. `TextureView` content works.
323
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) {