• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWENTY)) {
124             return needRender_ ;
125         }
126         return drawModifier_ || needRender_ ;
127     }
128 
ResetNeedRender()129     void ResetNeedRender()
130     {
131         needRender_  = false;
132     }
133 
HasDrawModifier()134     bool HasDrawModifier()
135     {
136         return drawModifier_;
137     }
138 
AttachFrameNode(FrameNode * node)139     void AttachFrameNode(FrameNode* node)
140     {
141         node_ = node;
142     }
143 
144 protected:
145     virtual void OnMeasure(const ExtensionLayoutConstraint& layoutConstraint);
146     virtual void OnLayout(int32_t width, int32_t height, int32_t positionX, int32_t positionY);
147     virtual void OnForegroundDraw(DrawingContext& context);
148     virtual void OnDraw(DrawingContext& context);
149     virtual void OnOverlayDraw(DrawingContext& context);
150 
151 private:
152     std::function<void(const ExtensionLayoutConstraint&)> innerMeasureImpl_;
153     std::function<void(int32_t, int32_t, int32_t, int32_t)> innerLayoutImpl_;
154     std::function<void(DrawingContext&)> innerDrawImpl_;
155     std::function<void(DrawingContext&)> innerForegroundDrawImpl_;
156     std::function<void(DrawingContext&)> innerOverlayDrawImpl_;
157     std::function<void()> invalidateRender_;
158     std::function<void()> overlayRender_;
159     std::function<void()> foreGroundRender_;
160     bool needRender_  = true;
161 
162     RefPtr<NG::DrawModifier> drawModifier_;
163     FrameNode* node_;
164 };
165 
166 } // namespace OHOS::Ace::NG
167 
168 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_EXTENSION_HANDLER_H
169