• 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 ARKUI_CAPI_DEMO_COMPONENT_H
17 #define ARKUI_CAPI_DEMO_COMPONENT_H
18 
19 #include "common/Common.h"
20 
21 namespace ArkUICapiTest {
22 
23 class Component {
24 public:
25     explicit Component(ArkUI_NodeType type);
26     explicit Component(ArkUI_NodeHandle handle);
27     virtual ~Component() = default;
28 
29     int32_t setAttribute(ArkUI_NodeAttributeType attribute, const ArkUI_AttributeItem* item);
30     const ArkUI_AttributeItem* getAttribute(ArkUI_NodeAttributeType attribute);
31     int32_t resetAttribute(ArkUI_NodeAttributeType attribute);
32 
33     void SetWidth(float width);
34     void SetPercentWidth(float percent);
35     void SetHeight(float height);
36     void SetPercentHeight(float percent);
37     void SetLayoutRect(int32_t x, int32_t y, int32_t width, int32_t height);
38     void SetBackgroundColor(uint32_t color);
39     void SetBackgroundImage(const std::string& backgroundImage);
40     void SetBackgroundImageSizeWithStyle(int32_t imageSize);
41     void SetBorderRadius(float topLeft, float topRight, float bottomLeft, float bottomRight);
42     void SetPadding(float top, float right, float bottom, float left);
43     void SetPaddingPercent(float top, float right, float bottom, float left);
44     void SetMargin(float top, float right, float bottom, float left);
45     void SetMargin(float margin);
46     void SetMarginPercent(float top, float right, float bottom, float left);
47     void SetEnabled(int32_t enable);
48     void SetAlign(int32_t align);
49     void SetAlignRules(ArkUI_AlignmentRuleOption* option);
50     void SetAlignSelf(int32_t value);
51     void SetOpacity(float opacity);
52     void SetBorderWidth(float top, float right, float bottom, float left);
53     void SetBorderWidth(float borderWidth);
54     void SetBorderColor(uint32_t top, uint32_t right, uint32_t bottom, uint32_t left);
55     void SetBorderStyle(int32_t top, int32_t right, int32_t bottom, int32_t left);
56     void SetVisibility(int32_t visibility);
57     virtual void SetFontSize(float fontSize);
58     void SetId(const std::string& id);
59     void AddChild(const std::shared_ptr<Component>& child);
60     void RemoveChild(const std::shared_ptr<Component>& child);
61     void RemoveAll();
62     void InsertChild(const std::shared_ptr<Component>& child, int32_t index);
GetChildren()63     std::list<std::shared_ptr<Component>> GetChildren()
64     {
65         return children_;
66     }
67     std::shared_ptr<Component> GetPreviousSibling();
68     std::shared_ptr<Component> GetNextSibling();
69 
GetComponent()70     ArkUI_NodeHandle GetComponent()
71     {
72         return _component;
73     }
GetXComponent()74     OH_NativeXComponent* GetXComponent()
75     {
76         return _xcomponent;
77     }
GetNodeApi()78     ArkUI_NativeNodeAPI_1* GetNodeApi()
79     {
80         return _nodeAPI;
81     }
82     // 处理通用事件。
83     void RegisterOnClick(const std::function<void()>& onClick);
84     void RegisterOnTouch(const std::function<void(ArkUI_NodeEvent* event)>& onTouch);
85     void RegisterOnDisappear(const std::function<void()>& onDisappear);
86     void RegisterOnAppear(const std::function<void()>& onAppear);
87     void RegisterOnHover(const std::function<void(bool isHover)>& onHover);
88     void RegisterOnMouse(const std::function<void(ArkUI_NodeEvent* event)>& onMouse);
89     void RegisterOnTouchIntercept(const std::function<void(ArkUI_NodeEvent* event)>& onTouchIntercept);
90     void RegisterOnFocus(const std::function<void()>& onFocus);
91     void RegisterOnVisibleAreaChange(
92         const std::function<void(bool isVisible, float ratio)>& onVisibleAreaChange, std::vector<float> ratioArray);
93 
94     static void NodeEventReceiver(ArkUI_NodeEvent* event);
95     void ProcessNodeEvent(ArkUI_NodeEvent* event);
OnNodeEvent(ArkUI_NodeEvent * event)96     virtual void OnNodeEvent(ArkUI_NodeEvent* event) {}
97 
98 protected:
99     // 组件树操作的实现类对接。
OnAddChild(const std::shared_ptr<Component> & child)100     void OnAddChild(const std::shared_ptr<Component>& child)
101     {
102         _nodeAPI->addChild(_component, child->GetComponent());
103     }
OnRemoveChild(const std::shared_ptr<Component> & child)104     void OnRemoveChild(const std::shared_ptr<Component>& child)
105     {
106         _nodeAPI->removeChild(_component, child->GetComponent());
107     }
OnRemoveAll()108     void OnRemoveAll()
109     {
110         _nodeAPI->removeAllChildren(_component);
111     }
OnInsertChild(const std::shared_ptr<Component> & child,int32_t index)112     void OnInsertChild(const std::shared_ptr<Component>& child, int32_t index)
113     {
114         _nodeAPI->insertChildAt(_component, child->GetComponent(), index);
115     }
116 
117 protected:
118     std::unordered_map<ArkUI_NodeEventType, std::function<void(ArkUI_NodeEvent *)>> eventMap_;
119     std::list<std::shared_ptr<Component>> children_;
120     ArkUI_NodeHandle _component { nullptr };
121     OH_NativeXComponent* _xcomponent;
122     ArkUI_NativeNodeAPI_1* _nodeAPI { nullptr };
123 };
124 } // namespace ArkUICApiCapiTest
125 
126 #endif // ARKUI_CAPI_DEMO_COMPONENT_H
127