• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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> & componnet)69     RefPtr<Component> CallRenderFunction(const RefPtr<Component>& componnet)
70     {
71         if (renderFunction_) {
72             return renderFunction_(componnet);
73         }
74         return nullptr;
75     }
76 
SetRenderFunction(RenderFunction && func)77     void SetRenderFunction(RenderFunction&& func)
78     {
79         renderFunction_ = std::move(func);
80     }
81 
SetRemoveFunction(RemoveFunction && func)82     void SetRemoveFunction(RemoveFunction&& func)
83     {
84         removeFunction_ = std::move(func);
85     }
86 
HasRenderFunction()87     bool HasRenderFunction()
88     {
89         if (renderFunction_) {
90             return true;
91         }
92         return false;
93     }
94 
CallPageTransitionFunction()95     RefPtr<Component> CallPageTransitionFunction()
96     {
97         if (!pageTransitionFunction_) {
98             return nullptr;
99         }
100         return pageTransitionFunction_();
101     }
102 
SetPageTransitionFunction(PageTransitionFunction && func)103     void SetPageTransitionFunction(PageTransitionFunction&& func)
104     {
105         pageTransitionFunction_ = std::move(func);
106     }
107 
HasPageTransitionFunction()108     bool HasPageTransitionFunction()
109     {
110         return !!pageTransitionFunction_;
111     }
112 
113     void UnregisterForElementProxy() override;
114 
115 protected:
116     virtual RefPtr<Component> BuildChild();
117     void Apply(const RefPtr<Element>& child) override;
118     void UmountRender() override;
119     void ApplyChildren();
CountRenderNode()120     int32_t CountRenderNode() const override
121     {
122         return countRenderNode_;
123     }
124 
125     ComposeId id_;
126     std::string name_;
127     bool addedToMap_ = false;
128     int32_t countRenderNode_ = -1;
129     RenderFunction renderFunction_;
130     RemoveFunction removeFunction_;
131     PageTransitionFunction pageTransitionFunction_;
132 
133     ApplyFunction applyFunction_;
134 };
135 
136 } // namespace OHOS::Ace
137 
138 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPOSED_ELEMENT_H
139