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