@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
package/AGENTS.md
CHANGED
|
@@ -18,7 +18,16 @@ doesn't work. Full docs: https://github.com/nativue/nativue/tree/main/docs
|
|
|
18
18
|
|
|
19
19
|
Global, with no import: `View`, `Text`, `Image`, `ImageBackground`, `Pressable`, `Button`, `TextInput`,
|
|
20
20
|
`Switch`, `ScrollView`, `FlatList`, `Swipeable`, `Modal`, `ActivityIndicator`, `SafeAreaView`,
|
|
21
|
-
`KeyboardAvoidingView
|
|
21
|
+
`KeyboardAvoidingView`.
|
|
22
|
+
|
|
23
|
+
- Charts and drawings: `<svg>` as on the web, with `@nativue/svg` installed (`@nativue/icons` brings
|
|
24
|
+
it): shapes, `text`/`tspan`/`textPath`, gradients and `pattern`s in `defs` (`fill="url(#id)"`),
|
|
25
|
+
`clipPath`, `mask`, `use`/`symbol`, `marker`, `image`, drop shadows and blur filters, and `@press`
|
|
26
|
+
on an element. Also as components to import: `import { Svg, Path, Text as SvgText } from
|
|
27
|
+
'@nativue/svg'` (rename `Text` and `Image`, which are nativue's). Without the package, `<svg>`
|
|
28
|
+
shows nothing. An imported `.svg` file is a component (`import Logo from '@/assets/logo.svg'`, with
|
|
29
|
+
`/// <reference types="@nativue/svg/client" />` in `src/env.d.ts`), and a `<style>` inside an `<svg>`
|
|
30
|
+
styles its elements by tag, class and id.
|
|
22
31
|
|
|
23
32
|
- Text must be inside a `<Text>`, never directly in a `<View>`.
|
|
24
33
|
- Taps: `<Pressable @press="save">` (also `@long-press`, and `v-slot="{ pressed }"`), or
|
|
@@ -36,10 +45,13 @@ Global, with no import: `View`, `Text`, `Image`, `ImageBackground`, `Pressable`,
|
|
|
36
45
|
`:style="{ padding: 16, borderRadius: 12, backgroundColor: '#ffffff' }"`. Arrays merge, later wins:
|
|
37
46
|
`:style="[styles.card, selected && styles.selected]"`.
|
|
38
47
|
- `StyleSheet.create({...})` (from `vue`) groups and types a screen's styles.
|
|
48
|
+
- Gradients: `backgroundImage: 'linear-gradient(135deg, #4af2ae, #10b39b)'` or `radial-gradient(...)`,
|
|
49
|
+
as in CSS, on a `View`; with Tailwind, `bg-linear-to-r from-emerald-400 to-teal-600`.
|
|
39
50
|
- Every view is a flexbox container stacking its children in a **column** by default; `flex: 1` takes
|
|
40
51
|
the space left. Percentages (`'50%'`) work for sizes.
|
|
41
|
-
- Text styles (`color`, `fontSize`, `fontWeight`, `textAlign`, `textDecorationLine
|
|
42
|
-
`<Text>` itself: native views
|
|
52
|
+
- Text styles (`color`, `fontSize`, `fontWeight`, `fontStyle`, `textAlign`, `textDecorationLine`,
|
|
53
|
+
`textTransform`, `letterSpacing`, `lineHeight`, `lineClamp`) go on the `<Text>` itself: native views
|
|
54
|
+
don't inherit them from their parents.
|
|
43
55
|
- Pressed and focused states: `pressed-style` on `Pressable`, `focused-style` on `TextInput`.
|
|
44
56
|
- Dark mode: `useColorScheme()` (from `vue`) gives `'light'` or `'dark'`, live.
|
|
45
57
|
- Platform: `Platform.OS` is `'ios'` or `'android'`; `Platform.select({ ios, android })`.
|
|
@@ -94,7 +106,9 @@ Import the runtime from `vue`: `ref`, `computed`, `StyleSheet`, `Animated`, `Lay
|
|
|
94
106
|
|
|
95
107
|
- `@/` points at `src/`. Shared components go in `src/components`, everything else in `src/lib`.
|
|
96
108
|
- Vue plugins (Pinia...) are installed in `src/app.ts`: `export default function setup(app: App)`.
|
|
97
|
-
-
|
|
109
|
+
- Tab and header icons (`tabBarIcon`, a header item's `icon`): an `@nativue/icons` component,
|
|
110
|
+
`svgIcon(viewBox, elements)` from `@nativue/svg`, an image `{ uri }`, or an SF Symbol name (iOS).
|
|
111
|
+
- Official packages: `@nativue/svg`, `@nativue/icons` (icon components), `@nativue/tailwind`, `@nativue/haptics`,
|
|
98
112
|
`@nativue/clipboard` (copy text), `@nativue/notifications` (local notifications), `@nativue/sqlite`.
|
|
99
113
|
A package with native code needs the app
|
|
100
114
|
rebuilt with `npm run ios` or `npm run android`, not just a reload.
|
|
@@ -18,18 +18,38 @@ fun packageDirectory(name: String, appDirectory: File): File? =
|
|
|
18
18
|
.map { File(it, "node_modules/$name") }
|
|
19
19
|
.firstOrNull { File(it, "package.json").isFile }
|
|
20
20
|
|
|
21
|
-
fun dependencyNames(
|
|
21
|
+
fun dependencyNames(packageJson: Map<String, Any?>, keys: List<String>): List<String> =
|
|
22
|
+
keys.flatMap { (packageJson[it] as? Map<*, *>)?.keys.orEmpty() }.map { it.toString() }
|
|
23
|
+
|
|
24
|
+
data class AndroidPlugin(val projectPath: String, val pluginClass: String)
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The app's packages with an Android side, then theirs, as cli/link.mjs finds them: a package made for
|
|
28
|
+
* nativue brings the native packages it depends on (@nativue/icons brings @nativue/svg).
|
|
29
|
+
*/
|
|
30
|
+
fun androidPlugins(appDirectory: File): List<AndroidPlugin> {
|
|
22
31
|
val appPackage = readJson(File(appDirectory, "package.json"))
|
|
32
|
+
val pending = ArrayDeque(dependencyNames(appPackage, listOf("dependencies", "devDependencies")).map { it to appDirectory })
|
|
33
|
+
val visited = mutableSetOf<String>()
|
|
34
|
+
val plugins = mutableListOf<AndroidPlugin>()
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
while (pending.isNotEmpty()) {
|
|
37
|
+
val (name, fromDirectory) = pending.removeFirst()
|
|
38
|
+
if (!visited.add(name)) continue
|
|
27
39
|
|
|
28
|
-
|
|
40
|
+
val directory = packageDirectory(name, fromDirectory) ?: continue
|
|
41
|
+
val packageJson = readJson(File(directory, "package.json"))
|
|
42
|
+
val nativue = packageJson["nativue"] as? Map<*, *>
|
|
43
|
+
val isForNativue = nativue != null || (packageJson["peerDependencies"] as? Map<*, *>)?.containsKey("@nativue/core") == true
|
|
44
|
+
|
|
45
|
+
androidPlugin(name, directory, nativue)?.let(plugins::add)
|
|
46
|
+
if (isForNativue) dependencyNames(packageJson, listOf("dependencies")).forEach { pending.add(it to directory) }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return plugins
|
|
50
|
+
}
|
|
29
51
|
|
|
30
|
-
fun androidPlugin(name: String,
|
|
31
|
-
val directory = packageDirectory(name, appDirectory) ?: return null
|
|
32
|
-
val nativue = readJson(File(directory, "package.json"))["nativue"] as? Map<*, *>
|
|
52
|
+
fun androidPlugin(name: String, directory: File, nativue: Map<*, *>?): AndroidPlugin? {
|
|
33
53
|
val android = nativue?.get("android") as? Map<*, *> ?: return null
|
|
34
54
|
val projectPath = ":" + name.removePrefix("@").replace('/', '-')
|
|
35
55
|
|
|
@@ -44,7 +64,7 @@ fun pascalCase(name: String): String =
|
|
|
44
64
|
name.split(Regex("[^a-zA-Z0-9]+")).filter { it.isNotEmpty() }.joinToString("") { it.replaceFirstChar(Char::uppercaseChar) }
|
|
45
65
|
|
|
46
66
|
val appDirectory: File = settingsDir.parentFile
|
|
47
|
-
val androidPlugins =
|
|
67
|
+
val androidPlugins = androidPlugins(appDirectory)
|
|
48
68
|
val appSourceDirectories = File(appDirectory, "native").listFiles().orEmpty()
|
|
49
69
|
// native/extensions holds iOS extension targets, not modules.
|
|
50
70
|
.filter { it.name != "extensions" }
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
package dev.nativue
|
|
2
|
+
|
|
3
|
+
import android.graphics.LinearGradient
|
|
4
|
+
import android.graphics.Matrix
|
|
5
|
+
import android.graphics.PointF
|
|
6
|
+
import android.graphics.RadialGradient
|
|
7
|
+
import android.graphics.Shader
|
|
8
|
+
import kotlin.math.abs
|
|
9
|
+
import kotlin.math.cos
|
|
10
|
+
import kotlin.math.hypot
|
|
11
|
+
import kotlin.math.max
|
|
12
|
+
import kotlin.math.min
|
|
13
|
+
import kotlin.math.sin
|
|
14
|
+
import kotlin.math.sqrt
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* backgroundImage: a CSS gradient (src/gradients.ts) as a shader for a box [width] by [height] pixels, where
|
|
18
|
+
* CSS puts it, as iOS's CSSGradient does. Lengths arrive in dp.
|
|
19
|
+
*/
|
|
20
|
+
object BackgroundGradients {
|
|
21
|
+
fun shader(gradient: Map<*, *>, width: Float, height: Float, density: Float): Shader? {
|
|
22
|
+
val colors = (gradient["colors"] as? List<*>).orEmpty().mapNotNull(::androidColor).toIntArray()
|
|
23
|
+
val positions = (gradient["positions"] as? List<*>).orEmpty().mapNotNull { (it as? Number)?.toFloat() }.toFloatArray()
|
|
24
|
+
if (colors.size < 2 || colors.size != positions.size || width <= 0 || height <= 0) return null
|
|
25
|
+
|
|
26
|
+
if (gradient["type"] != "radial") {
|
|
27
|
+
val (start, end) = linear(gradient, width, height)
|
|
28
|
+
|
|
29
|
+
return LinearGradient(start.x, start.y, end.x, end.y, colors, positions, Shader.TileMode.CLAMP)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
val (center, radii) = radial(gradient, width, height, density)
|
|
33
|
+
val radiusX = max(radii.x, 0.01f)
|
|
34
|
+
|
|
35
|
+
// Android's is a circle: stretched into the ellipse.
|
|
36
|
+
return RadialGradient(center.x, center.y, radiusX, colors, positions, Shader.TileMode.CLAMP).apply {
|
|
37
|
+
setLocalMatrix(Matrix().apply { setScale(1f, max(radii.y, 0.01f) / radiusX, center.x, center.y) })
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The gradient line: through the center, as long as the box is along it, so its ends touch the corners. */
|
|
42
|
+
private fun linear(gradient: Map<*, *>, width: Float, height: Float): Pair<PointF, PointF> {
|
|
43
|
+
val sides = (gradient["to"] as? List<*>)?.map { it.toString() }
|
|
44
|
+
val angle = (gradient["angle"] as? Number)?.toDouble()?.let(Math::toRadians)
|
|
45
|
+
var directionX = 0f
|
|
46
|
+
var directionY = 1f
|
|
47
|
+
|
|
48
|
+
if (sides != null) {
|
|
49
|
+
val x = if ("right" in sides) 1f else if ("left" in sides) -1f else 0f
|
|
50
|
+
val y = if ("bottom" in sides) 1f else if ("top" in sides) -1f else 0f
|
|
51
|
+
// To a corner: perpendicular to the diagonal between the two corners next to it.
|
|
52
|
+
val isCorner = x != 0f && y != 0f
|
|
53
|
+
|
|
54
|
+
directionX = if (isCorner) x * height else x
|
|
55
|
+
directionY = if (isCorner) y * width else y
|
|
56
|
+
} else if (angle != null) {
|
|
57
|
+
// 0 points up, 90 right.
|
|
58
|
+
directionX = sin(angle).toFloat()
|
|
59
|
+
directionY = -cos(angle).toFloat()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
val length = hypot(directionX, directionY)
|
|
63
|
+
if (length == 0f) return PointF(0f, 0f) to PointF(0f, height)
|
|
64
|
+
|
|
65
|
+
val unitX = directionX / length
|
|
66
|
+
val unitY = directionY / length
|
|
67
|
+
val halfLine = (abs(width * unitX) + abs(height * unitY)) / 2
|
|
68
|
+
|
|
69
|
+
return PointF(width / 2 - unitX * halfLine, height / 2 - unitY * halfLine) to
|
|
70
|
+
PointF(width / 2 + unitX * halfLine, height / 2 + unitY * halfLine)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The center and the radii: given, or reaching the extent keyword's side or corner. */
|
|
74
|
+
private fun radial(gradient: Map<*, *>, width: Float, height: Float, density: Float): Pair<PointF, PointF> {
|
|
75
|
+
val center = PointF(
|
|
76
|
+
length(gradient["center"], 0, width, density, 0.5f),
|
|
77
|
+
length(gradient["center"], 1, height, density, 0.5f),
|
|
78
|
+
)
|
|
79
|
+
val isCircle = gradient["shape"] == "circle"
|
|
80
|
+
|
|
81
|
+
if (gradient["radii"] != null) {
|
|
82
|
+
val radiusX = length(gradient["radii"], 0, width, density, 0.5f)
|
|
83
|
+
val radiusY = if (isCircle) radiusX else length(gradient["radii"], 1, height, density, 0.5f)
|
|
84
|
+
|
|
85
|
+
return center to PointF(radiusX, radiusY)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
val extent = gradient["extent"] as? String ?: "farthest-corner"
|
|
89
|
+
val pick = { first: Float, second: Float -> if (extent.startsWith("closest")) min(first, second) else max(first, second) }
|
|
90
|
+
val sideX = pick(abs(center.x), abs(width - center.x))
|
|
91
|
+
val sideY = pick(abs(center.y), abs(height - center.y))
|
|
92
|
+
|
|
93
|
+
val radii = when {
|
|
94
|
+
extent.endsWith("corner") && isCircle -> hypot(sideX, sideY).let { PointF(it, it) }
|
|
95
|
+
// An ellipse through the corner, shaped like the one that would touch the sides.
|
|
96
|
+
extent.endsWith("corner") -> PointF(sideX * sqrt(2f), sideY * sqrt(2f))
|
|
97
|
+
isCircle -> pick(sideX, sideY).let { PointF(it, it) }
|
|
98
|
+
else -> PointF(sideX, sideY)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return center to radii
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** [[fraction, dp], ...][index], in pixels of a side [side] long. */
|
|
105
|
+
private fun length(value: Any?, index: Int, side: Float, density: Float, defaultFraction: Float): Float {
|
|
106
|
+
val pair = ((value as? List<*>)?.getOrNull(index) as? List<*>)?.mapNotNull { (it as? Number)?.toFloat() }
|
|
107
|
+
if (pair == null || pair.size != 2) return defaultFraction * side
|
|
108
|
+
|
|
109
|
+
return pair[0] * side + pair[1] * density
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -4,18 +4,25 @@ import android.graphics.Canvas
|
|
|
4
4
|
import android.graphics.ColorFilter
|
|
5
5
|
import android.graphics.Outline
|
|
6
6
|
import android.graphics.Paint
|
|
7
|
+
import android.graphics.Path
|
|
7
8
|
import android.graphics.PixelFormat
|
|
8
9
|
import android.graphics.RectF
|
|
9
10
|
import android.graphics.drawable.Drawable
|
|
11
|
+
import android.os.Build
|
|
10
12
|
import kotlin.math.max
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
|
-
* A view's box: background color, rounded corners, border and a shadow drawn like iOS layers
|
|
14
|
-
* theirs (color, opacity, blur radius, offset), outside the view's bounds. Sizes in pixels.
|
|
15
|
+
* A view's box: background color and gradients, rounded corners, border and a shadow drawn like iOS layers
|
|
16
|
+
* draw theirs (color, opacity, blur radius, offset), outside the view's bounds. Sizes in pixels.
|
|
15
17
|
*/
|
|
16
18
|
class BoxDrawable : Drawable() {
|
|
17
19
|
var backgroundColor = 0
|
|
20
|
+
/** backgroundImage (BackgroundGradients), the first on top; their lengths in dp, hence the density. */
|
|
21
|
+
var gradients: List<Map<*, *>> = emptyList()
|
|
22
|
+
var density = 1f
|
|
18
23
|
var cornerRadius = 0f
|
|
24
|
+
/** Corners with their own radius, over cornerRadius: top left, top right, bottom right, bottom left. */
|
|
25
|
+
val cornerRadii = arrayOfNulls<Float>(4)
|
|
19
26
|
var borderWidth = 0f
|
|
20
27
|
var borderColor = 0
|
|
21
28
|
|
|
@@ -28,33 +35,62 @@ class BoxDrawable : Drawable() {
|
|
|
28
35
|
private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
29
36
|
private val borderPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE }
|
|
30
37
|
private val rect = RectF()
|
|
38
|
+
private val boxPath = Path()
|
|
39
|
+
private val borderPath = Path()
|
|
40
|
+
|
|
41
|
+
/** Whether a corner has its own radius: the box is then a path, not a rounded rectangle. */
|
|
42
|
+
val hasCornerRadii: Boolean
|
|
43
|
+
get() = cornerRadii.any { it != null }
|
|
31
44
|
|
|
32
45
|
override fun draw(canvas: Canvas) {
|
|
33
46
|
rect.set(bounds)
|
|
47
|
+
val box = roundedPath(boxPath, rect, inset = 0f)
|
|
34
48
|
|
|
49
|
+
fillPaint.shader = null
|
|
35
50
|
fillPaint.color = backgroundColor
|
|
36
|
-
applyShadow()
|
|
37
|
-
if (backgroundColor ushr 24 != 0) canvas.
|
|
51
|
+
applyShadow(castsShadow = backgroundColor ushr 24 != 0)
|
|
52
|
+
if (backgroundColor ushr 24 != 0) canvas.drawPath(box, fillPaint)
|
|
53
|
+
|
|
54
|
+
// The bottom one casts the shadow when there's no background color.
|
|
55
|
+
gradients.asReversed().forEachIndexed { index, gradient ->
|
|
56
|
+
fillPaint.shader = BackgroundGradients.shader(gradient, rect.width(), rect.height(), density) ?: return@forEachIndexed
|
|
57
|
+
fillPaint.color = 0xFF000000.toInt()
|
|
58
|
+
applyShadow(castsShadow = index == 0 && backgroundColor ushr 24 == 0)
|
|
59
|
+
canvas.drawPath(box, fillPaint)
|
|
60
|
+
}
|
|
61
|
+
fillPaint.shader = null
|
|
38
62
|
|
|
39
63
|
if (borderWidth > 0) {
|
|
40
64
|
// The stroke is centered on the path: inset by half so it stays inside, like iOS borders.
|
|
41
65
|
rect.inset(borderWidth / 2, borderWidth / 2)
|
|
42
66
|
borderPaint.color = borderColor
|
|
43
67
|
borderPaint.strokeWidth = borderWidth
|
|
44
|
-
|
|
45
|
-
canvas.drawRoundRect(rect, radius, radius, borderPaint)
|
|
68
|
+
canvas.drawPath(roundedPath(borderPath, rect, inset = borderWidth / 2), borderPaint)
|
|
46
69
|
}
|
|
47
70
|
}
|
|
48
71
|
|
|
72
|
+
/** [path] set to [bounds] with its corners rounded, each radius less [inset]. */
|
|
73
|
+
private fun roundedPath(path: Path, bounds: RectF, inset: Float): Path {
|
|
74
|
+
val radii = FloatArray(8) { index -> max(0f, (cornerRadii[index / 2] ?: cornerRadius) - inset) }
|
|
75
|
+
|
|
76
|
+
path.reset()
|
|
77
|
+
path.addRoundRect(bounds, radii, Path.Direction.CW)
|
|
78
|
+
|
|
79
|
+
return path
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The box's shape, which a ContainerView clips its children to where its outline can't. */
|
|
83
|
+
fun outlinePath(): Path = roundedPath(Path(), RectF(bounds), inset = 0f)
|
|
84
|
+
|
|
49
85
|
/**
|
|
50
86
|
* Core Animation blurs with a sigma of half the shadow radius; Skia's blur radius r gives a sigma of
|
|
51
87
|
* 0.57735 r + 0.5. The shadow takes the fill's alpha, like iOS (an invisible box casts none).
|
|
52
88
|
*/
|
|
53
|
-
private fun applyShadow() {
|
|
89
|
+
private fun applyShadow(castsShadow: Boolean) {
|
|
54
90
|
val sigma = shadowRadius / 2
|
|
55
91
|
val alpha = ((shadowColor ushr 24) * shadowOpacity).toInt().coerceIn(0, 255)
|
|
56
92
|
|
|
57
|
-
if (alpha == 0 ||
|
|
93
|
+
if (alpha == 0 || !castsShadow) {
|
|
58
94
|
fillPaint.clearShadowLayer()
|
|
59
95
|
return
|
|
60
96
|
}
|
|
@@ -65,7 +101,11 @@ class BoxDrawable : Drawable() {
|
|
|
65
101
|
|
|
66
102
|
/** `overflow: 'hidden'` clips to this outline, so rounded corners clip their content. */
|
|
67
103
|
override fun getOutline(outline: Outline) {
|
|
68
|
-
outline.setRoundRect(bounds, cornerRadius)
|
|
104
|
+
if (!hasCornerRadii) return outline.setRoundRect(bounds, cornerRadius)
|
|
105
|
+
|
|
106
|
+
val path = outlinePath()
|
|
107
|
+
|
|
108
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) outline.setPath(path) else @Suppress("DEPRECATION") outline.setConvexPath(path)
|
|
69
109
|
}
|
|
70
110
|
|
|
71
111
|
override fun setAlpha(alpha: Int) {}
|
|
@@ -787,8 +787,6 @@ class Bridge(private val activity: ComponentActivity, private val rootView: Root
|
|
|
787
787
|
setIndicatorSize(node, "medium")
|
|
788
788
|
}
|
|
789
789
|
|
|
790
|
-
"Svg" -> NativeNode(SvgView(activity), measuresContent = false)
|
|
791
|
-
|
|
792
790
|
else -> {
|
|
793
791
|
val makeView = pluginRegistry.viewMakers[type]
|
|
794
792
|
val view = makeView?.invoke { eventName, payload -> dispatchEvent(eventName, id, payload) }
|
|
@@ -882,7 +880,7 @@ class Bridge(private val activity: ComponentActivity, private val rootView: Root
|
|
|
882
880
|
private fun setText(text: String, id: Int) {
|
|
883
881
|
val node = nodesById[id] ?: return
|
|
884
882
|
|
|
885
|
-
(node.view as? TextView)?.text = text
|
|
883
|
+
(node.view as? TextView)?.text = node.withLineHeight(text)
|
|
886
884
|
node.markContentChanged()
|
|
887
885
|
}
|
|
888
886
|
|
|
@@ -890,7 +888,7 @@ class Bridge(private val activity: ComponentActivity, private val rootView: Root
|
|
|
890
888
|
val node = nodesById[id] ?: return
|
|
891
889
|
val view = node.view
|
|
892
890
|
|
|
893
|
-
if (key
|
|
891
|
+
if (key !in VIEW_PROPS && view is PluginView) return view.setProp(key, value)
|
|
894
892
|
|
|
895
893
|
when (key) {
|
|
896
894
|
"style" -> {
|
|
@@ -991,16 +989,7 @@ class Bridge(private val activity: ComponentActivity, private val rootView: Root
|
|
|
991
989
|
|
|
992
990
|
"size" -> if (view is ProgressBar) setIndicatorSize(node, value as? String ?: "medium")
|
|
993
991
|
|
|
994
|
-
"color" ->
|
|
995
|
-
(view as? ProgressBar)?.indeterminateTintList = androidColor(value)?.let(ColorStateList::valueOf)
|
|
996
|
-
(view as? SvgView)?.color = androidColor(value) ?: SVG_DEFAULT_COLOR
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
"viewBox" -> SvgView.rect(value)?.let { (view as? SvgView)?.viewBox = it }
|
|
1000
|
-
|
|
1001
|
-
"elements" -> (view as? SvgView)?.elements = value as? List<*> ?: emptyList<Any>()
|
|
1002
|
-
|
|
1003
|
-
"strokeWidth" -> (view as? SvgView)?.strokeWidth = number(value) ?: 2f
|
|
992
|
+
"color" -> (view as? ProgressBar)?.indeterminateTintList = androidColor(value)?.let(ColorStateList::valueOf)
|
|
1004
993
|
|
|
1005
994
|
"placeholder" -> (view as? EditText)?.hint = value as? String
|
|
1006
995
|
|
|
@@ -1178,12 +1167,13 @@ class Bridge(private val activity: ComponentActivity, private val rootView: Root
|
|
|
1178
1167
|
}
|
|
1179
1168
|
|
|
1180
1169
|
private companion object {
|
|
1181
|
-
const val TAG = "
|
|
1170
|
+
const val TAG = "nativue"
|
|
1182
1171
|
const val JAVASCRIPT_STACK_SIZE = 8L * 1024 * 1024
|
|
1183
1172
|
const val SYMBOLICATE_TIMEOUT_MS = 2000
|
|
1184
1173
|
const val RECONNECT_DELAY_MS = 2000L
|
|
1185
|
-
const val SVG_DEFAULT_COLOR = 0xFF000000.toInt()
|
|
1186
1174
|
const val BUTTON_TINT = 0xFF007AFF.toInt()
|
|
1175
|
+
/** What nativue applies to any view, plugin views included: style, accessibility, layout and gestures. */
|
|
1176
|
+
val VIEW_PROPS = setOf("style", "accessibility", "onLayout", "onPan", "onPinch", "onRotate")
|
|
1187
1177
|
val EDGES = listOf(YogaEdge.LEFT, YogaEdge.TOP, YogaEdge.RIGHT, YogaEdge.BOTTOM)
|
|
1188
1178
|
val SYSTEM_BARS = WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()
|
|
1189
1179
|
val IME = WindowInsetsCompat.Type.ime()
|
|
@@ -23,6 +23,10 @@ class NativeNode(val view: View, measuresContent: Boolean) {
|
|
|
23
23
|
|
|
24
24
|
var fontSize = DEFAULT_FONT_SIZE
|
|
25
25
|
var fontWeight = 400
|
|
26
|
+
var isItalic = false
|
|
27
|
+
/** Points, or ems of the font size ('1.5em'); null for the font's own. */
|
|
28
|
+
var letterSpacing: Any? = null
|
|
29
|
+
var lineHeight: Any? = null
|
|
26
30
|
|
|
27
31
|
/** WindowInsets types that pad this node (safe area, keyboard), 0 for none, and the last padding. */
|
|
28
32
|
var insetTypes = 0
|
|
@@ -17,6 +17,7 @@ import android.view.ViewConfiguration
|
|
|
17
17
|
import android.view.ViewGroup
|
|
18
18
|
import android.view.animation.PathInterpolator
|
|
19
19
|
import android.widget.FrameLayout
|
|
20
|
+
import android.widget.ImageView
|
|
20
21
|
import android.widget.LinearLayout
|
|
21
22
|
import android.widget.TextView
|
|
22
23
|
import kotlin.math.abs
|
|
@@ -202,7 +203,10 @@ class ScreenHeader(context: Context) : LinearLayout(context), ThemeAware {
|
|
|
202
203
|
|
|
203
204
|
private val dragHandle = View(context)
|
|
204
205
|
private val row = LinearLayout(context)
|
|
205
|
-
private val navigationButton =
|
|
206
|
+
private val navigationButton = IconButton(context).apply {
|
|
207
|
+
setIcon(R.drawable.nativue_arrow_back)
|
|
208
|
+
color = ThemeColors.onSurface
|
|
209
|
+
}
|
|
206
210
|
private val leftItems = LinearLayout(context)
|
|
207
211
|
private val title = textView(22f, Typeface.NORMAL)
|
|
208
212
|
private val rightItems = LinearLayout(context)
|
|
@@ -276,30 +280,30 @@ class ScreenHeader(context: Context) : LinearLayout(context), ThemeAware {
|
|
|
276
280
|
}
|
|
277
281
|
|
|
278
282
|
/** Back arrow inside a stack, a close icon on a full screen modal, nothing on a group's first screen. */
|
|
279
|
-
fun setNavigation(icon:
|
|
283
|
+
fun setNavigation(icon: Int?) {
|
|
280
284
|
navigationButton.visibility = if (icon == null) View.GONE else View.VISIBLE
|
|
281
|
-
icon?.let
|
|
285
|
+
icon?.let(navigationButton::setIcon)
|
|
282
286
|
(title.layoutParams as LayoutParams).marginStart = dp(if (icon == null) 12f else 4f)
|
|
283
287
|
}
|
|
284
288
|
|
|
285
289
|
/**
|
|
286
|
-
* [{ title?,
|
|
287
|
-
*
|
|
288
|
-
*
|
|
290
|
+
* [{ title?, image?, drawnIcon?, systemImage? }]: an icon shows as an icon button, otherwise the title as a
|
|
291
|
+
* text button; SF Symbol names are iOS-only. Presses are reported with the side and index, since the
|
|
292
|
+
* handlers live in JS.
|
|
289
293
|
*/
|
|
290
294
|
fun setItems(value: Any?, side: String) {
|
|
291
295
|
val items = if (side == "right") rightItems else leftItems
|
|
292
296
|
items.removeAllViews()
|
|
293
297
|
|
|
294
298
|
(value as? List<*>)?.filterIsInstance<Map<*, *>>()?.forEachIndexed { index, item ->
|
|
295
|
-
val
|
|
296
|
-
val button = if (
|
|
297
|
-
val width = if (
|
|
299
|
+
val hasIcon = item["image"] != null || item["drawnIcon"] != null
|
|
300
|
+
val button = if (hasIcon) IconButton(context).apply { setIcon(item) } else textButton(item["title"] as? String ?: "")
|
|
301
|
+
val width = if (hasIcon) dp(48f) else LayoutParams.WRAP_CONTENT
|
|
298
302
|
|
|
299
303
|
tint(button)
|
|
300
304
|
button.contentDescription = item["title"] as? String
|
|
301
305
|
button.setOnClickListener { onEvent("headeritempress", mapOf("side" to side, "index" to index)) }
|
|
302
|
-
items.addView(button, LayoutParams(width, if (
|
|
306
|
+
items.addView(button, LayoutParams(width, if (hasIcon) dp(48f) else dp(40f)))
|
|
303
307
|
}
|
|
304
308
|
}
|
|
305
309
|
|
|
@@ -324,31 +328,21 @@ class ScreenHeader(context: Context) : LinearLayout(context), ThemeAware {
|
|
|
324
328
|
includeFontPadding = false
|
|
325
329
|
}
|
|
326
330
|
|
|
327
|
-
private fun iconButton(elements: List<List<Any>>) = IconButton(context).apply {
|
|
328
|
-
this.elements = elements
|
|
329
|
-
color = ThemeColors.onSurface
|
|
330
|
-
}
|
|
331
331
|
|
|
332
332
|
companion object {
|
|
333
|
-
val ARROW_BACK =
|
|
334
|
-
val CLOSE =
|
|
333
|
+
val ARROW_BACK = R.drawable.nativue_arrow_back
|
|
334
|
+
val CLOSE = R.drawable.nativue_close
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
337
|
|
|
338
338
|
/** A 24dp icon in a 48dp round touch target with a ripple. */
|
|
339
339
|
class IconButton(context: Context) : FrameLayout(context) {
|
|
340
|
-
private val icon =
|
|
340
|
+
private val icon = ImageView(context)
|
|
341
341
|
|
|
342
|
-
var
|
|
343
|
-
get() = icon.elements
|
|
342
|
+
var color: Int = 0
|
|
344
343
|
set(value) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
var color: Int
|
|
349
|
-
get() = icon.color
|
|
350
|
-
set(value) {
|
|
351
|
-
icon.color = value
|
|
344
|
+
field = value
|
|
345
|
+
icon.imageTintList = ColorStateList.valueOf(value)
|
|
352
346
|
}
|
|
353
347
|
|
|
354
348
|
init {
|
|
@@ -356,10 +350,13 @@ class IconButton(context: Context) : FrameLayout(context) {
|
|
|
356
350
|
addView(icon, LayoutParams(dp(24f), dp(24f), Gravity.CENTER))
|
|
357
351
|
}
|
|
358
352
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
353
|
+
fun setIcon(drawable: Int) {
|
|
354
|
+
icon.setImageResource(drawable)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** A header item's icon: { image } or { drawnIcon }. */
|
|
358
|
+
fun setIcon(item: Map<*, *>) {
|
|
359
|
+
NavigationIcons.show(item, icon, dp(24f))
|
|
363
360
|
}
|
|
364
361
|
}
|
|
365
362
|
|
|
@@ -639,7 +636,7 @@ class FixedSizeTextView(context: Context) : TextView(context)
|
|
|
639
636
|
/** One destination of the navigation bar: an icon in a pill when selected, a label and a badge. */
|
|
640
637
|
private class TabItemView(context: Context) : LinearLayout(context) {
|
|
641
638
|
private val indicator = FrameLayout(context)
|
|
642
|
-
private val icon =
|
|
639
|
+
private val icon = ImageView(context)
|
|
643
640
|
private val label = FixedSizeTextView(context)
|
|
644
641
|
private val badge = FixedSizeTextView(context)
|
|
645
642
|
|
|
@@ -676,15 +673,12 @@ private class TabItemView(context: Context) : LinearLayout(context) {
|
|
|
676
673
|
background = RippleDrawable(ColorStateList.valueOf(ThemeColors.ripple), null, null)
|
|
677
674
|
}
|
|
678
675
|
|
|
679
|
-
/**
|
|
676
|
+
/** SF Symbol names are iOS-only: an image or a drawn icon shows here. */
|
|
680
677
|
fun update(item: Map<*, *>, isSelected: Boolean, activeTintColor: Int?, inactiveTintColor: Int?) {
|
|
681
678
|
val activeColor = activeTintColor ?: ThemeColors.onSurface
|
|
682
679
|
val color = if (isSelected) activeColor else inactiveTintColor ?: ThemeColors.onSurfaceVariant
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
icon.elements = svg?.get("elements") as? List<*> ?: emptyList<Any>()
|
|
686
|
-
SvgView.rect(svg?.get("viewBox"))?.let { icon.viewBox = it }
|
|
687
|
-
icon.color = color
|
|
680
|
+
NavigationIcons.show(item, icon, dp(24f))
|
|
681
|
+
icon.imageTintList = ColorStateList.valueOf(color)
|
|
688
682
|
label.text = item["label"] as? String ?: ""
|
|
689
683
|
label.setTextColor(color)
|
|
690
684
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package dev.nativue
|
|
2
|
+
|
|
3
|
+
import android.widget.ImageView
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The icons of tab bars and headers: an image (`image`, a url) or one a package draws (`drawnIcon`, registered
|
|
7
|
+
* with PluginRegistry.addIconDrawer). SF Symbol names are iOS-only. Tinted by what shows them.
|
|
8
|
+
*/
|
|
9
|
+
object NavigationIcons {
|
|
10
|
+
internal val drawersByName = mutableMapOf<String, DrawIcon>()
|
|
11
|
+
|
|
12
|
+
/** The item's icon in [view], [size] pixels square; an image from a url once it has loaded. */
|
|
13
|
+
fun show(item: Map<*, *>, view: ImageView, size: Int) {
|
|
14
|
+
val source = item["image"] as? String
|
|
15
|
+
val icon = item["drawnIcon"] as? Map<*, *>
|
|
16
|
+
val draw = (icon?.get("drawer") as? String)?.let(drawersByName::get)
|
|
17
|
+
|
|
18
|
+
view.tag = source
|
|
19
|
+
if (source == null) return view.setImageBitmap(if (icon != null && draw != null) draw(icon, size) else null)
|
|
20
|
+
|
|
21
|
+
view.setImageDrawable(null)
|
|
22
|
+
RemoteImageView.loadBitmap(view.context, source) { bitmap -> if (view.tag == source) view.setImageBitmap(bitmap) }
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package dev.nativue
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
+
import android.graphics.Bitmap
|
|
4
5
|
import android.view.View
|
|
5
6
|
import androidx.activity.ComponentActivity
|
|
6
7
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
@@ -47,6 +48,9 @@ class PluginException(val code: String, message: String) : Exception(message)
|
|
|
47
48
|
/** Called on the JavaScript thread; settles `promise` from any thread, when the work is done. */
|
|
48
49
|
typealias AsyncFunction = (arguments: List<Any?>, promise: Promise) -> Unit
|
|
49
50
|
|
|
51
|
+
/** An icon JS describes ({ drawer, ... }) drawn [size] pixels square, for a tab bar or a header. */
|
|
52
|
+
typealias DrawIcon = (icon: Map<*, *>, size: Int) -> Bitmap?
|
|
53
|
+
|
|
50
54
|
interface ActivityListener {
|
|
51
55
|
/** Register activity-result launchers here, every time and in the same order, so results survive a recreation. */
|
|
52
56
|
fun onActivityCreated(activity: ComponentActivity) {}
|
|
@@ -142,6 +146,14 @@ class PluginRegistry private constructor(context: Context) {
|
|
|
142
146
|
fun addView(type: String, makeView: (SendEvent) -> View) {
|
|
143
147
|
viewMakers[type] = makeView
|
|
144
148
|
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Draws the tab bar and header icons whose `drawnIcon.drawer` is [drawer] (see router.ts's DrawnIcon).
|
|
152
|
+
* The bitmap is tinted: only its shape counts.
|
|
153
|
+
*/
|
|
154
|
+
fun addIconDrawer(drawer: String, draw: DrawIcon) {
|
|
155
|
+
NavigationIcons.drawersByName[drawer] = draw
|
|
156
|
+
}
|
|
145
157
|
}
|
|
146
158
|
|
|
147
159
|
/** The string resource nativue.settings.gradle.kts writes: the plugin classes, comma-separated. */
|
|
@@ -152,7 +164,7 @@ private fun pluginClassNames(context: Context): List<String> {
|
|
|
152
164
|
return context.getString(resourceId).split(',').filter { it.isNotEmpty() }
|
|
153
165
|
}
|
|
154
166
|
|
|
155
|
-
/** A plugin view gets its props here, all but `style`, which nativue applies
|
|
167
|
+
/** A plugin view gets its props here, all but `style`, `accessibility`, `@layout` and the gestures (`@pan`...), which nativue applies. */
|
|
156
168
|
interface PluginView {
|
|
157
169
|
fun setProp(key: String, value: Any?)
|
|
158
170
|
}
|