HarmonyOS 能否提供OH_ArkUI_GetNodeHandleFromNapiValue的使用示例?

HarmonyOS 能否提供OH_ArkUI_GetNodeHandleFromNapiValue的使用示例。


HarmonyOS
2024-10-28 09:22:41
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

可参考:

import nativeNode from "libnativeNode.so";  
import { NodeController, BuilderNode, FrameNode } from "@ohos.arkui.node"  
import { UIContext } from '@ohos.arkui.UIContext';  
  
  
class Params {  
  text: string = ""  
  constructor(text: string) {  
    this.text = text;  
  }  
}  
@Builder  
function buildText(params: Params) {  
  Column() {  
    MyText({message:params.text})  
  }  
}  
  
@Component  
struct MyText {  
  @State fontColor: string = '#182431'  
  @State selectedFontColor: string = '#007DFF'  
  @State currentIndex: number = 0  
  private controller: TabsController = new TabsController()  
  @State message: string="hh"  
  
  @Builder  
  tabBuilder(index: number) {  
    Column() {  
      Image(this.currentIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')  
        .width(24)  
        .height(24)  
        .margin({ bottom: 4 })  
        .objectFit(ImageFit.Contain)  
      Text('Tab')  
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)  
        .fontSize(10)  
        .fontWeight(500)  
        .lineHeight(14)  
    }.width('100%').height('100%').justifyContent(FlexAlign.Center)  
  }  
  
  build() {  
    Column() {  
      Text(this.message)  
        .fontSize(50)  
        .fontWeight(FontWeight.Bold)  
        .backgroundColor(Color.Red)  
        .borderWidth(2)  
        .borderImage({  
          source: {  
            angle: 90,  
            direction: GradientDirection.Left,  
            colors: [[0xAEE1E1, 0.0], [0xD3E0DC, 0.3], [0xFCD1D1, 1.0]]  
          },  
          slice: { top: 10, bottom: 10, left: 10, right: 10 },  
          width: { top: "10px", bottom: "10px", left: "10px", right: "10px" },  
          repeat: RepeatMode.Stretch,  
          fill: false  
        })  
        .margin({ bottom: 36 })  
        .onAppear(() => {  
          console.error("Manager text on appear");  
          setTimeout(() => {  
            this.message = 'ArkUI'  
            console.error("Manager change text")  
          }, 1000)  
        })  
    }  
  }  
}  
  
@Builder  
function MyStack() {  
  Stack()  
    .width(300)  
    .height(300)  
    .backgroundColor(Color.Red)  
}  
  
class TextNodeController extends NodeController {  
  textNode: BuilderNode<[Params]> | null = null;  
  private message: string = "DEFAULT";  
  
  constructor(message: string) {  
    super();  
    this.message = message;  
  }  
  
  makeNode(context: UIContext): FrameNode | null {  
    this.textNode = new BuilderNode(context);  
    this.textNode.build(wrapBuilder<[Params]>(buildText), new Params("arkui"))  
    this.textNode.update(new Params("ArkUI"));  
    return this.textNode.getFrameNode();  
  }  
}  
  
@Entry  
@Component  
struct Xcomponent {  
  @State message: string = "hello"  
  params: Params = new Params("1")  
  controller: TextNodeController = new TextNodeController(this.message)  
  node: BuilderNode<Object[]> | null = null;  
  frameNode: FrameNode | null = null;  
  @State value: number = 40  
  
  build() {  
    Column() {  
      NodeContainer(this.controller)  
        .width('100%')  
        .height(100)  
        .backgroundColor('#FFF0F0F0')  
  
      Column() {  
        XComponent({  
          id: 'xcomponentId',  
          type: "node",  
          libraryname: 'nativeNode'  
        })  
          .onLoad((xComponentContext) => {  
            console.log("onLoad");  
          })  
          .onDestroy(() => {  
            console.log("onDestroy")  
          })  
          .onAppear(() => {  
            console.log("onAppear")  
            this.node = new BuilderNode<Object[]>(this.getUIContext());  
            this.node.build(wrapBuilder(MyStack));  
            this.frameNode = this.node.getFrameNode();  
            console.info("Manager", (this.frameNode instanceof Object));  
            if(this.frameNode != null){  
              console.info("Manager", this.frameNode["nodePtr_"])  
            }  
            if(this.controller.textNode != null) {  
              nativeNode.createNativeNode("xcomponentId", this.controller.textNode.getFrameNode());  
            }  
            setTimeout(() => {  
              console.error("Manager update text node");  
              if(this.controller.textNode != null) {  
                this.controller.textNode.update(new Params("ArkUI"));  
              }  
            }, 1000)  
  
          }).width("100%")  
          .id("xcomponent")  
      }  
      .borderWidth(5)  
      .borderColor(Color.Blue)  
      .margin({  
        top: 27,  
        left: 12,  
        right: 12  
      })  
      .height(`${this.value}%`)  
      .width('100%')  
      .onAppear(() => {  
  
      })  
    }  
    .width('100%')  
    .height('100%')  
  }  
}

在Native侧如下:

napi_value Manager::CreateNativeNode(napi_env env, napi_callback_info info) {  
  if ((env == nullptr) || (info == nullptr)) {  
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "CreateNativeNode env or info is null");  
    return nullptr;  
  }  
  
  size_t argCnt = 2;  
  napi_value args[2] = {nullptr};  
  if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {  
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "CreateNativeNode napi_get_cb_info failed");  
  }  
  if (argCnt < 1) {  
    napi_throw_type_error(env, NULL, "Wrong number of arguments");  
    return nullptr;  
  }  
  napi_valuetype valuetype;  
  if (napi_typeof(env, args[0], &valuetype) != napi_ok) {  
    napi_throw_type_error(env, NULL, "napi_typeof failed");  
    return nullptr;  
  }  
  
  if (valuetype != napi_string) {  
    napi_throw_type_error(env, NULL, "Wrong type of arguments");  
    return nullptr;  
  }  
  napi_valuetype valuetype1;  
  auto typeId = napi_typeof(env, args[1], &valuetype1);  
  if (typeId != napi_ok) {  
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "napi_typeof error  : %{public}d", typeId);  
    return nullptr;  
  }  
  if (valuetype1 != napi_object) {  
    napi_throw_type_error(env, NULL, "Wrong type of arguments of 1");  
    return nullptr;  
  }  
  
  char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};  
  constexpr uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;  
  size_t length;  
  if (napi_get_value_string_utf8(env, args[0], idStr, idSize, &length) != napi_ok) {  
    napi_throw_type_error(env, NULL, "napi_get_value_int64 failed");  
    return nullptr;  
  }  
  
  napi_value properties = nullptr;  
  napi_get_property_names(env, args[1], &properties);  
  uint32_t length1 = 0;  
  napi_get_array_length(env, properties, &length1);  
  for (uint32_t i = 0; i < length1; i++) {  
    napi_value propertyName = nullptr;  
    napi_get_element(env, properties, i, &propertyName);  
    char idStr[100] = {'\0'};  
    constexpr uint64_t idSize = 100;  
    size_t length;  
    napi_get_value_string_utf8(env, propertyName, idStr, idSize, &length);  
  }  
  
  
  auto manager = Manager::GetInstance();  
  if (manager == nullptr) {  
    return nullptr;  
  }  
  
  OH_NativeXComponent *component = manager->GetNativeXComponent(idStr);  
  if (component == nullptr) {  
    return nullptr;  
  }  
  
  ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;  
  OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);  
  if (nodeAPI != nullptr) {  
    if (nodeAPI->createNode != nullptr && nodeAPI->addChild != nullptr) {  
      ArkUI_NodeHandle stack = nodeAPI->createNode(ARKUI_NODE_TEXT);  
      ArkUI_NumberValue widthValue[] = {{.f32 = 300}};  
    ArkUI_AttributeItem widthItem = {.value = widthValue, .size = 1};  
    nodeAPI->setAttribute(stack, NODE_WIDTH, &widthItem);  
    ArkUI_NumberValue heightValue[] = {{.f32 = 600}};  
  ArkUI_AttributeItem heightItem = {.value = heightValue, .size = 1};  
  nodeAPI->setAttribute(stack, NODE_HEIGHT, &heightItem);  
  ArkUI_NumberValue backgroundColorValue[] = {{.u32 = 0xFFFF0000}};  
ArkUI_AttributeItem backgroundColorItem = {.value = backgroundColorValue, .size = 1};  
nodeAPI->setAttribute(stack, NODE_BACKGROUND_COLOR, &backgroundColorItem);  
ArkUI_NodeHandle tsStackNode;  
auto result = OH_ArkUI_GetNodeHandleFromNapiValue(env, args[1], &tsStackNode);  
if (result == 0) {  
  int code;  
  code = nodeAPI->getAttribute(tsStackNode, NODE_WIDTH)->value[0].f32;  
}
分享
微博
QQ
微信
回复
2024-10-28 16:40:19
相关问题
HarmonyOS 能否提供折线图组件
280浏览 • 1回复 待解决
HarmonyOS OH_Audio 需要提供 mute 方法
287浏览 • 1回复 待解决
ArkUI组件能否支持继承
1226浏览 • 1回复 待解决
HarmonyOS能否提供数据存储样例工程
242浏览 • 1回复 待解决
能否提供图片预览官方实现?
46浏览 • 1回复 待解决
harmonyos开发能否尽快提供map组件啊
6971浏览 • 2回复 待解决
HarmonyOS 能否提供Web样例工程代码?
110浏览 • 1回复 待解决
HarmonyOS 能否提供登录界面样例demo?
151浏览 • 1回复 待解决
能否提供命令行构建?
667浏览 • 1回复 待解决
HarmonyOS 消息通知使用示例demo
217浏览 • 1回复 待解决
HarmonyOS能否提供一个NFC识别的demo
257浏览 • 1回复 待解决
HarmonyOS 使用Web组件加载页面示例
356浏览 • 1回复 待解决
Aspect工具装饰器使用示例
1905浏览 • 2回复 待解决