forked from Tencent/Hippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.ets
More file actions
139 lines (126 loc) · 4.67 KB
/
Index.ets
File metadata and controls
139 lines (126 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { EngineInitParams,
HippyJSModuleCreator,
HippyLibrary,
HippyNativeModuleCreator, HippyRootViewWrapper,
HippyValue,
HRRenderViewCreator, EngineListener, ModuleListener, HippyJsException } from 'hippy';
import { HippyAPIProvider } from 'hippy';
import { createHippyEngine } from 'hippy';
import { ModuleLoadParams } from 'hippy';
import { ModuleLoadStatus } from 'hippy';
import { EngineInitStatus } from 'hippy';
import { HippyEngine } from 'hippy';
import { LogUtils } from 'hippy/src/main/ets/support/utils/LogUtils'
import { BizComponent } from './BizView';
import HippyRenderBaseView
from 'hippy/src/main/ets/renderer_native/components/base/HippyRenderBaseView';
import demoNapi from 'libdemo.so';
import { buildCustomRenderView, ExampleAPIProvider } from '../hippy_extend/ExampleAPIProvider';
class DefaultEngineListener implements EngineListener {
private hippyEngine: HippyEngine
private rootViewWrapper: HippyRootViewWrapper
constructor(hippyEngine: HippyEngine, rootViewWrapper: HippyRootViewWrapper) {
this.hippyEngine = hippyEngine
this.rootViewWrapper = rootViewWrapper
}
onInitialized(statusCode: EngineInitStatus, msg: string): void {
LogUtils.i('hippy demo', 'initEngine status: ' + statusCode + ' msg:' + msg);
if (statusCode == EngineInitStatus.STATUS_OK) {
let loadParams = new ModuleLoadParams(
'vue2/index.ohos.js',
'',
'',
'',
null,
null,
null,
)
loadParams.componentName = "Demo"
loadParams.codeCacheTag = "Demo_1.0" // 建议tag拼接js version,tag会用来拼接v8 code cache文件的路径,不同js不同cache文件
loadParams.jsParams = new Map<string, HippyValue>()
loadParams.jsParams.set(
"msgFromNative",
"Hi js developer, I come from ohos native code!"
)
loadParams.wrappedCustomRenderViewBuilder = wrapBuilder(buildCustomRenderView)
let moduleListener = new DefaultModuleListener()
let rootView = this.hippyEngine.loadModuleWithListener(loadParams, moduleListener)
if (rootView) {
this.rootViewWrapper.setRootView(rootView)
}
demoNapi.Demo_OnHippyRootViewReady();
}
}
}
class DefaultModuleListener implements ModuleListener {
onLoadCompleted(statusCode: ModuleLoadStatus, msg: string): void {
LogUtils.i('hippy demo', 'loadModule status: ' + statusCode + ' msg:' + msg);
}
onJsException(exception: HippyJsException): boolean {
LogUtils.e('hippy demo', 'loadModule onJsException: ' + exception);
return true;
}
onFirstViewAdded(): void {
}
onFirstContentfulPaint(): void {
}
}
@Entry
@Component
struct Index {
@State message: string = 'C接入Hippy根组件';
@StorageLink('libHippy') private libHippy: HippyLibrary | null = null
@StorageLink('abilityContext') private abilityContext: Context | null = null
@State exception: string = ""
private hippyEngine: HippyEngine | null = null
private rootViewWrapper: HippyRootViewWrapper = new HippyRootViewWrapper()
aboutToAppear(): void {
let params = new EngineInitParams(this.libHippy!, this.abilityContext!, this.getUIContext())
params.providers = new Array(new ExampleAPIProvider())
params.coreJSAssetsPath = "vue2/vendor.ohos.js"
params.enableArkCApi = true
this.hippyEngine = createHippyEngine(params)
let engineListener = new DefaultEngineListener(this.hippyEngine, this.rootViewWrapper)
this.hippyEngine.initEngine(engineListener)
this.hippyEngine.setUIContext(this.getUIContext())
this.hippyEngine.setCustomWrappedRenderViewBuilderForCInterface(wrapBuilder(buildCustomRenderView))
}
build() {
Column() {
Text(this.message)
.fontSize(16)
BizComponent()
.width(300)
.height(400)
.border({width: 1, color: Color.Black})
Button('测试销毁')
.width(120)
.height(30)
.onClick((event) => {
demoNapi.Demo_TestDestroy();
})
}
.width('100%')
}
}