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