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 #include "core/components_ng/base/extension_handler.h"
17 
18 #include "core/components_ng/base/frame_node.h"
19 
20 namespace OHOS::Ace::NG {
21 
Create(const LayoutConstraintF & layoutConstraintF)22 ExtensionLayoutConstraint ExtensionLayoutConstraint::Create(const LayoutConstraintF& layoutConstraintF)
23 {
24     auto maxWidth = floor(layoutConstraintF.maxSize.Width());
25     auto maxHeight = floor(layoutConstraintF.maxSize.Height());
26     auto minWidth = floor(layoutConstraintF.minSize.Width());
27     auto minHeight = floor(layoutConstraintF.minSize.Height());
28     if (layoutConstraintF.selfIdealSize.Width().has_value()) {
29         maxWidth = floor(layoutConstraintF.selfIdealSize.Width().value());
30         minWidth = maxWidth;
31     }
32     if (layoutConstraintF.selfIdealSize.Height().has_value()) {
33         maxHeight = floor(layoutConstraintF.selfIdealSize.Height().value());
34         minHeight = maxHeight;
35     }
36     auto parentIdealWidth = floor(layoutConstraintF.percentReference.Width());
37     auto parentIdealHeight = floor(layoutConstraintF.percentReference.Height());
38     return { maxWidth, minWidth, maxHeight, minHeight, parentIdealWidth, parentIdealHeight };
39 }
40 
Measure(const ExtensionLayoutConstraint & layoutConstraint)41 void ExtensionHandler::Measure(const ExtensionLayoutConstraint& layoutConstraint)
42 {
43     OnMeasure(layoutConstraint);
44 }
45 
Layout(int32_t width,int32_t height,int32_t positionX,int32_t positionY)46 void ExtensionHandler::Layout(int32_t width, int32_t height, int32_t positionX, int32_t positionY)
47 {
48     OnLayout(width, height, positionX, positionY);
49 }
50 
Draw(DrawingContext & context)51 void ExtensionHandler::Draw(DrawingContext& context)
52 {
53     needRender_ = false;
54     OnDraw(context);
55 }
56 
ForegroundDraw(DrawingContext & context)57 void ExtensionHandler::ForegroundDraw(DrawingContext& context)
58 {
59     needRender_ = false;
60     OnForegroundDraw(context);
61 }
62 
OverlayDraw(DrawingContext & context)63 void ExtensionHandler::OverlayDraw(DrawingContext& context)
64 {
65     needRender_ = false;
66     OnOverlayDraw(context);
67 }
68 
69 // 调用封装内部原始布局,绘制方法。
InnerMeasure(const ExtensionLayoutConstraint & layoutConstraint)70 void ExtensionHandler::InnerMeasure(const ExtensionLayoutConstraint& layoutConstraint)
71 {
72     if (innerMeasureImpl_) {
73         innerMeasureImpl_(layoutConstraint);
74     }
75 }
76 
InnerLayout(int32_t width,int32_t height,int32_t positionX,int32_t positionY)77 void ExtensionHandler::InnerLayout(int32_t width, int32_t height, int32_t positionX, int32_t positionY)
78 {
79     if (innerLayoutImpl_) {
80         innerLayoutImpl_(width, height, positionX, positionY);
81     }
82 }
83 
InnerDraw(DrawingContext & context)84 void ExtensionHandler::InnerDraw(DrawingContext& context)
85 {
86     if (innerDrawImpl_) {
87         innerDrawImpl_(context);
88     }
89 }
90 
InnerForegroundDraw(DrawingContext & context)91 void ExtensionHandler::InnerForegroundDraw(DrawingContext& context)
92 {
93     if (innerForegroundDrawImpl_) {
94         innerForegroundDrawImpl_(context);
95     }
96 }
97 
InnerOverlayDraw(DrawingContext & context)98 void ExtensionHandler::InnerOverlayDraw(DrawingContext& context)
99 {
100     if (innerOverlayDrawImpl_) {
101         innerOverlayDrawImpl_(context);
102     }
103 }
104 
OnMeasure(const ExtensionLayoutConstraint & layoutConstraint)105 void ExtensionHandler::OnMeasure(const ExtensionLayoutConstraint& layoutConstraint)
106 {
107     InnerMeasure(layoutConstraint);
108 }
109 
OnLayout(int32_t width,int32_t height,int32_t positionX,int32_t positionY)110 void ExtensionHandler::OnLayout(int32_t width, int32_t height, int32_t positionX, int32_t positionY)
111 {
112     InnerLayout(width, height, positionX, positionY);
113 }
114 
OnForegroundDraw(DrawingContext & context)115 void ExtensionHandler::OnForegroundDraw(DrawingContext& context)
116 {
117     InnerForegroundDraw(context);
118 }
119 
OnDraw(DrawingContext & context)120 void ExtensionHandler::OnDraw(DrawingContext& context)
121 {
122     if (drawModifier_ && drawModifier_->drawBehindFunc) {
123         drawModifier_->drawBehindFunc(context);
124     }
125 
126     if (drawModifier_ && drawModifier_->drawContentFunc) {
127         drawModifier_->drawContentFunc(context);
128     } else {
129         InnerDraw(context);
130     }
131 
132     if (drawModifier_ && drawModifier_->drawFrontFunc) {
133         drawModifier_->drawFrontFunc(context);
134     }
135 }
136 
OnOverlayDraw(DrawingContext & context)137 void ExtensionHandler::OnOverlayDraw(DrawingContext& context)
138 {
139     InnerOverlayDraw(context);
140 }
141 
InvalidateRender()142 void ExtensionHandler::InvalidateRender()
143 {
144     if (invalidateRender_) {
145         invalidateRender_();
146     } else if (node_) {
147         node_->MarkNeedRenderOnly();
148     }
149     needRender_ = true;
150 }
151 
OverlayRender()152 void ExtensionHandler::OverlayRender()
153 {
154     if (overlayRender_) {
155         overlayRender_();
156     } else if (node_) {
157         node_->MarkNeedRenderOnly();
158     }
159     needRender_ = true;
160 }
161 
ForegroundRender()162 void ExtensionHandler::ForegroundRender()
163 {
164     if (foreGroundRender_) {
165         foreGroundRender_();
166     } else if (node_) {
167         node_->MarkNeedRenderOnly();
168     }
169     needRender_ = true;
170 }
171 
172 } // namespace OHOS::Ace::NG
173