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