@nativue/core 0.4.4 → 0.5.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/android/src/main/java/dev/nativue/Bridge.kt +1 -0
- package/android/src/main/java/dev/nativue/Views.kt +79 -14
- package/ios/Bridge.swift +3 -0
- package/ios/ImageView.swift +82 -8
- package/package.json +1 -1
- package/src/components.ts +91 -39
- package/src/native-components.ts +4 -1
- package/src/renderer.ts +2 -1
|
@@ -894,6 +894,7 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
894
894
|
|
|
895
895
|
"source" -> (view as? RemoteImageView)?.load(value as? String)
|
|
896
896
|
|
|
897
|
+
"cache" -> (view as? RemoteImageView)?.cachePolicy = value as? String ?: "disk"
|
|
897
898
|
"resizeMode" -> (view as? RemoteImageView)?.scaleType = RemoteImageView.scaleTypes[value] ?: ImageView.ScaleType.CENTER_CROP
|
|
898
899
|
|
|
899
900
|
"onLongPress" -> (view as? PressableView)?.reportsLongPress = isTruthyAttribute(value)
|
|
@@ -20,7 +20,9 @@ import android.widget.ImageView
|
|
|
20
20
|
import android.widget.ProgressBar
|
|
21
21
|
import android.widget.ScrollView
|
|
22
22
|
import android.widget.Switch
|
|
23
|
+
import java.io.File
|
|
23
24
|
import java.net.URL
|
|
25
|
+
import java.security.MessageDigest
|
|
24
26
|
import java.util.concurrent.Executors
|
|
25
27
|
import kotlin.math.min
|
|
26
28
|
import kotlin.math.roundToInt
|
|
@@ -489,15 +491,17 @@ private fun pageOffset(offset: Int, pageSize: Int, velocity: Int): Int {
|
|
|
489
491
|
}
|
|
490
492
|
|
|
491
493
|
/**
|
|
492
|
-
* Loads `data:` URIs (bundled images) and http(s) URLs off the main thread.
|
|
493
|
-
*
|
|
494
|
+
* Image view behind <Image>. Loads `data:` URIs (bundled images) and http(s) URLs off the main thread.
|
|
495
|
+
* Mirrors ios/ImageView.swift: with `cache` "disk" (the default), downloads are kept on disk and decoded
|
|
496
|
+
* images in memory; "memory" keeps only the decoded images; "none" keeps nothing.
|
|
494
497
|
*/
|
|
495
|
-
// Limitation: No HTTP disk cache, unlike iOS's URLCache: after a relaunch images download again. Give image
|
|
496
|
-
// requests their own cache if that matters (a global HttpResponseCache would also cache the dev bundle).
|
|
497
498
|
class RemoteImageView(context: Context) : ImageView(context) {
|
|
498
499
|
/** The image changed, and with it the size the view measures. */
|
|
499
500
|
var onImageChange: () -> Unit = {}
|
|
500
501
|
|
|
502
|
+
/** "disk", "memory" or "none". Read when a download finishes, so it applies even when set after `source`. */
|
|
503
|
+
var cachePolicy = "disk"
|
|
504
|
+
|
|
501
505
|
private var source: String? = null
|
|
502
506
|
private var bitmap: Bitmap? = null
|
|
503
507
|
|
|
@@ -509,16 +513,33 @@ class RemoteImageView(context: Context) : ImageView(context) {
|
|
|
509
513
|
if (source == this.source) return
|
|
510
514
|
|
|
511
515
|
this.source = source
|
|
512
|
-
show(source
|
|
516
|
+
show(if (source != null && cachePolicy != "none") decodedImages.get(source) else null)
|
|
513
517
|
if (source == null || bitmap != null) return
|
|
514
518
|
|
|
519
|
+
val diskCache = ImageDiskCache(context.cacheDir)
|
|
520
|
+
val readsDisk = cachePolicy == "disk"
|
|
515
521
|
loader.execute {
|
|
516
|
-
val
|
|
517
|
-
|
|
518
|
-
|
|
522
|
+
val cachedBytes = if (readsDisk) diskCache.read(source) else null
|
|
523
|
+
val downloadedBytes = if (cachedBytes == null) runCatching { download(source) }.getOrNull() else null
|
|
524
|
+
val bytes = cachedBytes ?: downloadedBytes ?: return@execute
|
|
525
|
+
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) ?: return@execute
|
|
526
|
+
|
|
527
|
+
post {
|
|
528
|
+
store(bitmap, downloadedBytes, source, diskCache)
|
|
529
|
+
if (source == this.source) show(bitmap)
|
|
530
|
+
}
|
|
519
531
|
}
|
|
520
532
|
}
|
|
521
533
|
|
|
534
|
+
private fun store(bitmap: Bitmap, downloadedBytes: ByteArray?, source: String, diskCache: ImageDiskCache) {
|
|
535
|
+
if (cachePolicy == "none") return
|
|
536
|
+
|
|
537
|
+
decodedImages.put(source, bitmap)
|
|
538
|
+
if (cachePolicy != "disk" || downloadedBytes == null || source.startsWith("data:")) return
|
|
539
|
+
|
|
540
|
+
loader.execute { diskCache.write(source, downloadedBytes) }
|
|
541
|
+
}
|
|
542
|
+
|
|
522
543
|
private fun show(nextBitmap: Bitmap?) {
|
|
523
544
|
if (nextBitmap === bitmap) return
|
|
524
545
|
|
|
@@ -557,13 +578,10 @@ class RemoteImageView(context: Context) : ImageView(context) {
|
|
|
557
578
|
setMeasuredDimension(measuredWidth.roundToInt(), measuredHeight.roundToInt())
|
|
558
579
|
}
|
|
559
580
|
|
|
560
|
-
private fun
|
|
561
|
-
if (source.startsWith("data:"))
|
|
562
|
-
val bytes = Base64.decode(source.substringAfter(","), Base64.DEFAULT)
|
|
563
|
-
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
|
|
564
|
-
}
|
|
581
|
+
private fun download(source: String): ByteArray {
|
|
582
|
+
if (source.startsWith("data:")) return Base64.decode(source.substringAfter(","), Base64.DEFAULT)
|
|
565
583
|
|
|
566
|
-
return URL(source).openStream().use(
|
|
584
|
+
return URL(source).openStream().use { it.readBytes() }
|
|
567
585
|
}
|
|
568
586
|
|
|
569
587
|
companion object {
|
|
@@ -583,6 +601,53 @@ class RemoteImageView(context: Context) : ImageView(context) {
|
|
|
583
601
|
}
|
|
584
602
|
}
|
|
585
603
|
|
|
604
|
+
/**
|
|
605
|
+
* Downloaded images by url, in the app's cache folder, which Android may empty when storage runs low. Past
|
|
606
|
+
* its limit, the images used least recently go first.
|
|
607
|
+
*/
|
|
608
|
+
private class ImageDiskCache(cacheDirectory: File) {
|
|
609
|
+
private val directory = File(cacheDirectory, "nativue-images")
|
|
610
|
+
|
|
611
|
+
fun read(source: String): ByteArray? {
|
|
612
|
+
val file = fileFor(source)
|
|
613
|
+
if (!file.exists()) return null
|
|
614
|
+
|
|
615
|
+
file.setLastModified(System.currentTimeMillis())
|
|
616
|
+
|
|
617
|
+
return runCatching { file.readBytes() }.getOrNull()
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
fun write(source: String, bytes: ByteArray) {
|
|
621
|
+
directory.mkdirs()
|
|
622
|
+
val partialFile = File(directory, "${fileFor(source).name}.partial")
|
|
623
|
+
partialFile.writeBytes(bytes)
|
|
624
|
+
partialFile.renameTo(fileFor(source)) // whole files only: a reader never sees half an image
|
|
625
|
+
trim()
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
private fun fileFor(source: String): File {
|
|
629
|
+
val hash = MessageDigest.getInstance("SHA-256").digest(source.toByteArray())
|
|
630
|
+
|
|
631
|
+
return File(directory, hash.joinToString("") { "%02x".format(it) })
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
private fun trim() {
|
|
635
|
+
val files = directory.listFiles()?.sortedBy(File::lastModified) ?: return
|
|
636
|
+
var totalSize = files.sumOf(File::length)
|
|
637
|
+
|
|
638
|
+
for (file in files) {
|
|
639
|
+
if (totalSize <= MAX_BYTE_COUNT) break
|
|
640
|
+
|
|
641
|
+
totalSize -= file.length()
|
|
642
|
+
file.delete()
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
companion object {
|
|
647
|
+
const val MAX_BYTE_COUNT = 100L * 1024 * 1024
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
586
651
|
/** Single-line text field behind <TextInput>. Mirrors ios/TextInputView.swift. */
|
|
587
652
|
class TextInputView(context: Context) : android.widget.EditText(context), ThemeAware {
|
|
588
653
|
/** Without a `color` style, the text follows the theme. */
|
package/ios/Bridge.swift
CHANGED
|
@@ -899,6 +899,9 @@ final class Bridge {
|
|
|
899
899
|
case "source":
|
|
900
900
|
(node.view as? ImageView)?.load(value as? String)
|
|
901
901
|
|
|
902
|
+
case "cache":
|
|
903
|
+
(node.view as? ImageView)?.cachePolicy = value as? String ?? "disk"
|
|
904
|
+
|
|
902
905
|
case "resizeMode":
|
|
903
906
|
node.view.contentMode = ImageView.resizeModes[value as? String ?? ""] ?? .scaleAspectFill
|
|
904
907
|
|
package/ios/ImageView.swift
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import CryptoKit
|
|
1
2
|
import UIKit
|
|
2
3
|
import yoga
|
|
3
4
|
|
|
4
|
-
/// Image view behind <Image>. Loads `source` (an http(s) or data: url)
|
|
5
|
-
///
|
|
6
|
-
/// screen (a list row) shows at once.
|
|
5
|
+
/// Image view behind <Image>. Loads `source` (an http(s) or data: url). With `cache` "disk" (the default),
|
|
6
|
+
/// downloads are kept on disk and decoded images in memory; "memory" keeps only the decoded images, so
|
|
7
|
+
/// one that comes back on screen (a list row) shows at once; "none" keeps nothing.
|
|
7
8
|
final class ImageView: UIImageView {
|
|
8
9
|
static let resizeModes: [String: UIView.ContentMode] = [
|
|
9
10
|
"cover": .scaleAspectFill,
|
|
@@ -20,9 +21,20 @@ final class ImageView: UIImageView {
|
|
|
20
21
|
return cache
|
|
21
22
|
}()
|
|
22
23
|
|
|
24
|
+
/// Its own cache only: URLCache would keep a second copy of each download.
|
|
25
|
+
private static let session = URLSession(configuration: {
|
|
26
|
+
let configuration = URLSessionConfiguration.ephemeral
|
|
27
|
+
configuration.urlCache = nil
|
|
28
|
+
|
|
29
|
+
return configuration
|
|
30
|
+
}())
|
|
31
|
+
|
|
23
32
|
/// The image changed, and with it the size the view measures.
|
|
24
33
|
var onImageChange: (() -> Void)?
|
|
25
34
|
|
|
35
|
+
/// "disk", "memory" or "none". Read when a download finishes, so it applies even when set after `source`.
|
|
36
|
+
var cachePolicy = "disk"
|
|
37
|
+
|
|
26
38
|
private var loadingTask: Task<Void, Never>?
|
|
27
39
|
|
|
28
40
|
override init(frame: CGRect) {
|
|
@@ -40,21 +52,36 @@ final class ImageView: UIImageView {
|
|
|
40
52
|
loadingTask?.cancel()
|
|
41
53
|
|
|
42
54
|
guard let source, let url = URL(string: source) else { return show(nil) }
|
|
43
|
-
if let cachedImage = Self.decodedImages.object(forKey: source as NSString) {
|
|
55
|
+
if cachePolicy != "none", let cachedImage = Self.decodedImages.object(forKey: source as NSString) {
|
|
56
|
+
return show(cachedImage)
|
|
57
|
+
}
|
|
44
58
|
|
|
45
59
|
show(nil)
|
|
60
|
+
let readsDisk = cachePolicy == "disk"
|
|
46
61
|
loadingTask = Task { [weak self] in
|
|
62
|
+
let cachedData = readsDisk ? await Task.detached { ImageDiskCache.data(for: source) }.value : nil
|
|
63
|
+
let downloadedData = cachedData == nil ? try? await Self.session.data(from: url).0 : nil
|
|
47
64
|
guard
|
|
48
|
-
let
|
|
65
|
+
let data = cachedData ?? downloadedData,
|
|
49
66
|
let image = await UIImage(data: data)?.byPreparingForDisplay(), // decoded off the main thread
|
|
50
|
-
!Task.isCancelled
|
|
67
|
+
!Task.isCancelled,
|
|
68
|
+
let self
|
|
51
69
|
else { return }
|
|
52
70
|
|
|
53
|
-
|
|
54
|
-
|
|
71
|
+
store(image, downloadedData: downloadedData, for: source)
|
|
72
|
+
show(image)
|
|
55
73
|
}
|
|
56
74
|
}
|
|
57
75
|
|
|
76
|
+
private func store(_ image: UIImage, downloadedData: Data?, for source: String) {
|
|
77
|
+
guard cachePolicy != "none" else { return }
|
|
78
|
+
|
|
79
|
+
Self.decodedImages.setObject(image, forKey: source as NSString, cost: Self.byteCount(of: image))
|
|
80
|
+
|
|
81
|
+
guard cachePolicy == "disk", let downloadedData, !source.hasPrefix("data:") else { return }
|
|
82
|
+
Task.detached(priority: .utility) { ImageDiskCache.store(downloadedData, for: source) }
|
|
83
|
+
}
|
|
84
|
+
|
|
58
85
|
/// Without a width or height, the image takes its own size, a pixel per point; given one of them, the
|
|
59
86
|
/// other follows its aspect ratio, as on the web.
|
|
60
87
|
func measuredSize(width: Float, widthMode: YGMeasureMode, height: Float, heightMode: YGMeasureMode) -> YGSize {
|
|
@@ -90,3 +117,50 @@ final class ImageView: UIImageView {
|
|
|
90
117
|
return cgImage.bytesPerRow * cgImage.height
|
|
91
118
|
}
|
|
92
119
|
}
|
|
120
|
+
|
|
121
|
+
/// Downloaded images by url, in the system's caches folder, which iOS may empty when storage runs low. Past
|
|
122
|
+
/// its limit, the images used least recently go first.
|
|
123
|
+
private enum ImageDiskCache {
|
|
124
|
+
static let maxByteCount = 100 * 1024 * 1024
|
|
125
|
+
|
|
126
|
+
private static let directory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
|
|
127
|
+
.appendingPathComponent("nativue-images", isDirectory: true)
|
|
128
|
+
|
|
129
|
+
static func data(for source: String) -> Data? {
|
|
130
|
+
let file = fileURL(for: source)
|
|
131
|
+
guard let data = try? Data(contentsOf: file) else { return nil }
|
|
132
|
+
|
|
133
|
+
try? FileManager.default.setAttributes([.modificationDate: Date()], ofItemAtPath: file.path)
|
|
134
|
+
|
|
135
|
+
return data
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static func store(_ data: Data, for source: String) {
|
|
139
|
+
try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
140
|
+
try? data.write(to: fileURL(for: source), options: .atomic)
|
|
141
|
+
trim()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private static func fileURL(for source: String) -> URL {
|
|
145
|
+
let hash = SHA256.hash(data: Data(source.utf8)).map { String(format: "%02x", $0) }.joined()
|
|
146
|
+
|
|
147
|
+
return directory.appendingPathComponent(hash)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private static func trim() {
|
|
151
|
+
let keys: Set<URLResourceKey> = [.contentModificationDateKey, .fileSizeKey]
|
|
152
|
+
guard let files = try? FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: Array(keys)) else { return }
|
|
153
|
+
|
|
154
|
+
let entries = files.compactMap { file -> (file: URL, usedAt: Date, size: Int)? in
|
|
155
|
+
guard let values = try? file.resourceValues(forKeys: keys) else { return nil }
|
|
156
|
+
|
|
157
|
+
return (file, values.contentModificationDate ?? .distantPast, values.fileSize ?? 0)
|
|
158
|
+
}
|
|
159
|
+
var totalSize = entries.reduce(0) { $0 + $1.size }
|
|
160
|
+
|
|
161
|
+
for entry in entries.sorted(by: { $0.usedAt < $1.usedAt }) where totalSize > maxByteCount {
|
|
162
|
+
try? FileManager.default.removeItem(at: entry.file)
|
|
163
|
+
totalSize -= entry.size
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
package/package.json
CHANGED
package/src/components.ts
CHANGED
|
@@ -89,12 +89,13 @@ export const Pressable = defineComponent({
|
|
|
89
89
|
|
|
90
90
|
const OFFSCREEN_ROWS_RENDERED = 10
|
|
91
91
|
// What a row not yet measured counts as, until some are: then their average.
|
|
92
|
-
const
|
|
92
|
+
const ESTIMATED_ROW_LENGTH = 50
|
|
93
93
|
|
|
94
|
-
// Renders only the rows near the visible area; the rest is empty padding of the same
|
|
95
|
-
// Slots: #item="{ item, index }", plus #header, #footer and #empty.
|
|
96
|
-
//
|
|
97
|
-
//
|
|
94
|
+
// Renders only the rows near the visible area; the rest is empty padding of the same length.
|
|
95
|
+
// Slots: #item="{ item, index }", plus #header, #footer and #empty. Vertical by default, sideways with
|
|
96
|
+
// `horizontal`. With `item-height` (or `item-width` when horizontal), every row is that long; without
|
|
97
|
+
// it, rows size to their content and are measured as they render.
|
|
98
|
+
// Limitation: Rows not rendered yet count as the average measured length, so jumping far (dragging the
|
|
98
99
|
// scroll indicator) can shift the rows a few points once they're measured; anchor the scroll position
|
|
99
100
|
// to the first visible row if that shows.
|
|
100
101
|
// Limitation: Items are `any` in the #item slot; make FlatList generic if typed rows are wanted.
|
|
@@ -104,7 +105,9 @@ export const FlatList = defineComponent({
|
|
|
104
105
|
name: 'FlatList',
|
|
105
106
|
props: {
|
|
106
107
|
data: { type: Array as PropType<any[]>, required: true },
|
|
108
|
+
horizontal: Boolean,
|
|
107
109
|
itemHeight: Number,
|
|
110
|
+
itemWidth: Number,
|
|
108
111
|
keyExtractor: {
|
|
109
112
|
type: Function as PropType<(item: any, index: number) => string | number>,
|
|
110
113
|
default: (_item: unknown, index: number) => index,
|
|
@@ -119,48 +122,55 @@ export const FlatList = defineComponent({
|
|
|
119
122
|
}>,
|
|
120
123
|
|
|
121
124
|
setup(props, { slots }) {
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
const
|
|
125
|
-
const
|
|
125
|
+
const scrollPosition = ref(0)
|
|
126
|
+
const viewportLength = ref(0)
|
|
127
|
+
const headerLength = ref(0)
|
|
128
|
+
const measuredLengths = reactive(new Map<string | number, number>())
|
|
129
|
+
|
|
130
|
+
// Along the scroll direction: heights for a vertical list, widths for a horizontal one.
|
|
131
|
+
const fixedLength = computed(() => (props.horizontal ? props.itemWidth : props.itemHeight))
|
|
132
|
+
const lengthOf = ({ width, height }: { width: number; height: number }) =>
|
|
133
|
+
props.horizontal ? width : height
|
|
126
134
|
|
|
127
135
|
const offsets = computed(() => {
|
|
128
|
-
const
|
|
129
|
-
const
|
|
136
|
+
const estimatedLength = averageOf(measuredLengths.values()) ?? ESTIMATED_ROW_LENGTH
|
|
137
|
+
const lengths = props.data.map(
|
|
130
138
|
(item, index) =>
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
139
|
+
fixedLength.value ??
|
|
140
|
+
measuredLengths.get(props.keyExtractor(item, index)) ??
|
|
141
|
+
estimatedLength,
|
|
134
142
|
)
|
|
135
143
|
|
|
136
|
-
return rowOffsets(
|
|
144
|
+
return rowOffsets(lengths)
|
|
137
145
|
})
|
|
138
146
|
|
|
139
147
|
// Numbers, so scrolling within the same rows doesn't re-render.
|
|
140
148
|
const start = computed(() =>
|
|
141
|
-
Math.max(0, rowAt(offsets.value,
|
|
149
|
+
Math.max(0, rowAt(offsets.value, scrollPosition.value) - OFFSCREEN_ROWS_RENDERED),
|
|
142
150
|
)
|
|
143
151
|
const end = computed(() => {
|
|
144
|
-
const lastVisibleRow = rowAt(offsets.value,
|
|
152
|
+
const lastVisibleRow = rowAt(offsets.value, scrollPosition.value + viewportLength.value)
|
|
145
153
|
|
|
146
154
|
return Math.min(props.data.length, lastVisibleRow + 1 + OFFSCREEN_ROWS_RENDERED)
|
|
147
155
|
})
|
|
148
156
|
|
|
149
157
|
// Bouncing past either edge reports offsets outside the list: clamp them, or the window (and the
|
|
150
158
|
// layout) would change on every frame of the bounce.
|
|
151
|
-
function handleScroll(
|
|
152
|
-
const
|
|
159
|
+
function handleScroll(event: ScrollEvent) {
|
|
160
|
+
const offset = props.horizontal ? event.x : event.y
|
|
161
|
+
const length = lengthOf(event)
|
|
162
|
+
const maxScrollPosition = Math.max(0, offsets.value[props.data.length] - length)
|
|
153
163
|
|
|
154
|
-
|
|
155
|
-
|
|
164
|
+
scrollPosition.value = Math.min(Math.max(offset - headerLength.value, 0), maxScrollPosition)
|
|
165
|
+
viewportLength.value = length
|
|
156
166
|
}
|
|
157
167
|
|
|
158
168
|
function renderRow(item: unknown, offset: number): VNode {
|
|
159
169
|
const index = start.value + offset
|
|
160
170
|
const key = props.keyExtractor(item, index)
|
|
161
|
-
const sizing =
|
|
162
|
-
? { style: { height:
|
|
163
|
-
: { onLayout: (
|
|
171
|
+
const sizing = fixedLength.value
|
|
172
|
+
? { style: props.horizontal ? { width: fixedLength.value } : { height: fixedLength.value } }
|
|
173
|
+
: { onLayout: (layout: LayoutEvent) => measuredLengths.set(key, lengthOf(layout)) }
|
|
164
174
|
|
|
165
175
|
return h('View', { key, ...sizing }, slots.item?.({ item, index }))
|
|
166
176
|
}
|
|
@@ -168,28 +178,43 @@ export const FlatList = defineComponent({
|
|
|
168
178
|
function renderHeader(): VNode | null {
|
|
169
179
|
if (!slots.header) return null
|
|
170
180
|
|
|
171
|
-
const onLayout = (
|
|
181
|
+
const onLayout = (layout: LayoutEvent) => (headerLength.value = lengthOf(layout))
|
|
172
182
|
|
|
173
183
|
return h('View', { onLayout }, slots.header({}))
|
|
174
184
|
}
|
|
175
185
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
186
|
+
// overflow: 'scroll' tells Yoga the rows may reach past the window's box: otherwise it caps a row
|
|
187
|
+
// that sizes to its content at the space the paddings leave, which is none.
|
|
188
|
+
function windowStyle(): Style {
|
|
189
|
+
const before = offsets.value[start.value]
|
|
190
|
+
const after = offsets.value[props.data.length] - offsets.value[end.value]
|
|
191
|
+
|
|
192
|
+
if (props.horizontal) {
|
|
193
|
+
return {
|
|
194
|
+
flexDirection: 'row',
|
|
195
|
+
paddingLeft: before,
|
|
196
|
+
paddingRight: after,
|
|
197
|
+
overflow: 'scroll',
|
|
198
|
+
}
|
|
184
199
|
}
|
|
185
200
|
|
|
201
|
+
return { paddingTop: before, paddingBottom: after, overflow: 'scroll' }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return () => {
|
|
205
|
+
const rows = props.data.slice(start.value, end.value).map(renderRow)
|
|
206
|
+
|
|
186
207
|
// Other attributes (refreshing, @refresh...) fall through to the ScrollView.
|
|
187
|
-
return h(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
208
|
+
return h(
|
|
209
|
+
'ScrollView',
|
|
210
|
+
{ style: props.style, horizontal: props.horizontal, onScroll: handleScroll },
|
|
211
|
+
[
|
|
212
|
+
renderHeader(),
|
|
213
|
+
h('View', { style: windowStyle() }, rows),
|
|
214
|
+
props.data.length === 0 ? slots.empty?.({}) : null,
|
|
215
|
+
slots.footer?.({}),
|
|
216
|
+
],
|
|
217
|
+
)
|
|
193
218
|
}
|
|
194
219
|
},
|
|
195
220
|
})
|
|
@@ -205,6 +230,33 @@ function averageOf(values: Iterable<number>): number | undefined {
|
|
|
205
230
|
return count === 0 ? undefined : sum / count
|
|
206
231
|
}
|
|
207
232
|
|
|
233
|
+
const FILL_STYLE: Style = { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 }
|
|
234
|
+
|
|
235
|
+
// An <Image> behind its children: it fills the box, which `style` sizes. Takes the Image's props; other
|
|
236
|
+
// attributes (role, aria-*, @layout...) go to the box.
|
|
237
|
+
export const ImageBackground = defineComponent({
|
|
238
|
+
name: 'ImageBackground',
|
|
239
|
+
props: {
|
|
240
|
+
source: { type: [String, Object] as PropType<string | { uri: string }>, required: true },
|
|
241
|
+
resizeMode: String as PropType<'cover' | 'contain' | 'stretch' | 'center'>,
|
|
242
|
+
cache: String as PropType<'disk' | 'memory' | 'none'>,
|
|
243
|
+
alt: String,
|
|
244
|
+
style: Object as PropType<Style>,
|
|
245
|
+
},
|
|
246
|
+
slots: Object as SlotsType<{ default?: {} }>,
|
|
247
|
+
|
|
248
|
+
setup(props, { slots }) {
|
|
249
|
+
return () => {
|
|
250
|
+
const { style, ...imageProps } = props
|
|
251
|
+
|
|
252
|
+
return h('View', { style }, [
|
|
253
|
+
h('Image', { ...imageProps, style: FILL_STYLE }),
|
|
254
|
+
slots.default?.({}),
|
|
255
|
+
])
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
})
|
|
259
|
+
|
|
208
260
|
const SWIPE_ACTIONS_STYLE: Style = { position: 'absolute', top: 0, bottom: 0, flexDirection: 'row' }
|
|
209
261
|
const SWIPE_LEFT_ACTIONS_STYLE: Style = { ...SWIPE_ACTIONS_STYLE, left: 0 }
|
|
210
262
|
const SWIPE_RIGHT_ACTIONS_STYLE: Style = { ...SWIPE_ACTIONS_STYLE, right: 0 }
|
package/src/native-components.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Events are listed as on* props: @press is onPress, @change-text is onChangeText.
|
|
3
3
|
import type { DefineComponent } from '@vue/runtime-core'
|
|
4
4
|
import type { AnimatedEvent } from './animated'
|
|
5
|
-
import type { FlatList, Pressable, Swipeable } from './components'
|
|
5
|
+
import type { FlatList, ImageBackground, Pressable, Swipeable } from './components'
|
|
6
6
|
import type { LayoutEvent, PanEvent, PinchEvent, RotateEvent, ScrollEvent, Style } from './types'
|
|
7
7
|
|
|
8
8
|
type NativeComponent<Props = {}> = DefineComponent<
|
|
@@ -78,6 +78,8 @@ declare module '@vue/runtime-core' {
|
|
|
78
78
|
/** What the image shows, for screen readers. Leave it out for decorative images. */
|
|
79
79
|
alt?: string
|
|
80
80
|
resizeMode?: 'cover' | 'contain' | 'stretch' | 'center'
|
|
81
|
+
/** Where downloads are kept: on disk and in memory (the default), in memory only, or not at all. */
|
|
82
|
+
cache?: 'disk' | 'memory' | 'none'
|
|
81
83
|
}>
|
|
82
84
|
Switch: NativeComponent<
|
|
83
85
|
ModelProps<boolean> & { value?: boolean; onValueChange?: (value: boolean) => void }
|
|
@@ -106,5 +108,6 @@ declare module '@vue/runtime-core' {
|
|
|
106
108
|
Pressable: typeof Pressable
|
|
107
109
|
FlatList: typeof FlatList
|
|
108
110
|
Swipeable: typeof Swipeable
|
|
111
|
+
ImageBackground: typeof ImageBackground
|
|
109
112
|
}
|
|
110
113
|
}
|
package/src/renderer.ts
CHANGED
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
import { animatedEventMapping, animatedStyleBinding, isAnimatedEvent } from './animated'
|
|
22
22
|
import { Appearance } from './appearance'
|
|
23
23
|
import { COLOR_PROPERTIES, processColor } from './colors'
|
|
24
|
-
import { FlatList, Pressable, Swipeable } from './components'
|
|
24
|
+
import { FlatList, ImageBackground, Pressable, Swipeable } from './components'
|
|
25
25
|
import type { LayoutAnimationConfig, Style } from './types'
|
|
26
26
|
export * from '@vue/runtime-core'
|
|
27
27
|
export { AccessibilityInfo } from './accessibility'
|
|
@@ -511,6 +511,7 @@ export function createApp(rootComponent: Component) {
|
|
|
511
511
|
app.component('Pressable', Pressable)
|
|
512
512
|
app.component('FlatList', FlatList)
|
|
513
513
|
app.component('Swipeable', Swipeable)
|
|
514
|
+
app.component('ImageBackground', ImageBackground)
|
|
514
515
|
|
|
515
516
|
return Object.assign(app, { mount: () => mountOnContainer(rootNode) })
|
|
516
517
|
}
|