@onekeyfe/react-native-native-overlay 6.8.0-2 → 6.8.0-20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayHost.kt +37 -5
  2. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayPackage.kt +1 -1
  3. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastCardView.kt +869 -0
  4. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastSpec.kt +327 -0
  5. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterView.kt +574 -0
  6. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterViewManager.kt +54 -0
  7. package/android/src/test/java/com/onekey/nativeoverlay/NativeOverlayAccessibilityIsolationTest.kt +172 -0
  8. package/docs/SPEC.md +172 -11
  9. package/ios/NativeOverlayContainerView.swift +20 -4
  10. package/ios/NativeOverlayToastCardView.swift +572 -0
  11. package/ios/NativeOverlayToastSpec.swift +356 -0
  12. package/ios/NativeOverlayToasterView.swift +519 -0
  13. package/ios/RCTNativeOverlayToasterComponentView.h +11 -0
  14. package/ios/RCTNativeOverlayToasterComponentView.mm +85 -0
  15. package/lib/module/NativeOverlayToasterNativeComponent.ts +24 -0
  16. package/lib/module/OverlayStore.js +20 -3
  17. package/lib/module/OverlayStore.js.map +1 -1
  18. package/lib/module/OverlayView.web.js +9 -18
  19. package/lib/module/OverlayView.web.js.map +1 -1
  20. package/lib/module/index.js +2 -0
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/module/toast/OverlayToastHost.js +42 -0
  23. package/lib/module/toast/OverlayToastHost.js.map +1 -0
  24. package/lib/module/toast/OverlayToastHost.web.js +32 -0
  25. package/lib/module/toast/OverlayToastHost.web.js.map +1 -0
  26. package/lib/module/toast/OverlayToastStore.js +217 -0
  27. package/lib/module/toast/OverlayToastStore.js.map +1 -0
  28. package/lib/module/toast/toastBridge.js +65 -0
  29. package/lib/module/toast/toastBridge.js.map +1 -0
  30. package/lib/module/toast/toastDomContent.web.js +298 -0
  31. package/lib/module/toast/toastDomContent.web.js.map +1 -0
  32. package/lib/module/toast/toastDomRenderer.web.js +695 -0
  33. package/lib/module/toast/toastDomRenderer.web.js.map +1 -0
  34. package/lib/module/toast/toastStyles.web.js +97 -0
  35. package/lib/module/toast/toastStyles.web.js.map +1 -0
  36. package/lib/module/toast/types.js +2 -0
  37. package/lib/module/toast/types.js.map +1 -0
  38. package/lib/module/web/overlayLayers.js +82 -0
  39. package/lib/module/web/overlayLayers.js.map +1 -1
  40. package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts +19 -0
  41. package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts.map +1 -0
  42. package/lib/typescript/src/OverlayStore.d.ts +8 -1
  43. package/lib/typescript/src/OverlayStore.d.ts.map +1 -1
  44. package/lib/typescript/src/OverlayView.web.d.ts.map +1 -1
  45. package/lib/typescript/src/index.d.ts +4 -0
  46. package/lib/typescript/src/index.d.ts.map +1 -1
  47. package/lib/typescript/src/toast/OverlayToastHost.d.ts +6 -0
  48. package/lib/typescript/src/toast/OverlayToastHost.d.ts.map +1 -0
  49. package/lib/typescript/src/toast/OverlayToastHost.web.d.ts +6 -0
  50. package/lib/typescript/src/toast/OverlayToastHost.web.d.ts.map +1 -0
  51. package/lib/typescript/src/toast/OverlayToastStore.d.ts +47 -0
  52. package/lib/typescript/src/toast/OverlayToastStore.d.ts.map +1 -0
  53. package/lib/typescript/src/toast/toastBridge.d.ts +11 -0
  54. package/lib/typescript/src/toast/toastBridge.d.ts.map +1 -0
  55. package/lib/typescript/src/toast/toastDomContent.web.d.ts +15 -0
  56. package/lib/typescript/src/toast/toastDomContent.web.d.ts.map +1 -0
  57. package/lib/typescript/src/toast/toastDomRenderer.web.d.ts +8 -0
  58. package/lib/typescript/src/toast/toastDomRenderer.web.d.ts.map +1 -0
  59. package/lib/typescript/src/toast/toastStyles.web.d.ts +2 -0
  60. package/lib/typescript/src/toast/toastStyles.web.d.ts.map +1 -0
  61. package/lib/typescript/src/toast/types.d.ts +240 -0
  62. package/lib/typescript/src/toast/types.d.ts.map +1 -0
  63. package/lib/typescript/src/web/overlayLayers.d.ts +2 -0
  64. package/lib/typescript/src/web/overlayLayers.d.ts.map +1 -1
  65. package/package.json +3 -2
  66. package/react-native.config.js +1 -0
  67. package/src/NativeOverlayToasterNativeComponent.ts +24 -0
  68. package/src/OverlayStore.ts +35 -4
  69. package/src/OverlayView.web.tsx +10 -11
  70. package/src/index.ts +23 -0
  71. package/src/toast/OverlayToastHost.tsx +62 -0
  72. package/src/toast/OverlayToastHost.web.tsx +29 -0
  73. package/src/toast/OverlayToastStore.ts +254 -0
  74. package/src/toast/toastBridge.ts +62 -0
  75. package/src/toast/toastDomContent.web.ts +351 -0
  76. package/src/toast/toastDomRenderer.web.ts +856 -0
  77. package/src/toast/toastStyles.web.ts +97 -0
  78. package/src/toast/types.ts +251 -0
  79. package/src/web/overlayLayers.ts +114 -0
@@ -0,0 +1,327 @@
1
+ package com.onekey.nativeoverlay
2
+
3
+ import android.graphics.Color
4
+ import org.json.JSONArray
5
+ import org.json.JSONObject
6
+ import kotlin.math.abs
7
+ import kotlin.math.roundToInt
8
+
9
+ /** Mirrors `src/toast/types.ts`: everything a toast card paints, as data. */
10
+ internal object ToastColor {
11
+ /** Parses the CSS colour forms the JS theme resolves to. */
12
+ fun parse(value: String?): Int? {
13
+ val raw = value?.trim()?.lowercase()?.takeIf { it.isNotEmpty() } ?: return null
14
+ if (raw == "transparent") return Color.TRANSPARENT
15
+ if (raw.startsWith("#")) return hex(raw.substring(1))
16
+ val open = raw.indexOf('(')
17
+ val close = raw.lastIndexOf(')')
18
+ if (open < 0 || close < open) return null
19
+ val name = raw.substring(0, open)
20
+ val parts = raw.substring(open + 1, close).split(',', ' ', '/').filter { it.isNotEmpty() }
21
+ fun number(text: String, scale: Float): Float? =
22
+ if (text.endsWith("%")) text.dropLast(1).toFloatOrNull()?.div(100f)
23
+ else text.toFloatOrNull()?.div(scale)
24
+ if ((name == "rgb" || name == "rgba") && parts.size >= 3) {
25
+ val r = number(parts[0], 255f) ?: return null
26
+ val g = number(parts[1], 255f) ?: return null
27
+ val b = number(parts[2], 255f) ?: return null
28
+ val a = if (parts.size > 3) number(parts[3], 1f) ?: 1f else 1f
29
+ return argb(a, r, g, b)
30
+ }
31
+ if ((name == "hsl" || name == "hsla") && parts.size >= 3) {
32
+ val h = parts[0].removeSuffix("deg").toFloatOrNull() ?: return null
33
+ val s = number(parts[1], 100f) ?: return null
34
+ val l = number(parts[2], 100f) ?: return null
35
+ val a = if (parts.size > 3) number(parts[3], 1f) ?: 1f else 1f
36
+ val chroma = (1 - abs(2 * l - 1)) * s
37
+ val sector = (((h % 360) + 360) % 360) / 60f
38
+ val x = chroma * (1 - abs(sector % 2 - 1))
39
+ val (r, g, b) = when (sector.toInt()) {
40
+ 0 -> Triple(chroma, x, 0f)
41
+ 1 -> Triple(x, chroma, 0f)
42
+ 2 -> Triple(0f, chroma, x)
43
+ 3 -> Triple(0f, x, chroma)
44
+ 4 -> Triple(x, 0f, chroma)
45
+ else -> Triple(chroma, 0f, x)
46
+ }
47
+ val m = l - chroma / 2
48
+ return argb(a, r + m, g + m, b + m)
49
+ }
50
+ return null
51
+ }
52
+
53
+ private fun argb(a: Float, r: Float, g: Float, b: Float): Int = Color.argb(
54
+ (a.coerceIn(0f, 1f) * 255).roundToInt(),
55
+ (r.coerceIn(0f, 1f) * 255).roundToInt(),
56
+ (g.coerceIn(0f, 1f) * 255).roundToInt(),
57
+ (b.coerceIn(0f, 1f) * 255).roundToInt(),
58
+ )
59
+
60
+ private fun hex(text: String): Int? {
61
+ val digits = if (text.length == 3 || text.length == 4) {
62
+ text.map { "$it$it" }.joinToString("")
63
+ } else {
64
+ text
65
+ }
66
+ val value = digits.toLongOrNull(16) ?: return null
67
+ return when (digits.length) {
68
+ 6 -> Color.rgb((value shr 16 and 0xff).toInt(), (value shr 8 and 0xff).toInt(), (value and 0xff).toInt())
69
+ 8 -> Color.argb(
70
+ (value and 0xff).toInt(),
71
+ (value shr 24 and 0xff).toInt(),
72
+ (value shr 16 and 0xff).toInt(),
73
+ (value shr 8 and 0xff).toInt(),
74
+ )
75
+ else -> null
76
+ }
77
+ }
78
+
79
+ /** Formats a colour into the strict `rgba(r,g,b,a)` string the named-icon registry accepts. */
80
+ fun rgba(color: Int): String {
81
+ val r = Color.red(color)
82
+ val g = Color.green(color)
83
+ val b = Color.blue(color)
84
+ val a = Color.alpha(color)
85
+ val alpha = when (a) {
86
+ 255 -> "1"
87
+ 0 -> "0"
88
+ else -> String.format(java.util.Locale.US, "%.4f", a / 255.0).trimEnd('0').trimEnd('.')
89
+ }
90
+ return "rgba($r,$g,$b,$alpha)"
91
+ }
92
+ }
93
+
94
+ private fun JSONObject.float(key: String, fallback: Float): Float =
95
+ if (has(key) && !isNull(key)) optDouble(key, fallback.toDouble()).toFloat() else fallback
96
+
97
+ private fun JSONObject.stringOrNull(key: String): String? =
98
+ if (has(key) && !isNull(key)) optString(key) else null
99
+
100
+ private fun JSONObject.faces(key: String): Map<String, String> {
101
+ val faces = optJSONObject(key) ?: return emptyMap()
102
+ return faces.keys().asSequence().associateWith { faces.optString(it) }
103
+ }
104
+
105
+ private fun <T> JSONArray?.mapObjects(transform: (JSONObject) -> T?): List<T> {
106
+ this ?: return emptyList()
107
+ return (0 until length()).mapNotNull { optJSONObject(it)?.let(transform) }
108
+ }
109
+
110
+ internal data class ToastTextRun(
111
+ val text: String,
112
+ val color: Int?,
113
+ val fontWeight: String?,
114
+ val fontSize: Float?,
115
+ val underline: Boolean,
116
+ val link: String?,
117
+ ) {
118
+ companion object {
119
+ fun parse(json: JSONObject): ToastTextRun = ToastTextRun(
120
+ text = json.optString("text"),
121
+ color = ToastColor.parse(json.stringOrNull("color")),
122
+ fontWeight = json.stringOrNull("fontWeight"),
123
+ fontSize = if (json.has("fontSize")) json.float("fontSize", 14f) else null,
124
+ underline = json.optBoolean("underline", false),
125
+ link = json.stringOrNull("link"),
126
+ )
127
+ }
128
+ }
129
+
130
+ internal data class ToastText(
131
+ val paragraphs: List<List<ToastTextRun>>,
132
+ val color: Int,
133
+ val fontSize: Float,
134
+ val lineHeight: Float,
135
+ val fontWeight: String,
136
+ val letterSpacing: Float,
137
+ val fontFamily: String?,
138
+ /** Font name per weight, for typefaces that ship one face per weight. */
139
+ val fontFaces: Map<String, String>,
140
+ val tabularNumbers: Boolean,
141
+ val numberOfLines: Int,
142
+ ) {
143
+ companion object {
144
+ fun parse(json: JSONObject?): ToastText? {
145
+ json ?: return null
146
+ val paragraphs = json.optJSONArray("paragraphs")
147
+ return ToastText(
148
+ paragraphs = (0 until (paragraphs?.length() ?: 0)).map { index ->
149
+ paragraphs?.optJSONArray(index).mapObjects(ToastTextRun::parse)
150
+ },
151
+ color = ToastColor.parse(json.stringOrNull("color")) ?: Color.BLACK,
152
+ fontSize = json.float("fontSize", 14f),
153
+ lineHeight = json.float("lineHeight", 20f),
154
+ fontWeight = json.optString("fontWeight", "400"),
155
+ letterSpacing = json.float("letterSpacing", 0f),
156
+ fontFamily = json.stringOrNull("fontFamily"),
157
+ fontFaces = json.faces("fontFaces"),
158
+ tabularNumbers = json.optBoolean("tabularNumbers", false),
159
+ numberOfLines = json.optInt("numberOfLines", 0),
160
+ )
161
+ }
162
+ }
163
+ }
164
+
165
+ internal sealed class ToastIcon {
166
+ abstract val size: Float
167
+
168
+ data class Named(val name: String, val colorString: String, val color: Int, override val size: Float) :
169
+ ToastIcon()
170
+
171
+ data class Spinner(val color: Int, override val size: Float) : ToastIcon()
172
+
173
+ data class Image(val uri: String, override val size: Float, val radius: Float) : ToastIcon()
174
+
175
+ companion object {
176
+ fun parse(json: JSONObject?): ToastIcon? {
177
+ json ?: return null
178
+ val size = json.float("size", 20f)
179
+ return when (json.optString("kind")) {
180
+ "icon" -> {
181
+ val color = ToastColor.parse(json.stringOrNull("color")) ?: Color.BLACK
182
+ Named(json.optString("name"), ToastColor.rgba(color), color, size)
183
+ }
184
+ "spinner" -> Spinner(ToastColor.parse(json.stringOrNull("color")) ?: Color.GRAY, size)
185
+ "image" -> Image(json.optString("uri"), size, json.float("borderRadius", 0f))
186
+ else -> null
187
+ }
188
+ }
189
+ }
190
+ }
191
+
192
+ internal data class ToastAction(
193
+ val id: String,
194
+ val label: String,
195
+ val disabled: Boolean,
196
+ val testID: String?,
197
+ val icon: ToastIcon?,
198
+ val iconGap: Float,
199
+ val color: Int,
200
+ val backgroundColor: Int,
201
+ val pressedBackgroundColor: Int?,
202
+ val fontSize: Float,
203
+ val lineHeight: Float,
204
+ val fontWeight: String,
205
+ val fontFamily: String?,
206
+ val fontFaces: Map<String, String>,
207
+ val paddingHorizontal: Float,
208
+ val paddingVertical: Float,
209
+ val marginHorizontal: Float,
210
+ val marginVertical: Float,
211
+ val borderWidth: Float,
212
+ val borderRadius: Float,
213
+ val disabledOpacity: Float,
214
+ ) {
215
+ companion object {
216
+ fun parse(json: JSONObject): ToastAction = ToastAction(
217
+ id = json.optString("id"),
218
+ label = json.optString("label"),
219
+ disabled = json.optBoolean("disabled", false),
220
+ testID = json.stringOrNull("testID"),
221
+ icon = ToastIcon.parse(json.optJSONObject("icon")),
222
+ iconGap = json.float("iconGap", 0f),
223
+ color = ToastColor.parse(json.stringOrNull("color")) ?: Color.BLACK,
224
+ backgroundColor = ToastColor.parse(json.stringOrNull("backgroundColor")) ?: Color.TRANSPARENT,
225
+ pressedBackgroundColor = ToastColor.parse(json.stringOrNull("pressedBackgroundColor")),
226
+ fontSize = json.float("fontSize", 14f),
227
+ lineHeight = json.float("lineHeight", 20f),
228
+ fontWeight = json.optString("fontWeight", "500"),
229
+ fontFamily = json.stringOrNull("fontFamily"),
230
+ fontFaces = json.faces("fontFaces"),
231
+ paddingHorizontal = json.float("paddingHorizontal", 0f),
232
+ paddingVertical = json.float("paddingVertical", 0f),
233
+ marginHorizontal = json.float("marginHorizontal", 0f),
234
+ marginVertical = json.float("marginVertical", 0f),
235
+ borderWidth = json.float("borderWidth", 0f),
236
+ borderRadius = json.float("borderRadius", 0f),
237
+ disabledOpacity = json.float("disabledOpacity", 1f),
238
+ )
239
+ }
240
+ }
241
+
242
+ internal data class ToastSpec(
243
+ val id: String,
244
+ val isNotification: Boolean,
245
+ /** Milliseconds; negative never times out. */
246
+ val duration: Double,
247
+ val testID: String?,
248
+ val icon: ToastIcon?,
249
+ val title: ToastText?,
250
+ val message: ToastText?,
251
+ val actions: List<ToastAction>,
252
+ val actionsAlignLeft: Boolean,
253
+ val badgeColor: Int,
254
+ val badgeSize: Float,
255
+ val image: ToastIcon.Image?,
256
+ val pressable: Boolean,
257
+ ) {
258
+ companion object {
259
+ fun parse(json: JSONObject): ToastSpec = ToastSpec(
260
+ id = json.optString("id"),
261
+ isNotification = json.optString("layout") == "notification",
262
+ duration = json.optDouble("duration", 5000.0),
263
+ testID = json.stringOrNull("testID"),
264
+ icon = ToastIcon.parse(json.optJSONObject("icon")),
265
+ title = ToastText.parse(json.optJSONObject("title")),
266
+ message = ToastText.parse(json.optJSONObject("message")),
267
+ actions = json.optJSONArray("actions").mapObjects(ToastAction::parse),
268
+ actionsAlignLeft = json.optString("actionsAlign") == "left",
269
+ badgeColor = ToastColor.parse(json.stringOrNull("badgeColor")) ?: Color.TRANSPARENT,
270
+ badgeSize = json.float("badgeSize", 28f),
271
+ image = json.optJSONObject("image")?.let {
272
+ ToastIcon.Image(it.optString("uri"), it.float("size", 48f), it.float("borderRadius", 0f))
273
+ },
274
+ pressable = json.optBoolean("pressable", false),
275
+ )
276
+ }
277
+ }
278
+
279
+ internal data class ToastItem(
280
+ val spec: ToastSpec,
281
+ val visible: Boolean,
282
+ val revision: Int,
283
+ val generation: Int,
284
+ ) {
285
+ companion object {
286
+ fun parseList(json: String?): List<ToastItem> {
287
+ if (json.isNullOrEmpty()) return emptyList()
288
+ val list = runCatching { JSONArray(json) }.getOrNull() ?: return emptyList()
289
+ return list.mapObjects { entry ->
290
+ val spec = entry.optJSONObject("spec") ?: return@mapObjects null
291
+ ToastItem(
292
+ spec = ToastSpec.parse(spec),
293
+ visible = entry.optBoolean("visible", true),
294
+ revision = entry.optInt("revision", 0),
295
+ generation = entry.optInt("generation", 0),
296
+ )
297
+ }
298
+ }
299
+ }
300
+ }
301
+
302
+ internal data class ToasterConfig(
303
+ val cardColor: Int = Color.WHITE,
304
+ /** Width of the card's content box in dp; null derives it from the window. */
305
+ val contentWidth: Float? = null,
306
+ val contentMaxHeight: Float? = null,
307
+ val topObstruction: Float = 0f,
308
+ val topObstructionGap: Float = 8f,
309
+ val topObstructionRoom: Float = 88f,
310
+ val respectReducedMotion: Boolean = true,
311
+ ) {
312
+ companion object {
313
+ fun parse(json: String?): ToasterConfig {
314
+ if (json.isNullOrEmpty()) return ToasterConfig()
315
+ val dict = runCatching { JSONObject(json) }.getOrNull() ?: return ToasterConfig()
316
+ return ToasterConfig(
317
+ cardColor = ToastColor.parse(dict.stringOrNull("cardColor")) ?: Color.WHITE,
318
+ contentWidth = if (dict.has("contentWidth")) dict.float("contentWidth", 0f) else null,
319
+ contentMaxHeight = if (dict.has("contentMaxHeight")) dict.float("contentMaxHeight", 0f) else null,
320
+ topObstruction = dict.float("topObstruction", 0f),
321
+ topObstructionGap = dict.float("topObstructionGap", 8f),
322
+ topObstructionRoom = dict.float("topObstructionRoom", 88f),
323
+ respectReducedMotion = dict.optBoolean("respectReducedMotion", true),
324
+ )
325
+ }
326
+ }
327
+ }