@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,161 @@
1
+ package com.c9up.orbit
2
+
3
+ import android.content.Context
4
+ import android.graphics.Typeface
5
+ import android.text.Editable
6
+ import android.text.InputFilter
7
+ import android.text.InputType
8
+ import android.text.TextWatcher
9
+ import android.util.TypedValue
10
+ import android.view.Gravity
11
+ import android.view.KeyEvent
12
+ import android.view.ViewGroup
13
+ import android.view.inputmethod.EditorInfo
14
+ import android.widget.EditText
15
+ import org.json.JSONObject
16
+ import kotlin.math.ceil
17
+
18
+ /**
19
+ * A text field orbit-host lays out. The field owns its text while the user
20
+ * types: each change goes to JS (`topChange`), and JS's value comes back only
21
+ * with a newer `textVersion` — so typing never waits for JS and the caret
22
+ * never jumps.
23
+ */
24
+ class OrbitInput(context: Context) : EditText(context), Framed {
25
+ val frame = FloatArray(4)
26
+ private var appliedVersion = -1
27
+ private var muted = false
28
+ private var lastJson = ""
29
+
30
+ init {
31
+ // orbit-host sends a field's props before it attaches the field to its
32
+ // parent, and TextView reads its layout params on every text change.
33
+ layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
34
+ background = null
35
+ includeFontPadding = false
36
+ // Its text scrolls inside it: clipped to its box, whatever its parent clips.
37
+ clipToOwnBox()
38
+ addTextChangedListener(object : TextWatcher {
39
+ override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
40
+ override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
41
+ override fun afterTextChanged(s: Editable?) {
42
+ if (!muted) send("topChange")
43
+ }
44
+ })
45
+ setOnFocusChangeListener { _, focused -> send(if (focused) "topFocus" else "topBlur") }
46
+ // The keyboard's action (send, done) submits once. A hardware or emulated
47
+ // Enter arrives as IME_NULL twice, key down then key up: only the down
48
+ // submits. In a multi-line field Enter is a new line.
49
+ setOnEditorActionListener { _, action, event ->
50
+ val imeAction = action != EditorInfo.IME_NULL
51
+ when {
52
+ imeAction -> { send("topSubmit"); true }
53
+ isMultiline() -> false
54
+ event?.action == KeyEvent.ACTION_DOWN -> { send("topSubmit"); true }
55
+ else -> true
56
+ }
57
+ }
58
+ }
59
+
60
+ private fun send(type: String) {
61
+ val tag = tag as? Int ?: return
62
+ OrbitHost.nativeDispatchText(tag, type, text?.toString() ?: "")
63
+ }
64
+
65
+ private fun isMultiline() = inputType and InputType.TYPE_TEXT_FLAG_MULTI_LINE != 0
66
+
67
+ /** Apply orbit-host's INPUT payload (JSON); sizes in dp. */
68
+ fun apply(json: String, density: Float) {
69
+ if (json == lastJson) return
70
+ lastJson = json
71
+ val p = JSONObject(json)
72
+ hint = p.optString("placeholder", "")
73
+ if (p.has("placeholderColor")) setHintTextColor(p.getInt("placeholderColor"))
74
+ if (p.has("color")) setTextColor(p.getInt("color"))
75
+ setTextSize(TypedValue.COMPLEX_UNIT_DIP, p.optDouble("fontSize", 14.0).toFloat())
76
+ val weight = p.optString("fontWeight", "400")
77
+ val bold = weight == "bold" || (weight.toIntOrNull() ?: 400) >= 600
78
+ val italic = p.optString("fontStyle") == "italic"
79
+ setTypeface(Typeface.DEFAULT, (if (bold) Typeface.BOLD else 0) or (if (italic) Typeface.ITALIC else 0))
80
+
81
+ val multiline = p.optBoolean("multiline", false)
82
+ var type = when (p.optString("keyboard", "text")) {
83
+ "email" -> InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
84
+ "numeric" -> InputType.TYPE_CLASS_NUMBER
85
+ "decimal" -> InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
86
+ "phone" -> InputType.TYPE_CLASS_PHONE
87
+ "url" -> InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI
88
+ else -> InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
89
+ }
90
+ if (p.optBoolean("secure", false)) type = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
91
+ if (multiline) type = type or InputType.TYPE_TEXT_FLAG_MULTI_LINE
92
+ if (inputType != type) inputType = type
93
+ val rows = p.optInt("rows", 1).coerceAtLeast(1)
94
+ if (multiline) {
95
+ minLines = rows
96
+ gravity = Gravity.TOP or Gravity.START
97
+ isSingleLine = false
98
+ } else {
99
+ isSingleLine = true
100
+ gravity = Gravity.CENTER_VERTICAL or Gravity.START
101
+ imeOptions = EditorInfo.IME_ACTION_SEND
102
+ }
103
+ val max = p.optInt("maxLength", 0)
104
+ filters = if (max > 0) arrayOf(InputFilter.LengthFilter(max)) else arrayOf()
105
+ val editable = p.optBoolean("editable", true)
106
+ isEnabled = editable
107
+ isFocusableInTouchMode = editable
108
+
109
+ val pad = p.optJSONArray("padding")
110
+ if (pad != null && pad.length() == 4) {
111
+ val px = { i: Int -> ceil(pad.getDouble(i) * density).toInt() }
112
+ setPadding(px(0), px(1), px(2), px(3))
113
+ }
114
+
115
+ val version = p.optInt("textVersion", 0)
116
+ val next = p.optString("text", "")
117
+ if (version > appliedVersion) {
118
+ appliedVersion = version
119
+ if (next != text?.toString()) {
120
+ muted = true
121
+ setValue(next)
122
+ muted = false
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * The value the app sets, kept whole as HTML keeps a value set by script:
129
+ * `maxlength` limits what the user types, so the length filter is left out
130
+ * while the value goes in. The caret goes to the end of the text the field
131
+ * holds — never past it, which Android refuses with an exception.
132
+ */
133
+ private fun setValue(value: String) {
134
+ val kept = filters
135
+ filters = arrayOf()
136
+ try {
137
+ setText(value)
138
+ } finally {
139
+ filters = kept
140
+ }
141
+ setSelection(text?.length ?: 0)
142
+ }
143
+
144
+ override fun setFrame(x: Float, y: Float, w: Float, h: Float) {
145
+ frame[0] = x; frame[1] = y; frame[2] = w; frame[3] = h
146
+ measure(
147
+ MeasureSpec.makeMeasureSpec(w.toInt(), MeasureSpec.EXACTLY),
148
+ MeasureSpec.makeMeasureSpec(h.toInt(), MeasureSpec.EXACTLY))
149
+ applyFrame(frame)
150
+ }
151
+
152
+ override fun setLook(background: Int, borderColor: Int, borderWidth: Float, radius: Float, opacity: Float) =
153
+ applyLook(background, borderColor, borderWidth, radius, opacity)
154
+
155
+ // A text field lays its text out in onMeasure: measure it at its frame.
156
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
157
+ super.onMeasure(
158
+ MeasureSpec.makeMeasureSpec(frame[2].toInt(), MeasureSpec.EXACTLY),
159
+ MeasureSpec.makeMeasureSpec(frame[3].toInt(), MeasureSpec.EXACTLY))
160
+ }
161
+ }
@@ -0,0 +1,110 @@
1
+ package com.c9up.orbit
2
+
3
+ import android.content.res.ColorStateList
4
+ import android.graphics.Color
5
+ import android.graphics.drawable.GradientDrawable
6
+ import android.graphics.drawable.RippleDrawable
7
+ import android.view.View
8
+ import java.nio.ByteBuffer
9
+ import java.nio.ByteOrder
10
+ import java.util.WeakHashMap
11
+
12
+ /**
13
+ * Applies what orbit-host's gesture recognizer (Rust, on this UI thread)
14
+ * decides: the press feedback, a ripple started here without waiting for JS;
15
+ * a gesture claimed by a view; and the events JS receives when it can.
16
+ * Command codes match crates/orbit-host/src/gesture.rs.
17
+ */
18
+ object OrbitPress {
19
+ private const val CMD_PRESSED = 1
20
+ private const val CMD_CLAIM = 2
21
+ private const val CMD_EVENT = 3
22
+
23
+ private val masks = WeakHashMap<View, GradientDrawable>()
24
+ private val pressed = intArrayOf(android.R.attr.state_pressed, android.R.attr.state_enabled)
25
+ private val idle = intArrayOf(android.R.attr.state_enabled)
26
+
27
+ /** Applies `commands`; true when a view claimed the gesture. */
28
+ fun apply(root: OrbitRoot, commands: ByteArray): Boolean {
29
+ if (commands.isEmpty()) return false
30
+ var claimed = false
31
+ val buf = ByteBuffer.wrap(commands).order(ByteOrder.LITTLE_ENDIAN)
32
+ while (buf.hasRemaining()) {
33
+ when (buf.get().toInt()) {
34
+ CMD_PRESSED -> {
35
+ val tag = buf.int
36
+ val on = buf.get().toInt() != 0
37
+ val x = buf.float
38
+ val y = buf.float
39
+ OrbitHost.viewOf(tag)?.let { setPressed(root, it, on, x, y) }
40
+ }
41
+ CMD_CLAIM -> {
42
+ buf.int
43
+ claimed = true
44
+ }
45
+ CMD_EVENT -> {
46
+ val tag = buf.int
47
+ val type = string(buf)
48
+ val data = string(buf)
49
+ OrbitHost.nativeDispatchText(tag, type, data)
50
+ }
51
+ else -> return claimed
52
+ }
53
+ }
54
+ return claimed
55
+ }
56
+
57
+ private fun string(buf: ByteBuffer): String {
58
+ val bytes = ByteArray(buf.int)
59
+ buf.get(bytes)
60
+ return String(bytes, Charsets.UTF_8)
61
+ }
62
+
63
+ /**
64
+ * The ripple: masked by the view's own rounded box (spike 4's was
65
+ * rectangular), starting where the finger is in the pressable view (spike
66
+ * 4 used the deepest view's coordinates). Light on a dark fill, dark on a
67
+ * light one, as Material does.
68
+ */
69
+ private fun setPressed(root: OrbitRoot, view: View, on: Boolean, xDp: Float, yDp: Float) {
70
+ val ripple = rippleOf(view)
71
+ if (on) {
72
+ val (left, top) = offsetIn(root.content, view)
73
+ val d = OrbitHost.density
74
+ ripple.setHotspot(xDp * d - left, yDp * d - top)
75
+ }
76
+ ripple.state = if (on) pressed else idle
77
+ }
78
+
79
+ private fun rippleOf(view: View): RippleDrawable {
80
+ val radius = OrbitEffects.radiusOf(view)
81
+ val existing = view.foreground as? RippleDrawable
82
+ if (existing != null) {
83
+ masks[view]?.cornerRadius = radius
84
+ return existing
85
+ }
86
+ val mask = GradientDrawable().apply {
87
+ setColor(Color.WHITE)
88
+ cornerRadius = radius
89
+ }
90
+ masks[view] = mask
91
+ val color = if (OrbitEffects.isDark(view)) 0x3DFFFFFF else 0x1F000000
92
+ return RippleDrawable(ColorStateList.valueOf(color), null, mask).also { view.foreground = it }
93
+ }
94
+
95
+ /** Where `view` sits in `content`, in px: offsets, translations and scrolls on the way. */
96
+ private fun offsetIn(content: View, view: View): Pair<Float, Float> {
97
+ var x = 0f
98
+ var y = 0f
99
+ var at: View = view
100
+ while (at !== content) {
101
+ x += at.left + at.translationX
102
+ y += at.top + at.translationY
103
+ val parent = at.parent as? View ?: break
104
+ x -= parent.scrollX
105
+ y -= parent.scrollY
106
+ at = parent
107
+ }
108
+ return Pair(x, y)
109
+ }
110
+ }
@@ -0,0 +1,115 @@
1
+ package com.c9up.orbit
2
+
3
+ import android.content.Context
4
+ import android.graphics.Canvas
5
+ import android.graphics.Paint
6
+ import android.graphics.Path
7
+ import android.view.View
8
+ import java.nio.ByteBuffer
9
+ import java.nio.ByteOrder
10
+ import kotlin.math.min
11
+
12
+ /**
13
+ * An `<svg>`: the paths orbit-host resolved (`vector.rs`), replayed scaled to
14
+ * the view's box — the viewBox centred and fitted (`xMidYMid meet`), or
15
+ * stretched (`none`). Paths are built once per drawing, not per frame.
16
+ */
17
+ class OrbitVector(context: Context) : View(context), Framed {
18
+ val frame = FloatArray(4)
19
+
20
+ private class Shape(val path: Path, val fill: Paint?, val stroke: Paint?)
21
+
22
+ private var shapes: List<Shape> = emptyList()
23
+ private val viewBox = FloatArray(4)
24
+ private var stretch = false
25
+
26
+ init {
27
+ importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
28
+ }
29
+
30
+ /** The vector op's payload (see `vector.rs` for the encoding). */
31
+ fun apply(bytes: ByteArray) {
32
+ val buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)
33
+ for (i in 0 until 4) viewBox[i] = buf.float
34
+ stretch = buf.get().toInt() == 1
35
+ val count = buf.int
36
+ val next = ArrayList<Shape>(count)
37
+ repeat(count) {
38
+ val fillColor = buf.int
39
+ val evenOdd = buf.get().toInt() == 1
40
+ val strokeColor = buf.int
41
+ val strokeWidth = buf.float
42
+ val cap = buf.get().toInt()
43
+ val join = buf.get().toInt()
44
+ val miter = buf.float
45
+ val crisp = buf.get().toInt() == 1
46
+ val path = Path()
47
+ path.fillType = if (evenOdd) Path.FillType.EVEN_ODD else Path.FillType.WINDING
48
+ repeat(buf.int) {
49
+ when (buf.get().toInt()) {
50
+ 0 -> path.moveTo(buf.float, buf.float)
51
+ 1 -> path.lineTo(buf.float, buf.float)
52
+ 2 -> path.cubicTo(buf.float, buf.float, buf.float, buf.float, buf.float, buf.float)
53
+ 3 -> path.quadTo(buf.float, buf.float, buf.float, buf.float)
54
+ else -> path.close()
55
+ }
56
+ }
57
+ val fill = if (fillColor == 0) null else Paint().apply {
58
+ isAntiAlias = !crisp
59
+ style = Paint.Style.FILL
60
+ color = fillColor
61
+ }
62
+ val stroke = if (strokeColor == 0 || strokeWidth <= 0f) null else Paint().apply {
63
+ isAntiAlias = !crisp
64
+ style = Paint.Style.STROKE
65
+ color = strokeColor
66
+ this.strokeWidth = strokeWidth
67
+ strokeCap = when (cap) {
68
+ 1 -> Paint.Cap.ROUND
69
+ 2 -> Paint.Cap.SQUARE
70
+ else -> Paint.Cap.BUTT
71
+ }
72
+ strokeJoin = when (join) {
73
+ 1 -> Paint.Join.ROUND
74
+ 2 -> Paint.Join.BEVEL
75
+ else -> Paint.Join.MITER
76
+ }
77
+ strokeMiter = miter
78
+ }
79
+ next.add(Shape(path, fill, stroke))
80
+ }
81
+ shapes = next
82
+ invalidate()
83
+ }
84
+
85
+ override fun setFrame(x: Float, y: Float, w: Float, h: Float) {
86
+ frame[0] = x; frame[1] = y; frame[2] = w; frame[3] = h
87
+ applyFrame(frame)
88
+ invalidate()
89
+ }
90
+
91
+ override fun setLook(background: Int, borderColor: Int, borderWidth: Float, radius: Float, opacity: Float) {
92
+ applyLook(background, borderColor, borderWidth, radius, opacity)
93
+ }
94
+
95
+ override fun onDraw(canvas: Canvas) {
96
+ val (minX, minY, vw, vh) = viewBox
97
+ if (vw <= 0f || vh <= 0f || shapes.isEmpty()) return
98
+ val w = width.toFloat()
99
+ val h = height.toFloat()
100
+ canvas.save()
101
+ if (stretch) {
102
+ canvas.scale(w / vw, h / vh)
103
+ } else {
104
+ val s = min(w / vw, h / vh)
105
+ canvas.translate((w - vw * s) / 2f, (h - vh * s) / 2f)
106
+ canvas.scale(s, s)
107
+ }
108
+ canvas.translate(-minX, -minY)
109
+ for (shape in shapes) {
110
+ shape.fill?.let { canvas.drawPath(shape.path, it) }
111
+ shape.stroke?.let { canvas.drawPath(shape.path, it) }
112
+ }
113
+ canvas.restore()
114
+ }
115
+ }