1 /*
2 * Copyright (c) 2023 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/pattern/security_component/security_component_layout_algorithm.h"
17
18 #include "base/log/ace_scoring_log.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/pattern/image/image_layout_property.h"
21 #include "core/components_ng/pattern/text/text_layout_property.h"
22 #include "core/components_ng/pattern/button/button_layout_property.h"
23 #include "core/components_ng/pattern/image/image_render_property.h"
24 #include "core/components_v2/inspector/inspector_constants.h"
25 #include "core/pipeline_ng/pipeline_context.h"
26
27 namespace {
28 constexpr float HALF = 2.0f;
29 }
30
31 namespace OHOS::Ace::NG {
GetChildWrapper(LayoutWrapper * layoutWrapper,const std::string & tag)32 RefPtr<LayoutWrapper> SecurityComponentLayoutAlgorithm::GetChildWrapper(LayoutWrapper* layoutWrapper,
33 const std::string& tag)
34 {
35 int32_t count = layoutWrapper->GetTotalChildCount();
36 for (int32_t i = 0; i < count; i++) {
37 auto childWrapper = layoutWrapper->GetOrCreateChildByIndex(i);
38 if (childWrapper == nullptr) {
39 continue;
40 }
41 if (childWrapper->GetHostTag() == tag) {
42 return childWrapper;
43 }
44 }
45 return nullptr;
46 }
47
UpdateChildPosition(LayoutWrapper * layoutWrapper,const std::string & tag,OffsetT<Dimension> & offset)48 void SecurityComponentLayoutAlgorithm::UpdateChildPosition(LayoutWrapper* layoutWrapper, const std::string& tag,
49 OffsetT<Dimension>& offset)
50 {
51 auto childWrapper = GetChildWrapper(layoutWrapper, tag);
52 CHECK_NULL_VOID(childWrapper);
53 auto childNode = childWrapper->GetHostNode();
54 CHECK_NULL_VOID(childNode);
55 auto childRenderContext = childNode->GetRenderContext();
56 CHECK_NULL_VOID(childRenderContext);
57 childRenderContext->UpdatePosition(offset);
58 }
59
CreateDefaultChildConstraint(RefPtr<SecurityComponentLayoutProperty> & securityComponentProperty)60 static LayoutConstraintF CreateDefaultChildConstraint(
61 RefPtr<SecurityComponentLayoutProperty>& securityComponentProperty)
62 {
63 auto constraint = securityComponentProperty->CreateChildConstraint();
64 SizeT<float> maxSize { Infinity<float>(), Infinity<float>() };
65 constraint.maxSize = maxSize;
66 return constraint;
67 }
68
MeasureIcon(LayoutWrapper * layoutWrapper,RefPtr<SecurityComponentLayoutProperty> & securityComponentProperty)69 void SecurityComponentLayoutAlgorithm::MeasureIcon(LayoutWrapper* layoutWrapper,
70 RefPtr<SecurityComponentLayoutProperty>& securityComponentProperty)
71 {
72 auto iconWrapper = GetChildWrapper(layoutWrapper, V2::IMAGE_ETS_TAG);
73 CHECK_NULL_VOID(iconWrapper);
74
75 auto iconConstraint = CreateDefaultChildConstraint(securityComponentProperty);
76 iconWrapper->Measure(std::optional<LayoutConstraintF>(iconConstraint));
77 iconSizeF_ = iconWrapper->GetGeometryNode()->GetFrameSize();
78 }
79
MeasureText(LayoutWrapper * layoutWrapper,RefPtr<SecurityComponentLayoutProperty> & securityComponentProperty)80 void SecurityComponentLayoutAlgorithm::MeasureText(LayoutWrapper* layoutWrapper,
81 RefPtr<SecurityComponentLayoutProperty>& securityComponentProperty)
82 {
83 auto textWrapper = GetChildWrapper(layoutWrapper, V2::TEXT_ETS_TAG);
84 CHECK_NULL_VOID(textWrapper);
85 auto textLayoutProperty = DynamicCast<TextLayoutProperty>(textWrapper->GetLayoutProperty());
86 CHECK_NULL_VOID(textLayoutProperty);
87 auto textConstraint = CreateDefaultChildConstraint(securityComponentProperty);
88 textWrapper->Measure(std::optional<LayoutConstraintF>(textConstraint));
89 textSizeF_ = textWrapper->GetGeometryNode()->GetFrameSize();
90 }
91
MeasureButton(LayoutWrapper * layoutWrapper,RefPtr<SecurityComponentLayoutProperty> & securityComponentProperty)92 void SecurityComponentLayoutAlgorithm::MeasureButton(LayoutWrapper* layoutWrapper,
93 RefPtr<SecurityComponentLayoutProperty>& securityComponentProperty)
94 {
95 auto buttonWrapper = GetChildWrapper(layoutWrapper, V2::BUTTON_ETS_TAG);
96 CHECK_NULL_VOID(buttonWrapper);
97 auto buttonLayoutProperty = DynamicCast<ButtonLayoutProperty>(buttonWrapper->GetLayoutProperty());
98 CHECK_NULL_VOID(buttonLayoutProperty);
99 auto buttonConstraint = CreateDefaultChildConstraint(securityComponentProperty);
100
101 if (buttonType_ == static_cast<int32_t>(ButtonType::CIRCLE)) {
102 buttonConstraint.selfIdealSize.SetSize(SizeF(std::max(componentWidth_, componentHeight_),
103 std::max(componentWidth_, componentHeight_)));
104 } else {
105 buttonConstraint.selfIdealSize.SetSize(SizeF(componentWidth_, componentHeight_));
106 }
107
108 buttonWrapper->Measure(std::optional<LayoutConstraintF>(buttonConstraint));
109 buttonSizeF_ = buttonWrapper->GetGeometryNode()->GetFrameSize();
110 }
111
FillPaddingParams(RefPtr<SecurityComponentLayoutProperty> & securityComponentProperty,SecurityComponentLayoutPaddingParams & res)112 void SecurityComponentLayoutAlgorithm::FillPaddingParams(
113 RefPtr<SecurityComponentLayoutProperty>& securityComponentProperty, SecurityComponentLayoutPaddingParams& res)
114 {
115 res.top = securityComponentProperty->GetBackgroundTopPadding().value().ConvertToPx();
116 res.right = securityComponentProperty->GetBackgroundRightPadding().value().ConvertToPx();
117 res.bottom = securityComponentProperty->GetBackgroundBottomPadding().value().ConvertToPx();
118 res.left = securityComponentProperty->GetBackgroundLeftPadding().value().ConvertToPx();
119 res.textIconSpace = securityComponentProperty->GetTextIconSpace().value().ConvertToPx();
120 }
121
UpdateVertical(OffsetT<Dimension> & offsetIcon,OffsetT<Dimension> & offsetText,const SecurityComponentLayoutPaddingParams & params)122 void SecurityComponentLayoutAlgorithm::UpdateVertical(OffsetT<Dimension>& offsetIcon,
123 OffsetT<Dimension>& offsetText, const SecurityComponentLayoutPaddingParams& params)
124 {
125 componentHeight_ = params.top + iconSizeF_.Height() + params.textIconSpace +
126 textSizeF_.Height() + params.bottom;
127 componentWidth_ = params.left +
128 ((iconSizeF_.Width() > textSizeF_.Width()) ? iconSizeF_.Width() : textSizeF_.Width()) + params.right;
129 offsetText = offsetIcon + OffsetT<Dimension>(Dimension(0.0F),
130 Dimension(iconSizeF_.Height() + params.textIconSpace));
131 if (iconSizeF_.Width() > textSizeF_.Width()) {
132 offsetText += OffsetT<Dimension>(Dimension((iconSizeF_.Width() - textSizeF_.Width()) / HALF), Dimension(0.0F));
133 } else {
134 offsetIcon += OffsetT<Dimension>(Dimension((textSizeF_.Width() - iconSizeF_.Width()) / HALF), Dimension(0.0F));
135 }
136 }
137
UpdateHorizontal(OffsetT<Dimension> & offsetIcon,OffsetT<Dimension> & offsetText,const SecurityComponentLayoutPaddingParams & params)138 void SecurityComponentLayoutAlgorithm::UpdateHorizontal(OffsetT<Dimension>& offsetIcon,
139 OffsetT<Dimension>& offsetText, const SecurityComponentLayoutPaddingParams& params)
140 {
141 componentHeight_ =
142 params.top + ((iconSizeF_.Height() > textSizeF_.Height()) ? iconSizeF_.Height() : textSizeF_.Height()) +
143 params.bottom;
144 componentWidth_ = params.left + iconSizeF_.Width() + params.textIconSpace + textSizeF_.Width() + params.right;
145 offsetText = offsetIcon +
146 OffsetT<Dimension>(Dimension(iconSizeF_.Width() + params.textIconSpace), Dimension(0.0F));
147 if (iconSizeF_.Height() > textSizeF_.Height()) {
148 offsetText +=
149 OffsetT<Dimension>(Dimension(0.0F), Dimension((iconSizeF_.Height() - textSizeF_.Height()) / HALF));
150 } else {
151 offsetIcon +=
152 OffsetT<Dimension>(Dimension(0.0F), Dimension((textSizeF_.Height() - iconSizeF_.Height()) / HALF));
153 }
154 }
155
UpdateCircleBackground(OffsetT<Dimension> & offsetIcon,OffsetT<Dimension> & offsetText)156 void SecurityComponentLayoutAlgorithm::UpdateCircleBackground(OffsetT<Dimension>& offsetIcon,
157 OffsetT<Dimension>& offsetText)
158 {
159 if (componentHeight_ > componentWidth_) {
160 OffsetT<Dimension> rightMove(Dimension((componentHeight_ - componentWidth_) / HALF), Dimension(0.0F));
161 offsetIcon += rightMove;
162 offsetText += rightMove;
163 componentWidth_ = componentHeight_;
164 } else {
165 OffsetT<Dimension> downMove(Dimension(0.0F), Dimension((componentWidth_ - componentHeight_) / HALF));
166 offsetIcon += downMove;
167 offsetText += downMove;
168 componentHeight_ = componentWidth_;
169 }
170 }
171
UpdateFrameMeasure(LayoutWrapper * layoutWrapper,RefPtr<SecurityComponentLayoutProperty> & securityComponentProperty)172 void SecurityComponentLayoutAlgorithm::UpdateFrameMeasure(LayoutWrapper* layoutWrapper,
173 RefPtr<SecurityComponentLayoutProperty>& securityComponentProperty)
174 {
175 SecurityComponentLayoutPaddingParams params;
176 FillPaddingParams(securityComponentProperty, params);
177
178 OffsetT<Dimension> offsetIcon = OffsetT<Dimension>(Dimension(params.left), Dimension(params.top));
179 OffsetT<Dimension> offsetText = OffsetT<Dimension>(Dimension(params.left), Dimension(params.top));
180 if (securityComponentProperty->GetTextIconLayoutDirection().value() ==
181 SecurityComponentLayoutDirection::VERTICAL) {
182 UpdateVertical(offsetIcon, offsetText, params);
183 } else {
184 UpdateHorizontal(offsetIcon, offsetText, params);
185 }
186
187 if (buttonType_ == static_cast<int32_t>(ButtonType::CIRCLE)) {
188 UpdateCircleBackground(offsetIcon, offsetText);
189 }
190 UpdateChildPosition(layoutWrapper, V2::IMAGE_ETS_TAG, offsetIcon);
191 UpdateChildPosition(layoutWrapper, V2::TEXT_ETS_TAG, offsetText);
192 }
193
InitLayoutParams(RefPtr<SecurityComponentLayoutProperty> & property)194 void SecurityComponentLayoutAlgorithm::InitLayoutParams(RefPtr<SecurityComponentLayoutProperty>& property)
195 {
196 componentWidth_ = 0.0F;
197 componentHeight_ = 0.0F;
198 iconSizeF_.Reset();
199 textSizeF_.Reset();
200 buttonSizeF_.Reset();
201 buttonType_ = property->GetBackgroundType().value();
202 }
203
Measure(LayoutWrapper * layoutWrapper)204 void SecurityComponentLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
205 {
206 auto securityComponentLayoutProperty =
207 DynamicCast<SecurityComponentLayoutProperty>(layoutWrapper->GetLayoutProperty());
208 CHECK_NULL_VOID(securityComponentLayoutProperty);
209
210 InitLayoutParams(securityComponentLayoutProperty);
211 if (securityComponentLayoutProperty->GetIconStyle().value() !=
212 static_cast<int32_t>(SecurityComponentIconStyle::ICON_NULL)) {
213 MeasureIcon(layoutWrapper, securityComponentLayoutProperty);
214 }
215
216 if (securityComponentLayoutProperty->GetSecurityComponentDescription().value() !=
217 static_cast<int32_t>(SecurityComponentDescription::TEXT_NULL)) {
218 MeasureText(layoutWrapper, securityComponentLayoutProperty);
219 }
220
221 UpdateFrameMeasure(layoutWrapper, securityComponentLayoutProperty);
222 MeasureButton(layoutWrapper, securityComponentLayoutProperty);
223 componentWidth_ = buttonSizeF_.Width();
224 componentHeight_ = buttonSizeF_.Height();
225
226 LOGD("security components size %{public}f %{public}f icon %{public}f %{public}f text %{public}f %{public}f",
227 componentWidth_, componentHeight_, iconSizeF_.Width(),
228 iconSizeF_.Height(), textSizeF_.Width(), textSizeF_.Height());
229 layoutWrapper->GetGeometryNode()->SetFrameSize(SizeF(componentWidth_, componentHeight_));
230 }
231 } // namespace OHOS::Ace::NG
232