@nativue/core 0.15.4 → 0.16.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 +13 -2
- package/android/nativue.settings.gradle.kts +29 -9
- package/android/src/main/java/dev/nativue/Bridge.kt +4 -14
- 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/Views.kt +53 -31
- 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/link.mjs +25 -6
- package/cli/nativue.mjs +0 -1
- package/ios/Bridge.swift +10 -16
- package/ios/ImageView.swift +32 -10
- 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 +1 -1
- package/package.json +1 -1
- package/src/native-components.ts +0 -14
- package/src/renderer.ts +173 -8
- package/src/router.ts +20 -7
- package/android/src/main/java/dev/nativue/SvgView.kt +0 -443
- package/ios/SvgView.swift +0 -464
package/ios/ImageView.swift
CHANGED
|
@@ -32,7 +32,7 @@ final class ImageView: UIImageView {
|
|
|
32
32
|
/// The image changed, and with it the size the view measures.
|
|
33
33
|
var onImageChange: (() -> Void)?
|
|
34
34
|
|
|
35
|
-
/// "disk", "memory" or "none". Read
|
|
35
|
+
/// "disk", "memory" or "none". Read once loading starts, so it applies even when set after `source`.
|
|
36
36
|
var cachePolicy = "disk"
|
|
37
37
|
|
|
38
38
|
private var loadingTask: Task<Void, Never>?
|
|
@@ -57,28 +57,45 @@ final class ImageView: UIImageView {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
show(nil)
|
|
60
|
-
let readsDisk = cachePolicy == "disk"
|
|
61
60
|
loadingTask = Task { [weak self] in
|
|
62
|
-
let cachedData = readsDisk ? await Task.detached { ImageDiskCache.data(for: source) }.value : nil
|
|
63
|
-
let downloadedData = cachedData == nil ? await Self.download(source, from: url) : nil
|
|
64
61
|
guard
|
|
65
|
-
let
|
|
66
|
-
let image = await
|
|
62
|
+
let cachePolicy = self?.cachePolicy,
|
|
63
|
+
let image = await Self.image(for: source, from: url, cachePolicy: cachePolicy),
|
|
67
64
|
!Task.isCancelled,
|
|
68
65
|
let self
|
|
69
66
|
else { return }
|
|
70
67
|
|
|
71
|
-
store(image, downloadedData: downloadedData, for: source)
|
|
72
68
|
show(image)
|
|
73
69
|
}
|
|
74
70
|
}
|
|
75
71
|
|
|
76
|
-
|
|
72
|
+
/// An image as <Image> loads it: from memory, the disk cache or its url, then kept as `cachePolicy` says.
|
|
73
|
+
static func image(for source: String, cachePolicy: String = "disk") async -> UIImage? {
|
|
74
|
+
if cachePolicy != "none", let cachedImage = decodedImages.object(forKey: source as NSString) { return cachedImage }
|
|
75
|
+
guard let url = URL(string: source) else { return nil }
|
|
76
|
+
|
|
77
|
+
return await image(for: source, from: url, cachePolicy: cachePolicy)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private static func image(for source: String, from url: URL, cachePolicy: String) async -> UIImage? {
|
|
81
|
+
let cachedData = cachePolicy == "disk" ? await Task.detached { ImageDiskCache.data(for: source) }.value : nil
|
|
82
|
+
let downloadedData = cachedData == nil ? await download(source, from: url) : nil
|
|
83
|
+
guard
|
|
84
|
+
let data = cachedData ?? downloadedData,
|
|
85
|
+
let image = await UIImage(data: data)?.byPreparingForDisplay() // decoded off the main thread
|
|
86
|
+
else { return nil }
|
|
87
|
+
|
|
88
|
+
store(image, downloadedData: downloadedData, for: source, cachePolicy: cachePolicy)
|
|
89
|
+
|
|
90
|
+
return image
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private static func store(_ image: UIImage, downloadedData: Data?, for source: String, cachePolicy: String) {
|
|
77
94
|
guard cachePolicy != "none" else { return }
|
|
78
95
|
|
|
79
|
-
|
|
96
|
+
decodedImages.setObject(image, forKey: source as NSString, cost: byteCount(of: image))
|
|
80
97
|
|
|
81
|
-
guard cachePolicy == "disk", let downloadedData, !
|
|
98
|
+
guard cachePolicy == "disk", let downloadedData, !isInMemory(source) else { return }
|
|
82
99
|
Task.detached(priority: .utility) { ImageDiskCache.store(downloadedData, for: source) }
|
|
83
100
|
}
|
|
84
101
|
|
|
@@ -130,6 +147,11 @@ final class ImageView: UIImageView {
|
|
|
130
147
|
}
|
|
131
148
|
}
|
|
132
149
|
|
|
150
|
+
/// An image loaded and cached as <Image> does it: for a package's views.
|
|
151
|
+
public func loadImage(_ source: String) async -> UIImage? {
|
|
152
|
+
await ImageView.image(for: source)
|
|
153
|
+
}
|
|
154
|
+
|
|
133
155
|
/// Downloaded images by url, in the system's caches folder, which iOS may empty when storage runs low. Past
|
|
134
156
|
/// its limit, the images used least recently go first.
|
|
135
157
|
private enum ImageDiskCache {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
|
|
3
|
+
/// The icons of tab bars and headers: an SF Symbol (`systemImage`), an image (`image`, a url), or one a
|
|
4
|
+
/// package draws (`drawnIcon`, registered with PluginRegistry.addIconDrawer). Tinted by what shows them.
|
|
5
|
+
enum NavigationIcons {
|
|
6
|
+
static var drawersByName: [String: PluginRegistry.DrawIcon] = [:]
|
|
7
|
+
/// The url each item (a tab, a header button) shows last: a slower load of an earlier one is dropped.
|
|
8
|
+
private static let sourcesByOwner = NSMapTable<AnyObject, NSString>.weakToStrongObjects()
|
|
9
|
+
|
|
10
|
+
/// The icon now, or nil with `update` called (on the main thread) once an image loads, if `owner` still
|
|
11
|
+
/// shows that image then.
|
|
12
|
+
static func image(
|
|
13
|
+
_ item: [String: Any], size: CGFloat, for owner: AnyObject, update: @escaping (UIImage) -> Void
|
|
14
|
+
) -> UIImage? {
|
|
15
|
+
sourcesByOwner.setObject((item["image"] as? String) as NSString?, forKey: owner)
|
|
16
|
+
if let name = item["systemImage"] as? String { return UIImage(systemName: name) }
|
|
17
|
+
if let source = item["image"] as? String {
|
|
18
|
+
Task { @MainActor [weak owner] in
|
|
19
|
+
guard
|
|
20
|
+
let image = await loadImage(source),
|
|
21
|
+
let owner,
|
|
22
|
+
sourcesByOwner.object(forKey: owner) as String? == source
|
|
23
|
+
else { return }
|
|
24
|
+
|
|
25
|
+
update(fitted(image, size: size))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return nil
|
|
29
|
+
}
|
|
30
|
+
guard
|
|
31
|
+
let icon = item["drawnIcon"] as? [String: Any],
|
|
32
|
+
let drawer = icon["drawer"] as? String,
|
|
33
|
+
let draw = drawersByName[drawer]
|
|
34
|
+
else { return nil }
|
|
35
|
+
|
|
36
|
+
return draw(icon, size)?.withRenderingMode(.alwaysTemplate)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// Scaled to fit a square of `size` points, as the bar expects.
|
|
40
|
+
private static func fitted(_ image: UIImage, size: CGFloat) -> UIImage {
|
|
41
|
+
let scale = min(size / max(image.size.width, 1), size / max(image.size.height, 1))
|
|
42
|
+
let fittedSize = CGSize(width: image.size.width * scale, height: image.size.height * scale)
|
|
43
|
+
let fittedImage = UIGraphicsImageRenderer(size: fittedSize).image { _ in
|
|
44
|
+
image.draw(in: CGRect(origin: .zero, size: fittedSize))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return fittedImage.withRenderingMode(.alwaysTemplate)
|
|
48
|
+
}
|
|
49
|
+
}
|
package/ios/Plugins.swift
CHANGED
|
@@ -14,6 +14,8 @@ public final class PluginRegistry {
|
|
|
14
14
|
/// sendEvent("change", payload) reaches the element's @change; event names are lowercase.
|
|
15
15
|
public typealias SendEvent = (_ eventName: String, _ payload: Any?) -> Void
|
|
16
16
|
public typealias MakeView = (_ sendEvent: @escaping SendEvent) -> UIView
|
|
17
|
+
/// An icon JS describes ({ drawer, ... }) drawn at `size` points, for a tab bar or a header.
|
|
18
|
+
public typealias DrawIcon = (_ icon: [String: Any], _ size: CGFloat) -> UIImage?
|
|
17
19
|
|
|
18
20
|
/// Set by the Bridge: calls JavaScript's listeners.
|
|
19
21
|
var sendToJavaScript: (_ eventName: String, _ payload: Any?) -> Void = { _, _ in }
|
|
@@ -47,8 +49,18 @@ public final class PluginRegistry {
|
|
|
47
49
|
public func addView(_ type: String, _ makeView: @escaping MakeView) {
|
|
48
50
|
viewMakers[type] = makeView
|
|
49
51
|
}
|
|
52
|
+
|
|
53
|
+
/// Draws the tab bar and header icons whose `drawnIcon.drawer` is `drawer` (see router.ts's DrawnIcon).
|
|
54
|
+
/// The image is tinted, like an SF Symbol: only its shape counts.
|
|
55
|
+
public func addIconDrawer(_ drawer: String, _ draw: @escaping DrawIcon) {
|
|
56
|
+
NavigationIcons.drawersByName[drawer] = draw
|
|
57
|
+
}
|
|
50
58
|
}
|
|
51
59
|
|
|
60
|
+
/// A plugin view that takes its own touches where `point(inside:with:)` says, which a <Pressable> around it
|
|
61
|
+
/// leaves to it.
|
|
62
|
+
public protocol InteractiveView: UIView {}
|
|
63
|
+
|
|
52
64
|
/// Thrown by an async function: JavaScript's Promise rejects with an Error whose `code` is this one.
|
|
53
65
|
/// Codes nativue's packages share: ERR_PERMISSION_DENIED, ERR_UNAVAILABLE, ERR_CANCELED,
|
|
54
66
|
/// ERR_INVALID_ARGUMENT.
|
|
@@ -64,7 +76,7 @@ public struct PluginError: LocalizedError {
|
|
|
64
76
|
public var errorDescription: String? { message }
|
|
65
77
|
}
|
|
66
78
|
|
|
67
|
-
/// A plugin view gets its props here, all but `style`, which nativue applies
|
|
79
|
+
/// A plugin view gets its props here, all but `style`, `accessibility`, `@layout` and the gestures (`@pan`...), which nativue applies.
|
|
68
80
|
public protocol PluginView: UIView {
|
|
69
81
|
func setProp(_ key: String, value: Any?)
|
|
70
82
|
}
|
package/ios/PressableView.swift
CHANGED
|
@@ -51,7 +51,9 @@ final class PressableView: UIControl {
|
|
|
51
51
|
private func isInteractiveChild(_ view: UIView) -> Bool {
|
|
52
52
|
sequence(first: view, next: \.superview)
|
|
53
53
|
.prefix { $0 !== self }
|
|
54
|
-
.contains {
|
|
54
|
+
.contains {
|
|
55
|
+
$0 is UIControl || $0 is UIScrollView || $0 is InteractiveView || !($0.gestureRecognizers ?? []).isEmpty
|
|
56
|
+
}
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
/// VoiceOver's double tap: a press, even when something interactive sits under the activation point.
|
|
@@ -111,12 +111,15 @@ final class ScreenController: UIViewController, ContentRoot {
|
|
|
111
111
|
|
|
112
112
|
private func makeBarButtonItem(_ description: [String: Any], side: String, index: Int) -> UIBarButtonItem {
|
|
113
113
|
let title = description["title"] as? String ?? ""
|
|
114
|
-
let
|
|
114
|
+
let barButtonItem = UIBarButtonItem()
|
|
115
|
+
let image = NavigationIcons.image(description, size: 22, for: barButtonItem) { [weak barButtonItem] image in
|
|
116
|
+
barButtonItem?.image = image
|
|
117
|
+
}
|
|
115
118
|
|
|
116
|
-
|
|
119
|
+
barButtonItem.primaryAction = UIAction(title: title, image: image) { [unowned self] _ in
|
|
117
120
|
onEvent?("headeritempress", ["side": side, "index": index])
|
|
118
121
|
}
|
|
119
122
|
|
|
120
|
-
return
|
|
123
|
+
return barButtonItem
|
|
121
124
|
}
|
|
122
125
|
}
|
package/ios/Style.swift
CHANGED
|
@@ -357,7 +357,7 @@ private let fontWeights: [String: UIFont.Weight] = [
|
|
|
357
357
|
|
|
358
358
|
extension UIColor {
|
|
359
359
|
/// Colors arrive as 0xRRGGBBAA numbers: the JS side (src/colors.js) parses every CSS format.
|
|
360
|
-
convenience init?(rgba value: Any) {
|
|
360
|
+
public convenience init?(rgba value: Any) {
|
|
361
361
|
guard let rgba = (value as? NSNumber)?.uint32Value else { return nil }
|
|
362
362
|
|
|
363
363
|
self.init(
|
package/package.json
CHANGED
package/src/native-components.ts
CHANGED
|
@@ -24,13 +24,6 @@ export interface AccessibilityProps {
|
|
|
24
24
|
'aria-checked'?: BooleanAttribute
|
|
25
25
|
}
|
|
26
26
|
type EventHandler<Payload> = ((payload: Payload) => void) | AnimatedEvent<Payload>
|
|
27
|
-
// [tag, attributes], e.g. ['circle', { cx: 12, cy: 12, r: 10 }]: path, circle, ellipse, rect, line,
|
|
28
|
-
// polyline and polygon. Stroked with `color`; filled too when `fill` isn't 'none'.
|
|
29
|
-
export type SvgElement = [tag: string, attributes: Record<string, string | number>]
|
|
30
|
-
// An icon component that carries its drawing, so it can also be a tab bar icon (see @nativue/icons).
|
|
31
|
-
export interface SvgIcon {
|
|
32
|
-
svg: { viewBox: [number, number, number, number]; elements: SvgElement[] }
|
|
33
|
-
}
|
|
34
27
|
type ModelProps<Value> = { modelValue?: Value; 'onUpdate:modelValue'?: (value: Value) => void }
|
|
35
28
|
|
|
36
29
|
declare module '@vue/runtime-core' {
|
|
@@ -91,13 +84,6 @@ declare module '@vue/runtime-core' {
|
|
|
91
84
|
color?: string
|
|
92
85
|
size?: 'small' | 'large'
|
|
93
86
|
}>
|
|
94
|
-
// Low level: draws SVG elements, e.g. an icon set's nodes (see @nativue/icons).
|
|
95
|
-
Svg: NativeComponent<{
|
|
96
|
-
viewBox?: [number, number, number, number]
|
|
97
|
-
elements: SvgElement[]
|
|
98
|
-
color?: string
|
|
99
|
-
strokeWidth?: number
|
|
100
|
-
}>
|
|
101
87
|
Modal: NativeComponent<{
|
|
102
88
|
visible?: boolean
|
|
103
89
|
animationType?: 'none' | 'slide' | 'fade'
|
package/src/renderer.ts
CHANGED
|
@@ -25,6 +25,8 @@ import { FlatListComponent, ImageBackground, Pressable, Swipeable } from './comp
|
|
|
25
25
|
import type { LayoutAnimationConfig, Style } from './types'
|
|
26
26
|
export * from '@vue/runtime-core'
|
|
27
27
|
export { AccessibilityInfo } from './accessibility'
|
|
28
|
+
export type { AccessibilityProps } from './native-components'
|
|
29
|
+
export { processColor } from './colors'
|
|
28
30
|
export { Animated } from './animated'
|
|
29
31
|
export {
|
|
30
32
|
Appearance,
|
|
@@ -43,7 +45,6 @@ export { openSettings, usePermission } from './permissions'
|
|
|
43
45
|
export type { PermissionResponse, PermissionResponseWith } from './permissions'
|
|
44
46
|
export { AsyncStorage } from './storage'
|
|
45
47
|
export type * from './types'
|
|
46
|
-
export type { SvgElement, SvgIcon } from './native-components'
|
|
47
48
|
|
|
48
49
|
interface Node {
|
|
49
50
|
id: number
|
|
@@ -64,6 +65,44 @@ interface Node {
|
|
|
64
65
|
/** <TextInput v-model.trim>: its modifiers, and the text typed last. */
|
|
65
66
|
modelModifiers?: ModelModifiers
|
|
66
67
|
typedText?: string
|
|
68
|
+
/** A drawn view's style from its own props (<svg width height>): under its class and style. */
|
|
69
|
+
baseStyle?: Style
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** An element inside a drawn view, or the view itself, as its package sees it. */
|
|
73
|
+
export interface DrawnElement {
|
|
74
|
+
readonly id: number
|
|
75
|
+
readonly type: string
|
|
76
|
+
readonly parent: DrawnElement | null
|
|
77
|
+
readonly children: DrawnElement[]
|
|
78
|
+
readonly text: string
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A native view that draws what's inside it, like @nativue/svg's <svg>: its elements stay in JS and
|
|
83
|
+
* never become views. After each batch of changes, the view gets them as one prop, `drawing`.
|
|
84
|
+
*/
|
|
85
|
+
export interface DrawnView {
|
|
86
|
+
/** The native view's type, e.g. 'Svg', which a package's plugin registers natively. */
|
|
87
|
+
type: string
|
|
88
|
+
/** The tags that create it in a template, e.g. ['svg']. */
|
|
89
|
+
tags: string[]
|
|
90
|
+
/** The tags of the elements drawn inside it, e.g. 'path'. */
|
|
91
|
+
partTags: string[]
|
|
92
|
+
/** A part's prop (an @event's handler excepted, which nativue keeps). */
|
|
93
|
+
setPartProp(element: DrawnElement, key: string, value: unknown): void
|
|
94
|
+
/**
|
|
95
|
+
* The view's own prop: true when only the drawing reads it, false when it also goes to the native view.
|
|
96
|
+
* `setBaseStyle` styles the view under its class and style.
|
|
97
|
+
*/
|
|
98
|
+
setViewProp(
|
|
99
|
+
element: DrawnElement,
|
|
100
|
+
key: string,
|
|
101
|
+
value: unknown,
|
|
102
|
+
setBaseStyle: (style: Style) => void,
|
|
103
|
+
): boolean
|
|
104
|
+
/** The view's `drawing`, once per batch of changes; undefined leaves it as it is. */
|
|
105
|
+
drawing(view: DrawnElement, isPressable: (element: DrawnElement) => boolean): unknown
|
|
67
106
|
}
|
|
68
107
|
|
|
69
108
|
type ModelModifiers = { trim?: boolean; number?: boolean; lazy?: boolean }
|
|
@@ -90,6 +129,19 @@ export function setClassStyleResolver(resolver: ClassStyleResolver): void {
|
|
|
90
129
|
nodesWithClass.forEach((node) => updateStyle(node, {}))
|
|
91
130
|
}
|
|
92
131
|
|
|
132
|
+
const warnedTags = new Set<string>()
|
|
133
|
+
|
|
134
|
+
// Native components are capitalized: a lowercase tag is HTML (<div>), or a drawn view's (<svg>) whose
|
|
135
|
+
// package isn't installed. Either way it would show nothing.
|
|
136
|
+
function warnIfUnknown(tag: string) {
|
|
137
|
+
if (!/^[a-z]/.test(tag) || drawnViewsByTag.has(tag) || warnedTags.has(tag)) return
|
|
138
|
+
|
|
139
|
+
warnedTags.add(tag)
|
|
140
|
+
console.warn(
|
|
141
|
+
`<${tag}> isn't a nativue element, so it shows nothing: use nativue's components (<View>, <Text>...), or install the package that provides <${tag}>.`,
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
93
145
|
function createNode(type: string): Node {
|
|
94
146
|
return { id: nextNodeId++, type, parent: null, children: [], text: '' }
|
|
95
147
|
}
|
|
@@ -101,9 +153,76 @@ function nativeIdsIn(node: Node): number[] {
|
|
|
101
153
|
return [...ownId, ...node.children.flatMap(nativeIdsIn)]
|
|
102
154
|
}
|
|
103
155
|
|
|
104
|
-
// '#text' and '#comment' nodes only exist in JS (Vue anchors / raw text)
|
|
156
|
+
// '#text' and '#comment' nodes only exist in JS (Vue anchors / raw text), and so do a drawn view's
|
|
157
|
+
// elements, which the view draws: native never sees them.
|
|
105
158
|
function isNativeNode(node: Node): boolean {
|
|
106
|
-
return !node.type.startsWith('#')
|
|
159
|
+
return !node.type.startsWith('#') && !drawnViewsByPartTag.has(node.type)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const drawnViewsByTag = new Map<string, DrawnView>()
|
|
163
|
+
const drawnViewsByPartTag = new Map<string, DrawnView>()
|
|
164
|
+
const drawnViewsByType = new Map<string, DrawnView>()
|
|
165
|
+
const drawnViewsToRedraw = new Set<Node>()
|
|
166
|
+
|
|
167
|
+
/** For a package's runtime module, before the app starts: see DrawnView. */
|
|
168
|
+
export function registerDrawnView(view: DrawnView): void {
|
|
169
|
+
for (const tag of [...view.tags, view.type]) drawnViewsByTag.set(tag, view)
|
|
170
|
+
for (const tag of view.partTags) drawnViewsByPartTag.set(tag, view)
|
|
171
|
+
drawnViewsByType.set(view.type, view)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function drawnViewOf(node: Node | null): Node | null {
|
|
175
|
+
let current = node
|
|
176
|
+
|
|
177
|
+
while (current && !drawnViewsByType.has(current.type)) current = current.parent
|
|
178
|
+
|
|
179
|
+
return current
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Every change inside a drawn view in one batch: the drawing is sent once, after them. */
|
|
183
|
+
function redraw(node: Node | null) {
|
|
184
|
+
const view = drawnViewOf(node)
|
|
185
|
+
if (!view) return
|
|
186
|
+
|
|
187
|
+
if (drawnViewsToRedraw.size === 0) Promise.resolve().then(sendDrawings)
|
|
188
|
+
drawnViewsToRedraw.add(view)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function sendDrawings() {
|
|
192
|
+
const isPressable = (element: DrawnElement) => Boolean(eventHandlersByNodeId[element.id]?.press)
|
|
193
|
+
// Copied first: a drawing that fails doesn't keep the others from being sent next time.
|
|
194
|
+
const views = [...drawnViewsToRedraw]
|
|
195
|
+
|
|
196
|
+
drawnViewsToRedraw.clear()
|
|
197
|
+
for (const view of views) {
|
|
198
|
+
const drawing = drawnViewsByType.get(view.type)?.drawing(view, isPressable)
|
|
199
|
+
|
|
200
|
+
if (drawing !== undefined) native.setProp(view.id, 'drawing', drawing)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A drawn view's own props: false for those that go on as for any view. */
|
|
205
|
+
function patchDrawnViewProp(element: Node, key: string, value: unknown): boolean {
|
|
206
|
+
const name = camelize(key)
|
|
207
|
+
|
|
208
|
+
if (isEventProp(name)) redraw(element)
|
|
209
|
+
if (key === 'style' || key === 'class' || isEventProp(name) || isAccessibilityAttribute(key)) {
|
|
210
|
+
return false
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const setBaseStyle = (style: Style) => {
|
|
214
|
+
const previousStyle = combinedStyle(element)
|
|
215
|
+
|
|
216
|
+
element.baseStyle = style
|
|
217
|
+
patchStyle(element, previousStyle, combinedStyle(element))
|
|
218
|
+
}
|
|
219
|
+
const isDrawingOnly = drawnViewsByType
|
|
220
|
+
.get(element.type)!
|
|
221
|
+
.setViewProp(element, key, value, setBaseStyle)
|
|
222
|
+
|
|
223
|
+
redraw(element)
|
|
224
|
+
|
|
225
|
+
return isDrawingOnly
|
|
107
226
|
}
|
|
108
227
|
|
|
109
228
|
// Vue removes only the root of an unmounted subtree: its descendants go with it.
|
|
@@ -198,9 +317,9 @@ function withoutAnimatedProperties(
|
|
|
198
317
|
// The class's styles come first, so the style prop overrides them, as inline styles do on the web.
|
|
199
318
|
function combinedStyle(element: Node): Style | null {
|
|
200
319
|
const focusedStyle = element.focused ? element.focusedStyle : null
|
|
201
|
-
if (!element.classStyle && !focusedStyle) return element.style ?? null
|
|
320
|
+
if (!element.classStyle && !focusedStyle && !element.baseStyle) return element.style ?? null
|
|
202
321
|
|
|
203
|
-
return { ...element.classStyle, ...element.style, ...focusedStyle }
|
|
322
|
+
return { ...element.baseStyle, ...element.classStyle, ...element.style, ...focusedStyle }
|
|
204
323
|
}
|
|
205
324
|
|
|
206
325
|
function classStyleOf(element: Node): Style | undefined {
|
|
@@ -264,6 +383,23 @@ function reportsToJavaScript(eventName: string, handler: unknown): boolean {
|
|
|
264
383
|
return eventName !== 'scroll' || handler.listener != null
|
|
265
384
|
}
|
|
266
385
|
|
|
386
|
+
/** Inside a drawn view there's no native view to tell: the drawing says which elements take presses. */
|
|
387
|
+
function patchDrawnEventHandler(
|
|
388
|
+
element: Node,
|
|
389
|
+
key: string,
|
|
390
|
+
handler: unknown,
|
|
391
|
+
component: ComponentInternalInstance | null,
|
|
392
|
+
) {
|
|
393
|
+
const listener = handler as Function | undefined
|
|
394
|
+
|
|
395
|
+
eventHandlersByNodeId[element.id] ??= {}
|
|
396
|
+
eventHandlersByNodeId[element.id][key.slice(2).toLowerCase()] =
|
|
397
|
+
listener &&
|
|
398
|
+
((payload) =>
|
|
399
|
+
callWithAsyncErrorHandling(listener, component, ErrorCodes.NATIVE_EVENT_HANDLER, [payload]))
|
|
400
|
+
redraw(element)
|
|
401
|
+
}
|
|
402
|
+
|
|
267
403
|
// An Animated.event handler writes into values natively; JS keeps only its listener. Handlers run
|
|
268
404
|
// through Vue's error handling, like on the web, so an error names the component it came from.
|
|
269
405
|
function patchEventHandler(
|
|
@@ -291,8 +427,12 @@ function patchEventHandler(
|
|
|
291
427
|
|
|
292
428
|
const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
293
429
|
createElement(type) {
|
|
294
|
-
|
|
295
|
-
|
|
430
|
+
if (drawnViewsByPartTag.has(type)) return createNode(type)
|
|
431
|
+
|
|
432
|
+
const element = createNode(drawnViewsByTag.get(type)?.type ?? type)
|
|
433
|
+
|
|
434
|
+
warnIfUnknown(type)
|
|
435
|
+
native.create(element.id, element.type)
|
|
296
436
|
if (type === 'TextInput') textInputsById.set(element.id, element)
|
|
297
437
|
|
|
298
438
|
return element
|
|
@@ -312,11 +452,16 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
312
452
|
setText(textNode, text) {
|
|
313
453
|
textNode.text = text
|
|
314
454
|
syncTextContent(textNode.parent)
|
|
455
|
+
redraw(textNode.parent)
|
|
315
456
|
},
|
|
316
457
|
|
|
317
458
|
setElementText(element, text) {
|
|
318
459
|
element.children = []
|
|
319
460
|
if (element.type === 'Text') native.setText(element.id, text.trim())
|
|
461
|
+
if (drawnViewsByPartTag.has(element.type)) {
|
|
462
|
+
element.children = [{ ...createNode('#text'), text, parent: element }]
|
|
463
|
+
redraw(element)
|
|
464
|
+
}
|
|
320
465
|
},
|
|
321
466
|
|
|
322
467
|
insert(child, parent, anchor) {
|
|
@@ -328,6 +473,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
328
473
|
|
|
329
474
|
if (isNativeNode(child)) native.insert(parent.id, child.id, nativeIndexBefore(parent, index))
|
|
330
475
|
else syncTextContent(parent)
|
|
476
|
+
redraw(parent)
|
|
331
477
|
},
|
|
332
478
|
|
|
333
479
|
remove(child) {
|
|
@@ -336,6 +482,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
336
482
|
|
|
337
483
|
if (isNativeNode(child)) native.remove(child.id, nativeIdsIn(child))
|
|
338
484
|
else syncTextContent(parent)
|
|
485
|
+
redraw(parent)
|
|
339
486
|
},
|
|
340
487
|
|
|
341
488
|
parentNode(node) {
|
|
@@ -349,7 +496,18 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
349
496
|
},
|
|
350
497
|
|
|
351
498
|
patchProp(element, key, previousValue, nextValue, _namespace, component) {
|
|
352
|
-
|
|
499
|
+
const drawnView = drawnViewsByPartTag.get(element.type)
|
|
500
|
+
|
|
501
|
+
if (drawnView) {
|
|
502
|
+
if (isEventProp(camelize(key))) {
|
|
503
|
+
patchDrawnEventHandler(element, camelize(key), nextValue, component ?? null)
|
|
504
|
+
} else {
|
|
505
|
+
drawnView.setPartProp(element, key, nextValue)
|
|
506
|
+
redraw(element)
|
|
507
|
+
}
|
|
508
|
+
} else if (drawnViewsByType.has(element.type) && patchDrawnViewProp(element, key, nextValue)) {
|
|
509
|
+
return
|
|
510
|
+
} else if (key === 'style') updateStyle(element, { style: nextValue })
|
|
353
511
|
else if (key === 'class') updateStyle(element, { className: nextValue ?? undefined })
|
|
354
512
|
else if (key === 'pressed') updateStyle(element, { pressed: nextValue })
|
|
355
513
|
else if (isAccessibilityAttribute(key)) updateAccessibility(element, key, nextValue)
|
|
@@ -373,6 +531,13 @@ const MODEL_EVENT_BY_NATIVE_EVENT: Record<string, string> = {
|
|
|
373
531
|
}
|
|
374
532
|
|
|
375
533
|
globalThis.__dispatch = (nodeId, eventName, payload) => {
|
|
534
|
+
// A press on a drawn view's elements: the innermost first, then the groups around it.
|
|
535
|
+
if (eventName === 'elementpress') {
|
|
536
|
+
const { nodeIds, ...location } = payload as { nodeIds: number[] }
|
|
537
|
+
|
|
538
|
+
return nodeIds.forEach((id) => eventHandlersByNodeId[id]?.press?.(location))
|
|
539
|
+
}
|
|
540
|
+
|
|
376
541
|
updateFocus(nodeId, eventName)
|
|
377
542
|
|
|
378
543
|
const handlers = eventHandlersByNodeId[nodeId]
|
package/src/router.ts
CHANGED
|
@@ -28,16 +28,26 @@ import {
|
|
|
28
28
|
} from '@vue/runtime-core'
|
|
29
29
|
import { processColor } from './colors'
|
|
30
30
|
import { Pressable } from './components'
|
|
31
|
-
import type { SvgIcon } from './native-components'
|
|
32
31
|
import { setURLNavigation } from './native-events'
|
|
33
32
|
import { createApp } from './renderer'
|
|
34
33
|
|
|
35
34
|
export type Params = Record<string, string | string[]>
|
|
36
35
|
|
|
36
|
+
/**
|
|
37
|
+
* A tab bar or header icon: an SF Symbol's name (iOS only), an image ({ uri }: an imported asset or a url),
|
|
38
|
+
* or one a package draws natively, like @nativue/icons' components.
|
|
39
|
+
*/
|
|
40
|
+
export type NavigationIcon = string | { uri: string } | DrawnIcon
|
|
41
|
+
|
|
42
|
+
/** Drawn by the package that registered `drawer` natively (registry.addIconDrawer), e.g. @nativue/svg. */
|
|
43
|
+
export interface DrawnIcon {
|
|
44
|
+
drawnIcon: { drawer: string; [data: string]: unknown }
|
|
45
|
+
}
|
|
46
|
+
|
|
37
47
|
// An icon shows instead of the title, which still names it for screen readers.
|
|
38
48
|
export interface HeaderItem {
|
|
39
49
|
title?: string
|
|
40
|
-
icon?:
|
|
50
|
+
icon?: NavigationIcon
|
|
41
51
|
onPress?: () => void
|
|
42
52
|
}
|
|
43
53
|
|
|
@@ -52,7 +62,7 @@ export interface ScreenOptions {
|
|
|
52
62
|
presentation?: 'card' | 'modal' | 'formSheet' | 'fullScreenModal'
|
|
53
63
|
// <Tabs> only
|
|
54
64
|
tabBarLabel?: string
|
|
55
|
-
tabBarIcon?:
|
|
65
|
+
tabBarIcon?: NavigationIcon
|
|
56
66
|
tabBarBadge?: string | number
|
|
57
67
|
tabBarActiveTintColor?: string // the whole bar's: set it in <Tabs :screen-options>
|
|
58
68
|
tabBarInactiveTintColor?: string
|
|
@@ -710,7 +720,7 @@ const StackNavigatorComponent = defineComponent({
|
|
|
710
720
|
})
|
|
711
721
|
|
|
712
722
|
// A page tab gets a single-screen stack, for its header; a layout tab brings its own navigator.
|
|
713
|
-
// Tab options: title / tabBarLabel, tabBarIcon
|
|
723
|
+
// Tab options: title / tabBarLabel, tabBarIcon, tabBarBadge, plus the header ones, and
|
|
714
724
|
// lazy: mount the tab's content only when it's first opened (faster startup, a short build on first open).
|
|
715
725
|
function tabContent(tab: Entry, options: ScreenOptions) {
|
|
716
726
|
if (tab.node.type !== 'page') return screenContent(tab)
|
|
@@ -724,9 +734,12 @@ function optionalColor(color: string | undefined) {
|
|
|
724
734
|
return color && processColor(color)
|
|
725
735
|
}
|
|
726
736
|
|
|
727
|
-
// An
|
|
728
|
-
function iconProps(icon:
|
|
729
|
-
|
|
737
|
+
// An icon as the native side reads it: systemImage, image (a url) or drawnIcon.
|
|
738
|
+
function iconProps(icon: NavigationIcon | undefined) {
|
|
739
|
+
if (typeof icon === 'string') return { systemImage: icon }
|
|
740
|
+
if (icon && 'uri' in icon) return { image: icon.uri }
|
|
741
|
+
|
|
742
|
+
return { drawnIcon: icon?.drawnIcon }
|
|
730
743
|
}
|
|
731
744
|
|
|
732
745
|
const TabScreen = defineComponent({
|