1 /* 2 * Copyright (c) 2021 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_COMPOSED_ELEMENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPOSED_ELEMENT_H 18 19 #include <string> 20 21 #include "base/utils/macros.h" 22 #include "core/pipeline/base/composed_component.h" 23 #include "core/pipeline/base/element.h" 24 #include "core/pipeline/base/render_element.h" 25 #include "core/pipeline/base/render_node.h" 26 27 namespace OHOS::Ace { 28 29 // ComposedElement just maintain a child element may have render node. 30 class ACE_EXPORT ComposedElement : public Element { 31 DECLARE_ACE_TYPE(ComposedElement, Element); 32 33 public: 34 using RenderFunction = std::function<RefPtr<Component>(const RefPtr<Component>&)>; 35 using RemoveFunction = std::function<void()>; 36 using PageTransitionFunction = std::function<RefPtr<Component>()>; 37 using ApplyFunction = std::function<void(const RefPtr<RenderElement>&)>; 38 explicit ComposedElement(const ComposeId& id); 39 ~ComposedElement() override = default; 40 41 void Detached() override; 42 void Deactivate() override; 43 void PerformBuild() override; 44 void Update() override; 45 RefPtr<Element> UpdateChild(const RefPtr<Element>& child, const RefPtr<Component>& newComponent) final; 46 47 void Dump() override; 48 49 bool CanUpdate(const RefPtr<Component>& newComponent) override; 50 bool NeedUpdateWithComponent(const RefPtr<Component>& newComponent) override; 51 ApplyComposed(ApplyFunction && applyFunction)52 void ApplyComposed(ApplyFunction&& applyFunction) 53 { 54 applyFunction_ = std::move(applyFunction); 55 ApplyChildren(); 56 } 57 ApplyComposed(const ApplyFunction & applyFunction)58 void ApplyComposed(const ApplyFunction& applyFunction) 59 { 60 applyFunction_ = applyFunction; 61 ApplyChildren(); 62 } 63 GetId()64 const ComposeId& GetId() const 65 { 66 return id_; 67 } 68 CallRenderFunction(const RefPtr<Component> & component)69 RefPtr<Component> CallRenderFunction(const RefPtr<Component>& component) 70 { 71 if (renderFunction_) { 72 return renderFunction_(component); 73 } 74 return nullptr; 75 } 76 SetRenderFunction(RenderFunction && func)77 void SetRenderFunction(RenderFunction&& func) 78 { 79 renderFunction_ = std::move(func); 80 } 81 HasRenderFunction()82 bool HasRenderFunction() 83 { 84 return static_cast<bool>(renderFunction_); 85 } 86 CallRemoveFunction()87 void CallRemoveFunction() 88 { 89 if (removeFunction_) { 90 return removeFunction_(); 91 } 92 } 93 SetRemoveFunction(RemoveFunction && func)94 void SetRemoveFunction(RemoveFunction&& func) 95 { 96 removeFunction_ = std::move(func); 97 } 98 HasRemoveFunction()99 bool HasRemoveFunction() 100 { 101 return removeFunction_ != nullptr; 102 } 103 CallPageTransitionFunction()104 RefPtr<Component> CallPageTransitionFunction() 105 { 106 if (!pageTransitionFunction_) { 107 return nullptr; 108 } 109 return pageTransitionFunction_(); 110 } 111 SetPageTransitionFunction(PageTransitionFunction && func)112 void SetPageTransitionFunction(PageTransitionFunction&& func) 113 { 114 pageTransitionFunction_ = std::move(func); 115 } 116 HasPageTransitionFunction()117 bool HasPageTransitionFunction() 118 { 119 return !!pageTransitionFunction_; 120 } 121 GetName()122 const std::string& GetName() const 123 { 124 return name_; 125 } 126 127 void UnregisterForPartialUpdates() override; 128 129 protected: 130 virtual RefPtr<Component> BuildChild(); 131 void Apply(const RefPtr<Element>& child) override; 132 void UmountRender() override; 133 void ApplyChildren(); CountRenderNode()134 int32_t CountRenderNode() const override 135 { 136 return countRenderNode_; 137 } 138 139 ComposeId id_; 140 std::string name_; 141 bool addedToMap_ = false; 142 int32_t countRenderNode_ = -1; 143 RenderFunction renderFunction_; 144 RemoveFunction removeFunction_ = nullptr; 145 PageTransitionFunction pageTransitionFunction_; 146 147 ApplyFunction applyFunction_; 148 }; 149 150 } // namespace OHOS::Ace 151 152 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPOSED_ELEMENT_H 153