1 /*
2 * Copyright (c) 2022 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 "bridge/declarative_frontend/jsview/models/text_field_model_impl.h"
17
18 #include "base/geometry/dimension.h"
19 #include "base/memory/referenced.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/jsview/js_textfield.h"
22 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
23 #include "bridge/declarative_frontend/view_stack_processor.h"
24 #include "core/components/common/layout/constants.h"
25 #include "core/components/common/properties/text_style.h"
26 #include "core/components/text_field/text_field_component.h"
27 #include "core/components/text_field/textfield_theme.h"
28 #include "core/components_ng/pattern/text_field/text_field_model.h"
29 #include "core/pipeline/pipeline_context.h"
30
31 namespace OHOS::Ace::Framework {
32 namespace {
33 const std::vector<std::string> INPUT_FONT_FAMILY_VALUE = { "sans-serif" };
34 constexpr Dimension INNER_PADDING = 0.0_vp;
35 constexpr uint32_t TEXTAREA_MAXLENGTH_VALUE_DEFAULT = std::numeric_limits<uint32_t>::max();
36 } // namespace
37
CreateTextInput(const std::optional<std::string> & placeholder,const std::optional<std::string> & value)38 RefPtr<TextFieldControllerBase> TextFieldModelImpl::CreateTextInput(
39 const std::optional<std::string>& placeholder, const std::optional<std::string>& value)
40 {
41 auto textInputComponent = AceType::MakeRefPtr<TextFieldComponent>();
42 if (placeholder) {
43 textInputComponent->SetPlaceholder(placeholder.value());
44 }
45 if (value) {
46 textInputComponent->SetValue(value.value());
47 }
48 ViewStackProcessor::GetInstance()->ClaimElementId(textInputComponent);
49 textInputComponent->SetTextFieldController(AceType::MakeRefPtr<TextFieldController>());
50 // default type is text, default action is done.
51 textInputComponent->SetTextInputType(TextInputType::TEXT);
52 textInputComponent->SetAction(TextInputAction::DONE);
53 textInputComponent->SetInspectorTag("TextInput");
54 textInputComponent->SetHoverAnimationType(HoverAnimationType::BOARD);
55 ViewStackProcessor::GetInstance()->Push(textInputComponent);
56 InitTextInputDefaultStyle();
57 Border boxBorder;
58 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
59 auto pipeline = PipelineContext::GetCurrentContext();
60 if (pipeline) {
61 auto theme = pipeline->GetThemeManager()->GetTheme<TextFieldTheme>();
62 if (boxComponent->GetBackDecoration()) {
63 boxBorder = boxComponent->GetBackDecoration()->GetBorder();
64 }
65 JSTextField::UpdateDecoration(boxComponent, textInputComponent, boxBorder, theme);
66 }
67
68 return textInputComponent->GetTextFieldController();
69 }
70
InitTextAreaDefaultStyle()71 void InitTextAreaDefaultStyle()
72 {
73 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
74 auto* stack = ViewStackProcessor::GetInstance();
75 auto textAreaComponent = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
76 auto pipeline = PipelineBase::GetCurrentContext();
77 CHECK_NULL_VOID(pipeline);
78 auto theme = pipeline->GetThemeManager()->GetTheme<TextFieldTheme>();
79 if (!boxComponent || !textAreaComponent || !theme) {
80 LOGI("boxComponent or textAreaComponent or theme is null");
81 return;
82 }
83
84 textAreaComponent->SetTextMaxLines(TEXTAREA_MAXLENGTH_VALUE_DEFAULT);
85 textAreaComponent->SetCursorColor(theme->GetCursorColor());
86 textAreaComponent->SetPlaceholderColor(theme->GetPlaceholderColor());
87 textAreaComponent->SetFocusBgColor(theme->GetFocusBgColor());
88 textAreaComponent->SetFocusPlaceholderColor(theme->GetFocusPlaceholderColor());
89 textAreaComponent->SetFocusTextColor(theme->GetFocusTextColor());
90 textAreaComponent->SetBgColor(theme->GetBgColor());
91 textAreaComponent->SetTextColor(theme->GetTextColor());
92 textAreaComponent->SetSelectedColor(theme->GetSelectedColor());
93 textAreaComponent->SetHoverColor(theme->GetHoverColor());
94 textAreaComponent->SetPressColor(theme->GetPressColor());
95
96 TextStyle textStyle = textAreaComponent->GetTextStyle();
97 textStyle.SetTextColor(theme->GetTextColor());
98 textStyle.SetFontSize(theme->GetFontSize());
99 textStyle.SetFontWeight(theme->GetFontWeight());
100 textStyle.SetFontFamilies(INPUT_FONT_FAMILY_VALUE);
101 textAreaComponent->SetTextStyle(textStyle);
102 textAreaComponent->SetEditingStyle(textStyle);
103 textAreaComponent->SetPlaceHoldStyle(textStyle);
104
105 textAreaComponent->SetCountTextStyle(theme->GetCountTextStyle());
106 textAreaComponent->SetOverCountStyle(theme->GetOverCountStyle());
107 textAreaComponent->SetCountTextStyleOuter(theme->GetCountTextStyleOuter());
108 textAreaComponent->SetOverCountStyleOuter(theme->GetOverCountStyleOuter());
109 textAreaComponent->SetErrorBorderWidth(theme->GetErrorBorderWidth());
110 textAreaComponent->SetErrorBorderColor(theme->GetErrorBorderColor());
111
112 RefPtr<Decoration> backDecoration = AceType::MakeRefPtr<Decoration>();
113 backDecoration->SetPadding(theme->GetPadding());
114 backDecoration->SetBackgroundColor(theme->GetBgColor());
115 backDecoration->SetBorderRadius(theme->GetBorderRadius());
116 const auto& boxDecoration = boxComponent->GetBackDecoration();
117 if (boxDecoration) {
118 backDecoration->SetImage(boxDecoration->GetImage());
119 backDecoration->SetGradient(boxDecoration->GetGradient());
120 }
121 textAreaComponent->SetOriginBorder(backDecoration->GetBorder());
122 textAreaComponent->SetDecoration(backDecoration);
123 textAreaComponent->SetIconSize(theme->GetIconSize());
124 textAreaComponent->SetIconHotZoneSize(theme->GetIconHotZoneSize());
125 textAreaComponent->SetHeight(theme->GetHeight());
126
127 // text area need to extend height.
128 textAreaComponent->SetExtend(true);
129 boxComponent->SetHeight(-1.0, DimensionUnit::VP);
130 }
131
CreateTextArea(const std::optional<std::string> & placeholder,const std::optional<std::string> & value)132 RefPtr<TextFieldControllerBase> TextFieldModelImpl::CreateTextArea(
133 const std::optional<std::string>& placeholder, const std::optional<std::string>& value)
134 {
135 RefPtr<TextFieldComponent> textAreaComponent = AceType::MakeRefPtr<TextFieldComponent>();
136 textAreaComponent->SetTextFieldController(AceType::MakeRefPtr<TextFieldController>());
137 textAreaComponent->SetTextInputType(TextInputType::MULTILINE);
138 textAreaComponent->SetHoverAnimationType(HoverAnimationType::BOARD);
139
140 ViewStackProcessor::GetInstance()->ClaimElementId(textAreaComponent);
141 ViewStackProcessor::GetInstance()->Push(textAreaComponent);
142 InitTextAreaDefaultStyle();
143 Border boxBorder;
144 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
145 auto theme = JSViewAbstract::GetTheme<TextFieldTheme>();
146 if (boxComponent->GetBackDecoration()) {
147 boxBorder = boxComponent->GetBackDecoration()->GetBorder();
148 }
149 if (value) {
150 textAreaComponent->SetValue(value.value());
151 }
152 if (placeholder) {
153 textAreaComponent->SetPlaceholder(placeholder.value());
154 }
155 UpdateDecoration(boxComponent, textAreaComponent, boxBorder, theme);
156
157 return textAreaComponent->GetTextFieldController();
158 }
159
InitTextInputDefaultStyle()160 void TextFieldModelImpl::InitTextInputDefaultStyle()
161 {
162 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
163 auto* stack = ViewStackProcessor::GetInstance();
164 auto textInputComponent = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
165 auto theme = JSViewAbstract::GetTheme<TextFieldTheme>();
166 if (!boxComponent || !textInputComponent || !theme) {
167 return;
168 }
169
170 textInputComponent->SetCursorColor(theme->GetCursorColor());
171 textInputComponent->SetCursorRadius(theme->GetCursorRadius());
172 textInputComponent->SetPlaceholderColor(theme->GetPlaceholderColor());
173 textInputComponent->SetFocusBgColor(theme->GetFocusBgColor());
174 textInputComponent->SetFocusPlaceholderColor(theme->GetFocusPlaceholderColor());
175 textInputComponent->SetFocusTextColor(theme->GetFocusTextColor());
176 textInputComponent->SetBgColor(theme->GetBgColor());
177 textInputComponent->SetTextColor(theme->GetTextColor());
178 textInputComponent->SetSelectedColor(theme->GetSelectedColor());
179 textInputComponent->SetHoverColor(theme->GetHoverColor());
180 textInputComponent->SetPressColor(theme->GetPressColor());
181 textInputComponent->SetNeedFade(theme->NeedFade());
182 textInputComponent->SetShowEllipsis(theme->ShowEllipsis());
183
184 TextStyle textStyle = textInputComponent->GetTextStyle();
185 textStyle.SetTextColor(theme->GetTextColor());
186 textStyle.SetFontSize(theme->GetFontSize());
187 textStyle.SetFontWeight(theme->GetFontWeight());
188 textStyle.SetFontFamilies(INPUT_FONT_FAMILY_VALUE);
189 textInputComponent->SetTextStyle(textStyle);
190 textInputComponent->SetEditingStyle(textStyle);
191 textInputComponent->SetPlaceHoldStyle(textStyle);
192
193 textInputComponent->SetCountTextStyle(theme->GetCountTextStyle());
194 textInputComponent->SetOverCountStyle(theme->GetOverCountStyle());
195 textInputComponent->SetCountTextStyleOuter(theme->GetCountTextStyleOuter());
196 textInputComponent->SetOverCountStyleOuter(theme->GetOverCountStyleOuter());
197
198 textInputComponent->SetErrorTextStyle(theme->GetErrorTextStyle());
199 textInputComponent->SetErrorSpacing(theme->GetErrorSpacing());
200 textInputComponent->SetErrorIsInner(theme->GetErrorIsInner());
201 textInputComponent->SetErrorBorderWidth(theme->GetErrorBorderWidth());
202 textInputComponent->SetErrorBorderColor(theme->GetErrorBorderColor());
203
204 RefPtr<Decoration> decoration = AceType::MakeRefPtr<Decoration>();
205 // Need to update when UX of PC supported.
206 auto edge = theme->GetPadding();
207 edge.SetTop(INNER_PADDING);
208 edge.SetBottom(INNER_PADDING);
209 decoration->SetPadding(edge);
210 decoration->SetBackgroundColor(theme->GetBgColor());
211 decoration->SetBorderRadius(theme->GetBorderRadius());
212 const auto& boxDecoration = boxComponent->GetBackDecoration();
213 if (boxDecoration) {
214 decoration->SetImage(boxDecoration->GetImage());
215 decoration->SetGradient(boxDecoration->GetGradient());
216 }
217 textInputComponent->SetOriginBorder(decoration->GetBorder());
218 textInputComponent->SetDecoration(decoration);
219 textInputComponent->SetIconSize(theme->GetIconSize());
220 textInputComponent->SetIconHotZoneSize(theme->GetIconHotZoneSize());
221 textInputComponent->SetHeight(theme->GetHeight());
222 }
223
SetType(TextInputType value)224 void TextFieldModelImpl::SetType(TextInputType value)
225 {
226 auto* stack = ViewStackProcessor::GetInstance();
227 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
228 CHECK_NULL_VOID(component);
229 component->SetTextInputType(value);
230 component->SetObscure(value == TextInputType::VISIBLE_PASSWORD);
231 }
232
SetPlaceholderColor(const Color & value)233 void TextFieldModelImpl::SetPlaceholderColor(const Color& value)
234 {
235 auto* stack = ViewStackProcessor::GetInstance();
236 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
237 CHECK_NULL_VOID(component);
238 component->SetPlaceholderColor(value);
239 component->SetFocusPlaceholderColor(value);
240 }
241
SetPlaceholderFont(const Font & value)242 void TextFieldModelImpl::SetPlaceholderFont(const Font& value)
243 {
244 auto* stack = ViewStackProcessor::GetInstance();
245 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
246 CHECK_NULL_VOID(component);
247 auto textStyle = component->GetPlaceHoldStyle();
248 if (value.fontSize && value.fontSize->IsNonNegative()) {
249 textStyle.SetFontSize(value.fontSize.value());
250 }
251 if (value.fontWeight) {
252 textStyle.SetFontWeight(value.fontWeight.value());
253 }
254 if (value.fontStyle) {
255 textStyle.SetFontStyle(value.fontStyle.value());
256 }
257 if (!value.fontFamilies.empty()) {
258 textStyle.SetFontFamilies(value.fontFamilies);
259 }
260 component->SetPlaceHoldStyle(textStyle);
261 }
262
SetEnterKeyType(TextInputAction value)263 void TextFieldModelImpl::SetEnterKeyType(TextInputAction value)
264 {
265 auto* stack = ViewStackProcessor::GetInstance();
266 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
267 CHECK_NULL_VOID(component);
268 component->SetAction(value);
269 }
270
SetTextAlign(TextAlign value)271 void TextFieldModelImpl::SetTextAlign(TextAlign value)
272 {
273 auto* stack = ViewStackProcessor::GetInstance();
274 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
275 CHECK_NULL_VOID(component);
276 component->SetTextAlign(value);
277 }
278
SetInputStyle(InputStyle value)279 void TextFieldModelImpl::SetInputStyle(InputStyle value)
280 {
281 auto* stack = ViewStackProcessor::GetInstance();
282 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
283 CHECK_NULL_VOID(component);
284 component->SetInputStyle(value);
285 }
286
SetCaretColor(const Color & value)287 void TextFieldModelImpl::SetCaretColor(const Color& value)
288 {
289 auto* stack = ViewStackProcessor::GetInstance();
290 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
291 CHECK_NULL_VOID(component);
292 component->SetCursorColor(value);
293 }
294
SetCaretStyle(const CaretStyle & value)295 void TextFieldModelImpl::SetCaretStyle(const CaretStyle& value) {};
SetCaretPosition(const int32_t & value)296 void TextFieldModelImpl::SetCaretPosition(const int32_t& value) {};
SetSelectedBackgroundColor(const Color & value)297 void TextFieldModelImpl::SetSelectedBackgroundColor(const Color& value) {};
298
SetMaxLength(uint32_t value)299 void TextFieldModelImpl::SetMaxLength(uint32_t value)
300 {
301 auto* stack = ViewStackProcessor::GetInstance();
302 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
303 CHECK_NULL_VOID(component);
304 component->SetMaxLength(value);
305 }
306
SetMaxLines(uint32_t value)307 void TextFieldModelImpl::SetMaxLines(uint32_t value)
308 {
309 auto* stack = ViewStackProcessor::GetInstance();
310 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
311 CHECK_NULL_VOID(component);
312 component->SetTextMaxLines(value);
313 }
314
SetFontSize(const Dimension & value)315 void TextFieldModelImpl::SetFontSize(const Dimension& value)
316 {
317 if (value.IsNegative()) {
318 return;
319 }
320 auto* stack = ViewStackProcessor::GetInstance();
321 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
322 CHECK_NULL_VOID(component);
323 auto textStyle = component->GetEditingStyle();
324 textStyle.SetFontSize(value);
325 component->SetEditingStyle(textStyle);
326 }
327
SetFontWeight(FontWeight value)328 void TextFieldModelImpl::SetFontWeight(FontWeight value)
329 {
330 auto* stack = ViewStackProcessor::GetInstance();
331 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
332 CHECK_NULL_VOID(component);
333 auto textStyle = component->GetEditingStyle();
334 textStyle.SetFontWeight(value);
335 component->SetEditingStyle(textStyle);
336 }
337
SetTextColor(const Color & value)338 void TextFieldModelImpl::SetTextColor(const Color& value)
339 {
340 auto* stack = ViewStackProcessor::GetInstance();
341 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
342 CHECK_NULL_VOID(component);
343 auto textStyle = component->GetEditingStyle();
344 textStyle.SetTextColor(value);
345 component->SetEditingStyle(textStyle);
346 }
347
SetFontStyle(FontStyle value)348 void TextFieldModelImpl::SetFontStyle(FontStyle value)
349 {
350 auto* stack = ViewStackProcessor::GetInstance();
351 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
352 CHECK_NULL_VOID(component);
353 auto textStyle = component->GetEditingStyle();
354 textStyle.SetFontStyle(value);
355 component->SetEditingStyle(textStyle);
356 }
357
SetFontFamily(const std::vector<std::string> & value)358 void TextFieldModelImpl::SetFontFamily(const std::vector<std::string>& value)
359 {
360 auto* stack = ViewStackProcessor::GetInstance();
361 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
362 CHECK_NULL_VOID(component);
363 auto textStyle = component->GetEditingStyle();
364 textStyle.SetFontFamilies(value);
365 component->SetEditingStyle(textStyle);
366 }
367
SetInputFilter(const std::string & value,const std::function<void (const std::string &)> & onError)368 void TextFieldModelImpl::SetInputFilter(
369 const std::string& value, const std::function<void(const std::string&)>& onError)
370 {
371 auto* stack = ViewStackProcessor::GetInstance();
372 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
373 CHECK_NULL_VOID(component);
374 component->SetInputFilter(value);
375
376 if (onError) {
377 component->SetOnError(onError);
378 }
379 }
380
SetShowPasswordIcon(bool value)381 void TextFieldModelImpl::SetShowPasswordIcon(bool value)
382 {
383 auto* stack = ViewStackProcessor::GetInstance();
384 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
385 CHECK_NULL_VOID(component);
386 component->SetShowPasswordIcon(value);
387 }
388
SetOnEditChanged(std::function<void (bool)> && func)389 void TextFieldModelImpl::SetOnEditChanged(std::function<void(bool)>&& func)
390 {
391 auto* stack = ViewStackProcessor::GetInstance();
392 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
393 CHECK_NULL_VOID(component);
394 component->SetOnEditChanged(std::move(func));
395 }
396
SetOnSubmit(std::function<void (int32_t)> && func)397 void TextFieldModelImpl::SetOnSubmit(std::function<void(int32_t)>&& func)
398 {
399 auto* stack = ViewStackProcessor::GetInstance();
400 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
401 CHECK_NULL_VOID(component);
402 component->SetOnSubmit(std::move(func));
403 }
404
SetOnChange(std::function<void (const std::string &,PreviewText &)> && func)405 void TextFieldModelImpl::SetOnChange(std::function<void(const std::string&, PreviewText&)>&& func)
406 {
407 auto* stack = ViewStackProcessor::GetInstance();
408 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
409 CHECK_NULL_VOID(component);
410 auto onChange = [func] (const std::string& value) {
411 if (!func) {
412 PreviewText previewText {};
413 func(value, previewText);
414 }
415 };
416 component->SetOnChange(std::move(onChange));
417 }
418
SetOnCopy(std::function<void (const std::string &)> && func)419 void TextFieldModelImpl::SetOnCopy(std::function<void(const std::string&)>&& func)
420 {
421 auto* stack = ViewStackProcessor::GetInstance();
422 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
423 CHECK_NULL_VOID(component);
424 component->SetOnCopy(std::move(func));
425 }
426
SetOnCut(std::function<void (const std::string &)> && func)427 void TextFieldModelImpl::SetOnCut(std::function<void(const std::string&)>&& func)
428 {
429 auto* stack = ViewStackProcessor::GetInstance();
430 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
431 CHECK_NULL_VOID(component);
432 component->SetOnCut(std::move(func));
433 }
434
SetOnPaste(std::function<void (const std::string &)> && func)435 void TextFieldModelImpl::SetOnPaste(std::function<void(const std::string&)>&& func)
436 {
437 auto* stack = ViewStackProcessor::GetInstance();
438 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
439 CHECK_NULL_VOID(component);
440 component->SetOnPaste(std::move(func));
441 }
442
SetCopyOption(CopyOptions copyOption)443 void TextFieldModelImpl::SetCopyOption(CopyOptions copyOption)
444 {
445 JSViewSetProperty(&TextFieldComponent::SetCopyOption, copyOption);
446 }
447
SetBackgroundColor(const Color & color,bool tmp)448 void TextFieldModelImpl::SetBackgroundColor(const Color& color, bool tmp)
449 {
450 if (tmp) {
451 return;
452 }
453 auto* stack = ViewStackProcessor::GetInstance();
454 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
455 CHECK_NULL_VOID(component);
456 component->SetBgColor(color);
457 }
458
SetHeight(const Dimension & value)459 void TextFieldModelImpl::SetHeight(const Dimension& value)
460 {
461 auto* stack = ViewStackProcessor::GetInstance();
462 auto textInputComponent = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
463 CHECK_NULL_VOID(textInputComponent);
464 textInputComponent->SetHeight(value);
465 }
466
SetPadding(const NG::PaddingProperty & newPadding,Edge oldPadding,bool tmp)467 void TextFieldModelImpl::SetPadding(const NG::PaddingProperty& newPadding, Edge oldPadding, bool tmp)
468 {
469 if (tmp) {
470 return;
471 }
472 auto* stack = ViewStackProcessor::GetInstance();
473 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
474 CHECK_NULL_VOID(component);
475 auto decoration = component->GetDecoration();
476 CHECK_NULL_VOID(decoration);
477 decoration->SetPadding(oldPadding);
478 }
479
SetBackBorder()480 void TextFieldModelImpl::SetBackBorder()
481 {
482 auto* stack = ViewStackProcessor::GetInstance();
483 auto component = AceType::DynamicCast<OHOS::Ace::TextFieldComponent>(stack->GetMainComponent());
484 CHECK_NULL_VOID(component);
485 auto decoration = component->GetDecoration();
486 CHECK_NULL_VOID(decoration);
487 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
488 auto boxDecoration = box->GetBackDecoration();
489 CHECK_NULL_VOID(boxDecoration);
490 decoration->SetBorder(boxDecoration->GetBorder());
491 Border border = {};
492 boxDecoration->SetBorder(border);
493 component->SetOriginBorder(decoration->GetBorder());
494 }
495
SetHoverEffect(HoverEffectType value)496 void TextFieldModelImpl::SetHoverEffect(HoverEffectType value)
497 {
498 auto* stack = ViewStackProcessor::GetInstance();
499 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
500 CHECK_NULL_VOID(component);
501 component->SetHoverAnimationType(static_cast<HoverAnimationType>(value));
502 }
503
SetOnClick(std::function<void (const ClickInfo &)> && func)504 void TextFieldModelImpl::SetOnClick(std::function<void(const ClickInfo&)>&& func)
505 {
506 auto* stack = ViewStackProcessor::GetInstance();
507 auto component = AceType::DynamicCast<TextFieldComponent>(stack->GetMainComponent());
508 CHECK_NULL_VOID(component);
509 component->SetOnClick(std::move(func));
510 }
511
SetFocusableAndFocusNode()512 void TextFieldModelImpl::SetFocusableAndFocusNode()
513 {
514 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent();
515 if (focusableComponent) {
516 focusableComponent->SetFocusable(true);
517 }
518
519 auto focusNodeComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
520 if (focusNodeComponent) {
521 focusNodeComponent->SetFocusNode(false);
522 }
523 }
524
UpdateDecoration(const RefPtr<BoxComponent> & boxComponent,const RefPtr<TextFieldComponent> & component,const Border & boxBorder,const OHOS::Ace::RefPtr<OHOS::Ace::TextFieldTheme> & textFieldTheme)525 void TextFieldModelImpl::UpdateDecoration(const RefPtr<BoxComponent>& boxComponent,
526 const RefPtr<TextFieldComponent>& component, const Border& boxBorder,
527 const OHOS::Ace::RefPtr<OHOS::Ace::TextFieldTheme>& textFieldTheme)
528 {
529 if (!textFieldTheme) {
530 LOGI("UpdateDecoration: textFieldTheme is null.");
531 return;
532 }
533
534 RefPtr<Decoration> decoration = component->GetDecoration();
535 RefPtr<Decoration> boxDecoration = boxComponent->GetBackDecoration();
536 if (!decoration) {
537 decoration = AceType::MakeRefPtr<Decoration>();
538 }
539 if (boxDecoration) {
540 Border border = decoration->GetBorder();
541 border.SetLeftEdge(boxBorder.Left());
542 border.SetRightEdge(boxBorder.Right());
543 border.SetTopEdge(boxBorder.Top());
544 border.SetBottomEdge(boxBorder.Bottom());
545 border.SetBorderRadius(textFieldTheme->GetBorderRadius());
546 decoration->SetBorder(border);
547 component->SetOriginBorder(decoration->GetBorder());
548
549 if (boxDecoration->GetImage() || boxDecoration->GetGradient().IsValid()) {
550 // clear box properties except background image and radius.
551 boxDecoration->SetBackgroundColor(Color::TRANSPARENT);
552 Border border;
553 border.SetBorderRadius(textFieldTheme->GetBorderRadius());
554 boxDecoration->SetBorder(border);
555 }
556 } else {
557 boxDecoration = AceType::MakeRefPtr<Decoration>();
558 boxDecoration->SetBorderRadius(textFieldTheme->GetBorderRadius());
559 boxComponent->SetBackDecoration(boxDecoration);
560 }
561 }
562 } // namespace OHOS::Ace::Framework
563