@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,527 @@
1
+ package so.oxy.bloom.glassmenu.chrome
2
+
3
+ import android.content.Context
4
+ import android.graphics.Canvas
5
+ import android.graphics.Color
6
+ import android.graphics.Paint
7
+ import android.graphics.Rect
8
+ import android.graphics.RectF
9
+ import android.graphics.Typeface
10
+ import android.text.Editable
11
+ import android.text.TextWatcher
12
+ import android.util.TypedValue
13
+ import android.view.Choreographer
14
+ import android.view.MotionEvent
15
+ import android.view.View
16
+ import android.view.ViewConfiguration
17
+ import android.view.ViewGroup
18
+ import android.widget.EditText
19
+ import android.widget.FrameLayout
20
+ import so.oxy.bloom.glassmenu.backdrop.GlassBackdrop
21
+ import so.oxy.bloom.glassmenu.motion.GlassJelly
22
+ import so.oxy.bloom.glassmenu.motion.GlassMotion
23
+ import so.oxy.bloom.glassmenu.motion.GlassSpring
24
+ import so.oxy.bloom.glassmenu.motion.GlassSpringSpec
25
+ import so.oxy.bloom.glassmenu.overlay.GlassFeedback
26
+ import so.oxy.bloom.glassmenu.overlay.GlassScrub
27
+ import so.oxy.bloom.glassmenu.render.GlassRenderer
28
+ import so.oxy.bloom.glassmenu.render.GlassSurfaceState
29
+ import kotlin.math.abs
30
+ import kotlin.math.min
31
+
32
+ // One node of a glass layer (chrome/surface-nodes.ts). Geometry in dp, window coordinates.
33
+ internal data class SurfaceNode(
34
+ val id: String,
35
+ val kind: String,
36
+ val x: Float,
37
+ val y: Float,
38
+ val w: Float,
39
+ val h: Float,
40
+ val r: Float,
41
+ val icon: String?,
42
+ // Index of a React child of the host (its icon host), drawn centred; takes the place of `icon`. Null when unused.
43
+ val iconSlot: Int?,
44
+ val text: String?,
45
+ val textSize: Float?,
46
+ val color: Int,
47
+ val placeholder: String?,
48
+ val value: String?,
49
+ val pressable: Boolean,
50
+ ) {
51
+ val isGlass: Boolean get() = kind != "label"
52
+
53
+ companion object {
54
+ // Parses one map of the `nodes` prop. Numbers arrive as Double, missing optional keys as null.
55
+ fun parse(map: Map<String, Any?>): SurfaceNode? {
56
+ val id = map["id"] as? String ?: return null
57
+ return SurfaceNode(
58
+ id = id,
59
+ kind = map["kind"] as? String ?: "glass",
60
+ x = (map["x"] as? Number)?.toFloat() ?: 0f,
61
+ y = (map["y"] as? Number)?.toFloat() ?: 0f,
62
+ w = (map["w"] as? Number)?.toFloat() ?: 0f,
63
+ h = (map["h"] as? Number)?.toFloat() ?: 0f,
64
+ r = (map["r"] as? Number)?.toFloat() ?: 0f,
65
+ icon = map["icon"] as? String,
66
+ iconSlot = (map["iconSlot"] as? Number)?.toInt(),
67
+ text = map["text"] as? String,
68
+ textSize = (map["textSize"] as? Number)?.toFloat(),
69
+ color = parseColor(map["color"] as? String),
70
+ placeholder = map["placeholder"] as? String,
71
+ value = map["value"] as? String,
72
+ pressable = map["pressable"] as? Boolean ?: true,
73
+ )
74
+ }
75
+
76
+ private fun parseColor(text: String?): Int =
77
+ try {
78
+ if (text == null) Color.WHITE else Color.parseColor(text)
79
+ } catch (error: IllegalArgumentException) {
80
+ Color.WHITE
81
+ }
82
+ }
83
+ }
84
+
85
+ // Draws a node layer as a window overlay (the same role as GlassChromeView): the glass is the chrome material with
86
+ // no refraction (ChromeMaterial.apply); a "selection" node is the refractive pill (ChromeMaterial.applyPill). Each node
87
+ // springs from its displayed rectangle to a new one with the menu's springs, and a press deforms it with the menu's
88
+ // GlassJelly through GlassScrub. Taps go back to React as onNodePress; fields as onFieldText / onFieldFocus.
89
+ // The view is transparent: it only returns a touch when the touch starts on a pressable node, so the page under it
90
+ // keeps its touches everywhere else.
91
+ class SurfaceNodesView(context: Context) : FrameLayout(context) {
92
+ // Events to the host: "onNodePress", "onFieldText", "onFieldFocus".
93
+ var emit: (String, Map<String, Any?>) -> Unit = { _, _ -> }
94
+
95
+ private val density = resources.displayMetrics.density
96
+ private val slop = ViewConfiguration.get(context).scaledTouchSlop
97
+
98
+ internal var nodes: List<SurfaceNode> = emptyList()
99
+ set(value) {
100
+ field = value
101
+ retarget(value)
102
+ syncInputs(value)
103
+ kick()
104
+ invalidate()
105
+ }
106
+
107
+ // Per node: its spring, press jelly, scrub controller and renderer (one renderer per surface, as in the menu).
108
+ private class Track(
109
+ var node: SurfaceNode,
110
+ val spring: GlassSpring,
111
+ val jelly: GlassJelly,
112
+ val scrub: GlassScrub,
113
+ val renderer: GlassRenderer,
114
+ val from: RectF,
115
+ var fromRadius: Float,
116
+ )
117
+
118
+ // The field that must hold the keyboard (null for none), set by React.
119
+ var focusedField: String? = null
120
+ set(value) {
121
+ field = value
122
+ syncFocus()
123
+ }
124
+
125
+ private val tracks = LinkedHashMap<String, Track>()
126
+ private val inputs = HashMap<String, EditText>()
127
+ private val iconPaint = Paint(Paint.ANTI_ALIAS_FLAG)
128
+ // The React icon host of the owner (SurfaceNodesHost): its children are the iconSlot views.
129
+ internal var iconHost: () -> ViewGroup? = { null }
130
+ private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
131
+ typeface = Typeface.create(Typeface.DEFAULT, 500, false)
132
+ textAlign = Paint.Align.CENTER
133
+ }
134
+ private var backdropProvider: GlassBackdrop? = null
135
+ private var running = false
136
+ private var lastNanos = -1L
137
+ private val frameCallback = Choreographer.FrameCallback { nanos -> onFrame(nanos) }
138
+
139
+ init {
140
+ setWillNotDraw(false)
141
+ }
142
+
143
+ override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {}
144
+
145
+ override fun onAttachedToWindow() {
146
+ super.onAttachedToWindow()
147
+ val root = rootView as? ViewGroup ?: return
148
+ backdropProvider = GlassBackdrop.forRoot(root).also { it.register(this) { Color.BLACK } }
149
+ }
150
+
151
+ override fun onDetachedFromWindow() {
152
+ backdropProvider?.unregister(this)
153
+ backdropProvider = null
154
+ Choreographer.getInstance().removeFrameCallback(frameCallback)
155
+ running = false
156
+ lastNanos = -1L
157
+ tracks.values.forEach { it.renderer.release() }
158
+ tracks.clear()
159
+ super.onDetachedFromWindow()
160
+ }
161
+
162
+ // A new layer: a node that already exists springs from where it is drawn now to its new rectangle; a new node
163
+ // appears at its rectangle; a node that is gone is dropped.
164
+ private fun retarget(list: List<SurfaceNode>) {
165
+ val seen = HashSet<String>()
166
+ for (node in list) {
167
+ seen.add(node.id)
168
+ val existing = tracks[node.id]
169
+ val spec = specFor(node)
170
+ if (existing == null) {
171
+ val spring = GlassSpring(0.002f, 0.02f)
172
+ spring.reset(1f)
173
+ val jelly = GlassJelly()
174
+ tracks[node.id] = Track(node, spring, jelly, GlassScrub(jelly), GlassRenderer(density, density * FIELD_STEP), RectF(node.x, node.y, node.x + node.w, node.y + node.h), node.r)
175
+ continue
176
+ }
177
+ val current = displayRect(existing, false)
178
+ existing.from.set(current)
179
+ existing.fromRadius = currentRadius(existing)
180
+ // Always restart from the displayed rectangle: an unchanged node has from == to, so it does not move.
181
+ existing.node = node
182
+ existing.spring.reset(0f)
183
+ existing.spring.target(1f, spec)
184
+ }
185
+ val gone = tracks.keys.filter { it !in seen }
186
+ gone.forEach { id -> tracks.remove(id)?.renderer?.release() }
187
+ }
188
+
189
+ private fun specFor(node: SurfaceNode): GlassSpringSpec =
190
+ if (node.kind == "selection") GlassMotion.CollapseSpring else GlassMotion.EjectSpring
191
+
192
+ // The rectangle of a track at its spring position, before the press jelly.
193
+ private fun easedRect(track: Track): RectF {
194
+ val p = track.spring.value
195
+ val to = track.node
196
+ return RectF(
197
+ track.from.left + (to.x - track.from.left) * p,
198
+ track.from.top + (to.y - track.from.top) * p,
199
+ track.from.right + (to.x + to.w - track.from.right) * p,
200
+ track.from.bottom + (to.y + to.h - track.from.bottom) * p,
201
+ )
202
+ }
203
+
204
+ private fun currentRadius(track: Track): Float =
205
+ track.fromRadius + (track.node.r - track.fromRadius) * track.spring.value
206
+
207
+ // The drawn rectangle of a track in dp; with `pressed` it includes the press deformation of its jelly.
208
+ private fun displayRect(track: Track, deformed: Boolean): RectF {
209
+ val rect = easedRect(track)
210
+ // A node with no area is not deformed: the jelly would give it a width back.
211
+ if (deformed && track.node.pressable && rect.width() >= 1f && rect.height() >= 1f) track.jelly.deformControl(rect, 1f, 1f)
212
+ return rect
213
+ }
214
+
215
+ private fun kick() {
216
+ if (running) return
217
+ running = true
218
+ lastNanos = -1L
219
+ Choreographer.getInstance().postFrameCallback(frameCallback)
220
+ }
221
+
222
+ // One display frame: advance the springs and jellies with the real frame time (capped as the web loop), redraw.
223
+ private fun onFrame(nanos: Long) {
224
+ val seconds = if (lastNanos < 0L) 1f / 60f else min((nanos - lastNanos) / 1_000_000_000f, 1f / 30f)
225
+ lastNanos = nanos
226
+ var settled = true
227
+ tracks.values.forEach { track ->
228
+ track.spring.advance(seconds)
229
+ track.jelly.advance(seconds)
230
+ if (!track.spring.isSettled || !track.jelly.isSettled) settled = false
231
+ }
232
+ invalidate()
233
+ if (settled) {
234
+ tracks.values.forEach { it.spring.finish() }
235
+ invalidate()
236
+ running = false
237
+ lastNanos = -1L
238
+ } else {
239
+ Choreographer.getInstance().postFrameCallback(frameCallback)
240
+ }
241
+ }
242
+
243
+ override fun onDraw(canvas: Canvas) {
244
+ if (width <= 0 || height <= 0) return
245
+ val d = density
246
+ val root = rootView as? ViewGroup
247
+ val provider = backdropProvider
248
+ val list = nodes
249
+ if (root != null && provider != null) {
250
+ val rootLocation = IntArray(2)
251
+ val ownLocation = IntArray(2)
252
+ root.getLocationInWindow(rootLocation)
253
+ getLocationInWindow(ownLocation)
254
+ val ox = (ownLocation[0] - rootLocation[0]).toFloat()
255
+ val oy = (ownLocation[1] - rootLocation[1]).toFloat()
256
+ val limits = Rect(0, 0, root.width, root.height)
257
+ canvas.save()
258
+ canvas.translate(-ox, -oy)
259
+ // Glass first, in node order; the content (icons, text) after, so no glass covers a label.
260
+ for (node in list) {
261
+ if (!node.isGlass) continue
262
+ val track = tracks[node.id] ?: continue
263
+ val rect = displayRect(track, true)
264
+ // A node with no area (a control that has shrunk away) is not drawn.
265
+ if (rect.width() < 1f || rect.height() < 1f) continue
266
+ val state = glassStateFor(track, rect, ox, oy)
267
+ track.renderer.prepare(state)
268
+ track.renderer.drawGlass(canvas, provider, state, limits)
269
+ }
270
+ canvas.restore()
271
+ }
272
+
273
+ canvas.save()
274
+ canvas.scale(d, d)
275
+ for (node in list) {
276
+ val track = tracks[node.id] ?: continue
277
+ val rect = displayRect(track, true)
278
+ if (node.isGlass && (rect.width() < 1f || rect.height() < 1f)) continue
279
+ drawContent(canvas, node, rect)
280
+ }
281
+ canvas.restore()
282
+ placeInputs(list)
283
+ }
284
+
285
+ // State of one glass node in px (window coordinates). Refraction only on the selection pill.
286
+ private fun glassStateFor(track: Track, rect: RectF, ox: Float, oy: Float): GlassSurfaceState {
287
+ val d = density
288
+ val node = track.node
289
+ val state = GlassSurfaceState()
290
+ if (node.kind == "selection") {
291
+ ChromeMaterial.applyPill(state, track.jelly, rect, ox, oy, d)
292
+ } else {
293
+ ChromeMaterial.apply(state)
294
+ }
295
+ val radius = min(min(rect.width(), rect.height()) / 2f, currentRadius(track)) * d
296
+ state.incoming.set(rect.left * d + ox, rect.top * d + oy, rect.right * d + ox, rect.bottom * d + oy)
297
+ state.radiusX = radius
298
+ state.radiusY = radius
299
+ state.captureBounds.set(state.incoming)
300
+ state.captureBounds.inset(-CAPTURE_MARGIN * d, -CAPTURE_MARGIN * d)
301
+ return state
302
+ }
303
+
304
+ // A React icon (iconSlot): its view is drawn centred in the node. The view is measured by React after the first
305
+ // layout, so a view with no size yet asks for another frame.
306
+ private fun drawSlot(canvas: Canvas, rect: RectF, slot: Int) {
307
+ val view = iconHost()?.getChildAt(slot) ?: return
308
+ val w = view.width
309
+ val h = view.height
310
+ if (w == 0 || h == 0) {
311
+ invalidate()
312
+ return
313
+ }
314
+ canvas.save()
315
+ canvas.translate(rect.centerX(), rect.centerY())
316
+ canvas.scale(1f / density, 1f / density)
317
+ canvas.translate(-w / 2f, -h / 2f)
318
+ view.draw(canvas)
319
+ canvas.restore()
320
+ }
321
+
322
+ private fun drawContent(canvas: Canvas, node: SurfaceNode, rect: RectF) {
323
+ if (node.iconSlot != null && node.kind != "field") {
324
+ drawSlot(canvas, rect, node.iconSlot)
325
+ return
326
+ }
327
+ if (node.kind == "field") {
328
+ if (node.icon != null) {
329
+ SurfaceIcons.draw(canvas, iconPaint, node.icon, rect.left + FIELD_ICON_LEFT + FIELD_ICON_SIZE / 2f, rect.centerY(), FIELD_ICON_SIZE, FIELD_ICON_COLOR, 1f)
330
+ }
331
+ return
332
+ }
333
+ val cx = rect.centerX()
334
+ val cy = rect.centerY()
335
+ val iconSize = node.textSize ?: DEFAULT_ICON_SIZE
336
+ val textSize = node.textSize ?: DEFAULT_TEXT_SIZE
337
+ val hasIcon = node.icon != null
338
+ val hasText = node.text != null
339
+ if (hasIcon && hasText) {
340
+ // Icon and text side by side, centred as one group (the web draws them in a row too).
341
+ textPaint.textSize = textSize
342
+ textPaint.color = node.color
343
+ val textWidth = textPaint.measureText(node.text)
344
+ val gap = 6f
345
+ val total = iconSize + gap + textWidth
346
+ val left = cx - total / 2f
347
+ SurfaceIcons.draw(canvas, iconPaint, node.icon!!, left + iconSize / 2f, cy, iconSize, node.color, 1f)
348
+ textPaint.textAlign = Paint.Align.LEFT
349
+ canvas.drawText(node.text!!, left + iconSize + gap, baseline(cy, textSize), textPaint)
350
+ textPaint.textAlign = Paint.Align.CENTER
351
+ } else if (hasIcon) {
352
+ SurfaceIcons.draw(canvas, iconPaint, node.icon!!, cx, cy, iconSize, node.color, 1f)
353
+ } else if (hasText) {
354
+ textPaint.textSize = textSize
355
+ textPaint.color = node.color
356
+ canvas.drawText(node.text!!, cx, baseline(cy, textSize), textPaint)
357
+ }
358
+ }
359
+
360
+ // Baseline that centres a line of `size` dp on `cy`.
361
+ private fun baseline(cy: Float, size: Float): Float {
362
+ textPaint.textSize = size
363
+ val metrics = textPaint.fontMetrics
364
+ return cy - (metrics.ascent + metrics.descent) / 2f
365
+ }
366
+
367
+ // Text inputs of the field nodes: created on demand, placed on their rectangle, removed with the node.
368
+ private fun syncInputs(list: List<SurfaceNode>) {
369
+ val fields = list.filter { it.kind == "field" }
370
+ val ids = fields.map { it.id }.toSet()
371
+ inputs.keys.filter { it !in ids }.forEach { id ->
372
+ inputs.remove(id)?.let { removeView(it) }
373
+ }
374
+ for (field in fields) {
375
+ val input = inputs.getOrPut(field.id) { createInput(field.id) }
376
+ val text = field.value ?: ""
377
+ // While the field has focus the user's typing is the value; React's echo of it must not rewrite the text.
378
+ if (!input.hasFocus() && input.text.toString() != text) {
379
+ input.setText(text)
380
+ input.setSelection(text.length)
381
+ }
382
+ input.hint = field.placeholder ?: ""
383
+ if (field.id == focusedField) {
384
+ if (!input.hasFocus()) {
385
+ input.requestFocus()
386
+ context.getSystemService(android.view.inputmethod.InputMethodManager::class.java)?.showSoftInput(input, 0)
387
+ }
388
+ } else if (input.hasFocus()) {
389
+ input.clearFocus()
390
+ }
391
+ input.setTextColor(field.color)
392
+ input.textSize = field.textSize ?: DEFAULT_TEXT_SIZE
393
+ }
394
+ }
395
+
396
+ // Gives or takes the keyboard after focusedField changed.
397
+ private fun syncFocus() {
398
+ for ((id, input) in inputs) {
399
+ if (id == focusedField) {
400
+ if (!input.hasFocus()) input.requestFocus()
401
+ } else if (input.hasFocus()) {
402
+ input.clearFocus()
403
+ // Clearing the focus does not hide the keyboard: it is hidden here, as the chrome does on close.
404
+ context.getSystemService(android.view.inputmethod.InputMethodManager::class.java)?.hideSoftInputFromWindow(windowToken, 0)
405
+ }
406
+ }
407
+ }
408
+
409
+ private fun createInput(id: String): EditText {
410
+ val input = EditText(context).apply {
411
+ background = null
412
+ setPadding(0, 0, 0, 0)
413
+ setSingleLine(true)
414
+ setHintTextColor(FIELD_ICON_COLOR)
415
+ gravity = android.view.Gravity.CENTER_VERTICAL or android.view.Gravity.START
416
+ setTextSize(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_TEXT_SIZE)
417
+ addTextChangedListener(object : TextWatcher {
418
+ override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
419
+ override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
420
+ override fun afterTextChanged(s: Editable?) {
421
+ // The React side owns the value: it echoes it back through `value`, which is then equal and not re-set.
422
+ emit("onFieldText", mapOf("id" to id, "text" to (s?.toString() ?: "")))
423
+ }
424
+ })
425
+ onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
426
+ emit("onFieldFocus", mapOf("id" to id, "focused" to hasFocus))
427
+ }
428
+ }
429
+ addView(input, FrameLayout.LayoutParams(0, 0))
430
+ return input
431
+ }
432
+
433
+ // Places each input on its field node (px), at the node's text inset, with the node's opacity 1.
434
+ private fun placeInputs(list: List<SurfaceNode>) {
435
+ val d = density
436
+ for (field in list) {
437
+ if (field.kind != "field") continue
438
+ val input = inputs[field.id] ?: continue
439
+ val track = tracks[field.id] ?: continue
440
+ val rect = displayRect(track, true)
441
+ val left = rect.left + (if (field.icon != null) FIELD_TEXT_LEFT else FIELD_PAD)
442
+ val right = rect.right - FIELD_PAD
443
+ val line = min(rect.height(), FIELD_LINE)
444
+ val top = rect.centerY() - line / 2f
445
+ input.layout((left * d).toInt(), (top * d).toInt(), (right * d).toInt(), ((top + line) * d).toInt())
446
+ }
447
+ }
448
+
449
+ // The hit test: the topmost pressable node under (x, y) in dp, fields excluded (their EditText takes the touch).
450
+ private fun hitNode(x: Float, y: Float): SurfaceNode? {
451
+ for (node in nodes.asReversed()) {
452
+ if (!node.pressable || node.kind == "field") continue
453
+ val track = tracks[node.id] ?: continue
454
+ val rect = easedRect(track)
455
+ if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) return node
456
+ }
457
+ return null
458
+ }
459
+
460
+ private var pressedNode: SurfaceNode? = null
461
+ private var downX = 0f
462
+ private var downY = 0f
463
+
464
+ override fun onTouchEvent(event: MotionEvent): Boolean {
465
+ when (event.actionMasked) {
466
+ MotionEvent.ACTION_DOWN -> {
467
+ val x = event.x / density
468
+ val y = event.y / density
469
+ val node = hitNode(x, y) ?: return false
470
+ pressedNode = node
471
+ downX = event.x
472
+ downY = event.y
473
+ tracks[node.id]?.scrub?.press(x, y, GlassScrub.NONE)
474
+ kick()
475
+ return true
476
+ }
477
+ MotionEvent.ACTION_MOVE -> {
478
+ val node = pressedNode ?: return true
479
+ val dx = event.x - downX
480
+ val dy = event.y - downY
481
+ // Leaving the slop cancels the press, as the menu does for a vertical move.
482
+ if (abs(dx) > slop || abs(dy) > slop) {
483
+ pressedNode = null
484
+ release(node)
485
+ }
486
+ return true
487
+ }
488
+ MotionEvent.ACTION_UP -> {
489
+ val node = pressedNode
490
+ pressedNode = null
491
+ if (node != null) {
492
+ release(node)
493
+ // A label (a segment) changes the selection under the finger: the menu's highlight tick.
494
+ if (node.kind == "label") GlassFeedback.tick(this)
495
+ emit("onNodePress", mapOf("id" to node.id))
496
+ }
497
+ return true
498
+ }
499
+ MotionEvent.ACTION_CANCEL -> {
500
+ val node = pressedNode
501
+ pressedNode = null
502
+ if (node != null) release(node)
503
+ return true
504
+ }
505
+ }
506
+ return false
507
+ }
508
+
509
+ private fun release(node: SurfaceNode) {
510
+ tracks[node.id]?.scrub?.release()
511
+ kick()
512
+ }
513
+
514
+ companion object {
515
+ // Same value as the menu's GlassMenuAndroidView.FieldStep (its companion is private).
516
+ private const val FIELD_STEP = 4f
517
+ private const val CAPTURE_MARGIN = 12f
518
+ private const val DEFAULT_TEXT_SIZE = 17f
519
+ private const val DEFAULT_ICON_SIZE = 24f
520
+ private const val FIELD_ICON_LEFT = 16f
521
+ private const val FIELD_ICON_SIZE = 18f
522
+ private const val FIELD_TEXT_LEFT = 42f
523
+ private const val FIELD_PAD = 16f
524
+ private const val FIELD_LINE = 28f
525
+ private const val FIELD_ICON_COLOR = 0xFFA1A1A6.toInt()
526
+ }
527
+ }
@@ -0,0 +1,68 @@
1
+ package so.oxy.bloom.glassmenu.content
2
+
3
+ import android.graphics.Path
4
+ import android.graphics.RectF
5
+
6
+ internal object GlassIcons {
7
+ private val cache = HashMap<String, Path>()
8
+
9
+ fun path(name: String): Path? = cache[name] ?: build(name)?.also { cache[name] = it }
10
+
11
+ private fun build(name: String): Path? = when (name) {
12
+ "check" -> Path().apply { moveTo(4.5f, 12.8f); lineTo(9.8f, 18.5f); lineTo(19.5f, 5.5f) }
13
+ "grid" -> Path().apply {
14
+ for (x in listOf(3.5f, 13.5f)) for (y in listOf(3.5f, 13.5f)) {
15
+ addRoundRect(RectF(x, y, x + 7f, y + 7f), 1.8f, 1.8f, Path.Direction.CW)
16
+ }
17
+ }
18
+ "list" -> Path().apply {
19
+ for (y in listOf(6f, 12f, 18f)) {
20
+ moveTo(8.5f, y); lineTo(21f, y)
21
+ addCircle(3.9f, y, .35f, Path.Direction.CW)
22
+ }
23
+ }
24
+ "chevron" -> Path().apply { moveTo(9f, 6f); lineTo(15f, 12f); lineTo(9f, 18f) }
25
+ "plus" -> Path().apply { moveTo(12f, 4f); lineTo(12f, 20f); moveTo(4f, 12f); lineTo(20f, 12f) }
26
+ "filter" -> Path().apply {
27
+ moveTo(3.5f, 7f); lineTo(20.5f, 7f); moveTo(6.5f, 12f); lineTo(17.5f, 12f); moveTo(9.5f, 17f); lineTo(14.5f, 17f)
28
+ }
29
+ "ellipsis" -> Path().apply { for (x in listOf(5f, 12f, 19f)) addCircle(x, 12f, .9f, Path.Direction.CW) }
30
+ "trash" -> Path().apply {
31
+ moveTo(4f, 6.5f); lineTo(20f, 6.5f)
32
+ moveTo(9f, 6.5f); lineTo(9f, 4f); lineTo(15f, 4f); lineTo(15f, 6.5f)
33
+ moveTo(6f, 6.5f); lineTo(7f, 20f); lineTo(17f, 20f); lineTo(18f, 6.5f)
34
+ moveTo(10f, 10f); lineTo(10.3f, 16.5f); moveTo(14f, 10f); lineTo(13.7f, 16.5f)
35
+ }
36
+ "share" -> Path().apply {
37
+ moveTo(12f, 15f); lineTo(12f, 3.5f); moveTo(8f, 7f); lineTo(12f, 3.5f); lineTo(16f, 7f)
38
+ moveTo(8f, 10.5f); lineTo(5.5f, 10.5f); lineTo(5.5f, 20.5f); lineTo(18.5f, 20.5f); lineTo(18.5f, 10.5f); lineTo(16f, 10.5f)
39
+ }
40
+ "pencil" -> Path().apply {
41
+ moveTo(4f, 20f); lineTo(5f, 15.5f); lineTo(16f, 4.5f); lineTo(19.5f, 8f); lineTo(8.5f, 19f); close()
42
+ moveTo(14f, 6.5f); lineTo(17.5f, 10f)
43
+ }
44
+ "copy" -> Path().apply {
45
+ addRoundRect(RectF(8.5f, 8.5f, 20f, 20f), 2.5f, 2.5f, Path.Direction.CW)
46
+ moveTo(15.5f, 5.5f); lineTo(15.5f, 5f); quadTo(15.5f, 4f, 13.5f, 4f); lineTo(6f, 4f)
47
+ quadTo(4f, 4f, 4f, 6f); lineTo(4f, 13.5f); quadTo(4f, 15.5f, 5.5f, 15.5f)
48
+ }
49
+ "heart" -> Path().apply {
50
+ moveTo(12f, 20f)
51
+ cubicTo(5f, 15f, 3f, 11.5f, 3f, 8.6f); cubicTo(3f, 6f, 5f, 4.2f, 7.4f, 4.2f); cubicTo(9.3f, 4.2f, 11f, 5.3f, 12f, 7f)
52
+ cubicTo(13f, 5.3f, 14.7f, 4.2f, 16.6f, 4.2f); cubicTo(19f, 4.2f, 21f, 6f, 21f, 8.6f); cubicTo(21f, 11.5f, 19f, 15f, 12f, 20f); close()
53
+ }
54
+ "folder" -> Path().apply {
55
+ moveTo(3.5f, 18f); lineTo(3.5f, 6.5f); quadTo(3.5f, 5f, 5f, 5f); lineTo(9.5f, 5f); lineTo(11.5f, 7.5f); lineTo(19f, 7.5f)
56
+ quadTo(20.5f, 7.5f, 20.5f, 9f); lineTo(20.5f, 18f); quadTo(20.5f, 19.5f, 19f, 19.5f); lineTo(5f, 19.5f); quadTo(3.5f, 19.5f, 3.5f, 18f); close()
57
+ }
58
+ "sort" -> Path().apply {
59
+ moveTo(8f, 19.5f); lineTo(8f, 4.5f); moveTo(4f, 8.5f); lineTo(8f, 4.5f); lineTo(12f, 8.5f)
60
+ moveTo(16f, 4.5f); lineTo(16f, 19.5f); moveTo(12f, 15.5f); lineTo(16f, 19.5f); lineTo(20f, 15.5f)
61
+ }
62
+ "clock" -> Path().apply {
63
+ addCircle(12f, 12f, 8.5f, Path.Direction.CW)
64
+ moveTo(12f, 7f); lineTo(12f, 12f); lineTo(15.5f, 14f)
65
+ }
66
+ else -> null
67
+ }
68
+ }