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