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/text_field/text_input_response_area.h"
17
18 #include "base/geometry/dimension.h"
19 #include "base/geometry/ng/offset_t.h"
20 #include "base/geometry/ng/size_t.h"
21 #include "base/memory/referenced.h"
22 #include "base/utils/utils.h"
23 #include "core/common/ime/text_input_type.h"
24 #include "core/components_ng/layout/layout_property.h"
25 #include "core/components_ng/pattern/image/image_pattern.h"
26 #include "core/components_ng/pattern/stack/stack_pattern.h"
27 #include "core/components_ng/pattern/text_field/text_field_layout_property.h"
28 #include "core/components_ng/pattern/text_field/text_field_pattern.h"
29 #include "core/components_ng/property/measure_property.h"
30 #include "core/components_v2/inspector/inspector_constants.h"
31 #include "core/pipeline_ng/ui_task_scheduler.h"
32
33 namespace OHOS::Ace::NG {
34 // TextInputResponseArea begin
LayoutChild(LayoutWrapper * layoutWrapper,int32_t index,float & nodeWidth)35 void TextInputResponseArea::LayoutChild(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth)
36 {
37 auto frameNode = layoutWrapper->GetHostNode();
38 CHECK_NULL_VOID(frameNode);
39 auto children = frameNode->GetChildren();
40 CHECK_NULL_VOID(!children.empty());
41 auto pattern = frameNode->GetPattern<TextFieldPattern>();
42 CHECK_NULL_VOID(pattern);
43 auto textInputGeometryNode = layoutWrapper->GetGeometryNode();
44 CHECK_NULL_VOID(textInputGeometryNode);
45 auto contentRect = textInputGeometryNode->GetContentRect();
46 auto textInputFrameSize = textInputGeometryNode->GetFrameSize();
47 auto childWrapper = layoutWrapper->GetOrCreateChildByIndex(index);
48 CHECK_NULL_VOID(childWrapper);
49 auto childGeometryNode = childWrapper->GetGeometryNode();
50 CHECK_NULL_VOID(childGeometryNode);
51 auto childFrameSize = childGeometryNode->GetFrameSize();
52 auto childOffset = GetChildOffset(textInputFrameSize, contentRect, childFrameSize, nodeWidth);
53 childGeometryNode->SetFrameOffset(childOffset);
54 childWrapper->GetGeometryNode()->SetFrameSize(childFrameSize);
55 areaRect_.SetSize(childFrameSize);
56 areaRect_.SetOffset(childOffset);
57 childWrapper->Layout();
58 nodeWidth += childFrameSize.Width();
59 }
60
Measure(LayoutWrapper * layoutWrapper,int32_t index)61 SizeF TextInputResponseArea::Measure(LayoutWrapper* layoutWrapper, int32_t index)
62 {
63 auto childWrapper = layoutWrapper->GetOrCreateChildByIndex(index);
64 auto textfieldLayoutProperty = AceType::DynamicCast<TextFieldLayoutProperty>(layoutWrapper->GetLayoutProperty());
65 SizeF size(0, 0);
66 CHECK_NULL_RETURN(textfieldLayoutProperty, size);
67 auto childLayoutConstraint = textfieldLayoutProperty->CreateChildConstraint();
68 CHECK_NULL_RETURN(childWrapper, size);
69 auto childLayoutProperty = childWrapper->GetLayoutProperty();
70 auto&& layoutConstraint = childLayoutProperty->GetCalcLayoutConstraint();
71 if (layoutConstraint && layoutConstraint->selfIdealSize) {
72 layoutConstraint->selfIdealSize->SetHeight(
73 CalcLength(layoutWrapper->GetGeometryNode()->GetFrameSize().Height()));
74 }
75 childWrapper->Measure(childLayoutConstraint);
76 auto geometryNode = childWrapper->GetGeometryNode();
77 CHECK_NULL_RETURN(geometryNode, size);
78 return geometryNode->GetFrameSize();
79 } // TextInputResponseArea end
80
81 // PasswordResponseArea begin
InitResponseArea()82 void PasswordResponseArea::InitResponseArea()
83 {
84 ClearArea();
85 auto pattern = hostPattern_.Upgrade();
86 CHECK_NULL_VOID(pattern);
87 auto host = pattern->GetHost();
88 CHECK_NULL_VOID(host);
89 if (!IsShowPasswordIcon()) {
90 return;
91 }
92 auto passwordNode = CreateNode();
93 CHECK_NULL_VOID(passwordNode);
94 passwordNode->MountToParent(host);
95 }
96
GetFrameNode()97 const RefPtr<FrameNode> PasswordResponseArea::GetFrameNode()
98 {
99 auto frameNode = passwordNode_.Upgrade();
100 CHECK_NULL_RETURN(frameNode, nullptr);
101 auto stackNode = frameNode->GetParent();
102 CHECK_NULL_RETURN(stackNode, nullptr);
103 auto ret = AceType::DynamicCast<FrameNode>(stackNode);
104 CHECK_NULL_RETURN(ret, nullptr);
105 return ret;
106 }
107
CreateNode()108 RefPtr<FrameNode> PasswordResponseArea::CreateNode()
109 {
110 auto textFieldPattern = DynamicCast<TextFieldPattern>(hostPattern_.Upgrade());
111 CHECK_NULL_RETURN(textFieldPattern, nullptr);
112 auto iconSize = GetIconSize();
113 auto rightOffset = GetIconRightOffset();
114 auto hotZoneSize = iconSize + rightOffset;
115
116 auto stackNode = FrameNode::CreateFrameNode(
117 V2::STACK_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<StackPattern>());
118 auto stackLayoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
119 CHECK_NULL_RETURN(stackLayoutProperty, nullptr);
120 stackLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(hotZoneSize), std::nullopt));
121 stackLayoutProperty->UpdateAlignment(Alignment::CENTER_LEFT);
122 AddEvent(stackNode);
123 stackNode->MarkModifyDone();
124
125 auto imageNode = FrameNode::CreateFrameNode(
126 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ImagePattern>());
127 imageNode->SetDraggable(false);
128 LoadImageSourceInfo();
129 auto currentImageSourceInfo = GetCurrentSourceInfo();
130 CHECK_NULL_RETURN(currentImageSourceInfo, nullptr);
131 auto imageLayoutProperty = imageNode->GetLayoutProperty<ImageLayoutProperty>();
132 imageLayoutProperty->UpdateImageSourceInfo(currentImageSourceInfo.value());
133 imageLayoutProperty->UpdateImageFit(ImageFit::FILL);
134 imageLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(iconSize), CalcLength(iconSize)));
135 auto eventHub = imageNode->GetEventHub<ImageEventHub>();
136 CHECK_NULL_RETURN(eventHub, nullptr);
137 eventHub->SetOnError([ weakNode = WeakClaim(AceType::RawPtr(imageNode)), weakArea = WeakClaim(this) ]
138 (const LoadImageFailEvent& info) {
139 auto host = weakNode.Upgrade();
140 CHECK_NULL_VOID(host);
141 auto area = weakArea.Upgrade();
142 CHECK_NULL_VOID(area);
143 auto imagePattern = host->GetPattern<ImagePattern>();
144 CHECK_NULL_VOID(imagePattern);
145 auto layoutProperty = host->GetLayoutProperty<ImageLayoutProperty>();
146 layoutProperty->UpdateImageSourceInfo(area->GetDefaultSourceInfo(area->isObscured_));
147 imagePattern->LoadImageDataIfNeed();
148 });
149 imageNode->MarkModifyDone();
150 imageNode->MountToParent(stackNode);
151 passwordNode_ = imageNode;
152 stackNode_ = stackNode;
153 return stackNode;
154 }
155
AddEvent(const RefPtr<FrameNode> & node)156 void PasswordResponseArea::AddEvent(const RefPtr<FrameNode>& node)
157 {
158 CHECK_NULL_VOID(node);
159 auto focusHub = node->GetOrCreateFocusHub();
160 CHECK_NULL_VOID(focusHub);
161 auto gesture = node->GetOrCreateGestureEventHub();
162 auto clickCallback = [weak = WeakClaim(this)](GestureEvent& info) {
163 auto button = weak.Upgrade();
164 CHECK_NULL_VOID(button);
165 button->OnPasswordIconClicked();
166 };
167 auto longPressCallback = [](GestureEvent& info) {
168 LOGI("PasswordResponseArea long press");
169 };
170 gesture->SetLongPressEvent(MakeRefPtr<LongPressEvent>(std::move(longPressCallback)));
171 gesture->AddClickEvent(MakeRefPtr<ClickEvent>(std::move(clickCallback)));
172 }
173
Refresh()174 void PasswordResponseArea::Refresh()
175 {
176 auto imageNode = passwordNode_.Upgrade();
177 if (!imageNode) {
178 InitResponseArea();
179 return;
180 }
181 auto imageLayoutProperty = imageNode->GetLayoutProperty<ImageLayoutProperty>();
182 CHECK_NULL_VOID(imageLayoutProperty);
183 auto currentSrc = imageLayoutProperty->GetImageSourceInfoValue().GetSrc();
184 LoadImageSourceInfo();
185 auto src = isObscured_ ? hideIcon_->GetSrc() : showIcon_->GetSrc();
186 if (currentSrc != src) {
187 UpdateImageSource();
188 }
189 }
190
OnPasswordIconClicked()191 void PasswordResponseArea::OnPasswordIconClicked()
192 {
193 isObscured_ = !isObscured_;
194 UpdateImageSource();
195 auto textFieldPattern = DynamicCast<TextFieldPattern>(hostPattern_.Upgrade());
196 CHECK_NULL_VOID(textFieldPattern);
197 textFieldPattern->OnObscuredChanged(isObscured_);
198 }
199
Measure(LayoutWrapper * layoutWrapper,int32_t index)200 SizeF PasswordResponseArea::Measure(LayoutWrapper* layoutWrapper, int32_t index)
201 {
202 if (!IsShowPasswordIcon()) {
203 return SizeF(0, 0);
204 }
205 return TextInputResponseArea::Measure(layoutWrapper, index);
206 }
207
Layout(LayoutWrapper * layoutWrapper,int32_t index,float & nodeWidth)208 void PasswordResponseArea::Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth)
209 {
210 if (!IsShowPasswordIcon()) {
211 return;
212 }
213 LayoutChild(layoutWrapper, index, nodeWidth);
214 }
215
GetChildOffset(SizeF parentSize,RectF contentRect,SizeF childSize,float nodeWidth)216 OffsetF PasswordResponseArea::GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth)
217 {
218 return OffsetF(parentSize.Width() - childSize.Width() - nodeWidth, 0);
219 }
220
GetIconSize()221 float PasswordResponseArea::GetIconSize()
222 {
223 auto textFieldPattern = hostPattern_.Upgrade();
224 CHECK_NULL_RETURN(textFieldPattern, 0.0f);
225 auto tmpHost = textFieldPattern->GetHost();
226 CHECK_NULL_RETURN(tmpHost, 0.0f);
227 auto pipeline = tmpHost->GetContext();
228 CHECK_NULL_RETURN(pipeline, 0.0f);
229 auto themeManager = pipeline->GetThemeManager();
230 CHECK_NULL_RETURN(themeManager, 0.0f);
231 auto textFieldTheme = themeManager->GetTheme<TextFieldTheme>();
232 CHECK_NULL_RETURN(textFieldTheme, 0.0f);
233 return static_cast<float>(textFieldTheme->GetIconSize().ConvertToPx());
234 }
235
GetIconRightOffset()236 float PasswordResponseArea::GetIconRightOffset()
237 {
238 auto textFieldPattern = hostPattern_.Upgrade();
239 CHECK_NULL_RETURN(textFieldPattern, 0.0f);
240 auto tmpHost = textFieldPattern->GetHost();
241 auto pipeline = tmpHost->GetContext();
242 CHECK_NULL_RETURN(pipeline, 0.0f);
243 auto themeManager = pipeline->GetThemeManager();
244 CHECK_NULL_RETURN(themeManager, 0.0f);
245 auto textFieldTheme = themeManager->GetTheme<TextFieldTheme>();
246 CHECK_NULL_RETURN(textFieldTheme, 0.0f);
247 auto themePadding = textFieldTheme->GetPadding();
248 return static_cast<float>(themePadding.Left().ConvertToPx());
249 }
250
LoadImageSourceInfo()251 void PasswordResponseArea::LoadImageSourceInfo()
252 {
253 auto textFieldPattern = hostPattern_.Upgrade();
254 CHECK_NULL_VOID(textFieldPattern);
255 auto layoutProperty = textFieldPattern->GetLayoutProperty<TextFieldLayoutProperty>();
256 CHECK_NULL_VOID(layoutProperty);
257 showIcon_ = layoutProperty->GetShowPasswordSourceInfoValue(GetDefaultSourceInfo(false));
258 hideIcon_ = layoutProperty->GetHidePasswordSourceInfoValue(GetDefaultSourceInfo(true));
259 auto tmpHost = textFieldPattern->GetHost();
260 CHECK_NULL_VOID(tmpHost);
261 auto pipeline = tmpHost->GetContext();
262 CHECK_NULL_VOID(pipeline);
263 auto themeManager = pipeline->GetThemeManager();
264 CHECK_NULL_VOID(themeManager);
265 auto textFieldTheme = themeManager->GetTheme<TextFieldTheme>();
266 CHECK_NULL_VOID(textFieldTheme);
267 if (showIcon_->GetResourceId() == InternalResource::ResourceId::SHOW_PASSWORD_SVG) {
268 showIcon_->SetFillColor(textFieldTheme->GetTextColor());
269 }
270 if (hideIcon_->GetResourceId() == InternalResource::ResourceId::HIDE_PASSWORD_SVG) {
271 hideIcon_->SetFillColor(textFieldTheme->GetTextColor());
272 }
273 if (layoutProperty->GetIsDisabledValue(false)) {
274 auto iconTheme = pipeline->GetTheme<IconTheme>();
275 CHECK_NULL_VOID(iconTheme);
276 auto textDisableColor = textFieldTheme->GetTextColorDisable();
277 auto hideIconPath = iconTheme->GetIconPath(hideIcon_->GetResourceId());
278 hideIcon_->SetSrc(hideIconPath, textDisableColor);
279 auto showIconPath = iconTheme->GetIconPath(showIcon_->GetResourceId());
280 showIcon_->SetSrc(showIconPath, textDisableColor);
281 UpdateImageSource();
282 }
283 }
284
GetDefaultSourceInfo(bool isObscured)285 ImageSourceInfo PasswordResponseArea::GetDefaultSourceInfo(bool isObscured)
286 {
287 if (isObscured) {
288 ImageSourceInfo hideSystemSourceInfo;
289 hideSystemSourceInfo.SetResourceId(InternalResource::ResourceId::HIDE_PASSWORD_SVG);
290 return hideSystemSourceInfo;
291 }
292 ImageSourceInfo showSystemSourceInfo;
293 showSystemSourceInfo.SetResourceId(InternalResource::ResourceId::SHOW_PASSWORD_SVG);
294 return showSystemSourceInfo;
295 }
296
UpdateImageSource()297 void PasswordResponseArea::UpdateImageSource()
298 {
299 auto frameNode = passwordNode_.Upgrade();
300 CHECK_NULL_VOID(frameNode);
301 auto layoutProperty = frameNode->GetLayoutProperty<ImageLayoutProperty>();
302 CHECK_NULL_VOID(layoutProperty);
303 auto currentImageSourceInfo = GetCurrentSourceInfo();
304 CHECK_NULL_VOID(currentImageSourceInfo);
305 layoutProperty->UpdateImageSourceInfo(currentImageSourceInfo.value());
306 auto imagePattern = frameNode->GetPattern<ImagePattern>();
307 CHECK_NULL_VOID(imagePattern);
308 imagePattern->LoadImageDataIfNeed();
309 }
310
IsShowPasswordIcon()311 bool PasswordResponseArea::IsShowPasswordIcon()
312 {
313 auto textFieldPattern = AceType::DynamicCast<TextFieldPattern>(hostPattern_.Upgrade());
314 CHECK_NULL_RETURN(textFieldPattern, false);
315 return textFieldPattern->IsShowPasswordIcon();
316 } // PasswordResponseArea end
317
318 // UnitResponseArea begin
InitResponseArea()319 void UnitResponseArea::InitResponseArea()
320 {
321 auto pattern = hostPattern_.Upgrade();
322 CHECK_NULL_VOID(pattern);
323 auto host = pattern->GetHost();
324 CHECK_NULL_VOID(host);
325 if (!IsShowUnit()) {
326 return;
327 }
328 CHECK_NULL_VOID(unitNode_);
329 unitNode_->MountToParent(host);
330 }
331
GetFrameNode()332 const RefPtr<FrameNode> UnitResponseArea::GetFrameNode()
333 {
334 auto frameNode = AceType::DynamicCast<FrameNode>(unitNode_);
335 CHECK_NULL_RETURN(frameNode, nullptr);
336 return frameNode;
337 }
338
Measure(LayoutWrapper * layoutWrapper,int32_t index)339 SizeF UnitResponseArea::Measure(LayoutWrapper* layoutWrapper, int32_t index)
340 {
341 if (!IsShowUnit()) {
342 return SizeF(0, 0);
343 }
344 return TextInputResponseArea::Measure(layoutWrapper, index);
345 }
346
Layout(LayoutWrapper * layoutWrapper,int32_t index,float & nodeWidth)347 void UnitResponseArea::Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth)
348 {
349 if (!IsShowUnit()) {
350 return;
351 }
352 LayoutChild(layoutWrapper, index, nodeWidth);
353 }
354
GetChildOffset(SizeF parentSize,RectF contentRect,SizeF childSize,float nodeWidth)355 OffsetF UnitResponseArea::GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth)
356 {
357 return OffsetF(parentSize.Width() - childSize.Width() - nodeWidth, 0);
358 }
359
IsShowUnit()360 bool UnitResponseArea::IsShowUnit()
361 {
362 auto pattern = hostPattern_.Upgrade();
363 CHECK_NULL_RETURN(pattern, false);
364 auto textFieldPattern = AceType::DynamicCast<TextFieldPattern>(pattern);
365 CHECK_NULL_RETURN(textFieldPattern, false);
366 return textFieldPattern->IsShowUnit();
367 } // UnitResponseArea end
368
InitResponseArea()369 void CleanNodeResponseArea::InitResponseArea()
370 {
371 auto pattern = hostPattern_.Upgrade();
372 CHECK_NULL_VOID(pattern);
373 auto host = pattern->GetHost();
374 CHECK_NULL_VOID(host);
375 LoadingImageProperty();
376 auto cleanNode = CreateNode();
377 CHECK_NULL_VOID(cleanNode);
378 cleanNode->MountToParent(host);
379 }
380
Measure(LayoutWrapper * layoutWrapper,int32_t index)381 SizeF CleanNodeResponseArea::Measure(LayoutWrapper* layoutWrapper, int32_t index)
382 {
383 return TextInputResponseArea::Measure(layoutWrapper, index);
384 }
385
Layout(LayoutWrapper * layoutWrapper,int32_t index,float & nodeWidth)386 void CleanNodeResponseArea::Layout(LayoutWrapper* layoutWrapper, int32_t index, float& nodeWidth)
387 {
388 LayoutChild(layoutWrapper, index, nodeWidth);
389 }
390
GetChildOffset(SizeF parentSize,RectF contentRect,SizeF childSize,float nodeWidth)391 OffsetF CleanNodeResponseArea::GetChildOffset(SizeF parentSize, RectF contentRect, SizeF childSize, float nodeWidth)
392 {
393 return OffsetF(parentSize.Width() - childSize.Width() - nodeWidth, 0);
394 }
395
GetFrameNode()396 const RefPtr<FrameNode> CleanNodeResponseArea::GetFrameNode()
397 {
398 return cleanNode_;
399 }
400
CreateNode()401 RefPtr<FrameNode> CleanNodeResponseArea::CreateNode()
402 {
403 auto stackNode = FrameNode::CreateFrameNode(
404 V2::STACK_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<StackPattern>());
405 auto stackLayoutProperty = stackNode->GetLayoutProperty<LayoutProperty>();
406 CHECK_NULL_RETURN(stackLayoutProperty, nullptr);
407 stackLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(0.0f), std::nullopt));
408 stackLayoutProperty->UpdateAlignment(Alignment::CENTER_LEFT);
409 stackNode->MarkModifyDone();
410 auto cleanNode = FrameNode::CreateFrameNode(
411 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ImagePattern>());
412 CHECK_NULL_RETURN(cleanNode, nullptr);
413 cleanNode->SetDraggable(false);
414 cleanNode_ = stackNode;
415 auto info = CreateImageSourceInfo();
416 auto imageLayoutProperty = cleanNode->GetLayoutProperty<ImageLayoutProperty>();
417 CHECK_NULL_RETURN(imageLayoutProperty, nullptr);
418 imageLayoutProperty->UpdateImageSourceInfo(info);
419 imageLayoutProperty->UpdateImageFit(ImageFit::COVER);
420 imageLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(0.0f), CalcLength(0.0f)));
421 cleanNode->MarkModifyDone();
422 cleanNode->MountToParent(stackNode);
423 InitClickEvent(stackNode);
424 return stackNode;
425 }
426
InitClickEvent(const RefPtr<FrameNode> & frameNode)427 void CleanNodeResponseArea::InitClickEvent(const RefPtr<FrameNode>& frameNode)
428 {
429 auto focusHub = frameNode->GetOrCreateFocusHub();
430 CHECK_NULL_VOID(focusHub);
431 auto gesture = frameNode->GetOrCreateGestureEventHub();
432 auto clickCallback = [weak = WeakClaim(this)](GestureEvent& info) {
433 auto cleanNode = weak.Upgrade();
434 CHECK_NULL_VOID(cleanNode);
435 cleanNode->OnCleanNodeClicked();
436 };
437 auto longPressCallback = [](GestureEvent& info) { LOGI("CleanNodeResponseArea long press"); };
438 gesture->SetLongPressEvent(MakeRefPtr<LongPressEvent>(std::move(longPressCallback)));
439 gesture->AddClickEvent(MakeRefPtr<ClickEvent>(std::move(clickCallback)));
440 }
441
OnCleanNodeClicked()442 void CleanNodeResponseArea::OnCleanNodeClicked()
443 {
444 auto textFieldPattern = DynamicCast<TextFieldPattern>(hostPattern_.Upgrade());
445 CHECK_NULL_VOID(textFieldPattern);
446 CHECK_NULL_VOID(!textFieldPattern->IsDragging());
447 textFieldPattern->CleanNodeResponseKeyEvent();
448 }
449
UpdateCleanNode(bool isShow)450 void CleanNodeResponseArea::UpdateCleanNode(bool isShow)
451 {
452 isShow_ = isShow;
453 auto textFieldPattern = DynamicCast<TextFieldPattern>(hostPattern_.Upgrade());
454 CHECK_NULL_VOID(textFieldPattern);
455 CHECK_NULL_VOID(cleanNode_);
456 auto stackLayoutProperty = cleanNode_->GetLayoutProperty<LayoutProperty>();
457 CHECK_NULL_VOID(stackLayoutProperty);
458 auto imageNode = cleanNode_->GetFirstChild();
459 CHECK_NULL_VOID(imageNode);
460 auto imageFrameNode = AceType::DynamicCast<FrameNode>(imageNode);
461 CHECK_NULL_VOID(imageFrameNode);
462 auto imageLayoutProperty = imageFrameNode->GetLayoutProperty<ImageLayoutProperty>();
463 CHECK_NULL_VOID(imageLayoutProperty);
464 if (isShow) {
465 auto host = textFieldPattern->GetHost();
466 CHECK_NULL_VOID(host);
467 auto pipeline = host->GetContext();
468 CHECK_NULL_VOID(pipeline);
469 auto themeManager = pipeline->GetThemeManager();
470 CHECK_NULL_VOID(themeManager);
471 auto textFieldTheme = themeManager->GetTheme<TextFieldTheme>();
472 CHECK_NULL_VOID(textFieldTheme);
473 auto themePadding = textFieldTheme->GetPadding();
474 auto rightOffset = static_cast<float>(themePadding.Left().ConvertToPx());
475 auto geometryNode = host->GetGeometryNode();
476 CHECK_NULL_VOID(geometryNode);
477 auto frameSize = geometryNode->GetFrameSize();
478 auto iconSize = std::min(iconSize_.ConvertToPx(), static_cast<double>(frameSize.Height()));
479 if (NearZero(iconSize)) {
480 isShow_ = false;
481 }
482 auto hotZoneSize = iconSize + rightOffset;
483 stackLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(hotZoneSize), std::nullopt));
484 imageLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(iconSize), CalcLength(iconSize)));
485 } else {
486 stackLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(0.0f), std::nullopt));
487 imageLayoutProperty->UpdateUserDefinedIdealSize(CalcSize(CalcLength(0.0f), CalcLength(0.0f)));
488 }
489 imageFrameNode->MarkModifyDone();
490 imageFrameNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF);
491 }
492
ClearArea()493 void CleanNodeResponseArea::ClearArea()
494 {
495 auto hostPattern = hostPattern_.Upgrade();
496 CHECK_NULL_VOID(hostPattern);
497 auto host = hostPattern->GetHost();
498 CHECK_NULL_VOID(host);
499 CHECK_NULL_VOID(cleanNode_);
500 host->RemoveChildAndReturnIndex(cleanNode_);
501 cleanNode_.Reset();
502 }
503
Refresh()504 void CleanNodeResponseArea::Refresh()
505 {
506 LoadingImageProperty();
507 auto info = CreateImageSourceInfo();
508 CHECK_NULL_VOID(cleanNode_);
509 auto imageNode = cleanNode_->GetFirstChild();
510 CHECK_NULL_VOID(imageNode);
511 auto imageFrameNode = AceType::DynamicCast<FrameNode>(imageNode);
512 CHECK_NULL_VOID(imageFrameNode);
513 auto imageLayoutProperty = imageFrameNode->GetLayoutProperty<ImageLayoutProperty>();
514 CHECK_NULL_VOID(imageLayoutProperty);
515 imageLayoutProperty->UpdateImageSourceInfo(info);
516 imageFrameNode->MarkModifyDone();
517 imageFrameNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF);
518 }
519
LoadingImageProperty()520 void CleanNodeResponseArea::LoadingImageProperty()
521 {
522 auto pattern = hostPattern_.Upgrade();
523 CHECK_NULL_VOID(pattern);
524 auto textFieldLayoutProperty = pattern->GetLayoutProperty<TextFieldLayoutProperty>();
525 CHECK_NULL_VOID(textFieldLayoutProperty);
526 if (textFieldLayoutProperty->HasIconSize()) {
527 iconSize_ = textFieldLayoutProperty->GetIconSizeValue();
528 }
529 if (textFieldLayoutProperty->HasIconSrc()) {
530 iconSrc_ = textFieldLayoutProperty->GetIconSrcValue();
531 }
532 if (textFieldLayoutProperty->HasIconColor()) {
533 iconColor_ = textFieldLayoutProperty->GetIconColorValue();
534 }
535 }
536
CreateImageSourceInfo()537 ImageSourceInfo CleanNodeResponseArea::CreateImageSourceInfo()
538 {
539 ImageSourceInfo info;
540 if (iconSrc_.empty()) {
541 info.SetResourceId(InternalResource::ResourceId::CLOSE_SVG);
542 } else {
543 info.SetSrc(iconSrc_);
544 }
545 if (info.IsSvg()) {
546 info.SetFillColor(iconColor_);
547 CHECK_NULL_RETURN(cleanNode_, info);
548 auto imageNode = cleanNode_->GetFirstChild();
549 CHECK_NULL_RETURN(imageNode, info);
550 auto imageFrameNode = AceType::DynamicCast<FrameNode>(imageNode);
551 CHECK_NULL_RETURN(imageFrameNode, info);
552 auto imageRenderProperty = imageFrameNode->GetPaintProperty<ImageRenderProperty>();
553 CHECK_NULL_RETURN(imageRenderProperty, info);
554 imageRenderProperty->UpdateSvgFillColor(iconColor_);
555 }
556 return info;
557 }
558 } // namespace OHOS::Ace::NG