orvyn-lang 0.2.0 → 0.4.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/GUIDE.md +198 -0
- package/LANGUAGE.md +818 -30
- package/README.md +211 -250
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +207 -16
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +152 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +615 -58
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +69 -8
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +65 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +176 -7
- package/src/lang/native/android/OrvynRenderer.kt +970 -50
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/assets.js +39 -0
- package/src/lang/native/build.js +140 -63
- package/src/lang/native/compatibility.js +57 -0
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +291 -4
- package/src/lang/native/ios/OrvynViews.swift +769 -41
- package/src/lang/native/ios.js +81 -2
- package/src/lang/native-runtime.js +541 -55
- package/src/lang/native-size.js +53 -0
- package/src/lang/page-files.js +71 -0
- package/src/lang/page.js +63 -0
- package/src/lang/parser.js +278 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +12 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +796 -10
- package/src/lang/runtime.js +399 -51
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/style-check.js +60 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +317 -0
- package/src/lang/vite-plugin.js +108 -6
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/animations.js +24 -0
- package/src/runtime/appear.js +73 -0
- package/src/runtime/marquee.js +57 -0
- package/src/runtime/mount.js +120 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +50 -9
- package/src/styling/fluid.js +129 -6
- package/src/styling/spec.js +319 -0
|
@@ -5,37 +5,347 @@
|
|
|
5
5
|
|
|
6
6
|
package dev.orvyn.runtime
|
|
7
7
|
|
|
8
|
+
import android.animation.LayoutTransition
|
|
9
|
+
import android.animation.ValueAnimator
|
|
10
|
+
import android.app.DatePickerDialog
|
|
11
|
+
import android.app.Dialog
|
|
8
12
|
import android.content.Context
|
|
9
13
|
import android.content.Intent
|
|
10
|
-
import android.
|
|
14
|
+
import android.graphics.Bitmap
|
|
11
15
|
import android.graphics.BitmapFactory
|
|
16
|
+
import android.graphics.Canvas
|
|
12
17
|
import android.graphics.Color
|
|
18
|
+
import android.graphics.ColorFilter
|
|
19
|
+
import android.graphics.Outline
|
|
20
|
+
import android.graphics.Paint
|
|
21
|
+
import android.graphics.PixelFormat
|
|
22
|
+
import android.graphics.RectF
|
|
23
|
+
import android.graphics.drawable.Drawable
|
|
24
|
+
import android.graphics.drawable.LayerDrawable
|
|
13
25
|
import android.graphics.Typeface
|
|
14
26
|
import android.graphics.drawable.GradientDrawable
|
|
27
|
+
import android.graphics.drawable.StateListDrawable
|
|
28
|
+
import android.net.Uri
|
|
29
|
+
import android.util.Base64
|
|
15
30
|
import android.os.Handler
|
|
16
31
|
import android.os.Looper
|
|
17
32
|
import android.text.Editable
|
|
18
33
|
import android.text.InputType
|
|
19
34
|
import android.text.TextWatcher
|
|
35
|
+
import android.util.LruCache
|
|
20
36
|
import android.util.TypedValue
|
|
21
37
|
import android.view.Gravity
|
|
22
38
|
import android.view.View
|
|
23
39
|
import android.view.ViewGroup
|
|
40
|
+
import android.view.ViewOutlineProvider
|
|
41
|
+
import android.webkit.WebView
|
|
42
|
+
import android.webkit.WebViewClient
|
|
43
|
+
import android.widget.AbsListView
|
|
44
|
+
import android.widget.AdapterView
|
|
45
|
+
import android.widget.ArrayAdapter
|
|
46
|
+
import android.widget.CheckBox
|
|
47
|
+
import android.widget.CompoundButton
|
|
48
|
+
import android.widget.Spinner
|
|
49
|
+
import android.widget.Switch
|
|
50
|
+
import android.widget.BaseAdapter
|
|
51
|
+
import android.widget.Button
|
|
24
52
|
import android.widget.EditText
|
|
53
|
+
import android.widget.FrameLayout
|
|
54
|
+
import android.widget.ListView
|
|
55
|
+
import android.widget.HorizontalScrollView
|
|
25
56
|
import android.widget.ImageView
|
|
26
57
|
import android.widget.LinearLayout
|
|
58
|
+
import android.widget.ScrollView
|
|
27
59
|
import android.widget.TextView
|
|
60
|
+
import org.json.JSONArray
|
|
28
61
|
import org.json.JSONObject
|
|
29
62
|
import java.net.URL
|
|
63
|
+
import java.util.Calendar
|
|
64
|
+
import java.util.Locale
|
|
30
65
|
import kotlin.concurrent.thread
|
|
66
|
+
import kotlin.math.min
|
|
31
67
|
|
|
32
|
-
private class Info(val type: String) {
|
|
68
|
+
private class Info(val type: String, val wrapped: Boolean = false) {
|
|
33
69
|
var tap: Int? = null
|
|
34
70
|
var change: Int? = null
|
|
35
71
|
var value: String? = null
|
|
36
72
|
var src: String? = null
|
|
37
73
|
var href: String? = null
|
|
38
74
|
var updating = false
|
|
75
|
+
var disabled = false
|
|
76
|
+
var loading = false
|
|
77
|
+
var appeared = false
|
|
78
|
+
var appearUntil = 0L
|
|
79
|
+
var bgColor: Int? = null
|
|
80
|
+
var abs: JSONObject? = null
|
|
81
|
+
var bgImage: String? = null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// position: "absolute" children are taken out of the flow and placed by
|
|
85
|
+
// top / left / right / bottom (points, or "40%" of the parent), like the web
|
|
86
|
+
private fun absoluteSpec(view: View): JSONObject? = (view.tag as? Info)?.abs
|
|
87
|
+
|
|
88
|
+
private fun edge(spec: JSONObject, key: String, size: Int, density: Float): Int? {
|
|
89
|
+
val value = spec.opt(key) ?: return null
|
|
90
|
+
return when (value) {
|
|
91
|
+
is Number -> (value.toFloat() * density).toInt()
|
|
92
|
+
is String -> {
|
|
93
|
+
val text = value.trim()
|
|
94
|
+
val amount = text.removeSuffix("%").toFloatOrNull() ?: return null
|
|
95
|
+
if (text.endsWith("%")) (size * amount / 100f).toInt() else (amount * density).toInt()
|
|
96
|
+
}
|
|
97
|
+
else -> null
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private fun measureAbsolute(parent: ViewGroup, width: Int) {
|
|
102
|
+
for (i in 0 until parent.childCount) {
|
|
103
|
+
val child = parent.getChildAt(i)
|
|
104
|
+
if (absoluteSpec(child) == null || child.visibility == View.GONE) continue
|
|
105
|
+
val lp = child.layoutParams
|
|
106
|
+
val ws = if (lp.width > 0) View.MeasureSpec.makeMeasureSpec(lp.width, View.MeasureSpec.EXACTLY)
|
|
107
|
+
else View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.AT_MOST)
|
|
108
|
+
val hs = if (lp.height > 0) View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY)
|
|
109
|
+
else View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
|
|
110
|
+
child.measure(ws, hs)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private fun placeAbsolute(parent: ViewGroup, width: Int, height: Int) {
|
|
115
|
+
val density = parent.resources.displayMetrics.density
|
|
116
|
+
for (i in 0 until parent.childCount) {
|
|
117
|
+
val child = parent.getChildAt(i)
|
|
118
|
+
val spec = absoluteSpec(child) ?: continue
|
|
119
|
+
if (child.visibility == View.GONE) continue
|
|
120
|
+
val cw = child.measuredWidth
|
|
121
|
+
val ch = child.measuredHeight
|
|
122
|
+
val x = edge(spec, "left", width, density) ?: edge(spec, "right", width, density)?.let { width - it - cw } ?: parent.paddingLeft
|
|
123
|
+
val y = edge(spec, "top", height, density) ?: edge(spec, "bottom", height, density)?.let { height - it - ch } ?: parent.paddingTop
|
|
124
|
+
child.layout(x, y, x + cw, y + ch)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// justify: between / around / evenly: where the first item starts and the
|
|
129
|
+
// extra space after each item
|
|
130
|
+
private fun spreadSpace(justify: String, free: Int, count: Int): Pair<Float, Float>? {
|
|
131
|
+
if (free <= 0 || count == 0) return null
|
|
132
|
+
return when (justify.removePrefix("space-")) {
|
|
133
|
+
"between" -> if (count == 1) 0f to 0f else 0f to free.toFloat() / (count - 1)
|
|
134
|
+
"around" -> free / (2f * count) to free.toFloat() / count
|
|
135
|
+
"evenly" -> free / (count + 1f) to free / (count + 1f)
|
|
136
|
+
else -> null
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// A background photo drawn to cover the box (like CSS background-size: cover),
|
|
141
|
+
// darkened by `dim` so text on top stays readable
|
|
142
|
+
private class CoverDrawable(private val bitmap: Bitmap, private val dim: Float) : Drawable() {
|
|
143
|
+
private val paint = Paint(Paint.FILTER_BITMAP_FLAG or Paint.ANTI_ALIAS_FLAG)
|
|
144
|
+
override fun draw(canvas: Canvas) {
|
|
145
|
+
val b = bounds
|
|
146
|
+
if (b.isEmpty || bitmap.width == 0 || bitmap.height == 0) return
|
|
147
|
+
val scale = maxOf(b.width() / bitmap.width.toFloat(), b.height() / bitmap.height.toFloat())
|
|
148
|
+
val w = bitmap.width * scale
|
|
149
|
+
val h = bitmap.height * scale
|
|
150
|
+
val left = b.left + (b.width() - w) / 2
|
|
151
|
+
val top = b.top + (b.height() - h) / 2
|
|
152
|
+
canvas.save()
|
|
153
|
+
canvas.clipRect(b)
|
|
154
|
+
canvas.drawBitmap(bitmap, null, RectF(left, top, left + w, top + h), paint)
|
|
155
|
+
if (dim > 0f) canvas.drawColor(Color.argb((dim * 255).toInt().coerceIn(0, 255), 0, 0, 0))
|
|
156
|
+
canvas.restore()
|
|
157
|
+
}
|
|
158
|
+
override fun setAlpha(alpha: Int) { paint.alpha = alpha }
|
|
159
|
+
override fun setColorFilter(colorFilter: ColorFilter?) { paint.colorFilter = colorFilter }
|
|
160
|
+
@Deprecated("Deprecated in Java")
|
|
161
|
+
override fun getOpacity() = PixelFormat.TRANSLUCENT
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// marquee: "left 30s": the row keeps its natural width and slides in an
|
|
165
|
+
// endless loop. The row is drawn again right after itself, so there is no gap.
|
|
166
|
+
private class MarqueeLayout(context: Context) : ViewGroup(context) {
|
|
167
|
+
var seconds = 30f
|
|
168
|
+
var toRight = false
|
|
169
|
+
private var offset = 0f
|
|
170
|
+
private var animator: ValueAnimator? = null
|
|
171
|
+
|
|
172
|
+
init { setWillNotDraw(false) }
|
|
173
|
+
|
|
174
|
+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
175
|
+
val child = getChildAt(0)
|
|
176
|
+
child?.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), heightMeasureSpec)
|
|
177
|
+
val width = MeasureSpec.getSize(widthMeasureSpec)
|
|
178
|
+
setMeasuredDimension(width, resolveSize(child?.measuredHeight ?: 0, heightMeasureSpec))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
|
|
182
|
+
val child = getChildAt(0) ?: return
|
|
183
|
+
child.layout(0, 0, child.measuredWidth, child.measuredHeight)
|
|
184
|
+
restart(child.measuredWidth)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private fun restart(span: Int) {
|
|
188
|
+
val running = animator
|
|
189
|
+
// keep sliding smoothly across re-renders unless the width or speed changed
|
|
190
|
+
if (running != null && running.isRunning && running.duration == (seconds * 1000).toLong() && lastSpan == span) return
|
|
191
|
+
running?.cancel()
|
|
192
|
+
lastSpan = span
|
|
193
|
+
if (span <= 0) return
|
|
194
|
+
animator = ValueAnimator.ofFloat(0f, span.toFloat()).apply {
|
|
195
|
+
duration = (seconds * 1000).toLong()
|
|
196
|
+
repeatCount = ValueAnimator.INFINITE
|
|
197
|
+
interpolator = android.view.animation.LinearInterpolator()
|
|
198
|
+
addUpdateListener {
|
|
199
|
+
offset = it.animatedValue as Float
|
|
200
|
+
invalidate()
|
|
201
|
+
}
|
|
202
|
+
start()
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
private var lastSpan = -1
|
|
206
|
+
|
|
207
|
+
override fun onDetachedFromWindow() {
|
|
208
|
+
super.onDetachedFromWindow()
|
|
209
|
+
animator?.cancel()
|
|
210
|
+
animator = null
|
|
211
|
+
lastSpan = -1
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
override fun dispatchDraw(canvas: Canvas) {
|
|
215
|
+
val child = getChildAt(0) ?: return
|
|
216
|
+
val span = child.measuredWidth
|
|
217
|
+
if (span <= 0) return
|
|
218
|
+
val start = if (toRight) offset - span else -offset
|
|
219
|
+
canvas.save()
|
|
220
|
+
canvas.translate(start, 0f)
|
|
221
|
+
var drawn = start
|
|
222
|
+
while (drawn < width) {
|
|
223
|
+
drawChild(canvas, child, drawingTime)
|
|
224
|
+
canvas.translate(span.toFloat(), 0f)
|
|
225
|
+
drawn += span
|
|
226
|
+
}
|
|
227
|
+
canvas.restore()
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Rows and columns: LinearLayout plus justify: between and absolute children
|
|
232
|
+
private class FlowLayout(context: Context) : LinearLayout(context) {
|
|
233
|
+
var justify = ""
|
|
234
|
+
|
|
235
|
+
// like the web, a child placed at top: -10 can stick out of its box
|
|
236
|
+
init { clipChildren = false; clipToPadding = false }
|
|
237
|
+
|
|
238
|
+
// absolute children are detached while LinearLayout measures and places
|
|
239
|
+
// the rest, so they take no space (detaching does not ask for a new layout)
|
|
240
|
+
private fun withoutAbsolute(block: () -> Unit) {
|
|
241
|
+
val hidden = (0 until childCount).map { it to getChildAt(it) }.filter { absoluteSpec(it.second) != null }
|
|
242
|
+
for ((index, _) in hidden.reversed()) detachViewFromParent(index)
|
|
243
|
+
try {
|
|
244
|
+
block()
|
|
245
|
+
} finally {
|
|
246
|
+
for ((index, child) in hidden) attachViewToParent(child, index, child.layoutParams)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
251
|
+
withoutAbsolute { super.onMeasure(widthMeasureSpec, heightMeasureSpec) }
|
|
252
|
+
measureAbsolute(this, measuredWidth)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
|
|
256
|
+
withoutAbsolute {
|
|
257
|
+
super.onLayout(changed, l, t, r, b)
|
|
258
|
+
spread()
|
|
259
|
+
}
|
|
260
|
+
placeAbsolute(this, r - l, b - t)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private fun spread() {
|
|
264
|
+
val kids = (0 until childCount).map { getChildAt(it) }.filter { it.visibility != View.GONE }
|
|
265
|
+
val horizontal = orientation == HORIZONTAL
|
|
266
|
+
val inner = if (horizontal) width - paddingLeft - paddingRight else height - paddingTop - paddingBottom
|
|
267
|
+
val used = kids.sumOf {
|
|
268
|
+
val lp = it.layoutParams as MarginLayoutParams
|
|
269
|
+
if (horizontal) it.measuredWidth + lp.leftMargin + lp.rightMargin else it.measuredHeight + lp.topMargin + lp.bottomMargin
|
|
270
|
+
}
|
|
271
|
+
val (lead, step) = spreadSpace(justify, inner - used, kids.size) ?: return
|
|
272
|
+
kids.forEachIndexed { i, child ->
|
|
273
|
+
val offset = (lead + step * i).toInt()
|
|
274
|
+
if (horizontal) child.offsetLeftAndRight(offset) else child.offsetTopAndBottom(offset)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// `Row wrap: true` behaves like CSS flex-wrap, with justify and align per line
|
|
280
|
+
private class WrapLayout(context: Context) : ViewGroup(context) {
|
|
281
|
+
var gapPx: Int = 0
|
|
282
|
+
var justify = ""
|
|
283
|
+
var align = ""
|
|
284
|
+
|
|
285
|
+
init { clipChildren = false; clipToPadding = false }
|
|
286
|
+
|
|
287
|
+
private fun flowChildren() = (0 until childCount).map { getChildAt(it) }
|
|
288
|
+
.filter { it.visibility != View.GONE && absoluteSpec(it) == null }
|
|
289
|
+
|
|
290
|
+
private fun lines(available: Int): List<List<View>> {
|
|
291
|
+
val result = mutableListOf<MutableList<View>>()
|
|
292
|
+
var x = 0
|
|
293
|
+
for (child in flowChildren()) {
|
|
294
|
+
val cw = child.measuredWidth
|
|
295
|
+
if (result.isEmpty() || (x > 0 && x + cw > available)) {
|
|
296
|
+
result.add(mutableListOf())
|
|
297
|
+
x = 0
|
|
298
|
+
}
|
|
299
|
+
result.last().add(child)
|
|
300
|
+
x += cw + gapPx
|
|
301
|
+
}
|
|
302
|
+
return result
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
306
|
+
val maxWidth = MeasureSpec.getSize(widthMeasureSpec)
|
|
307
|
+
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
|
|
308
|
+
val available = if (widthMode == MeasureSpec.UNSPECIFIED) Int.MAX_VALUE else (maxWidth - paddingLeft - paddingRight).coerceAtLeast(0)
|
|
309
|
+
for (child in flowChildren()) measureChild(child, widthMeasureSpec, heightMeasureSpec)
|
|
310
|
+
|
|
311
|
+
val rows = lines(available)
|
|
312
|
+
val widest = rows.maxOfOrNull { row -> row.sumOf { it.measuredWidth } + gapPx * (row.size - 1) } ?: 0
|
|
313
|
+
val tall = rows.sumOf { row -> row.maxOf { it.measuredHeight } } + gapPx * (rows.size - 1).coerceAtLeast(0)
|
|
314
|
+
val desiredWidth = if (widthMode == MeasureSpec.EXACTLY) maxWidth else widest + paddingLeft + paddingRight
|
|
315
|
+
setMeasuredDimension(desiredWidth, resolveSize(tall + paddingTop + paddingBottom, heightMeasureSpec))
|
|
316
|
+
measureAbsolute(this, measuredWidth)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
|
|
320
|
+
val available = (r - l - paddingLeft - paddingRight).coerceAtLeast(0)
|
|
321
|
+
var y = paddingTop
|
|
322
|
+
for (row in lines(available)) {
|
|
323
|
+
val rowWidth = row.sumOf { it.measuredWidth } + gapPx * (row.size - 1)
|
|
324
|
+
val rowHeight = row.maxOf { it.measuredHeight }
|
|
325
|
+
val free = available - rowWidth
|
|
326
|
+
val (lead, extra) = spreadSpace(justify, free, row.size) ?: when (justify) {
|
|
327
|
+
"center" -> (free / 2f).coerceAtLeast(0f) to 0f
|
|
328
|
+
"end", "flex-end" -> free.toFloat().coerceAtLeast(0f) to 0f
|
|
329
|
+
else -> 0f to 0f
|
|
330
|
+
}
|
|
331
|
+
// margin: "0 auto" on an item alone on its line centres it
|
|
332
|
+
val centredAlone = row.size == 1 && (row[0].layoutParams as? LinearLayout.LayoutParams)?.gravity == Gravity.CENTER_HORIZONTAL
|
|
333
|
+
var x = paddingLeft + if (centredAlone) (free / 2f).coerceAtLeast(0f) else lead
|
|
334
|
+
for (child in row) {
|
|
335
|
+
val cw = child.measuredWidth
|
|
336
|
+
val ch = child.measuredHeight
|
|
337
|
+
val dy = when (align) {
|
|
338
|
+
"end", "flex-end", "bottom", "baseline" -> rowHeight - ch
|
|
339
|
+
"center" -> (rowHeight - ch) / 2
|
|
340
|
+
else -> 0
|
|
341
|
+
}
|
|
342
|
+
child.layout(x.toInt(), y + dy, x.toInt() + cw, y + dy + ch)
|
|
343
|
+
x += cw + gapPx + extra
|
|
344
|
+
}
|
|
345
|
+
y += rowHeight + gapPx
|
|
346
|
+
}
|
|
347
|
+
placeAbsolute(this, r - l, b - t)
|
|
348
|
+
}
|
|
39
349
|
}
|
|
40
350
|
|
|
41
351
|
class OrvynRenderer(
|
|
@@ -44,44 +354,288 @@ class OrvynRenderer(
|
|
|
44
354
|
) {
|
|
45
355
|
private val density = context.resources.displayMetrics.density
|
|
46
356
|
private val main = Handler(Looper.getMainLooper())
|
|
47
|
-
private
|
|
357
|
+
private var accent = Color.parseColor("#1A73E8")
|
|
358
|
+
private var defaultTextColor = Color.BLACK
|
|
359
|
+
private var defaultBorderColor = Color.parseColor("#CBD5E1")
|
|
360
|
+
private var dangerColor = Color.parseColor("#B91C1C")
|
|
48
361
|
|
|
49
|
-
|
|
362
|
+
fun dp(value: Float): Int = (value * density).toInt()
|
|
50
363
|
|
|
51
|
-
fun
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
364
|
+
fun setTheme(theme: JSONObject?) {
|
|
365
|
+
if (theme == null) return
|
|
366
|
+
color(theme.opt("primary"))?.let { accent = it }
|
|
367
|
+
color(theme.opt("text"))?.let { defaultTextColor = it }
|
|
368
|
+
color(theme.opt("border"))?.let { defaultBorderColor = it }
|
|
369
|
+
color(theme.opt("danger"))?.let { dangerColor = it }
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
fun render(
|
|
373
|
+
node: JSONObject,
|
|
374
|
+
existing: View?,
|
|
375
|
+
parentType: String,
|
|
376
|
+
availableWidthPx: Int = context.resources.displayMetrics.widthPixels
|
|
377
|
+
): View {
|
|
378
|
+
var type = node.optString("t", "box")
|
|
379
|
+
if (type == "toggle") type = "toggle-" + node.optString("inputType", "checkbox")
|
|
55
380
|
val style = node.optJSONObject("s") ?: JSONObject()
|
|
381
|
+
if (type == "modal") return renderModal(node, existing, availableWidthPx)
|
|
382
|
+
if (type == "input" && node.optString("inputType") == "date") return renderDate(node, existing)
|
|
383
|
+
if (type == "map") return renderMap(node, style, existing, parentType, availableWidthPx)
|
|
384
|
+
if (type == "list") return renderList(node, style, existing, parentType, availableWidthPx)
|
|
385
|
+
val wrapped = type == "row" && style.optBoolean("wrap", false)
|
|
386
|
+
val scrollVal = style.opt("scroll")
|
|
387
|
+
val scrollMode = when {
|
|
388
|
+
scrollVal == true || scrollVal == "vertical" -> if (type == "row") "horizontal" else "vertical"
|
|
389
|
+
scrollVal == "horizontal" -> "horizontal"
|
|
390
|
+
scrollVal == "both" -> if (type == "row") "horizontal" else "vertical"
|
|
391
|
+
else -> null
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
val marqueeSpec = if (type == "row" && !wrapped) style.optString("marquee", "").trim() else ""
|
|
395
|
+
val innerExisting = when {
|
|
396
|
+
existing is MarqueeLayout -> if (marqueeSpec.isNotEmpty() && existing.childCount > 0) existing.getChildAt(0) else null
|
|
397
|
+
marqueeSpec.isNotEmpty() -> null
|
|
398
|
+
scrollMode == "vertical" && existing is ScrollView && existing.childCount > 0 -> existing.getChildAt(0)
|
|
399
|
+
scrollMode == "horizontal" && existing is HorizontalScrollView && existing.childCount > 0 -> existing.getChildAt(0)
|
|
400
|
+
existing is ScrollView || existing is HorizontalScrollView -> null
|
|
401
|
+
else -> existing
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
val existingInfo = innerExisting?.tag as? Info
|
|
405
|
+
val view = if (innerExisting != null && existingInfo?.type == type && existingInfo.wrapped == wrapped) {
|
|
406
|
+
innerExisting
|
|
407
|
+
} else {
|
|
408
|
+
create(type, wrapped)
|
|
409
|
+
}
|
|
410
|
+
val info = view.tag as Info
|
|
56
411
|
|
|
57
412
|
val events = node.optJSONObject("e")
|
|
58
413
|
info.tap = if (events?.has("tap") == true) events.getInt("tap") else null
|
|
59
414
|
info.change = if (events?.has("change") == true) events.getInt("change") else null
|
|
60
415
|
info.href = if (node.has("href")) node.optString("href") else null
|
|
416
|
+
info.disabled = style.optBoolean("disabled", false)
|
|
417
|
+
info.loading = style.optBoolean("loading", false)
|
|
418
|
+
|
|
419
|
+
if (style.has("rtl")) {
|
|
420
|
+
view.layoutDirection = if (style.optBoolean("rtl", false)) View.LAYOUT_DIRECTION_RTL else View.LAYOUT_DIRECTION_LTR
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// label: is what TalkBack says; label: "" hides a decorative element
|
|
424
|
+
val label = if (node.has("a")) node.optString("a") else null
|
|
425
|
+
view.contentDescription = label?.ifEmpty { null }
|
|
426
|
+
view.importantForAccessibility =
|
|
427
|
+
if (label == "") View.IMPORTANT_FOR_ACCESSIBILITY_NO else View.IMPORTANT_FOR_ACCESSIBILITY_AUTO
|
|
61
428
|
|
|
62
429
|
applyBox(view, type, style)
|
|
63
|
-
|
|
430
|
+
// overflow: "hidden" clips children; otherwise they may stick out, like the web
|
|
431
|
+
if (view is FlowLayout || view is WrapLayout) {
|
|
432
|
+
val hidden = style.optString("overflow") == "hidden"
|
|
433
|
+
(view as ViewGroup).clipChildren = hidden
|
|
434
|
+
view.clipToPadding = hidden
|
|
435
|
+
if (hidden) view.clipToOutline = true
|
|
436
|
+
}
|
|
437
|
+
info.abs = if (style.optString("position") == "absolute") JSONObject().apply {
|
|
438
|
+
for (side in listOf("top", "left", "right", "bottom")) style.opt(side)?.let { put(side, it) }
|
|
439
|
+
} else null
|
|
440
|
+
// zIndex lifts absolute decorations above the content they float over
|
|
441
|
+
view.translationZ = if (info.abs != null) dp(number(style.opt("zIndex")) ?: 0f).toFloat() else 0f
|
|
442
|
+
playAppear(view, info, style, events?.let { if (it.has("visible")) it.getInt("visible") else null })
|
|
443
|
+
val params = layoutParams(type, style, parentType, availableWidthPx)
|
|
444
|
+
// the screen, scroll areas and pop-ups are FrameLayouts and crash when
|
|
445
|
+
// given another container's layout settings on the next update
|
|
446
|
+
val before = view.layoutParams
|
|
447
|
+
view.layoutParams = if (parentType == "root" || scrollMode != null) {
|
|
448
|
+
FrameLayout.LayoutParams(params as ViewGroup.MarginLayoutParams)
|
|
449
|
+
} else params
|
|
450
|
+
animateSize(view, before, animationMs(style))
|
|
451
|
+
|
|
452
|
+
val maxW = number(style.opt("maxWidth"))?.let { dp(it) }
|
|
453
|
+
val resolvedWidth = when {
|
|
454
|
+
params.width > 0 -> params.width
|
|
455
|
+
maxW != null -> min(availableWidthPx, maxW)
|
|
456
|
+
else -> availableWidthPx
|
|
457
|
+
}
|
|
458
|
+
val px = dp(number(style.opt("paddingX")) ?: number(style.opt("padding")) ?: 0f)
|
|
459
|
+
val childWidth = (resolvedWidth - px * 2).coerceAtLeast(0)
|
|
64
460
|
|
|
65
461
|
when (view) {
|
|
462
|
+
// CheckBox and Switch are TextViews too, so they come first
|
|
463
|
+
is CompoundButton -> updateToggle(view, info, node, style)
|
|
464
|
+
is Spinner -> updateSelect(view, info, node)
|
|
66
465
|
is EditText -> updateInput(view, info, node, style)
|
|
67
|
-
is TextView -> updateText(view, type, node, style)
|
|
466
|
+
is TextView -> updateText(view, info, type, node, style)
|
|
68
467
|
is ImageView -> updateImage(view, info, node)
|
|
69
|
-
is
|
|
468
|
+
is WrapLayout -> updateWrapChildren(view, type, node, style, childWidth)
|
|
469
|
+
is LinearLayout -> updateChildren(view, type, node, style, childWidth)
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (marqueeSpec.isNotEmpty()) {
|
|
473
|
+
val box = (existing as? MarqueeLayout) ?: MarqueeLayout(context)
|
|
474
|
+
val parts = marqueeSpec.split(" ")
|
|
475
|
+
box.toRight = parts[0] == "right"
|
|
476
|
+
box.seconds = parts.getOrNull(1)?.removeSuffix("s")?.toFloatOrNull() ?: 30f
|
|
477
|
+
box.layoutParams = view.layoutParams
|
|
478
|
+
view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
|
479
|
+
if (view.parent !== box) {
|
|
480
|
+
(view.parent as? ViewGroup)?.removeView(view)
|
|
481
|
+
box.removeAllViews()
|
|
482
|
+
box.addView(view)
|
|
483
|
+
}
|
|
484
|
+
box.tag = Info("marquee")
|
|
485
|
+
return box
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (scrollMode == "horizontal") {
|
|
489
|
+
val scroller = (existing as? HorizontalScrollView) ?: HorizontalScrollView(context)
|
|
490
|
+
if (view.parent !== scroller) {
|
|
491
|
+
scroller.removeAllViews()
|
|
492
|
+
scroller.addView(view)
|
|
493
|
+
}
|
|
494
|
+
return scroller
|
|
495
|
+
}
|
|
496
|
+
if (scrollMode == "vertical") {
|
|
497
|
+
val scroller = (existing as? ScrollView) ?: ScrollView(context)
|
|
498
|
+
if (view.parent !== scroller) {
|
|
499
|
+
scroller.removeAllViews()
|
|
500
|
+
scroller.addView(view)
|
|
501
|
+
}
|
|
502
|
+
return scroller
|
|
70
503
|
}
|
|
71
504
|
return view
|
|
72
505
|
}
|
|
73
506
|
|
|
507
|
+
// ------------------------------------------------------- pop-ups and dates
|
|
508
|
+
|
|
509
|
+
private class Popup(var dialog: Dialog, var close: Int? = null, var showing: Boolean = true)
|
|
510
|
+
|
|
511
|
+
// an invisible view in the layout owns the dialog; removing it closes it
|
|
512
|
+
private fun renderModal(node: JSONObject, existing: View?, widthPx: Int): View {
|
|
513
|
+
val anchor = existing?.takeIf { it.tag is Popup } ?: View(context).also { view ->
|
|
514
|
+
val popup = Popup(Dialog(context))
|
|
515
|
+
view.tag = popup
|
|
516
|
+
popup.dialog.setOnCancelListener { popup.close?.let { fire(it, null) } }
|
|
517
|
+
view.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
|
|
518
|
+
override fun onViewAttachedToWindow(v: View) {
|
|
519
|
+
popup.dialog.show()
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
override fun onViewDetachedFromWindow(v: View) {
|
|
523
|
+
popup.dialog.dismiss()
|
|
524
|
+
}
|
|
525
|
+
})
|
|
526
|
+
}
|
|
527
|
+
val popup = anchor.tag as Popup
|
|
528
|
+
popup.close = node.optJSONObject("e")?.let { if (it.has("tap")) it.getInt("tap") else null }
|
|
529
|
+
val card = node.optJSONArray("c")?.optJSONObject(0) ?: JSONObject()
|
|
530
|
+
val holder = popup.dialog.findViewById<ViewGroup>(android.R.id.content)
|
|
531
|
+
val old = holder?.getChildAt(0)
|
|
532
|
+
val view = render(card, old, "root", (widthPx * 0.9f).toInt())
|
|
533
|
+
if (view !== old) popup.dialog.setContentView(view)
|
|
534
|
+
anchor.layoutParams = layoutParams("box", JSONObject(), "col", 0)
|
|
535
|
+
return anchor
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// a button that opens the system date picker; the value is "2000-01-31"
|
|
539
|
+
private fun renderDate(node: JSONObject, existing: View?): View {
|
|
540
|
+
val button = (existing as? Button) ?: Button(context)
|
|
541
|
+
val value = node.optString("v", "")
|
|
542
|
+
val change = node.optJSONObject("e")?.let { if (it.has("change")) it.getInt("change") else null }
|
|
543
|
+
button.text = value.ifEmpty { node.optString("ph", "Choose a date") }
|
|
544
|
+
button.layoutParams = layoutParams("input", JSONObject(), "col", context.resources.displayMetrics.widthPixels)
|
|
545
|
+
button.setOnClickListener {
|
|
546
|
+
val parts = value.split("-").mapNotNull { it.toIntOrNull() }
|
|
547
|
+
val now = Calendar.getInstance()
|
|
548
|
+
DatePickerDialog(
|
|
549
|
+
context,
|
|
550
|
+
{ _, year, month, day -> change?.let { fire(it, "%04d-%02d-%02d".format(Locale.US, year, month + 1, day)) } },
|
|
551
|
+
parts.getOrNull(0) ?: now.get(Calendar.YEAR),
|
|
552
|
+
parts.getOrNull(1)?.minus(1) ?: now.get(Calendar.MONTH),
|
|
553
|
+
parts.getOrNull(2) ?: now.get(Calendar.DAY_OF_MONTH)
|
|
554
|
+
).show()
|
|
555
|
+
}
|
|
556
|
+
return button
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// -------------------------------------------------------------------- maps
|
|
560
|
+
|
|
561
|
+
// the same OpenStreetMap page as the web, so no map key is needed
|
|
562
|
+
private fun renderMap(node: JSONObject, style: JSONObject, existing: View?, parentType: String, widthPx: Int): View {
|
|
563
|
+
val map = (existing as? WebView) ?: WebView(context).apply {
|
|
564
|
+
settings.javaScriptEnabled = true
|
|
565
|
+
webViewClient = WebViewClient()
|
|
566
|
+
}
|
|
567
|
+
val url = node.optString("src")
|
|
568
|
+
if (map.tag != url) {
|
|
569
|
+
map.tag = url
|
|
570
|
+
map.loadUrl(url)
|
|
571
|
+
}
|
|
572
|
+
map.contentDescription = node.optString("a")
|
|
573
|
+
val params = layoutParams("col", style, parentType, widthPx)
|
|
574
|
+
params.height = dp(number(style.opt("height")) ?: 240f)
|
|
575
|
+
map.layoutParams = if (parentType == "root") FrameLayout.LayoutParams(params as ViewGroup.MarginLayoutParams) else params
|
|
576
|
+
return map
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// ------------------------------------------------------------------- lists
|
|
580
|
+
|
|
581
|
+
// ListView recycles row views, so only the rows on screen exist
|
|
582
|
+
private class Rows(val renderer: OrvynRenderer) : BaseAdapter() {
|
|
583
|
+
var rows: JSONArray = JSONArray()
|
|
584
|
+
var rowHeight = 48f
|
|
585
|
+
var widthPx = 0
|
|
586
|
+
|
|
587
|
+
override fun getCount() = rows.length()
|
|
588
|
+
override fun getItem(position: Int): Any = rows.getJSONObject(position)
|
|
589
|
+
override fun getItemId(position: Int) = position.toLong()
|
|
590
|
+
|
|
591
|
+
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
|
592
|
+
val holder = (convertView as? LinearLayout) ?: LinearLayout(parent.context).apply {
|
|
593
|
+
orientation = LinearLayout.VERTICAL
|
|
594
|
+
}
|
|
595
|
+
holder.layoutParams = AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, renderer.dp(rowHeight))
|
|
596
|
+
val existing = if (holder.childCount > 0) holder.getChildAt(0) else null
|
|
597
|
+
val child = renderer.render(rows.getJSONObject(position), existing, "col", widthPx)
|
|
598
|
+
if (child !== existing) {
|
|
599
|
+
holder.removeAllViews()
|
|
600
|
+
holder.addView(child)
|
|
601
|
+
}
|
|
602
|
+
return holder
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
private fun renderList(node: JSONObject, style: JSONObject, existing: View?, parentType: String, widthPx: Int): View {
|
|
607
|
+
val list = (existing as? ListView) ?: ListView(context).apply {
|
|
608
|
+
divider = null
|
|
609
|
+
adapter = Rows(this@OrvynRenderer)
|
|
610
|
+
// the app's own scroll view must not steal the swipe
|
|
611
|
+
setOnTouchListener { view, _ ->
|
|
612
|
+
view.parent?.requestDisallowInterceptTouchEvent(true)
|
|
613
|
+
false
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
val rows = list.adapter as Rows
|
|
617
|
+
rows.rows = node.optJSONArray("c") ?: JSONArray()
|
|
618
|
+
rows.rowHeight = node.optDouble("rh", 48.0).toFloat()
|
|
619
|
+
rows.widthPx = widthPx
|
|
620
|
+
val params = layoutParams("col", style, parentType, widthPx)
|
|
621
|
+
params.height = dp(number(style.opt("height")) ?: 480f)
|
|
622
|
+
list.layoutParams = params
|
|
623
|
+
rows.notifyDataSetChanged()
|
|
624
|
+
return list
|
|
625
|
+
}
|
|
626
|
+
|
|
74
627
|
// ------------------------------------------------------------------ create
|
|
75
628
|
|
|
76
|
-
private fun create(type: String): View {
|
|
77
|
-
val info = Info(type)
|
|
78
|
-
val view: View = when
|
|
79
|
-
|
|
629
|
+
private fun create(type: String, wrapped: Boolean): View {
|
|
630
|
+
val info = Info(type, wrapped)
|
|
631
|
+
val view: View = when {
|
|
632
|
+
wrapped -> WrapLayout(context)
|
|
633
|
+
type == "row" -> FlowLayout(context).apply {
|
|
80
634
|
orientation = LinearLayout.HORIZONTAL
|
|
81
635
|
gravity = Gravity.CENTER_VERTICAL
|
|
82
636
|
}
|
|
83
|
-
"col"
|
|
84
|
-
"input" -> EditText(context).apply {
|
|
637
|
+
type == "col" || type == "box" -> FlowLayout(context).apply { orientation = LinearLayout.VERTICAL }
|
|
638
|
+
type == "input" -> EditText(context).apply {
|
|
85
639
|
setSingleLine(true)
|
|
86
640
|
addTextChangedListener(object : TextWatcher {
|
|
87
641
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
|
@@ -94,7 +648,10 @@ class OrvynRenderer(
|
|
|
94
648
|
}
|
|
95
649
|
})
|
|
96
650
|
}
|
|
97
|
-
"
|
|
651
|
+
type == "toggle-checkbox" -> CheckBox(context)
|
|
652
|
+
type == "toggle-switch" -> Switch(context)
|
|
653
|
+
type == "select" -> Spinner(context)
|
|
654
|
+
type == "image" -> ImageView(context).apply {
|
|
98
655
|
adjustViewBounds = true
|
|
99
656
|
scaleType = ImageView.ScaleType.FIT_CENTER
|
|
100
657
|
}
|
|
@@ -103,6 +660,7 @@ class OrvynRenderer(
|
|
|
103
660
|
if (type == "button" || type == "link") {
|
|
104
661
|
view.isClickable = true
|
|
105
662
|
view.setOnClickListener {
|
|
663
|
+
if (info.disabled || info.loading) return@setOnClickListener
|
|
106
664
|
val tap = info.tap
|
|
107
665
|
val href = info.href
|
|
108
666
|
if (tap != null) {
|
|
@@ -118,8 +676,9 @@ class OrvynRenderer(
|
|
|
118
676
|
|
|
119
677
|
// ------------------------------------------------------------------ update
|
|
120
678
|
|
|
121
|
-
private fun updateText(view: TextView, type: String, node: JSONObject, style: JSONObject) {
|
|
122
|
-
val
|
|
679
|
+
private fun updateText(view: TextView, info: Info, type: String, node: JSONObject, style: JSONObject) {
|
|
680
|
+
val raw = node.optString("x", "")
|
|
681
|
+
val text = if (type == "button" && info.loading) "… $raw" else raw
|
|
123
682
|
if (view.text.toString() != text) view.text = text
|
|
124
683
|
|
|
125
684
|
val isHeading = type == "heading"
|
|
@@ -127,18 +686,98 @@ class OrvynRenderer(
|
|
|
127
686
|
val size = number(style.opt("size")) ?: if (isHeading) 28f else 17f
|
|
128
687
|
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, size)
|
|
129
688
|
|
|
130
|
-
val weight = weight(style.opt("weight")) ?: if (isHeading) 700 else if (isButton) 600 else 400
|
|
131
|
-
|
|
689
|
+
val weight = weight(style.opt("weight") ?: style.opt("fontWeight")) ?: if (isHeading) 700 else if (isButton) 600 else 400
|
|
690
|
+
val styleBits = if (weight >= 600) Typeface.BOLD else Typeface.NORMAL
|
|
691
|
+
val baseTypeface = when ((style.optString("font")).trim()) {
|
|
692
|
+
"mono" -> Typeface.MONOSPACE
|
|
693
|
+
"serif" -> Typeface.SERIF
|
|
694
|
+
"sans", "" -> Typeface.SANS_SERIF
|
|
695
|
+
else -> Typeface.create(style.optString("font"), styleBits)
|
|
696
|
+
}
|
|
697
|
+
view.setTypeface(baseTypeface, styleBits)
|
|
698
|
+
|
|
699
|
+
// letterSpacing: points, or "-0.04em" (a share of the text size)
|
|
700
|
+
val tracking = style.opt("letterSpacing")
|
|
701
|
+
view.letterSpacing = when {
|
|
702
|
+
tracking is String && tracking.trim().endsWith("em") -> tracking.trim().removeSuffix("em").toFloatOrNull() ?: 0f
|
|
703
|
+
number(tracking) != null -> number(tracking)!! / size
|
|
704
|
+
else -> 0f
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
number(style.opt("lineHeight"))?.let { lh ->
|
|
708
|
+
if (lh <= 4f) view.setLineSpacing(0f, lh)
|
|
709
|
+
else view.setLineSpacing((dp(lh) - view.lineHeight).coerceAtLeast(0).toFloat(), 1f)
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
val align = style.optString("textAlign", if (style.optBoolean("center", false)) "center" else "")
|
|
713
|
+
view.gravity = when (align) {
|
|
714
|
+
"" -> if (isButton) Gravity.CENTER else Gravity.START
|
|
715
|
+
"center" -> Gravity.CENTER_HORIZONTAL
|
|
716
|
+
"right", "end" -> Gravity.END
|
|
717
|
+
else -> Gravity.START
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
view.isEnabled = !(info.disabled || info.loading)
|
|
721
|
+
view.isClickable = isButton && !(info.disabled || info.loading)
|
|
722
|
+
view.setTextColor(color(style.opt("color")) ?: if (isButton) accent else defaultTextColor)
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
private fun updateToggle(view: CompoundButton, info: Info, node: JSONObject, style: JSONObject) {
|
|
726
|
+
view.setOnCheckedChangeListener(null)
|
|
727
|
+
view.text = node.optString("x", "")
|
|
728
|
+
view.setTextColor(color(style.opt("color")) ?: defaultTextColor)
|
|
729
|
+
view.isChecked = node.optString("v") == "true"
|
|
730
|
+
view.isEnabled = !info.disabled
|
|
731
|
+
view.setOnCheckedChangeListener { _, on -> info.change?.let { fire(it, on.toString()) } }
|
|
732
|
+
}
|
|
132
733
|
|
|
133
|
-
|
|
734
|
+
private fun updateSelect(view: Spinner, info: Info, node: JSONObject) {
|
|
735
|
+
val options = node.optJSONArray("o") ?: JSONArray()
|
|
736
|
+
val labels = mutableListOf(node.optString("ph", ""))
|
|
737
|
+
for (i in 0 until options.length()) labels.add(options.getString(i))
|
|
738
|
+
val signature = labels.joinToString("\u0000")
|
|
739
|
+
if (info.src != signature) {
|
|
740
|
+
info.src = signature
|
|
741
|
+
view.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, labels)
|
|
742
|
+
}
|
|
743
|
+
val selected = labels.indexOf(node.optString("v")).coerceAtLeast(0)
|
|
744
|
+
info.value = if (selected == 0) "" else labels[selected]
|
|
745
|
+
if (view.selectedItemPosition != selected) view.setSelection(selected, false)
|
|
746
|
+
view.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
|
747
|
+
override fun onItemSelected(parent: AdapterView<*>?, item: View?, position: Int, id: Long) {
|
|
748
|
+
val value = if (position == 0) "" else labels[position]
|
|
749
|
+
// also called when the app sets the choice: only report real changes
|
|
750
|
+
if (value == info.value) return
|
|
751
|
+
info.value = value
|
|
752
|
+
info.change?.let { fire(it, value) }
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
|
756
|
+
}
|
|
134
757
|
}
|
|
135
758
|
|
|
136
759
|
private fun updateInput(view: EditText, info: Info, node: JSONObject, style: JSONObject) {
|
|
137
760
|
view.hint = node.optString("ph", "")
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
761
|
+
view.isEnabled = !info.disabled
|
|
762
|
+
view.setTextColor(color(style.opt("color")) ?: defaultTextColor)
|
|
763
|
+
val kind = node.optString("inputType")
|
|
764
|
+
val inputType = when (kind) {
|
|
765
|
+
"email" -> InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|
|
766
|
+
// never show what is typed
|
|
767
|
+
"password" -> InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
|
|
768
|
+
"number" -> InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_SIGNED
|
|
769
|
+
"tel" -> InputType.TYPE_CLASS_PHONE
|
|
770
|
+
"multiline" -> InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
|
|
771
|
+
else -> InputType.TYPE_CLASS_TEXT
|
|
772
|
+
}
|
|
773
|
+
if (view.inputType != inputType) {
|
|
774
|
+
view.inputType = inputType
|
|
775
|
+
if (kind == "multiline") {
|
|
776
|
+
view.isSingleLine = false
|
|
777
|
+
view.minLines = 4
|
|
778
|
+
view.gravity = Gravity.TOP or Gravity.START
|
|
779
|
+
}
|
|
780
|
+
}
|
|
142
781
|
val value = if (node.has("v")) node.optString("v", "") else null
|
|
143
782
|
if (value != null && value != view.text.toString()) {
|
|
144
783
|
info.updating = true
|
|
@@ -149,49 +788,161 @@ class OrvynRenderer(
|
|
|
149
788
|
if (!style.has("padding")) {
|
|
150
789
|
view.setPadding(dp(10f), dp(10f), dp(10f), dp(10f))
|
|
151
790
|
}
|
|
152
|
-
|
|
791
|
+
val hasError = style.has("error") && style.opt("error") != false
|
|
792
|
+
if (hasError || (!style.has("border") && !style.has("bg"))) {
|
|
153
793
|
view.background = GradientDrawable().apply {
|
|
794
|
+
color(style.opt("bg"))?.let { setColor(it) }
|
|
154
795
|
cornerRadius = dp(number(style.opt("radius")) ?: 8f).toFloat()
|
|
155
|
-
setStroke(dp(1f),
|
|
796
|
+
setStroke(dp(if (hasError) 2f else 1f), if (hasError) dangerColor else defaultBorderColor)
|
|
156
797
|
}
|
|
157
798
|
}
|
|
158
799
|
}
|
|
159
800
|
|
|
801
|
+
// an eighth of the app's memory for pictures already loaded
|
|
802
|
+
private val images = object : LruCache<String, Bitmap>((Runtime.getRuntime().maxMemory() / 8).toInt()) {
|
|
803
|
+
override fun sizeOf(key: String, value: Bitmap) = value.byteCount
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// asset:/ (from public/), data: (picked photos) or a web address
|
|
807
|
+
private fun loadBitmap(src: String): Bitmap? = try {
|
|
808
|
+
if (src.startsWith("asset:/")) {
|
|
809
|
+
context.assets.open("public/" + src.removePrefix("asset:/")).use { BitmapFactory.decodeStream(it) }
|
|
810
|
+
} else if (src.startsWith("data:")) {
|
|
811
|
+
val bytes = Base64.decode(src.substringAfter(","), Base64.DEFAULT)
|
|
812
|
+
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
|
|
813
|
+
} else URL(src).openStream().use { BitmapFactory.decodeStream(it) }
|
|
814
|
+
} catch (_: Exception) {
|
|
815
|
+
null
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// bg: "linear-gradient(...), url(photo.jpg)": the photo covers the box under its
|
|
819
|
+
// colour; a gradient on top becomes a dark tint (bgDim) so text stays readable
|
|
820
|
+
private fun applyBackgroundPhoto(view: View, style: JSONObject, radius: Float?) {
|
|
821
|
+
val info = view.tag as? Info ?: return
|
|
822
|
+
val src = style.optString("bgImage", "").ifEmpty { null }
|
|
823
|
+
info.bgImage = src
|
|
824
|
+
if (src == null) return
|
|
825
|
+
val dim = number(style.opt("bgDim")) ?: 0f
|
|
826
|
+
val corner = dp(radius ?: 0f).toFloat()
|
|
827
|
+
view.outlineProvider = object : ViewOutlineProvider() {
|
|
828
|
+
override fun getOutline(v: View, outline: Outline) = outline.setRoundRect(0, 0, v.width, v.height, corner)
|
|
829
|
+
}
|
|
830
|
+
view.clipToOutline = true
|
|
831
|
+
fun show(bitmap: Bitmap) {
|
|
832
|
+
val base = view.background
|
|
833
|
+
val photo = CoverDrawable(bitmap, dim)
|
|
834
|
+
view.background = if (base != null) LayerDrawable(arrayOf(base, photo)) else photo
|
|
835
|
+
}
|
|
836
|
+
images.get(src)?.let { show(it); return }
|
|
837
|
+
thread {
|
|
838
|
+
val bitmap = loadBitmap(src) ?: return@thread
|
|
839
|
+
images.put(src, bitmap)
|
|
840
|
+
main.post { if (info.bgImage == src) show(bitmap) }
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
160
844
|
private fun updateImage(view: ImageView, info: Info, node: JSONObject) {
|
|
161
845
|
val src = node.optString("src", "")
|
|
162
846
|
if (src.isEmpty() || src == info.src) return
|
|
163
847
|
info.src = src
|
|
848
|
+
images.get(src)?.let {
|
|
849
|
+
view.background = null
|
|
850
|
+
view.setImageBitmap(it)
|
|
851
|
+
return
|
|
852
|
+
}
|
|
853
|
+
// a quiet placeholder while it loads, and no picture left over from another row
|
|
854
|
+
view.setImageDrawable(null)
|
|
855
|
+
view.setBackgroundColor(Color.parseColor("#14808080"))
|
|
164
856
|
thread {
|
|
165
857
|
try {
|
|
166
|
-
|
|
167
|
-
|
|
858
|
+
// picked photos arrive as data: text, which URL can't open
|
|
859
|
+
val bitmap = (if (src.startsWith("asset:/")) {
|
|
860
|
+
// an image from public/, shipped in the app's assets
|
|
861
|
+
view.context.assets.open("public/" + src.removePrefix("asset:/")).use { BitmapFactory.decodeStream(it) }
|
|
862
|
+
} else if (src.startsWith("data:")) {
|
|
863
|
+
val bytes = Base64.decode(src.substringAfter(","), Base64.DEFAULT)
|
|
864
|
+
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
|
|
865
|
+
} else URL(src).openStream().use { BitmapFactory.decodeStream(it) }) ?: return@thread
|
|
866
|
+
images.put(src, bitmap)
|
|
867
|
+
main.post {
|
|
868
|
+
if (info.src == src) {
|
|
869
|
+
view.background = null
|
|
870
|
+
view.setImageBitmap(bitmap)
|
|
871
|
+
}
|
|
872
|
+
}
|
|
168
873
|
} catch (_: Exception) {
|
|
169
874
|
}
|
|
170
875
|
}
|
|
171
876
|
}
|
|
172
877
|
|
|
173
|
-
private fun
|
|
174
|
-
|
|
175
|
-
|
|
878
|
+
private fun updateWrapChildren(layout: WrapLayout, type: String, node: JSONObject, style: JSONObject, childWidth: Int) {
|
|
879
|
+
layout.gapPx = dp(number(style.opt("gap")) ?: 0f)
|
|
880
|
+
layout.justify = style.optString("justify")
|
|
881
|
+
layout.align = style.optString("align")
|
|
882
|
+
val children = node.optJSONArray("c")
|
|
883
|
+
val count = children?.length() ?: 0
|
|
884
|
+
for (i in 0 until count) {
|
|
885
|
+
val childNode = children!!.getJSONObject(i)
|
|
886
|
+
val existing = if (i < layout.childCount) layout.getChildAt(i) else null
|
|
887
|
+
val child = render(childNode, existing, type, childWidth)
|
|
888
|
+
if (child !== existing) {
|
|
889
|
+
if (existing != null) layout.removeViewAt(i)
|
|
890
|
+
layout.addView(child, i)
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
while (layout.childCount > count) {
|
|
894
|
+
layout.removeViewAt(layout.childCount - 1)
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
private fun updateChildren(layout: LinearLayout, type: String, node: JSONObject, style: JSONObject, childWidth: Int) {
|
|
899
|
+
val centered = style.optBoolean("center", false)
|
|
900
|
+
val justify = style.optString("justify")
|
|
901
|
+
if (type == "row") {
|
|
902
|
+
val vGravity = when (style.optString("align")) {
|
|
903
|
+
"start", "flex-start", "top" -> Gravity.TOP
|
|
904
|
+
"end", "flex-end", "bottom" -> Gravity.BOTTOM
|
|
905
|
+
else -> Gravity.CENTER_VERTICAL
|
|
906
|
+
}
|
|
907
|
+
val hGravity = when {
|
|
908
|
+
centered || justify == "center" -> Gravity.CENTER_HORIZONTAL
|
|
909
|
+
justify == "end" || justify == "flex-end" -> Gravity.END
|
|
910
|
+
else -> Gravity.START
|
|
911
|
+
}
|
|
912
|
+
layout.gravity = vGravity or hGravity
|
|
913
|
+
} else {
|
|
914
|
+
val hGravity = when (if (centered) "center" else style.optString("align")) {
|
|
176
915
|
"center" -> Gravity.CENTER_HORIZONTAL
|
|
177
916
|
"end", "flex-end", "right" -> Gravity.END
|
|
178
917
|
else -> Gravity.START
|
|
179
918
|
}
|
|
919
|
+
val vGravity = when {
|
|
920
|
+
centered || justify == "center" -> Gravity.CENTER_VERTICAL
|
|
921
|
+
justify == "end" || justify == "flex-end" -> Gravity.BOTTOM
|
|
922
|
+
else -> Gravity.TOP
|
|
923
|
+
}
|
|
924
|
+
layout.gravity = hGravity or vGravity
|
|
180
925
|
}
|
|
926
|
+
(layout as? FlowLayout)?.justify = if (centered) "" else justify
|
|
181
927
|
|
|
182
928
|
val children = node.optJSONArray("c")
|
|
183
929
|
val count = children?.length() ?: 0
|
|
184
930
|
val gap = dp(number(style.opt("gap")) ?: 0f)
|
|
185
931
|
|
|
932
|
+
var inFlow = 0
|
|
186
933
|
for (i in 0 until count) {
|
|
187
934
|
val childNode = children!!.getJSONObject(i)
|
|
188
935
|
val existing = if (i < layout.childCount) layout.getChildAt(i) else null
|
|
189
|
-
val child = render(childNode, existing, type)
|
|
936
|
+
val child = render(childNode, existing, type, childWidth)
|
|
190
937
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
938
|
+
// absolute children take no space, so no gap before or after them
|
|
939
|
+
if ((child.tag as? Info)?.abs == null) {
|
|
940
|
+
(child.layoutParams as? LinearLayout.LayoutParams)?.let { params ->
|
|
941
|
+
if (inFlow > 0) {
|
|
942
|
+
if (type == "row") params.leftMargin += gap else params.topMargin += gap
|
|
943
|
+
}
|
|
194
944
|
}
|
|
945
|
+
inFlow++
|
|
195
946
|
}
|
|
196
947
|
|
|
197
948
|
if (child !== existing) {
|
|
@@ -213,17 +964,43 @@ class OrvynRenderer(
|
|
|
213
964
|
val padding = number(style.opt("padding"))
|
|
214
965
|
val px = number(style.opt("paddingX")) ?: padding ?: 0f
|
|
215
966
|
val py = number(style.opt("paddingY")) ?: padding ?: 0f
|
|
216
|
-
|
|
217
|
-
|
|
967
|
+
// paddingTop/Right/Bottom/Left come from shorthands like "20px 5% 0"
|
|
968
|
+
fun side(key: String, fallback: Float) = dp(number(style.opt(key)) ?: fallback)
|
|
969
|
+
val hasSides = listOf("paddingTop", "paddingRight", "paddingBottom", "paddingLeft").any { style.has(it) }
|
|
970
|
+
if (type != "input" || padding != null || hasSides) {
|
|
971
|
+
view.setPadding(side("paddingLeft", px), side("paddingTop", py), side("paddingRight", px), side("paddingBottom", py))
|
|
218
972
|
}
|
|
219
973
|
|
|
974
|
+
number(style.opt("minHeight"))?.let { view.minimumHeight = dp(it) }
|
|
975
|
+
|
|
220
976
|
val bg = color(style.opt("bg"))
|
|
221
977
|
val radius = number(style.opt("radius"))
|
|
222
978
|
val border = style.opt("border") as? String
|
|
223
979
|
|
|
980
|
+
val info = view.tag as? Info
|
|
981
|
+
val animateMs = animationMs(style)
|
|
982
|
+
// with animate:, children that grow or shrink push the others smoothly
|
|
983
|
+
if (view is ViewGroup && view !is AdapterView<*>) {
|
|
984
|
+
view.layoutTransition = animateMs?.let { ms ->
|
|
985
|
+
(view.layoutTransition ?: LayoutTransition()).apply {
|
|
986
|
+
enableTransitionType(LayoutTransition.CHANGING)
|
|
987
|
+
setDuration(ms)
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
}
|
|
224
991
|
if (bg != null || border != null || (radius != null && type != "input")) {
|
|
225
|
-
|
|
226
|
-
|
|
992
|
+
val previousBg = info?.bgColor
|
|
993
|
+
info?.bgColor = bg
|
|
994
|
+
val normal = GradientDrawable().apply {
|
|
995
|
+
setColor(if (animateMs != null && previousBg != null && bg != null && previousBg != bg) previousBg else bg ?: Color.TRANSPARENT)
|
|
996
|
+
if (animateMs != null && previousBg != null && bg != null && previousBg != bg) {
|
|
997
|
+
val drawable = this
|
|
998
|
+
ValueAnimator.ofArgb(previousBg, bg).apply {
|
|
999
|
+
duration = animateMs
|
|
1000
|
+
addUpdateListener { drawable.setColor(it.animatedValue as Int) }
|
|
1001
|
+
start()
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
227
1004
|
cornerRadius = dp(radius ?: 0f).toFloat()
|
|
228
1005
|
if (border != null) {
|
|
229
1006
|
val parts = border.split(" ")
|
|
@@ -232,41 +1009,184 @@ class OrvynRenderer(
|
|
|
232
1009
|
setStroke(dp(width), borderColor)
|
|
233
1010
|
}
|
|
234
1011
|
}
|
|
1012
|
+
if (type == "button") {
|
|
1013
|
+
val pressed = style.optJSONObject("pressed") ?: style.optJSONObject("active")
|
|
1014
|
+
val pressedBg = color(pressed?.opt("bg")) ?: bg
|
|
1015
|
+
val states = StateListDrawable()
|
|
1016
|
+
if (pressedBg != null) {
|
|
1017
|
+
val down = GradientDrawable().apply {
|
|
1018
|
+
setColor(pressedBg)
|
|
1019
|
+
cornerRadius = dp(radius ?: 0f).toFloat()
|
|
1020
|
+
}
|
|
1021
|
+
states.addState(intArrayOf(android.R.attr.state_pressed), down)
|
|
1022
|
+
}
|
|
1023
|
+
states.addState(intArrayOf(), normal)
|
|
1024
|
+
view.background = states
|
|
1025
|
+
} else {
|
|
1026
|
+
view.background = normal
|
|
1027
|
+
}
|
|
235
1028
|
view.clipToOutline = radius != null
|
|
236
1029
|
} else if (type != "input") {
|
|
237
1030
|
view.background = null
|
|
238
1031
|
}
|
|
239
1032
|
|
|
1033
|
+
applyBackgroundPhoto(view, style, radius)
|
|
1034
|
+
|
|
240
1035
|
view.elevation = dp((number(style.opt("shadow")) ?: 0f) * 2f).toFloat()
|
|
241
|
-
|
|
1036
|
+
val disabled = style.optBoolean("disabled", false)
|
|
1037
|
+
val loading = style.optBoolean("loading", false)
|
|
1038
|
+
val targetAlpha = when {
|
|
1039
|
+
disabled -> 0.5f
|
|
1040
|
+
loading -> 0.75f
|
|
1041
|
+
else -> number(style.opt("opacity")) ?: 1f
|
|
1042
|
+
}
|
|
1043
|
+
// an appear: animation that is still playing owns the alpha
|
|
1044
|
+
val appearing = style.optString("appear").isNotEmpty() &&
|
|
1045
|
+
(info?.appeared != true || android.os.SystemClock.uptimeMillis() < (info?.appearUntil ?: 0L))
|
|
1046
|
+
if (appearing) return
|
|
1047
|
+
if (animateMs != null) view.animate().alpha(targetAlpha).setDuration(animateMs).start()
|
|
1048
|
+
else view.alpha = targetAlpha
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
// animate: "spring 0.4s" with a new width or height: grow to it instead of jumping
|
|
1052
|
+
private fun animateSize(view: View, before: ViewGroup.LayoutParams?, ms: Long?) {
|
|
1053
|
+
if (ms == null || before == null) return
|
|
1054
|
+
val after = view.layoutParams
|
|
1055
|
+
fun tween(from: Int, to: Int, set: (Int) -> Unit) {
|
|
1056
|
+
if (from > 0 && to > 0 && from != to) {
|
|
1057
|
+
set(from)
|
|
1058
|
+
ValueAnimator.ofInt(from, to).apply {
|
|
1059
|
+
duration = ms
|
|
1060
|
+
addUpdateListener {
|
|
1061
|
+
set(it.animatedValue as Int)
|
|
1062
|
+
view.requestLayout()
|
|
1063
|
+
}
|
|
1064
|
+
start()
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
tween(before.height, after.height) { after.height = it }
|
|
1069
|
+
tween(before.width, after.width) { after.width = it }
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
// "ease 0.4s" -> 400
|
|
1073
|
+
private fun animationMs(style: JSONObject): Long? {
|
|
1074
|
+
val animate = style.optString("animate", "")
|
|
1075
|
+
if (animate.isEmpty()) return null
|
|
1076
|
+
val seconds = animate.split(" ").getOrNull(1)?.removeSuffix("s")?.toFloatOrNull() ?: 0.3f
|
|
1077
|
+
return (seconds * 1000).toLong()
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
// runs `show` once, the first time any part of the view is on screen
|
|
1081
|
+
// (checked as the page scrolls and before each frame is drawn)
|
|
1082
|
+
private fun whenOnScreen(view: View, show: () -> Unit) {
|
|
1083
|
+
val rect = android.graphics.Rect()
|
|
1084
|
+
var done = false
|
|
1085
|
+
var scroll: android.view.ViewTreeObserver.OnScrollChangedListener? = null
|
|
1086
|
+
var draw: android.view.ViewTreeObserver.OnPreDrawListener? = null
|
|
1087
|
+
val check = {
|
|
1088
|
+
if (!done && view.isAttachedToWindow && view.isShown && view.getGlobalVisibleRect(rect)) {
|
|
1089
|
+
done = true
|
|
1090
|
+
view.viewTreeObserver.removeOnScrollChangedListener(scroll)
|
|
1091
|
+
view.viewTreeObserver.removeOnPreDrawListener(draw)
|
|
1092
|
+
main.post(show)
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
scroll = android.view.ViewTreeObserver.OnScrollChangedListener { check() }
|
|
1096
|
+
draw = android.view.ViewTreeObserver.OnPreDrawListener { check(); true }
|
|
1097
|
+
view.viewTreeObserver.addOnScrollChangedListener(scroll)
|
|
1098
|
+
view.viewTreeObserver.addOnPreDrawListener(draw)
|
|
242
1099
|
}
|
|
243
1100
|
|
|
244
|
-
|
|
1101
|
+
// appear: "fade-up 0.6s 0.2s" plays once, when the view scrolls into sight;
|
|
1102
|
+
// onVisible: runs at the same moment (like the web)
|
|
1103
|
+
private fun playAppear(view: View, info: Info, style: JSONObject, visible: Int?) {
|
|
1104
|
+
if (info.appeared) return
|
|
1105
|
+
val spec = style.optString("appear", "")
|
|
1106
|
+
if (spec.isEmpty() && visible == null) return
|
|
1107
|
+
info.appeared = true
|
|
1108
|
+
val reduceMotion = android.provider.Settings.Global.getFloat(
|
|
1109
|
+
view.context.contentResolver, android.provider.Settings.Global.ANIMATOR_DURATION_SCALE, 1f) == 0f
|
|
1110
|
+
val animated = spec.isNotEmpty() && !reduceMotion
|
|
1111
|
+
val parts = spec.split(" ")
|
|
1112
|
+
val ms = ((parts.getOrNull(1)?.removeSuffix("s")?.toFloatOrNull() ?: 0.5f) * 1000).toLong()
|
|
1113
|
+
val delay = ((parts.getOrNull(2)?.removeSuffix("s")?.toFloatOrNull() ?: 0f) * 1000).toLong()
|
|
1114
|
+
val distance = dp(24f).toFloat()
|
|
1115
|
+
val targetAlpha = number(style.opt("opacity")) ?: 1f
|
|
1116
|
+
if (animated) {
|
|
1117
|
+
// hidden and waiting until it is scrolled to; re-renders leave it alone
|
|
1118
|
+
info.appearUntil = Long.MAX_VALUE
|
|
1119
|
+
view.alpha = 0f
|
|
1120
|
+
// animation pop { opacity: 0, y: 30, scale: 0.8, rotate: -6 }: start there
|
|
1121
|
+
val custom = style.optJSONObject("appearFrom")
|
|
1122
|
+
if (custom != null) {
|
|
1123
|
+
view.alpha = number(custom.opt("opacity")) ?: targetAlpha
|
|
1124
|
+
view.translationX = dp(number(custom.opt("x")) ?: 0f).toFloat()
|
|
1125
|
+
view.translationY = dp(number(custom.opt("y")) ?: 0f).toFloat()
|
|
1126
|
+
(number(custom.opt("scale")) ?: 1f).let { view.scaleX = it; view.scaleY = it }
|
|
1127
|
+
view.rotation = number(custom.opt("rotate")) ?: 0f
|
|
1128
|
+
} else when (parts[0]) {
|
|
1129
|
+
"fade-up" -> view.translationY = distance
|
|
1130
|
+
"fade-down" -> view.translationY = -distance
|
|
1131
|
+
"slide-left" -> view.translationX = distance
|
|
1132
|
+
"slide-right" -> view.translationX = -distance
|
|
1133
|
+
"scale" -> { view.scaleX = 0.9f; view.scaleY = 0.9f }
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
whenOnScreen(view) {
|
|
1137
|
+
visible?.let { fire(it, null) }
|
|
1138
|
+
if (!animated) return@whenOnScreen
|
|
1139
|
+
info.appearUntil = android.os.SystemClock.uptimeMillis() + delay + ms
|
|
1140
|
+
view.animate().alpha(targetAlpha).translationX(0f).translationY(0f).scaleX(1f).scaleY(1f).rotation(0f)
|
|
1141
|
+
.setDuration(ms).setStartDelay(delay)
|
|
1142
|
+
.setInterpolator(android.view.animation.PathInterpolator(0.2f, 0.8f, 0.2f, 1f))
|
|
1143
|
+
.start()
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
private fun layoutParams(type: String, style: JSONObject, parentType: String, availableWidthPx: Int): ViewGroup.LayoutParams {
|
|
245
1148
|
val widthValue = style.opt("width")
|
|
246
1149
|
val isContainer = type == "col" || type == "row" || type == "box" || type == "input"
|
|
1150
|
+
val margin = dp(number(style.opt("margin")) ?: 0f)
|
|
247
1151
|
|
|
248
1152
|
var width = when {
|
|
249
|
-
widthValue is String && widthValue.
|
|
1153
|
+
widthValue is String && widthValue.trim() == "100%" -> ViewGroup.LayoutParams.MATCH_PARENT
|
|
1154
|
+
widthValue is String && widthValue.trim().endsWith("%") -> {
|
|
1155
|
+
val pct = widthValue.trim().removeSuffix("%").toFloatOrNull()
|
|
1156
|
+
if (pct != null && availableWidthPx > 0) ((availableWidthPx - margin * 2) * (pct / 100f)).toInt().coerceAtLeast(0)
|
|
1157
|
+
else ViewGroup.LayoutParams.MATCH_PARENT
|
|
1158
|
+
}
|
|
250
1159
|
number(widthValue) != null -> dp(number(widthValue)!!)
|
|
251
1160
|
parentType != "row" && isContainer -> ViewGroup.LayoutParams.MATCH_PARENT
|
|
252
1161
|
else -> ViewGroup.LayoutParams.WRAP_CONTENT
|
|
253
1162
|
}
|
|
1163
|
+
number(style.opt("maxWidth"))?.let { maxW ->
|
|
1164
|
+
val maxPx = dp(maxW)
|
|
1165
|
+
if (width == ViewGroup.LayoutParams.MATCH_PARENT) width = min(availableWidthPx, maxPx)
|
|
1166
|
+
else if (width > maxPx) width = maxPx
|
|
1167
|
+
}
|
|
254
1168
|
val height = number(style.opt("height"))?.let { dp(it) } ?: ViewGroup.LayoutParams.WRAP_CONTENT
|
|
255
1169
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
1170
|
+
val growVal = style.opt("grow")
|
|
1171
|
+
var weight = when {
|
|
1172
|
+
growVal == true -> 1f
|
|
1173
|
+
number(growVal) != null -> number(growVal)!!
|
|
1174
|
+
parentType == "row" && type == "input" && widthValue == null -> 1f
|
|
1175
|
+
else -> 0f
|
|
1176
|
+
}
|
|
1177
|
+
if (weight > 0f && parentType == "row" && widthValue == null) width = 0
|
|
259
1178
|
|
|
260
1179
|
val params = LinearLayout.LayoutParams(width, height, weight)
|
|
261
|
-
val margin = dp(number(style.opt("margin")) ?: 0f)
|
|
262
1180
|
params.setMargins(margin, margin, margin, margin)
|
|
1181
|
+
// margin: "0 auto" centres the element in its column, like the web
|
|
1182
|
+
if (style.optBoolean("centerSelf", false)) params.gravity = Gravity.CENTER_HORIZONTAL
|
|
263
1183
|
return params
|
|
264
1184
|
}
|
|
265
1185
|
|
|
266
1186
|
private fun number(value: Any?): Float? = when (value) {
|
|
267
1187
|
is Boolean -> null
|
|
268
1188
|
is Number -> value.toFloat()
|
|
269
|
-
is String -> value.replace("px", "").trim().toFloatOrNull()
|
|
1189
|
+
is String -> if (value.contains("%")) null else value.replace("px", "").trim().toFloatOrNull()
|
|
270
1190
|
else -> null
|
|
271
1191
|
}
|
|
272
1192
|
|