• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import router from '@ohos.router';
17import image from '@ohos.multimedia.image';
18import Logger from '../../../util/Logger';
19import testNapi, { testNapiValue } from 'libentry.so';
20import { TitleBar } from '../../../common/TitleBar'
21import hilog from '@ohos.hilog';
22
23const TAG: string = 'napi_call_function';
24
25function AddTwo(num: number) {
26  return num + 2;
27}
28
29@Entry
30@Component
31struct napiCallFunction {
32  private btnFontColor: Resource = $r('app.color.white');
33  private pixelMapFormat: image.PixelMapFormat = 3;
34  @State isSetInstance: Boolean = false;
35  @State imagePixelMap: PixelMap | undefined = undefined;
36  @State textcont: string = 'napi_call_function用于从原生附加组件调用 JavaScript 函数对象。'
37    + '这是从加载项的原生代码回调到 JavaScript 的主要机制。';
38  @State testcont: string = ' // 测试 N-API napi_call_function \n'
39    + ' let fun = function AddTwo(num) {return num + 2;} \n'
40    + ' const result = testNapi.testNapiCallFunction(fun, 7); \n'
41    + ' console.log(result); \n'
42
43
44  controller: TextAreaController = new TextAreaController()
45
46  build() {
47    Column() {
48      // 标题
49      TitleBar({ title: $r('app.string.napi_call_function') })
50
51      Column() {
52        Column() {
53          TextArea({
54            text: this.textcont,
55            placeholder: '',
56          })
57            .placeholderFont({ size: 16, weight: 400 })
58            .width('90%')
59            .margin(10)
60            .fontSize(16)
61            .fontColor($r('app.color.sub_title_color'))
62            .backgroundColor($r('app.color.white'))
63            .enabled(false)
64
65          TextArea({
66            text: this.testcont,
67            placeholder: '',
68          })
69            .placeholderFont({ size: 16, weight: 400 })
70            .width('90%')
71            .margin(10)
72            .fontSize(16)
73            .fontColor($r('app.color.textarea_font_color'))
74            .backgroundColor($r('app.color.white'))
75            .enabled(false)
76        }
77        .width('100%')
78        .alignItems(HorizontalAlign.Center)
79        .justifyContent(FlexAlign.Start)
80
81        Row() {
82
83          Button($r('app.string.napi_call_function'), { type: ButtonType.Capsule })
84            .backgroundColor(Color.Blue)
85            .width('80%')
86            .height(48)
87            .fontSize(16)
88            .fontWeight(500)
89            .fontColor(this.btnFontColor)
90            .margin({ left: 24 })
91            .id('napi_call_function')
92            .onClick(() => {
93              let fun: Function = AddTwo;
94              let ret: number = testNapi.testNapiCallFunction(fun, 7);
95              this.testcont = this.testcont.replace('log(result)', 'log(## ' + ret + ' ##)');
96            })
97        }
98        .width('100%')
99        .height(48)
100        .alignItems(VerticalAlign.Center)
101        .justifyContent(FlexAlign.SpaceBetween)
102      }
103      .width('100%')
104    }
105    .height('100%')
106    .width('100%')
107    .backgroundColor($r('app.color.background_shallow_grey'))
108  }
109}