• 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_ARKUINODE_H
16 #define MYAPPLICATION_ARKUINODE_H
17 
18 #include "ArkUIBaseNode.h"
19 #include "NativeModule.h"
20 #include <arkui/native_node.h>
21 #include <arkui/native_type.h>
22 
23 namespace NativeModule {
24 
25 class ArkUINode : public ArkUIBaseNode {
26 public:
ArkUINode(ArkUI_NodeHandle handle)27     explicit ArkUINode(ArkUI_NodeHandle handle) : ArkUIBaseNode(handle) {
28         nativeModule_ = NativeModuleInstance::GetInstance()->GetNativeNodeAPI();
29         // 事件触发时需要通过函数获取对应的事件对象,这边通过设置节点自定义数据将封装类指针保持在组件上,方便后续事件分发。
30         nativeModule_->setUserData(handle_, this);
31         // 注册节点监听事件接受器。
32         nativeModule_->addNodeEventReceiver(handle_, ArkUINode::NodeEventReceiver);
33     }
34 
~ArkUINode()35     ~ArkUINode() override {
36         if (onClick_) {
37             nativeModule_->unregisterNodeEvent(handle_, NODE_ON_CLICK);
38         }
39         if (onTouch_) {
40             nativeModule_->unregisterNodeEvent(handle_, NODE_TOUCH_EVENT);
41         }
42         if (onDisappear_) {
43             nativeModule_->unregisterNodeEvent(handle_, NODE_EVENT_ON_DISAPPEAR);
44         }
45         if (onAppear_) {
46             nativeModule_->unregisterNodeEvent(handle_, NODE_EVENT_ON_APPEAR);
47         }
48         nativeModule_->removeNodeEventReceiver(handle_, ArkUINode::NodeEventReceiver);
49     }
50 
51     // NDK相关通用属性调用封装
52     void SetWidth(float width, bool isPercent = false) {
53         assert(handle_);
54         ArkUI_NumberValue value[] = {{.f32 = width}};
55         ArkUI_AttributeItem item = {value, 1};
56         nativeModule_->setAttribute(handle_, isPercent ? NODE_WIDTH_PERCENT : NODE_WIDTH, &item);
57     }
58 
59     void SetHeight(float height, bool isPercent = false) {
60         assert(handle_);
61         ArkUI_NumberValue value[] = {{.f32 = height}};
62         ArkUI_AttributeItem item = {value, 1};
63         nativeModule_->setAttribute(handle_, isPercent ? NODE_HEIGHT_PERCENT : NODE_HEIGHT, &item);
64     }
65 
SetBackgroundColor(uint32_t color)66     void SetBackgroundColor(uint32_t color) {
67         assert(handle_);
68         ArkUI_NumberValue value[] = {{.u32 = color}};
69         ArkUI_AttributeItem item = {value, 1};
70         nativeModule_->setAttribute(handle_, NODE_BACKGROUND_COLOR, &item);
71     }
72     void SetPadding(float padding, bool isPercent = false) {
73         assert(handle_);
74         ArkUI_NumberValue value[] = {{.f32 = padding}};
75         ArkUI_AttributeItem item = {value, 1};
76         nativeModule_->setAttribute(handle_, isPercent ? NODE_PADDING_PERCENT : NODE_PADDING, &item);
77     }
78 
79     // 处理通用事件。
RegisterOnClick(const std::function<void ()> & onClick)80     void RegisterOnClick(const std::function<void()> &onClick) {
81         assert(handle_);
82         onClick_ = onClick;
83         // 注册点击事件。
84         nativeModule_->registerNodeEvent(handle_, NODE_ON_CLICK, 0, nullptr);
85     }
86 
RegisterOnTouch(const std::function<void (int32_t type,float x,float y)> & onTouch)87     void RegisterOnTouch(const std::function<void(int32_t type, float x, float y)> &onTouch) {
88         assert(handle_);
89         onTouch_ = onTouch;
90         // 注册触碰事件。
91         nativeModule_->registerNodeEvent(handle_, NODE_TOUCH_EVENT, 0, nullptr);
92     }
93 
RegisterOnDisappear(const std::function<void ()> & onDisappear)94     void RegisterOnDisappear(const std::function<void()> &onDisappear) {
95         assert(handle_);
96         onDisappear_ = onDisappear;
97         // 注册卸载事件。
98         nativeModule_->registerNodeEvent(handle_, NODE_EVENT_ON_DISAPPEAR, 0, nullptr);
99     }
100 
RegisterOnAppear(const std::function<void ()> & onAppear)101     void RegisterOnAppear(const std::function<void()> &onAppear) {
102         assert(handle_);
103         onAppear_ = onAppear;
104         // 注册挂载事件。
105         nativeModule_->registerNodeEvent(handle_, NODE_EVENT_ON_APPEAR, 0, nullptr);
106     }
107 
108 protected:
109     // 组件树操作的实现类对接。
OnAddChild(const std::shared_ptr<ArkUIBaseNode> & child)110     void OnAddChild(const std::shared_ptr<ArkUIBaseNode> &child) override {
111         nativeModule_->addChild(handle_, child->GetHandle());
112     }
OnRemoveChild(const std::shared_ptr<ArkUIBaseNode> & child)113     void OnRemoveChild(const std::shared_ptr<ArkUIBaseNode> &child) override {
114         nativeModule_->removeChild(handle_, child->GetHandle());
115     }
OnInsertChild(const std::shared_ptr<ArkUIBaseNode> & child,int32_t index)116     void OnInsertChild(const std::shared_ptr<ArkUIBaseNode> &child, int32_t index) override {
117         nativeModule_->insertChildAt(handle_, child->GetHandle(), index);
118     }
119 
120     // 事件监听器函数指针。
NodeEventReceiver(ArkUI_NodeEvent * event)121     static void NodeEventReceiver(ArkUI_NodeEvent *event) {
122         // 获取事件发生的UI组件对象。
123         auto nodeHandle = OH_ArkUI_NodeEvent_GetNodeHandle(event);
124         // 获取保持在UI组件对象中的自定义数据,返回封装类指针。
125         auto *node = reinterpret_cast<ArkUINode *>(
126             NativeModuleInstance::GetInstance()->GetNativeNodeAPI()->getUserData(nodeHandle));
127         // 基于封装类实例对象处理事件。
128         node->ProcessNodeEvent(event);
129     }
ProcessNodeEvent(ArkUI_NodeEvent * event)130     void ProcessNodeEvent(ArkUI_NodeEvent *event) {
131         auto eventType = OH_ArkUI_NodeEvent_GetEventType(event);
132         switch (eventType) {
133         case NODE_ON_CLICK: {
134             if (onClick_) {
135                 onClick_();
136             }
137             break;
138         }
139         case NODE_TOUCH_EVENT: {
140             if (onTouch_) {
141                 auto *uiInputEvent = OH_ArkUI_NodeEvent_GetInputEvent(event);
142                 float x = OH_ArkUI_PointerEvent_GetX(uiInputEvent);
143                 float y = OH_ArkUI_PointerEvent_GetY(uiInputEvent);
144                 auto type = OH_ArkUI_UIInputEvent_GetAction(uiInputEvent);
145                 onTouch_(type, x, y);
146             }
147         }
148         case NODE_EVENT_ON_DISAPPEAR: {
149             if (onDisappear_) {
150                 onDisappear_();
151             }
152             break;
153         }
154         case NODE_EVENT_ON_APPEAR: {
155             if (onAppear_) {
156                 onAppear_();
157             }
158             break;
159         }
160         default: {
161             // 组件特有事件交给子类处理
162             OnNodeEvent(event);
163         }
164         }
165     }
166 
OnNodeEvent(ArkUI_NodeEvent * event)167     virtual void OnNodeEvent(ArkUI_NodeEvent *event) {}
168 private:
169     std::function<void()> onClick_;
170     std::function<void()> onDisappear_;
171     std::function<void()> onAppear_;
172     std::function<void(int32_t type, float x, float y)> onTouch_;
173 };
174 } // namespace NativeModule
175 
176 #endif // MYAPPLICATION_ARKUINODE_H