@getcortexlabs/mavis 0.1.0-alpha.4 → 0.1.0-alpha.5

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.
Files changed (155) hide show
  1. package/README.md +7 -5
  2. package/dist/files.d.ts +4 -0
  3. package/dist/files.d.ts.map +1 -0
  4. package/dist/files.js +3 -0
  5. package/dist/files.js.map +1 -0
  6. package/dist/native/combobox-editor.android.d.ts +3 -0
  7. package/dist/native/combobox-editor.android.d.ts.map +1 -0
  8. package/dist/native/combobox-editor.android.js +5 -0
  9. package/dist/native/combobox-editor.android.js.map +1 -0
  10. package/dist/native/combobox-editor.d.ts +4 -0
  11. package/dist/native/combobox-editor.d.ts.map +1 -0
  12. package/dist/native/combobox-editor.js +5 -0
  13. package/dist/native/combobox-editor.js.map +1 -0
  14. package/dist/native/file-access.d.ts +23 -0
  15. package/dist/native/file-access.d.ts.map +1 -0
  16. package/dist/native/file-access.js +55 -0
  17. package/dist/native/file-access.js.map +1 -0
  18. package/dist/native/file-picker.android.d.ts +3 -0
  19. package/dist/native/file-picker.android.d.ts.map +1 -0
  20. package/dist/native/file-picker.android.js +147 -0
  21. package/dist/native/file-picker.android.js.map +1 -0
  22. package/dist/native/file-picker.d.ts +4 -0
  23. package/dist/native/file-picker.d.ts.map +1 -0
  24. package/dist/native/file-picker.ios.d.ts +3 -0
  25. package/dist/native/file-picker.ios.d.ts.map +1 -0
  26. package/dist/native/file-picker.ios.js +152 -0
  27. package/dist/native/file-picker.ios.js.map +1 -0
  28. package/dist/native/file-picker.js +7 -0
  29. package/dist/native/file-picker.js.map +1 -0
  30. package/dist/native/motion-owner.d.ts +5 -0
  31. package/dist/native/motion-owner.d.ts.map +1 -0
  32. package/dist/native/motion-owner.js +53 -0
  33. package/dist/native/motion-owner.js.map +1 -0
  34. package/dist/native/motion-region.d.ts +6 -0
  35. package/dist/native/motion-region.d.ts.map +1 -0
  36. package/dist/native/motion-region.js +199 -0
  37. package/dist/native/motion-region.js.map +1 -0
  38. package/dist/native/motion-surface.d.ts +1 -1
  39. package/dist/native/motion-surface.d.ts.map +1 -1
  40. package/dist/native/motion-surface.js +18 -0
  41. package/dist/native/motion-surface.js.map +1 -1
  42. package/dist/native/scroll-motion.d.ts +1 -0
  43. package/dist/native/scroll-motion.d.ts.map +1 -1
  44. package/dist/native/scroll-motion.js +6 -0
  45. package/dist/native/scroll-motion.js.map +1 -1
  46. package/dist/vue/components/ChoiceControl.d.ts +21 -11
  47. package/dist/vue/components/ChoiceControl.d.ts.map +1 -1
  48. package/dist/vue/components/DateCalendar.d.ts +94 -0
  49. package/dist/vue/components/DateCalendar.d.ts.map +1 -0
  50. package/dist/vue/components/DateCalendar.js +96 -0
  51. package/dist/vue/components/DateCalendar.js.map +1 -0
  52. package/dist/vue/components/MAccordion.d.ts +19 -9
  53. package/dist/vue/components/MAccordion.d.ts.map +1 -1
  54. package/dist/vue/components/MAlert.d.ts +21 -11
  55. package/dist/vue/components/MAlert.d.ts.map +1 -1
  56. package/dist/vue/components/MAvatar.d.ts +12 -6
  57. package/dist/vue/components/MAvatar.d.ts.map +1 -1
  58. package/dist/vue/components/MBadge.d.ts +13 -7
  59. package/dist/vue/components/MBadge.d.ts.map +1 -1
  60. package/dist/vue/components/MButton.d.ts +81 -39
  61. package/dist/vue/components/MButton.d.ts.map +1 -1
  62. package/dist/vue/components/MCard.d.ts +46 -18
  63. package/dist/vue/components/MCard.d.ts.map +1 -1
  64. package/dist/vue/components/MCheckbox.d.ts +20 -10
  65. package/dist/vue/components/MCheckbox.d.ts.map +1 -1
  66. package/dist/vue/components/MChip.d.ts +23 -13
  67. package/dist/vue/components/MChip.d.ts.map +1 -1
  68. package/dist/vue/components/MCombobox.d.ts +279 -0
  69. package/dist/vue/components/MCombobox.d.ts.map +1 -0
  70. package/dist/vue/components/MCombobox.js +234 -0
  71. package/dist/vue/components/MCombobox.js.map +1 -0
  72. package/dist/vue/components/MDatePicker.d.ts +1254 -0
  73. package/dist/vue/components/MDatePicker.d.ts.map +1 -0
  74. package/dist/vue/components/MDatePicker.js +35 -0
  75. package/dist/vue/components/MDatePicker.js.map +1 -0
  76. package/dist/vue/components/MDialog.d.ts +7 -5
  77. package/dist/vue/components/MDialog.d.ts.map +1 -1
  78. package/dist/vue/components/MEmptyState.d.ts +20 -10
  79. package/dist/vue/components/MEmptyState.d.ts.map +1 -1
  80. package/dist/vue/components/MFilePicker.d.ts +112 -0
  81. package/dist/vue/components/MFilePicker.d.ts.map +1 -0
  82. package/dist/vue/components/MFilePicker.js +196 -0
  83. package/dist/vue/components/MFilePicker.js.map +1 -0
  84. package/dist/vue/components/MIcon.d.ts +5 -3
  85. package/dist/vue/components/MIcon.d.ts.map +1 -1
  86. package/dist/vue/components/MInput.d.ts +131 -59
  87. package/dist/vue/components/MInput.d.ts.map +1 -1
  88. package/dist/vue/components/MListItem.d.ts +24 -12
  89. package/dist/vue/components/MListItem.d.ts.map +1 -1
  90. package/dist/vue/components/MProgress.d.ts +19 -9
  91. package/dist/vue/components/MProgress.d.ts.map +1 -1
  92. package/dist/vue/components/MRadioGroup.d.ts +19 -9
  93. package/dist/vue/components/MRadioGroup.d.ts.map +1 -1
  94. package/dist/vue/components/MReveal.d.ts +73 -37
  95. package/dist/vue/components/MReveal.d.ts.map +1 -1
  96. package/dist/vue/components/MScreen.d.ts +12 -6
  97. package/dist/vue/components/MScreen.d.ts.map +1 -1
  98. package/dist/vue/components/MSegmentedControl.d.ts +19 -9
  99. package/dist/vue/components/MSegmentedControl.d.ts.map +1 -1
  100. package/dist/vue/components/MSelect.d.ts +21 -11
  101. package/dist/vue/components/MSelect.d.ts.map +1 -1
  102. package/dist/vue/components/MSeparator.d.ts +4 -2
  103. package/dist/vue/components/MSeparator.d.ts.map +1 -1
  104. package/dist/vue/components/MSheet.d.ts +19 -13
  105. package/dist/vue/components/MSheet.d.ts.map +1 -1
  106. package/dist/vue/components/MSkeleton.d.ts +17 -9
  107. package/dist/vue/components/MSkeleton.d.ts.map +1 -1
  108. package/dist/vue/components/MSlider.d.ts +9 -5
  109. package/dist/vue/components/MSlider.d.ts.map +1 -1
  110. package/dist/vue/components/MSwitch.d.ts +19 -9
  111. package/dist/vue/components/MSwitch.d.ts.map +1 -1
  112. package/dist/vue/components/MTabs.d.ts +19 -9
  113. package/dist/vue/components/MTabs.d.ts.map +1 -1
  114. package/dist/vue/components/MText.d.ts +46 -30
  115. package/dist/vue/components/MText.d.ts.map +1 -1
  116. package/dist/vue/components/layout.d.ts +200 -91
  117. package/dist/vue/components/layout.d.ts.map +1 -1
  118. package/dist/vue/components/layout.js +6 -4
  119. package/dist/vue/components/layout.js.map +1 -1
  120. package/dist/vue/components/workflows.d.ts +251 -0
  121. package/dist/vue/components/workflows.d.ts.map +1 -0
  122. package/dist/vue/components/workflows.js +35 -0
  123. package/dist/vue/components/workflows.js.map +1 -0
  124. package/dist/vue/index.d.ts +7 -2
  125. package/dist/vue/index.d.ts.map +1 -1
  126. package/dist/vue/index.js +15 -2
  127. package/dist/vue/index.js.map +1 -1
  128. package/dist/vue/use-motion-region.d.ts +7 -0
  129. package/dist/vue/use-motion-region.d.ts.map +1 -0
  130. package/dist/vue/use-motion-region.js +28 -0
  131. package/dist/vue/use-motion-region.js.map +1 -0
  132. package/dist/vue/use-motion-surface.d.ts.map +1 -1
  133. package/dist/vue/use-motion-surface.js +3 -2
  134. package/dist/vue/use-motion-surface.js.map +1 -1
  135. package/package.json +3 -3
  136. package/src/files.ts +3 -0
  137. package/src/native/combobox-editor.android.ts +11 -0
  138. package/src/native/combobox-editor.ts +6 -0
  139. package/src/native/file-access.ts +71 -0
  140. package/src/native/file-picker.android.ts +191 -0
  141. package/src/native/file-picker.ios.ts +230 -0
  142. package/src/native/file-picker.ts +8 -0
  143. package/src/native/motion-owner.ts +59 -0
  144. package/src/native/motion-region.ts +213 -0
  145. package/src/native/motion-surface.ts +26 -1
  146. package/src/native/scroll-motion.ts +5 -0
  147. package/src/vue/components/DateCalendar.ts +117 -0
  148. package/src/vue/components/MCombobox.ts +257 -0
  149. package/src/vue/components/MDatePicker.ts +33 -0
  150. package/src/vue/components/MFilePicker.ts +226 -0
  151. package/src/vue/components/layout.ts +6 -3
  152. package/src/vue/components/workflows.ts +38 -0
  153. package/src/vue/index.ts +29 -1
  154. package/src/vue/use-motion-region.ts +29 -0
  155. package/src/vue/use-motion-surface.ts +21 -14
@@ -0,0 +1,191 @@
1
+ import { Application, ImageSource } from '@nativescript/core'
2
+ import { SDK_VERSION } from '@nativescript/core/utils/constants'
3
+ import type { FilePickerAdapter, SelectedFile } from '@getcortexlabs/mavis-core'
4
+ import { ownNativeFile } from './file-access.js'
5
+
6
+ interface Uri {
7
+ toString(): string
8
+ getLastPathSegment(): string
9
+ }
10
+ interface Stream {
11
+ close(): void
12
+ }
13
+ interface Cursor {
14
+ moveToFirst(): boolean
15
+ getColumnIndex(name: string): number
16
+ isNull(index: number): boolean
17
+ getString(index: number): string
18
+ getLong(index: number): number
19
+ close(): void
20
+ }
21
+ interface Resolver {
22
+ getType(uri: Uri): string | null
23
+ query(uri: Uri, projection: null, selection: null, args: null, order: null): Cursor | null
24
+ openInputStream(uri: Uri): Stream | null
25
+ loadThumbnail(uri: Uri, size: unknown, cancellation: null): unknown
26
+ }
27
+ interface Intent {
28
+ addCategory(value: string): Intent
29
+ setType(type: string): Intent
30
+ putExtra(name: string, value: unknown): Intent
31
+ addFlags(flags: number): Intent
32
+ getData(): Uri | null
33
+ getClipData(): { getItemCount(): number; getItemAt(index: number): { getUri(): Uri } } | null
34
+ }
35
+ declare const android: {
36
+ content: {
37
+ Intent: {
38
+ new (action: string): Intent
39
+ ACTION_OPEN_DOCUMENT: string
40
+ CATEGORY_OPENABLE: string
41
+ EXTRA_ALLOW_MULTIPLE: string
42
+ EXTRA_MIME_TYPES: string
43
+ FLAG_GRANT_READ_URI_PERMISSION: number
44
+ }
45
+ }
46
+ util: { Size: new (width: number, height: number) => unknown }
47
+ webkit: {
48
+ MimeTypeMap: { getSingleton(): { getMimeTypeFromExtension(extension: string): string | null } }
49
+ }
50
+ }
51
+
52
+ // One active system picker per adapter; unique codes prevent cross-delivery between owners.
53
+ let requestSequence = 0x4d00
54
+ export function createNativeFilePicker(): FilePickerAdapter {
55
+ return {
56
+ pick(request, cancellation) {
57
+ if (cancellation.cancelled) return Promise.resolve(null)
58
+ const app = Application.android
59
+ const activity = app.foregroundActivity ?? app.startActivity
60
+ if (!activity)
61
+ return Promise.reject(new Error('File selection requires a foreground Activity'))
62
+ const resolver = activity.getContentResolver() as Resolver
63
+ const requestCode = requestSequence++
64
+ if (requestSequence > 0x5d00) requestSequence = 0x4d00
65
+ return new Promise((resolve, reject) => {
66
+ let done = false
67
+ let unsubscribe = () => {}
68
+ function finish(files: readonly SelectedFile[] | null, error?: unknown) {
69
+ if (done) {
70
+ for (const file of files ?? []) file.release()
71
+ return
72
+ }
73
+ done = true
74
+ unsubscribe()
75
+ app.off('activityResult', result)
76
+ if (error) reject(error)
77
+ else resolve(files)
78
+ }
79
+ function result(event: { requestCode: number; resultCode: number; intent?: Intent }) {
80
+ if (event.requestCode !== requestCode) return
81
+ if (event.resultCode !== -1 || !event.intent) {
82
+ finish(null)
83
+ return
84
+ }
85
+ const files: SelectedFile[] = []
86
+ try {
87
+ const uris: Uri[] = []
88
+ const clip = event.intent.getClipData()
89
+ if (clip)
90
+ for (let index = 0; index < clip.getItemCount(); index++)
91
+ uris.push(clip.getItemAt(index).getUri())
92
+ else {
93
+ const uri = event.intent.getData()
94
+ if (uri) uris.push(uri)
95
+ }
96
+ const unique = new Set<string>()
97
+ for (const uri of uris) {
98
+ if (unique.has(uri.toString())) continue
99
+ unique.add(uri.toString())
100
+ let name = uri.getLastPathSegment()?.split('/').at(-1) || 'Document'
101
+ let size: number | undefined
102
+ const cursor = resolver.query(uri, null, null, null, null)
103
+ try {
104
+ if (cursor?.moveToFirst()) {
105
+ const nameColumn = cursor.getColumnIndex('_display_name')
106
+ const sizeColumn = cursor.getColumnIndex('_size')
107
+ if (nameColumn >= 0 && !cursor.isNull(nameColumn))
108
+ name = cursor.getString(nameColumn)
109
+ if (sizeColumn >= 0 && !cursor.isNull(sizeColumn))
110
+ size = Number(cursor.getLong(sizeColumn))
111
+ }
112
+ } finally {
113
+ cursor?.close()
114
+ }
115
+ let released = false
116
+ const owner = ownNativeFile(
117
+ 'android',
118
+ uri,
119
+ () => {
120
+ const stream = resolver.openInputStream(uri)
121
+ if (!stream) throw new Error('The document provider did not open this file')
122
+ return { native: stream, close: () => stream.close() }
123
+ },
124
+ () => {
125
+ released = true
126
+ },
127
+ )
128
+ files.push({
129
+ name,
130
+ size,
131
+ type: resolver.getType(uri) ?? undefined,
132
+ ...owner,
133
+ async preview() {
134
+ if (released || SDK_VERSION < 29) return
135
+ // The provider creates a bounded thumbnail; failures retain the file.
136
+ const bitmap = resolver.loadThumbnail(uri, new android.util.Size(160, 160), null)
137
+ if (released || !bitmap) return
138
+ return new ImageSource(bitmap)
139
+ },
140
+ })
141
+ }
142
+ finish(files)
143
+ } catch (error) {
144
+ for (const file of files) file.release()
145
+ finish(null, error)
146
+ }
147
+ }
148
+ app.on('activityResult', result)
149
+ unsubscribe = cancellation.onCancel(() => {
150
+ // finishActivity only dismisses the child Activity launched by this request.
151
+ try {
152
+ activity.finishActivity(requestCode)
153
+ } finally {
154
+ finish(null)
155
+ }
156
+ })
157
+ try {
158
+ const intent = new android.content.Intent(android.content.Intent.ACTION_OPEN_DOCUMENT)
159
+ intent.addCategory(android.content.Intent.CATEGORY_OPENABLE)
160
+ const hints = request.accept.map((hint) =>
161
+ hint.startsWith('.')
162
+ ? android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(
163
+ hint.slice(1).toLowerCase(),
164
+ )
165
+ : hint,
166
+ )
167
+ const types = hints.filter((hint): hint is string => Boolean(hint))
168
+ // Unknown extensions widen the platform hint; core checks names after selection.
169
+ intent.setType(types.length === 1 && types.length === hints.length ? types[0]! : '*/*')
170
+ if (types.length > 1 && types.length === hints.length) {
171
+ // A JS array selects the Bundle overload in the NativeScript bridge.
172
+ const nativeTypes = (
173
+ Array as ArrayConstructor & {
174
+ create(type: string, length: number): string[]
175
+ }
176
+ ).create('java.lang.String', types.length)
177
+ types.forEach((type, index) => {
178
+ nativeTypes[index] = type
179
+ })
180
+ intent.putExtra(android.content.Intent.EXTRA_MIME_TYPES, nativeTypes)
181
+ }
182
+ intent.putExtra(android.content.Intent.EXTRA_ALLOW_MULTIPLE, request.multiple)
183
+ intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
184
+ activity.startActivityForResult(intent, requestCode)
185
+ } catch (error) {
186
+ finish(null, error)
187
+ }
188
+ })
189
+ },
190
+ }
191
+ }
@@ -0,0 +1,230 @@
1
+ import { Application, ImageSource } from '@nativescript/core'
2
+ import type { FilePickerAdapter, SelectedFile } from '@getcortexlabs/mavis-core'
3
+ import { ownNativeFile } from './file-access.js'
4
+
5
+ interface URLValue {
6
+ path: string
7
+ lastPathComponent: string
8
+ pathExtension: string
9
+ }
10
+ interface ContentType {
11
+ identifier: string
12
+ preferredMIMEType?: string
13
+ }
14
+ interface Controller {
15
+ presentedViewController?: Controller
16
+ presentViewControllerAnimatedCompletion(
17
+ controller: Controller,
18
+ animated: boolean,
19
+ completion: (() => void) | null,
20
+ ): void
21
+ dismissViewControllerAnimatedCompletion(animated: boolean, completion: (() => void) | null): void
22
+ }
23
+ interface Picker extends Controller {
24
+ delegate: object | null
25
+ allowsMultipleSelection: boolean
26
+ presentationController?: { delegate: object | null }
27
+ }
28
+ interface ThumbnailRequest {}
29
+ interface ThumbnailGenerator {
30
+ generateBestRepresentationForRequestCompletionHandler(
31
+ request: ThumbnailRequest,
32
+ completion: (representation: { UIImage: unknown } | null, error: unknown) => void,
33
+ ): void
34
+ cancelRequest(request: ThumbnailRequest): void
35
+ }
36
+ declare const NSObject: { extend(members: object, options: object): { new (): object } }
37
+ declare const UIDocumentPickerDelegate: unknown
38
+ declare const UIAdaptivePresentationControllerDelegate: unknown
39
+ declare const UIDocumentPickerViewController: {
40
+ alloc(): { initForOpeningContentTypesAsCopy(types: ContentType[], asCopy: boolean): Picker }
41
+ }
42
+ declare const UTType: {
43
+ typeWithMIMEType(type: string): ContentType | null
44
+ typeWithFilenameExtension(extension: string): ContentType | null
45
+ typeWithIdentifier(identifier: string): ContentType
46
+ }
47
+ declare const NSFileManager: {
48
+ defaultManager: {
49
+ attributesOfItemAtPathError(path: string): { objectForKey(name: string): unknown }
50
+ removeItemAtURLError(url: URLValue): boolean
51
+ }
52
+ }
53
+ declare const NSInputStream: { inputStreamWithURL(url: URLValue): { open(): void; close(): void } }
54
+ declare const QLThumbnailGenerationRequest: {
55
+ alloc(): {
56
+ initWithFileAtURLSizeScaleRepresentationTypes(
57
+ url: URLValue,
58
+ size: { width: number; height: number },
59
+ scale: number,
60
+ types: number,
61
+ ): ThumbnailRequest
62
+ }
63
+ }
64
+ declare const QLThumbnailGenerator: { sharedGenerator: ThumbnailGenerator }
65
+
66
+ const families: Readonly<Record<string, string>> = {
67
+ 'image/*': 'public.image',
68
+ 'video/*': 'public.movie',
69
+ 'audio/*': 'public.audio',
70
+ 'text/*': 'public.text',
71
+ }
72
+ interface PickerCallbacks {
73
+ cancel(): void
74
+ selected(urls: { count: number; objectAtIndex(index: number): URLValue }): void
75
+ }
76
+ const delegates = new WeakMap<object, PickerCallbacks>()
77
+ let Delegate: { new (): object } | undefined
78
+ function delegateClass() {
79
+ Delegate ??= NSObject.extend(
80
+ {
81
+ documentPickerWasCancelled(this: object) {
82
+ delegates.get(this)?.cancel()
83
+ },
84
+ presentationControllerDidDismiss(this: object) {
85
+ delegates.get(this)?.cancel()
86
+ },
87
+ documentPickerDidPickDocumentsAtURLs(
88
+ this: object,
89
+ _controller: Picker,
90
+ urls: { count: number; objectAtIndex(index: number): URLValue },
91
+ ) {
92
+ delegates.get(this)?.selected(urls)
93
+ },
94
+ },
95
+ { protocols: [UIDocumentPickerDelegate, UIAdaptivePresentationControllerDelegate] },
96
+ )
97
+ return Delegate
98
+ }
99
+
100
+ export function createNativeFilePicker(): FilePickerAdapter {
101
+ return {
102
+ pick(request, cancellation) {
103
+ if (cancellation.cancelled) return Promise.resolve(null)
104
+ let presenter = Application.ios.rootController as Controller | undefined
105
+ while (presenter?.presentedViewController) presenter = presenter.presentedViewController
106
+ if (!presenter)
107
+ return Promise.reject(new Error('File selection requires a mounted iOS view controller'))
108
+ return new Promise((resolve, reject) => {
109
+ let done = false
110
+ let unsubscribe = () => {}
111
+ let picker: Picker | undefined
112
+ // UIKit delegates are weak. Keep this reference until the picker has finished.
113
+ let delegate: object | undefined
114
+ const finish = (files: readonly SelectedFile[] | null, error?: unknown) => {
115
+ if (done) {
116
+ for (const file of files ?? []) file.release()
117
+ return
118
+ }
119
+ done = true
120
+ unsubscribe()
121
+ if (picker) {
122
+ picker.delegate = null
123
+ if (picker.presentationController) picker.presentationController.delegate = null
124
+ }
125
+ if (delegate) delegates.delete(delegate)
126
+ delegate = undefined
127
+ if (error) reject(error)
128
+ else resolve(files)
129
+ }
130
+ try {
131
+ const types = request.accept.map((hint) => {
132
+ if (hint.startsWith('.')) return UTType.typeWithFilenameExtension(hint.slice(1))
133
+ if (hint.endsWith('/*'))
134
+ return families[hint] ? UTType.typeWithIdentifier(families[hint]) : null
135
+ return UTType.typeWithMIMEType(hint)
136
+ })
137
+ const effective =
138
+ !types.length || types.some((type) => !type)
139
+ ? [UTType.typeWithIdentifier('public.data')]
140
+ : (types as ContentType[])
141
+ // Import a private temporary copy: no security-scoped bookmark or broad permission.
142
+ picker = UIDocumentPickerViewController.alloc().initForOpeningContentTypesAsCopy(
143
+ effective,
144
+ true,
145
+ )
146
+ picker.allowsMultipleSelection = request.multiple
147
+ const PickerDelegate = delegateClass()
148
+ delegate = new PickerDelegate()
149
+ delegates.set(delegate, {
150
+ cancel() {
151
+ finish(null)
152
+ },
153
+ selected(urls) {
154
+ const files: SelectedFile[] = []
155
+ try {
156
+ for (let index = 0; index < urls.count; index++) {
157
+ const url = urls.objectAtIndex(index)
158
+ const attributes = NSFileManager.defaultManager.attributesOfItemAtPathError(
159
+ url.path,
160
+ )
161
+ const size = attributes?.objectForKey('NSFileSize')
162
+ let thumbnail: ThumbnailRequest | undefined
163
+ let released = false
164
+ const owner = ownNativeFile(
165
+ 'ios',
166
+ url,
167
+ () => {
168
+ const stream = NSInputStream.inputStreamWithURL(url)
169
+ stream.open()
170
+ return { native: stream, close: () => stream.close() }
171
+ },
172
+ () => {
173
+ released = true
174
+ if (thumbnail) QLThumbnailGenerator.sharedGenerator.cancelRequest(thumbnail)
175
+ // asCopy=true guarantees that this is the imported sandbox copy.
176
+ NSFileManager.defaultManager.removeItemAtURLError(url)
177
+ },
178
+ )
179
+ files.push({
180
+ name: url.lastPathComponent,
181
+ size: size == null ? undefined : Number(size),
182
+ type:
183
+ UTType.typeWithFilenameExtension(url.pathExtension)?.preferredMIMEType ||
184
+ undefined,
185
+ ...owner,
186
+ preview() {
187
+ if (released) return Promise.resolve(undefined)
188
+ return new Promise((previewResolve, previewReject) => {
189
+ thumbnail =
190
+ QLThumbnailGenerationRequest.alloc().initWithFileAtURLSizeScaleRepresentationTypes(
191
+ url,
192
+ { width: 160, height: 160 },
193
+ 1,
194
+ 4,
195
+ )
196
+ QLThumbnailGenerator.sharedGenerator.generateBestRepresentationForRequestCompletionHandler(
197
+ thumbnail,
198
+ (representation, error) => {
199
+ thumbnail = undefined
200
+ if (released) previewResolve(undefined)
201
+ else if (representation)
202
+ previewResolve(new ImageSource(representation.UIImage))
203
+ else previewReject(error ?? new Error('Preview unavailable'))
204
+ },
205
+ )
206
+ })
207
+ },
208
+ })
209
+ }
210
+ finish(files)
211
+ } catch (error) {
212
+ for (const file of files) file.release()
213
+ finish(null, error)
214
+ }
215
+ },
216
+ })
217
+ picker.delegate = delegate
218
+ if (picker.presentationController) picker.presentationController.delegate = delegate
219
+ unsubscribe = cancellation.onCancel(() => {
220
+ picker?.dismissViewControllerAnimatedCompletion(true, null)
221
+ finish(null)
222
+ })
223
+ presenter!.presentViewControllerAnimatedCompletion(picker, true, null)
224
+ } catch (error) {
225
+ finish(null, error)
226
+ }
227
+ })
228
+ },
229
+ }
230
+ }
@@ -0,0 +1,8 @@
1
+ import type { FilePickerAdapter } from '@getcortexlabs/mavis-core'
2
+
3
+ /** NativeScript resolves this module to the .ios/.android implementation at bundle time. */
4
+ export function createNativeFilePicker(): FilePickerAdapter {
5
+ return {
6
+ pick: () => Promise.reject(new Error('File picking requires the iOS or Android runtime')),
7
+ }
8
+ }
@@ -0,0 +1,59 @@
1
+ import { motionScope } from '@rafaelaugusto/jolt'
2
+ import type { MotionSurfaceAdapter } from '@getcortexlabs/mavis-core/experimental'
3
+ import type { View } from '@nativescript/core'
4
+ import { registerNativeSurface } from './motion-region.js'
5
+
6
+ /** Native scopes also recreate subscriptions after application suspension. */
7
+ export function nativeMotionOwner(
8
+ adapter: MotionSurfaceAdapter,
9
+ target: View,
10
+ ): MotionSurfaceAdapter {
11
+ const result: MotionSurfaceAdapter = {
12
+ ...adapter,
13
+ use(setup) {
14
+ const parent = motionScope()
15
+ try {
16
+ parent.use(() => {
17
+ const child = motionScope()
18
+ const owned: MotionSurfaceAdapter = {
19
+ ...result,
20
+ animate: (...args) => child.add(result.animate(...args)),
21
+ set: (...args) => child.add(result.set(...args)),
22
+ motionValue:
23
+ result.motionValue && ((...args) => child.add(result.motionValue!(...args))),
24
+ springValue:
25
+ result.springValue && ((...args) => child.add(result.springValue!(...args))),
26
+ map: result.map && ((...args) => child.add(result.map!(...args))),
27
+ bind: result.bind && ((...args) => child.add(result.bind!(...args))),
28
+ drag: result.drag && ((...args) => child.add(result.drag!(...args))),
29
+ pointerFollow:
30
+ result.pointerFollow && ((...args) => child.add(result.pointerFollow!(...args))),
31
+ scrollLinked:
32
+ result.scrollLinked && ((...args) => child.add(result.scrollLinked!(...args))),
33
+ }
34
+ registerNativeSurface(owned, target)
35
+ let cleanup: () => void
36
+ try {
37
+ cleanup = setup(owned)
38
+ } catch (error) {
39
+ child.dispose()
40
+ throw error
41
+ }
42
+ return () => {
43
+ try {
44
+ cleanup()
45
+ } finally {
46
+ child.dispose()
47
+ }
48
+ }
49
+ })
50
+ } catch (error) {
51
+ parent.dispose()
52
+ throw error
53
+ }
54
+ return () => parent.dispose()
55
+ },
56
+ }
57
+ registerNativeSurface(result, target)
58
+ return result
59
+ }
@@ -0,0 +1,213 @@
1
+ import { Application, ScrollView, type View } from '@nativescript/core'
2
+ import { animate, reducedMotion, onReducedMotion } from '@rafaelaugusto/jolt'
3
+ import { sharedLayout } from '@rafaelaugusto/jolt/shared-layout'
4
+ import {
5
+ bindMotionRegion,
6
+ type MotionRegion,
7
+ type MotionRegionAdapter,
8
+ type MotionSurfaceAdapter,
9
+ } from '@getcortexlabs/mavis-core/experimental'
10
+ import { nativeScrollElement } from './scroll-motion.js'
11
+
12
+ const targets = new WeakMap<MotionSurfaceAdapter, View>()
13
+ const regions = new WeakMap<MotionRegionAdapter, View>()
14
+ export function assertNativeRegion(adapter: MotionRegionAdapter, target: View) {
15
+ const region = regions.get(adapter)
16
+ if (!region || region === target || !inside(target, region))
17
+ throw new TypeError('Use a separate enclosing native motion region.')
18
+ }
19
+ export function registerNativeSurface(adapter: MotionSurfaceAdapter, target: View) {
20
+ targets.set(adapter, target)
21
+ }
22
+ function resolve(adapter: MotionSurfaceAdapter) {
23
+ const target = targets.get(adapter)
24
+ if (!target?.isLoaded) throw new Error('Use a loaded native motion surface.')
25
+ return target
26
+ }
27
+ function inside(view: View, container: View) {
28
+ for (let current: View | undefined = view; current; current = current.parent as View | undefined)
29
+ if (current === container) return true
30
+ return false
31
+ }
32
+
33
+ /** Native observation is event-driven geometry; Jolt remains the only playback clock. */
34
+ function viewportEnter(
35
+ container: View,
36
+ surfaces: readonly MotionSurfaceAdapter[],
37
+ options: Parameters<MotionRegionAdapter['viewportEnter']>[1],
38
+ ) {
39
+ const targets = surfaces.map(resolve)
40
+ let root: View = options.root ? nativeScrollElement(options.root) : container
41
+ if (!options.root) {
42
+ for (
43
+ let parent = container.parent as View | undefined;
44
+ parent;
45
+ parent = parent.parent as View | undefined
46
+ )
47
+ if (parent instanceof ScrollView) {
48
+ root = parent
49
+ break
50
+ }
51
+ }
52
+ const states = targets.map((target, index) => ({
53
+ target,
54
+ index,
55
+ visible: false,
56
+ completed: false,
57
+ animation: undefined as ReturnType<typeof animate> | undefined,
58
+ }))
59
+ let disposed = false,
60
+ disabled = options.disabled ?? false,
61
+ observing = false
62
+ const stop = (state: (typeof states)[number]) => {
63
+ const handle = state.animation
64
+ state.animation = undefined
65
+ if (handle) {
66
+ handle.progress = 1
67
+ handle.stop()
68
+ }
69
+ }
70
+ const detach = () => {
71
+ if (!observing) return
72
+ observing = false
73
+ root.off('scroll', refresh)
74
+ root.off('layoutChanged', refresh)
75
+ for (const state of states) state.target.off('layoutChanged', refresh)
76
+ }
77
+ const refresh = () => {
78
+ if (disposed || disabled || Application.suspended || reducedMotion()) {
79
+ for (const state of states) {
80
+ stop(state)
81
+ state.visible = false
82
+ }
83
+ return
84
+ }
85
+ const size = root.getActualSize(),
86
+ margin = options.margin ?? 0
87
+ for (const state of states) {
88
+ if (!state.target.isLoaded || (options.once !== false && state.completed)) continue
89
+ const point = state.target.getLocationRelativeTo(root),
90
+ bounds = state.target.getActualSize()
91
+ const area = bounds.width * bounds.height
92
+ const overlap = point
93
+ ? Math.max(
94
+ 0,
95
+ Math.min(point.x + bounds.width, size.width + margin) - Math.max(point.x, -margin),
96
+ ) *
97
+ Math.max(
98
+ 0,
99
+ Math.min(point.y + bounds.height, size.height + margin) - Math.max(point.y, -margin),
100
+ )
101
+ : 0
102
+ const visible = area > 0 && overlap > 0 && overlap / area >= (options.threshold ?? 0)
103
+ if (!visible) stop(state)
104
+ if (visible && !state.visible && !state.animation) {
105
+ const animation = animate(state.target, options.frames ?? { opacity: [0, 1], y: [20, 0] }, {
106
+ duration: 450,
107
+ ...options.transition,
108
+ delay: (options.transition?.delay ?? 0) + state.index * (options.stagger ?? 70),
109
+ })
110
+ state.animation = animation
111
+ void animation.finished.then((completed) => {
112
+ if (disposed || state.animation !== animation) return
113
+ state.animation = undefined
114
+ if (completed) state.completed = true
115
+ if (
116
+ options.once !== false &&
117
+ states.every((entry) => entry.completed || !entry.target.isLoaded)
118
+ )
119
+ detach()
120
+ })
121
+ }
122
+ state.visible = visible
123
+ }
124
+ }
125
+ const attach = () => {
126
+ if (
127
+ observing ||
128
+ disposed ||
129
+ disabled ||
130
+ (options.once !== false && states.every((state) => state.completed))
131
+ )
132
+ return
133
+ observing = true
134
+ root.on('scroll', refresh)
135
+ root.on('layoutChanged', refresh)
136
+ for (const state of states) state.target.on('layoutChanged', refresh)
137
+ }
138
+ const preference = onReducedMotion(() => {
139
+ attach()
140
+ refresh()
141
+ })
142
+ Application.on(Application.suspendEvent, refresh)
143
+ Application.on(Application.resumeEvent, refresh)
144
+ attach()
145
+ refresh()
146
+ return {
147
+ execution: 'native-layout' as const,
148
+ get pending() {
149
+ return states.filter((state) => state.animation).length
150
+ },
151
+ get completed() {
152
+ return states.filter((state) => state.completed).length
153
+ },
154
+ update(state: { disabled: boolean }) {
155
+ disabled = state.disabled
156
+ if (disabled) detach()
157
+ else attach()
158
+ refresh()
159
+ },
160
+ refresh,
161
+ dispose() {
162
+ if (disposed) return
163
+ disposed = true
164
+ detach()
165
+ preference()
166
+ Application.off(Application.suspendEvent, refresh)
167
+ Application.off(Application.resumeEvent, refresh)
168
+ for (const state of states) stop(state)
169
+ },
170
+ }
171
+ }
172
+
173
+ export function attachNativeMotionRegion(reference: MotionRegion, target: View) {
174
+ const resolveChild = (adapter: MotionSurfaceAdapter) => {
175
+ const child = resolve(adapter)
176
+ if (!inside(child, target) || child === target)
177
+ throw new Error('Declare each surface inside this motion region.')
178
+ return child
179
+ }
180
+ const adapter: MotionRegionAdapter = {
181
+ kind: 'native',
182
+ viewportEnter(surfaces, options) {
183
+ surfaces.forEach(resolveChild)
184
+ return viewportEnter(target, surfaces, options)
185
+ },
186
+ sharedLayout(options) {
187
+ const control = sharedLayout(target, options)
188
+ return {
189
+ get active() {
190
+ return control.active
191
+ },
192
+ attach: (surface, identity) => control.attach(resolveChild(surface), identity),
193
+ transition: (change, options) => control.transition(change, options),
194
+ cancel: () => control.cancel(),
195
+ dispose: () => control.dispose(),
196
+ }
197
+ },
198
+ }
199
+ regions.set(adapter, target)
200
+ try {
201
+ const release = bindMotionRegion(reference, adapter)
202
+ return () => {
203
+ try {
204
+ release()
205
+ } finally {
206
+ regions.delete(adapter)
207
+ }
208
+ }
209
+ } catch (error) {
210
+ regions.delete(adapter)
211
+ throw error
212
+ }
213
+ }