@getcortexlabs/create-mavis 0.1.0-rc.14 → 0.1.0-rc.15

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 (44) hide show
  1. package/README.md +25 -9
  2. package/package.json +2 -2
  3. package/src/adapters.mjs +49 -20
  4. package/src/add-desktop.mjs +308 -0
  5. package/src/add-macos.mjs +3 -3
  6. package/src/capabilities.mjs +288 -42
  7. package/src/cli.mjs +98 -8
  8. package/src/desktop.mjs +155 -0
  9. package/src/doctor.mjs +86 -5
  10. package/src/native-framework.mjs +144 -0
  11. package/src/portability.mjs +22 -7
  12. package/src/scaffold.mjs +38 -9
  13. package/src/ui.mjs +51 -3
  14. package/templates/desktop/common/LICENSE +21 -0
  15. package/templates/desktop/common/build.mjs +58 -0
  16. package/templates/desktop/common/src/components.ts +60 -0
  17. package/templates/desktop/common/src/contract.mjs +35 -0
  18. package/templates/desktop/common/src/renderer.ts +76 -0
  19. package/templates/desktop/common/src/storage.ts +29 -0
  20. package/templates/desktop/directory/main.ts +35 -0
  21. package/templates/desktop/macos/launch.mjs +6 -0
  22. package/templates/desktop/macos/package.mjs +114 -0
  23. package/templates/desktop/macos/src/host.mjs +468 -0
  24. package/templates/desktop/macos/src/motion.mjs +51 -0
  25. package/templates/desktop/welcome/Welcome.vue +56 -0
  26. package/templates/desktop/welcome/main.ts +24 -0
  27. package/templates/desktop/windows/NativeScript-LICENSE +19 -0
  28. package/templates/desktop/windows/launch.mjs +16 -0
  29. package/templates/desktop/windows/package.mjs +54 -0
  30. package/templates/desktop/windows/prepare-host.mjs +73 -0
  31. package/templates/desktop/windows/src/host.mjs +277 -0
  32. package/templates/native-framework/angular/screen.ts +84 -0
  33. package/templates/native-framework/common/nativescript.config.ts +6 -0
  34. package/templates/native-framework/common/src/app.ts +5 -0
  35. package/templates/native-framework/common/src/model.ts +40 -0
  36. package/templates/native-framework/common/src/references.d.ts +8 -0
  37. package/templates/native-framework/common/tsconfig.json +16 -0
  38. package/templates/native-framework/common/webpack.config.js +67 -0
  39. package/templates/native-framework/plain/screen.ts +102 -0
  40. package/templates/native-framework/react/screen.ts +83 -0
  41. package/templates/native-framework/solid/screen.ts +111 -0
  42. package/templates/native-framework/svelte/Welcome.svelte +26 -0
  43. package/templates/native-framework/svelte/screen.ts +14 -0
  44. package/templates/native-framework/svelte-tooling/svelte.config.js +20 -0
@@ -0,0 +1,73 @@
1
+ import { execFileSync } from 'node:child_process'
2
+ import {
3
+ copyFileSync,
4
+ existsSync,
5
+ mkdirSync,
6
+ readFileSync,
7
+ readdirSync,
8
+ writeFileSync,
9
+ } from 'node:fs'
10
+ import { dirname, join, resolve } from 'node:path'
11
+ import { fileURLToPath } from 'node:url'
12
+
13
+ export function findManifestTool() {
14
+ const kits = join(
15
+ process.env['ProgramFiles(x86)'] ?? 'C:/Program Files (x86)',
16
+ 'Windows Kits/10/bin',
17
+ )
18
+ let manifestTool = process.env.MAVIS_MT
19
+ if (!manifestTool && existsSync(kits)) {
20
+ for (const version of readdirSync(kits).sort((a, b) =>
21
+ b.localeCompare(a, undefined, { numeric: true }),
22
+ )) {
23
+ for (const arch of [process.arch === 'arm64' ? 'arm64' : 'x64', 'x64']) {
24
+ const candidate = join(kits, version, arch, 'mt.exe')
25
+ if (existsSync(candidate)) {
26
+ manifestTool = candidate
27
+ break
28
+ }
29
+ }
30
+ if (manifestTool) break
31
+ }
32
+ }
33
+ return manifestTool && existsSync(manifestTool) ? manifestTool : undefined
34
+ }
35
+
36
+ /** XAML Islands require a manifest on the executable hosting Node-API. */
37
+ export function prepareWindowsHost(destination) {
38
+ if (process.platform !== 'win32') throw new Error('Build and run the Windows app on Windows.')
39
+ const executable = resolve(destination)
40
+ const stamp = `${executable}.json`
41
+ const identity = JSON.stringify({ node: process.version, arch: process.arch, manifest: 1 })
42
+ if (existsSync(executable) && existsSync(stamp) && readFileSync(stamp, 'utf8') === identity)
43
+ return executable
44
+ const manifestTool = findManifestTool()
45
+ if (!manifestTool)
46
+ throw new Error('Install the Windows 10/11 SDK (Manifest Tool), or set MAVIS_MT to mt.exe.')
47
+ mkdirSync(dirname(executable), { recursive: true })
48
+ copyFileSync(process.execPath, executable)
49
+ const manifest = `${executable}.manifest`
50
+ execFileSync(manifestTool, ['-nologo', `-inputresource:${executable};#1`, `-out:${manifest}`])
51
+ const bytes = readFileSync(manifest)
52
+ let xml = bytes
53
+ .toString(bytes[0] === 255 && bytes[1] === 254 ? 'utf16le' : 'utf8')
54
+ .replace(/^\uFEFF/, '')
55
+ const maximum = '<maxversiontested Id="10.0.18362.0"/>'
56
+ if (/<maxversiontested\b[^>]*\/>/.test(xml))
57
+ xml = xml.replace(/<maxversiontested\b[^>]*\/>/, maximum)
58
+ else if (/<compatibility\b/.test(xml))
59
+ xml = xml.replace(/(<compatibility\b[\s\S]*?)(<\/application>)/, `$1${maximum}$2`)
60
+ else
61
+ xml = xml.replace(
62
+ '</assembly>',
63
+ `<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"><application>${maximum}</application></compatibility></assembly>`,
64
+ )
65
+ if (!xml.includes(maximum)) throw new Error('Cannot add XAML compatibility to the Node manifest.')
66
+ writeFileSync(manifest, xml)
67
+ execFileSync(manifestTool, ['-nologo', '-manifest', manifest, `-outputresource:${executable};#1`])
68
+ writeFileSync(stamp, identity)
69
+ return executable
70
+ }
71
+
72
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url))
73
+ console.log(prepareWindowsHost(process.argv[2] ?? '.mavis/runtime/node.exe'))
@@ -0,0 +1,277 @@
1
+ import { nativeProps } from './contract.mjs'
2
+ import { createRequire } from 'node:module'
3
+ import { mavis, midnight } from '@getcortexlabs/mavis-core'
4
+ const { Windows, XamlWindow, native } = createRequire(import.meta.url)('@nativescript/windows-napi')
5
+ const Xaml = Windows.UI.Xaml
6
+ const C = Xaml.Controls
7
+ const emit = (listener, ...args) => {
8
+ for (const fn of Array.isArray(listener) ? listener : [listener])
9
+ if (typeof fn === 'function') fn(...args)
10
+ }
11
+ const text = (node) => node.text || node.children.map(text).join('')
12
+ const brush = (hex) => {
13
+ const value = Number.parseInt(hex.replace('#', '').slice(0, 6), 16)
14
+ return new Xaml.Media.SolidColorBrush(
15
+ Windows.UI.ColorHelper.FromArgb(255, value >> 16, (value >> 8) & 255, value & 255),
16
+ )
17
+ }
18
+ const supported = {
19
+ root: [],
20
+ screen: ['scroll'],
21
+ column: ['padding', 'gap'],
22
+ row: ['padding', 'gap'],
23
+ grid: ['minColumnWidth', 'gap'],
24
+ text: ['text', 'as', 'variant', 'tone'],
25
+ input: [
26
+ 'modelValue',
27
+ 'label',
28
+ 'placeholder',
29
+ 'readonly',
30
+ 'disabled',
31
+ 'onUpdate:modelValue',
32
+ 'onSubmit',
33
+ 'onEscape',
34
+ ],
35
+ reveal: ['show'],
36
+ button: ['href', 'text', 'loading', 'loadingText', 'disabled', 'onClick'],
37
+ link: ['href', 'text', 'disabled', 'onClick'],
38
+ card: ['href', 'padding', 'gap', 'onClick', 'disabled'],
39
+ }
40
+
41
+ /** Explicit XAML Islands subset. NativeScript Core mobile widgets are not used on Windows. */
42
+ export function createHost({ title = '__MAVIS_NAME__', onClose = () => {} } = {}) {
43
+ const window = new XamlWindow({ title, theme: 'light' })
44
+ const root = {
45
+ kind: 'root',
46
+ text: '',
47
+ props: {},
48
+ children: [],
49
+ parent: null,
50
+ native: new C.Canvas(),
51
+ }
52
+ root.content = root.native
53
+ window.content = root.native
54
+ window.place({ width: 780, height: 620 })
55
+ const nodes = new Set([root])
56
+ let closed = false,
57
+ scheduled = false,
58
+ timer,
59
+ theme = mavis
60
+ const tokens = (key) => (typeof key === 'number' ? key : (theme.space[key] ?? 0))
61
+ function create(kind) {
62
+ if (!(kind in supported) && !['#text', '#comment'].includes(kind))
63
+ throw new Error(`Windows desktop host does not implement ${kind}`)
64
+ const node = { kind, text: '', props: {}, children: [], parent: null }
65
+ nodes.add(node)
66
+ if (kind[0] === '#') return node
67
+ if (kind === 'text') {
68
+ node.native = new C.TextBlock()
69
+ node.native.TextWrapping = Xaml.TextWrapping.Wrap
70
+ } else if (kind === 'input') {
71
+ node.native = node.editor = new C.TextBox()
72
+ node.native.TextChanged = () => {
73
+ if (!node.updating && !closed) emit(node.props['onUpdate:modelValue'], node.editor.Text)
74
+ }
75
+ node.native.KeyDown = (_sender, event) => {
76
+ if (event.Key === Windows.System.VirtualKey.Enter) emit(node.props.onSubmit)
77
+ if (event.Key === Windows.System.VirtualKey.Escape) emit(node.props.onEscape)
78
+ }
79
+ } else if (['button', 'link', 'card'].includes(kind)) {
80
+ node.native = new C.Button()
81
+ node.native.HorizontalContentAlignment = Xaml.HorizontalAlignment.Stretch
82
+ node.native.Click = () => {
83
+ if (!closed && !node.props.disabled && !node.props.loading) emit(node.props.onClick)
84
+ }
85
+ if (kind === 'card') node.native.Content = node.content = new C.Canvas()
86
+ } else if (kind === 'screen') {
87
+ node.native = new C.ScrollViewer()
88
+ node.native.VerticalScrollBarVisibility = C.ScrollBarVisibility.Auto
89
+ node.native.HorizontalScrollBarVisibility = C.ScrollBarVisibility.Disabled
90
+ node.native.Content = node.content = new C.Canvas()
91
+ } else node.native = new C.Canvas()
92
+ node.content ??= node.native
93
+ node.dispose = () => {
94
+ if (node.editor) {
95
+ node.editor.TextChanged = null
96
+ node.editor.KeyDown = null
97
+ }
98
+ if (['button', 'link', 'card'].includes(kind)) node.native.Click = null
99
+ nodes.delete(node)
100
+ }
101
+ return node
102
+ }
103
+ function update(node) {
104
+ if (!node.native || closed) return
105
+ const p = node.props,
106
+ view = node.native
107
+ for (const key of Object.keys(p))
108
+ if (!nativeProps(node.kind, 'windows').includes(key))
109
+ throw new Error(`Windows desktop host does not implement ${node.kind}.${key}`)
110
+ Xaml.Automation.AutomationProperties.SetAutomationId(view, String(p.id ?? ''))
111
+ if (p.accessibilityLabel)
112
+ Xaml.Automation.AutomationProperties.SetName(view, String(p.accessibilityLabel))
113
+ if (node.kind === 'text') {
114
+ view.Text = String(p.text ?? text(node))
115
+ view.FontSize = { title: 28, heading: 20, caption: 12 }[p.variant] ?? 14
116
+ view.Foreground = brush(theme.colors[p.tone] ?? theme.colors.text)
117
+ }
118
+ if (node.kind === 'input') {
119
+ node.updating = true
120
+ try {
121
+ view.Header = String(p.label ?? '')
122
+ if (view.Text !== String(p.modelValue ?? '')) view.Text = String(p.modelValue ?? '')
123
+ view.PlaceholderText = String(p.placeholder ?? '')
124
+ view.IsReadOnly = Boolean(p.readonly)
125
+ view.IsEnabled = !p.disabled
126
+ } finally {
127
+ node.updating = false
128
+ }
129
+ }
130
+ if (['button', 'link', 'card'].includes(node.kind)) {
131
+ if (node.kind !== 'card')
132
+ view.Content = String(p.loading ? (p.loadingText ?? 'Loading…') : (p.text ?? text(node)))
133
+ view.IsEnabled = !p.disabled && !p.loading
134
+ }
135
+ if (node.kind === 'reveal') {
136
+ node.shown = p.show !== false
137
+ view.Visibility = node.shown ? Xaml.Visibility.Visible : Xaml.Visibility.Collapsed
138
+ }
139
+ if (node.kind === 'screen')
140
+ view.VerticalScrollMode = p.scroll === false ? C.ScrollMode.Disabled : C.ScrollMode.Enabled
141
+ }
142
+ function detachNative(node) {
143
+ const parent = node.attached
144
+ if (parent) {
145
+ const children = parent.content.Children
146
+ // Rebuild from known native children: no reliance on WinRT object wrapper identity.
147
+ children.Clear()
148
+ for (const sibling of parent.children)
149
+ if (sibling !== node && sibling.native) children.Append(sibling.native)
150
+ node.attached = null
151
+ }
152
+ }
153
+ function attach(node, parent) {
154
+ if (!node.native) return
155
+ if (node.attached && node.attached !== parent) detachNative(node)
156
+ parent.content.Children.Clear()
157
+ for (const sibling of parent.children)
158
+ if (sibling.native) parent.content.Children.Append(sibling.native)
159
+ node.attached = parent
160
+ }
161
+ function detach(node) {
162
+ detachNative(node)
163
+ nodes.delete(node)
164
+ }
165
+ function layoutNode(node, x, y, width, viewport) {
166
+ if (!node.native) return 0
167
+ const p = node.props,
168
+ view = node.native
169
+ let height = 0
170
+ width = Math.max(0, width)
171
+ if (node.kind === 'reveal' && !node.shown) height = 0
172
+ else if (node.kind === 'text') {
173
+ view.Width = width
174
+ view.Measure({ Width: width, Height: 100000 })
175
+ height = Math.max(20, view.DesiredSize.Height)
176
+ } else if (node.kind === 'input') height = p.label ? 70 : 40
177
+ else if (['button', 'link'].includes(node.kind)) height = 38
178
+ else {
179
+ const pad = tokens(p.padding ?? (node.kind === 'card' ? 'md' : 'none')),
180
+ gap = tokens(p.gap)
181
+ const children = node.children.filter(
182
+ (child) => child.native && !(child.kind === 'reveal' && !child.shown),
183
+ )
184
+ const columns =
185
+ node.kind === 'grid'
186
+ ? Math.max(
187
+ 1,
188
+ Math.floor((width - pad * 2 + gap) / (Number(p.minColumnWidth ?? 240) + gap)),
189
+ )
190
+ : node.kind === 'row'
191
+ ? Math.max(1, children.length)
192
+ : 1
193
+ const cellWidth = (width - pad * 2 - gap * (columns - 1)) / columns
194
+ let top = pad
195
+ for (let i = 0; i < children.length; i += columns) {
196
+ let rowHeight = 0
197
+ for (let col = 0; col < columns && i + col < children.length; col++)
198
+ rowHeight = Math.max(
199
+ rowHeight,
200
+ layoutNode(children[i + col], pad + col * (cellWidth + gap), top, cellWidth, viewport),
201
+ )
202
+ top += rowHeight + gap
203
+ }
204
+ height = top + pad - (children.length ? gap : 0)
205
+ if (node.kind === 'screen') {
206
+ node.content.Width = width
207
+ node.content.Height = Math.max(viewport, height)
208
+ height = viewport
209
+ }
210
+ if (node.kind === 'card') {
211
+ node.content.Width = Math.max(0, width - 16)
212
+ node.content.Height = height
213
+ height += 16
214
+ }
215
+ }
216
+ if (node.kind !== 'root') {
217
+ C.Canvas.SetLeft(view, x)
218
+ C.Canvas.SetTop(view, y)
219
+ view.Width = width
220
+ view.Height = Math.max(0, height)
221
+ }
222
+ return height
223
+ }
224
+ function layout() {
225
+ if (closed || scheduled) return
226
+ scheduled = true
227
+ queueMicrotask(() => {
228
+ scheduled = false
229
+ if (!closed)
230
+ layoutNode(root, 0, 0, root.native.ActualWidth || 780, root.native.ActualHeight || 620)
231
+ })
232
+ }
233
+ root.native.SizeChanged = layout
234
+ function setTheme(dark) {
235
+ theme = dark ? midnight : mavis
236
+ root.native.RequestedTheme = dark ? Xaml.ElementTheme.Dark : Xaml.ElementTheme.Light
237
+ root.native.Background = brush(theme.colors.background)
238
+ for (const node of nodes) update(node)
239
+ layout()
240
+ }
241
+ function stop() {
242
+ if (closed) return
243
+ closed = true
244
+ clearInterval(timer)
245
+ root.native.SizeChanged = null
246
+ for (const node of nodes) node.dispose?.()
247
+ nodes.clear()
248
+ }
249
+ window.on('closed', () => {
250
+ onClose()
251
+ stop()
252
+ })
253
+ function show() {
254
+ window.show()
255
+ timer = setInterval(() => native.pumpMessages(), 8)
256
+ layout()
257
+ }
258
+ return {
259
+ root,
260
+ window,
261
+ nodes,
262
+ create,
263
+ update,
264
+ attach,
265
+ detach,
266
+ layout,
267
+ show,
268
+ stop,
269
+ setTheme,
270
+ close() {
271
+ window.close()
272
+ },
273
+ get closed() {
274
+ return closed
275
+ },
276
+ }
277
+ }
@@ -0,0 +1,84 @@
1
+ import '@angular/compiler'
2
+ import { Component, signal, ViewChild } from '@angular/core'
3
+ import {
4
+ NativeScriptCommonModule,
5
+ bootstrapApplication,
6
+ runNativeScriptAngularApp,
7
+ } from '@nativescript/angular'
8
+ import { MAVIS_COMPONENTS, MInput } from '@getcortexlabs/mavis-angular-nativescript'
9
+ import { bridge, initial, theme, isolatedTheme, surface } from './model'
10
+
11
+ @Component({
12
+ selector: 'adapter-catalog',
13
+ standalone: true,
14
+ imports: [NativeScriptCommonModule, ...MAVIS_COMPONENTS],
15
+ template: `<Page actionBarHidden="true">
16
+ <MColumn
17
+ id="catalog"
18
+ gap="md"
19
+ padding="lg"
20
+ height="100%"
21
+ [backgroundColor]="background()"
22
+ [theme]="theme"
23
+ >
24
+ <MText id="heading" text="__MAVIS_NAME__" variant="title" [theme]="theme" />
25
+ <MRow id="row" gap="sm"
26
+ ><MButton
27
+ id="action"
28
+ [theme]="theme"
29
+ [text]="'Count ' + state().count"
30
+ [disabled]="state().disabled"
31
+ [loading]="state().loading"
32
+ (click)="update({ count: state().count + 1 })"
33
+ /></MRow>
34
+ <MInput
35
+ id="editor"
36
+ label="Name"
37
+ [theme]="theme"
38
+ [modelValue]="state().value"
39
+ [disabled]="state().disabled"
40
+ (modelValueChange)="update({ value: $event })"
41
+ />
42
+ @if (state().visible) {
43
+ <MBox
44
+ id="motion"
45
+ [width]="100"
46
+ [height]="30"
47
+ backgroundColor="#4f46e5"
48
+ [motionSurface]="surface"
49
+ />
50
+ }
51
+ <MBox padding="md" radius="md" [backgroundColor]="isolated.theme.colors.background"
52
+ ><MText id="isolated" text="Independent theme" [theme]="isolated"
53
+ /></MBox>
54
+ </MColumn>
55
+ </Page>`,
56
+ })
57
+ class Catalog {
58
+ @ViewChild(MInput) input?: MInput
59
+ background = signal(theme.theme.colors.background)
60
+ private offTheme = theme.subscribe(() => this.background.set(theme.theme.colors.background))
61
+ ngOnDestroy() {
62
+ this.offTheme()
63
+ }
64
+ state = signal({ ...initial })
65
+ theme = theme
66
+ isolated = isolatedTheme
67
+ surface = surface
68
+ update = (patch: Partial<typeof initial>) =>
69
+ this.state.update((value) => ({ ...value, ...patch }))
70
+ constructor() {
71
+ bridge.read = this.state
72
+ bridge.update = this.update
73
+ bridge.focus = () => this.input?.focusInput()
74
+ }
75
+ }
76
+ export function start() {
77
+ runNativeScriptAngularApp({
78
+ appModuleBootstrap: async () => {
79
+ const app = await bootstrapApplication(Catalog)
80
+ bridge.dispose = () => app.destroy()
81
+ return app
82
+ },
83
+ })
84
+ }
@@ -0,0 +1,6 @@
1
+ import type { NativeScriptConfig } from '@nativescript/core'
2
+ export default {
3
+ id: 'org.nativescript.__MAVIS_APPID__',
4
+ appPath: 'src',
5
+ appResourcesPath: 'App_Resources',
6
+ } satisfies NativeScriptConfig
@@ -0,0 +1,5 @@
1
+ import { Application } from '@nativescript/core'
2
+ import { start } from './screen'
3
+ import { bridge } from './model'
4
+ Application.on(Application.exitEvent, () => bridge.dispose?.())
5
+ start()
@@ -0,0 +1,40 @@
1
+ import { createMavisTheme } from '@getcortexlabs/mavis/native'
2
+ import { mavis, midnight } from '@getcortexlabs/mavis-core'
3
+ import { createMotionSurface } from '@getcortexlabs/mavis-core/experimental'
4
+
5
+ export type State = {
6
+ value: string
7
+ count: number
8
+ disabled: boolean
9
+ loading: boolean
10
+ visible: boolean
11
+ }
12
+ export const initial: State = {
13
+ value: 'Ada',
14
+ count: 0,
15
+ disabled: false,
16
+ loading: false,
17
+ visible: true,
18
+ }
19
+ export const theme = createMavisTheme(__MAVIS_THEME__)
20
+ export const isolatedTheme = createMavisTheme(midnight)
21
+ export const surface = createMotionSurface()
22
+ export const lifecycle = { active: 0, attached: 0, animations: 0 }
23
+ surface.use((motion) => {
24
+ lifecycle.active++
25
+ lifecycle.attached++
26
+ const animation = motion.animate({ opacity: [0.7, 1] }, { duration: 60 })
27
+ void animation.finished.then(() => {
28
+ lifecycle.animations++
29
+ })
30
+ return () => {
31
+ lifecycle.active--
32
+ animation.stop()
33
+ }
34
+ })
35
+ export const bridge: {
36
+ read?: () => State
37
+ update?: (patch: Partial<State>) => void
38
+ dispose?: () => void
39
+ focus?: () => void
40
+ } = {}
@@ -0,0 +1,8 @@
1
+ /// <reference types="@nativescript/types" />
2
+ /// <reference types="@nativescript/types-ios" />
3
+ /// <reference types="@nativescript/types-android" />
4
+
5
+ declare module '*.svelte' {
6
+ import type { SvelteComponentTyped } from 'svelte'
7
+ export default class Component extends SvelteComponentTyped {}
8
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "strict": true,
7
+ "ignoreDeprecations": "6.0",
8
+ "skipLibCheck": true,
9
+ "experimentalDecorators": true,
10
+ "useDefineForClassFields": false,
11
+ "esModuleInterop": true,
12
+ "lib": ["ES2023", "DOM"],
13
+ "types": ["@nativescript/types"]
14
+ },
15
+ "include": ["src"]
16
+ }
@@ -0,0 +1,67 @@
1
+ const { resolve, dirname } = require('node:path')
2
+ const webpack = require('@nativescript/webpack')
3
+ module.exports = (env) => {
4
+ const framework = '__MAVIS_FRAMEWORK__'
5
+ if (!['plain', 'react', 'angular', 'svelte', 'solid'].includes(framework))
6
+ throw new Error('Unknown framework')
7
+ webpack.init(env)
8
+ webpack.useConfig('typescript')
9
+ webpack.Utils.addCopyRule({
10
+ from: 'lucide.ttf',
11
+ to: 'fonts',
12
+ context: resolve(__dirname, 'node_modules/@getcortexlabs/mavis/fonts'),
13
+ })
14
+ webpack.chainWebpack((config) => {
15
+ config.resolve.alias.set(
16
+ '@nativescript/core',
17
+ resolve(__dirname, 'node_modules/@nativescript/core'),
18
+ )
19
+ config.resolve.alias.set('@rafaelaugusto/jolt$', require.resolve('@rafaelaugusto/jolt'))
20
+ config.resolve.merge({ conditionNames: ['svelte', 'browser', '...'] })
21
+ config.plugin('NoVueInDirectAdapters').use(
22
+ class {
23
+ apply(compiler) {
24
+ compiler.hooks.afterCompile.tap('NoVueInDirectAdapters', (compilation) => {
25
+ for (const module of compilation.modules) {
26
+ const file = module.resource?.replaceAll('\\', '/') ?? ''
27
+ if (
28
+ /\/node_modules\/(vue|@vue|nativescript-vue)\//.test(file) ||
29
+ /\/mavis\/dist\/vue\//.test(file)
30
+ )
31
+ throw new Error(`Direct ${framework} adapter imported Vue: ${file}`)
32
+ }
33
+ })
34
+ }
35
+ },
36
+ )
37
+ config.plugin('DefinePlugin').tap((args) => {
38
+ Object.assign(args[0], {
39
+ __DEV__: 'true',
40
+ __TEST__: 'false',
41
+ __FRAMEWORK__: JSON.stringify(framework),
42
+ __ADAPTER_CHECK__: 'false',
43
+ })
44
+ return args
45
+ })
46
+ // NativeScript's resolver otherwise chooses Solid's nonreactive Node/server condition.
47
+ if (framework === 'solid')
48
+ for (const name of ['solid-js', '@solidjs/signals', '@solidjs/universal']) {
49
+ const manifest = resolve(__dirname, 'node_modules', name, 'package.json')
50
+ const pkg = require(manifest)
51
+ let entry = pkg.exports['.']
52
+ while (typeof entry !== 'string') entry = entry.browser ?? entry.default
53
+ config.resolve.alias.set(`${name}$`, resolve(dirname(manifest), entry))
54
+ }
55
+ if (framework === 'svelte')
56
+ config.module
57
+ .rule('svelte')
58
+ .test(/\.svelte$/)
59
+ .use('svelte-loader')
60
+ .loader('svelte-loader')
61
+ .options({
62
+ compilerOptions: { dev: false, namespace: 'foreign' },
63
+ preprocess: require('./svelte.config.js').preprocess,
64
+ })
65
+ })
66
+ return webpack.resolveConfig()
67
+ }
@@ -0,0 +1,102 @@
1
+ import { Application, Page, GridLayout, type LayoutBase } from '@nativescript/core'
2
+ import { createNativeComponent } from '@getcortexlabs/mavis/native'
3
+ import { bridge, initial, theme, isolatedTheme, surface, type State } from './model'
4
+
5
+ export function start() {
6
+ Application.run({
7
+ create: () => {
8
+ let state: State = { ...initial }
9
+ const all: Array<{ dispose(): void }> = []
10
+ const create: typeof createNativeComponent = (name, props, options = {}) => {
11
+ const control = createNativeComponent(name, props, { theme, ...options })
12
+ all.push(control)
13
+ return control
14
+ }
15
+ const root = create('MColumn', {
16
+ id: 'catalog',
17
+ gap: 'md',
18
+ padding: 'lg',
19
+ height: '100%',
20
+ backgroundColor: theme.theme.colors.background,
21
+ })
22
+ const heading = create('MText', {
23
+ id: 'heading',
24
+ text: '__MAVIS_NAME__',
25
+ variant: 'title',
26
+ })
27
+ const row = create('MRow', { id: 'row', gap: 'sm' })
28
+ const button = create(
29
+ 'MButton',
30
+ { id: 'action', text: 'Count 0' },
31
+ { events: { activate: () => update({ count: state.count + 1 }) } },
32
+ )
33
+ const input = create(
34
+ 'MInput',
35
+ { id: 'editor', label: 'Name', modelValue: state.value },
36
+ { events: { valueChange: (value) => update({ value }) } },
37
+ )
38
+ const box = create('MBox', {
39
+ id: 'motion',
40
+ width: 100,
41
+ height: 30,
42
+ backgroundColor: '#4f46e5',
43
+ motionSurface: surface,
44
+ })
45
+ const separate = create(
46
+ 'MText',
47
+ { id: 'isolated', text: 'Independent theme' },
48
+ { theme: isolatedTheme },
49
+ )
50
+ const separateBox = create('MBox', {
51
+ padding: 'md',
52
+ radius: 'md',
53
+ backgroundColor: isolatedTheme.theme.colors.background,
54
+ })
55
+ separateBox.view.addChild(separate.view)
56
+ const offTheme = theme.subscribe(() =>
57
+ root.update({
58
+ id: 'catalog',
59
+ gap: 'md',
60
+ padding: 'lg',
61
+ height: '100%',
62
+ backgroundColor: theme.theme.colors.background,
63
+ }),
64
+ )
65
+ ;(row.view as LayoutBase).addChild(button.view)
66
+ for (const control of [heading, row, input, box, separateBox])
67
+ (root.view as LayoutBase).addChild(control.view)
68
+ function update(patch: Partial<State>) {
69
+ const before = state
70
+ state = { ...state, ...patch }
71
+ button.update({
72
+ id: 'action',
73
+ text: `Count ${state.count}`,
74
+ disabled: state.disabled,
75
+ loading: state.loading,
76
+ })
77
+ input.update({
78
+ id: 'editor',
79
+ label: 'Name',
80
+ modelValue: state.value,
81
+ disabled: state.disabled,
82
+ })
83
+ if (before.visible !== state.visible) {
84
+ if (state.visible) (root.view as LayoutBase).addChild(box.view)
85
+ else (root.view as LayoutBase).removeChild(box.view)
86
+ }
87
+ }
88
+ const page = new Page()
89
+ page.actionBarHidden = true
90
+ page.content = root.view
91
+ bridge.read = () => state
92
+ bridge.update = update
93
+ bridge.focus = input.focus
94
+ bridge.dispose = () => {
95
+ page.content = new GridLayout()
96
+ offTheme()
97
+ all.forEach((control) => control.dispose())
98
+ }
99
+ return page
100
+ },
101
+ })
102
+ }