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 #include "core/pipeline/base/element_register.h" 32 33 namespace OHOS::Ace { 34 35 class Element; 36 class ComposedElement; 37 class SingleChild; 38 39 using ElementFunction = std::function<void(const RefPtr<ComposedElement>&)>; 40 41 enum class UpdateType { 42 STYLE, 43 ATTR, 44 EVENT, 45 METHOD, 46 REBUILD, 47 ALL, 48 }; 49 50 enum class UpdateRenderType : uint32_t { 51 NONE = 0, 52 LAYOUT = 1, 53 PAINT = 1 << 1, 54 EVENT = 1 << 2 55 }; 56 57 enum class HoverAnimationType : int32_t { 58 NONE, 59 OPACITY, 60 SCALE, 61 BOARD, 62 AUTO, 63 UNKNOWN, 64 }; 65 66 // Component is a read-only structure, represent the basic information how to display it. 67 class ACE_EXPORT Component : public virtual AceType { 68 DECLARE_ACE_TYPE(Component, AceType); 69 70 public: 71 Component(); 72 ~Component() override; 73 74 virtual RefPtr<Element> CreateElement() = 0; 75 GetTextDirection()76 TextDirection GetTextDirection() const 77 { 78 return direction_; 79 } 80 SetTextDirection(TextDirection direction)81 virtual void SetTextDirection(TextDirection direction) 82 { 83 direction_ = direction; 84 } 85 SetInspectorTag(std::string inspectorTag)86 virtual void SetInspectorTag(std::string inspectorTag) 87 { 88 inspectorTag_ = inspectorTag; 89 } 90 GetInspectorTag()91 virtual std::string GetInspectorTag() 92 { 93 return inspectorTag_; 94 } 95 GetUpdateType()96 UpdateType GetUpdateType() const 97 { 98 return updateType_; 99 } 100 SetUpdateType(UpdateType updateType)101 virtual void SetUpdateType(UpdateType updateType) 102 { 103 updateType_ = updateType; 104 } 105 SetParent(const WeakPtr<Component> & parent)106 void SetParent(const WeakPtr<Component>& parent) 107 { 108 parent_ = parent; 109 } 110 GetParent()111 const WeakPtr<Component>& GetParent() const 112 { 113 return parent_; 114 } 115 IsDisabledStatus()116 bool IsDisabledStatus() const 117 { 118 return disabledStatus_; 119 } SetDisabledStatus(bool disabledStatus)120 virtual void SetDisabledStatus(bool disabledStatus) 121 { 122 disabledStatus_ = disabledStatus; 123 } IsTouchable()124 bool IsTouchable() const 125 { 126 return touchable_; 127 } SetTouchable(bool touchable)128 void SetTouchable(bool touchable) 129 { 130 touchable_ = touchable; 131 } 132 IsSetFocusOnTouch()133 bool IsSetFocusOnTouch() const 134 { 135 return isFocusOnTouch_.has_value(); 136 } IsFocusOnTouch()137 bool IsFocusOnTouch() const 138 { 139 return isFocusOnTouch_.value(); 140 } SetIsFocusOnTouch(bool isFocusOnTouch)141 void SetIsFocusOnTouch(bool isFocusOnTouch) 142 { 143 isFocusOnTouch_ = isFocusOnTouch; 144 } 145 146 void SetRetakeId(int32_t retakeId); 147 int32_t GetRetakeId() const; 148 HasElementFunction()149 virtual bool HasElementFunction() 150 { 151 return false; 152 } 153 SetElementFunction(ElementFunction && func)154 virtual void SetElementFunction(ElementFunction&& func) {} CallElementFunction(const RefPtr<Element> & element)155 virtual void CallElementFunction(const RefPtr<Element>& element) {} 156 IsStatic()157 bool IsStatic() 158 { 159 return static_; 160 } 161 SetStatic()162 void SetStatic() 163 { 164 static_ = true; 165 } 166 AddAnimatable(AnimatableType type,const RefPtr<PropertyAnimation> animation)167 void AddAnimatable(AnimatableType type, const RefPtr<PropertyAnimation> animation) 168 { 169 propAnimations_[type] = animation; 170 } 171 ClearAnimatables()172 void ClearAnimatables() 173 { 174 propAnimations_.clear(); 175 } 176 GetAnimatables()177 const PropAnimationMap& GetAnimatables() const 178 { 179 return propAnimations_; 180 } 181 SetOnAppearEventId(const EventMarker & appearEventId)182 void SetOnAppearEventId(const EventMarker& appearEventId) 183 { 184 appearEventId_ = appearEventId; 185 } 186 GetAppearEventMarker()187 const EventMarker& GetAppearEventMarker() const 188 { 189 return appearEventId_; 190 } 191 SetOnDisappearEventId(const EventMarker & disappearEventId)192 void SetOnDisappearEventId(const EventMarker& disappearEventId) 193 { 194 disappearEventId_ = disappearEventId; 195 } 196 GetDisappearEventMarker()197 const EventMarker& GetDisappearEventMarker() const 198 { 199 return disappearEventId_; 200 } 201 OnWrap()202 virtual void OnWrap() {} 203 IsHeadComponent()204 bool IsHeadComponent() const { return isHeadComponent_; } IsTailComponent()205 bool IsTailComponent() const { return isTailComponent_; } 206 static void MergeRSNode(const std::vector<RefPtr<Component>>& components); 207 static void MergeRSNode(const std::vector<RefPtr<SingleChild>>& components); 208 static void MergeRSNode(const std::vector<RefPtr<SingleChild>>& components, const RefPtr<Component>& mainComponent); 209 static void MergeRSNode(const RefPtr<Component>& head, const RefPtr<Component>& tail); 210 static void MergeRSNode(const RefPtr<Component>& standaloneNode); 211 static void ExtendRSNode(const RefPtr<Component>& newHead, const RefPtr<Component>& prevHead); MarkUseExternalRSNode(bool flag)212 void MarkUseExternalRSNode(bool flag) 213 { 214 useExternalRSNode_ = flag; 215 } UseExternalRSNode()216 bool UseExternalRSNode() const 217 { 218 return useExternalRSNode_; 219 } 220 Compare(const RefPtr<Component> & component)221 virtual uint32_t Compare(const RefPtr<Component>& component) const 222 { 223 return static_cast<uint32_t>(UpdateRenderType::LAYOUT); 224 } 225 SetIgnoreInspector(bool ignoreInspector)226 void SetIgnoreInspector(bool ignoreInspector) 227 { 228 ignoreInspector_ = ignoreInspector; 229 } 230 IsIgnoreInspector()231 bool IsIgnoreInspector() const 232 { 233 return ignoreInspector_; 234 } 235 GetEventExtensions()236 RefPtr<V2::EventExtensions> GetEventExtensions() 237 { 238 if (!eventExtensions_) { 239 eventExtensions_ = MakeRefPtr<V2::EventExtensions>(); 240 } 241 return eventExtensions_; 242 } 243 HasEventExtensions()244 bool HasEventExtensions() const 245 { 246 return eventExtensions_; 247 } 248 GetInspectorKey()249 const std::string& GetInspectorKey() const 250 { 251 return inspectorKey_; 252 } SetInspectorKey(const std::string & inspectorKey)253 void SetInspectorKey(const std::string& inspectorKey) 254 { 255 inspectorKey_ = inspectorKey; 256 } 257 GetRestoreId()258 int32_t GetRestoreId() const 259 { 260 return restoreId_; 261 } 262 SetRestoreId(int32_t restoreId)263 void SetRestoreId(int32_t restoreId) 264 { 265 restoreId_ = restoreId; 266 } 267 SetIsFirst(bool flag)268 void SetIsFirst(bool flag) 269 { 270 isFirstNode_ = flag; 271 } 272 IsFirstNode()273 bool IsFirstNode() 274 { 275 return isFirstNode_; 276 } 277 278 /* 279 * Assign unique elmtId to Component 280 * will move to the Element when updating the Element 281 * typical usage is from the JSXYZ::Create() function 282 */ 283 void AssignUniqueElementId(ElementIdType elmtId); 284 GetElementId()285 ElementIdType GetElementId() const 286 { 287 return elmtId_; 288 } 289 SetElementId(ElementIdType elmtId)290 void SetElementId(ElementIdType elmtId) 291 { 292 elmtId_ = elmtId; 293 } 294 SetHitTestMode(HitTestMode hitTestMode)295 void SetHitTestMode(HitTestMode hitTestMode) 296 { 297 hitTestMode_ = hitTestMode; 298 } 299 GetHitTestMode()300 HitTestMode GetHitTestMode() const 301 { 302 return hitTestMode_; 303 } 304 305 protected: 306 TextDirection direction_ = TextDirection::LTR; 307 bool isFirstNode_ = false; 308 309 private: 310 bool ignoreInspector_ = false; 311 PropAnimationMap propAnimations_; 312 UpdateType updateType_ = UpdateType::ALL; 313 WeakPtr<Component> parent_; 314 bool disabledStatus_ = false; 315 bool touchable_ = true; 316 std::optional<bool> isFocusOnTouch_; 317 static std::atomic<int32_t> key_; 318 // Set the id for the component to identify the unique component. 319 int32_t retakeId_ = 0; 320 bool static_ = false; 321 std::string inspectorKey_; 322 // eventMarker used to handle component detach and attach to the render tree. 323 EventMarker appearEventId_; 324 EventMarker disappearEventId_; 325 RefPtr<V2::EventExtensions> eventExtensions_; 326 bool isHeadComponent_ = false; 327 bool isTailComponent_ = false; 328 bool useExternalRSNode_ = false; 329 std::string inspectorTag_; 330 int32_t restoreId_ = -1; 331 HitTestMode hitTestMode_ = HitTestMode::HTMDEFAULT; 332 333 ElementIdType elmtId_ = ElementRegister::UndefinedElementId; 334 }; 335 336 } // namespace OHOS::Ace 337 338 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPONENT_H 339