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