• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MYAPPLICATION_ARKUIBASENODE_H
16 #define MYAPPLICATION_ARKUIBASENODE_H
17 
18 #include <arkui/native_type.h>
19 #include <list>
20 #include <memory>
21 
22 #include "NativeModule.h"
23 
24 namespace NativeModule {
25 
26 class ArkUIBaseNode {
27 public:
ArkUIBaseNode(ArkUI_NodeHandle handle)28     explicit ArkUIBaseNode(ArkUI_NodeHandle handle)
29         : handle_(handle), nativeModule_(NativeModuleInstance::GetInstance()->GetNativeNodeAPI()) {}
30 
~ArkUIBaseNode()31     virtual ~ArkUIBaseNode() {
32         // 封装析构函数,实现子节点移除功能。
33         if (!children_.empty()) {
34             for (const auto& child : children_) {
35                 nativeModule_->removeChild(handle_, child->GetHandle());
36             }
37             children_.clear();
38         }
39         // 封装析构函数,统一回收节点资源。
40         nativeModule_->disposeNode(handle_);
41     }
42 
AddChild(const std::shared_ptr<ArkUIBaseNode> & child)43     void AddChild(const std::shared_ptr<ArkUIBaseNode> &child) {
44         children_.emplace_back(child);
45         OnAddChild(child);
46     }
47 
RemoveChild(const std::shared_ptr<ArkUIBaseNode> & child)48     void RemoveChild(const std::shared_ptr<ArkUIBaseNode> &child) {
49         children_.remove(child);
50         OnRemoveChild(child);
51     }
52 
InsertChild(const std::shared_ptr<ArkUIBaseNode> & child,int32_t index)53     void InsertChild(const std::shared_ptr<ArkUIBaseNode> &child, int32_t index) {
54         if (index >= children_.size()) {
55             AddChild(child);
56         } else {
57             auto iter = children_.begin();
58             std::advance(iter, index);
59             children_.insert(iter, child);
60             OnInsertChild(child, index);
61         }
62     }
63 
GetHandle()64     ArkUI_NodeHandle GetHandle() const { return handle_; }
65 
66 protected:
67     // 针对父容器子类需要重载下面的函数,实现组件挂载和卸载。
OnAddChild(const std::shared_ptr<ArkUIBaseNode> & child)68     virtual void OnAddChild(const std::shared_ptr<ArkUIBaseNode> &child) {}
OnRemoveChild(const std::shared_ptr<ArkUIBaseNode> & child)69     virtual void OnRemoveChild(const std::shared_ptr<ArkUIBaseNode> &child) {}
OnInsertChild(const std::shared_ptr<ArkUIBaseNode> & child,int32_t index)70     virtual void OnInsertChild(const std::shared_ptr<ArkUIBaseNode> &child, int32_t index) {}
71 
72     ArkUI_NodeHandle handle_;
73     ArkUI_NativeNodeAPI_1 *nativeModule_ = nullptr;
74 
75 private:
76     std::list<std::shared_ptr<ArkUIBaseNode>> children_;
77 };
78 } // namespace NativeModule
79 
80 #endif // MYAPPLICATION_ARKUIBASENODE_H