1 /*
2  * Copyright (c) 2024 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_NG_BASE_EXTENSION_HANDLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_EXTENSION_HANDLER_H
18 
19 #include <cmath>
20 #include <cstdint>
21 #include <functional>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/memory/referenced.h"
25 #include "base/utils/macros.h"
26 #include "core/components_ng/base/modifier.h"
27 #include "core/components_ng/property/layout_constraint.h"
28 
29 namespace OHOS::Ace::NG {
30 struct ExtensionLayoutConstraint {
31     int32_t maxWidth { 0 };
32     int32_t minWidth { 0 };
33     int32_t maxHeight { 0 };
34     int32_t minHeight { 0 };
35     int32_t parentIdealWidth { 0 };  // 设置百分比基准
36     int32_t parentIdealHeight { 0 }; // 设置百分比基准
37 
38     static ExtensionLayoutConstraint Create(const LayoutConstraintF& layoutConstraintF);
39 };
40 
41 class FrameNode;
42 
43 class ACE_EXPORT ExtensionHandler : public virtual AceType {
44     DECLARE_ACE_TYPE(ExtensionHandler, AceType);
45 
46 public:
47     void Measure(const ExtensionLayoutConstraint& layoutConstraint);
48     void Layout(int32_t width, int32_t height, int32_t positionX, int32_t positionY);
49     void Draw(DrawingContext& context);
50     void ForegroundDraw(DrawingContext& context);
51     void OverlayDraw(DrawingContext& context);
52 
53     // 调用封装内部原始布局,绘制方法。
54     void InnerMeasure(const ExtensionLayoutConstraint& layoutConstraint);
55     void InnerLayout(int32_t width, int32_t height, int32_t positionX, int32_t positionY);
56     void InnerDraw(DrawingContext& context);
57     void InnerForegroundDraw(DrawingContext& context);
58     void InnerOverlayDraw(DrawingContext& context);
59 
SetInnerMeasureImpl(std::function<void (const ExtensionLayoutConstraint &)> && impl)60     void SetInnerMeasureImpl(std::function<void(const ExtensionLayoutConstraint&)>&& impl)
61     {
62         innerMeasureImpl_ = std::move(impl);
63     }
64 
SetInnerLayoutImpl(std::function<void (int32_t,int32_t,int32_t,int32_t)> && impl)65     void SetInnerLayoutImpl(std::function<void(int32_t, int32_t, int32_t, int32_t)>&& impl)
66     {
67         innerLayoutImpl_ = std::move(impl);
68     }
69 
SetInnerDrawImpl(std::function<void (DrawingContext & Context)> && impl)70     void SetInnerDrawImpl(std::function<void(DrawingContext& Context)>&& impl)
71     {
72         innerDrawImpl_ = std::move(impl);
73     }
74 
SetInnerForegroundDrawImpl(std::function<void (DrawingContext & Context)> && impl)75     void SetInnerForegroundDrawImpl(std::function<void(DrawingContext& Context)>&& impl)
76     {
77         innerForegroundDrawImpl_ = std::move(impl);
78     }
79 
SetInnerOverlayDrawImpl(std::function<void (DrawingContext & Context)> && impl)80     void SetInnerOverlayDrawImpl(std::function<void(DrawingContext& Context)>&& impl)
81     {
82         innerOverlayDrawImpl_ = std::move(impl);
83     }
84 
85     void InvalidateRender();
86 
87     void OverlayRender();
88 
89     void ForegroundRender();
90 
SetInvalidateRenderImpl(std::function<void ()> && impl)91     void SetInvalidateRenderImpl(std::function<void()>&& impl)
92     {
93         invalidateRender_ = std::move(impl);
94     }
95 
SetOverlayRenderImpl(std::function<void ()> && impl)96     void SetOverlayRenderImpl(std::function<void()>&& impl)
97     {
98         overlayRender_ = std::move(impl);
99     }
100 
SetForeGroundRenderImpl(std::function<void ()> && impl)101     void SetForeGroundRenderImpl(std::function<void()>&& impl)
102     {
103         foreGroundRender_ = std::move(impl);
104     }
105 
SetDrawModifier(const RefPtr<NG::DrawModifier> & drawModifier)106     void SetDrawModifier(const RefPtr<NG::DrawModifier>& drawModifier)
107     {
108         drawModifier_ = drawModifier;
109     }
110 
HasCustomerMeasure()111     virtual bool HasCustomerMeasure() const
112     {
113         return false;
114     }
115 
HasCustomerLayout()116     virtual bool HasCustomerLayout() const
117     {
118         return false;
119     }
120 
NeedRender()121     virtual bool NeedRender() const
122     {
123         return drawModifier_ || needRender_ ;
124     }
125 
ResetNeedRender()126     void ResetNeedRender()
127     {
128         needRender_  = false;
129     }
130 
HasDrawModifier()131     bool HasDrawModifier()
132     {
133         return drawModifier_;
134     }
135 
AttachFrameNode(FrameNode * node)136     void AttachFrameNode(FrameNode* node)
137     {
138         node_ = node;
139     }
140 
141 protected:
142     virtual void OnMeasure(const ExtensionLayoutConstraint& layoutConstraint);
143     virtual void OnLayout(int32_t width, int32_t height, int32_t positionX, int32_t positionY);
144     virtual void OnForegroundDraw(DrawingContext& context);
145     virtual void OnDraw(DrawingContext& context);
146     virtual void OnOverlayDraw(DrawingContext& context);
147 
148 private:
149     std::function<void(const ExtensionLayoutConstraint&)> innerMeasureImpl_;
150     std::function<void(int32_t, int32_t, int32_t, int32_t)> innerLayoutImpl_;
151     std::function<void(DrawingContext&)> innerDrawImpl_;
152     std::function<void(DrawingContext&)> innerForegroundDrawImpl_;
153     std::function<void(DrawingContext&)> innerOverlayDrawImpl_;
154     std::function<void()> invalidateRender_;
155     std::function<void()> overlayRender_;
156     std::function<void()> foreGroundRender_;
157     bool needRender_  = true;
158 
159     RefPtr<NG::DrawModifier> drawModifier_;
160     FrameNode* node_;
161 };
162 
163 } // namespace OHOS::Ace::NG
164 
165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_EXTENSION_HANDLER_H
166