1 /* 2 * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXTFIELD_TEXTFIELD_DECLARATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXTFIELD_TEXTFIELD_DECLARATION_H 18 19 #include "core/common/ime/text_edit_controller.h" 20 #include "core/common/ime/text_input_action.h" 21 #include "core/common/ime/text_input_type.h" 22 #include "core/common/ime/text_selection.h" 23 #include "core/components/common/properties/input_option.h" 24 #include "core/components/declaration/common/declaration.h" 25 #include "core/components/declaration/input/input_declaration.h" 26 #include "core/components/text_field/text_field_controller.h" 27 #include "frameworks/bridge/common/dom/dom_type.h" 28 #include "frameworks/core/components/text_field/text_field_controller.h" 29 30 namespace OHOS::Ace { 31 32 struct TextFieldAttribute : Attribute { 33 bool isValueUpdated = false; 34 std::string value; 35 std::string placeholder; 36 std::string inputFilter; 37 TextSelection selection; 38 Dimension widthReserved; 39 // Obscure the text, for example, password. 40 bool obscure = false; 41 // Whether show counter, should work together with maxLength. 42 bool showCounter = false; 43 // Whether height of textfield can auto-extend. 44 bool extend = false; 45 bool autoFocus = false; 46 bool showEllipsis = false; 47 bool blockRightShade = false; 48 bool isVisible = true; 49 bool resetToStart = true; 50 bool hasSetResetToStart = false; 51 bool showCursor = true; 52 bool enabled = true; 53 bool needFade = false; 54 bool lengthLimited = false; 55 uint32_t maxLength = std::numeric_limits<uint32_t>::max(); 56 uint32_t textMaxLines = 1; 57 58 bool softKeyboardEnabled = true; 59 // Type of input method. 60 TextInputType keyboard = TextInputType::TEXT; 61 // Action when "enter" pressed. 62 TextInputAction action = TextInputAction::UNSPECIFIED; 63 std::string actionLabel; 64 65 // Attribute about header-icon of text-field. 66 std::string iconImage; 67 Dimension iconSize; 68 Dimension iconHotZoneSize; 69 // Icon to control password show or hide. 70 std::string showImage; 71 std::string hideImage; 72 bool showPasswordIcon = true; 73 74 // Menu options of text overlay 75 std::vector<InputOption> inputOptions; 76 }; 77 78 struct TextFieldStyle : Style { 79 Dimension height; 80 81 // style about cursor 82 Dimension cursorRadius; 83 Color cursorColor; 84 bool cursorColorIsSet = false; 85 86 TextAlign textAlign = TextAlign::START; 87 TextStyle textStyle; 88 TextStyle countTextStyle; 89 TextStyle overCountStyle; 90 TextStyle countTextStyleOuter; 91 TextStyle overCountStyleOuter; 92 TextStyle editingStyle; 93 TextStyle placeHoldStyle; 94 95 Color textColor; 96 Color focusTextColor; 97 Color placeholderColor; 98 Color focusPlaceholderColor; 99 Color bgColor; 100 Color focusBgColor; 101 Color selectedColor; 102 Color hoverColor; 103 Color pressColor; 104 105 // Style about error text 106 // Whether error text show inner or under. 107 bool errorIsInner = false; 108 TextStyle errorTextStyle; 109 // Spacing between error text and input text. 110 Dimension errorSpacing; 111 Dimension errorBorderWidth; 112 Color errorBorderColor; 113 Border originBorder; 114 115 RefPtr<Decoration> decoration; 116 }; 117 118 struct TextFieldEvent : Event { 119 // Trigger when text is changed. 120 EventMarker onTextChange; 121 // Trigger when selection is changed 122 EventMarker onSelectChange; 123 // Trigger when user press "enter" 124 EventMarker onFinishInput; 125 // Trigger when user click 126 EventMarker onTap; 127 // Trigger when user long press 128 EventMarker onLongPress; 129 // Trigger when user click option of menu 130 EventMarker onOptionsClick; 131 // Trigger when user click translate button of text overlay 132 EventMarker onTranslate; 133 // Trigger when user click share button of text overlay 134 EventMarker onShare; 135 // Trigger when user click search button of text overlay 136 EventMarker onSearch; 137 }; 138 139 struct TextFieldMethod : Method { DeleteTextFieldMethod140 void Delete(const RefPtr<TextFieldController>& textFieldController) const 141 { 142 if (textFieldController) { 143 textFieldController->Delete(); 144 } 145 } 146 }; 147 148 class TextFieldDeclaration : public Declaration { 149 DECLARE_ACE_TYPE(TextFieldDeclaration, Declaration); 150 151 public: 152 TextFieldDeclaration() = default; 153 ~TextFieldDeclaration() override = default; 154 155 void InitializeStyle() override; 156 157 bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override; 158 bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override; 159 bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override; 160 void CallSpecializedMethod(const std::string& method, const std::string& args) override; 161 void OnRequestFocus(bool shouldFocus) override; 162 GetInputFilter()163 const std::string& GetInputFilter() const 164 { 165 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 166 return attribute.inputFilter; 167 } 168 SetInputFilter(const std::string & inputFilter)169 void SetInputFilter(const std::string& inputFilter) 170 { 171 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 172 attribute.inputFilter = inputFilter; 173 } 174 GetValue()175 const std::string& GetValue() const 176 { 177 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 178 return attribute.value; 179 } 180 SetValue(const std::string & value)181 void SetValue(const std::string& value) 182 { 183 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 184 attribute.value = value; 185 attribute.isValueUpdated = true; 186 } 187 IsValueUpdated()188 bool IsValueUpdated() const 189 { 190 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 191 return attribute.isValueUpdated; 192 } 193 SetIsValueUpdated(bool isValueUpdated)194 void SetIsValueUpdated(bool isValueUpdated) 195 { 196 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 197 attribute.isValueUpdated = isValueUpdated; 198 } 199 SetPlaceHoldStyle(const TextStyle & textstyle)200 void SetPlaceHoldStyle(const TextStyle& textstyle) 201 { 202 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 203 style.placeHoldStyle = textstyle; 204 } 205 SetEditingStyle(const TextStyle & textstyle)206 void SetEditingStyle(const TextStyle& textstyle) 207 { 208 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 209 style.editingStyle = textstyle; 210 } 211 GetPlaceHoldStyle()212 const TextStyle& GetPlaceHoldStyle() const 213 { 214 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 215 return style.placeHoldStyle; 216 } 217 GetEditingStyle()218 const TextStyle& GetEditingStyle() const 219 { 220 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 221 return style.editingStyle; 222 } 223 GetPlaceholder()224 const std::string& GetPlaceholder() const 225 { 226 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 227 return attribute.placeholder; 228 } 229 SetPlaceholder(const std::string & placeholder)230 void SetPlaceholder(const std::string& placeholder) 231 { 232 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 233 attribute.placeholder = placeholder; 234 } 235 GetSelection()236 const TextSelection& GetSelection() const 237 { 238 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 239 return attribute.selection; 240 } 241 SetSelectedStart(int32_t selectedStart)242 void SetSelectedStart(int32_t selectedStart) 243 { 244 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 245 attribute.selection.baseOffset = selectedStart; 246 } 247 SetSelectedEnd(int32_t selectedEnd)248 void SetSelectedEnd(int32_t selectedEnd) 249 { 250 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 251 attribute.selection.extentOffset = selectedEnd; 252 } 253 GetPlaceholderColor()254 const Color& GetPlaceholderColor() const 255 { 256 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 257 return style.placeholderColor; 258 } 259 SetPlaceholderColor(const Color & placeholderColor)260 void SetPlaceholderColor(const Color& placeholderColor) 261 { 262 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 263 style.placeholderColor = placeholderColor; 264 } 265 SetTextMaxLines(uint32_t textMaxLines)266 void SetTextMaxLines(uint32_t textMaxLines) 267 { 268 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 269 attribute.textMaxLines = textMaxLines; 270 } 271 GetTextMaxLines()272 uint32_t GetTextMaxLines() const 273 { 274 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 275 return attribute.textMaxLines; 276 } 277 GetTextAlign()278 TextAlign GetTextAlign() const 279 { 280 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 281 return style.textAlign; 282 } 283 SetTextAlign(TextAlign textAlign)284 void SetTextAlign(TextAlign textAlign) 285 { 286 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 287 style.textAlign = textAlign; 288 } 289 GetTextStyle()290 TextStyle& GetTextStyle() const 291 { 292 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 293 return style.textStyle; 294 } 295 SetTextStyle(const TextStyle & textStyle)296 void SetTextStyle(const TextStyle& textStyle) 297 { 298 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 299 style.textStyle = textStyle; 300 } 301 GetErrorTextStyle()302 const TextStyle& GetErrorTextStyle() const 303 { 304 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 305 return style.errorTextStyle; 306 } 307 SetErrorTextStyle(const TextStyle & errorTextStyle)308 void SetErrorTextStyle(const TextStyle& errorTextStyle) 309 { 310 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 311 style.errorTextStyle = errorTextStyle; 312 } 313 GetErrorSpacing()314 const Dimension& GetErrorSpacing() const 315 { 316 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 317 return style.errorSpacing; 318 } 319 SetErrorSpacing(const Dimension & errorSpacing)320 void SetErrorSpacing(const Dimension& errorSpacing) 321 { 322 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 323 style.errorSpacing = errorSpacing; 324 } 325 GetErrorIsInner()326 bool GetErrorIsInner() const 327 { 328 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 329 return style.errorIsInner; 330 } 331 SetErrorIsInner(bool errorIsInner)332 void SetErrorIsInner(bool errorIsInner) 333 { 334 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 335 style.errorIsInner = errorIsInner; 336 } 337 GetErrorBorderWidth()338 const Dimension& GetErrorBorderWidth() const 339 { 340 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 341 return style.errorBorderWidth; 342 } 343 SetErrorBorderWidth(const Dimension & errorBorderWidth)344 void SetErrorBorderWidth(const Dimension& errorBorderWidth) 345 { 346 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 347 style.errorBorderWidth = errorBorderWidth; 348 } 349 GetErrorBorderColor()350 const Color& GetErrorBorderColor() const 351 { 352 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 353 return style.errorBorderColor; 354 } 355 SetErrorBorderColor(const Color & errorBorderColor)356 void SetErrorBorderColor(const Color& errorBorderColor) 357 { 358 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 359 style.errorBorderColor = errorBorderColor; 360 } 361 NeedFade()362 bool NeedFade() const 363 { 364 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 365 return attribute.needFade; 366 } 367 SetNeedFade(bool needFade)368 void SetNeedFade(bool needFade) 369 { 370 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 371 attribute.needFade = needFade; 372 } 373 IsSoftKeyboardEnabled()374 bool IsSoftKeyboardEnabled() const 375 { 376 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 377 return attribute.softKeyboardEnabled; 378 } 379 SetSoftKeyboardEnabled(bool softKeyboardEnabled)380 void SetSoftKeyboardEnabled(bool softKeyboardEnabled) 381 { 382 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 383 attribute.softKeyboardEnabled = softKeyboardEnabled; 384 } 385 GetDecoration()386 RefPtr<Decoration> GetDecoration() const 387 { 388 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 389 return style.decoration; 390 } 391 SetDecoration(const RefPtr<Decoration> & decoration)392 void SetDecoration(const RefPtr<Decoration>& decoration) 393 { 394 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 395 style.decoration = decoration; 396 } 397 SetOriginBorder(const Border & originBorder)398 void SetOriginBorder(const Border& originBorder) 399 { 400 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 401 style.originBorder = originBorder; 402 } 403 GetOriginBorder()404 const Border& GetOriginBorder() const 405 { 406 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 407 return style.originBorder; 408 } 409 ShowCursor()410 bool ShowCursor() const 411 { 412 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 413 return attribute.showCursor; 414 } 415 SetShowCursor(bool showCursor)416 void SetShowCursor(bool showCursor) 417 { 418 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 419 attribute.showCursor = showCursor; 420 } 421 NeedObscure()422 bool NeedObscure() const 423 { 424 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 425 return attribute.obscure; 426 } 427 SetObscure(bool obscure)428 void SetObscure(bool obscure) 429 { 430 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 431 attribute.obscure = obscure; 432 } 433 IsEnabled()434 bool IsEnabled() const 435 { 436 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 437 return attribute.enabled; 438 } 439 SetEnabled(bool enable)440 void SetEnabled(bool enable) 441 { 442 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 443 attribute.enabled = enable; 444 } 445 GetTextInputType()446 TextInputType GetTextInputType() const 447 { 448 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 449 return attribute.keyboard; 450 } 451 SetTextInputType(TextInputType type)452 void SetTextInputType(TextInputType type) 453 { 454 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 455 attribute.keyboard = type; 456 } 457 GetAction()458 TextInputAction GetAction() const 459 { 460 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 461 return attribute.action; 462 } 463 SetAction(TextInputAction action)464 void SetAction(TextInputAction action) 465 { 466 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 467 attribute.action = action; 468 } 469 SetCursorColor(const Color & color)470 void SetCursorColor(const Color& color) 471 { 472 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 473 style.cursorColor = color; 474 style.cursorColorIsSet = true; 475 } 476 GetCursorColor()477 const Color& GetCursorColor() 478 { 479 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 480 return style.cursorColor; 481 } 482 IsCursorColorSet()483 bool IsCursorColorSet() const 484 { 485 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 486 return style.cursorColorIsSet; 487 } 488 SetCursorRadius(const Dimension & radius)489 void SetCursorRadius(const Dimension& radius) 490 { 491 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 492 style.cursorRadius = radius; 493 } 494 GetCursorRadius()495 const Dimension& GetCursorRadius() const 496 { 497 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 498 return style.cursorRadius; 499 } 500 GetActionLabel()501 const std::string& GetActionLabel() const 502 { 503 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 504 return attribute.actionLabel; 505 } 506 SetActionLabel(const std::string & actionLabel)507 void SetActionLabel(const std::string& actionLabel) 508 { 509 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 510 attribute.actionLabel = actionLabel; 511 } 512 GetMaxLength()513 uint32_t GetMaxLength() const 514 { 515 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 516 return attribute.maxLength; 517 } 518 SetMaxLength(uint32_t maxLength)519 void SetMaxLength(uint32_t maxLength) 520 { 521 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 522 attribute.maxLength = maxLength; 523 attribute.lengthLimited = true; 524 } 525 IsTextLengthLimited()526 bool IsTextLengthLimited() const 527 { 528 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 529 return attribute.lengthLimited; 530 } 531 GetHeight()532 const Dimension& GetHeight() const 533 { 534 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 535 return style.height; 536 } 537 SetHeight(const Dimension & height)538 void SetHeight(const Dimension& height) 539 { 540 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 541 style.height = height; 542 } 543 GetAutoFocus()544 bool GetAutoFocus() const 545 { 546 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 547 return attribute.autoFocus; 548 } 549 SetAutoFocus(bool autoFocus)550 void SetAutoFocus(bool autoFocus) 551 { 552 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 553 attribute.autoFocus = autoFocus; 554 } 555 IsExtend()556 bool IsExtend() const 557 { 558 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 559 return attribute.extend; 560 } 561 SetExtend(bool extend)562 void SetExtend(bool extend) 563 { 564 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 565 attribute.extend = extend; 566 } 567 ShowEllipsis()568 bool ShowEllipsis() const 569 { 570 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 571 return attribute.showEllipsis; 572 } 573 SetShowEllipsis(bool showEllipsis)574 void SetShowEllipsis(bool showEllipsis) 575 { 576 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 577 attribute.showEllipsis = showEllipsis; 578 } 579 ShowPasswordIcon()580 bool ShowPasswordIcon() const 581 { 582 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 583 return attribute.showPasswordIcon; 584 } 585 SetShowPasswordIcon(bool showPasswordIcon)586 void SetShowPasswordIcon(bool showPasswordIcon) 587 { 588 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 589 attribute.showPasswordIcon = showPasswordIcon; 590 } 591 GetImageFill()592 const std::optional<Color>& GetImageFill() const 593 { 594 auto& imageStyle = static_cast<CommonImageStyle&>(GetStyle(StyleTag::COMMON_IMAGE_STYLE)); 595 return imageStyle.imageFill; 596 } 597 SetImageFill(const std::optional<Color> & color)598 void SetImageFill(const std::optional<Color>& color) 599 { 600 auto& imageStyle = MaybeResetStyle<CommonImageStyle>(StyleTag::COMMON_IMAGE_STYLE); 601 if (color != std::nullopt) { 602 imageStyle.imageFill = color.value(); 603 } 604 } 605 GetIconImage()606 const std::string& GetIconImage() const 607 { 608 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 609 return attribute.iconImage; 610 } 611 SetIconImage(const std::string & iconImage)612 void SetIconImage(const std::string& iconImage) 613 { 614 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 615 attribute.iconImage = iconImage; 616 } 617 GetShowIconImage()618 const std::string& GetShowIconImage() const 619 { 620 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 621 return attribute.showImage; 622 } 623 SetShowIconImage(const std::string & showImage)624 void SetShowIconImage(const std::string& showImage) 625 { 626 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 627 attribute.showImage = showImage; 628 } 629 GetHideIconImage()630 const std::string& GetHideIconImage() const 631 { 632 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 633 return attribute.hideImage; 634 } 635 SetHideIconImage(const std::string & hideImage)636 void SetHideIconImage(const std::string& hideImage) 637 { 638 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 639 attribute.hideImage = hideImage; 640 } 641 GetIconSize()642 const Dimension& GetIconSize() const 643 { 644 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 645 return attribute.iconSize; 646 } 647 SetIconSize(const Dimension & iconSize)648 void SetIconSize(const Dimension& iconSize) 649 { 650 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 651 attribute.iconSize = iconSize; 652 } 653 GetIconHotZoneSize()654 const Dimension& GetIconHotZoneSize() const 655 { 656 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 657 return attribute.iconHotZoneSize; 658 } 659 SetIconHotZoneSize(const Dimension & iconHotZoneSize)660 void SetIconHotZoneSize(const Dimension& iconHotZoneSize) 661 { 662 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 663 attribute.iconHotZoneSize = iconHotZoneSize; 664 } 665 SetFocusBgColor(const Color & focusBgColor)666 void SetFocusBgColor(const Color& focusBgColor) 667 { 668 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 669 style.focusBgColor = focusBgColor; 670 } 671 GetFocusBgColor()672 const Color& GetFocusBgColor() 673 { 674 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 675 return style.focusBgColor; 676 } 677 SetFocusPlaceholderColor(const Color & focusPlaceholderColor)678 void SetFocusPlaceholderColor(const Color& focusPlaceholderColor) 679 { 680 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 681 style.focusPlaceholderColor = focusPlaceholderColor; 682 } 683 GetFocusPlaceholderColor()684 const Color& GetFocusPlaceholderColor() 685 { 686 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 687 return style.focusPlaceholderColor; 688 } 689 SetFocusTextColor(const Color & focusTextColor)690 void SetFocusTextColor(const Color& focusTextColor) 691 { 692 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 693 style.focusTextColor = focusTextColor; 694 } 695 GetFocusTextColor()696 const Color& GetFocusTextColor() 697 { 698 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 699 return style.focusTextColor; 700 } 701 SetBgColor(const Color & bgColor)702 void SetBgColor(const Color& bgColor) 703 { 704 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 705 style.bgColor = bgColor; 706 } 707 GetBgColor()708 const Color& GetBgColor() 709 { 710 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 711 return style.bgColor; 712 } 713 SetTextColor(const Color & textColor)714 void SetTextColor(const Color& textColor) 715 { 716 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 717 style.textColor = textColor; 718 } 719 GetTextColor()720 const Color& GetTextColor() 721 { 722 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 723 return style.textColor; 724 } 725 SetWidthReserved(const Dimension & widthReserved)726 void SetWidthReserved(const Dimension& widthReserved) 727 { 728 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 729 attribute.widthReserved = widthReserved; 730 } 731 GetWidthReserved()732 const Dimension& GetWidthReserved() const 733 { 734 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 735 return attribute.widthReserved; 736 } 737 GetSelectedColor()738 const Color& GetSelectedColor() const 739 { 740 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 741 return style.selectedColor; 742 } 743 SetSelectedColor(const Color & selectedColor)744 void SetSelectedColor(const Color& selectedColor) 745 { 746 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 747 style.selectedColor = selectedColor; 748 } 749 GetHoverColor()750 const Color& GetHoverColor() const 751 { 752 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 753 return style.hoverColor; 754 } 755 SetHoverColor(const Color & hoverColor)756 void SetHoverColor(const Color& hoverColor) 757 { 758 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 759 style.hoverColor = hoverColor; 760 } 761 GetPressColor()762 const Color& GetPressColor() const 763 { 764 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 765 return style.pressColor; 766 } 767 SetPressColor(const Color & pressColor)768 void SetPressColor(const Color& pressColor) 769 { 770 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 771 style.pressColor = pressColor; 772 } 773 SetBlockRightShade(bool blockRightShade)774 void SetBlockRightShade(bool blockRightShade) 775 { 776 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 777 attribute.blockRightShade = blockRightShade; 778 } 779 GetBlockRightShade()780 bool GetBlockRightShade() const 781 { 782 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 783 return attribute.blockRightShade; 784 } 785 SetIsVisible(bool isVisible)786 void SetIsVisible(bool isVisible) 787 { 788 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 789 attribute.isVisible = isVisible; 790 } 791 IsVisible()792 bool IsVisible() const 793 { 794 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 795 return attribute.isVisible; 796 } 797 SetResetToStart(bool resetToStart)798 void SetResetToStart(bool resetToStart) 799 { 800 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 801 attribute.resetToStart = resetToStart; 802 attribute.hasSetResetToStart = true; 803 } 804 GetResetToStart()805 bool GetResetToStart() const 806 { 807 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 808 return attribute.resetToStart; 809 } 810 HasSetResetToStart()811 bool HasSetResetToStart() const 812 { 813 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 814 return attribute.hasSetResetToStart; 815 } 816 SetShowCounter(bool showCounter)817 void SetShowCounter(bool showCounter) 818 { 819 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 820 attribute.showCounter = showCounter; 821 } 822 ShowCounter()823 bool ShowCounter() const 824 { 825 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 826 return attribute.showCounter; 827 } 828 SetCountTextStyle(const TextStyle & countTextStyle)829 void SetCountTextStyle(const TextStyle& countTextStyle) 830 { 831 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 832 style.countTextStyle = countTextStyle; 833 } 834 GetCountTextStyle()835 const TextStyle& GetCountTextStyle() const 836 { 837 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 838 return style.countTextStyle; 839 } 840 SetOverCountStyle(const TextStyle & overCountStyle)841 void SetOverCountStyle(const TextStyle& overCountStyle) 842 { 843 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 844 style.overCountStyle = overCountStyle; 845 } 846 GetOverCountStyle()847 const TextStyle& GetOverCountStyle() const 848 { 849 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 850 return style.overCountStyle; 851 } 852 SetCountTextStyleOuter(const TextStyle & countTextStyleOuter)853 void SetCountTextStyleOuter(const TextStyle& countTextStyleOuter) 854 { 855 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 856 style.countTextStyleOuter = countTextStyleOuter; 857 } 858 GetCountTextStyleOuter()859 const TextStyle& GetCountTextStyleOuter() const 860 { 861 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 862 return style.countTextStyleOuter; 863 } 864 SetOverCountStyleOuter(const TextStyle & overCountStyleOuter)865 void SetOverCountStyleOuter(const TextStyle& overCountStyleOuter) 866 { 867 auto& style = MaybeResetStyle<TextFieldStyle>(StyleTag::SPECIALIZED_STYLE); 868 style.overCountStyleOuter = overCountStyleOuter; 869 } 870 GetOverCountStyleOuter()871 const TextStyle& GetOverCountStyleOuter() const 872 { 873 auto& style = static_cast<TextFieldStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 874 return style.overCountStyleOuter; 875 } 876 SetInputOptions(const std::vector<InputOption> & inputOptions)877 void SetInputOptions(const std::vector<InputOption>& inputOptions) 878 { 879 auto& attribute = MaybeResetAttribute<TextFieldAttribute>(AttributeTag::SPECIALIZED_ATTR); 880 attribute.inputOptions = inputOptions; 881 } 882 GetInputOptions()883 const std::vector<InputOption>& GetInputOptions() const 884 { 885 auto& attribute = static_cast<TextFieldAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 886 return attribute.inputOptions; 887 } 888 GetOnTextChange()889 const EventMarker& GetOnTextChange() const 890 { 891 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 892 return event.onTextChange; 893 } 894 SetOnTextChange(const EventMarker & onTextChange)895 void SetOnTextChange(const EventMarker& onTextChange) 896 { 897 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 898 event.onTextChange = onTextChange; 899 } 900 SetOnTextChangeFunction(std::function<void (const std::string &)> && onTextChangeCallback)901 void SetOnTextChangeFunction(std::function<void(const std::string&)>&& onTextChangeCallback) 902 { 903 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 904 event.onTextChange.SetUiStrFunction(std::move(onTextChangeCallback)); 905 } 906 GetOnSelectChange()907 const EventMarker& GetOnSelectChange() const 908 { 909 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 910 return event.onSelectChange; 911 } 912 SetOnSelectChange(const EventMarker & onSelectChange)913 void SetOnSelectChange(const EventMarker& onSelectChange) 914 { 915 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 916 event.onSelectChange = onSelectChange; 917 } 918 GetOnFinishInput()919 const EventMarker& GetOnFinishInput() const 920 { 921 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 922 return event.onFinishInput; 923 } 924 SetOnFinishInput(const EventMarker & onFinishInput)925 void SetOnFinishInput(const EventMarker& onFinishInput) 926 { 927 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 928 event.onFinishInput = onFinishInput; 929 } 930 GetOnTap()931 const EventMarker& GetOnTap() const 932 { 933 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 934 return event.onTap; 935 } 936 SetOnTap(const EventMarker & onTap)937 void SetOnTap(const EventMarker& onTap) 938 { 939 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 940 event.onTap = onTap; 941 } 942 GetOnLongPress()943 const EventMarker& GetOnLongPress() const 944 { 945 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 946 return event.onLongPress; 947 } 948 SetOnLongPress(const EventMarker & onLongPress)949 void SetOnLongPress(const EventMarker& onLongPress) 950 { 951 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 952 event.onLongPress = onLongPress; 953 } 954 GetOnOptionsClick()955 const EventMarker& GetOnOptionsClick() const 956 { 957 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 958 return event.onOptionsClick; 959 } 960 SetOnOptionsClick(const EventMarker & onOptionsClick)961 void SetOnOptionsClick(const EventMarker& onOptionsClick) 962 { 963 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 964 event.onOptionsClick = onOptionsClick; 965 } 966 GetOnTranslate()967 const EventMarker& GetOnTranslate() const 968 { 969 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 970 return event.onTranslate; 971 } 972 SetOnTranslate(const EventMarker & onTranslate)973 void SetOnTranslate(const EventMarker& onTranslate) 974 { 975 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 976 event.onTranslate = onTranslate; 977 } 978 GetOnShare()979 const EventMarker& GetOnShare() const 980 { 981 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 982 return event.onShare; 983 } 984 SetOnShare(const EventMarker & onShare)985 void SetOnShare(const EventMarker& onShare) 986 { 987 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 988 event.onShare = onShare; 989 } 990 GetOnSearch()991 const EventMarker& GetOnSearch() const 992 { 993 auto& event = static_cast<TextFieldEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 994 return event.onSearch; 995 } 996 SetOnSearch(const EventMarker & onSearch)997 void SetOnSearch(const EventMarker& onSearch) 998 { 999 auto& event = MaybeResetEvent<TextFieldEvent>(EventTag::SPECIALIZED_EVENT); 1000 event.onSearch = onSearch; 1001 } 1002 GetTextEditController()1003 const RefPtr<TextEditController>& GetTextEditController() const 1004 { 1005 return textEditController_; 1006 } 1007 SetTextEditController(const RefPtr<TextEditController> & controller)1008 void SetTextEditController(const RefPtr<TextEditController>& controller) 1009 { 1010 textEditController_ = controller; 1011 } 1012 GetTextFieldController()1013 const RefPtr<TextFieldController>& GetTextFieldController() const 1014 { 1015 return textFieldController_; 1016 } 1017 SetTextFieldController(const RefPtr<TextFieldController> & controller)1018 void SetTextFieldController(const RefPtr<TextFieldController>& controller) 1019 { 1020 textFieldController_ = controller; 1021 } 1022 HasBoxRadius()1023 bool HasBoxRadius() const 1024 { 1025 return hasBoxRadius_; 1026 } 1027 1028 protected: 1029 void InitSpecialized() override; 1030 1031 private: 1032 bool hasBoxRadius_ = false; 1033 RefPtr<TextEditController> textEditController_; 1034 RefPtr<TextFieldController> textFieldController_; 1035 }; 1036 1037 } // namespace OHOS::Ace 1038 1039 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_TEXTFIELD_TEXTFIELD_DECLARATION_H