@oxy.so/bloom-lens 0.0.0-stage → 1.0.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 (108) hide show
  1. package/LICENSE +673 -0
  2. package/NOTICE +26 -0
  3. package/android/build.gradle +19 -0
  4. package/android/src/main/AndroidManifest.xml +1 -0
  5. package/android/src/main/assets/fonts/Inter-OFL.txt +93 -0
  6. package/android/src/main/assets/fonts/Inter-Regular.ttf +0 -0
  7. package/android/src/main/java/so/oxy/bloom/glassmenu/220 +0 -0
  8. package/android/src/main/java/so/oxy/bloom/glassmenu/GlassMenuAndroidModule.kt +71 -0
  9. package/android/src/main/java/so/oxy/bloom/glassmenu/GlassMenuAndroidView.kt +548 -0
  10. package/android/src/main/java/so/oxy/bloom/glassmenu/backdrop/GlassBackdrop.kt +215 -0
  11. package/android/src/main/java/so/oxy/bloom/glassmenu/backdrop/GlassLens.kt +103 -0
  12. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/ChromeGeometry.kt +73 -0
  13. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/ChromeIcons.kt +74 -0
  14. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/ChromeMaterial.kt +74 -0
  15. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/GlassChromeHost.kt +73 -0
  16. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/GlassChromeModule.kt +31 -0
  17. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/GlassChromeView.kt +609 -0
  18. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/StaticGlassCache.kt +73 -0
  19. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceIcons.kt +114 -0
  20. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceNodesHost.kt +71 -0
  21. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceNodesModule.kt +22 -0
  22. package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceNodesView.kt +527 -0
  23. package/android/src/main/java/so/oxy/bloom/glassmenu/content/GlassIcons.kt +68 -0
  24. package/android/src/main/java/so/oxy/bloom/glassmenu/content/GlassMenuContent.kt +349 -0
  25. package/android/src/main/java/so/oxy/bloom/glassmenu/content/GlassRow.kt +19 -0
  26. package/android/src/main/java/so/oxy/bloom/glassmenu/field/GlassFieldTexture.kt +102 -0
  27. package/android/src/main/java/so/oxy/bloom/glassmenu/field/GlassGaussianField.kt +161 -0
  28. package/android/src/main/java/so/oxy/bloom/glassmenu/field/GlassShapes.kt +37 -0
  29. package/android/src/main/java/so/oxy/bloom/glassmenu/field/RoundedField.kt +119 -0
  30. package/android/src/main/java/so/oxy/bloom/glassmenu/field/RoundedUnion.kt +81 -0
  31. package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassGroup.kt +225 -0
  32. package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassJelly.kt +118 -0
  33. package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassMorphFrame.kt +22 -0
  34. package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassMorphTuning.kt +31 -0
  35. package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassMotion.kt +475 -0
  36. package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassSpring.kt +78 -0
  37. package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassDevGuard.kt +31 -0
  38. package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassFeedback.kt +18 -0
  39. package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassMenuOverlay.kt +433 -0
  40. package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassMenuSession.kt +750 -0
  41. package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassScrub.kt +41 -0
  42. package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassSubmenuLayer.kt +290 -0
  43. package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassAbsorbRecord.kt +9 -0
  44. package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassMemberRecord.kt +14 -0
  45. package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassMenuConfigRecord.kt +24 -0
  46. package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassMenuItemRecord.kt +23 -0
  47. package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassShrinkRecord.kt +10 -0
  48. package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassTriggerConfigRecord.kt +19 -0
  49. package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassRecipe.kt +18 -0
  50. package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassRenderer.kt +361 -0
  51. package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassShaders.kt +318 -0
  52. package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassStill.kt +115 -0
  53. package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassSurfaceState.kt +38 -0
  54. package/expo-module.config.json +10 -0
  55. package/package.json +29 -4
  56. package/patches/bloom.patch +1908 -0
  57. package/patches/menu/bloom.patch +148 -0
  58. package/src/base.tsx +208 -0
  59. package/src/chrome/compositor-geometry.ts +163 -0
  60. package/src/chrome/glass-header.tsx +155 -0
  61. package/src/chrome/glass-search-fab.tsx +113 -0
  62. package/src/chrome/glass-size-class.tsx +150 -0
  63. package/src/chrome/glass-toolbar.tsx +45 -0
  64. package/src/chrome/interface.ts +17 -0
  65. package/src/chrome/ios-tab-bar.tsx +49 -0
  66. package/src/chrome/surface-layer.ts +91 -0
  67. package/src/chrome/surface-nodes.tsx +54 -0
  68. package/src/index.ts +19 -0
  69. package/src/menu/components/menu/index.ts +22 -0
  70. package/src/menu/components/menu/menu.tsx +97 -0
  71. package/src/menu/components/menu-action-row/index.ts +1 -0
  72. package/src/menu/components/menu-action-row/menu-action-row.tsx +12 -0
  73. package/src/menu/components/menu-content/index.ts +2 -0
  74. package/src/menu/components/menu-content/menu-content.tsx +11 -0
  75. package/src/menu/components/menu-item/index.ts +13 -0
  76. package/src/menu/components/menu-item/menu-item-icon.tsx +10 -0
  77. package/src/menu/components/menu-item/menu-item-subtitle.tsx +14 -0
  78. package/src/menu/components/menu-item/menu-item-title.tsx +11 -0
  79. package/src/menu/components/menu-item/menu-item.tsx +11 -0
  80. package/src/menu/components/menu-label/index.ts +3 -0
  81. package/src/menu/components/menu-label/menu-label.tsx +11 -0
  82. package/src/menu/components/menu-separator/index.ts +3 -0
  83. package/src/menu/components/menu-separator/menu-separator.tsx +7 -0
  84. package/src/menu/components/menu-submenu/index.ts +1 -0
  85. package/src/menu/components/menu-submenu/menu-submenu.tsx +12 -0
  86. package/src/menu/components/menu-trigger/index.ts +3 -0
  87. package/src/menu/components/menu-trigger/interface.ts +41 -0
  88. package/src/menu/components/menu-trigger/menu-trigger.android.tsx +97 -0
  89. package/src/menu/components/menu-trigger/menu-trigger.tsx +15 -0
  90. package/src/menu/components/menu-trigger/native-menu.android.ts +9 -0
  91. package/src/menu/components/menu-trigger-button/index.ts +3 -0
  92. package/src/menu/components/menu-trigger-button/menu-trigger-button.android.tsx +38 -0
  93. package/src/menu/components/menu-trigger-button/menu-trigger-button.tsx +17 -0
  94. package/src/menu/constants/interface.ts +6 -0
  95. package/src/menu/constants/menu.const.ts +11 -0
  96. package/src/menu/context/interface.ts +63 -0
  97. package/src/menu/context/menu.context.ts +7 -0
  98. package/src/menu/index.ts +53 -0
  99. package/src/menu/interface.ts +229 -0
  100. package/src/menu/utils/create-compound-component.util.ts +18 -0
  101. package/src/menu/utils/find-menu-content.util.ts +17 -0
  102. package/src/menu/utils/flatten-elements.util.ts +18 -0
  103. package/src/menu/utils/parse-menu-content.util.ts +98 -0
  104. package/src/menu/utils/parse-menu-item.util.ts +56 -0
  105. package/src/menu/utils/register-component.util.ts +13 -0
  106. package/src/menu/utils/to-native-item.util.ts +24 -0
  107. package/src/rows.tsx +61 -0
  108. package/README.md +0 -3
@@ -0,0 +1,548 @@
1
+ package so.oxy.bloom.glassmenu
2
+
3
+ import android.content.Context
4
+ import android.content.res.Configuration
5
+ import android.graphics.Canvas
6
+ import android.graphics.Rect
7
+ import android.graphics.RectF
8
+ import android.graphics.RenderNode
9
+ import android.os.Build
10
+ import android.os.SystemClock
11
+ import android.os.Trace
12
+ import android.view.ViewConfiguration
13
+ import android.view.MotionEvent
14
+ import android.view.View
15
+ import android.view.ViewGroup
16
+ import androidx.annotation.RequiresApi
17
+ import com.facebook.react.R
18
+ import so.oxy.bloom.glassmenu.backdrop.GlassBackdrop
19
+ import so.oxy.bloom.glassmenu.motion.GlassGroup
20
+ import so.oxy.bloom.glassmenu.motion.GlassJelly
21
+ import so.oxy.bloom.glassmenu.overlay.GlassDevGuard
22
+ import so.oxy.bloom.glassmenu.overlay.GlassMenuOverlay
23
+ import so.oxy.bloom.glassmenu.records.GlassMenuConfigRecord
24
+ import so.oxy.bloom.glassmenu.records.GlassMemberRecord
25
+ import so.oxy.bloom.glassmenu.records.GlassMenuItemRecord
26
+ import so.oxy.bloom.glassmenu.records.GlassTriggerConfigRecord
27
+ import so.oxy.bloom.glassmenu.render.GlassRenderer
28
+ import so.oxy.bloom.glassmenu.render.GlassSurfaceState
29
+ import so.oxy.bloom.glassmenu.render.SharedStills
30
+ import expo.modules.kotlin.AppContext
31
+ import expo.modules.kotlin.viewevent.EventDispatcher
32
+ import expo.modules.kotlin.views.ExpoView
33
+ import kotlin.math.abs
34
+ import kotlin.math.min
35
+
36
+ class GlassMenuAndroidView(context: Context, appContext: AppContext) : ExpoView(context, appContext), GlassBackdrop.OffPage {
37
+ private val onItemSelect by EventDispatcher<Map<String, Any?>>()
38
+ private val onMenuOpenChange by EventDispatcher<Map<String, Any?>>()
39
+ private val onMenuClosed by EventDispatcher<Map<String, Any?>>()
40
+
41
+ private var overlay: GlassMenuOverlay? = null
42
+ private var glassRenderer: Any? = null
43
+ private var contentNode: Any? = null
44
+ private var menuActive = false
45
+ private val density = resources.displayMetrics.density
46
+ internal val jelly = GlassJelly()
47
+ private var pressX = 0f
48
+ private var pressY = 0f
49
+ private var pressTime = 0L
50
+ internal val placement = RectF()
51
+ internal var placementAlpha = -1f
52
+ internal var triggerConfig = GlassTriggerConfigRecord()
53
+ set(value) {
54
+ field = value
55
+ overlay?.invalidate()
56
+ }
57
+ internal var config = GlassMenuConfigRecord()
58
+ set(value) {
59
+ field = value
60
+ overlay?.invalidate()
61
+ }
62
+
63
+ internal val surfaceState = GlassSurfaceState()
64
+ internal var items: List<GlassMenuItemRecord> = emptyList()
65
+ set(value) {
66
+ field = value
67
+ overlay?.menuSession?.takeIf { it.anchor === this }?.refreshItems()
68
+ openWhenReady()
69
+ }
70
+ // An open the owner asks for together with the rows it opens with (rows built by the press that opens it, or rows
71
+ // swapped in for it): while set, the view opens itself, once, as soon as it has rows and is laid out, since a call
72
+ // to `open` could arrive before the rows do. Set false, it opens again the next time it is set.
73
+ internal var openWithItems = false
74
+ set(value) {
75
+ field = value
76
+ if (!value) openedWithItems = false
77
+ openWhenReady()
78
+ }
79
+ private var openedWithItems = false
80
+ internal var glass = true
81
+ set(value) {
82
+ field = value
83
+ refreshDrawing()
84
+ }
85
+ // Morph from the trigger without glass: a glass trigger always morphs (its glass is the origin); a plain trigger
86
+ // morphs only when this is set. The plain trigger is drawn by the hierarchy at rest and by the session while open.
87
+ internal var morph = true
88
+ set(value) {
89
+ field = value
90
+ overlay?.invalidate()
91
+ }
92
+ // The session takes the trigger as its origin (a lobe with glass, the tint and radius of the trigger) when true.
93
+ internal val morphs get() = glass || morph
94
+ internal var cornerRadius: Float? = null
95
+ set(value) {
96
+ field = value
97
+ overlay?.invalidate()
98
+ }
99
+ internal var colorScheme = "auto"
100
+ set(value) {
101
+ field = value
102
+ overlay?.invalidate()
103
+ }
104
+ // The colour the trigger sits on. Set, the view draws its own lens at rest, in its place in the view hierarchy (so
105
+ // a trigger in a scroll is clipped and stacked as the content it belongs to, under the bars), against that colour:
106
+ // at rest the shared still of its look (SharedStills: one texture for every trigger that looks the same, no layer
107
+ // of its own), and while pressed its live lens in a compositing layer. The window overlay draws the
108
+ // lens only while the menu is open: the session starts from the same base. A live lens inside the hierarchy cannot
109
+ // sample the window backdrop (the backdrop records the page, which would hold the lens: a RenderNode cycle).
110
+ internal var restBackdrop: Int? = null
111
+ set(value) {
112
+ field = value
113
+ restRecorded = false
114
+ refreshDrawing()
115
+ }
116
+ internal val inPlace get() = restBackdrop != null
117
+ private var restRenderer: Any? = null
118
+ private var restNode: Any? = null
119
+ private var restBackdropNode: Any? = null
120
+ private var restRecorded = false
121
+ // The look of the resting lens, the key of its shared still.
122
+ private var restLook: RestLook? = null
123
+ private val restRect = RectF()
124
+ private val restLimits = Rect()
125
+ // A tap on the view opens its menu right there, on the UI thread, with no round trip to JS: the menu is the view's
126
+ // own (not one of several in a group, which their own presses open). Only with rows: a menu whose rows are built
127
+ // by its press is opened by its owner as they arrive (openWithItems). The owner's own press handler may still
128
+ // call `open`, which finds the menu open and does nothing.
129
+ internal var openOnTap = false
130
+ private var tapping = false
131
+ private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
132
+ // A group of glasses on this base (GlassGroup): the base draws each member's glass, not its own rect. Null: one glass.
133
+ // Drawn by the overlay only: a base drawn in its place (restBackdrop) is its own rect.
134
+ internal val group = GlassGroup()
135
+ internal var members: List<GlassMemberRecord>? = null
136
+ set(value) {
137
+ field = value
138
+ group.setRecords(value)
139
+ overlay?.invalidate()
140
+ }
141
+ // The distance in points within which a group's glasses blend into one another.
142
+ internal var blend = 0f
143
+ set(value) {
144
+ field = value
145
+ group.blend = value
146
+ overlay?.invalidate()
147
+ }
148
+ internal var menuWidth: Float? = null
149
+ internal var fontFamily: String? = null
150
+ internal var alignment = "auto"
151
+
152
+ private val supportsGlass get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
153
+
154
+ internal val drawsGlass get() = glass && supportsGlass
155
+
156
+ private val overlayDrawsChildren get() = supportsGlass && overlay != null && ((glass && !inPlace) || menuActive)
157
+
158
+ // Drawn by the overlay, the view's lens and content are not part of the page the glass samples.
159
+ override val offPage get() = overlayDrawsChildren
160
+
161
+ // The lens this view draws in its own place: at rest, when it sits in the hierarchy (restBackdrop).
162
+ private val drawsRestInPlace get() = inPlace && drawsGlass && !menuActive
163
+
164
+ init {
165
+ setWillNotDraw(false)
166
+ }
167
+
168
+ override fun onAttachedToWindow() {
169
+ super.onAttachedToWindow()
170
+ val root = rootView as? ViewGroup ?: return
171
+ overlay = GlassMenuOverlay.obtain(root).also { it.register(this) }
172
+ if (drawsGlass && !inPlace) GlassDevGuard.warnIfInOverscrollScroll(this)
173
+ refreshDrawing()
174
+ openWhenReady()
175
+ }
176
+
177
+ override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
178
+ super.onSizeChanged(w, h, oldw, oldh)
179
+ restRecorded = false
180
+ openWhenReady()
181
+ }
182
+
183
+ private fun openWhenReady() {
184
+ if (!openWithItems || openedWithItems || overlay == null || items.isEmpty() || width == 0 || height == 0) return
185
+ openedWithItems = true
186
+ // After the rest of this layout pass, so the anchor is measured where it ends up.
187
+ post { open(null, null, null, null) }
188
+ }
189
+
190
+ override fun onDetachedFromWindow() {
191
+ overlay?.unregister(this)
192
+ overlay = null
193
+ menuActive = false
194
+ if (supportsGlass) releaseGraphics()
195
+ super.onDetachedFromWindow()
196
+ }
197
+
198
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
199
+ private fun releaseGraphics() {
200
+ (glassRenderer as? GlassRenderer)?.release()
201
+ glassRenderer = null
202
+ (restRenderer as? GlassRenderer)?.release()
203
+ restRenderer = null
204
+ (restNode as? RenderNode)?.discardDisplayList()
205
+ restNode = null
206
+ (restBackdropNode as? RenderNode)?.discardDisplayList()
207
+ restBackdropNode = null
208
+ restLook?.let { SharedStills.release(it) }
209
+ restLook = null
210
+ restRecorded = false
211
+ (contentNode as? RenderNode)?.discardDisplayList()
212
+ contentNode = null
213
+ }
214
+
215
+ private fun refreshDrawing() {
216
+ restRecorded = false
217
+ invalidate()
218
+ overlay?.invalidate()
219
+ }
220
+
221
+ // A frame of the press jelly (the overlay runs the frame loop): a lens drawn in place records its new shape.
222
+ internal fun jellyFrame() {
223
+ if (!drawsRestInPlace) return
224
+ restRecorded = false
225
+ invalidate()
226
+ }
227
+
228
+ override fun onDraw(canvas: Canvas) {
229
+ super.onDraw(canvas)
230
+ if (supportsGlass && drawsRestInPlace && canvas.isHardwareAccelerated) drawRestInPlace(canvas)
231
+ }
232
+
233
+ // The resting lens, as the overlay draws a resting trigger (drawRestingAnchor), in this view's coordinates and
234
+ // against the colour it sits on. At rest the shared still of its look; pressed, recorded into a compositing layer,
235
+ // again only when its shape, colour or press changes.
236
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
237
+ private fun drawRestInPlace(canvas: Canvas) {
238
+ if (width == 0 || height == 0) return
239
+ val renderer = restRenderer as? GlassRenderer ?: newRenderer(density).also { restRenderer = it }
240
+ if (jelly.isSettled && drawRestStill(canvas, renderer)) return
241
+ val node = restNode as? RenderNode ?: RenderNode("GlassMenu.restInPlace").also {
242
+ it.setUseCompositingLayer(true, null)
243
+ restNode = it
244
+ }
245
+ if (!restRecorded) {
246
+ val backdrop = prepareRest()
247
+ node.setPosition(restLimits)
248
+ val recording = node.beginRecording(restLimits.width(), restLimits.height())
249
+ recording.translate(-restLimits.left.toFloat(), -restLimits.top.toFloat())
250
+ renderer.prepare(surfaceState)
251
+ renderer.drawGlass(recording, backdrop, surfaceState, restLimits)
252
+ node.endRecording()
253
+ restRecorded = !renderer.warmPipelines(canvas)
254
+ if (!restRecorded) postInvalidateOnAnimation()
255
+ }
256
+ canvas.drawRenderNode(node)
257
+ }
258
+
259
+ // The settled lens: the still of its look, shared with every trigger that looks the same. False until it has
260
+ // arrived (the view is invalidated then); the live lens is drawn meanwhile.
261
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
262
+ private fun drawRestStill(canvas: Canvas, renderer: GlassRenderer): Boolean {
263
+ val backdrop = prepareRest()
264
+ val state = surfaceState
265
+ val look = RestLook(
266
+ restLimits.width(), restLimits.height(), state.radiusX, restBackdrop ?: 0, state.dark, density,
267
+ config.blur, config.tintColor?.toArgb(), config.tintOpacity, config.saturation, config.refraction, config.rim,
268
+ triggerConfig.blur, triggerConfig.tintColor?.toArgb(), triggerConfig.tintOpacity, triggerConfig.saturation,
269
+ triggerConfig.refraction, triggerConfig.rim,
270
+ )
271
+ if (look != restLook) {
272
+ restLook?.let { SharedStills.release(it) }
273
+ restLook = look
274
+ val limits = Rect(restLimits)
275
+ SharedStills.acquire(look, limits.width(), limits.height()) { recording ->
276
+ recording.translate(-limits.left.toFloat(), -limits.top.toFloat())
277
+ renderer.prepare(state)
278
+ renderer.drawGlass(recording, backdrop, state, limits)
279
+ }
280
+ }
281
+ val still = SharedStills.bitmap(look, this) ?: return false
282
+ if (restNode != null) {
283
+ (restNode as RenderNode).discardDisplayList()
284
+ restNode = null
285
+ restRecorded = false
286
+ }
287
+ canvas.drawBitmap(still, restLimits.left.toFloat(), restLimits.top.toFloat(), null)
288
+ if (renderer.warmPipelines(canvas)) postInvalidateOnAnimation()
289
+ return true
290
+ }
291
+
292
+ // The resting lens's limits and state, in this view's coordinates, and the fill it samples.
293
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
294
+ private fun prepareRest(): RenderNode {
295
+ val pad = (RestCaptureMargin + RestGuard) * density
296
+ restLimits.set(-pad.toInt(), -pad.toInt(), width + pad.toInt(), height + pad.toInt())
297
+ val backdrop = restBackdropNode as? RenderNode ?: RenderNode("GlassMenu.restBackdrop").also { restBackdropNode = it }
298
+ backdrop.setPosition(restLimits)
299
+ val fill = backdrop.beginRecording(restLimits.width(), restLimits.height())
300
+ fill.drawColor(restBackdrop ?: 0)
301
+ backdrop.endRecording()
302
+
303
+ val state = surfaceState
304
+ restRect.set(0f, 0f, width.toFloat(), height.toFloat())
305
+ state.incoming.set(restRect)
306
+ state.captureBounds.set(restRect)
307
+ state.captureBounds.inset(-RestCaptureMargin * density, -RestCaptureMargin * density)
308
+ state.lockedFieldStep = 0f
309
+ jelly.deformControl(state.incoming, density, triggerConfig.jelly)
310
+ val radius = cornerRadiusPx(state.incoming)
311
+ state.radiusX = radius
312
+ state.radiusY = radius
313
+ state.hasSource = false
314
+ state.members.clear()
315
+ state.smoothing = 0f
316
+ state.lensHeight = 0f
317
+ state.lensAmount = 0f
318
+ state.materialProgress = 0f
319
+ state.surfaceOpacity = 1f
320
+ state.dark = isDark()
321
+ state.config = config
322
+ state.trigger = triggerConfig
323
+ applyLight(state, restRect, 1f)
324
+ return backdrop
325
+ }
326
+
327
+ // What the resting lens drawn in place looks like: its size, shape, fill, scheme and material. Triggers that agree
328
+ // on all of it draw the same pixels, so they share one still.
329
+ private data class RestLook(
330
+ val width: Int, val height: Int, val radius: Float, val fill: Int, val dark: Boolean, val density: Float,
331
+ val blur: Float, val tint: Int?, val tintOpacity: Float?, val saturation: Float, val refraction: Float, val rim: Float,
332
+ val triggerBlur: Float, val triggerTint: Int?, val triggerTintOpacity: Float?, val triggerSaturation: Float,
333
+ val triggerRefraction: Float, val triggerRim: Float,
334
+ )
335
+
336
+ override fun dispatchTouchEvent(event: MotionEvent): Boolean {
337
+ when (event.actionMasked) {
338
+ MotionEvent.ACTION_DOWN -> {
339
+ pressX = event.x
340
+ pressY = event.y
341
+ pressTime = SystemClock.uptimeMillis()
342
+ tapping = openOnTap && items.isNotEmpty()
343
+ }
344
+ MotionEvent.ACTION_MOVE ->
345
+ if (abs(event.x - pressX) > touchSlop || abs(event.y - pressY) > touchSlop) tapping = false
346
+ MotionEvent.ACTION_CANCEL -> tapping = false
347
+ MotionEvent.ACTION_UP -> if (tapping) {
348
+ tapping = false
349
+ val inside = event.x >= 0f && event.y >= 0f && event.x <= width && event.y <= height
350
+ if (inside && SystemClock.uptimeMillis() - pressTime < ViewConfiguration.getLongPressTimeout()) {
351
+ open(null, null, null, null)
352
+ }
353
+ }
354
+ }
355
+ if (drawsGlass) {
356
+ when (event.actionMasked) {
357
+ MotionEvent.ACTION_DOWN -> {
358
+ jelly.down(event.x, event.y)
359
+ overlay?.requestFrame()
360
+ }
361
+ MotionEvent.ACTION_MOVE -> {
362
+ jelly.move(event.x, event.y)
363
+ overlay?.requestFrame()
364
+ }
365
+ MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
366
+ jelly.release()
367
+ overlay?.requestFrame()
368
+ }
369
+ }
370
+ }
371
+ return super.dispatchTouchEvent(event)
372
+ }
373
+
374
+ internal fun applyLight(state: GlassSurfaceState, origin: RectF, opacity: Float) {
375
+ val glow = jelly.glowValue * opacity * triggerConfig.shine
376
+ state.pressX = origin.left + jelly.lightPositionX
377
+ state.pressY = origin.top + jelly.lightPositionY
378
+ state.pressRadius = LightRadius * density
379
+ state.poolGain = LightPool * glow
380
+ state.pressLift = LightLift * glow
381
+ state.rimLift = LightRimLift * glow
382
+ }
383
+
384
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
385
+ setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec))
386
+ }
387
+
388
+ override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) = Unit
389
+
390
+ private fun iconHost(): ViewGroup? {
391
+ for (index in 0 until childCount) {
392
+ val child = getChildAt(index)
393
+ if (child.getTag(R.id.view_tag_native_id) == IconHostId) return child as? ViewGroup
394
+ }
395
+ return null
396
+ }
397
+
398
+ internal fun iconView(slot: Int): View? = iconHost()?.getChildAt(slot)
399
+
400
+ override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
401
+ if (child.getTag(R.id.view_tag_native_id) == IconHostId) return false
402
+ return super.drawChild(canvas, child, drawingTime)
403
+ }
404
+
405
+ override fun dispatchDraw(canvas: Canvas) {
406
+ if (overlayDrawsChildren) return
407
+ if (!drawsRestInPlace || jelly.isSettled) {
408
+ super.dispatchDraw(canvas)
409
+ return
410
+ }
411
+ // Pressed, the content follows the lens's jelly, as the overlay scales a resting trigger's content.
412
+ val shape = surfaceState.incoming
413
+ val save = canvas.save()
414
+ canvas.translate(shape.left, shape.top)
415
+ canvas.scale(shape.width() / width, shape.height() / height)
416
+ super.dispatchDraw(canvas)
417
+ canvas.restoreToCount(save)
418
+ }
419
+
420
+ // The content again, into the node the overlay already draws, with the node's placement kept: content that redrew
421
+ // (a trigger drawn by the overlay keeps it out of the page, dispatchDraw) reaches the screen without the overlay
422
+ // recording anything else.
423
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
424
+ internal fun refreshContent() {
425
+ val node = contentNode as? RenderNode ?: return
426
+ val recording = node.beginRecording(width, height)
427
+ super.dispatchDraw(recording)
428
+ node.endRecording()
429
+ }
430
+
431
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
432
+ internal fun recordContent(): RenderNode {
433
+ val node = contentNode as? RenderNode ?: RenderNode("GlassMenu.trigger").also { contentNode = it }
434
+ node.setPosition(0, 0, width, height)
435
+ node.setPivotX(0f)
436
+ node.setPivotY(0f)
437
+ node.setScaleX(1f)
438
+ node.setScaleY(1f)
439
+ node.setTranslationX(0f)
440
+ node.setTranslationY(0f)
441
+ node.setRenderEffect(null)
442
+ val recording = node.beginRecording(width, height)
443
+ super.dispatchDraw(recording)
444
+ node.endRecording()
445
+ return node
446
+ }
447
+
448
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
449
+ internal fun renderer(pixelDensity: Float): GlassRenderer =
450
+ glassRenderer as? GlassRenderer ?: GlassRenderer(pixelDensity, pixelDensity * FieldStep).also { glassRenderer = it }
451
+
452
+ // A separate renderer for each stacked panel: a renderer records into its own shared RenderNodes, so two
453
+ // panels drawn from one renderer in the same frame would overwrite each other's recordings.
454
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
455
+ internal fun newRenderer(pixelDensity: Float): GlassRenderer = GlassRenderer(pixelDensity, pixelDensity * FieldStep)
456
+
457
+ internal fun isDark(): Boolean = when (colorScheme) {
458
+ "dark" -> true
459
+ "light" -> false
460
+ else -> resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
461
+ }
462
+
463
+ internal fun cornerRadiusPx(bounds: RectF): Float {
464
+ val capsule = min(bounds.width(), bounds.height()) / 2f
465
+ return min(cornerRadius?.times(density) ?: capsule, capsule)
466
+ }
467
+
468
+ internal fun effectiveAlpha(): Float {
469
+ var result = 1f
470
+ var current: View? = this
471
+ while (current != null) {
472
+ result *= current.alpha
473
+ current = current.parent as? View
474
+ }
475
+ return result
476
+ }
477
+
478
+ fun open(x: Double?, y: Double?, width: Double?, height: Double?) {
479
+ val target = overlay ?: return
480
+ if (items.isEmpty() || this.width == 0 || this.height == 0) return
481
+ if (menuActive && target.menuSession?.expanded == true) return
482
+ // Marks the open in a system trace (atrace `view`), for tap-to-first-frame measurements.
483
+ Trace.beginSection("BloomLens.open")
484
+ try {
485
+ openNow(x, y, width, height, target)
486
+ } finally {
487
+ Trace.endSection()
488
+ }
489
+ }
490
+
491
+ private fun openNow(x: Double?, y: Double?, width: Double?, height: Double?, target: GlassMenuOverlay) {
492
+ val pressed = if (followsPress) pressedRect(target) else null
493
+ val through = pressed ?: if (x != null && y != null && width != null && height != null) {
494
+ RectF(
495
+ (x * density).toFloat(), (y * density).toFloat(),
496
+ ((x + width) * density).toFloat(), ((y + height) * density).toFloat(),
497
+ )
498
+ } else null
499
+ menuActive = true
500
+ target.open(this, through)
501
+ invalidate()
502
+ }
503
+
504
+ internal val followsPress get() = triggerConfig.morphOrigin == MorphFromPress
505
+
506
+ private fun pressedRect(target: GlassMenuOverlay): RectF? {
507
+ if (SystemClock.uptimeMillis() - pressTime > PressMemory) return null
508
+ val bounds = RectF()
509
+ target.anchorRect(this, bounds)
510
+ val half = min(bounds.height() / 2f, PressRadius * density)
511
+ val centerX = (bounds.left + pressX).coerceIn(bounds.left + half, bounds.right - half)
512
+ val centerY = (bounds.top + pressY).coerceIn(bounds.top + half, bounds.bottom - half)
513
+ return RectF(centerX - half, centerY - half, centerX + half, centerY + half)
514
+ }
515
+
516
+ fun close() {
517
+ overlay?.takeIf { it.menuSession?.anchor === this }?.requestClose()
518
+ }
519
+
520
+ internal fun notifySelect(item: GlassMenuItemRecord, index: Int) {
521
+ onItemSelect(mapOf("id" to item.id, "index" to index, "title" to item.title))
522
+ }
523
+
524
+ internal fun notifyOpenChange(open: Boolean) {
525
+ onMenuOpenChange(mapOf("open" to open))
526
+ }
527
+
528
+ internal fun onSessionFinished() {
529
+ menuActive = false
530
+ invalidate()
531
+ onMenuClosed(mapOf())
532
+ }
533
+
534
+ internal companion object {
535
+ const val FieldStep = 4f
536
+ const val IconHostId = "lens-icons"
537
+ const val PressMemory = 1000L
538
+ const val MorphFromPress = "press"
539
+ const val PressRadius = 22f
540
+ const val LightRadius = 38f
541
+ const val LightLift = .055f
542
+ const val LightPool = .07f
543
+ const val LightRimLift = .3f
544
+ // The resting capture margin around a trigger (the overlay's RestCaptureMargin) and the blur's guard beyond it.
545
+ const val RestCaptureMargin = 16f
546
+ const val RestGuard = 24f
547
+ }
548
+ }