• 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_ARKUINODE_H
17 #define MYAPPLICATION_ARKUINODE_H
18 
19 #include "ArkUIBaseNode.h"
20 
21 namespace NativeModule {
22 
23 class ArkUINode : public ArkUIBaseNode {
24 public:
ArkUINode(ArkUI_NodeHandle handle)25     explicit ArkUINode(ArkUI_NodeHandle handle) : ArkUIBaseNode(handle) {}
26 
~ArkUINode()27     ~ArkUINode() override {}
28 
29     // Native相关通用属性调用封装。
SetWidth(float width)30     void SetWidth(float width)
31     {
32         ArkUI_NumberValue value[] = {{.f32 = width}};
33         ArkUI_AttributeItem item = {value, 1};
34         auto result = nativeModule_->setAttribute(handle_, NODE_WIDTH, &item);
35         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
36             OH_LOG_ERROR(LOG_APP, "ArkUINode SetWidth Failed %{public}d", result);
37         }
38     }
SetPercentWidth(float percent)39     void SetPercentWidth(float percent)
40     {
41         ArkUI_NumberValue value[] = {{.f32 = percent}};
42         ArkUI_AttributeItem item = {value, 1};
43         auto result = nativeModule_->setAttribute(handle_, NODE_WIDTH_PERCENT, &item);
44         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
45             OH_LOG_ERROR(LOG_APP, "ArkUINode SetPercentWidth Failed %{public}d", result);
46         }
47     }
SetHeight(float height)48     void SetHeight(float height)
49     {
50         ArkUI_NumberValue value[] = {{.f32 = height}};
51         ArkUI_AttributeItem item = {value, 1};
52         auto result = nativeModule_->setAttribute(handle_, NODE_HEIGHT, &item);
53         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
54             OH_LOG_ERROR(LOG_APP, "ArkUINode SetHeight Failed %{public}d", result);
55         }
56     }
SetPercentHeight(float percent)57     void SetPercentHeight(float percent)
58     {
59         ArkUI_NumberValue value[] = {{.f32 = percent}};
60         ArkUI_AttributeItem item = {value, 1};
61         auto result = nativeModule_->setAttribute(handle_, NODE_HEIGHT_PERCENT, &item);
62         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
63             OH_LOG_ERROR(LOG_APP, "ArkUINode SetPercentHeight Failed %{public}d", result);
64         }
65     }
SetPosition(float x,float y)66     void SetPosition(float x, float y)
67     {
68         ArkUI_NumberValue value[] = {{.f32 = x}, {.f32 = y}};
69         ArkUI_AttributeItem item = {value, 2};
70         auto result = nativeModule_->setAttribute(handle_, NODE_POSITION, &item);
71         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
72             OH_LOG_ERROR(LOG_APP, "ArkUINode SetPosition Failed %{public}d", result);
73         }
74     }
SetBackgroundColor(uint32_t color)75     void SetBackgroundColor(uint32_t color)
76     {
77         ArkUI_NumberValue value[] = {{.u32 = color}};
78         ArkUI_AttributeItem item = {value, 1};
79         auto result = nativeModule_->setAttribute(handle_, NODE_BACKGROUND_COLOR, &item);
80         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
81             OH_LOG_ERROR(LOG_APP, "ArkUINode SetBackgroundColor Failed %{public}d", result);
82         }
83     }
SetForegroundColor(uint32_t color)84     void SetForegroundColor(uint32_t color)
85     {
86         ArkUI_NumberValue value[] = {{.u32 = color}};
87         ArkUI_AttributeItem item = {value, 1};
88         auto result = nativeModule_->setAttribute(handle_, NODE_FOREGROUND_COLOR, &item);
89         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
90             OH_LOG_ERROR(LOG_APP, "ArkUINode SetForegroundColor Failed %{public}d", result);
91         }
92     }
93 
SetBorderRadius(float borderRadius)94     void SetBorderRadius(float borderRadius)
95     {
96         ArkUI_NumberValue value[] = {{.f32 = borderRadius}};
97         ArkUI_AttributeItem item = {value, 1};
98         auto result = nativeModule_->setAttribute(handle_, NODE_BORDER_RADIUS, &item);
99         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
100             OH_LOG_ERROR(LOG_APP, "ArkUINode SetBorderRadius Failed %{public}d", result);
101         }
102     }
SetMargin(float top,float right,float bottom,float left)103     void SetMargin(float top, float right, float bottom, float left)
104     {
105         ArkUI_NumberValue value[] = {{.f32 = top}, {.f32 = right}, {.f32 = bottom}, {.f32 = left}};
106         ArkUI_AttributeItem item = {value, 4};
107         auto result = nativeModule_->setAttribute(handle_, NODE_MARGIN, &item);
108         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
109             OH_LOG_ERROR(LOG_APP, "ArkUINode SetMargin Failed %{public}d", result);
110         }
111     }
SetPadding(float top,float right,float bottom,float left)112     void SetPadding(float top, float right, float bottom, float left)
113     {
114         ArkUI_NumberValue value[] = {{.f32 = top}, {.f32 = right}, {.f32 = bottom}, {.f32 = left}};
115         ArkUI_AttributeItem item = {value, 4};
116         auto result = nativeModule_->setAttribute(handle_, NODE_PADDING, &item);
117         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
118             OH_LOG_ERROR(LOG_APP, "ArkUINode SetPadding Failed %{public}d", result);
119         }
120     }
SetClip(bool clip)121     void SetClip(bool clip)
122     {
123         ArkUI_NumberValue value[] = {{.i32 = clip}};
124         ArkUI_AttributeItem item = {value, 1};
125         auto result = nativeModule_->setAttribute(handle_, NODE_CLIP, &item);
126         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
127             OH_LOG_ERROR(LOG_APP, "ArkUINode SetClip Failed %{public}d", result);
128         }
129     }
130 
SetAlignment(ArkUI_Alignment alignment)131     void SetAlignment(ArkUI_Alignment alignment)
132     {
133         ArkUI_NumberValue value[] = {{.i32 = alignment}};
134         ArkUI_AttributeItem item = {value, 1};
135         auto result = nativeModule_->setAttribute(handle_, NODE_ALIGNMENT, &item);
136         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
137             OH_LOG_ERROR(LOG_APP, "ArkUINode SetAlignment Failed %{public}d", result);
138         }
139     }
140 
141 protected:
142     // 组件树操作的实现类对接。
OnAddChild(const std::shared_ptr<ArkUIBaseNode> & child)143     void OnAddChild(const std::shared_ptr<ArkUIBaseNode> &child) override
144     {
145         auto result = nativeModule_->addChild(handle_, child->GetHandle());
146         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
147             OH_LOG_ERROR(LOG_APP, "ArkUINode AddChild Failed %{public}d", result);
148         }
149     }
OnRemoveChild(const std::shared_ptr<ArkUIBaseNode> & child)150     void OnRemoveChild(const std::shared_ptr<ArkUIBaseNode> &child) override
151     {
152         auto result = nativeModule_->removeChild(handle_, child->GetHandle());
153         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
154             OH_LOG_ERROR(LOG_APP, "ArkUINode RemoveChild Failed %{public}d", result);
155         }
156     }
OnInsertChild(const std::shared_ptr<ArkUIBaseNode> & child,int32_t index)157     void OnInsertChild(const std::shared_ptr<ArkUIBaseNode> &child, int32_t index) override
158     {
159         auto result = nativeModule_->insertChildAt(handle_, child->GetHandle(), index);
160         if (result != ARKUI_ERROR_CODE_NO_ERROR) {
161             OH_LOG_ERROR(LOG_APP, "ArkUINode InsertChild Failed %{public}d", result);
162         }
163     }
164 };
165 } // namespace NativeModule
166 
167 #endif // MYAPPLICATION_ARKUINODE_H
168