@oxy.so/bloom-lens 0.0.0-bootstrap.0 → 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.
- package/LICENSE +673 -0
- package/NOTICE +26 -0
- package/android/build.gradle +19 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/assets/fonts/Inter-OFL.txt +93 -0
- package/android/src/main/assets/fonts/Inter-Regular.ttf +0 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/220 +0 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/GlassMenuAndroidModule.kt +71 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/GlassMenuAndroidView.kt +548 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/backdrop/GlassBackdrop.kt +215 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/backdrop/GlassLens.kt +103 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/ChromeGeometry.kt +73 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/ChromeIcons.kt +74 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/ChromeMaterial.kt +74 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/GlassChromeHost.kt +73 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/GlassChromeModule.kt +31 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/GlassChromeView.kt +609 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/StaticGlassCache.kt +73 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceIcons.kt +114 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceNodesHost.kt +71 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceNodesModule.kt +22 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/chrome/SurfaceNodesView.kt +527 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/content/GlassIcons.kt +68 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/content/GlassMenuContent.kt +349 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/content/GlassRow.kt +19 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/field/GlassFieldTexture.kt +102 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/field/GlassGaussianField.kt +161 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/field/GlassShapes.kt +37 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/field/RoundedField.kt +119 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/field/RoundedUnion.kt +81 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassGroup.kt +225 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassJelly.kt +118 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassMorphFrame.kt +22 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassMorphTuning.kt +31 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassMotion.kt +475 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/motion/GlassSpring.kt +78 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassDevGuard.kt +31 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassFeedback.kt +18 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassMenuOverlay.kt +433 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassMenuSession.kt +750 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassScrub.kt +41 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/overlay/GlassSubmenuLayer.kt +290 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassAbsorbRecord.kt +9 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassMemberRecord.kt +14 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassMenuConfigRecord.kt +24 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassMenuItemRecord.kt +23 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassShrinkRecord.kt +10 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/records/GlassTriggerConfigRecord.kt +19 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassRecipe.kt +18 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassRenderer.kt +361 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassShaders.kt +318 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassStill.kt +115 -0
- package/android/src/main/java/so/oxy/bloom/glassmenu/render/GlassSurfaceState.kt +38 -0
- package/expo-module.config.json +10 -0
- package/package.json +31 -1
- package/patches/bloom.patch +1908 -0
- package/patches/menu/bloom.patch +148 -0
- package/src/base.tsx +208 -0
- package/src/chrome/compositor-geometry.ts +163 -0
- package/src/chrome/glass-header.tsx +155 -0
- package/src/chrome/glass-search-fab.tsx +113 -0
- package/src/chrome/glass-size-class.tsx +150 -0
- package/src/chrome/glass-toolbar.tsx +45 -0
- package/src/chrome/interface.ts +17 -0
- package/src/chrome/ios-tab-bar.tsx +49 -0
- package/src/chrome/surface-layer.ts +91 -0
- package/src/chrome/surface-nodes.tsx +54 -0
- package/src/index.ts +19 -0
- package/src/menu/components/menu/index.ts +22 -0
- package/src/menu/components/menu/menu.tsx +97 -0
- package/src/menu/components/menu-action-row/index.ts +1 -0
- package/src/menu/components/menu-action-row/menu-action-row.tsx +12 -0
- package/src/menu/components/menu-content/index.ts +2 -0
- package/src/menu/components/menu-content/menu-content.tsx +11 -0
- package/src/menu/components/menu-item/index.ts +13 -0
- package/src/menu/components/menu-item/menu-item-icon.tsx +10 -0
- package/src/menu/components/menu-item/menu-item-subtitle.tsx +14 -0
- package/src/menu/components/menu-item/menu-item-title.tsx +11 -0
- package/src/menu/components/menu-item/menu-item.tsx +11 -0
- package/src/menu/components/menu-label/index.ts +3 -0
- package/src/menu/components/menu-label/menu-label.tsx +11 -0
- package/src/menu/components/menu-separator/index.ts +3 -0
- package/src/menu/components/menu-separator/menu-separator.tsx +7 -0
- package/src/menu/components/menu-submenu/index.ts +1 -0
- package/src/menu/components/menu-submenu/menu-submenu.tsx +12 -0
- package/src/menu/components/menu-trigger/index.ts +3 -0
- package/src/menu/components/menu-trigger/interface.ts +41 -0
- package/src/menu/components/menu-trigger/menu-trigger.android.tsx +97 -0
- package/src/menu/components/menu-trigger/menu-trigger.tsx +15 -0
- package/src/menu/components/menu-trigger/native-menu.android.ts +9 -0
- package/src/menu/components/menu-trigger-button/index.ts +3 -0
- package/src/menu/components/menu-trigger-button/menu-trigger-button.android.tsx +38 -0
- package/src/menu/components/menu-trigger-button/menu-trigger-button.tsx +17 -0
- package/src/menu/constants/interface.ts +6 -0
- package/src/menu/constants/menu.const.ts +11 -0
- package/src/menu/context/interface.ts +63 -0
- package/src/menu/context/menu.context.ts +7 -0
- package/src/menu/index.ts +53 -0
- package/src/menu/interface.ts +229 -0
- package/src/menu/utils/create-compound-component.util.ts +18 -0
- package/src/menu/utils/find-menu-content.util.ts +17 -0
- package/src/menu/utils/flatten-elements.util.ts +18 -0
- package/src/menu/utils/parse-menu-content.util.ts +98 -0
- package/src/menu/utils/parse-menu-item.util.ts +56 -0
- package/src/menu/utils/register-component.util.ts +13 -0
- package/src/menu/utils/to-native-item.util.ts +24 -0
- package/src/rows.tsx +61 -0
- package/README.md +0 -1
|
@@ -0,0 +1,1908 @@
|
|
|
1
|
+
--- a/content/GlassMenuContent.kt
|
|
2
|
+
+++ b/content/GlassMenuContent.kt
|
|
3
|
+
@@ -74,6 +74,8 @@
|
|
4
|
+
var height = 0f
|
|
5
|
+
private set
|
|
6
|
+
var dark = true
|
|
7
|
+
+ // The theme's row ink (null channels keep the built-in colours).
|
|
8
|
+
+ var ink: so.oxy.bloom.glassmenu.records.GlassMenuConfigRecord? = null
|
|
9
|
+
// Alpha of the whole content: the submenu navigation fades the level in (RowsRevealSpring).
|
|
10
|
+
var contentAlpha = 1f
|
|
11
|
+
|
|
12
|
+
@@ -234,11 +236,11 @@
|
|
13
|
+
// (the submenu header) is drawn by the same code wherever it is. `chevronRotation` turns the trailing chevron
|
|
14
|
+
// around its own centre.
|
|
15
|
+
fun drawRow(canvas: Canvas, row: GlassRow, chevronRotation: Float) {
|
|
16
|
+
- val foreground = if (dark) Color.argb(245, 255, 255, 255) else Color.argb(235, 0, 0, 0)
|
|
17
|
+
- val muted = if (dark) Color.argb(153, 235, 235, 245) else Color.argb(153, 60, 60, 67)
|
|
18
|
+
- val destructive = if (dark) Color.rgb(255, 82, 84) else Color.rgb(220, 30, 48)
|
|
19
|
+
+ val foreground = ink?.textColor?.toArgb() ?: if (dark) Color.argb(245, 255, 255, 255) else Color.argb(235, 0, 0, 0)
|
|
20
|
+
+ val muted = ink?.mutedColor?.toArgb() ?: if (dark) Color.argb(153, 235, 235, 245) else Color.argb(153, 60, 60, 67)
|
|
21
|
+
+ val destructive = ink?.destructiveColor?.toArgb() ?: if (dark) Color.rgb(255, 82, 84) else Color.rgb(220, 30, 48)
|
|
22
|
+
if (row.isSeparator) {
|
|
23
|
+
- fillPaint.color = if (dark) Color.argb(17, 255, 255, 255) else Color.argb(26, 0, 0, 0)
|
|
24
|
+
+ fillPaint.color = ink?.separatorColor?.toArgb() ?: if (dark) Color.argb(17, 255, 255, 255) else Color.argb(26, 0, 0, 0)
|
|
25
|
+
val y = row.rect.centerY()
|
|
26
|
+
val half = density / 2f
|
|
27
|
+
canvas.drawRect(Metrics.SeparatorInset * density, y - half, width - Metrics.SeparatorInset * density, y + half, fillPaint)
|
|
28
|
+
--- a/GlassMenuAndroidModule.kt
|
|
29
|
+
+++ b/GlassMenuAndroidModule.kt
|
|
30
|
+
@@ -2,21 +2,22 @@
|
|
31
|
+
|
|
32
|
+
import so.oxy.bloom.glassmenu.records.GlassMenuConfigRecord
|
|
33
|
+
import so.oxy.bloom.glassmenu.records.GlassMenuItemRecord
|
|
34
|
+
+import so.oxy.bloom.glassmenu.records.GlassMemberRecord
|
|
35
|
+
import so.oxy.bloom.glassmenu.records.GlassTriggerConfigRecord
|
|
36
|
+
import expo.modules.kotlin.modules.Module
|
|
37
|
+
import expo.modules.kotlin.modules.ModuleDefinition
|
|
38
|
+
|
|
39
|
+
class GlassMenuAndroidModule : Module() {
|
|
40
|
+
override fun definition() = ModuleDefinition {
|
|
41
|
+
- Name("GlassMenuAndroid")
|
|
42
|
+
+ Name("BloomLensMenu")
|
|
43
|
+
|
|
44
|
+
View(GlassMenuAndroidView::class) {
|
|
45
|
+
- Events("onItemSelect", "onMenuOpenChange")
|
|
46
|
+
+ Events("onItemSelect", "onMenuOpenChange", "onMenuClosed")
|
|
47
|
+
|
|
48
|
+
Prop("items") { view: GlassMenuAndroidView, items: List<GlassMenuItemRecord>? ->
|
|
49
|
+
view.items = items ?: emptyList()
|
|
50
|
+
}
|
|
51
|
+
- Prop("glass") { view: GlassMenuAndroidView, glass: Boolean? ->
|
|
52
|
+
+ Prop("lens") { view: GlassMenuAndroidView, glass: Boolean? ->
|
|
53
|
+
view.glass = glass ?: true
|
|
54
|
+
}
|
|
55
|
+
Prop("morph") { view: GlassMenuAndroidView, morph: Boolean? ->
|
|
56
|
+
@@ -40,9 +41,24 @@
|
|
57
|
+
Prop("triggerConfig") { view: GlassMenuAndroidView, config: GlassTriggerConfigRecord? ->
|
|
58
|
+
view.triggerConfig = config ?: GlassTriggerConfigRecord()
|
|
59
|
+
}
|
|
60
|
+
+ Prop("openWithItems") { view: GlassMenuAndroidView, open: Boolean? ->
|
|
61
|
+
+ view.openWithItems = open ?: false
|
|
62
|
+
+ }
|
|
63
|
+
+ Prop("openOnTap") { view: GlassMenuAndroidView, open: Boolean? ->
|
|
64
|
+
+ view.openOnTap = open ?: false
|
|
65
|
+
+ }
|
|
66
|
+
+ Prop("restBackdrop") { view: GlassMenuAndroidView, color: android.graphics.Color? ->
|
|
67
|
+
+ view.restBackdrop = color?.toArgb()
|
|
68
|
+
+ }
|
|
69
|
+
Prop("alignment") { view: GlassMenuAndroidView, alignment: String? ->
|
|
70
|
+
view.alignment = alignment ?: "auto"
|
|
71
|
+
}
|
|
72
|
+
+ Prop("members") { view: GlassMenuAndroidView, members: List<GlassMemberRecord>? ->
|
|
73
|
+
+ view.members = members
|
|
74
|
+
+ }
|
|
75
|
+
+ Prop("blend") { view: GlassMenuAndroidView, blend: Float? ->
|
|
76
|
+
+ view.blend = blend ?: 0f
|
|
77
|
+
+ }
|
|
78
|
+
|
|
79
|
+
AsyncFunction("open") { view: GlassMenuAndroidView, x: Double?, y: Double?, width: Double?, height: Double? ->
|
|
80
|
+
view.post { view.open(x, y, width, height) }
|
|
81
|
+
--- a/GlassMenuAndroidView.kt
|
|
82
|
+
+++ b/GlassMenuAndroidView.kt
|
|
83
|
+
@@ -3,31 +3,40 @@
|
|
84
|
+
import android.content.Context
|
|
85
|
+
import android.content.res.Configuration
|
|
86
|
+
import android.graphics.Canvas
|
|
87
|
+
+import android.graphics.Rect
|
|
88
|
+
import android.graphics.RectF
|
|
89
|
+
import android.graphics.RenderNode
|
|
90
|
+
import android.os.Build
|
|
91
|
+
import android.os.SystemClock
|
|
92
|
+
+import android.os.Trace
|
|
93
|
+
+import android.view.ViewConfiguration
|
|
94
|
+
import android.view.MotionEvent
|
|
95
|
+
import android.view.View
|
|
96
|
+
import android.view.ViewGroup
|
|
97
|
+
import androidx.annotation.RequiresApi
|
|
98
|
+
import com.facebook.react.R
|
|
99
|
+
+import so.oxy.bloom.glassmenu.backdrop.GlassBackdrop
|
|
100
|
+
+import so.oxy.bloom.glassmenu.motion.GlassGroup
|
|
101
|
+
import so.oxy.bloom.glassmenu.motion.GlassJelly
|
|
102
|
+
import so.oxy.bloom.glassmenu.overlay.GlassDevGuard
|
|
103
|
+
import so.oxy.bloom.glassmenu.overlay.GlassMenuOverlay
|
|
104
|
+
import so.oxy.bloom.glassmenu.records.GlassMenuConfigRecord
|
|
105
|
+
+import so.oxy.bloom.glassmenu.records.GlassMemberRecord
|
|
106
|
+
import so.oxy.bloom.glassmenu.records.GlassMenuItemRecord
|
|
107
|
+
import so.oxy.bloom.glassmenu.records.GlassTriggerConfigRecord
|
|
108
|
+
import so.oxy.bloom.glassmenu.render.GlassRenderer
|
|
109
|
+
import so.oxy.bloom.glassmenu.render.GlassSurfaceState
|
|
110
|
+
+import so.oxy.bloom.glassmenu.render.SharedStills
|
|
111
|
+
import expo.modules.kotlin.AppContext
|
|
112
|
+
import expo.modules.kotlin.viewevent.EventDispatcher
|
|
113
|
+
import expo.modules.kotlin.views.ExpoView
|
|
114
|
+
+import kotlin.math.abs
|
|
115
|
+
import kotlin.math.min
|
|
116
|
+
|
|
117
|
+
-class GlassMenuAndroidView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
|
|
118
|
+
+class GlassMenuAndroidView(context: Context, appContext: AppContext) : ExpoView(context, appContext), GlassBackdrop.OffPage {
|
|
119
|
+
private val onItemSelect by EventDispatcher<Map<String, Any?>>()
|
|
120
|
+
private val onMenuOpenChange by EventDispatcher<Map<String, Any?>>()
|
|
121
|
+
+ private val onMenuClosed by EventDispatcher<Map<String, Any?>>()
|
|
122
|
+
|
|
123
|
+
private var overlay: GlassMenuOverlay? = null
|
|
124
|
+
private var glassRenderer: Any? = null
|
|
125
|
+
@@ -56,7 +65,18 @@
|
|
126
|
+
set(value) {
|
|
127
|
+
field = value
|
|
128
|
+
overlay?.menuSession?.takeIf { it.anchor === this }?.refreshItems()
|
|
129
|
+
+ openWhenReady()
|
|
130
|
+
}
|
|
131
|
+
+ // An open the owner asks for together with the rows it opens with (rows built by the press that opens it, or rows
|
|
132
|
+
+ // swapped in for it): while set, the view opens itself, once, as soon as it has rows and is laid out, since a call
|
|
133
|
+
+ // to `open` could arrive before the rows do. Set false, it opens again the next time it is set.
|
|
134
|
+
+ internal var openWithItems = false
|
|
135
|
+
+ set(value) {
|
|
136
|
+
+ field = value
|
|
137
|
+
+ if (!value) openedWithItems = false
|
|
138
|
+
+ openWhenReady()
|
|
139
|
+
+ }
|
|
140
|
+
+ private var openedWithItems = false
|
|
141
|
+
internal var glass = true
|
|
142
|
+
set(value) {
|
|
143
|
+
field = value
|
|
144
|
+
@@ -81,6 +101,50 @@
|
|
145
|
+
field = value
|
|
146
|
+
overlay?.invalidate()
|
|
147
|
+
}
|
|
148
|
+
+ // The colour the trigger sits on. Set, the view draws its own lens at rest, in its place in the view hierarchy (so
|
|
149
|
+
+ // a trigger in a scroll is clipped and stacked as the content it belongs to, under the bars), against that colour:
|
|
150
|
+
+ // at rest the shared still of its look (SharedStills: one texture for every trigger that looks the same, no layer
|
|
151
|
+
+ // of its own), and while pressed its live lens in a compositing layer. The window overlay draws the
|
|
152
|
+
+ // lens only while the menu is open: the session starts from the same base. A live lens inside the hierarchy cannot
|
|
153
|
+
+ // sample the window backdrop (the backdrop records the page, which would hold the lens: a RenderNode cycle).
|
|
154
|
+
+ internal var restBackdrop: Int? = null
|
|
155
|
+
+ set(value) {
|
|
156
|
+
+ field = value
|
|
157
|
+
+ restRecorded = false
|
|
158
|
+
+ refreshDrawing()
|
|
159
|
+
+ }
|
|
160
|
+
+ internal val inPlace get() = restBackdrop != null
|
|
161
|
+
+ private var restRenderer: Any? = null
|
|
162
|
+
+ private var restNode: Any? = null
|
|
163
|
+
+ private var restBackdropNode: Any? = null
|
|
164
|
+
+ private var restRecorded = false
|
|
165
|
+
+ // The look of the resting lens, the key of its shared still.
|
|
166
|
+
+ private var restLook: RestLook? = null
|
|
167
|
+
+ private val restRect = RectF()
|
|
168
|
+
+ private val restLimits = Rect()
|
|
169
|
+
+ // 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
|
|
170
|
+
+ // own (not one of several in a group, which their own presses open). Only with rows: a menu whose rows are built
|
|
171
|
+
+ // by its press is opened by its owner as they arrive (openWithItems). The owner's own press handler may still
|
|
172
|
+
+ // call `open`, which finds the menu open and does nothing.
|
|
173
|
+
+ internal var openOnTap = false
|
|
174
|
+
+ private var tapping = false
|
|
175
|
+
+ private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
|
|
176
|
+
+ // A group of glasses on this base (GlassGroup): the base draws each member's glass, not its own rect. Null: one glass.
|
|
177
|
+
+ // Drawn by the overlay only: a base drawn in its place (restBackdrop) is its own rect.
|
|
178
|
+
+ internal val group = GlassGroup()
|
|
179
|
+
+ internal var members: List<GlassMemberRecord>? = null
|
|
180
|
+
+ set(value) {
|
|
181
|
+
+ field = value
|
|
182
|
+
+ group.setRecords(value)
|
|
183
|
+
+ overlay?.invalidate()
|
|
184
|
+
+ }
|
|
185
|
+
+ // The distance in points within which a group's glasses blend into one another.
|
|
186
|
+
+ internal var blend = 0f
|
|
187
|
+
+ set(value) {
|
|
188
|
+
+ field = value
|
|
189
|
+
+ group.blend = value
|
|
190
|
+
+ overlay?.invalidate()
|
|
191
|
+
+ }
|
|
192
|
+
internal var menuWidth: Float? = null
|
|
193
|
+
internal var fontFamily: String? = null
|
|
194
|
+
internal var alignment = "auto"
|
|
195
|
+
@@ -89,7 +153,13 @@
|
|
196
|
+
|
|
197
|
+
internal val drawsGlass get() = glass && supportsGlass
|
|
198
|
+
|
|
199
|
+
- private val overlayDrawsChildren get() = supportsGlass && overlay != null && (glass || menuActive)
|
|
200
|
+
+ private val overlayDrawsChildren get() = supportsGlass && overlay != null && ((glass && !inPlace) || menuActive)
|
|
201
|
+
+
|
|
202
|
+
+ // Drawn by the overlay, the view's lens and content are not part of the page the glass samples.
|
|
203
|
+
+ override val offPage get() = overlayDrawsChildren
|
|
204
|
+
+
|
|
205
|
+
+ // The lens this view draws in its own place: at rest, when it sits in the hierarchy (restBackdrop).
|
|
206
|
+
+ private val drawsRestInPlace get() = inPlace && drawsGlass && !menuActive
|
|
207
|
+
|
|
208
|
+
init {
|
|
209
|
+
setWillNotDraw(false)
|
|
210
|
+
@@ -99,8 +169,22 @@
|
|
211
|
+
super.onAttachedToWindow()
|
|
212
|
+
val root = rootView as? ViewGroup ?: return
|
|
213
|
+
overlay = GlassMenuOverlay.obtain(root).also { it.register(this) }
|
|
214
|
+
- if (drawsGlass) GlassDevGuard.warnIfInOverscrollScroll(this)
|
|
215
|
+
+ if (drawsGlass && !inPlace) GlassDevGuard.warnIfInOverscrollScroll(this)
|
|
216
|
+
refreshDrawing()
|
|
217
|
+
+ openWhenReady()
|
|
218
|
+
+ }
|
|
219
|
+
+
|
|
220
|
+
+ override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
|
221
|
+
+ super.onSizeChanged(w, h, oldw, oldh)
|
|
222
|
+
+ restRecorded = false
|
|
223
|
+
+ openWhenReady()
|
|
224
|
+
+ }
|
|
225
|
+
+
|
|
226
|
+
+ private fun openWhenReady() {
|
|
227
|
+
+ if (!openWithItems || openedWithItems || overlay == null || items.isEmpty() || width == 0 || height == 0) return
|
|
228
|
+
+ openedWithItems = true
|
|
229
|
+
+ // After the rest of this layout pass, so the anchor is measured where it ends up.
|
|
230
|
+
+ post { open(null, null, null, null) }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
override fun onDetachedFromWindow() {
|
|
234
|
+
@@ -115,20 +199,158 @@
|
|
235
|
+
private fun releaseGraphics() {
|
|
236
|
+
(glassRenderer as? GlassRenderer)?.release()
|
|
237
|
+
glassRenderer = null
|
|
238
|
+
+ (restRenderer as? GlassRenderer)?.release()
|
|
239
|
+
+ restRenderer = null
|
|
240
|
+
+ (restNode as? RenderNode)?.discardDisplayList()
|
|
241
|
+
+ restNode = null
|
|
242
|
+
+ (restBackdropNode as? RenderNode)?.discardDisplayList()
|
|
243
|
+
+ restBackdropNode = null
|
|
244
|
+
+ restLook?.let { SharedStills.release(it) }
|
|
245
|
+
+ restLook = null
|
|
246
|
+
+ restRecorded = false
|
|
247
|
+
(contentNode as? RenderNode)?.discardDisplayList()
|
|
248
|
+
contentNode = null
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private fun refreshDrawing() {
|
|
252
|
+
+ restRecorded = false
|
|
253
|
+
invalidate()
|
|
254
|
+
overlay?.invalidate()
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
+ // A frame of the press jelly (the overlay runs the frame loop): a lens drawn in place records its new shape.
|
|
258
|
+
+ internal fun jellyFrame() {
|
|
259
|
+
+ if (!drawsRestInPlace) return
|
|
260
|
+
+ restRecorded = false
|
|
261
|
+
+ invalidate()
|
|
262
|
+
+ }
|
|
263
|
+
+
|
|
264
|
+
+ override fun onDraw(canvas: Canvas) {
|
|
265
|
+
+ super.onDraw(canvas)
|
|
266
|
+
+ if (supportsGlass && drawsRestInPlace && canvas.isHardwareAccelerated) drawRestInPlace(canvas)
|
|
267
|
+
+ }
|
|
268
|
+
+
|
|
269
|
+
+ // The resting lens, as the overlay draws a resting trigger (drawRestingAnchor), in this view's coordinates and
|
|
270
|
+
+ // against the colour it sits on. At rest the shared still of its look; pressed, recorded into a compositing layer,
|
|
271
|
+
+ // again only when its shape, colour or press changes.
|
|
272
|
+
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
273
|
+
+ private fun drawRestInPlace(canvas: Canvas) {
|
|
274
|
+
+ if (width == 0 || height == 0) return
|
|
275
|
+
+ val renderer = restRenderer as? GlassRenderer ?: newRenderer(density).also { restRenderer = it }
|
|
276
|
+
+ if (jelly.isSettled && drawRestStill(canvas, renderer)) return
|
|
277
|
+
+ val node = restNode as? RenderNode ?: RenderNode("GlassMenu.restInPlace").also {
|
|
278
|
+
+ it.setUseCompositingLayer(true, null)
|
|
279
|
+
+ restNode = it
|
|
280
|
+
+ }
|
|
281
|
+
+ if (!restRecorded) {
|
|
282
|
+
+ val backdrop = prepareRest()
|
|
283
|
+
+ node.setPosition(restLimits)
|
|
284
|
+
+ val recording = node.beginRecording(restLimits.width(), restLimits.height())
|
|
285
|
+
+ recording.translate(-restLimits.left.toFloat(), -restLimits.top.toFloat())
|
|
286
|
+
+ renderer.prepare(surfaceState)
|
|
287
|
+
+ renderer.drawGlass(recording, backdrop, surfaceState, restLimits)
|
|
288
|
+
+ node.endRecording()
|
|
289
|
+
+ restRecorded = !renderer.warmPipelines(canvas)
|
|
290
|
+
+ if (!restRecorded) postInvalidateOnAnimation()
|
|
291
|
+
+ }
|
|
292
|
+
+ canvas.drawRenderNode(node)
|
|
293
|
+
+ }
|
|
294
|
+
+
|
|
295
|
+
+ // The settled lens: the still of its look, shared with every trigger that looks the same. False until it has
|
|
296
|
+
+ // arrived (the view is invalidated then); the live lens is drawn meanwhile.
|
|
297
|
+
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
298
|
+
+ private fun drawRestStill(canvas: Canvas, renderer: GlassRenderer): Boolean {
|
|
299
|
+
+ val backdrop = prepareRest()
|
|
300
|
+
+ val state = surfaceState
|
|
301
|
+
+ val look = RestLook(
|
|
302
|
+
+ restLimits.width(), restLimits.height(), state.radiusX, restBackdrop ?: 0, state.dark, density,
|
|
303
|
+
+ config.blur, config.tintColor?.toArgb(), config.tintOpacity, config.saturation, config.refraction, config.rim,
|
|
304
|
+
+ triggerConfig.blur, triggerConfig.tintColor?.toArgb(), triggerConfig.tintOpacity, triggerConfig.saturation,
|
|
305
|
+
+ triggerConfig.refraction, triggerConfig.rim,
|
|
306
|
+
+ )
|
|
307
|
+
+ if (look != restLook) {
|
|
308
|
+
+ restLook?.let { SharedStills.release(it) }
|
|
309
|
+
+ restLook = look
|
|
310
|
+
+ val limits = Rect(restLimits)
|
|
311
|
+
+ SharedStills.acquire(look, limits.width(), limits.height()) { recording ->
|
|
312
|
+
+ recording.translate(-limits.left.toFloat(), -limits.top.toFloat())
|
|
313
|
+
+ renderer.prepare(state)
|
|
314
|
+
+ renderer.drawGlass(recording, backdrop, state, limits)
|
|
315
|
+
+ }
|
|
316
|
+
+ }
|
|
317
|
+
+ val still = SharedStills.bitmap(look, this) ?: return false
|
|
318
|
+
+ if (restNode != null) {
|
|
319
|
+
+ (restNode as RenderNode).discardDisplayList()
|
|
320
|
+
+ restNode = null
|
|
321
|
+
+ restRecorded = false
|
|
322
|
+
+ }
|
|
323
|
+
+ canvas.drawBitmap(still, restLimits.left.toFloat(), restLimits.top.toFloat(), null)
|
|
324
|
+
+ if (renderer.warmPipelines(canvas)) postInvalidateOnAnimation()
|
|
325
|
+
+ return true
|
|
326
|
+
+ }
|
|
327
|
+
+
|
|
328
|
+
+ // The resting lens's limits and state, in this view's coordinates, and the fill it samples.
|
|
329
|
+
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
330
|
+
+ private fun prepareRest(): RenderNode {
|
|
331
|
+
+ val pad = (RestCaptureMargin + RestGuard) * density
|
|
332
|
+
+ restLimits.set(-pad.toInt(), -pad.toInt(), width + pad.toInt(), height + pad.toInt())
|
|
333
|
+
+ val backdrop = restBackdropNode as? RenderNode ?: RenderNode("GlassMenu.restBackdrop").also { restBackdropNode = it }
|
|
334
|
+
+ backdrop.setPosition(restLimits)
|
|
335
|
+
+ val fill = backdrop.beginRecording(restLimits.width(), restLimits.height())
|
|
336
|
+
+ fill.drawColor(restBackdrop ?: 0)
|
|
337
|
+
+ backdrop.endRecording()
|
|
338
|
+
+
|
|
339
|
+
+ val state = surfaceState
|
|
340
|
+
+ restRect.set(0f, 0f, width.toFloat(), height.toFloat())
|
|
341
|
+
+ state.incoming.set(restRect)
|
|
342
|
+
+ state.captureBounds.set(restRect)
|
|
343
|
+
+ state.captureBounds.inset(-RestCaptureMargin * density, -RestCaptureMargin * density)
|
|
344
|
+
+ state.lockedFieldStep = 0f
|
|
345
|
+
+ jelly.deformControl(state.incoming, density, triggerConfig.jelly)
|
|
346
|
+
+ val radius = cornerRadiusPx(state.incoming)
|
|
347
|
+
+ state.radiusX = radius
|
|
348
|
+
+ state.radiusY = radius
|
|
349
|
+
+ state.hasSource = false
|
|
350
|
+
+ state.members.clear()
|
|
351
|
+
+ state.smoothing = 0f
|
|
352
|
+
+ state.lensHeight = 0f
|
|
353
|
+
+ state.lensAmount = 0f
|
|
354
|
+
+ state.materialProgress = 0f
|
|
355
|
+
+ state.surfaceOpacity = 1f
|
|
356
|
+
+ state.dark = isDark()
|
|
357
|
+
+ state.config = config
|
|
358
|
+
+ state.trigger = triggerConfig
|
|
359
|
+
+ applyLight(state, restRect, 1f)
|
|
360
|
+
+ return backdrop
|
|
361
|
+
+ }
|
|
362
|
+
+
|
|
363
|
+
+ // What the resting lens drawn in place looks like: its size, shape, fill, scheme and material. Triggers that agree
|
|
364
|
+
+ // on all of it draw the same pixels, so they share one still.
|
|
365
|
+
+ private data class RestLook(
|
|
366
|
+
+ val width: Int, val height: Int, val radius: Float, val fill: Int, val dark: Boolean, val density: Float,
|
|
367
|
+
+ val blur: Float, val tint: Int?, val tintOpacity: Float?, val saturation: Float, val refraction: Float, val rim: Float,
|
|
368
|
+
+ val triggerBlur: Float, val triggerTint: Int?, val triggerTintOpacity: Float?, val triggerSaturation: Float,
|
|
369
|
+
+ val triggerRefraction: Float, val triggerRim: Float,
|
|
370
|
+
+ )
|
|
371
|
+
+
|
|
372
|
+
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
|
|
373
|
+
- if (event.actionMasked == MotionEvent.ACTION_DOWN) {
|
|
374
|
+
- pressX = event.x
|
|
375
|
+
- pressY = event.y
|
|
376
|
+
- pressTime = SystemClock.uptimeMillis()
|
|
377
|
+
+ when (event.actionMasked) {
|
|
378
|
+
+ MotionEvent.ACTION_DOWN -> {
|
|
379
|
+
+ pressX = event.x
|
|
380
|
+
+ pressY = event.y
|
|
381
|
+
+ pressTime = SystemClock.uptimeMillis()
|
|
382
|
+
+ tapping = openOnTap && items.isNotEmpty()
|
|
383
|
+
+ }
|
|
384
|
+
+ MotionEvent.ACTION_MOVE ->
|
|
385
|
+
+ if (abs(event.x - pressX) > touchSlop || abs(event.y - pressY) > touchSlop) tapping = false
|
|
386
|
+
+ MotionEvent.ACTION_CANCEL -> tapping = false
|
|
387
|
+
+ MotionEvent.ACTION_UP -> if (tapping) {
|
|
388
|
+
+ tapping = false
|
|
389
|
+
+ val inside = event.x >= 0f && event.y >= 0f && event.x <= width && event.y <= height
|
|
390
|
+
+ if (inside && SystemClock.uptimeMillis() - pressTime < ViewConfiguration.getLongPressTimeout()) {
|
|
391
|
+
+ open(null, null, null, null)
|
|
392
|
+
+ }
|
|
393
|
+
+ }
|
|
394
|
+
}
|
|
395
|
+
if (drawsGlass) {
|
|
396
|
+
when (event.actionMasked) {
|
|
397
|
+
@@ -182,7 +404,28 @@
|
|
398
|
+
|
|
399
|
+
override fun dispatchDraw(canvas: Canvas) {
|
|
400
|
+
if (overlayDrawsChildren) return
|
|
401
|
+
+ if (!drawsRestInPlace || jelly.isSettled) {
|
|
402
|
+
+ super.dispatchDraw(canvas)
|
|
403
|
+
+ return
|
|
404
|
+
+ }
|
|
405
|
+
+ // Pressed, the content follows the lens's jelly, as the overlay scales a resting trigger's content.
|
|
406
|
+
+ val shape = surfaceState.incoming
|
|
407
|
+
+ val save = canvas.save()
|
|
408
|
+
+ canvas.translate(shape.left, shape.top)
|
|
409
|
+
+ canvas.scale(shape.width() / width, shape.height() / height)
|
|
410
|
+
super.dispatchDraw(canvas)
|
|
411
|
+
+ canvas.restoreToCount(save)
|
|
412
|
+
+ }
|
|
413
|
+
+
|
|
414
|
+
+ // The content again, into the node the overlay already draws, with the node's placement kept: content that redrew
|
|
415
|
+
+ // (a trigger drawn by the overlay keeps it out of the page, dispatchDraw) reaches the screen without the overlay
|
|
416
|
+
+ // recording anything else.
|
|
417
|
+
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
418
|
+
+ internal fun refreshContent() {
|
|
419
|
+
+ val node = contentNode as? RenderNode ?: return
|
|
420
|
+
+ val recording = node.beginRecording(width, height)
|
|
421
|
+
+ super.dispatchDraw(recording)
|
|
422
|
+
+ node.endRecording()
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
426
|
+
@@ -236,6 +479,16 @@
|
|
427
|
+
val target = overlay ?: return
|
|
428
|
+
if (items.isEmpty() || this.width == 0 || this.height == 0) return
|
|
429
|
+
if (menuActive && target.menuSession?.expanded == true) return
|
|
430
|
+
+ // Marks the open in a system trace (atrace `view`), for tap-to-first-frame measurements.
|
|
431
|
+
+ Trace.beginSection("BloomLens.open")
|
|
432
|
+
+ try {
|
|
433
|
+
+ openNow(x, y, width, height, target)
|
|
434
|
+
+ } finally {
|
|
435
|
+
+ Trace.endSection()
|
|
436
|
+
+ }
|
|
437
|
+
+ }
|
|
438
|
+
+
|
|
439
|
+
+ private fun openNow(x: Double?, y: Double?, width: Double?, height: Double?, target: GlassMenuOverlay) {
|
|
440
|
+
val pressed = if (followsPress) pressedRect(target) else null
|
|
441
|
+
val through = pressed ?: if (x != null && y != null && width != null && height != null) {
|
|
442
|
+
RectF(
|
|
443
|
+
@@ -275,11 +528,12 @@
|
|
444
|
+
internal fun onSessionFinished() {
|
|
445
|
+
menuActive = false
|
|
446
|
+
invalidate()
|
|
447
|
+
+ onMenuClosed(mapOf())
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
internal companion object {
|
|
451
|
+
const val FieldStep = 4f
|
|
452
|
+
- const val IconHostId = "glass-menu-icons"
|
|
453
|
+
+ const val IconHostId = "lens-icons"
|
|
454
|
+
const val PressMemory = 1000L
|
|
455
|
+
const val MorphFromPress = "press"
|
|
456
|
+
const val PressRadius = 22f
|
|
457
|
+
@@ -287,5 +541,8 @@
|
|
458
|
+
const val LightLift = .055f
|
|
459
|
+
const val LightPool = .07f
|
|
460
|
+
const val LightRimLift = .3f
|
|
461
|
+
+ // The resting capture margin around a trigger (the overlay's RestCaptureMargin) and the blur's guard beyond it.
|
|
462
|
+
+ const val RestCaptureMargin = 16f
|
|
463
|
+
+ const val RestGuard = 24f
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
--- a/overlay/GlassMenuOverlay.kt
|
|
467
|
+
+++ b/overlay/GlassMenuOverlay.kt
|
|
468
|
+
@@ -50,6 +50,9 @@
|
|
469
|
+
private var backCallback: Any? = null
|
|
470
|
+
private var animationScale = 1f
|
|
471
|
+
private var startupFrames = 0
|
|
472
|
+
+ // What the overlay draws (the resting lenses and the open menu), in a node of its own: a frame of the page that moves
|
|
473
|
+
+ // a trigger or redraws its content records it again from the pre-draw, without invalidating the overlay.
|
|
474
|
+
+ private var drawing: Any? = null
|
|
475
|
+
|
|
476
|
+
init {
|
|
477
|
+
setWillNotDraw(false)
|
|
478
|
+
@@ -91,26 +94,33 @@
|
|
479
|
+
viewTreeObserver.removeOnPreDrawListener(this)
|
|
480
|
+
Choreographer.getInstance().removeFrameCallback(this)
|
|
481
|
+
framePending = false
|
|
482
|
+
+ if (supportsGlass) (drawing as? RenderNode)?.discardDisplayList()
|
|
483
|
+
+ drawing = null
|
|
484
|
+
super.onDetachedFromWindow()
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
+ // The page behind the glass reaches it through the backdrop's node (GlassBackdrop): what is drawn here is recorded
|
|
488
|
+
+ // again only for a trigger that moved or faded, and from here, into the node onDraw draws, unless the overlay is
|
|
489
|
+
+ // already invalidated for this frame. An invalidation from a pre-draw would schedule another frame. A trigger whose
|
|
490
|
+
+ // content redrew records only that content, into the node this overlay already draws (an animated avatar in a
|
|
491
|
+
+ // header trigger records no glass).
|
|
492
|
+
override fun onPreDraw(): Boolean {
|
|
493
|
+
- if (backdropChanged() or anchorsMoved()) invalidate()
|
|
494
|
+
+ if (!supportsGlass || !isHardwareAccelerated) return true
|
|
495
|
+
+ if (anchorsMoved()) {
|
|
496
|
+
+ if (!isDirty) recordGlass()
|
|
497
|
+
+ } else if (!isDirty) {
|
|
498
|
+
+ for (anchor in anchors) if (drawnHere(anchor) && anchor.isDirty) anchor.refreshContent()
|
|
499
|
+
+ }
|
|
500
|
+
return true
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
- private fun backdropChanged(): Boolean {
|
|
504
|
+
- for (index in 0 until root.childCount) {
|
|
505
|
+
- val child = root.getChildAt(index)
|
|
506
|
+
- if (child !== this && child.isDirty) return true
|
|
507
|
+
- }
|
|
508
|
+
- return false
|
|
509
|
+
- }
|
|
510
|
+
+ private fun drawnHere(anchor: GlassMenuAndroidView): Boolean =
|
|
511
|
+
+ (anchor.drawsGlass && !anchor.inPlace) || session?.anchor === anchor
|
|
512
|
+
|
|
513
|
+
private fun anchorsMoved(): Boolean {
|
|
514
|
+
var moved = false
|
|
515
|
+
for (anchor in anchors) {
|
|
516
|
+
- if (!anchor.drawsGlass && session?.anchor !== anchor) continue
|
|
517
|
+
+ if (!drawnHere(anchor)) continue
|
|
518
|
+
anchorRect(anchor, probe)
|
|
519
|
+
val alpha = if (anchor.isShown) anchor.effectiveAlpha() else 0f
|
|
520
|
+
if (probe != anchor.placement || alpha != anchor.placementAlpha) {
|
|
521
|
+
@@ -118,32 +128,51 @@
|
|
522
|
+
anchor.placementAlpha = alpha
|
|
523
|
+
moved = true
|
|
524
|
+
}
|
|
525
|
+
+ // A group's glasses follow their views: one that moved, arrived or left is drawn again now, and springs on.
|
|
526
|
+
+ if (anchor.group.measure(anchor)) {
|
|
527
|
+
+ moved = true
|
|
528
|
+
+ if (!anchor.group.isSettled) requestFrame()
|
|
529
|
+
+ }
|
|
530
|
+
}
|
|
531
|
+
return moved
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
override fun onDraw(canvas: Canvas) {
|
|
535
|
+
- limits.set(0, 0, width, height)
|
|
536
|
+
- val active = session
|
|
537
|
+
if (!supportsGlass || !canvas.isHardwareAccelerated) {
|
|
538
|
+
- active?.drawFallback(canvas)
|
|
539
|
+
+ session?.drawFallback(canvas)
|
|
540
|
+
return
|
|
541
|
+
}
|
|
542
|
+
- drawGlass(canvas, active)
|
|
543
|
+
+ canvas.drawRenderNode(recordGlass() ?: return)
|
|
544
|
+
+ }
|
|
545
|
+
+
|
|
546
|
+
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
547
|
+
+ private fun recordGlass(): RenderNode? {
|
|
548
|
+
+ if (width <= 0 || height <= 0) return null
|
|
549
|
+
+ val node = drawing as? RenderNode ?: RenderNode("GlassMenuOverlay").also { drawing = it }
|
|
550
|
+
+ node.setPosition(0, 0, width, height)
|
|
551
|
+
+ val canvas = node.beginRecording(width, height)
|
|
552
|
+
+ try {
|
|
553
|
+
+ drawGlass(canvas, session)
|
|
554
|
+
+ } finally {
|
|
555
|
+
+ node.endRecording()
|
|
556
|
+
+ }
|
|
557
|
+
+ return node
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
561
|
+
private fun drawGlass(canvas: Canvas, active: GlassMenuSession?) {
|
|
562
|
+
- val node = backdropProvider?.node ?: return
|
|
563
|
+
+ val page = backdropProvider ?: return
|
|
564
|
+
+ limits.set(0, 0, width, height)
|
|
565
|
+
|
|
566
|
+
for (anchor in anchors) {
|
|
567
|
+
- if (anchor !== active?.anchor && anchor.drawsGlass) drawRestingAnchor(canvas, node, anchor)
|
|
568
|
+
+ // A trigger that draws its lens in place (restBackdrop) is drawn here only while its menu is open.
|
|
569
|
+
+ if (anchor !== active?.anchor && anchor.drawsGlass && !anchor.inPlace) drawRestingAnchor(canvas, page, anchor)
|
|
570
|
+
}
|
|
571
|
+
- active?.draw(canvas, node, limits)
|
|
572
|
+
+ active?.draw(canvas, page, limits)
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
576
|
+
- private fun drawRestingAnchor(canvas: Canvas, node: RenderNode, anchor: GlassMenuAndroidView) {
|
|
577
|
+
+ private fun drawRestingAnchor(canvas: Canvas, page: GlassBackdrop, anchor: GlassMenuAndroidView) {
|
|
578
|
+
if (!anchor.isShown || anchor.width == 0 || anchor.height == 0) return
|
|
579
|
+
val opacity = anchor.effectiveAlpha()
|
|
580
|
+
if (opacity <= 0f) return
|
|
581
|
+
@@ -167,10 +196,13 @@
|
|
582
|
+
state.config = anchor.config
|
|
583
|
+
state.trigger = anchor.triggerConfig
|
|
584
|
+
anchor.applyLight(state, restRect, 1f)
|
|
585
|
+
- val renderer = anchor.renderer(density)
|
|
586
|
+
- renderer.prepare(state)
|
|
587
|
+
- renderer.drawGlass(canvas, node, state, limits)
|
|
588
|
+
- if (renderer.warmPipelines(canvas)) post { invalidate() }
|
|
589
|
+
+ // A group draws its glasses, and none while it has none (its first views not laid out yet).
|
|
590
|
+
+ if (anchor.group.place(state.members, state.incoming, anchor.width, anchor.height, density)) {
|
|
591
|
+
+ val renderer = anchor.renderer(density)
|
|
592
|
+
+ renderer.prepare(state)
|
|
593
|
+
+ renderer.drawGlass(canvas, page, state, limits)
|
|
594
|
+
+ if (renderer.warmPipelines(canvas)) post { invalidate() }
|
|
595
|
+
+ }
|
|
596
|
+
val content = anchor.recordContent()
|
|
597
|
+
content.alpha = opacity
|
|
598
|
+
content.setScaleX(state.incoming.width() / anchor.width)
|
|
599
|
+
@@ -265,8 +297,12 @@
|
|
600
|
+
lastFrameNanos = frameTimeNanos
|
|
601
|
+
var running = false
|
|
602
|
+
for (anchor in anchors) {
|
|
603
|
+
+ val wasSettled = anchor.jelly.isSettled
|
|
604
|
+
anchor.jelly.advance(seconds * anchor.config.speed)
|
|
605
|
+
if (!anchor.jelly.isSettled) running = true
|
|
606
|
+
+ if (!wasSettled || !anchor.jelly.isSettled) anchor.jellyFrame()
|
|
607
|
+
+ anchor.group.advance(seconds)
|
|
608
|
+
+ if (!anchor.group.isSettled) running = true
|
|
609
|
+
}
|
|
610
|
+
val active = session
|
|
611
|
+
if (active != null) {
|
|
612
|
+
--- a/overlay/GlassMenuSession.kt
|
|
613
|
+
+++ b/overlay/GlassMenuSession.kt
|
|
614
|
+
@@ -7,6 +7,7 @@
|
|
615
|
+
import android.graphics.RectF
|
|
616
|
+
import android.graphics.RenderEffect
|
|
617
|
+
import android.graphics.RenderNode
|
|
618
|
+
+import so.oxy.bloom.glassmenu.backdrop.GlassBackdrop
|
|
619
|
+
import android.graphics.Shader
|
|
620
|
+
import android.os.Build
|
|
621
|
+
import android.view.MotionEvent
|
|
622
|
+
@@ -99,6 +100,7 @@
|
|
623
|
+
val margin = 16f * density
|
|
624
|
+
availableWidth = windowWidth - margin * 2f
|
|
625
|
+
content.dark = anchor.isDark()
|
|
626
|
+
+ content.ink = anchor.config
|
|
627
|
+
levelItems = null
|
|
628
|
+
nav.reset(1f)
|
|
629
|
+
parentDim.reset(0f)
|
|
630
|
+
@@ -292,13 +294,13 @@
|
|
631
|
+
jelly.deformMenu(out, if (opensFromRight) -1f else 1f, if (opensAbove) -1f else 1f, density, anchor.config.jelly)
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
- fun draw(canvas: Canvas, backdrop: RenderNode, limits: Rect) {
|
|
635
|
+
+ fun draw(canvas: Canvas, backdrop: GlassBackdrop, limits: Rect) {
|
|
636
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
|
|
637
|
+
drawRoot(canvas, backdrop, limits)
|
|
638
|
+
child?.draw(canvas, backdrop, limits)
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
- private fun drawRoot(canvas: Canvas, backdrop: RenderNode, limits: Rect) {
|
|
642
|
+
+ private fun drawRoot(canvas: Canvas, backdrop: GlassBackdrop, limits: Rect) {
|
|
643
|
+
val frame = motion.frame
|
|
644
|
+
liveBounds(shown)
|
|
645
|
+
// While a stacked submenu is open the root recedes: the glass shrinks and the rows dim.
|
|
646
|
+
@@ -317,6 +319,9 @@
|
|
647
|
+
// A plain trigger keeps its own corner radius (cornerRadius), not the capsule of a glass lobe.
|
|
648
|
+
state.sourceRadius = if (anchor.glass) min(state.source.width(), state.source.height()) / 2f
|
|
649
|
+
else anchor.cornerRadiusPx(state.source)
|
|
650
|
+
+ // A group's glasses are its lobe: they follow it as its content does, scaled into it.
|
|
651
|
+
+ if (anchor.morphs) anchor.group.place(state.members, state.source, anchor.width, anchor.height, density)
|
|
652
|
+
+ else state.members.clear()
|
|
653
|
+
state.smoothing = frame.smoothing
|
|
654
|
+
state.lensHeight = frame.lensHeight
|
|
655
|
+
state.lensAmount = frame.lensAmount
|
|
656
|
+
@@ -557,6 +562,7 @@
|
|
657
|
+
anchor, density,
|
|
658
|
+
GlassMenuContent(overlay.context, density) { slot -> anchor.iconView(slot) }.also {
|
|
659
|
+
it.dark = content.dark
|
|
660
|
+
+ it.ink = content.ink
|
|
661
|
+
it.forceCheckColumn = content.reservesCheckColumn
|
|
662
|
+
},
|
|
663
|
+
if (supportsGlass) RenderNode("GlassMenu.submenu.rows") else null,
|
|
664
|
+
--- a/records/GlassMenuConfigRecord.kt
|
|
665
|
+
+++ b/records/GlassMenuConfigRecord.kt
|
|
666
|
+
@@ -9,6 +9,11 @@
|
|
667
|
+
@Field val morphBlur: Float = 1f
|
|
668
|
+
@Field val tintColor: Color? = null
|
|
669
|
+
@Field val tintOpacity: Float? = null
|
|
670
|
+
+ // Row ink; null keeps the built-in light/dark colours.
|
|
671
|
+
+ @Field val textColor: Color? = null
|
|
672
|
+
+ @Field val mutedColor: Color? = null
|
|
673
|
+
+ @Field val destructiveColor: Color? = null
|
|
674
|
+
+ @Field val separatorColor: Color? = null
|
|
675
|
+
@Field val saturation: Float = 1f
|
|
676
|
+
@Field val refraction: Float = 1f
|
|
677
|
+
@Field val rim: Float = 1f
|
|
678
|
+
--- a/render/GlassRenderer.kt
|
|
679
|
+
+++ b/render/GlassRenderer.kt
|
|
680
|
+
@@ -12,8 +12,12 @@
|
|
681
|
+
import android.graphics.Shader
|
|
682
|
+
import android.os.Build
|
|
683
|
+
import androidx.annotation.RequiresApi
|
|
684
|
+
+import java.util.concurrent.atomic.AtomicBoolean
|
|
685
|
+
+import so.oxy.bloom.glassmenu.backdrop.GlassBackdrop
|
|
686
|
+
+import so.oxy.bloom.glassmenu.backdrop.GlassLens
|
|
687
|
+
import so.oxy.bloom.glassmenu.field.GlassFieldTexture
|
|
688
|
+
import so.oxy.bloom.glassmenu.field.GlassGaussianField
|
|
689
|
+
+import so.oxy.bloom.glassmenu.field.GlassShapes
|
|
690
|
+
import kotlin.math.ceil
|
|
691
|
+
import kotlin.math.floor
|
|
692
|
+
import kotlin.math.max
|
|
693
|
+
@@ -35,10 +39,21 @@
|
|
694
|
+
private val texture = GlassFieldTexture()
|
|
695
|
+
private val nodeRect = Rect()
|
|
696
|
+
private val union = RectF()
|
|
697
|
+
- private var warmed = false
|
|
698
|
+
+ private val memberBounds = RectF()
|
|
699
|
+
+ private val memberShapes = FloatArray(MemberSlots * 4)
|
|
700
|
+
+ private val memberRadii = FloatArray(MemberSlots)
|
|
701
|
+
+ // This surface's view of the window page (GlassLens): the page while the region behind it changes, else its still.
|
|
702
|
+
+ private var lens: GlassLens? = null
|
|
703
|
+
+ private val signature = FloatArray(SignatureSize)
|
|
704
|
+
+ private val recorded = FloatArray(SignatureSize)
|
|
705
|
+
+ private var recordedSource: RenderNode? = null
|
|
706
|
+
+ private var recordedConfig: Any? = null
|
|
707
|
+
+ private var recordedTrigger: Any? = null
|
|
708
|
+
|
|
709
|
+
init {
|
|
710
|
+
- warmField()
|
|
711
|
+
+ // The warm-ups prime what every renderer shares (the field code, the GPU pipelines), so they run once per
|
|
712
|
+
+ // process: a lens per feed row would otherwise start a thread and draw the warm-up for every row.
|
|
713
|
+
+ if (fieldWarmed.compareAndSet(false, true)) warmField()
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
private fun warmField() {
|
|
717
|
+
@@ -59,8 +74,7 @@
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
fun warmPipelines(canvas: Canvas): Boolean {
|
|
721
|
+
- if (warmed) return false
|
|
722
|
+
- warmed = true
|
|
723
|
+
+ if (!pipelinesWarmed.compareAndSet(false, true)) return false
|
|
724
|
+
texture.preallocate()
|
|
725
|
+
texture.bindPrepared(material)
|
|
726
|
+
texture.bindPrepared(portal)
|
|
727
|
+
@@ -94,6 +108,7 @@
|
|
728
|
+
val active = field.update(
|
|
729
|
+
state.incoming, state.radiusX, state.radiusY,
|
|
730
|
+
if (state.hasSource) state.source else null, state.sourceRadius, state.smoothing, state.lockedFieldStep,
|
|
731
|
+
+ state.members,
|
|
732
|
+
)
|
|
733
|
+
texture.update(if (active) field else null)
|
|
734
|
+
bindField(material, state)
|
|
735
|
+
@@ -102,22 +117,52 @@
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
private fun bindField(shader: RuntimeShader, state: GlassSurfaceState) {
|
|
739
|
+
- val a = state.incoming
|
|
740
|
+
+ // A trigger of several shapes (a group of glasses) draws them in its place, the source of a morph or else the
|
|
741
|
+
+ // glass: its first shape takes that place, the others follow in the member uniforms.
|
|
742
|
+
+ val shapes = state.members
|
|
743
|
+
+ val inIncoming = shapes.count > 0 && !state.hasSource
|
|
744
|
+
+ val inSource = shapes.count > 0 && state.hasSource
|
|
745
|
+
+ val a = if (inIncoming) shapes.rects[0] else state.incoming
|
|
746
|
+
shader.setFloatUniform("center", a.centerX(), a.centerY())
|
|
747
|
+
shader.setFloatUniform("halfSize", a.width() / 2f, a.height() / 2f)
|
|
748
|
+
- shader.setFloatUniform("cornerRadii", state.radiusX, state.radiusY)
|
|
749
|
+
- val b = if (state.hasSource) state.source else a
|
|
750
|
+
+ if (inIncoming) shader.setFloatUniform("cornerRadii", shapes.radii[0], shapes.radii[0])
|
|
751
|
+
+ else shader.setFloatUniform("cornerRadii", state.radiusX, state.radiusY)
|
|
752
|
+
+ val b = if (inSource) shapes.rects[0] else if (state.hasSource) state.source else a
|
|
753
|
+
+ val bRadius = if (inSource) shapes.radii[0] else state.sourceRadius
|
|
754
|
+
shader.setFloatUniform("secondaryEnabled", if (state.hasSource) 1f else 0f)
|
|
755
|
+
shader.setFloatUniform("secondaryCenter", b.centerX(), b.centerY())
|
|
756
|
+
shader.setFloatUniform("secondaryHalfSize", b.width() / 2f, b.height() / 2f)
|
|
757
|
+
- shader.setFloatUniform("secondaryRadii", state.sourceRadius, state.sourceRadius)
|
|
758
|
+
+ shader.setFloatUniform("secondaryRadii", bRadius, bRadius)
|
|
759
|
+
shader.setFloatUniform("smoothing", state.smoothing)
|
|
760
|
+
+ memberShapes.fill(0f)
|
|
761
|
+
+ memberRadii.fill(0f)
|
|
762
|
+
+ for (index in 1 until shapes.count) {
|
|
763
|
+
+ val rect = shapes.rects[index]
|
|
764
|
+
+ val slot = (index - 1) * 4
|
|
765
|
+
+ memberShapes[slot] = rect.centerX()
|
|
766
|
+
+ memberShapes[slot + 1] = rect.centerY()
|
|
767
|
+
+ memberShapes[slot + 2] = rect.width() / 2f
|
|
768
|
+
+ memberShapes[slot + 3] = rect.height() / 2f
|
|
769
|
+
+ memberRadii[index - 1] = shapes.radii[index]
|
|
770
|
+
+ }
|
|
771
|
+
+ shader.setFloatUniform("memberCount", max(shapes.count - 1, 0).toFloat())
|
|
772
|
+
+ shader.setFloatUniform("memberBlend", shapes.blend)
|
|
773
|
+
+ shader.setFloatUniform("members", memberShapes)
|
|
774
|
+
+ shader.setFloatUniform("memberRadii", memberRadii)
|
|
775
|
+
texture.bind(shader)
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
- fun drawGlass(canvas: Canvas, backdrop: RenderNode, state: GlassSurfaceState, limits: Rect) {
|
|
779
|
+
+ // The glass over the window page, sampled through this renderer's lens.
|
|
780
|
+
+ fun drawGlass(canvas: Canvas, page: GlassBackdrop, state: GlassSurfaceState, limits: Rect) =
|
|
781
|
+
+ drawGlass(canvas, page.node, state, limits, page)
|
|
782
|
+
+
|
|
783
|
+
+ fun drawGlass(canvas: Canvas, backdrop: RenderNode, state: GlassSurfaceState, limits: Rect, page: GlassBackdrop? = null) {
|
|
784
|
+
union.set(state.incoming)
|
|
785
|
+
if (state.hasSource) union.union(state.source)
|
|
786
|
+
+ if (state.members.count > 0) {
|
|
787
|
+
+ state.members.bounds(memberBounds)
|
|
788
|
+
+ union.union(memberBounds)
|
|
789
|
+
+ }
|
|
790
|
+
val t = state.materialProgress.coerceIn(0f, 1f)
|
|
791
|
+
val config = state.config
|
|
792
|
+
val trigger = state.trigger
|
|
793
|
+
@@ -138,6 +183,13 @@
|
|
794
|
+
if (nodeRect.isEmpty) return
|
|
795
|
+
val width = nodeRect.width()
|
|
796
|
+
val height = nodeRect.height()
|
|
797
|
+
+ // A surface drawn again as it was keeps its recording: its layer is not rendered again (a resting lens while a
|
|
798
|
+
+ // menu opens beside it). What it samples may still change behind it, through the source node.
|
|
799
|
+
+ val source = if (page != null) lensOn(page).sample(nodeRect) else backdrop
|
|
800
|
+
+ if (sameAsRecorded(state, source)) {
|
|
801
|
+
+ canvas.drawRenderNode(glassNode)
|
|
802
|
+
+ return
|
|
803
|
+
+ }
|
|
804
|
+
|
|
805
|
+
val control = if (state.dark) GlassRecipe.ControlDark else GlassRecipe.ControlLight
|
|
806
|
+
val menu = if (state.dark) GlassRecipe.MenuDark else GlassRecipe.MenuLight
|
|
807
|
+
@@ -194,11 +246,48 @@
|
|
808
|
+
glassNode.setPosition(nodeRect)
|
|
809
|
+
val recording = glassNode.beginRecording(width, height)
|
|
810
|
+
recording.translate(-nodeRect.left.toFloat(), -nodeRect.top.toFloat())
|
|
811
|
+
- recording.drawRenderNode(backdrop)
|
|
812
|
+
+ recording.drawRenderNode(source)
|
|
813
|
+
glassNode.endRecording()
|
|
814
|
+
canvas.drawRenderNode(glassNode)
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
+ // Every value the glass is recorded from, against the last recording; the field texture follows the same state.
|
|
818
|
+
+ private fun sameAsRecorded(state: GlassSurfaceState, source: RenderNode): Boolean {
|
|
819
|
+
+ val values = signature
|
|
820
|
+
+ var index = 0
|
|
821
|
+
+ fun put(value: Float) {
|
|
822
|
+
+ values[index++] = value
|
|
823
|
+
+ }
|
|
824
|
+
+ fun put(rect: RectF) {
|
|
825
|
+
+ put(rect.left); put(rect.top); put(rect.right); put(rect.bottom)
|
|
826
|
+
+ }
|
|
827
|
+
+ put(state.incoming); put(state.source); put(state.captureBounds)
|
|
828
|
+
+ put(nodeRect.left.toFloat()); put(nodeRect.top.toFloat()); put(nodeRect.right.toFloat()); put(nodeRect.bottom.toFloat())
|
|
829
|
+
+ put(if (state.hasSource) 1f else 0f); put(if (state.dark) 1f else 0f)
|
|
830
|
+
+ put(state.radiusX); put(state.radiusY); put(state.sourceRadius); put(state.smoothing); put(state.lockedFieldStep)
|
|
831
|
+
+ put(state.lensHeight); put(state.lensAmount); put(state.lensBand); put(state.materialProgress)
|
|
832
|
+
+ put(state.surfaceOpacity); put(state.refractionScale); put(state.magnify)
|
|
833
|
+
+ put(state.pressX); put(state.pressY); put(state.pressRadius); put(state.poolGain); put(state.pressLift); put(state.rimLift)
|
|
834
|
+
+ val shapes = state.members
|
|
835
|
+
+ put(shapes.count.toFloat()); put(shapes.blend)
|
|
836
|
+
+ for (slot in 0 until GlassShapes.Capacity) {
|
|
837
|
+
+ if (slot < shapes.count) {
|
|
838
|
+
+ put(shapes.rects[slot]); put(shapes.radii[slot])
|
|
839
|
+
+ } else {
|
|
840
|
+
+ repeat(5) { put(0f) }
|
|
841
|
+
+ }
|
|
842
|
+
+ }
|
|
843
|
+
+ val same = recordedSource === source && recordedConfig === state.config && recordedTrigger === state.trigger &&
|
|
844
|
+
+ values.contentEquals(recorded)
|
|
845
|
+
+ if (!same) {
|
|
846
|
+
+ values.copyInto(recorded)
|
|
847
|
+
+ recordedSource = source
|
|
848
|
+
+ recordedConfig = state.config
|
|
849
|
+
+ recordedTrigger = state.trigger
|
|
850
|
+
+ }
|
|
851
|
+
+ return same
|
|
852
|
+
+ }
|
|
853
|
+
+
|
|
854
|
+
fun drawForeground(
|
|
855
|
+
canvas: Canvas, state: GlassSurfaceState, warp: Boolean, portalBlur: Float,
|
|
856
|
+
record: (RecordingCanvas) -> Unit,
|
|
857
|
+
@@ -235,13 +324,27 @@
|
|
858
|
+
)
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
+ private fun lensOn(page: GlassBackdrop): GlassLens =
|
|
862
|
+
+ lens?.takeIf { it.page === page } ?: page.lens().also {
|
|
863
|
+
+ lens?.release()
|
|
864
|
+
+ lens = it
|
|
865
|
+
+ }
|
|
866
|
+
+
|
|
867
|
+
fun release() {
|
|
868
|
+
+ lens?.release()
|
|
869
|
+
+ lens = null
|
|
870
|
+
+ recordedSource = null
|
|
871
|
+
glassNode.discardDisplayList()
|
|
872
|
+
foregroundNode.discardDisplayList()
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
private companion object {
|
|
876
|
+
+ val fieldWarmed = AtomicBoolean(false)
|
|
877
|
+
+ val pipelinesWarmed = AtomicBoolean(false)
|
|
878
|
+
const val MorphBlur = 1.2f
|
|
879
|
+
+ const val SignatureSize = 38 + GlassShapes.Capacity * 5
|
|
880
|
+
+ // The shader's member uniforms: a trigger's shapes beyond its first.
|
|
881
|
+
+ const val MemberSlots = GlassShapes.Capacity - 1
|
|
882
|
+
const val BlurReach = 2f
|
|
883
|
+
const val PortalReach = 28f
|
|
884
|
+
const val WarmRounds = 40
|
|
885
|
+
--- a/backdrop/GlassBackdrop.kt
|
|
886
|
+
+++ b/backdrop/GlassBackdrop.kt
|
|
887
|
+
@@ -1,9 +1,14 @@
|
|
888
|
+
package so.oxy.bloom.glassmenu.backdrop
|
|
889
|
+
|
|
890
|
+
+import android.graphics.Rect
|
|
891
|
+
+import android.graphics.RectF
|
|
892
|
+
import android.graphics.RenderNode
|
|
893
|
+
+import android.os.Build
|
|
894
|
+
+import android.os.SystemClock
|
|
895
|
+
import android.view.View
|
|
896
|
+
import android.view.ViewGroup
|
|
897
|
+
import android.view.ViewTreeObserver
|
|
898
|
+
+import androidx.annotation.RequiresApi
|
|
899
|
+
import java.util.WeakHashMap
|
|
900
|
+
|
|
901
|
+
// The window backdrop: the page behind every glass surface of the window (menus, tab bar, search, create circle).
|
|
902
|
+
@@ -13,13 +18,30 @@
|
|
903
|
+
// Consumers call register(view, baseColor) and unregister(view). A registered view that is a direct child of the root
|
|
904
|
+
// (the menu overlay) is left out of the recording. Surfaces that live inside the page skip their own drawing while
|
|
905
|
+
// `recording` is true (dispatchDraw / onDraw check it), so they are not recorded into the backdrop.
|
|
906
|
+
-internal class GlassBackdrop private constructor(private val root: ViewGroup) : ViewTreeObserver.OnPreDrawListener {
|
|
907
|
+
+//
|
|
908
|
+
+// The recording holds the page by reference: drawing a child of the root draws each of its children as that view's own
|
|
909
|
+
+// RenderNode, so whatever redraws in the page (a scroll, a video, an animation) reaches every glass surface through
|
|
910
|
+
+// those nodes, and no surface records again for it. The recording itself changes only with the window's layout (its
|
|
911
|
+
+// size and children), the registered surfaces and the base colour. Recorded again for any redraw in the page, it would
|
|
912
|
+
+// make every surface record again too, at the page's frame rate, for an animation nowhere near the glass.
|
|
913
|
+
+//
|
|
914
|
+
+// A surface samples the page through a lens of its own (GlassLens), which holds the page only while the region behind
|
|
915
|
+
+// the surface changes. Before each frame the backdrop finds what changed, in window rects, and tells every lens.
|
|
916
|
+
+internal class GlassBackdrop private constructor(private val root: ViewGroup) :
|
|
917
|
+
+ ViewTreeObserver.OnPreDrawListener, ViewTreeObserver.OnGlobalLayoutListener {
|
|
918
|
+
val node = RenderNode("GlassBackdrop")
|
|
919
|
+
|
|
920
|
+
private val excluded = HashSet<View>()
|
|
921
|
+
private var baseColor: () -> Int = { android.graphics.Color.BLACK }
|
|
922
|
+
private var users = 0
|
|
923
|
+
- private var recorded = false
|
|
924
|
+
+ private var stale = true
|
|
925
|
+
+ private var recordedColor = 0
|
|
926
|
+
+ private val lenses = ArrayList<GlassLens>()
|
|
927
|
+
+ private val dirt = ArrayList<Rect>()
|
|
928
|
+
+ private val dirtRect = RectF()
|
|
929
|
+
+ // The place, transform, scroll, alpha and visibility of each view seen on a dirty path, as of the last frame.
|
|
930
|
+
+ private val seen = WeakHashMap<View, FloatArray>()
|
|
931
|
+
+ private val shape = FloatArray(ShapeSize)
|
|
932
|
+
|
|
933
|
+
// Bumped each time the page is recorded: consumers that cache the backdrop regenerate when it changes.
|
|
934
|
+
var version: Int = 0
|
|
935
|
+
@@ -29,30 +51,121 @@
|
|
936
|
+
excluded.add(view)
|
|
937
|
+
baseColor = color
|
|
938
|
+
users += 1
|
|
939
|
+
- if (users == 1) root.viewTreeObserver.addOnPreDrawListener(this)
|
|
940
|
+
+ stale = true
|
|
941
|
+
+ if (users == 1) {
|
|
942
|
+
+ root.viewTreeObserver.addOnPreDrawListener(this)
|
|
943
|
+
+ root.viewTreeObserver.addOnGlobalLayoutListener(this)
|
|
944
|
+
+ }
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
fun unregister(view: View) {
|
|
948
|
+
excluded.remove(view)
|
|
949
|
+
users -= 1
|
|
950
|
+
+ stale = true
|
|
951
|
+
if (users <= 0) {
|
|
952
|
+
users = 0
|
|
953
|
+
- if (root.viewTreeObserver.isAlive) root.viewTreeObserver.removeOnPreDrawListener(this)
|
|
954
|
+
- recorded = false
|
|
955
|
+
+ if (root.viewTreeObserver.isAlive) {
|
|
956
|
+
+ root.viewTreeObserver.removeOnPreDrawListener(this)
|
|
957
|
+
+ root.viewTreeObserver.removeOnGlobalLayoutListener(this)
|
|
958
|
+
+ }
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
+ override fun onGlobalLayout() {
|
|
963
|
+
+ stale = true
|
|
964
|
+
+ }
|
|
965
|
+
+
|
|
966
|
+
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
967
|
+
+ fun lens(): GlassLens = GlassLens(this).also { lenses.add(it) }
|
|
968
|
+
+
|
|
969
|
+
+ fun drop(lens: GlassLens) {
|
|
970
|
+
+ lenses.remove(lens)
|
|
971
|
+
+ }
|
|
972
|
+
+
|
|
973
|
+
override fun onPreDraw(): Boolean {
|
|
974
|
+
- if (!recorded || anyChildDirty()) record()
|
|
975
|
+
+ val color = baseColor()
|
|
976
|
+
+ val whole = stale || color != recordedColor
|
|
977
|
+
+ if (whole) record(color)
|
|
978
|
+
+ if (lenses.isNotEmpty() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
979
|
+
+ dirt.clear()
|
|
980
|
+
+ if (!whole && root.isDirty) collectDirt(root)
|
|
981
|
+
+ val now = SystemClock.uptimeMillis()
|
|
982
|
+
+ for (lens in lenses) lens.frame(if (whole) null else dirt, now)
|
|
983
|
+
+ }
|
|
984
|
+
return true
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
- private fun anyChildDirty(): Boolean {
|
|
988
|
+
- for (index in 0 until root.childCount) if (root.getChildAt(index).isDirty) return true
|
|
989
|
+
- return false
|
|
990
|
+
+ // What redraws in this frame, from the dirty flags Android keeps under hardware rendering (it keeps no dirty rects):
|
|
991
|
+
+ // each dirty view none of whose children is dirty, and each child of a dirty view whose place, transform, scroll,
|
|
992
|
+
+ // alpha or visibility changed (a change of those marks only its parent), with that parent, which holds where it was.
|
|
993
|
+
+ // Left out: the glass surfaces, views drawn off the page (OffPage), views that show nothing (hidden or clear), and
|
|
994
|
+
+ // views off the window. A pager's off-screen page is one: Compose draws an interop page only while it is on screen,
|
|
995
|
+
+ // so the page's holder keeps the dirty flag it took when it moved away, frame after frame, with nothing under it
|
|
996
|
+
+ // redrawing.
|
|
997
|
+
+ private fun collectDirt(group: ViewGroup) {
|
|
998
|
+
+ var dirtyChild = false
|
|
999
|
+
+ for (index in 0 until group.childCount) {
|
|
1000
|
+
+ val child = group.getChildAt(index)
|
|
1001
|
+
+ if (child in excluded || (child as? OffPage)?.offPage == true) {
|
|
1002
|
+
+ dirtyChild = dirtyChild || child.isDirty
|
|
1003
|
+
+ continue
|
|
1004
|
+
+ }
|
|
1005
|
+
+ if (moved(child)) {
|
|
1006
|
+
+ addDirt(group)
|
|
1007
|
+
+ addDirt(child)
|
|
1008
|
+
+ }
|
|
1009
|
+
+ if (!child.isDirty) continue
|
|
1010
|
+
+ dirtyChild = true
|
|
1011
|
+
+ if (child.visibility != View.VISIBLE || child.alpha == 0f) continue
|
|
1012
|
+
+ if (child is ViewGroup) collectDirt(child) else addDirt(child)
|
|
1013
|
+
+ }
|
|
1014
|
+
+ if (!dirtyChild) addDirt(group)
|
|
1015
|
+
+ }
|
|
1016
|
+
+
|
|
1017
|
+
+ // A view in the page whose drawing is not part of it while [offPage] (a trigger whose lens and content the menu
|
|
1018
|
+
+ // overlay draws): what redraws in it does not change what the glass samples.
|
|
1019
|
+
+ interface OffPage {
|
|
1020
|
+
+ val offPage: Boolean
|
|
1021
|
+
+ }
|
|
1022
|
+
+
|
|
1023
|
+
+ private fun moved(view: View): Boolean {
|
|
1024
|
+
+ shape[0] = view.left.toFloat()
|
|
1025
|
+
+ shape[1] = view.top.toFloat()
|
|
1026
|
+
+ shape[2] = view.right.toFloat()
|
|
1027
|
+
+ shape[3] = view.bottom.toFloat()
|
|
1028
|
+
+ shape[4] = view.scrollX.toFloat()
|
|
1029
|
+
+ shape[5] = view.scrollY.toFloat()
|
|
1030
|
+
+ shape[6] = view.translationX
|
|
1031
|
+
+ shape[7] = view.translationY
|
|
1032
|
+
+ shape[8] = view.scaleX
|
|
1033
|
+
+ shape[9] = view.scaleY
|
|
1034
|
+
+ shape[10] = view.rotation
|
|
1035
|
+
+ shape[11] = view.alpha
|
|
1036
|
+
+ shape[12] = view.visibility.toFloat()
|
|
1037
|
+
+ val last = seen[view]
|
|
1038
|
+
+ if (last != null && last.contentEquals(shape)) return false
|
|
1039
|
+
+ if (last == null) seen[view] = shape.copyOf() else shape.copyInto(last)
|
|
1040
|
+
+ return true
|
|
1041
|
+
+ }
|
|
1042
|
+
+
|
|
1043
|
+
+ // The view's bounds in the root's coordinates (the backdrop's), through every transform and scroll above it.
|
|
1044
|
+
+ private fun addDirt(view: View) {
|
|
1045
|
+
+ dirtRect.set(0f, 0f, view.width.toFloat(), view.height.toFloat())
|
|
1046
|
+
+ var current = view
|
|
1047
|
+
+ while (current !== root) {
|
|
1048
|
+
+ if (!current.matrix.isIdentity) current.matrix.mapRect(dirtRect)
|
|
1049
|
+
+ dirtRect.offset(current.left.toFloat(), current.top.toFloat())
|
|
1050
|
+
+ val parent = current.parent as? View ?: return
|
|
1051
|
+
+ dirtRect.offset(-parent.scrollX.toFloat(), -parent.scrollY.toFloat())
|
|
1052
|
+
+ current = parent
|
|
1053
|
+
+ }
|
|
1054
|
+
+ if (!dirtRect.intersects(0f, 0f, root.width.toFloat(), root.height.toFloat())) return
|
|
1055
|
+
+ val rect = Rect()
|
|
1056
|
+
+ dirtRect.roundOut(rect)
|
|
1057
|
+
+ dirt.add(rect)
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
- private fun record() {
|
|
1061
|
+
+ private fun record(color: Int) {
|
|
1062
|
+
val width = root.width
|
|
1063
|
+
val height = root.height
|
|
1064
|
+
if (width <= 0 || height <= 0) return
|
|
1065
|
+
@@ -70,7 +183,7 @@
|
|
1066
|
+
recording = true
|
|
1067
|
+
try {
|
|
1068
|
+
val canvas = node.beginRecording(width, height)
|
|
1069
|
+
- canvas.drawColor(baseColor())
|
|
1070
|
+
+ canvas.drawColor(color)
|
|
1071
|
+
root.background?.draw(canvas)
|
|
1072
|
+
for (index in 0 until root.childCount) {
|
|
1073
|
+
val child = root.getChildAt(index)
|
|
1074
|
+
@@ -83,12 +196,14 @@
|
|
1075
|
+
recording = false
|
|
1076
|
+
for (view in hidden) view.visibility = View.VISIBLE
|
|
1077
|
+
}
|
|
1078
|
+
- recorded = true
|
|
1079
|
+
+ stale = false
|
|
1080
|
+
+ recordedColor = color
|
|
1081
|
+
version += 1
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
companion object {
|
|
1085
|
+
private val byRoot = WeakHashMap<ViewGroup, GlassBackdrop>()
|
|
1086
|
+
+ private const val ShapeSize = 13
|
|
1087
|
+
|
|
1088
|
+
// True while a backdrop is being recorded. Surfaces inside the page must not draw then.
|
|
1089
|
+
@Volatile
|
|
1090
|
+
--- a/backdrop/GlassLens.kt
|
|
1091
|
+
+++ b/backdrop/GlassLens.kt
|
|
1092
|
+
@@ -0,0 +1,103 @@
|
|
1093
|
+
+package so.oxy.bloom.glassmenu.backdrop
|
|
1094
|
+
+
|
|
1095
|
+
+import android.graphics.Bitmap
|
|
1096
|
+
+import android.graphics.Rect
|
|
1097
|
+
+import android.graphics.RenderNode
|
|
1098
|
+
+import android.os.Build
|
|
1099
|
+
+import android.os.SystemClock
|
|
1100
|
+
+import androidx.annotation.RequiresApi
|
|
1101
|
+
+import so.oxy.bloom.glassmenu.render.GlassStill
|
|
1102
|
+
+
|
|
1103
|
+
+// What one glass surface samples of the page: the page itself, by reference, while anything behind the surface
|
|
1104
|
+
+// changes, and a still of the region behind it once that region has been quiet for a moment (StillAfterMs).
|
|
1105
|
+
+//
|
|
1106
|
+
+// Android draws a RenderNode reached from a second parent in the same frame as fully damaged: every glass that holds
|
|
1107
|
+
+// the page re-renders on every frame the window draws, for an animation anywhere on screen, and the page's tree is
|
|
1108
|
+
+// prepared once more for each of them. A lens that shows its still does not hold the page: its glass is not drawn
|
|
1109
|
+
+// again, and the page is prepared once. The still is the same pixels: the region of the same nodes (GlassStill).
|
|
1110
|
+
+//
|
|
1111
|
+
+// The backdrop tells each lens, before every frame, which window rects change in it (GlassBackdrop.onPreDraw); a lens
|
|
1112
|
+
+// whose region is touched shows the page again in that same frame. The region is the one the glass samples.
|
|
1113
|
+
+@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
1114
|
+
+internal class GlassLens(val page: GlassBackdrop) {
|
|
1115
|
+
+ val node = RenderNode("GlassLens")
|
|
1116
|
+
+ private val region = Rect()
|
|
1117
|
+
+ private var showsStill = false
|
|
1118
|
+
+ private var recorded = false
|
|
1119
|
+
+ private var quietSince = 0L
|
|
1120
|
+
+ // Bumped whenever the page is shown again: a still asked before then is dropped when it arrives.
|
|
1121
|
+
+ private var generation = 0
|
|
1122
|
+
+ private var askedGeneration = -1
|
|
1123
|
+
+ private var still: GlassStill? = null
|
|
1124
|
+
+
|
|
1125
|
+
+ // The node the glass draws for [area] (window px): the region's still, or the page.
|
|
1126
|
+
+ fun sample(area: Rect): RenderNode {
|
|
1127
|
+
+ if (area != region || !recorded) {
|
|
1128
|
+
+ region.set(area)
|
|
1129
|
+
+ showPage(SystemClock.uptimeMillis())
|
|
1130
|
+
+ }
|
|
1131
|
+
+ return node
|
|
1132
|
+
+ }
|
|
1133
|
+
+
|
|
1134
|
+
+ // Before a frame: [dirt] are the window rects that change in it (null: the whole page changed).
|
|
1135
|
+
+ fun frame(dirt: List<Rect>?, now: Long) {
|
|
1136
|
+
+ if (!recorded || region.isEmpty) return
|
|
1137
|
+
+ if (dirt == null || dirt.any { Rect.intersects(it, region) }) {
|
|
1138
|
+
+ showPage(now)
|
|
1139
|
+
+ } else if (!showsStill && still == null && now - quietSince >= StillAfterMs) {
|
|
1140
|
+
+ askStill()
|
|
1141
|
+
+ }
|
|
1142
|
+
+ }
|
|
1143
|
+
+
|
|
1144
|
+
+ // The page again, and the still let go: a lens holds a still only while it shows it (a menu's large region, a
|
|
1145
|
+
+ // region that changes now and then, would otherwise keep buffers it does not draw).
|
|
1146
|
+
+ private fun showPage(now: Long) {
|
|
1147
|
+
+ quietSince = now
|
|
1148
|
+
+ generation += 1
|
|
1149
|
+
+ still?.release()
|
|
1150
|
+
+ still = null
|
|
1151
|
+
+ if (!showsStill && recorded && node.left == region.left && node.top == region.top &&
|
|
1152
|
+
+ node.width == region.width() && node.height == region.height()) return
|
|
1153
|
+
+ node.setPosition(region)
|
|
1154
|
+
+ val canvas = node.beginRecording(region.width(), region.height())
|
|
1155
|
+
+ canvas.translate(-region.left.toFloat(), -region.top.toFloat())
|
|
1156
|
+
+ canvas.drawRenderNode(page.node)
|
|
1157
|
+
+ node.endRecording()
|
|
1158
|
+
+ showsStill = false
|
|
1159
|
+
+ recorded = true
|
|
1160
|
+
+ }
|
|
1161
|
+
+
|
|
1162
|
+
+ private fun askStill() {
|
|
1163
|
+
+ val target = GlassStill(region.width(), region.height(), ::showStill).also { still = it }
|
|
1164
|
+
+ askedGeneration = generation
|
|
1165
|
+
+ val left = region.left.toFloat()
|
|
1166
|
+
+ val top = region.top.toFloat()
|
|
1167
|
+
+ target.render { canvas ->
|
|
1168
|
+
+ canvas.translate(-left, -top)
|
|
1169
|
+
+ canvas.drawRenderNode(page.node)
|
|
1170
|
+
+ }
|
|
1171
|
+
+ }
|
|
1172
|
+
+
|
|
1173
|
+
+ private fun showStill(bitmap: Bitmap) {
|
|
1174
|
+
+ if (askedGeneration != generation || bitmap.width != region.width() || bitmap.height != region.height()) return
|
|
1175
|
+
+ node.setPosition(region)
|
|
1176
|
+
+ val canvas = node.beginRecording(region.width(), region.height())
|
|
1177
|
+
+ canvas.drawBitmap(bitmap, 0f, 0f, null)
|
|
1178
|
+
+ node.endRecording()
|
|
1179
|
+
+ showsStill = true
|
|
1180
|
+
+ }
|
|
1181
|
+
+
|
|
1182
|
+
+ fun release() {
|
|
1183
|
+
+ page.drop(this)
|
|
1184
|
+
+ still?.release()
|
|
1185
|
+
+ still = null
|
|
1186
|
+
+ node.discardDisplayList()
|
|
1187
|
+
+ recorded = false
|
|
1188
|
+
+ showsStill = false
|
|
1189
|
+
+ }
|
|
1190
|
+
+
|
|
1191
|
+
+ private companion object {
|
|
1192
|
+
+ // Longer than the gap between two frames of a video and than a press: a region that changes keeps the page.
|
|
1193
|
+
+ const val StillAfterMs = 500L
|
|
1194
|
+
+ }
|
|
1195
|
+
+}
|
|
1196
|
+
--- a/chrome/GlassChromeView.kt
|
|
1197
|
+
+++ b/chrome/GlassChromeView.kt
|
|
1198
|
+
@@ -273,12 +273,12 @@
|
|
1199
|
+
) {
|
|
1200
|
+
val barState = glassState(tabs, search, ChromeGeometry.layerFor("bar", rest), ox, oy)
|
|
1201
|
+
barRenderer.prepare(barState)
|
|
1202
|
+
- barRenderer.drawGlass(canvas, provider.node, barState, limits)
|
|
1203
|
+
+ barRenderer.drawGlass(canvas, provider, barState, limits)
|
|
1204
|
+
// The create circle shrinks to nothing as the search opens: skip it when it has no area left.
|
|
1205
|
+
if (fab.x1 - fab.x0 >= 1f) {
|
|
1206
|
+
val fabState = glassState(fab, null, ChromeGeometry.layerFor("fab", rest), ox, oy)
|
|
1207
|
+
fabRenderer.prepare(fabState)
|
|
1208
|
+
- fabRenderer.drawGlass(canvas, provider.node, fabState, limits)
|
|
1209
|
+
+ fabRenderer.drawGlass(canvas, provider, fabState, limits)
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
--- a/chrome/SurfaceNodesView.kt
|
|
1214
|
+
+++ b/chrome/SurfaceNodesView.kt
|
|
1215
|
+
@@ -265,7 +265,7 @@
|
|
1216
|
+
if (rect.width() < 1f || rect.height() < 1f) continue
|
|
1217
|
+
val state = glassStateFor(track, rect, ox, oy)
|
|
1218
|
+
track.renderer.prepare(state)
|
|
1219
|
+
- track.renderer.drawGlass(canvas, provider.node, state, limits)
|
|
1220
|
+
+ track.renderer.drawGlass(canvas, provider, state, limits)
|
|
1221
|
+
}
|
|
1222
|
+
canvas.restore()
|
|
1223
|
+
}
|
|
1224
|
+
--- a/overlay/GlassSubmenuLayer.kt
|
|
1225
|
+
+++ b/overlay/GlassSubmenuLayer.kt
|
|
1226
|
+
@@ -4,6 +4,7 @@
|
|
1227
|
+
import android.graphics.Rect
|
|
1228
|
+
import android.graphics.RectF
|
|
1229
|
+
import android.graphics.RenderNode
|
|
1230
|
+
+import so.oxy.bloom.glassmenu.backdrop.GlassBackdrop
|
|
1231
|
+
import so.oxy.bloom.glassmenu.GlassMenuAndroidView
|
|
1232
|
+
import so.oxy.bloom.glassmenu.content.GlassMenuContent
|
|
1233
|
+
import so.oxy.bloom.glassmenu.content.GlassRow
|
|
1234
|
+
@@ -116,7 +117,7 @@
|
|
1235
|
+
return closing && progress.value <= CloseDone
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
- fun draw(canvas: Canvas, backdrop: RenderNode, limits: Rect) {
|
|
1239
|
+
+ fun draw(canvas: Canvas, backdrop: GlassBackdrop, limits: Rect) {
|
|
1240
|
+
val p = progress.value.coerceIn(0f, 1f)
|
|
1241
|
+
lerp(row, target, p, shown)
|
|
1242
|
+
val rowRadius = min(row.width(), row.height()) / 2f
|
|
1243
|
+
--- a/render/GlassStill.kt
|
|
1244
|
+
+++ b/render/GlassStill.kt
|
|
1245
|
+
@@ -0,0 +1,115 @@
|
|
1246
|
+
+package so.oxy.bloom.glassmenu.render
|
|
1247
|
+
+
|
|
1248
|
+
+import android.graphics.Bitmap
|
|
1249
|
+
+import android.graphics.ColorSpace
|
|
1250
|
+
+import android.graphics.HardwareRenderer
|
|
1251
|
+
+import android.graphics.PixelFormat
|
|
1252
|
+
+import android.graphics.RecordingCanvas
|
|
1253
|
+
+import android.graphics.RenderNode
|
|
1254
|
+
+import android.hardware.HardwareBuffer
|
|
1255
|
+
+import android.media.Image
|
|
1256
|
+
+import android.media.ImageReader
|
|
1257
|
+
+import android.os.Handler
|
|
1258
|
+
+import android.os.Looper
|
|
1259
|
+
+
|
|
1260
|
+
+// Glass drawn once into a hardware buffer, by a renderer of its own, and drawn from then on as a bitmap: no layer is
|
|
1261
|
+
+// kept for it in the window, and nothing in it is rendered again while what it shows stays the same. The same pixels
|
|
1262
|
+
+// as a live drawing: the same nodes, on the same RenderThread, at whole pixels. The bitmap arrives on the main thread,
|
|
1263
|
+
+// after the render, in [ready]; until then the caller draws live.
|
|
1264
|
+
+internal class GlassStill(val width: Int, val height: Int, private val ready: (Bitmap) -> Unit) :
|
|
1265
|
+
+ ImageReader.OnImageAvailableListener {
|
|
1266
|
+
+ private val reader = ImageReader.newInstance(
|
|
1267
|
+
+ width, height, PixelFormat.RGBA_8888, 2,
|
|
1268
|
+
+ HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or HardwareBuffer.USAGE_GPU_COLOR_OUTPUT,
|
|
1269
|
+
+ )
|
|
1270
|
+
+ private val root = RenderNode("GlassStill")
|
|
1271
|
+
+ private val renderer = HardwareRenderer()
|
|
1272
|
+
+ private var image: Image? = null
|
|
1273
|
+
+ private var bitmap: Bitmap? = null
|
|
1274
|
+
+
|
|
1275
|
+
+ init {
|
|
1276
|
+
+ reader.setOnImageAvailableListener(this, Handler(Looper.getMainLooper()))
|
|
1277
|
+
+ root.setPosition(0, 0, width, height)
|
|
1278
|
+
+ renderer.setSurface(reader.surface)
|
|
1279
|
+
+ renderer.setContentRoot(root)
|
|
1280
|
+
+ renderer.isOpaque = false
|
|
1281
|
+
+ }
|
|
1282
|
+
+
|
|
1283
|
+
+ // Renders what [record] draws, in the still's pixels.
|
|
1284
|
+
+ fun render(record: (RecordingCanvas) -> Unit) {
|
|
1285
|
+
+ val canvas = root.beginRecording(width, height)
|
|
1286
|
+
+ try {
|
|
1287
|
+
+ record(canvas)
|
|
1288
|
+
+ } finally {
|
|
1289
|
+
+ root.endRecording()
|
|
1290
|
+
+ }
|
|
1291
|
+
+ renderer.createRenderRequest().syncAndDraw()
|
|
1292
|
+
+ }
|
|
1293
|
+
+
|
|
1294
|
+
+ override fun onImageAvailable(source: ImageReader) {
|
|
1295
|
+
+ val next = try { source.acquireNextImage() } catch (_: IllegalStateException) { null } ?: return
|
|
1296
|
+
+ val buffer = next.hardwareBuffer
|
|
1297
|
+
+ val bitmap = buffer?.let { Bitmap.wrapHardwareBuffer(it, ColorSpace.get(ColorSpace.Named.SRGB)) }
|
|
1298
|
+
+ buffer?.close()
|
|
1299
|
+
+ if (bitmap == null) {
|
|
1300
|
+
+ next.close()
|
|
1301
|
+
+ return
|
|
1302
|
+
+ }
|
|
1303
|
+
+ // The previous image is closed only now: its bitmap is not drawn once the next one is handed over.
|
|
1304
|
+
+ this.bitmap?.recycle()
|
|
1305
|
+
+ image?.close()
|
|
1306
|
+
+ image = next
|
|
1307
|
+
+ this.bitmap = bitmap
|
|
1308
|
+
+ ready(bitmap)
|
|
1309
|
+
+ }
|
|
1310
|
+
+
|
|
1311
|
+
+ // Released once nothing records the bitmap any more: its buffer is freed now, not when the bitmap is collected (a
|
|
1312
|
+
+ // frame already recorded holds its own image of it).
|
|
1313
|
+
+ fun release() {
|
|
1314
|
+
+ renderer.destroy()
|
|
1315
|
+
+ bitmap?.recycle()
|
|
1316
|
+
+ bitmap = null
|
|
1317
|
+
+ reader.close()
|
|
1318
|
+
+ image = null
|
|
1319
|
+
+ root.discardDisplayList()
|
|
1320
|
+
+ }
|
|
1321
|
+
+}
|
|
1322
|
+
+
|
|
1323
|
+
+// Stills shared by what they show: every trigger that rests with the same look (a feed's ⋯ on every row) draws the one
|
|
1324
|
+
+// bitmap of that look, so the glass at rest costs one texture per look, not a layer per trigger.
|
|
1325
|
+
+internal object SharedStills {
|
|
1326
|
+
+ private class Entry(width: Int, height: Int, record: (RecordingCanvas) -> Unit) {
|
|
1327
|
+
+ var bitmap: Bitmap? = null
|
|
1328
|
+
+ var users = 0
|
|
1329
|
+
+ val waiting = ArrayList<java.lang.ref.WeakReference<android.view.View>>()
|
|
1330
|
+
+ val still = GlassStill(width, height) { arrived ->
|
|
1331
|
+
+ bitmap = arrived
|
|
1332
|
+
+ for (view in waiting) view.get()?.invalidate()
|
|
1333
|
+
+ waiting.clear()
|
|
1334
|
+
+ }.also { it.render(record) }
|
|
1335
|
+
+ }
|
|
1336
|
+
+
|
|
1337
|
+
+ private val entries = HashMap<Any, Entry>()
|
|
1338
|
+
+
|
|
1339
|
+
+ // One more user of [look]; the first one renders it with [record], at [width] x [height] px.
|
|
1340
|
+
+ fun acquire(look: Any, width: Int, height: Int, record: (RecordingCanvas) -> Unit) {
|
|
1341
|
+
+ entries.getOrPut(look) { Entry(width, height, record) }.users += 1
|
|
1342
|
+
+ }
|
|
1343
|
+
+
|
|
1344
|
+
+ fun release(look: Any) {
|
|
1345
|
+
+ val entry = entries[look] ?: return
|
|
1346
|
+
+ entry.users -= 1
|
|
1347
|
+
+ if (entry.users <= 0) {
|
|
1348
|
+
+ entries.remove(look)
|
|
1349
|
+
+ entry.still.release()
|
|
1350
|
+
+ }
|
|
1351
|
+
+ }
|
|
1352
|
+
+
|
|
1353
|
+
+ // The bitmap of [look], or null until it has arrived (then [view] is invalidated).
|
|
1354
|
+
+ fun bitmap(look: Any, view: android.view.View): Bitmap? {
|
|
1355
|
+
+ val entry = entries[look] ?: return null
|
|
1356
|
+
+ entry.bitmap?.let { return it }
|
|
1357
|
+
+ entry.waiting.add(java.lang.ref.WeakReference(view))
|
|
1358
|
+
+ return null
|
|
1359
|
+
+ }
|
|
1360
|
+
+}
|
|
1361
|
+
--- a/field/GlassGaussianField.kt
|
|
1362
|
+
+++ b/field/GlassGaussianField.kt
|
|
1363
|
+
@@ -1,7 +1,6 @@
|
|
1364
|
+
package so.oxy.bloom.glassmenu.field
|
|
1365
|
+
|
|
1366
|
+
import android.graphics.RectF
|
|
1367
|
+
-import kotlin.math.abs
|
|
1368
|
+
import kotlin.math.ceil
|
|
1369
|
+
import kotlin.math.exp
|
|
1370
|
+
import kotlin.math.floor
|
|
1371
|
+
@@ -28,26 +27,33 @@
|
|
1372
|
+
private var raw = FloatArray(0)
|
|
1373
|
+
private var horizontal = FloatArray(0)
|
|
1374
|
+
private val weights = FloatArray(17)
|
|
1375
|
+
- private val incoming = RoundedField()
|
|
1376
|
+
- private val outgoing = RoundedField()
|
|
1377
|
+
+ private val incoming = RoundedUnion()
|
|
1378
|
+
+ private val outgoing = RoundedUnion()
|
|
1379
|
+
private var hasOutgoing = false
|
|
1380
|
+
private var blend = 0f
|
|
1381
|
+
|
|
1382
|
+
+ // [members], when it has shapes, is the trigger drawn as several (a group of glasses): they take the trigger's
|
|
1383
|
+
+ // place, the outgoing shape in a morph, else the incoming one.
|
|
1384
|
+
fun update(
|
|
1385
|
+
incomingRect: RectF, radiusX: Float, radiusY: Float,
|
|
1386
|
+
outgoingRect: RectF?, outgoingRadius: Float, smoothing: Float, lockedStep: Float = 0f,
|
|
1387
|
+
+ members: GlassShapes? = null,
|
|
1388
|
+
): Boolean {
|
|
1389
|
+
val sigma = max(smoothing, 0f)
|
|
1390
|
+
if (sigma <= 0.000001f) return false
|
|
1391
|
+
- var left = incomingRect.left
|
|
1392
|
+
- var top = incomingRect.top
|
|
1393
|
+
- var right = incomingRect.right
|
|
1394
|
+
- var bottom = incomingRect.bottom
|
|
1395
|
+
- if (outgoingRect != null) {
|
|
1396
|
+
- left = min(left, outgoingRect.left)
|
|
1397
|
+
- top = min(top, outgoingRect.top)
|
|
1398
|
+
- right = max(right, outgoingRect.right)
|
|
1399
|
+
- bottom = max(bottom, outgoingRect.bottom)
|
|
1400
|
+
+ incoming.set(incomingRect, radiusX, radiusY)
|
|
1401
|
+
+ hasOutgoing = outgoingRect != null
|
|
1402
|
+
+ if (outgoingRect != null) outgoing.set(outgoingRect, outgoingRadius, outgoingRadius)
|
|
1403
|
+
+ if (members != null && members.count > 0) (if (hasOutgoing) outgoing else incoming).set(members)
|
|
1404
|
+
+ var left = incoming.bounds.left
|
|
1405
|
+
+ var top = incoming.bounds.top
|
|
1406
|
+
+ var right = incoming.bounds.right
|
|
1407
|
+
+ var bottom = incoming.bounds.bottom
|
|
1408
|
+
+ if (hasOutgoing) {
|
|
1409
|
+
+ left = min(left, outgoing.bounds.left)
|
|
1410
|
+
+ top = min(top, outgoing.bounds.top)
|
|
1411
|
+
+ right = max(right, outgoing.bounds.right)
|
|
1412
|
+
+ bottom = max(bottom, outgoing.bounds.bottom)
|
|
1413
|
+
}
|
|
1414
|
+
val grow = if (outgoingRect != null) sigma * 0.25f * max(radiusX, radiusY) / max(min(radiusX, radiusY), 0.01f) else 0f
|
|
1415
|
+
left -= grow
|
|
1416
|
+
@@ -75,9 +81,6 @@
|
|
1417
|
+
for (index in 1..radius) sum += weights[index] * 2f
|
|
1418
|
+
for (index in 0..radius) weights[index] /= sum
|
|
1419
|
+
|
|
1420
|
+
- incoming.set(incomingRect, radiusX, radiusY)
|
|
1421
|
+
- hasOutgoing = outgoingRect != null
|
|
1422
|
+
- if (outgoingRect != null) outgoing.set(outgoingRect, outgoingRadius, outgoingRadius)
|
|
1423
|
+
blend = sigma
|
|
1424
|
+
originX = floor(left / step) * step - step
|
|
1425
|
+
originY = floor(top / step) * step - step
|
|
1426
|
+
@@ -146,36 +149,13 @@
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
private fun sample(x: Float, y: Float, index: Int) {
|
|
1430
|
+
- val a = incoming.distance(x, y)
|
|
1431
|
+
- incoming.gradient(x, y)
|
|
1432
|
+
- val ax = incoming.gradientX
|
|
1433
|
+
- val ay = incoming.gradientY
|
|
1434
|
+
- if (!hasOutgoing) {
|
|
1435
|
+
- raw[index] = a
|
|
1436
|
+
- raw[index + 1] = ax
|
|
1437
|
+
- raw[index + 2] = ay
|
|
1438
|
+
- return
|
|
1439
|
+
- }
|
|
1440
|
+
- val b = outgoing.distance(x, y)
|
|
1441
|
+
- outgoing.gradient(x, y)
|
|
1442
|
+
- val bx = outgoing.gradientX
|
|
1443
|
+
- val by = outgoing.gradientY
|
|
1444
|
+
- if (abs(a - b) >= blend) {
|
|
1445
|
+
- val first = a <= b
|
|
1446
|
+
- raw[index] = min(a, b)
|
|
1447
|
+
- raw[index + 1] = if (first) ax else bx
|
|
1448
|
+
- raw[index + 2] = if (first) ay else by
|
|
1449
|
+
- return
|
|
1450
|
+
- }
|
|
1451
|
+
- val alignment = ax * bx + ay * by
|
|
1452
|
+
- val radius = blend * (0.5f - 0.5f * alignment).coerceIn(0f, 1f)
|
|
1453
|
+
- val weight = if (radius <= 0.000001f) {
|
|
1454
|
+
- if (a <= b) 0f else 1f
|
|
1455
|
+
- } else {
|
|
1456
|
+
- (0.5f + 0.5f * (a - b) / radius).coerceIn(0f, 1f)
|
|
1457
|
+
- }
|
|
1458
|
+
- raw[index] = a + (b - a) * weight - radius * weight * (1f - weight)
|
|
1459
|
+
- raw[index + 1] = ax + (bx - ax) * weight
|
|
1460
|
+
- raw[index + 2] = ay + (by - ay) * weight
|
|
1461
|
+
+ incoming.sample(x, y)
|
|
1462
|
+
+ if (hasOutgoing) {
|
|
1463
|
+
+ outgoing.sample(x, y)
|
|
1464
|
+
+ incoming.blendWith(outgoing.distance, outgoing.gradientX, outgoing.gradientY, blend)
|
|
1465
|
+
+ }
|
|
1466
|
+
+ raw[index] = incoming.distance
|
|
1467
|
+
+ raw[index + 1] = incoming.gradientX
|
|
1468
|
+
+ raw[index + 2] = incoming.gradientY
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
--- a/field/GlassShapes.kt
|
|
1472
|
+
+++ b/field/GlassShapes.kt
|
|
1473
|
+
@@ -0,0 +1,37 @@
|
|
1474
|
+
+package so.oxy.bloom.glassmenu.field
|
|
1475
|
+
+
|
|
1476
|
+
+import android.graphics.RectF
|
|
1477
|
+
+import kotlin.math.min
|
|
1478
|
+
+
|
|
1479
|
+
+// A glass that is several shapes (a group of glasses on one base): each shape's rect and corner radius, in the
|
|
1480
|
+
+// surface's pixels, blended into one another within [blend] px (a smooth union: shapes closer than it flow together,
|
|
1481
|
+
+// and 0 joins them only where they overlap). Empty, the glass is its one rect, as every glass but a group's is.
|
|
1482
|
+
+internal class GlassShapes {
|
|
1483
|
+
+ val rects = Array(Capacity) { RectF() }
|
|
1484
|
+
+ val radii = FloatArray(Capacity)
|
|
1485
|
+
+ var count = 0
|
|
1486
|
+
+ private set
|
|
1487
|
+
+ var blend = 0f
|
|
1488
|
+
+
|
|
1489
|
+
+ fun clear() {
|
|
1490
|
+
+ count = 0
|
|
1491
|
+
+ }
|
|
1492
|
+
+
|
|
1493
|
+
+ // A shape; its radius is capped at a capsule's. Shapes past Capacity are not drawn.
|
|
1494
|
+
+ fun add(rect: RectF, radius: Float) {
|
|
1495
|
+
+ if (count == Capacity) return
|
|
1496
|
+
+ rects[count].set(rect)
|
|
1497
|
+
+ radii[count] = min(radius, min(rect.width(), rect.height()) / 2f)
|
|
1498
|
+
+ count += 1
|
|
1499
|
+
+ }
|
|
1500
|
+
+
|
|
1501
|
+
+ fun bounds(out: RectF) {
|
|
1502
|
+
+ out.setEmpty()
|
|
1503
|
+
+ for (index in 0 until count) out.union(rects[index])
|
|
1504
|
+
+ }
|
|
1505
|
+
+
|
|
1506
|
+
+ companion object {
|
|
1507
|
+
+ // The shader's: its first shape and seven more (GlassShaders, MemberCapacity).
|
|
1508
|
+
+ const val Capacity = 8
|
|
1509
|
+
+ }
|
|
1510
|
+
+}
|
|
1511
|
+
--- a/field/RoundedUnion.kt
|
|
1512
|
+
+++ b/field/RoundedUnion.kt
|
|
1513
|
+
@@ -0,0 +1,81 @@
|
|
1514
|
+
+package so.oxy.bloom.glassmenu.field
|
|
1515
|
+
+
|
|
1516
|
+
+import android.graphics.RectF
|
|
1517
|
+
+import kotlin.math.abs
|
|
1518
|
+
+import kotlin.math.min
|
|
1519
|
+
+import kotlin.math.sqrt
|
|
1520
|
+
+
|
|
1521
|
+
+// One shape of the field (a rounded rect) or several blended into one (GlassShapes): the distance and gradient at a
|
|
1522
|
+
+// point, sampled into [distance], [gradientX] and [gradientY].
|
|
1523
|
+
+internal class RoundedUnion {
|
|
1524
|
+
+ private val shapes = Array(GlassShapes.Capacity) { RoundedField() }
|
|
1525
|
+
+ private var count = 0
|
|
1526
|
+
+ private var blend = 0f
|
|
1527
|
+
+ val bounds = RectF()
|
|
1528
|
+
+ var distance = 0f
|
|
1529
|
+
+ private set
|
|
1530
|
+
+ var gradientX = 0f
|
|
1531
|
+
+ private set
|
|
1532
|
+
+ var gradientY = 0f
|
|
1533
|
+
+ private set
|
|
1534
|
+
+
|
|
1535
|
+
+ fun set(rect: RectF, radiusX: Float, radiusY: Float) {
|
|
1536
|
+
+ shapes[0].set(rect, radiusX, radiusY)
|
|
1537
|
+
+ count = 1
|
|
1538
|
+
+ bounds.set(rect)
|
|
1539
|
+
+ }
|
|
1540
|
+
+
|
|
1541
|
+
+ fun set(members: GlassShapes) {
|
|
1542
|
+
+ count = members.count
|
|
1543
|
+
+ blend = members.blend
|
|
1544
|
+
+ for (index in 0 until count) shapes[index].set(members.rects[index], members.radii[index], members.radii[index])
|
|
1545
|
+
+ members.bounds(bounds)
|
|
1546
|
+
+ }
|
|
1547
|
+
+
|
|
1548
|
+
+ fun sample(x: Float, y: Float) {
|
|
1549
|
+
+ val first = shapes[0]
|
|
1550
|
+
+ distance = first.distance(x, y)
|
|
1551
|
+
+ first.gradient(x, y)
|
|
1552
|
+
+ gradientX = first.gradientX
|
|
1553
|
+
+ gradientY = first.gradientY
|
|
1554
|
+
+ for (index in 1 until count) {
|
|
1555
|
+
+ val shape = shapes[index]
|
|
1556
|
+
+ val d = shape.distance(x, y)
|
|
1557
|
+
+ shape.gradient(x, y)
|
|
1558
|
+
+ blendWith(d, shape.gradientX, shape.gradientY, blend)
|
|
1559
|
+
+ }
|
|
1560
|
+
+ // Several shapes: a unit normal where they meet, as the shader's (liquidMemberField).
|
|
1561
|
+
+ if (count > 1) {
|
|
1562
|
+
+ val length = sqrt(gradientX * gradientX + gradientY * gradientY)
|
|
1563
|
+
+ if (length > 0.000001f) {
|
|
1564
|
+
+ gradientX /= length
|
|
1565
|
+
+ gradientY /= length
|
|
1566
|
+
+ }
|
|
1567
|
+
+ }
|
|
1568
|
+
+ }
|
|
1569
|
+
+
|
|
1570
|
+
+ // The smooth union of this sample with another shape's, within [smoothing] px: the closer two surfaces face each
|
|
1571
|
+
+ // other, the more they flow together (none where their normals agree).
|
|
1572
|
+
+ fun blendWith(b: Float, bx: Float, by: Float, smoothing: Float) {
|
|
1573
|
+
+ val a = distance
|
|
1574
|
+
+ val ax = gradientX
|
|
1575
|
+
+ val ay = gradientY
|
|
1576
|
+
+ if (abs(a - b) >= smoothing) {
|
|
1577
|
+
+ val first = a <= b
|
|
1578
|
+
+ distance = min(a, b)
|
|
1579
|
+
+ gradientX = if (first) ax else bx
|
|
1580
|
+
+ gradientY = if (first) ay else by
|
|
1581
|
+
+ return
|
|
1582
|
+
+ }
|
|
1583
|
+
+ val alignment = ax * bx + ay * by
|
|
1584
|
+
+ val radius = smoothing * (0.5f - 0.5f * alignment).coerceIn(0f, 1f)
|
|
1585
|
+
+ val weight = if (radius <= 0.000001f) {
|
|
1586
|
+
+ if (a <= b) 0f else 1f
|
|
1587
|
+
+ } else {
|
|
1588
|
+
+ (0.5f + 0.5f * (a - b) / radius).coerceIn(0f, 1f)
|
|
1589
|
+
+ }
|
|
1590
|
+
+ distance = a + (b - a) * weight - radius * weight * (1f - weight)
|
|
1591
|
+
+ gradientX = ax + (bx - ax) * weight
|
|
1592
|
+
+ gradientY = ay + (by - ay) * weight
|
|
1593
|
+
+ }
|
|
1594
|
+
+}
|
|
1595
|
+
--- a/motion/GlassGroup.kt
|
|
1596
|
+
+++ b/motion/GlassGroup.kt
|
|
1597
|
+
@@ -0,0 +1,225 @@
|
|
1598
|
+
+package so.oxy.bloom.glassmenu.motion
|
|
1599
|
+
+
|
|
1600
|
+
+import android.graphics.RectF
|
|
1601
|
+
+import android.view.View
|
|
1602
|
+
+import android.view.ViewGroup
|
|
1603
|
+
+import com.facebook.react.R
|
|
1604
|
+
+import so.oxy.bloom.glassmenu.field.GlassShapes
|
|
1605
|
+
+import so.oxy.bloom.glassmenu.records.GlassMemberRecord
|
|
1606
|
+
+import kotlin.math.max
|
|
1607
|
+
+import kotlin.math.min
|
|
1608
|
+
+
|
|
1609
|
+
+// The glasses of a group on one base (SwiftUI's GlassEffectContainer), in the base's own pixels. A member is the views
|
|
1610
|
+
+// in the base that carry its key as their nativeID: one view, or several drawn as one shape around them all (a union).
|
|
1611
|
+
+// Each member's glass is where its views are drawn, transforms included, so what moves a view (a matched geometry, an
|
|
1612
|
+
+// animation) moves its glass with it. What a view cannot do the group does here, on a spring: a key that moves to
|
|
1613
|
+
+// another view takes its glass there, a member that arrives grows out of the nearest glass and one that leaves goes
|
|
1614
|
+
+// back into the nearest one ('materialize': out of and into its own centre). The first members of a group are where
|
|
1615
|
+
+// they are, with no motion.
|
|
1616
|
+
+internal class GlassGroup {
|
|
1617
|
+
+ private class Member(val key: String) {
|
|
1618
|
+
+ var radius: Float? = null
|
|
1619
|
+
+ var materialize = false
|
|
1620
|
+
+ var leaving = false
|
|
1621
|
+
+ // The view its glass is on (the first, for a union): another one takes the glass there on a spring.
|
|
1622
|
+
+ var view: View? = null
|
|
1623
|
+
+ val target = RectF()
|
|
1624
|
+
+ val centerX = GlassSpring(.5f, 8f)
|
|
1625
|
+
+ val centerY = GlassSpring(.5f, 8f)
|
|
1626
|
+
+ val width = GlassSpring(.5f, 8f)
|
|
1627
|
+
+ val height = GlassSpring(.5f, 8f)
|
|
1628
|
+
+ val isSettled get() = centerX.isSettled && centerY.isSettled && width.isSettled && height.isSettled
|
|
1629
|
+
+
|
|
1630
|
+
+ fun reset(rect: RectF) {
|
|
1631
|
+
+ centerX.reset(rect.centerX())
|
|
1632
|
+
+ centerY.reset(rect.centerY())
|
|
1633
|
+
+ width.reset(rect.width())
|
|
1634
|
+
+ height.reset(rect.height())
|
|
1635
|
+
+ }
|
|
1636
|
+
+
|
|
1637
|
+
+ fun spring(rect: RectF) {
|
|
1638
|
+
+ centerX.target(rect.centerX(), Spring)
|
|
1639
|
+
+ centerY.target(rect.centerY(), Spring)
|
|
1640
|
+
+ width.target(rect.width(), Spring)
|
|
1641
|
+
+ height.target(rect.height(), Spring)
|
|
1642
|
+
+ }
|
|
1643
|
+
+
|
|
1644
|
+
+ fun advance(seconds: Float) {
|
|
1645
|
+
+ centerX.advance(seconds)
|
|
1646
|
+
+ centerY.advance(seconds)
|
|
1647
|
+
+ width.advance(seconds)
|
|
1648
|
+
+ height.advance(seconds)
|
|
1649
|
+
+ }
|
|
1650
|
+
+
|
|
1651
|
+
+ fun current(out: RectF) {
|
|
1652
|
+
+ val w = max(width.value, 0f)
|
|
1653
|
+
+ val h = max(height.value, 0f)
|
|
1654
|
+
+ out.set(centerX.value - w / 2f, centerY.value - h / 2f, centerX.value + w / 2f, centerY.value + h / 2f)
|
|
1655
|
+
+ }
|
|
1656
|
+
+ }
|
|
1657
|
+
+
|
|
1658
|
+
+ // Null: the base is not a group, and is its own rect.
|
|
1659
|
+
+ private var records: List<GlassMemberRecord>? = null
|
|
1660
|
+
+ private val members = LinkedHashMap<String, Member>()
|
|
1661
|
+
+ private val found = HashMap<String, RectF>()
|
|
1662
|
+
+ private val owners = HashMap<String, View>()
|
|
1663
|
+
+ // The rects [found] holds, kept from frame to frame (measuring runs before every frame the window draws).
|
|
1664
|
+
+ private val pool = ArrayList<RectF>()
|
|
1665
|
+
+ private var pooled = 0
|
|
1666
|
+
+ private val measured = RectF()
|
|
1667
|
+
+ private val placed = RectF()
|
|
1668
|
+
+ var blend = 0f
|
|
1669
|
+
+
|
|
1670
|
+
+ val isGroup get() = records != null
|
|
1671
|
+
+ val isSettled get() = members.values.all { it.isSettled && !it.leaving }
|
|
1672
|
+
+
|
|
1673
|
+
+ fun setRecords(value: List<GlassMemberRecord>?) {
|
|
1674
|
+
+ records = value
|
|
1675
|
+
+ if (value == null) members.clear()
|
|
1676
|
+
+ }
|
|
1677
|
+
+
|
|
1678
|
+
+ // Where the members' views are in [base] now. True when a member's glass changed (the caller draws it again).
|
|
1679
|
+
+ fun measure(base: ViewGroup): Boolean {
|
|
1680
|
+
+ val list = records ?: return false
|
|
1681
|
+
+ found.clear()
|
|
1682
|
+
+ owners.clear()
|
|
1683
|
+
+ pooled = 0
|
|
1684
|
+
+ collect(base, base)
|
|
1685
|
+
+ val drawn = members.values.any { !it.leaving }
|
|
1686
|
+
+ var changed = false
|
|
1687
|
+
+ for (record in list) {
|
|
1688
|
+
+ val rect = found[record.key] ?: continue
|
|
1689
|
+
+ val owner = owners[record.key]
|
|
1690
|
+
+ val member = members[record.key]
|
|
1691
|
+
+ if (member == null) {
|
|
1692
|
+
+ val arriving = Member(record.key).also { members[record.key] = it }
|
|
1693
|
+
+ arriving.radius = record.cornerRadius
|
|
1694
|
+
+ arriving.materialize = record.transition == Materialize
|
|
1695
|
+
+ arriving.view = owner
|
|
1696
|
+
+ arriving.target.set(rect)
|
|
1697
|
+
+ if (drawn) {
|
|
1698
|
+
+ arriving.reset(origin(arriving, rect))
|
|
1699
|
+
+ arriving.spring(rect)
|
|
1700
|
+
+ } else {
|
|
1701
|
+
+ arriving.reset(rect)
|
|
1702
|
+
+ }
|
|
1703
|
+
+ changed = true
|
|
1704
|
+
+ continue
|
|
1705
|
+
+ }
|
|
1706
|
+
+ member.radius = record.cornerRadius
|
|
1707
|
+
+ member.materialize = record.transition == Materialize
|
|
1708
|
+
+ if (member.leaving) {
|
|
1709
|
+
+ member.leaving = false
|
|
1710
|
+
+ member.target.set(rect)
|
|
1711
|
+
+ member.spring(rect)
|
|
1712
|
+
+ changed = true
|
|
1713
|
+
+ } else if (member.target != rect || member.view !== owner) {
|
|
1714
|
+
+ // Its own view moved: settled, the glass follows it. Moving still, or on another view now (the key moved
|
|
1715
|
+
+ // there), it springs to where the view is.
|
|
1716
|
+
+ val moved = member.view !== owner
|
|
1717
|
+
+ member.view = owner
|
|
1718
|
+
+ member.target.set(rect)
|
|
1719
|
+
+ if (member.isSettled && !moved) member.reset(rect) else member.spring(rect)
|
|
1720
|
+
+ changed = true
|
|
1721
|
+
+ }
|
|
1722
|
+
+ }
|
|
1723
|
+
+ for (member in members.values) {
|
|
1724
|
+
+ if (member.leaving || list.any { it.key == member.key && found.containsKey(it.key) }) continue
|
|
1725
|
+
+ member.leaving = true
|
|
1726
|
+
+ member.view = null
|
|
1727
|
+
+ member.spring(origin(member, member.target))
|
|
1728
|
+
+ changed = true
|
|
1729
|
+
+ }
|
|
1730
|
+
+ return changed
|
|
1731
|
+
+ }
|
|
1732
|
+
+
|
|
1733
|
+
+ // Where a member grows out of or goes back into: the nearest other glass, or its own centre.
|
|
1734
|
+
+ private fun origin(member: Member, rect: RectF): RectF {
|
|
1735
|
+
+ if (!member.materialize) {
|
|
1736
|
+
+ var nearest: Member? = null
|
|
1737
|
+
+ var best = Float.MAX_VALUE
|
|
1738
|
+
+ for (other in members.values) {
|
|
1739
|
+
+ if (other === member || other.leaving || !found.containsKey(other.key)) continue
|
|
1740
|
+
+ val dx = other.target.centerX() - rect.centerX()
|
|
1741
|
+
+ val dy = other.target.centerY() - rect.centerY()
|
|
1742
|
+
+ val distance = dx * dx + dy * dy
|
|
1743
|
+
+ if (distance < best) {
|
|
1744
|
+
+ best = distance
|
|
1745
|
+
+ nearest = other
|
|
1746
|
+
+ }
|
|
1747
|
+
+ }
|
|
1748
|
+
+ if (nearest != null) {
|
|
1749
|
+
+ nearest.current(placed)
|
|
1750
|
+
+ return placed
|
|
1751
|
+
+ }
|
|
1752
|
+
+ }
|
|
1753
|
+
+ placed.set(rect.centerX(), rect.centerY(), rect.centerX(), rect.centerY())
|
|
1754
|
+
+ return placed
|
|
1755
|
+
+ }
|
|
1756
|
+
+
|
|
1757
|
+
+ // The views in [view] that carry a member's key, each shape the union of its views' rects in [base].
|
|
1758
|
+
+ private fun collect(view: View, base: ViewGroup) {
|
|
1759
|
+
+ if (view !== base) {
|
|
1760
|
+
+ val key = view.getTag(R.id.view_tag_native_id) as? String
|
|
1761
|
+
+ if (key != null && view.visibility == View.VISIBLE && measure(view, base)) {
|
|
1762
|
+
+ val shape = found[key]
|
|
1763
|
+
+ if (shape != null) {
|
|
1764
|
+
+ shape.union(measured)
|
|
1765
|
+
+ } else {
|
|
1766
|
+
+ found[key] = take().apply { set(measured) }
|
|
1767
|
+
+ owners[key] = view
|
|
1768
|
+
+ }
|
|
1769
|
+
+ }
|
|
1770
|
+
+ }
|
|
1771
|
+
+ if (view is ViewGroup) for (index in 0 until view.childCount) collect(view.getChildAt(index), base)
|
|
1772
|
+
+ }
|
|
1773
|
+
+
|
|
1774
|
+
+ private fun take(): RectF = (pool.getOrNull(pooled) ?: RectF().also { pool.add(it) }).also { pooled += 1 }
|
|
1775
|
+
+
|
|
1776
|
+
+ // The view's bounds in the base's pixels, through every transform and scroll between them.
|
|
1777
|
+
+ private fun measure(view: View, base: ViewGroup): Boolean {
|
|
1778
|
+
+ measured.set(0f, 0f, view.width.toFloat(), view.height.toFloat())
|
|
1779
|
+
+ var current = view
|
|
1780
|
+
+ while (current !== base) {
|
|
1781
|
+
+ if (!current.matrix.isIdentity) current.matrix.mapRect(measured)
|
|
1782
|
+
+ measured.offset(current.left.toFloat(), current.top.toFloat())
|
|
1783
|
+
+ val parent = current.parent as? View ?: return false
|
|
1784
|
+
+ measured.offset(-parent.scrollX.toFloat(), -parent.scrollY.toFloat())
|
|
1785
|
+
+ current = parent
|
|
1786
|
+
+ }
|
|
1787
|
+
+ return measured.width() > 0f && measured.height() > 0f
|
|
1788
|
+
+ }
|
|
1789
|
+
+
|
|
1790
|
+
+ fun advance(seconds: Float) {
|
|
1791
|
+
+ if (members.isEmpty()) return
|
|
1792
|
+
+ for (member in members.values) member.advance(seconds)
|
|
1793
|
+
+ members.values.removeAll { it.leaving && it.isSettled }
|
|
1794
|
+
+ }
|
|
1795
|
+
+
|
|
1796
|
+
+ // The members as they are now into [out], from the base's pixels ([baseWidth] x [baseHeight]) into [rect], where
|
|
1797
|
+
+ // the base is drawn now (its press and its morph stretch it). False for a group with nothing to draw.
|
|
1798
|
+
+ fun place(out: GlassShapes, rect: RectF, baseWidth: Int, baseHeight: Int, density: Float): Boolean {
|
|
1799
|
+
+ out.clear()
|
|
1800
|
+
+ out.blend = blend * density
|
|
1801
|
+
+ if (records == null) return true
|
|
1802
|
+
+ val scaleX = rect.width() / max(baseWidth, 1)
|
|
1803
|
+
+ val scaleY = rect.height() / max(baseHeight, 1)
|
|
1804
|
+
+ for (member in members.values) {
|
|
1805
|
+
+ member.current(placed)
|
|
1806
|
+
+ if (placed.width() < MinimumSize || placed.height() < MinimumSize) continue
|
|
1807
|
+
+ placed.set(
|
|
1808
|
+
+ rect.left + placed.left * scaleX, rect.top + placed.top * scaleY,
|
|
1809
|
+
+ rect.left + placed.right * scaleX, rect.top + placed.bottom * scaleY,
|
|
1810
|
+
+ )
|
|
1811
|
+
+ val radius = member.radius?.let { it * density * min(scaleX, scaleY) } ?: Float.MAX_VALUE
|
|
1812
|
+
+ out.add(placed, radius)
|
|
1813
|
+
+ }
|
|
1814
|
+
+ return out.count > 0
|
|
1815
|
+
+ }
|
|
1816
|
+
+
|
|
1817
|
+
+ private companion object {
|
|
1818
|
+
+ const val Materialize = "materialize"
|
|
1819
|
+
+ const val MinimumSize = .5f
|
|
1820
|
+
+ val Spring = GlassSpringSpec(.75f, .32f)
|
|
1821
|
+
+ }
|
|
1822
|
+
+}
|
|
1823
|
+
--- a/records/GlassMemberRecord.kt
|
|
1824
|
+
+++ b/records/GlassMemberRecord.kt
|
|
1825
|
+
@@ -0,0 +1,14 @@
|
|
1826
|
+
+package so.oxy.bloom.glassmenu.records
|
|
1827
|
+
+
|
|
1828
|
+
+import expo.modules.kotlin.records.Field
|
|
1829
|
+
+import expo.modules.kotlin.records.Record
|
|
1830
|
+
+
|
|
1831
|
+
+// A glass of a group on one base (GlassGroup).
|
|
1832
|
+
+class GlassMemberRecord : Record {
|
|
1833
|
+
+ // The nativeID of the member's views; several views with one key are one shape around them all (a union).
|
|
1834
|
+
+ @Field val key: String = ""
|
|
1835
|
+
+ // The corner radius in points; null is a capsule.
|
|
1836
|
+
+ @Field val cornerRadius: Float? = null
|
|
1837
|
+
+ // How it arrives and leaves: "matchedGeometry" out of and into the nearest glass, "materialize" where it is.
|
|
1838
|
+
+ @Field val transition: String = "matchedGeometry"
|
|
1839
|
+
+}
|
|
1840
|
+
--- a/render/GlassShaders.kt
|
|
1841
|
+
+++ b/render/GlassShaders.kt
|
|
1842
|
+
@@ -54,20 +54,42 @@
|
|
1843
|
+
uniform float2 secondaryHalfSize;
|
|
1844
|
+
uniform float2 secondaryRadii;
|
|
1845
|
+
uniform float smoothing;
|
|
1846
|
+
+uniform float memberCount;
|
|
1847
|
+
+uniform float memberBlend;
|
|
1848
|
+
+uniform float4 members[7];
|
|
1849
|
+
+uniform float memberRadii[7];
|
|
1850
|
+
uniform shader smoothedField;
|
|
1851
|
+
uniform float smoothedFieldEnabled;
|
|
1852
|
+
uniform float2 fieldCoordinateScale;
|
|
1853
|
+
uniform float2 fieldCoordinateOffset;
|
|
1854
|
+
uniform float2 fieldExtent;
|
|
1855
|
+
-float3 liquidAnalyticFieldGradient(float2 point) {
|
|
1856
|
+
- float3 a = nativeGlassRoundedField(point - center, halfSize, cornerRadii, 0.5);
|
|
1857
|
+
- if (secondaryEnabled < 0.5) return a;
|
|
1858
|
+
- float3 b = nativeGlassRoundedField(point - secondaryCenter, secondaryHalfSize, secondaryRadii, 0.5);
|
|
1859
|
+
- float k = smoothing * clamp(0.5 - 0.5 * dot(a.yz, b.yz), 0.0, 1.0);
|
|
1860
|
+
+float3 nativeGlassSmoothUnion(float3 a, float3 b, float blend) {
|
|
1861
|
+
+ float k = blend * clamp(0.5 - 0.5 * dot(a.yz, b.yz), 0.0, 1.0);
|
|
1862
|
+
if (k <= 0.000001) return a.x < b.x ? a : b;
|
|
1863
|
+
float h = clamp(0.5 + 0.5 * (a.x - b.x) / k, 0.0, 1.0);
|
|
1864
|
+
return float3(mix(a.x, b.x, h) - k * h * (1.0 - h), mix(a.yz, b.yz, h));
|
|
1865
|
+
}
|
|
1866
|
+
+// A trigger of several shapes (a group of glasses): its other shapes (center, half size; radius), blended into its first.
|
|
1867
|
+
+// 7: GlassShapes.Capacity less the first. Where two shapes meet, the union's gradient is the mean of normals that face
|
|
1868
|
+
+// each other, short at the neck: the glass there still faces outward, so the group's normal is of unit length.
|
|
1869
|
+
+float3 liquidMemberField(float2 point, float3 first) {
|
|
1870
|
+
+ if (memberCount < 0.5) return first;
|
|
1871
|
+
+ for (int i = 0; i < 7; i++) {
|
|
1872
|
+
+ if (float(i) >= memberCount) break;
|
|
1873
|
+
+ float3 member = nativeGlassRoundedField(point - members[i].xy, members[i].zw, float2(memberRadii[i]), 0.5);
|
|
1874
|
+
+ first = nativeGlassSmoothUnion(first, member, memberBlend);
|
|
1875
|
+
+ }
|
|
1876
|
+
+ return float3(first.x, nativeGlassSafeNormal(first.yz));
|
|
1877
|
+
+}
|
|
1878
|
+
+// The glass's field: the incoming shape, and in a morph the trigger it grows out of (the secondary shape). The
|
|
1879
|
+
+// trigger's other shapes, if it has any, are drawn where the trigger is: the secondary in a morph, else the incoming.
|
|
1880
|
+
+float3 liquidAnalyticFieldGradient(float2 point) {
|
|
1881
|
+
+ float3 a = nativeGlassRoundedField(point - center, halfSize, cornerRadii, 0.5);
|
|
1882
|
+
+ if (secondaryEnabled < 0.5) return liquidMemberField(point, a);
|
|
1883
|
+
+ float3 b = liquidMemberField(point,
|
|
1884
|
+
+ nativeGlassRoundedField(point - secondaryCenter, secondaryHalfSize, secondaryRadii, 0.5));
|
|
1885
|
+
+ return nativeGlassSmoothUnion(a, b, smoothing);
|
|
1886
|
+
+}
|
|
1887
|
+
float3 smoothedFieldSample(float2 pixel) {
|
|
1888
|
+
float2 st = pixel - 0.5;
|
|
1889
|
+
float2 base = floor(st);
|
|
1890
|
+
--- a/render/GlassSurfaceState.kt
|
|
1891
|
+
+++ b/render/GlassSurfaceState.kt
|
|
1892
|
+
@@ -1,6 +1,7 @@
|
|
1893
|
+
package so.oxy.bloom.glassmenu.render
|
|
1894
|
+
|
|
1895
|
+
import android.graphics.RectF
|
|
1896
|
+
+import so.oxy.bloom.glassmenu.field.GlassShapes
|
|
1897
|
+
import so.oxy.bloom.glassmenu.records.GlassMenuConfigRecord
|
|
1898
|
+
import so.oxy.bloom.glassmenu.records.GlassTriggerConfigRecord
|
|
1899
|
+
|
|
1900
|
+
@@ -30,6 +31,8 @@
|
|
1901
|
+
var magnify = 1f
|
|
1902
|
+
// Width of the refraction band in dp. 0 = the default band (min(extent * 0.25, 20 dp)). The menu never sets it.
|
|
1903
|
+
var lensBand = 0f
|
|
1904
|
+
+ // The trigger drawn as several shapes (a group of glasses), in its place: empty, the trigger is its one rect.
|
|
1905
|
+
+ val members = GlassShapes()
|
|
1906
|
+
var config = GlassMenuConfigRecord()
|
|
1907
|
+
var trigger = GlassTriggerConfigRecord()
|
|
1908
|
+
}
|