• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPONENT_H
18 
19 #include <array>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <utility>
24 
25 #include "base/memory/ace_type.h"
26 #include "base/memory/referenced.h"
27 #include "core/animation/property_animation.h"
28 #include "core/components/common/layout/constants.h"
29 #include "core/components_v2/extensions/events/event_extensions.h"
30 #include "core/event/ace_event_handler.h"
31 
32 namespace OHOS::Ace {
33 
34 class Element;
35 class ComposedElement;
36 class SingleChild;
37 
38 using ElementFunction = std::function<void(const RefPtr<ComposedElement>&)>;
39 
40 enum class UpdateType {
41     STYLE,
42     ATTR,
43     EVENT,
44     METHOD,
45     REBUILD,
46     ALL,
47 };
48 
49 enum class UpdateRenderType : uint32_t {
50     NONE = 0,
51     LAYOUT = 1,
52     PAINT = 1 << 1,
53     EVENT = 1 << 2
54 };
55 
56 enum class HoverAnimationType : int32_t {
57     NONE,
58     OPACITY,
59     SCALE,
60     BOARD,
61     AUTO,
62 };
63 
64 // Component is a read-only structure, represent the basic information how to display it.
65 class ACE_EXPORT Component : public virtual AceType {
66     DECLARE_ACE_TYPE(Component, AceType);
67 
68 public:
69     Component();
70     ~Component() override;
71 
72     virtual RefPtr<Element> CreateElement() = 0;
73 
GetTextDirection()74     TextDirection GetTextDirection() const
75     {
76         return direction_;
77     }
78 
SetTextDirection(TextDirection direction)79     virtual void SetTextDirection(TextDirection direction)
80     {
81         direction_ = direction;
82     }
83 
SetInspectorTag(std::string inspectorTag)84     virtual void SetInspectorTag(std::string inspectorTag)
85     {
86         inspectorTag_ = inspectorTag;
87     }
88 
GetInspectorTag()89     virtual std::string GetInspectorTag()
90     {
91         return inspectorTag_;
92     }
93 
GetUpdateType()94     UpdateType GetUpdateType() const
95     {
96         return updateType_;
97     }
98 
SetUpdateType(UpdateType updateType)99     virtual void SetUpdateType(UpdateType updateType)
100     {
101         updateType_ = updateType;
102     }
103 
SetParent(const WeakPtr<Component> & parent)104     void SetParent(const WeakPtr<Component>& parent)
105     {
106         parent_ = parent;
107     }
108 
GetParent()109     const WeakPtr<Component>& GetParent() const
110     {
111         return parent_;
112     }
113 
IsDisabledStatus()114     bool IsDisabledStatus() const
115     {
116         return disabledStatus_;
117     }
SetDisabledStatus(bool disabledStatus)118     virtual void SetDisabledStatus(bool disabledStatus)
119     {
120         disabledStatus_ = disabledStatus;
121     }
IsTouchable()122     bool IsTouchable() const
123     {
124         return touchable_;
125     }
SetTouchable(bool touchable)126     void SetTouchable(bool touchable)
127     {
128         touchable_ = touchable;
129     }
130 
131     void SetRetakeId(int32_t retakeId);
132     int32_t GetRetakeId() const;
133 
HasElementFunction()134     virtual bool HasElementFunction()
135     {
136         return false;
137     }
138 
SetElementFunction(ElementFunction && func)139     virtual void SetElementFunction(ElementFunction&& func) {}
CallElementFunction(const RefPtr<Element> & element)140     virtual void CallElementFunction(const RefPtr<Element>& element) {}
141 
IsStatic()142     bool IsStatic()
143     {
144         return static_;
145     }
146 
SetStatic()147     void SetStatic()
148     {
149         static_ = true;
150     }
151 
AddAnimatable(AnimatableType type,const RefPtr<PropertyAnimation> animation)152     void AddAnimatable(AnimatableType type, const RefPtr<PropertyAnimation> animation)
153     {
154         propAnimations_[type] = animation;
155     }
156 
ClearAnimatables()157     void ClearAnimatables()
158     {
159         propAnimations_.clear();
160     }
161 
GetAnimatables()162     const PropAnimationMap& GetAnimatables() const
163     {
164         return propAnimations_;
165     }
166 
SetOnAppearEventId(const EventMarker & appearEventId)167     void SetOnAppearEventId(const EventMarker& appearEventId)
168     {
169         appearEventId_ = appearEventId;
170     }
171 
GetAppearEventMarker()172     const EventMarker& GetAppearEventMarker() const
173     {
174         return appearEventId_;
175     }
176 
SetOnDisappearEventId(const EventMarker & disappearEventId)177     void SetOnDisappearEventId(const EventMarker& disappearEventId)
178     {
179         disappearEventId_ = disappearEventId;
180     }
181 
GetDisappearEventMarker()182     const EventMarker& GetDisappearEventMarker() const
183     {
184         return disappearEventId_;
185     }
186 
OnWrap()187     virtual void OnWrap() {}
188 
IsHeadComponent()189     bool IsHeadComponent() const { return isHeadComponent_; }
IsTailComponent()190     bool IsTailComponent() const { return isTailComponent_; }
191     static void MergeRSNode(const std::vector<RefPtr<Component>>& components);
192     static void MergeRSNode(const std::vector<RefPtr<SingleChild>>& components);
193     static void MergeRSNode(const std::vector<RefPtr<SingleChild>>& components, const RefPtr<Component>& mainComponent);
194     static void MergeRSNode(const RefPtr<Component>& head, const RefPtr<Component>& tail);
195     static void MergeRSNode(const RefPtr<Component>& standaloneNode);
196     static void ExtendRSNode(const RefPtr<Component>& newHead, const RefPtr<Component>& prevHead);
197 
Compare(const RefPtr<Component> & component)198     virtual uint32_t Compare(const RefPtr<Component>& component) const
199     {
200         return static_cast<uint32_t>(UpdateRenderType::LAYOUT);
201     }
202 
SetIgnoreInspector(bool ignoreInspector)203     void SetIgnoreInspector(bool ignoreInspector)
204     {
205         ignoreInspector_ = ignoreInspector;
206     }
207 
IsIgnoreInspector()208     bool IsIgnoreInspector() const
209     {
210         return ignoreInspector_;
211     }
212 
GetEventExtensions()213     RefPtr<V2::EventExtensions> GetEventExtensions()
214     {
215         if (!eventExtensions_) {
216             eventExtensions_ = MakeRefPtr<V2::EventExtensions>();
217         }
218         return eventExtensions_;
219     }
220 
HasEventExtensions()221     bool HasEventExtensions() const
222     {
223         return eventExtensions_;
224     }
225 
GetInspectorKey()226     const std::string& GetInspectorKey() const
227     {
228         return inspectorKey_;
229     }
SetInspectorKey(const std::string & inspectorKey)230     void SetInspectorKey(const std::string& inspectorKey)
231     {
232         inspectorKey_ = inspectorKey;
233     }
234 
GetRestoreId()235     int32_t GetRestoreId() const
236     {
237         return restoreId_;
238     }
239 
SetRestoreId(int32_t restoreId)240     void SetRestoreId(int32_t restoreId)
241     {
242         restoreId_ = restoreId;
243     }
244 
245 protected:
246     TextDirection direction_ = TextDirection::LTR;
247 
248 private:
249     bool ignoreInspector_ = false;
250     PropAnimationMap propAnimations_;
251     UpdateType updateType_ = UpdateType::ALL;
252     WeakPtr<Component> parent_;
253     bool disabledStatus_ = false;
254     bool touchable_ = true;
255     static std::atomic<int32_t> key_;
256     // Set the id for the component to identify the unique component.
257     int32_t retakeId_ = 0;
258     bool static_ = false;
259     std::string inspectorKey_;
260     // eventMarker used to handle component detach and attach to the render tree.
261     EventMarker appearEventId_;
262     EventMarker disappearEventId_;
263     RefPtr<V2::EventExtensions> eventExtensions_;
264     bool isHeadComponent_ = false;
265     bool isTailComponent_ = false;
266     std::string inspectorTag_;
267     int32_t restoreId_ = -1;
268 };
269 
270 } // namespace OHOS::Ace
271 
272 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPONENT_H
273