@c9up/orbit 0.0.0-stage → 0.1.1

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 (105) hide show
  1. package/Cargo.lock +139 -0
  2. package/Cargo.toml +20 -0
  3. package/LICENSE +21 -0
  4. package/README.md +320 -2
  5. package/android/orbit.cmake +31 -0
  6. package/android/src/main/cpp/NativeOrbit.cpp +309 -0
  7. package/android/src/main/cpp/NativeOrbit.h +35 -0
  8. package/android/src/main/cpp/orbit_host.h +34 -0
  9. package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +286 -0
  10. package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +258 -0
  11. package/android/src/main/java/com/c9up/orbit/OrbitImage.kt +346 -0
  12. package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +161 -0
  13. package/android/src/main/java/com/c9up/orbit/OrbitPress.kt +110 -0
  14. package/android/src/main/java/com/c9up/orbit/OrbitVector.kt +115 -0
  15. package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +362 -0
  16. package/crates/orbit-host/Cargo.toml +14 -0
  17. package/crates/orbit-host/src/gesture.rs +561 -0
  18. package/crates/orbit-host/src/lib.rs +2042 -0
  19. package/crates/orbit-host/src/motion.rs +328 -0
  20. package/crates/orbit-host/src/vector.rs +1173 -0
  21. package/dist/color.d.ts +10 -0
  22. package/dist/color.d.ts.map +1 -0
  23. package/dist/color.js +62 -0
  24. package/dist/color.js.map +1 -0
  25. package/dist/commit.d.ts +77 -0
  26. package/dist/commit.d.ts.map +1 -0
  27. package/dist/commit.js +302 -0
  28. package/dist/commit.js.map +1 -0
  29. package/dist/errors.d.ts +76 -0
  30. package/dist/errors.d.ts.map +1 -0
  31. package/dist/errors.js +89 -0
  32. package/dist/errors.js.map +1 -0
  33. package/dist/host.d.ts +49 -0
  34. package/dist/host.d.ts.map +1 -0
  35. package/dist/host.js +20 -0
  36. package/dist/host.js.map +1 -0
  37. package/dist/index.d.ts +31 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +31 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/motion.d.ts +18 -0
  42. package/dist/motion.d.ts.map +1 -0
  43. package/dist/motion.js +67 -0
  44. package/dist/motion.js.map +1 -0
  45. package/dist/node.d.ts +46 -0
  46. package/dist/node.d.ts.map +1 -0
  47. package/dist/node.js +110 -0
  48. package/dist/node.js.map +1 -0
  49. package/dist/pages.d.ts +56 -0
  50. package/dist/pages.d.ts.map +1 -0
  51. package/dist/pages.js +157 -0
  52. package/dist/pages.js.map +1 -0
  53. package/dist/react-native/index.d.ts +33 -0
  54. package/dist/react-native/index.d.ts.map +1 -0
  55. package/dist/react-native/index.js +71 -0
  56. package/dist/react-native/index.js.map +1 -0
  57. package/dist/report.d.ts +13 -0
  58. package/dist/report.d.ts.map +1 -0
  59. package/dist/report.js +22 -0
  60. package/dist/report.js.map +1 -0
  61. package/dist/server.d.ts +109 -0
  62. package/dist/server.d.ts.map +1 -0
  63. package/dist/server.js +290 -0
  64. package/dist/server.js.map +1 -0
  65. package/dist/stack.d.ts +117 -0
  66. package/dist/stack.d.ts.map +1 -0
  67. package/dist/stack.js +299 -0
  68. package/dist/stack.js.map +1 -0
  69. package/dist/styles.d.ts +138 -0
  70. package/dist/styles.d.ts.map +1 -0
  71. package/dist/styles.js +920 -0
  72. package/dist/styles.js.map +1 -0
  73. package/dist/template.d.ts +27 -0
  74. package/dist/template.d.ts.map +1 -0
  75. package/dist/template.js +1436 -0
  76. package/dist/template.js.map +1 -0
  77. package/dist/textDecoder.d.ts +29 -0
  78. package/dist/textDecoder.d.ts.map +1 -0
  79. package/dist/textDecoder.js +149 -0
  80. package/dist/textDecoder.js.map +1 -0
  81. package/dist/url.d.ts +45 -0
  82. package/dist/url.d.ts.map +1 -0
  83. package/dist/url.js +303 -0
  84. package/dist/url.js.map +1 -0
  85. package/package.json +96 -3
  86. package/scripts/build-android.sh +27 -0
  87. package/scripts/test-styles.mjs +31 -0
  88. package/specs/NativeOrbit.ts +34 -0
  89. package/src/color.ts +68 -0
  90. package/src/commit.ts +373 -0
  91. package/src/errors.ts +178 -0
  92. package/src/host.ts +91 -0
  93. package/src/index.ts +64 -0
  94. package/src/motion.ts +88 -0
  95. package/src/node.ts +133 -0
  96. package/src/pages.ts +207 -0
  97. package/src/react-native/index.ts +115 -0
  98. package/src/react-native/react-native.d.ts +39 -0
  99. package/src/report.ts +25 -0
  100. package/src/server.ts +395 -0
  101. package/src/stack.ts +428 -0
  102. package/src/styles.ts +1021 -0
  103. package/src/template.ts +1681 -0
  104. package/src/textDecoder.ts +162 -0
  105. package/src/url.ts +333 -0
@@ -0,0 +1,362 @@
1
+ package com.c9up.orbit
2
+
3
+ import android.content.Context
4
+ import android.os.Build
5
+ import android.graphics.Canvas
6
+ import android.graphics.Paint
7
+ import android.graphics.Typeface
8
+ import android.graphics.drawable.GradientDrawable
9
+ import android.text.Layout
10
+ import android.text.SpannableStringBuilder
11
+ import android.text.Spanned
12
+ import android.text.StaticLayout
13
+ import android.text.TextPaint
14
+ import android.text.style.AbsoluteSizeSpan
15
+ import android.text.style.ForegroundColorSpan
16
+ import android.text.style.LineHeightSpan
17
+ import android.text.style.StyleSpan
18
+ import android.text.style.TypefaceSpan
19
+ import android.view.MotionEvent
20
+ import android.view.View
21
+ import android.view.ViewGroup
22
+ import android.widget.ScrollView
23
+ import java.nio.ByteBuffer
24
+ import java.nio.ByteOrder
25
+ import kotlin.math.ceil
26
+
27
+ /** A host view whose frame and look come from orbit-host, not from Android layout. */
28
+ interface Framed {
29
+ fun setFrame(x: Float, y: Float, w: Float, h: Float)
30
+ fun setLook(background: Int, borderColor: Int, borderWidth: Float, radius: Float, opacity: Float)
31
+ }
32
+
33
+ fun View.applyFrame(frame: FloatArray) {
34
+ layout(frame[0].toInt(), frame[1].toInt(), (frame[0] + frame[2]).toInt(), (frame[1] + frame[3]).toInt())
35
+ }
36
+
37
+ fun View.applyLook(fill: Int, borderColor: Int, borderWidth: Float, radius: Float, opacity: Float) {
38
+ OrbitEffects.setRadius(this, radius)
39
+ OrbitEffects.setOpaque(this, fill ushr 24 == 0xff && opacity >= 1f)
40
+ OrbitEffects.setFill(this, fill)
41
+ invalidateOutline()
42
+ background = if (fill == 0 && borderWidth == 0f) null else GradientDrawable().apply {
43
+ setColor(fill)
44
+ cornerRadius = radius
45
+ if (borderWidth > 0f) setStroke(ceil(borderWidth).toInt(), borderColor)
46
+ }
47
+ alpha = opacity
48
+ }
49
+
50
+ /**
51
+ * Clip a view to its own (rounded) box, whatever its parent does. On Android
52
+ * a view is clipped to its bounds by its parent's `clipChildren`, which
53
+ * orbit's boxes turn off (CSS's overflow is visible): a scroll view and a
54
+ * text field, which scroll their content inside them, clip themselves.
55
+ */
56
+ fun View.clipToOwnBox() {
57
+ outlineProvider = object : android.view.ViewOutlineProvider() {
58
+ override fun getOutline(view: View, outline: android.graphics.Outline) {
59
+ val radius = OrbitEffects.radiusOf(view)
60
+ outline.setRoundRect(0, 0, view.width, view.height, minOf(radius, minOf(view.width, view.height) / 2f))
61
+ }
62
+ }
63
+ clipToOutline = true
64
+ }
65
+
66
+ fun frameOf(view: View): FloatArray? =
67
+ (view as? OrbitBox)?.frame ?: (view as? OrbitTextView)?.frame ?: (view as? OrbitScroll)?.frame
68
+ ?: (view as? OrbitInput)?.frame ?: (view as? OrbitImage)?.frame
69
+
70
+ /** Native scrolling: the content child's frame (from orbit-host) sets the range. */
71
+ class OrbitScroll(context: Context) : ScrollView(context), Framed {
72
+ val frame = FloatArray(4)
73
+
74
+ init {
75
+ isVerticalScrollBarEnabled = false
76
+ overScrollMode = OVER_SCROLL_NEVER
77
+ clipToOwnBox()
78
+ // The offset, in dp, for what reads it in JS (a lazy list picks its rows by it).
79
+ setOnScrollChangeListener { _, _, y, _, _ ->
80
+ (tag as? Int)?.let { OrbitHost.nativeDispatch(it, "topScroll", 0f, y / OrbitHost.density) }
81
+ }
82
+ }
83
+
84
+ override fun setFrame(x: Float, y: Float, w: Float, h: Float) {
85
+ frame[0] = x; frame[1] = y; frame[2] = w; frame[3] = h
86
+ applyFrame(frame)
87
+ }
88
+
89
+ override fun setLook(background: Int, borderColor: Int, borderWidth: Float, radius: Float, opacity: Float) =
90
+ applyLook(background, borderColor, borderWidth, radius, opacity)
91
+
92
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
93
+ setMeasuredDimension(frame[2].toInt(), frame[3].toInt())
94
+ }
95
+
96
+ override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
97
+ for (i in 0 until childCount) {
98
+ val child = getChildAt(i)
99
+ frameOf(child)?.let { child.applyFrame(it) }
100
+ }
101
+ }
102
+ }
103
+
104
+ open class OrbitBox(context: Context) : ViewGroup(context), Framed {
105
+ val frame = FloatArray(4)
106
+ /** Backdrop blur, a Gaussian sigma in px; 0 for none. */
107
+ var backdropSigma = 0f
108
+ internal var appliedSigma = -1f
109
+ /** The backdrop's render node (Android 12+), kept across frames. */
110
+ internal var backdropNode: Any? = null
111
+ private var radius = 0f
112
+ private var clips = false
113
+
114
+ init {
115
+ // CSS's overflow is visible: a child, and the shadow its parent draws for
116
+ // it, may reach past this box. `overflow-hidden` clips (setClip).
117
+ clipChildren = false
118
+ clipToPadding = false
119
+ }
120
+
121
+ fun setClip(clip: Boolean) {
122
+ clips = clip
123
+ clipChildren = clip
124
+ clipToOutline = clip && radius > 0f
125
+ }
126
+
127
+ override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
128
+ OrbitEffects.drawShadow(canvas, child)
129
+ return super.drawChild(canvas, child, drawingTime)
130
+ }
131
+
132
+ override fun draw(canvas: Canvas) {
133
+ if (backdropSigma > 0f) OrbitEffects.drawBackdrop(this, canvas)
134
+ super.draw(canvas)
135
+ }
136
+
137
+ override fun setFrame(x: Float, y: Float, w: Float, h: Float) {
138
+ frame[0] = x; frame[1] = y; frame[2] = w; frame[3] = h
139
+ applyFrame(frame)
140
+ }
141
+
142
+ override fun setLook(background: Int, borderColor: Int, borderWidth: Float, radius: Float, opacity: Float) {
143
+ this.radius = radius
144
+ applyLook(background, borderColor, borderWidth, radius, opacity)
145
+ clipToOutline = clips && radius > 0f
146
+ }
147
+
148
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
149
+ setMeasuredDimension(frame[2].toInt(), frame[3].toInt())
150
+ }
151
+
152
+ // Children keep the frames orbit-host gave them.
153
+ override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
154
+ for (i in 0 until childCount) {
155
+ val child = getChildAt(i)
156
+ val f = frameOf(child) ?: continue
157
+ child.applyFrame(f)
158
+ }
159
+ }
160
+ }
161
+
162
+ /** The surface root: fills the activity and turns touches into JS events. */
163
+ /**
164
+ * The surface root: fills the activity and turns touches into JS events.
165
+ *
166
+ * Apps run edge to edge (React Native's default), so the window does not
167
+ * shrink for the system bars or the keyboard. The root lays orbit's tree out
168
+ * in the safe area instead — inside the status bar, the navigation bar, the
169
+ * display cutout and the keyboard — and reports that area as the viewport.
170
+ */
171
+ class OrbitRoot(context: Context) : OrbitBox(context) {
172
+ private val insets = IntArray(4)
173
+ private var reported = Pair(-1f, -1f)
174
+
175
+ /** The safe area; orbit-host's root view is mounted in it. */
176
+ val content = OrbitBox(context)
177
+
178
+ init {
179
+ addView(content)
180
+ setOnApplyWindowInsetsListener { _, windowInsets ->
181
+ if (android.os.Build.VERSION.SDK_INT >= 30) {
182
+ val types = android.view.WindowInsets.Type.systemBars() or
183
+ android.view.WindowInsets.Type.displayCutout() or
184
+ android.view.WindowInsets.Type.ime()
185
+ val i = windowInsets.getInsets(types)
186
+ insets[0] = i.left; insets[1] = i.top; insets[2] = i.right; insets[3] = i.bottom
187
+ } else {
188
+ @Suppress("DEPRECATION")
189
+ run {
190
+ insets[0] = windowInsets.systemWindowInsetLeft
191
+ insets[1] = windowInsets.systemWindowInsetTop
192
+ insets[2] = windowInsets.systemWindowInsetRight
193
+ insets[3] = windowInsets.systemWindowInsetBottom
194
+ }
195
+ }
196
+ requestLayout()
197
+ windowInsets
198
+ }
199
+ }
200
+
201
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
202
+ setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec))
203
+ }
204
+
205
+ override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
206
+ val w = (r - l) - insets[0] - insets[2]
207
+ val h = (b - t) - insets[1] - insets[3]
208
+ content.frame[0] = insets[0].toFloat(); content.frame[1] = insets[1].toFloat()
209
+ content.frame[2] = w.toFloat(); content.frame[3] = h.toFloat()
210
+ content.layout(insets[0], insets[1], insets[0] + w, insets[1] + h)
211
+ val d = OrbitHost.density
212
+ val viewport = Pair(w / d, h / d)
213
+ if (viewport != reported && w > 0 && h > 0) {
214
+ reported = viewport
215
+ OrbitHost.reportViewport(viewport.first, viewport.second)
216
+ }
217
+ }
218
+
219
+ /** A view claimed the gesture under way: the views below no longer get it. */
220
+ private var claimed = false
221
+
222
+ /**
223
+ * Every touch goes to orbit-host's gesture recognizer first, on this
224
+ * thread: the press feedback starts here, whatever JS is doing. When a view
225
+ * claims the gesture (a pan across a scroll view), the views below get a
226
+ * cancel and nothing more until the finger lifts.
227
+ */
228
+ override fun dispatchTouchEvent(event: MotionEvent): Boolean {
229
+ val action = when (event.actionMasked) {
230
+ MotionEvent.ACTION_DOWN -> 0
231
+ MotionEvent.ACTION_MOVE -> 1
232
+ MotionEvent.ACTION_UP -> 2
233
+ MotionEvent.ACTION_CANCEL -> 3
234
+ else -> -1
235
+ }
236
+ if (action == 0) claimed = false
237
+ if (action >= 0) {
238
+ val d = OrbitHost.density
239
+ val hit = if (action == 0) OrbitHost.hitTest(this, event.x, event.y) ?: 0 else 0
240
+ // In the safe area's coordinates, as JS lays out.
241
+ val commands = OrbitHost.nativeTouch(action, (event.x - insets[0]) / d, (event.y - insets[1]) / d, System.nanoTime(), hit)
242
+ if (OrbitPress.apply(this, commands) && !claimed) {
243
+ claimed = true
244
+ val cancel = MotionEvent.obtain(event)
245
+ cancel.action = MotionEvent.ACTION_CANCEL
246
+ super.dispatchTouchEvent(cancel)
247
+ cancel.recycle()
248
+ }
249
+ }
250
+ if (!claimed) super.dispatchTouchEvent(event)
251
+ if (action == 2 || action == 3) claimed = false
252
+ return true
253
+ }
254
+ }
255
+
256
+ /**
257
+ * CSS's line box: every line exactly `height` px tall, the glyphs centred in
258
+ * it — the leading split half above, half below — so a text is as tall as on
259
+ * the web, whatever the font's own metrics.
260
+ */
261
+ private class CssLineHeight(private val height: Int) : LineHeightSpan {
262
+ override fun chooseHeight(text: CharSequence, start: Int, end: Int, spanstartv: Int, lineHeight: Int, fm: Paint.FontMetricsInt) {
263
+ val extra = height - (fm.descent - fm.ascent)
264
+ val above = extra / 2
265
+ fm.ascent -= above
266
+ fm.descent += extra - above
267
+ fm.top = fm.ascent
268
+ fm.bottom = fm.descent
269
+ }
270
+ }
271
+
272
+ /** Attributed text decoded from orbit-host's encoding; measured and drawn alike. */
273
+ class OrbitText(val text: CharSequence, private val align: Layout.Alignment, private val paint: TextPaint) {
274
+ fun layout(widthPx: Int): StaticLayout =
275
+ StaticLayout.Builder.obtain(text, 0, text.length, paint, widthPx.coerceAtLeast(1))
276
+ .setAlignment(align)
277
+ .setIncludePad(false)
278
+ .build()
279
+
280
+ fun measure(maxWidthPx: Int): Pair<Float, Float> {
281
+ val desired = ceil(Layout.getDesiredWidth(text, paint)).toInt()
282
+ val laid = layout(minOf(desired, maxWidthPx))
283
+ var width = 0f
284
+ for (i in 0 until laid.lineCount) width = maxOf(width, laid.getLineWidth(i))
285
+ return Pair(ceil(width), laid.height.toFloat())
286
+ }
287
+
288
+ companion object {
289
+ fun decode(bytes: ByteArray, density: Float): OrbitText {
290
+ val buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)
291
+ val align = when (buf.get().toInt()) {
292
+ 1 -> Layout.Alignment.ALIGN_CENTER
293
+ 2 -> Layout.Alignment.ALIGN_OPPOSITE
294
+ else -> Layout.Alignment.ALIGN_NORMAL
295
+ }
296
+ val lineHeight = buf.float
297
+ val runs = buf.int
298
+ val out = SpannableStringBuilder()
299
+ val paint = TextPaint(TextPaint.ANTI_ALIAS_FLAG)
300
+ repeat(runs) { index ->
301
+ val color = buf.int; val size = buf.float; val flags = buf.int
302
+ val raw = ByteArray(buf.int)
303
+ buf.get(raw)
304
+ val start = out.length
305
+ out.append(String(raw, Charsets.UTF_8))
306
+ val end = out.length
307
+ if (index == 0) {
308
+ paint.textSize = size * density
309
+ paint.color = color
310
+ }
311
+ out.setSpan(AbsoluteSizeSpan((size * density).toInt()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
312
+ out.setSpan(ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
313
+ val italic = flags and 2 != 0
314
+ val weight = flags shr 4
315
+ if (Build.VERSION.SDK_INT >= 28 && weight > 0) {
316
+ // The exact weight (font-medium is 500), as the web draws it.
317
+ if (weight != 400 || italic) {
318
+ out.setSpan(TypefaceSpan(Typeface.create(Typeface.DEFAULT, weight, italic)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
319
+ }
320
+ } else {
321
+ val style = (if (flags and 1 != 0) Typeface.BOLD else 0) or (if (italic) Typeface.ITALIC else 0)
322
+ if (style != 0) out.setSpan(StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
323
+ }
324
+ }
325
+ if (lineHeight > 0f && out.isNotEmpty()) {
326
+ out.setSpan(CssLineHeight(Math.round(lineHeight * density)), 0, out.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
327
+ }
328
+ return OrbitText(out, align, paint)
329
+ }
330
+ }
331
+ }
332
+
333
+ class OrbitTextView(context: Context) : View(context), Framed {
334
+ val frame = FloatArray(4)
335
+ private var text: OrbitText? = null
336
+ private var laid: StaticLayout? = null
337
+
338
+ fun setText(value: OrbitText) {
339
+ text = value
340
+ laid = value.layout(ceil(frame[2]).toInt())
341
+ invalidate()
342
+ }
343
+
344
+ override fun setFrame(x: Float, y: Float, w: Float, h: Float) {
345
+ val widthChanged = w != frame[2]
346
+ frame[0] = x; frame[1] = y; frame[2] = w; frame[3] = h
347
+ applyFrame(frame)
348
+ if (widthChanged) laid = text?.layout(ceil(w).toInt())
349
+ invalidate()
350
+ }
351
+
352
+ override fun setLook(background: Int, borderColor: Int, borderWidth: Float, radius: Float, opacity: Float) =
353
+ applyLook(background, borderColor, borderWidth, radius, opacity)
354
+
355
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
356
+ setMeasuredDimension(frame[2].toInt(), frame[3].toInt())
357
+ }
358
+
359
+ override fun onDraw(canvas: Canvas) {
360
+ laid?.draw(canvas)
361
+ }
362
+ }
@@ -0,0 +1,14 @@
1
+ [package]
2
+ name = "orbit-host"
3
+ version = "0.1.0"
4
+ edition.workspace = true
5
+ license.workspace = true
6
+ repository.workspace = true
7
+ description = "Orbit's native host: the tree aurora commits, laid out with taffy, diffed into platform mount operations, and UI-thread animation"
8
+
9
+ [lib]
10
+ crate-type = ["staticlib", "rlib"]
11
+
12
+ [dependencies]
13
+ taffy = "0.15"
14
+ serde_json = "1"