1 /* 2 * Copyright (c) 2025 Huawei Device 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 16 #ifndef MYAPPLICATION_NATIVEENTRY_H 17 #define MYAPPLICATION_NATIVEENTRY_H 18 19 #include <js_native_api_types.h> 20 #include <ArkUIBaseNode.h> 21 22 namespace NativeModule { 23 24 napi_value CreateNativeRoot(napi_env env, napi_callback_info info); 25 26 napi_value DestroyNativeRoot(napi_env env, napi_callback_info info); 27 28 // 管理Native组件的生命周期和内存。 29 class NativeEntry { 30 public: GetInstance()31 static NativeEntry *GetInstance() { 32 static NativeEntry nativeEntry; 33 return &nativeEntry; 34 } 35 SetContentHandle(ArkUI_NodeContentHandle handle)36 void SetContentHandle(ArkUI_NodeContentHandle handle) { 37 handle_ = handle; 38 } 39 SetRootNode(const std::shared_ptr<ArkUIBaseNode> & baseNode)40 void SetRootNode(const std::shared_ptr<ArkUIBaseNode> &baseNode) { 41 root_ = baseNode; 42 // 添加Native组件到NodeContent上用于挂载显示。 43 OH_ArkUI_NodeContent_AddNode(handle_, root_->GetHandle()); 44 } DisposeRootNode()45 void DisposeRootNode() { 46 // 从NodeContent上卸载组件并销毁Native组件。 47 OH_ArkUI_NodeContent_RemoveNode(handle_, root_->GetHandle()); 48 root_.reset(); 49 } 50 51 private: 52 std::shared_ptr<ArkUIBaseNode> root_; 53 ArkUI_NodeContentHandle handle_; 54 }; 55 56 } // namespace NativeModule 57 58 #endif // MYAPPLICATION_NATIVEENTRY_H