• 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_get_property_names';
24
25interface myObj {
26  key1: string;
27  key2: number;
28  key3: boolean;
29  key4: Array<string>;
30  key5: Function;
31}
32
33@Entry
34@Component
35struct napiGetPropertyNames {
36  private btnFontColor: Resource = $r('app.color.white');
37  private pixelMapFormat: image.PixelMapFormat = 3;
38  private textcont: Resource = $r('app.string.napi_get_property_names_description');
39  @State isSetInstance: Boolean = false;
40  @State imagePixelMap: PixelMap | undefined = undefined;
41  @State testcont: string = ' // 测试 N-API napi_get_property_names: js输入一个对象,值可以是标量、函数、数组、对象等 \n'
42    + '  let obj = { key1: "aa", key2: 1, key3: false, key4: ["a", "b", "c"],key5: function () { console.info("This is a function") }}\n'
43    + '  const myData = testNapi.testNapiGetPropertyNames(obj); \n'
44    + ' // 使用获取的自定义数据:输出对象所有属性值 \n'
45    + ' console.log(myData); // 输出自定义数据 \n';
46
47  controller: TextAreaController = new TextAreaController()
48
49  build() {
50    Column() {
51      // 标题
52      TitleBar({ title: $r('app.string.napi_get_property_names') })
53
54      Column() {
55        Column() {
56          TextArea({
57            text: this.textcont,
58            placeholder: '',
59          })
60            .placeholderFont({ size: 16, weight: 400 })
61            .width('90%')
62            .margin(10)
63            .fontSize(16)
64            .fontColor($r('app.color.sub_title_color'))
65            .backgroundColor($r('app.color.white'))
66            .enabled(false)
67
68          TextArea({
69            text: this.testcont,
70            placeholder: '',
71          })
72            .placeholderFont({ size: 16, weight: 400 })
73            .width('90%')
74            .margin(10)
75            .fontSize(16)
76            .fontColor($r('app.color.textarea_font_color'))
77            .backgroundColor($r('app.color.textarea_background_color'))
78            .enabled(false)
79        }
80        .width('100%')
81        .alignItems(HorizontalAlign.Center)
82        .justifyContent(FlexAlign.Start)
83
84        Row() {
85
86          Button($r('app.string.napi_get_property_names'), { type: ButtonType.Capsule })
87            .backgroundColor(Color.Blue)
88            .width('80%')
89            .height(48)
90            .fontSize(16)
91            .fontWeight(500)
92            .fontColor(this.btnFontColor)
93            .margin({ left: 24 })
94            .id('napi_get_property_names')
95            .onClick(() => {
96              let obj: myObj = {
97                key1: "value",
98                key2: 1,
99                key3: false,
100                key4: ["a", "b", "c"],
101                key5: () => {
102                  console.info("This is a function");
103                }
104              }
105              let ret = testNapi.testNapiGetPropertyNames(obj);
106              this.testcont = this.testcont.replace('log(myData)', 'log(## '+ret+' ##)');
107            })
108        }
109        .width('100%')
110        .height(48)
111        .alignItems(VerticalAlign.Center)
112        .justifyContent(FlexAlign.SpaceBetween)
113      }
114      .width('100%')
115    }
116    .height('100%')
117    .width('100%')
118    .backgroundColor($r('app.color.background_shallow_grey'))
119  }
120}
121