• 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';
24
25interface myObjVal {
26  key: string
27}
28
29interface myObj {
30  key1: string;
31  true: number;
32  key3: boolean;
33  key4: Array<string>;
34  key5: Function;
35  key6: myObjVal;
36}
37
38@Entry
39@Component
40struct napiGetPropertyNames {
41  private btnFontColor: Resource = $r('app.color.white');
42  private pixelMapFormat: image.PixelMapFormat = 3;
43  private textcont: Resource = $r('app.string.napi_get_property_description');
44  @State isSetInstance: Boolean = false;
45  @State imagePixelMap: PixelMap | undefined = undefined;
46  @State testcont: string = ' // 测试 N-API napi_get_property \n'
47    + 'let obj = {key1: "value",true: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n'
48    + '  const myData = testNapi.testNapiGetProperty(obj, "key1"); \n'
49    + ' // 使用获取的自定义数据 \n'
50    + ' console.log(myData); // 输出自定义数据 \n'
51    + ' const myData2 = testNapi.testNapiGetProperty(obj, true); \n'
52    + ' console.log(myData2); // 输出自定义数据 \n'
53    + ' const myData3 = testNapi.testNapiGetProperty(obj, key3); \n'
54    + ' console.log(myData3); // 输出自定义数据 \n'
55    + ' const myData4 = testNapi.testNapiGetProperty(obj, "key4"); \n'
56    + ' console.log(myData4); // 输出自定义数据 \n'
57    + ' const myData5 = testNapi.testNapiGetProperty(obj, "key5"); \n'
58    + ' console.log(myData5); // 输出自定义数据 \n'
59    + ' const myData6 = testNapi.testNapiGetProperty(obj, "key6"); \n'
60    + ' console.log(myData6); // 输出自定义数据 \n';
61
62  controller: TextAreaController = new TextAreaController()
63
64  build() {
65    Column() {
66      // 标题
67      TitleBar({ title: $r('app.string.napi_get_property') })
68
69      Column() {
70        Column() {
71          TextArea({
72            text: this.textcont,
73            placeholder: '',
74          })
75            .placeholderFont({ size: 16, weight: 400 })
76            .width('90%')
77            .margin(10)
78            .fontSize(16)
79            .fontColor($r('app.color.sub_title_color'))
80            .backgroundColor($r('app.color.white'))
81            .enabled(false)
82
83          TextArea({
84            text: this.testcont,
85            placeholder: '',
86          })
87            .placeholderFont({ size: 16, weight: 400 })
88            .width('90%')
89            .margin(10)
90            .fontSize(16)
91            .fontColor($r('app.color.textarea_font_color'))
92            .backgroundColor($r('app.color.textarea_background_color'))
93            .enabled(false)
94        }
95        .width('100%')
96        .alignItems(HorizontalAlign.Center)
97        .justifyContent(FlexAlign.Start)
98
99        Row() {
100
101          Button($r('app.string.napi_get_property'), { type: ButtonType.Capsule })
102            .backgroundColor(Color.Blue)
103            .width('80%')
104            .height(48)
105            .fontSize(16)
106            .fontWeight(500)
107            .fontColor(this.btnFontColor)
108            .margin({ left: 24 })
109            .id('napi_get_property')
110            .onClick(() => {
111              let obj: myObj = {
112                key1: "value",
113                true: 1,
114                key3: false,
115                key4: ["a", "b", "c"],
116                key5: () => {
117                  return "function";
118                },
119                key6: {key: "value"}
120              }
121              let ret = testNapi.testNapiGetProperty(obj, "key1");
122              this.testcont = this.testcont.replace('log(myData)', 'log(## '+ret+' ##)');
123              let ret2 = testNapi.testNapiGetProperty(obj, true);
124              this.testcont = this.testcont.replace('log(myData2)', 'log(## '+ret2+' ##)');
125              let ret3 = testNapi.testNapiGetProperty(obj, "key3");
126              this.testcont = this.testcont.replace('log(myData3)', 'log(## '+ret3+' ##)');
127              let ret4 = testNapi.testNapiGetProperty(obj, "key4");
128              this.testcont = this.testcont.replace('log(myData4)', 'log(## '+ret4+' ##)');
129              let ret5 = testNapi.testNapiGetProperty(obj, "key5");
130              this.testcont = this.testcont.replace('log(myData5)', 'log(## '+ret5+' ##)');
131              let ret6 = testNapi.testNapiGetProperty(obj, "key6");
132              this.testcont = this.testcont.replace('log(myData6)', 'log(## '+JSON.stringify(ret6)+' ##)');
133            })
134        }
135        .width('100%')
136        .height(48)
137        .alignItems(VerticalAlign.Center)
138        .justifyContent(FlexAlign.SpaceBetween)
139      }
140      .width('100%')
141    }
142    .height('100%')
143    .width('100%')
144    .backgroundColor($r('app.color.background_shallow_grey'))
145  }
146}
147