@getcortexlabs/nitroui-vue 0.1.0 → 0.2.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/README.md +2 -2
- package/compiler.d.mts +5 -0
- package/compiler.mjs +63 -0
- package/package.json +16 -3
- package/src/components.ts +6 -0
- package/src/index.ts +4 -1
- package/src/layout.ts +22 -0
- package/src/list.ts +97 -0
- package/src/navigation.ts +75 -0
- package/src/pressable.ts +26 -0
- package/src/renderer.ts +50 -21
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# NitroUI Vue
|
|
2
2
|
|
|
3
|
-
A community Vue 3 renderer for NitroUI.
|
|
3
|
+
A community Vue 3 renderer for NitroUI. Build native iOS and Android screens with Vue components, scrollable content, recycled lists and a native navigation stack.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -31,4 +31,4 @@ const App = defineComponent(() => {
|
|
|
31
31
|
registerApp(App)
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
For
|
|
34
|
+
For nested Vue templates, the Bun compiler and a native demo, see the [single-file component and compiler setup](https://github.com/getcortexlabs/nitroui-vue/tree/main/example). The package uses the public NitroUI native bindings and ships TypeScript for the NitroUI bundler.
|
package/compiler.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Compile a Vue entry and its local imports to one module for NitroUI. Requires Bun. */
|
|
2
|
+
export function compileVue(entry: string, output: string): Promise<void>
|
|
3
|
+
|
|
4
|
+
/** Recompile after edits under the source directory. Close the watcher to stop. */
|
|
5
|
+
export function watchVue(entry: string, output: string, directory?: string): Promise<{ close(): void }>
|
package/compiler.mjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
2
|
+
import { watch } from 'node:fs'
|
|
3
|
+
import { readFile, rename, rm, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { dirname, resolve } from 'node:path'
|
|
5
|
+
import { compileScript, compileTemplate, parse } from '@vue/compiler-sfc'
|
|
6
|
+
|
|
7
|
+
const vue = {
|
|
8
|
+
name: 'nitroui-vue-sfc',
|
|
9
|
+
setup(build) {
|
|
10
|
+
build.onLoad({ filter: /\.vue$/ }, async ({ path }) => {
|
|
11
|
+
const source = await readFile(path, 'utf8')
|
|
12
|
+
const { descriptor, errors } = parse(source, { filename: path })
|
|
13
|
+
if (errors.length) throw new Error(path + '\n' + errors.join('\n'))
|
|
14
|
+
if (descriptor.styles.length) throw new Error(path + ' uses an unsupported native <style> block.')
|
|
15
|
+
const id = createHash('sha256').update(path).digest('hex').slice(0, 10)
|
|
16
|
+
const compilerOptions = { runtimeModuleName: '@vue/runtime-core' }
|
|
17
|
+
if (descriptor.scriptSetup) {
|
|
18
|
+
const script = compileScript(descriptor, { id, inlineTemplate: true, templateOptions: { compilerOptions } })
|
|
19
|
+
return { contents: script.content, loader: 'ts' }
|
|
20
|
+
}
|
|
21
|
+
const script = descriptor.script ? compileScript(descriptor, { id, genDefaultAs: 'component' }).content : 'const component = {}'
|
|
22
|
+
if (!descriptor.template) return { contents: script + '\nexport default component', loader: 'ts' }
|
|
23
|
+
const template = compileTemplate({ source: descriptor.template.content, filename: path, id, compilerOptions })
|
|
24
|
+
if (template.errors.length) throw new Error(path + '\n' + template.errors.join('\n'))
|
|
25
|
+
return { contents: script + '\n' + template.code + '\nexport default Object.assign(component, { render })', loader: 'ts' }
|
|
26
|
+
})
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Bundle a local component tree, leaving runtime packages for NitroUI's platform build. */
|
|
31
|
+
export async function compileVue(entry, output) {
|
|
32
|
+
const result = await Bun.build({ entrypoints: [resolve(entry)], target: 'browser', format: 'esm', packages: 'external', plugins: [vue], throw: false })
|
|
33
|
+
if (!result.success) throw new Error(result.logs.map(log => log.message).join('\n'))
|
|
34
|
+
if (result.outputs.length !== 1) throw new Error('Expected a single compiled Vue module.')
|
|
35
|
+
const temporary = output + '.' + randomUUID() + '.tmp'
|
|
36
|
+
try {
|
|
37
|
+
await writeFile(temporary, await result.outputs[0].text())
|
|
38
|
+
await rename(temporary, output)
|
|
39
|
+
} finally {
|
|
40
|
+
await rm(temporary, { force: true })
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Watch local source files, preserving the last successful build on a compiler error. */
|
|
45
|
+
export async function watchVue(entry, output, directory = dirname(entry)) {
|
|
46
|
+
let pending = false
|
|
47
|
+
let running = false
|
|
48
|
+
async function rebuild() {
|
|
49
|
+
pending = true
|
|
50
|
+
if (running) return
|
|
51
|
+
running = true
|
|
52
|
+
while (pending) {
|
|
53
|
+
pending = false
|
|
54
|
+
try { await compileVue(entry, output) } catch (error) { console.error(error.message) }
|
|
55
|
+
}
|
|
56
|
+
running = false
|
|
57
|
+
}
|
|
58
|
+
const watcher = watch(directory, { recursive: true }, (_, name) => {
|
|
59
|
+
if (name && /\.(?:vue|[cm]?[jt]sx?)$/.test(name) && resolve(directory, name) !== resolve(output)) void rebuild()
|
|
60
|
+
})
|
|
61
|
+
await rebuild()
|
|
62
|
+
return watcher
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getcortexlabs/nitroui-vue",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Experimental Vue 3 renderer for NitroUI.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"exports":
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.ts",
|
|
8
|
+
"./compiler": {
|
|
9
|
+
"types": "./compiler.d.mts",
|
|
10
|
+
"default": "./compiler.mjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
7
13
|
"peerDependencies": {
|
|
8
14
|
"@vue/runtime-core": "^3.5.46",
|
|
15
|
+
"@vue/compiler-sfc": "^3.5.46",
|
|
16
|
+
"nitro-core": "1.0.0-alpha.nightly.137.gd4793c40b8b8",
|
|
9
17
|
"nitro-ui-layout": "0.3.0",
|
|
10
18
|
"nitro-ui-renderer": "0.3.0",
|
|
11
19
|
"nitro-ui-runtime": "0.3.0"
|
|
12
20
|
},
|
|
21
|
+
"peerDependenciesMeta": {
|
|
22
|
+
"@vue/compiler-sfc": { "optional": true }
|
|
23
|
+
},
|
|
13
24
|
"files": [
|
|
14
|
-
"src"
|
|
25
|
+
"src",
|
|
26
|
+
"compiler.mjs",
|
|
27
|
+
"compiler.d.mts"
|
|
15
28
|
],
|
|
16
29
|
"publishConfig": {
|
|
17
30
|
"access": "public"
|
package/src/components.ts
CHANGED
|
@@ -23,6 +23,12 @@ export const Text: FunctionalComponent<TextProps> = (props, { slots }) =>
|
|
|
23
23
|
View.inheritAttrs = false
|
|
24
24
|
Text.inheritAttrs = false
|
|
25
25
|
|
|
26
|
+
export const ScrollView: FunctionalComponent<ViewProps> = (props, { slots }) =>
|
|
27
|
+
h('nitro-scroll', { layout: { flexGrow: 1, ...props.layout } }, [
|
|
28
|
+
h('nitro-scroll-content', null, slots.default?.()),
|
|
29
|
+
])
|
|
30
|
+
ScrollView.inheritAttrs = false
|
|
31
|
+
|
|
26
32
|
export const TextInput = defineComponent({
|
|
27
33
|
name: 'TextInput',
|
|
28
34
|
props: {
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,10 @@ import type { Component } from '@vue/runtime-core'
|
|
|
3
3
|
import { Application, ViewController, Window } from 'nitro-ui-renderer/hybrids'
|
|
4
4
|
import { mount } from './renderer'
|
|
5
5
|
|
|
6
|
-
export {
|
|
6
|
+
export { Pressable } from './pressable'
|
|
7
|
+
export { NavigationStack, useNavigation } from './navigation'
|
|
8
|
+
export { ListView } from './list'
|
|
9
|
+
export { ScrollView, Text, TextInput, View } from './components'
|
|
7
10
|
export type { Layout } from './layout'
|
|
8
11
|
export { mount }
|
|
9
12
|
|
package/src/layout.ts
CHANGED
|
@@ -6,14 +6,36 @@ export interface Layout {
|
|
|
6
6
|
padding?: number
|
|
7
7
|
gap?: number
|
|
8
8
|
flexGrow?: number
|
|
9
|
+
flexDirection?: 'column' | 'column-reverse' | 'row' | 'row-reverse'
|
|
10
|
+
alignItems?: 'flex-start' | 'center' | 'flex-end' | 'stretch' | 'baseline'
|
|
11
|
+
margin?: number
|
|
9
12
|
justifyContent?: 'flex-start' | 'center' | 'flex-end'
|
|
10
13
|
}
|
|
11
14
|
|
|
15
|
+
const fields: (keyof Layout)[] = ['width', 'height', 'padding', 'gap', 'flexGrow', 'justifyContent', 'flexDirection', 'alignItems', 'margin']
|
|
16
|
+
|
|
17
|
+
export function sameLayout(next: Layout = {}, previous: Layout = {}) {
|
|
18
|
+
return fields.every(field => Object.is(next[field], previous[field]))
|
|
19
|
+
}
|
|
20
|
+
|
|
12
21
|
export function updateLayout(node: NLNodeRef, next: Layout = {}, previous: Layout = {}) {
|
|
13
22
|
const api = nitroLayout
|
|
14
23
|
if (next.width !== previous.width) api.NLSetWidth(node, next.width ?? NaN, api.NLPoint)
|
|
15
24
|
if (next.height !== previous.height) api.NLSetHeight(node, next.height ?? NaN, api.NLPoint)
|
|
16
25
|
if (next.flexGrow !== previous.flexGrow) api.NLSetFlexGrow(node, next.flexGrow ?? 0)
|
|
26
|
+
if (next.flexDirection !== previous.flexDirection) {
|
|
27
|
+
const directions = { column: api.NLColumn, 'column-reverse': api.NLColumnReverse, row: api.NLRow, 'row-reverse': api.NLRowReverse }
|
|
28
|
+
api.NLSetFlexDirection(node, directions[next.flexDirection ?? 'column'])
|
|
29
|
+
}
|
|
30
|
+
if (next.alignItems !== previous.alignItems) {
|
|
31
|
+
const alignments = { 'flex-start': api.NLAlignStart, center: api.NLAlignCenter, 'flex-end': api.NLAlignEnd, stretch: api.NLAlignStretch, baseline: api.NLAlignBaseline }
|
|
32
|
+
api.NLSetAlignItems(node, alignments[next.alignItems ?? 'stretch'])
|
|
33
|
+
}
|
|
34
|
+
if (next.margin !== previous.margin) {
|
|
35
|
+
for (const edge of [api.NLLeft, api.NLTop, api.NLRight, api.NLBottom]) {
|
|
36
|
+
api.NLSetMargin(node, edge, next.margin ?? 0, api.NLPoint)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
17
39
|
if (next.gap !== previous.gap) {
|
|
18
40
|
api.NLSetColumnGap(node, next.gap ?? 0, api.NLPoint)
|
|
19
41
|
api.NLSetRowGap(node, next.gap ?? 0, api.NLPoint)
|
package/src/list.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { defineComponent, getCurrentInstance, h, onBeforeUnmount, onMounted, watchEffect, type AppContext, type PropType, type VNodeChild } from '@vue/runtime-core'
|
|
2
|
+
import { NitroModules } from 'nitro-core'
|
|
3
|
+
import type { View } from 'nitro-ui-renderer/hybrids'
|
|
4
|
+
import type { Layout } from './layout'
|
|
5
|
+
import { createHost } from './renderer'
|
|
6
|
+
|
|
7
|
+
interface Descriptor { id: string; type: string; version: number; height?: number }
|
|
8
|
+
interface NativeList extends View {
|
|
9
|
+
setHorizontal(horizontal: boolean): void
|
|
10
|
+
setEstimatedItemSize(height?: number): void
|
|
11
|
+
applySnapshot(items: Descriptor[], animated: boolean, bind: (cell: number, view: View, id: string, index: number) => void, done: () => void): void
|
|
12
|
+
clearDataSource(): void
|
|
13
|
+
}
|
|
14
|
+
type Entry = { item: unknown; index: number; descriptor: Descriptor }
|
|
15
|
+
type Snapshot = Map<string, Entry>
|
|
16
|
+
const Cell = defineComponent({
|
|
17
|
+
props: { content: { type: Function as PropType<() => VNodeChild>, required: true } },
|
|
18
|
+
setup: props => () => props.content(),
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
export const ListView = defineComponent({
|
|
22
|
+
name: 'ListView',
|
|
23
|
+
props: {
|
|
24
|
+
items: { type: Array as PropType<readonly any[]>, required: true },
|
|
25
|
+
itemKey: { type: Function as PropType<(item: any) => string>, required: true },
|
|
26
|
+
itemHeight: Number,
|
|
27
|
+
layout: Object as PropType<Layout>,
|
|
28
|
+
},
|
|
29
|
+
setup(props, { slots }) {
|
|
30
|
+
const Native = NitroModules.getHybridClass<new (queue: typeof queueMicrotask) => NativeList>('renderer.ListView')
|
|
31
|
+
const view = new Native(queueMicrotask)
|
|
32
|
+
view.setHorizontal(false)
|
|
33
|
+
// A cell is a separate render root. Inherit the list's ancestor providers as well as app plugins.
|
|
34
|
+
const instance = getCurrentInstance()! as NonNullable<ReturnType<typeof getCurrentInstance>> & Pick<AppContext, 'provides'>
|
|
35
|
+
const context: AppContext = { ...instance.appContext, provides: instance.provides }
|
|
36
|
+
const cells = new Map<number, { host: ReturnType<typeof createHost>; id: string }>()
|
|
37
|
+
let previous: Snapshot = new Map()
|
|
38
|
+
let pending: Snapshot | undefined
|
|
39
|
+
let applying = false
|
|
40
|
+
let active = true
|
|
41
|
+
function release(cell: { host: ReturnType<typeof createHost> }) {
|
|
42
|
+
cell.host.stop()
|
|
43
|
+
cell.host.renderer.render(null, cell.host.root)
|
|
44
|
+
}
|
|
45
|
+
function apply(entries: Snapshot) {
|
|
46
|
+
if (applying) { pending = entries; return }
|
|
47
|
+
applying = true
|
|
48
|
+
view.applySnapshot(Array.from(entries.values(), entry => entry.descriptor), false, (cellId, container, id, index) => {
|
|
49
|
+
const entry = entries.get(id)
|
|
50
|
+
if (!active || !entry) return
|
|
51
|
+
let cell = cells.get(cellId)
|
|
52
|
+
if (!cell || !cell.host.root.native.equals(container)) {
|
|
53
|
+
if (cell) release(cell)
|
|
54
|
+
cell = { host: createHost(container, true), id }
|
|
55
|
+
cells.set(cellId, cell)
|
|
56
|
+
}
|
|
57
|
+
cell.id = id
|
|
58
|
+
const node = h(Cell, { key: id, content: () => slots.default?.({ item: entry.item, index }) })
|
|
59
|
+
node.appContext = context
|
|
60
|
+
cell.host.renderer.render(node, cell.host.root)
|
|
61
|
+
cell.host.flushLayout()
|
|
62
|
+
}, () => {
|
|
63
|
+
if (!active) return
|
|
64
|
+
for (const [key, cell] of cells) {
|
|
65
|
+
if (!entries.has(cell.id)) { release(cell); cells.delete(key) }
|
|
66
|
+
}
|
|
67
|
+
applying = false
|
|
68
|
+
const next = pending
|
|
69
|
+
pending = undefined
|
|
70
|
+
if (next) apply(next)
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
onMounted(() => watchEffect(() => {
|
|
74
|
+
const entries: Snapshot = new Map()
|
|
75
|
+
const height = props.itemHeight
|
|
76
|
+
for (const [index, item] of props.items.entries()) {
|
|
77
|
+
const id = props.itemKey(item)
|
|
78
|
+
if (typeof id !== 'string' || entries.has(id)) throw new Error('ListView itemKey must return a unique string for each row.')
|
|
79
|
+
const old = previous.get(id)
|
|
80
|
+
const unchanged = old && old.item === item && old.index === index && old.descriptor.height === height
|
|
81
|
+
const version = unchanged ? old.descriptor.version : (old?.descriptor.version ?? 0) + 1
|
|
82
|
+
entries.set(id, { item, index, descriptor: { id, type: 'row', version, height } })
|
|
83
|
+
}
|
|
84
|
+
previous = entries
|
|
85
|
+
view.setEstimatedItemSize(height)
|
|
86
|
+
apply(entries)
|
|
87
|
+
}, { flush: 'post' }))
|
|
88
|
+
onBeforeUnmount(() => {
|
|
89
|
+
active = false
|
|
90
|
+
pending = undefined
|
|
91
|
+
view.clearDataSource()
|
|
92
|
+
for (const cell of cells.values()) release(cell)
|
|
93
|
+
cells.clear()
|
|
94
|
+
})
|
|
95
|
+
return () => h('nitro-native', { view, layout: { flexGrow: 1, ...props.layout } })
|
|
96
|
+
},
|
|
97
|
+
})
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { defineComponent, h, inject, onBeforeUnmount, onMounted, provide, shallowRef, type Component, type InjectionKey } from '@vue/runtime-core'
|
|
2
|
+
import { NitroModules } from 'nitro-core'
|
|
3
|
+
import type { View, ViewController } from 'nitro-ui-renderer/hybrids'
|
|
4
|
+
import { controllerKey } from './renderer'
|
|
5
|
+
|
|
6
|
+
interface NativeStack extends View {
|
|
7
|
+
attachTo(controller: ViewController): void
|
|
8
|
+
removeFromParent(): void
|
|
9
|
+
addOnPopListener(callback: (count: number) => void): { remove(): void }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface NativeScreen extends View {
|
|
13
|
+
viewController: ViewController
|
|
14
|
+
presentation: 'card' | 'modal'
|
|
15
|
+
headerShown: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface Navigation {
|
|
19
|
+
present(component: Component, props?: Record<string, unknown>): void
|
|
20
|
+
dismiss(): void
|
|
21
|
+
}
|
|
22
|
+
const navigationKey: InjectionKey<Navigation> = Symbol('Navigation')
|
|
23
|
+
|
|
24
|
+
export function useNavigation(): Navigation {
|
|
25
|
+
const navigation = inject(navigationKey)
|
|
26
|
+
if (!navigation) throw new Error('useNavigation() requires a NavigationStack ancestor.')
|
|
27
|
+
return navigation
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const Screen = defineComponent({
|
|
31
|
+
props: { modal: Boolean },
|
|
32
|
+
setup(props, { slots }) {
|
|
33
|
+
const ScreenClass = NitroModules.getHybridClass<new () => NativeScreen>('navigation.NavigationScreen')
|
|
34
|
+
const view = new ScreenClass()
|
|
35
|
+
view.headerShown = false
|
|
36
|
+
view.presentation = props.modal ? 'modal' : 'card'
|
|
37
|
+
provide(controllerKey, view.viewController)
|
|
38
|
+
const frame = shallowRef(view.viewController.view.getFrame())
|
|
39
|
+
const size = view.viewController.addOnSizeChangedListener(value => {
|
|
40
|
+
frame.value = { ...frame.value, ...value }
|
|
41
|
+
})
|
|
42
|
+
const node = nitroLayout.NLNodeFromHandle(view.layoutNodePtr)
|
|
43
|
+
nitroLayout.NLSetPositionType(node, nitroLayout.NLAbsolute)
|
|
44
|
+
nitroLayout.NLSetInset(node, nitroLayout.NLLeft, 0, nitroLayout.NLPoint)
|
|
45
|
+
nitroLayout.NLSetInset(node, nitroLayout.NLTop, 0, nitroLayout.NLPoint)
|
|
46
|
+
onBeforeUnmount(() => size.remove())
|
|
47
|
+
return () => h('nitro-native', { view, layout: { width: frame.value.width, height: frame.value.height } }, slots.default?.())
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
export const NavigationStack = defineComponent({
|
|
52
|
+
name: 'NavigationStack',
|
|
53
|
+
setup(_, { slots }) {
|
|
54
|
+
const controller = inject(controllerKey)
|
|
55
|
+
if (!controller) throw new Error('A NavigationStack must be mounted in a NitroUI controller.')
|
|
56
|
+
const Stack = NitroModules.getHybridClass<new () => NativeStack>('navigation.NavigationStack')
|
|
57
|
+
const view = new Stack()
|
|
58
|
+
let nextId = 0
|
|
59
|
+
const screens = shallowRef<{ id: number; component: Component; props?: Record<string, unknown> }[]>([])
|
|
60
|
+
function pop(count: number) {
|
|
61
|
+
if (count > 0) screens.value = screens.value.slice(0, Math.max(0, screens.value.length - count))
|
|
62
|
+
}
|
|
63
|
+
provide(navigationKey, {
|
|
64
|
+
present(component, props) { screens.value = [...screens.value, { id: ++nextId, component, props }] },
|
|
65
|
+
dismiss() { pop(1) },
|
|
66
|
+
})
|
|
67
|
+
const back = view.addOnPopListener(pop)
|
|
68
|
+
onMounted(() => view.attachTo(controller))
|
|
69
|
+
onBeforeUnmount(() => { back.remove(); view.removeFromParent() })
|
|
70
|
+
return () => h('nitro-native', { view, layout: { flexGrow: 1 } }, [
|
|
71
|
+
h(Screen, { key: 'root' }, slots),
|
|
72
|
+
...screens.value.map(screen => h(Screen, { key: screen.id, modal: true }, () => h(screen.component, screen.props))),
|
|
73
|
+
])
|
|
74
|
+
},
|
|
75
|
+
})
|
package/src/pressable.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defineComponent, h, onBeforeUnmount, watchEffect, type PropType } from '@vue/runtime-core'
|
|
2
|
+
import { Application, type View } from 'nitro-ui-renderer/hybrids'
|
|
3
|
+
import { NitroModules } from 'nitro-core'
|
|
4
|
+
import type { Layout } from './layout'
|
|
5
|
+
|
|
6
|
+
type TapGesture = NonNullable<Parameters<View['setGestureRecognizers']>[0]>[number] & { onTap?: () => void }
|
|
7
|
+
|
|
8
|
+
export const Pressable = defineComponent({
|
|
9
|
+
name: 'Pressable',
|
|
10
|
+
props: { disabled: Boolean, layout: Object as PropType<Layout> },
|
|
11
|
+
emits: { press: () => true },
|
|
12
|
+
setup(props, { slots, emit }) {
|
|
13
|
+
const view = Application.createView()
|
|
14
|
+
const Tap = NitroModules.getHybridClass<new () => TapGesture>('renderer.TapGestureRecognizer')
|
|
15
|
+
const tap = new Tap()
|
|
16
|
+
tap.onTap = () => { if (!props.disabled) emit('press') }
|
|
17
|
+
watchEffect(() => { tap.isEnabled = !props.disabled })
|
|
18
|
+
view.setGestureRecognizers([tap])
|
|
19
|
+
onBeforeUnmount(() => {
|
|
20
|
+
view.setGestureRecognizers([])
|
|
21
|
+
tap.onTap = undefined
|
|
22
|
+
tap.dispose()
|
|
23
|
+
})
|
|
24
|
+
return () => h('nitro-native', { view, layout: { ...props.layout } }, slots.default?.())
|
|
25
|
+
},
|
|
26
|
+
})
|
package/src/renderer.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { createRenderer, type Component } from '@vue/runtime-core'
|
|
2
|
-
import { Application, Label, type TextInput, type View, type ViewController } from 'nitro-ui-renderer/hybrids'
|
|
3
|
-
import { updateLayout } from './layout'
|
|
1
|
+
import { createRenderer, type Component, type InjectionKey } from '@vue/runtime-core'
|
|
2
|
+
import { Application, Label, ScrollView, ScrollViewContent, type TextInput, type View, type ViewController } from 'nitro-ui-renderer/hybrids'
|
|
3
|
+
import { sameLayout, updateLayout } from './layout'
|
|
4
|
+
|
|
5
|
+
export const controllerKey: InjectionKey<ViewController> = Symbol('ViewController')
|
|
4
6
|
|
|
5
7
|
type Element = {
|
|
6
8
|
parent: Element | null
|
|
@@ -47,28 +49,35 @@ function dispose(node: HostNode) {
|
|
|
47
49
|
node.children.forEach(dispose)
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const root: Element = { kind: 'view', native: controller.view, parent: null, children: [] }
|
|
52
|
+
export function createHost(native: View, cell = false) {
|
|
53
|
+
const root: Element = { kind: 'view', native, parent: null, children: [] }
|
|
53
54
|
let queued = false
|
|
54
55
|
let active = true
|
|
56
|
+
function flushLayout() {
|
|
57
|
+
if (!active || !queued) return
|
|
58
|
+
queued = false
|
|
59
|
+
const frame = cell ? undefined : native.getFrame()
|
|
60
|
+
const node = layoutNode(root)
|
|
61
|
+
nitroLayout.NLSolve(node, frame?.width ?? NaN, frame?.height ?? NaN)
|
|
62
|
+
nitroLayout.NLApplyLayout(node)
|
|
63
|
+
}
|
|
55
64
|
function requestLayout() {
|
|
56
65
|
if (queued || !active) return
|
|
57
66
|
queued = true
|
|
58
|
-
queueMicrotask(
|
|
59
|
-
queued = false
|
|
60
|
-
if (!active) return
|
|
61
|
-
const { width, height } = root.native.getFrame()
|
|
62
|
-
const node = layoutNode(root)
|
|
63
|
-
nitroLayout.NLSolve(node, width, height)
|
|
64
|
-
nitroLayout.NLApplyLayout(node)
|
|
65
|
-
})
|
|
67
|
+
queueMicrotask(flushLayout)
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
const renderer = createRenderer<HostNode, Element>({
|
|
69
|
-
createElement(tag) {
|
|
71
|
+
createElement(tag, _namespace, _is, props) {
|
|
70
72
|
const base = { parent: null, children: [] }
|
|
71
73
|
switch (tag) {
|
|
74
|
+
case 'nitro-native': return { ...base, kind: 'view', native: props!.view }
|
|
75
|
+
case 'nitro-scroll': {
|
|
76
|
+
const native = new ScrollView()
|
|
77
|
+
nitroLayout.NLSetOverflow(nitroLayout.NLNodeFromHandle(native.layoutNodePtr), nitroLayout.NLOverflowScroll)
|
|
78
|
+
return { ...base, kind: 'view', native }
|
|
79
|
+
}
|
|
80
|
+
case 'nitro-scroll-content': return { ...base, kind: 'view', native: new ScrollViewContent() }
|
|
72
81
|
case 'nitro-view': return { ...base, kind: 'view', native: Application.createView() }
|
|
73
82
|
case 'nitro-text': return { ...base, kind: 'label', native: new Label() }
|
|
74
83
|
case 'nitro-input': {
|
|
@@ -107,7 +116,11 @@ export function mount(component: Component, controller: ViewController) {
|
|
|
107
116
|
// Vue uses empty text nodes and comments as fragment and v-if anchors.
|
|
108
117
|
const index = anchor ? parent.children.indexOf(anchor) : parent.children.length
|
|
109
118
|
if (isElement(child)) {
|
|
110
|
-
|
|
119
|
+
let next: Element | undefined
|
|
120
|
+
for (let i = index; i < parent.children.length; i++) {
|
|
121
|
+
const candidate = parent.children[i]
|
|
122
|
+
if (isElement(candidate)) { next = candidate; break }
|
|
123
|
+
}
|
|
111
124
|
if (next) {
|
|
112
125
|
parent.native.insertBefore(child.native, next.native)
|
|
113
126
|
nitroLayout.NLInsertBefore(layoutNode(parent), layoutNode(child), layoutNode(next))
|
|
@@ -133,6 +146,12 @@ export function mount(component: Component, controller: ViewController) {
|
|
|
133
146
|
requestLayout()
|
|
134
147
|
},
|
|
135
148
|
setElementText(node, text) {
|
|
149
|
+
if (node.kind === 'label' && node.children.length === 1 && node.children[0].kind === 'text') {
|
|
150
|
+
node.children[0].value = text
|
|
151
|
+
syncLabel(node)
|
|
152
|
+
requestLayout()
|
|
153
|
+
return
|
|
154
|
+
}
|
|
136
155
|
for (const child of [...node.children]) {
|
|
137
156
|
detach(child)
|
|
138
157
|
dispose(child)
|
|
@@ -146,7 +165,10 @@ export function mount(component: Component, controller: ViewController) {
|
|
|
146
165
|
requestLayout()
|
|
147
166
|
},
|
|
148
167
|
patchProp(node, key, previous, next) {
|
|
149
|
-
if (key === 'layout')
|
|
168
|
+
if (key === 'layout') {
|
|
169
|
+
if (sameLayout(next ?? {}, previous ?? {})) return
|
|
170
|
+
updateLayout(layoutNode(node), next ?? {}, previous ?? {})
|
|
171
|
+
}
|
|
150
172
|
else if (key === 'fontSize' && node.kind === 'label') node.native.fontSize = next ?? 17
|
|
151
173
|
else if (key === 'modelValue' && node.kind === 'input') node.native.setText(next ?? '')
|
|
152
174
|
else if (key === 'placeholder' && node.kind === 'input') node.native.placeholder = next ?? undefined
|
|
@@ -159,12 +181,19 @@ export function mount(component: Component, controller: ViewController) {
|
|
|
159
181
|
},
|
|
160
182
|
})
|
|
161
183
|
|
|
162
|
-
|
|
163
|
-
|
|
184
|
+
return { renderer, root, requestLayout, flushLayout, stop() { active = false } }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Mount a Vue tree into an existing native controller. Call app.unmount() to detach it. */
|
|
188
|
+
export function mount(component: Component, controller: ViewController) {
|
|
189
|
+
const host = createHost(controller.view)
|
|
190
|
+
const app = host.renderer.createApp(component)
|
|
191
|
+
app.provide(controllerKey, controller)
|
|
192
|
+
const size = controller.addOnSizeChangedListener(host.requestLayout)
|
|
164
193
|
app.onUnmount(() => {
|
|
165
|
-
|
|
194
|
+
host.stop()
|
|
166
195
|
size.remove()
|
|
167
196
|
})
|
|
168
|
-
app.mount(root)
|
|
197
|
+
app.mount(host.root)
|
|
169
198
|
return app
|
|
170
199
|
}
|