@nativue/core 0.15.4 → 0.17.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/AGENTS.md +18 -4
- package/android/nativue.settings.gradle.kts +29 -9
- package/android/src/main/java/dev/nativue/BackgroundGradients.kt +111 -0
- package/android/src/main/java/dev/nativue/BoxDrawable.kt +49 -9
- package/android/src/main/java/dev/nativue/Bridge.kt +6 -16
- package/android/src/main/java/dev/nativue/NativeNode.kt +4 -0
- package/android/src/main/java/dev/nativue/Navigation.kt +31 -37
- package/android/src/main/java/dev/nativue/NavigationIcons.kt +24 -0
- package/android/src/main/java/dev/nativue/Plugins.kt +13 -1
- package/android/src/main/java/dev/nativue/Style.kt +81 -3
- package/android/src/main/java/dev/nativue/Views.kt +70 -33
- package/android/src/main/res/drawable/nativue_arrow_back.xml +14 -0
- package/android/src/main/res/drawable/nativue_close.xml +14 -0
- package/cli/ios-project.mjs +1 -1
- package/cli/link.mjs +25 -6
- package/cli/nativue.mjs +0 -1
- package/ios/BackgroundGradients.swift +191 -0
- package/ios/Bridge.swift +14 -20
- package/ios/ImageView.swift +32 -10
- package/ios/NativeNode.swift +19 -2
- package/ios/NavigationIcons.swift +49 -0
- package/ios/Plugins.swift +13 -1
- package/ios/PressableView.swift +3 -1
- package/ios/ScreenController.swift +6 -3
- package/ios/Style.swift +76 -14
- package/ios/VueScrollView.swift +1 -0
- package/package.json +2 -2
- package/src/colors.ts +10 -6
- package/src/gradients.ts +304 -0
- package/src/native-components.ts +0 -14
- package/src/renderer.ts +203 -12
- package/src/router.ts +20 -7
- package/src/types.ts +18 -0
- package/android/src/main/java/dev/nativue/SvgView.kt +0 -443
- package/ios/SvgView.swift +0 -464
|
@@ -3,6 +3,10 @@ package dev.nativue
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import android.graphics.Paint
|
|
5
5
|
import android.graphics.Typeface
|
|
6
|
+
import android.text.SpannableString
|
|
7
|
+
import android.text.Spanned
|
|
8
|
+
import android.text.TextUtils
|
|
9
|
+
import android.text.style.LineHeightSpan
|
|
6
10
|
import android.util.TypedValue
|
|
7
11
|
import android.view.Gravity
|
|
8
12
|
import android.view.View
|
|
@@ -19,6 +23,7 @@ import com.facebook.yoga.YogaNode
|
|
|
19
23
|
import com.facebook.yoga.YogaOverflow
|
|
20
24
|
import com.facebook.yoga.YogaPositionType
|
|
21
25
|
import com.facebook.yoga.YogaWrap
|
|
26
|
+
import kotlin.math.roundToInt
|
|
22
27
|
|
|
23
28
|
/**
|
|
24
29
|
* Receives only the properties that changed: the JS renderer diffs style objects before sending. A removed
|
|
@@ -48,7 +53,14 @@ private fun NativeNode.applyVisualProperty(property: String, value: Any?) {
|
|
|
48
53
|
|
|
49
54
|
when (property) {
|
|
50
55
|
"backgroundColor" -> updateBox { backgroundColor = androidColor(value) ?: 0 }
|
|
56
|
+
"backgroundImage" -> updateBox {
|
|
57
|
+
gradients = (value as? List<*>)?.filterIsInstance<Map<*, *>>().orEmpty()
|
|
58
|
+
density = view.resources.displayMetrics.density
|
|
59
|
+
}
|
|
51
60
|
"borderRadius" -> updateBox { cornerRadius = pixels(value) }
|
|
61
|
+
"borderTopLeftRadius", "borderTopRightRadius", "borderBottomRightRadius", "borderBottomLeftRadius" -> updateBox {
|
|
62
|
+
cornerRadii[CORNERS.indexOf(property)] = value?.let { pixels(it) }
|
|
63
|
+
}
|
|
52
64
|
"borderWidth" -> updateBox { borderWidth = pixels(value) }
|
|
53
65
|
"borderColor" -> updateBox { borderColor = androidColor(value) ?: 0 }
|
|
54
66
|
"shadowColor" -> updateBox { shadowColor = androidColor(value) ?: 0xFF000000.toInt() }
|
|
@@ -69,6 +81,26 @@ private fun NativeNode.applyVisualProperty(property: String, value: Any?) {
|
|
|
69
81
|
}
|
|
70
82
|
"textAlign" -> textView?.gravity = textGravities[value] ?: Gravity.START
|
|
71
83
|
"textDecorationLine" -> textView?.let { it.paintFlags = decoratedPaintFlags(it.paintFlags, value as? String) }
|
|
84
|
+
"fontStyle" -> textView?.let {
|
|
85
|
+
isItalic = value == "italic"
|
|
86
|
+
it.typeface = typefaceOfWeight(it.context, fontWeight, isItalic)
|
|
87
|
+
markContentChanged()
|
|
88
|
+
}
|
|
89
|
+
"letterSpacing" -> {
|
|
90
|
+
letterSpacing = value
|
|
91
|
+
updateTextSpacing()
|
|
92
|
+
}
|
|
93
|
+
"lineHeight" -> {
|
|
94
|
+
lineHeight = value
|
|
95
|
+
updateTextSpacing()
|
|
96
|
+
}
|
|
97
|
+
"lineClamp" -> textView?.let {
|
|
98
|
+
val lines = number(value)?.toInt() ?: 0
|
|
99
|
+
|
|
100
|
+
it.maxLines = if (lines > 0) lines else Int.MAX_VALUE
|
|
101
|
+
it.ellipsize = if (lines > 0) TextUtils.TruncateAt.END else null
|
|
102
|
+
markContentChanged()
|
|
103
|
+
}
|
|
72
104
|
}
|
|
73
105
|
}
|
|
74
106
|
|
|
@@ -120,8 +152,8 @@ private fun degrees(value: Any?): Float {
|
|
|
120
152
|
}
|
|
121
153
|
|
|
122
154
|
/** The system font at a weight (100 to 900). */
|
|
123
|
-
fun typefaceOfWeight(context: Context, weight: Int): Typeface =
|
|
124
|
-
TypefaceCompat.create(context, Typeface.DEFAULT, weight,
|
|
155
|
+
fun typefaceOfWeight(context: Context, weight: Int, isItalic: Boolean = false): Typeface =
|
|
156
|
+
TypefaceCompat.create(context, Typeface.DEFAULT, weight, isItalic)
|
|
125
157
|
|
|
126
158
|
/** Font size and weight can change independently, so the node remembers both. */
|
|
127
159
|
private fun NativeNode.applyFont(changes: Map<String, Any?>) {
|
|
@@ -129,7 +161,7 @@ private fun NativeNode.applyFont(changes: Map<String, Any?>) {
|
|
|
129
161
|
if ("fontWeight" in changes) fontWeight = changes["fontWeight"]?.let(::fontWeightOf) ?: 400
|
|
130
162
|
|
|
131
163
|
val textView = view as? TextView ?: return
|
|
132
|
-
textView.typeface = typefaceOfWeight(textView.context, fontWeight)
|
|
164
|
+
textView.typeface = typefaceOfWeight(textView.context, fontWeight, isItalic)
|
|
133
165
|
updateFontSize()
|
|
134
166
|
}
|
|
135
167
|
|
|
@@ -141,9 +173,52 @@ fun NativeNode.updateFontSize() {
|
|
|
141
173
|
val textView = view as? TextView ?: return
|
|
142
174
|
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize)
|
|
143
175
|
|
|
176
|
+
if (letterSpacing != null || lineHeight != null) updateTextSpacing()
|
|
144
177
|
markContentChanged()
|
|
145
178
|
}
|
|
146
179
|
|
|
180
|
+
/** letterSpacing and lineHeight, which can be in ems of the font size: set again when that size changes. */
|
|
181
|
+
private fun NativeNode.updateTextSpacing() {
|
|
182
|
+
val textLabel = view as? TextLabel ?: return
|
|
183
|
+
|
|
184
|
+
// Android's is in ems already.
|
|
185
|
+
textLabel.letterSpacing = ems(letterSpacing) ?: number(letterSpacing)?.let { pixels(it) / textLabel.textSize } ?: 0f
|
|
186
|
+
textLabel.text = withLineHeight(textLabel.text.toString())
|
|
187
|
+
markContentChanged()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** A <Text>'s text, its lines as tall as lineHeight when it has one. */
|
|
191
|
+
fun NativeNode.withLineHeight(text: String): CharSequence {
|
|
192
|
+
val textSize = (view as? TextLabel)?.textSize ?: return text
|
|
193
|
+
val height = ems(lineHeight)?.let { it * textSize } ?: number(lineHeight)?.let { pixels(it) } ?: return text
|
|
194
|
+
|
|
195
|
+
return SpannableString(text).apply {
|
|
196
|
+
setSpan(CenteredLineHeightSpan(height.roundToInt()), 0, length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** "1.5em" as 1.5; null for anything else. */
|
|
201
|
+
private fun ems(value: Any?): Float? = (value as? String)?.takeIf { it.endsWith("em") }?.dropLast(2)?.toFloatOrNull()
|
|
202
|
+
|
|
203
|
+
/** Each line [height] pixels tall, the text centered in it, as CSS lays out a line height. */
|
|
204
|
+
private class CenteredLineHeightSpan(private val height: Int) : LineHeightSpan {
|
|
205
|
+
override fun chooseHeight(
|
|
206
|
+
text: CharSequence,
|
|
207
|
+
start: Int,
|
|
208
|
+
end: Int,
|
|
209
|
+
spanStart: Int,
|
|
210
|
+
lineHeight: Int,
|
|
211
|
+
metrics: Paint.FontMetricsInt,
|
|
212
|
+
) {
|
|
213
|
+
val extra = height - (metrics.descent - metrics.ascent)
|
|
214
|
+
|
|
215
|
+
metrics.ascent -= extra / 2
|
|
216
|
+
metrics.descent += extra - extra / 2
|
|
217
|
+
metrics.top = metrics.ascent
|
|
218
|
+
metrics.bottom = metrics.descent
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
147
222
|
private fun NativeNode.pixels(value: Any?): Float = (number(value) ?: 0f) * view.resources.displayMetrics.density
|
|
148
223
|
|
|
149
224
|
fun number(value: Any?): Float? = (value as? Number)?.toFloat()
|
|
@@ -157,6 +232,9 @@ fun androidColor(value: Any?): Int? {
|
|
|
157
232
|
|
|
158
233
|
// ---------- CSS value tables ----------
|
|
159
234
|
|
|
235
|
+
// In BoxDrawable.cornerRadii's order.
|
|
236
|
+
private val CORNERS = listOf("borderTopLeftRadius", "borderTopRightRadius", "borderBottomRightRadius", "borderBottomLeftRadius")
|
|
237
|
+
|
|
160
238
|
private val edgesBySuffix = mapOf(
|
|
161
239
|
"" to YogaEdge.ALL,
|
|
162
240
|
"Horizontal" to YogaEdge.HORIZONTAL,
|
|
@@ -4,7 +4,9 @@ import android.annotation.SuppressLint
|
|
|
4
4
|
import android.content.Context
|
|
5
5
|
import android.graphics.Bitmap
|
|
6
6
|
import android.graphics.BitmapFactory
|
|
7
|
+
import android.graphics.Canvas
|
|
7
8
|
import android.graphics.Rect
|
|
9
|
+
import android.os.Build
|
|
8
10
|
import android.os.Handler
|
|
9
11
|
import android.os.Looper
|
|
10
12
|
import android.os.SystemClock
|
|
@@ -14,9 +16,9 @@ import android.view.MotionEvent
|
|
|
14
16
|
import android.view.View
|
|
15
17
|
import android.view.ViewConfiguration
|
|
16
18
|
import android.view.ViewGroup
|
|
19
|
+
import android.view.inputmethod.InputMethodManager
|
|
17
20
|
import android.widget.FrameLayout
|
|
18
21
|
import android.widget.HorizontalScrollView
|
|
19
|
-
import android.view.inputmethod.InputMethodManager
|
|
20
22
|
import android.widget.ImageView
|
|
21
23
|
import android.widget.ProgressBar
|
|
22
24
|
import android.widget.ScrollView
|
|
@@ -26,9 +28,9 @@ import java.io.IOException
|
|
|
26
28
|
import java.net.URL
|
|
27
29
|
import java.security.MessageDigest
|
|
28
30
|
import java.util.concurrent.Executors
|
|
31
|
+
import kotlin.math.abs
|
|
29
32
|
import kotlin.math.min
|
|
30
33
|
import kotlin.math.roundToInt
|
|
31
|
-
import kotlin.math.abs
|
|
32
34
|
|
|
33
35
|
/**
|
|
34
36
|
* A view whose children Yoga places (NativeNode.applyFrames): Android's own layout passes keep them
|
|
@@ -68,6 +70,19 @@ open class ContainerView(context: Context) : ViewGroup(context) {
|
|
|
68
70
|
override fun onInterceptTouchEvent(event: MotionEvent): Boolean =
|
|
69
71
|
gestureReporter?.isRecognizing == true || super.onInterceptTouchEvent(event)
|
|
70
72
|
|
|
73
|
+
/** Before Android 13 only a rounded rectangle outline clips: corners with their own radius clip here. */
|
|
74
|
+
override fun dispatchDraw(canvas: Canvas) {
|
|
75
|
+
val box = background as? BoxDrawable
|
|
76
|
+
if (!clipToOutline || box?.hasCornerRadii != true || Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
77
|
+
return super.dispatchDraw(canvas)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
canvas.save()
|
|
81
|
+
canvas.clipPath(box.outlinePath())
|
|
82
|
+
super.dispatchDraw(canvas)
|
|
83
|
+
canvas.restore()
|
|
84
|
+
}
|
|
85
|
+
|
|
71
86
|
/** Without an exact size (inside a scroll view), the size set in its layout params. */
|
|
72
87
|
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
73
88
|
setMeasuredDimension(
|
|
@@ -549,28 +564,7 @@ class RemoteImageView(context: Context) : ImageView(context) {
|
|
|
549
564
|
show(if (source != null && cachePolicy != "none") decodedImages.get(source) else null)
|
|
550
565
|
if (source == null || bitmap != null) return
|
|
551
566
|
|
|
552
|
-
|
|
553
|
-
val readsDisk = cachePolicy == "disk"
|
|
554
|
-
loader.execute {
|
|
555
|
-
val cachedBytes = if (readsDisk) diskCache.read(source) else null
|
|
556
|
-
val downloadedBytes = if (cachedBytes == null) runCatching { download(source) }.getOrNull() else null
|
|
557
|
-
val bytes = cachedBytes ?: downloadedBytes ?: return@execute
|
|
558
|
-
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) ?: return@execute
|
|
559
|
-
|
|
560
|
-
post {
|
|
561
|
-
store(bitmap, downloadedBytes, source, diskCache)
|
|
562
|
-
if (source == this.source) show(bitmap)
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
private fun store(bitmap: Bitmap, downloadedBytes: ByteArray?, source: String, diskCache: ImageDiskCache) {
|
|
568
|
-
if (cachePolicy == "none") return
|
|
569
|
-
|
|
570
|
-
decodedImages.put(source, bitmap)
|
|
571
|
-
if (cachePolicy != "disk" || downloadedBytes == null || isInMemory(source)) return
|
|
572
|
-
|
|
573
|
-
loader.execute { diskCache.write(source, downloadedBytes) }
|
|
567
|
+
loadBitmap(context, source, { cachePolicy }) { loaded -> if (source == this.source) show(loaded) }
|
|
574
568
|
}
|
|
575
569
|
|
|
576
570
|
private fun show(nextBitmap: Bitmap?) {
|
|
@@ -611,18 +605,61 @@ class RemoteImageView(context: Context) : ImageView(context) {
|
|
|
611
605
|
setMeasuredDimension(measuredWidth.roundToInt(), measuredHeight.roundToInt())
|
|
612
606
|
}
|
|
613
607
|
|
|
614
|
-
|
|
615
|
-
|
|
608
|
+
companion object {
|
|
609
|
+
private val loader = Executors.newFixedThreadPool(4)
|
|
610
|
+
private val mainThread = Handler(Looper.getMainLooper())
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* An image as <Image> loads it: from memory, the disk cache or its url, then kept as [cachePolicy] says
|
|
614
|
+
* (read once it has loaded, so it applies even when set after the source). [onLoaded], or [onFailed], runs
|
|
615
|
+
* on the main thread.
|
|
616
|
+
*/
|
|
617
|
+
fun loadBitmap(
|
|
618
|
+
context: Context,
|
|
619
|
+
source: String,
|
|
620
|
+
cachePolicy: () -> String = { "disk" },
|
|
621
|
+
onFailed: () -> Unit = {},
|
|
622
|
+
onLoaded: (Bitmap) -> Unit,
|
|
623
|
+
) {
|
|
624
|
+
if (cachePolicy() != "none") decodedImages.get(source)?.let { return onLoaded(it) }
|
|
625
|
+
|
|
626
|
+
val diskCache = ImageDiskCache(context.cacheDir)
|
|
627
|
+
val readsDisk = cachePolicy() == "disk"
|
|
628
|
+
loader.execute {
|
|
629
|
+
val cachedBytes = if (readsDisk) diskCache.read(source) else null
|
|
630
|
+
val downloadedBytes = if (cachedBytes == null) runCatching { download(source) }.getOrNull() else null
|
|
631
|
+
val bytes = cachedBytes ?: downloadedBytes
|
|
632
|
+
val bitmap = bytes?.let { BitmapFactory.decodeByteArray(it, 0, it.size) }
|
|
633
|
+
if (bitmap == null) {
|
|
634
|
+
mainThread.post(onFailed)
|
|
635
|
+
return@execute
|
|
636
|
+
}
|
|
616
637
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
638
|
+
mainThread.post {
|
|
639
|
+
store(bitmap, downloadedBytes, source, diskCache, cachePolicy())
|
|
640
|
+
onLoaded(bitmap)
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
620
644
|
|
|
621
|
-
|
|
622
|
-
|
|
645
|
+
private fun store(bitmap: Bitmap, downloadedBytes: ByteArray?, source: String, diskCache: ImageDiskCache, cachePolicy: String) {
|
|
646
|
+
if (cachePolicy == "none") return
|
|
623
647
|
|
|
624
|
-
|
|
625
|
-
|
|
648
|
+
decodedImages.put(source, bitmap)
|
|
649
|
+
if (cachePolicy != "disk" || downloadedBytes == null || isInMemory(source)) return
|
|
650
|
+
|
|
651
|
+
loader.execute { diskCache.write(source, downloadedBytes) }
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/** data: and blob: images are already on the device: nothing to keep on disk. */
|
|
655
|
+
private fun isInMemory(source: String) = source.startsWith("data:") || source.startsWith("blob:")
|
|
656
|
+
|
|
657
|
+
private fun download(source: String): ByteArray {
|
|
658
|
+
if (source.startsWith("data:")) return Base64.decode(source.substringAfter(","), Base64.DEFAULT)
|
|
659
|
+
if (source.startsWith("blob:")) return BlobStore.shared.bytes(source) ?: throw IOException("The blob has been released")
|
|
660
|
+
|
|
661
|
+
return URL(source).openStream().use { it.readBytes() }
|
|
662
|
+
}
|
|
626
663
|
|
|
627
664
|
/** Decoded images, an eighth of the app's memory at most: one that comes back on screen shows at once. */
|
|
628
665
|
private val decodedImages = object : LruCache<String, Bitmap>((Runtime.getRuntime().maxMemory() / 8).toInt()) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<!-- The header's back arrow (Lucide's arrow-left), tinted by its button. -->
|
|
3
|
+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
|
4
|
+
android:width="24dp"
|
|
5
|
+
android:height="24dp"
|
|
6
|
+
android:viewportWidth="24"
|
|
7
|
+
android:viewportHeight="24">
|
|
8
|
+
<path
|
|
9
|
+
android:pathData="M19,12H5M12,19l-7,-7 7,-7"
|
|
10
|
+
android:strokeWidth="2"
|
|
11
|
+
android:strokeColor="#FF000000"
|
|
12
|
+
android:strokeLineCap="round"
|
|
13
|
+
android:strokeLineJoin="round" />
|
|
14
|
+
</vector>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<!-- A full screen modal's close icon (Lucide's x), tinted by its button. -->
|
|
3
|
+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
|
4
|
+
android:width="24dp"
|
|
5
|
+
android:height="24dp"
|
|
6
|
+
android:viewportWidth="24"
|
|
7
|
+
android:viewportHeight="24">
|
|
8
|
+
<path
|
|
9
|
+
android:pathData="M18,6 6,18M6,6l12,12"
|
|
10
|
+
android:strokeWidth="2"
|
|
11
|
+
android:strokeColor="#FF000000"
|
|
12
|
+
android:strokeLineCap="round"
|
|
13
|
+
android:strokeLineJoin="round" />
|
|
14
|
+
</vector>
|
package/cli/ios-project.mjs
CHANGED
|
@@ -544,7 +544,7 @@ function nativueInfoPlist(app) {
|
|
|
544
544
|
],
|
|
545
545
|
NSAppTransportSecurity: { NSAllowsLocalNetworking: true },
|
|
546
546
|
NSLocalNetworkUsageDescription:
|
|
547
|
-
'Connects to the
|
|
547
|
+
'Connects to the nativue dev server on your Mac for hot reload.',
|
|
548
548
|
UILaunchScreen: { UIColorName: 'LaunchBackground', UIImageName: 'LaunchLogo' },
|
|
549
549
|
}
|
|
550
550
|
}
|
package/cli/link.mjs
CHANGED
|
@@ -46,14 +46,33 @@ export function nativuePackages(appDirectory) {
|
|
|
46
46
|
if (!existsSync(join(appDirectory, 'package.json'))) return []
|
|
47
47
|
|
|
48
48
|
const { dependencies = {}, devDependencies = {} } = readJson(join(appDirectory, 'package.json'))
|
|
49
|
-
const
|
|
49
|
+
const visited = new Set()
|
|
50
|
+
const found = []
|
|
51
|
+
const pending = [...Object.keys(dependencies), ...Object.keys(devDependencies)].map((name) => [
|
|
52
|
+
name,
|
|
53
|
+
appDirectory,
|
|
54
|
+
])
|
|
55
|
+
// The app's packages, then theirs: a package made for nativue brings the native packages it
|
|
56
|
+
// depends on (@nativue/icons brings @nativue/svg).
|
|
57
|
+
const visit = (name, fromDirectory) => {
|
|
58
|
+
if (visited.has(name)) return
|
|
59
|
+
visited.add(name)
|
|
60
|
+
|
|
61
|
+
const directory = packageDirectory(name, fromDirectory)
|
|
62
|
+
const manifest = directory && readJson(join(directory, 'package.json'))
|
|
63
|
+
const isForNativue = manifest?.nativue || manifest?.peerDependencies?.['@nativue/core']
|
|
64
|
+
|
|
65
|
+
if (manifest?.nativue) found.push({ name, directory, nativue: manifest.nativue })
|
|
66
|
+
if (isForNativue) {
|
|
67
|
+
Object.keys(manifest.dependencies ?? {}).forEach((dependency) =>
|
|
68
|
+
pending.push([dependency, directory]),
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
50
72
|
|
|
51
|
-
|
|
52
|
-
const directory = packageDirectory(name, appDirectory)
|
|
53
|
-
const nativue = directory && readJson(join(directory, 'package.json')).nativue
|
|
73
|
+
while (pending.length > 0) visit(...pending.shift())
|
|
54
74
|
|
|
55
|
-
|
|
56
|
-
})
|
|
75
|
+
return found
|
|
57
76
|
}
|
|
58
77
|
|
|
59
78
|
function iosPlugins(appDirectory) {
|
package/cli/nativue.mjs
CHANGED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
|
|
3
|
+
/// backgroundImage: CSS gradients (src/gradients.ts) under a view's content and over its backgroundColor,
|
|
4
|
+
/// in a layer that follows its bounds and corners. The first gradient is on top, as in CSS.
|
|
5
|
+
final class BackgroundGradientsLayer: CALayer {
|
|
6
|
+
private var gradients: [[String: Any]] = []
|
|
7
|
+
|
|
8
|
+
func setGradients(_ gradients: [[String: Any]]) {
|
|
9
|
+
self.gradients = gradients
|
|
10
|
+
sublayers = gradients.reversed().map(GradientLayer.init)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/// Their geometry depends on the size: an angle's line, a corner, a radius.
|
|
14
|
+
func layoutGradients() {
|
|
15
|
+
for layer in sublayers ?? [] where layer.frame != bounds {
|
|
16
|
+
layer.frame = bounds
|
|
17
|
+
layer.setNeedsDisplay()
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// One gradient, drawn in points: CAGradientLayer works in a unit square stretched to the layer, which
|
|
23
|
+
/// tilts an angle on a box that isn't square.
|
|
24
|
+
private final class GradientLayer: CALayer {
|
|
25
|
+
private let gradient: [String: Any]
|
|
26
|
+
|
|
27
|
+
init(_ gradient: [String: Any]) {
|
|
28
|
+
self.gradient = gradient
|
|
29
|
+
super.init()
|
|
30
|
+
contentsScale = UIScreen.main.scale
|
|
31
|
+
needsDisplayOnBoundsChange = true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
override init(layer: Any) {
|
|
35
|
+
gradient = (layer as? GradientLayer)?.gradient ?? [:]
|
|
36
|
+
super.init(layer: layer)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
required init?(coder: NSCoder) {
|
|
40
|
+
fatalError("init(coder:) is not supported")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
override func draw(in context: CGContext) {
|
|
44
|
+
CSSGradient.draw(gradient, in: context, size: bounds.size)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
extension UIView {
|
|
49
|
+
/// The gradients layer, created under the view's subviews the first time it's needed. A label or an image
|
|
50
|
+
/// view draws its content in its own layer, which the gradients would cover: they get none.
|
|
51
|
+
func setBackgroundGradients(_ gradients: [[String: Any]]) {
|
|
52
|
+
guard !(self is UILabel || self is UIImageView) else { return }
|
|
53
|
+
|
|
54
|
+
let existing = layer.sublayers?.first { $0 is BackgroundGradientsLayer } as? BackgroundGradientsLayer
|
|
55
|
+
guard !gradients.isEmpty else {
|
|
56
|
+
existing?.removeFromSuperlayer()
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let gradientsLayer = existing ?? BackgroundGradientsLayer()
|
|
61
|
+
if existing == nil {
|
|
62
|
+
// Behind the subviews' layers, wherever they're inserted.
|
|
63
|
+
gradientsLayer.zPosition = -1
|
|
64
|
+
layer.insertSublayer(gradientsLayer, at: 0)
|
|
65
|
+
}
|
|
66
|
+
gradientsLayer.setGradients(gradients)
|
|
67
|
+
applyBackgroundGradients()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Fitted to the view's bounds and corners, again as its frame changes: at once, or along with a
|
|
71
|
+
/// LayoutAnimation. A scroll view's bounds move as it scrolls, keeping the gradients in place.
|
|
72
|
+
func applyBackgroundGradients() {
|
|
73
|
+
guard let gradientsLayer = layer.sublayers?.first(where: { $0 is BackgroundGradientsLayer }) as? BackgroundGradientsLayer
|
|
74
|
+
else { return }
|
|
75
|
+
|
|
76
|
+
let animationDuration = UIView.inheritedAnimationDuration
|
|
77
|
+
|
|
78
|
+
CATransaction.begin()
|
|
79
|
+
CATransaction.setDisableActions(animationDuration == 0)
|
|
80
|
+
CATransaction.setAnimationDuration(animationDuration)
|
|
81
|
+
gradientsLayer.frame = bounds
|
|
82
|
+
gradientsLayer.cornerRadius = layer.cornerRadius
|
|
83
|
+
gradientsLayer.maskedCorners = layer.maskedCorners
|
|
84
|
+
gradientsLayer.masksToBounds = true
|
|
85
|
+
gradientsLayer.layoutGradients()
|
|
86
|
+
CATransaction.commit()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Where CSS puts a gradient in a box of `size`, as Android's BackgroundGradients does.
|
|
91
|
+
enum CSSGradient {
|
|
92
|
+
static func draw(_ gradient: [String: Any], in context: CGContext, size: CGSize) {
|
|
93
|
+
let colors = (gradient["colors"] as? [Any] ?? []).compactMap { UIColor(rgba: $0)?.cgColor }
|
|
94
|
+
let locations = (gradient["positions"] as? [NSNumber] ?? []).map { CGFloat($0.doubleValue) }
|
|
95
|
+
guard
|
|
96
|
+
size.width > 0, size.height > 0, colors.count == locations.count,
|
|
97
|
+
let cgGradient = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB), colors: colors as CFArray, locations: locations)
|
|
98
|
+
else { return }
|
|
99
|
+
|
|
100
|
+
// Past the ends, the end colors, as CSS pads them.
|
|
101
|
+
let extend: CGGradientDrawingOptions = [.drawsBeforeStartLocation, .drawsAfterEndLocation]
|
|
102
|
+
|
|
103
|
+
if gradient["type"] as? String == "radial" {
|
|
104
|
+
let (center, radii) = radial(gradient, size: size)
|
|
105
|
+
let radius = max(radii.width, 0.01)
|
|
106
|
+
|
|
107
|
+
// A circle, stretched into the ellipse.
|
|
108
|
+
context.translateBy(x: center.x, y: center.y)
|
|
109
|
+
context.scaleBy(x: 1, y: max(radii.height, 0.01) / radius)
|
|
110
|
+
context.drawRadialGradient(
|
|
111
|
+
cgGradient, startCenter: .zero, startRadius: 0, endCenter: .zero, endRadius: radius, options: extend
|
|
112
|
+
)
|
|
113
|
+
} else {
|
|
114
|
+
let (start, end) = linear(gradient, size: size)
|
|
115
|
+
|
|
116
|
+
context.drawLinearGradient(cgGradient, start: start, end: end, options: extend)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// The gradient line: through the center, as long as the box is along it, so its ends touch the corners.
|
|
121
|
+
static func linear(_ gradient: [String: Any], size: CGSize) -> (start: CGPoint, end: CGPoint) {
|
|
122
|
+
var direction = CGVector(dx: 0, dy: 1)
|
|
123
|
+
|
|
124
|
+
if let sides = gradient["to"] as? [String] {
|
|
125
|
+
let x: CGFloat = sides.contains("right") ? 1 : sides.contains("left") ? -1 : 0
|
|
126
|
+
let y: CGFloat = sides.contains("bottom") ? 1 : sides.contains("top") ? -1 : 0
|
|
127
|
+
// To a corner: perpendicular to the diagonal between the two corners next to it.
|
|
128
|
+
direction = x != 0 && y != 0 ? CGVector(dx: x * size.height, dy: y * size.width) : CGVector(dx: x, dy: y)
|
|
129
|
+
} else if let angle = (gradient["angle"] as? NSNumber).map({ CGFloat($0.doubleValue) * .pi / 180 }) {
|
|
130
|
+
// 0 points up, 90 right.
|
|
131
|
+
direction = CGVector(dx: sin(angle), dy: -cos(angle))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let length = hypot(direction.dx, direction.dy)
|
|
135
|
+
guard length > 0 else { return (.zero, CGPoint(x: 0, y: size.height)) }
|
|
136
|
+
|
|
137
|
+
let unitDirection = CGVector(dx: direction.dx / length, dy: direction.dy / length)
|
|
138
|
+
let halfLine = (abs(size.width * unitDirection.dx) + abs(size.height * unitDirection.dy)) / 2
|
|
139
|
+
let center = CGPoint(x: size.width / 2, y: size.height / 2)
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
CGPoint(x: center.x - unitDirection.dx * halfLine, y: center.y - unitDirection.dy * halfLine),
|
|
143
|
+
CGPoint(x: center.x + unitDirection.dx * halfLine, y: center.y + unitDirection.dy * halfLine)
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/// The center and the radii: given, or reaching the extent keyword's side or corner.
|
|
148
|
+
static func radial(_ gradient: [String: Any], size: CGSize) -> (center: CGPoint, radii: CGSize) {
|
|
149
|
+
let center = CGPoint(
|
|
150
|
+
x: length(gradient["center"], index: 0, of: size.width, default: 0.5),
|
|
151
|
+
y: length(gradient["center"], index: 1, of: size.height, default: 0.5)
|
|
152
|
+
)
|
|
153
|
+
let isCircle = gradient["shape"] as? String == "circle"
|
|
154
|
+
|
|
155
|
+
if gradient["radii"] != nil {
|
|
156
|
+
let radiusX = length(gradient["radii"], index: 0, of: size.width, default: 0.5)
|
|
157
|
+
let radiusY = isCircle ? radiusX : length(gradient["radii"], index: 1, of: size.height, default: 0.5)
|
|
158
|
+
|
|
159
|
+
return (center, CGSize(width: radiusX, height: radiusY))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let horizontal = [center.x, size.width - center.x].map(abs)
|
|
163
|
+
let vertical = [center.y, size.height - center.y].map(abs)
|
|
164
|
+
let extent = gradient["extent"] as? String ?? "farthest-corner"
|
|
165
|
+
let pick = extent.hasPrefix("closest") ? { (values: [CGFloat]) in values.min() ?? 0 } : { $0.max() ?? 0 }
|
|
166
|
+
let (sideX, sideY) = (pick(horizontal), pick(vertical))
|
|
167
|
+
|
|
168
|
+
switch (extent.hasSuffix("corner"), isCircle) {
|
|
169
|
+
case (true, true):
|
|
170
|
+
let radius = hypot(sideX, sideY)
|
|
171
|
+
return (center, CGSize(width: radius, height: radius))
|
|
172
|
+
case (true, false):
|
|
173
|
+
// An ellipse through the corner, shaped like the one that would touch the sides.
|
|
174
|
+
return (center, CGSize(width: sideX * sqrt(2), height: sideY * sqrt(2)))
|
|
175
|
+
case (false, true):
|
|
176
|
+
let radius = pick([sideX, sideY])
|
|
177
|
+
return (center, CGSize(width: radius, height: radius))
|
|
178
|
+
case (false, false):
|
|
179
|
+
return (center, CGSize(width: sideX, height: sideY))
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/// [[fraction, points], ...][index], in points of a side `side` long.
|
|
184
|
+
private static func length(_ value: Any?, index: Int, of side: CGFloat, default fraction: CGFloat) -> CGFloat {
|
|
185
|
+
guard let lengths = value as? [[NSNumber]], index < lengths.count, lengths[index].count == 2 else {
|
|
186
|
+
return fraction * side
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return CGFloat(lengths[index][0].doubleValue) * side + CGFloat(lengths[index][1].doubleValue)
|
|
190
|
+
}
|
|
191
|
+
}
|
package/ios/Bridge.swift
CHANGED
|
@@ -297,7 +297,7 @@ final class Bridge {
|
|
|
297
297
|
|
|
298
298
|
@MainActor func reload() async throws {
|
|
299
299
|
let data = try await download(Self.devServerURL.appendingPathComponent("bundle.js"))
|
|
300
|
-
print("[
|
|
300
|
+
print("[nativue] reload")
|
|
301
301
|
run(data)
|
|
302
302
|
}
|
|
303
303
|
|
|
@@ -461,7 +461,7 @@ final class Bridge {
|
|
|
461
461
|
|
|
462
462
|
/// Logged always; shown over the app in development builds.
|
|
463
463
|
private func reportError(message: String, details: String) {
|
|
464
|
-
print("[
|
|
464
|
+
print("[nativue] JS error:", message, "\n" + details)
|
|
465
465
|
|
|
466
466
|
#if DEBUG
|
|
467
467
|
Task { @MainActor [unowned self] in
|
|
@@ -510,7 +510,7 @@ final class Bridge {
|
|
|
510
510
|
guard let parent = nodesById[parentId], let child = nodesById[childId] else { return }
|
|
511
511
|
|
|
512
512
|
guard parent.canHaveChildren else {
|
|
513
|
-
print("[
|
|
513
|
+
print("[nativue] <\(type(of: parent.view))> can't have native children; use <View>")
|
|
514
514
|
return
|
|
515
515
|
}
|
|
516
516
|
|
|
@@ -638,9 +638,6 @@ final class Bridge {
|
|
|
638
638
|
case "SwipeableView":
|
|
639
639
|
return NativeNode(view: SwipeableView(frame: .zero), measuresContent: false)
|
|
640
640
|
|
|
641
|
-
case "Svg":
|
|
642
|
-
return NativeNode(view: SvgView(frame: .zero), measuresContent: false)
|
|
643
|
-
|
|
644
641
|
case "PressableView":
|
|
645
642
|
let pressableView = PressableView()
|
|
646
643
|
pressableView.onEvent = { [unowned self] eventName in dispatchEvent(eventName, toNodeId: id) }
|
|
@@ -901,14 +898,17 @@ final class Bridge {
|
|
|
901
898
|
guard let node = nodesById[id] else { return }
|
|
902
899
|
|
|
903
900
|
(node.view as? UILabel)?.text = text
|
|
904
|
-
if node.
|
|
901
|
+
if node.hasTextAttributes { node.updateTextAttributes() }
|
|
905
902
|
node.markContentChanged()
|
|
906
903
|
}
|
|
907
904
|
|
|
905
|
+
/// What nativue applies to any view, plugin views included: style, accessibility, layout and gestures.
|
|
906
|
+
private static let viewProps: Set<String> = ["style", "accessibility", "onLayout", "onPan", "onPinch", "onRotate"]
|
|
907
|
+
|
|
908
908
|
private func setProp(_ key: String, value: Any?, onNodeId id: Int) {
|
|
909
909
|
guard let node = nodesById[id] else { return }
|
|
910
910
|
|
|
911
|
-
if key
|
|
911
|
+
if !Self.viewProps.contains(key), let pluginView = node.view as? PluginView {
|
|
912
912
|
pluginView.setProp(key, value: value is NSNull ? nil : value)
|
|
913
913
|
return
|
|
914
914
|
}
|
|
@@ -936,9 +936,13 @@ final class Bridge {
|
|
|
936
936
|
|
|
937
937
|
case "tabBarItem":
|
|
938
938
|
let item = value as? [String: Any] ?? [:]
|
|
939
|
-
screenController(id)
|
|
939
|
+
let screen = screenController(id)
|
|
940
|
+
let image = screen.flatMap { screen in
|
|
941
|
+
NavigationIcons.image(item, size: 25, for: screen) { [weak screen] image in screen?.tabBarItem.image = image }
|
|
942
|
+
}
|
|
943
|
+
screen?.setTabBarItem(
|
|
940
944
|
label: item["label"] as? String,
|
|
941
|
-
image:
|
|
945
|
+
image: image,
|
|
942
946
|
badge: (item["badge"] as? CustomStringConvertible)?.description
|
|
943
947
|
)
|
|
944
948
|
|
|
@@ -1001,16 +1005,6 @@ final class Bridge {
|
|
|
1001
1005
|
|
|
1002
1006
|
case "color":
|
|
1003
1007
|
(node.view as? UIActivityIndicatorView)?.color = UIColor(rgba: value as Any)
|
|
1004
|
-
(node.view as? SvgView)?.color = UIColor(rgba: value as Any) ?? .black
|
|
1005
|
-
|
|
1006
|
-
case "viewBox":
|
|
1007
|
-
if let viewBox = SvgView.rect(fromViewBox: value) { (node.view as? SvgView)?.viewBox = viewBox }
|
|
1008
|
-
|
|
1009
|
-
case "elements":
|
|
1010
|
-
(node.view as? SvgView)?.elements = value as? [[Any]] ?? []
|
|
1011
|
-
|
|
1012
|
-
case "strokeWidth":
|
|
1013
|
-
(node.view as? SvgView)?.strokeWidth = CGFloat((value as? NSNumber)?.doubleValue ?? 2)
|
|
1014
1008
|
|
|
1015
1009
|
case "visible":
|
|
1016
1010
|
modalController(id)?.setVisible(isTruthyAttribute(value))
|