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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_RENDER_TEXT_FIELD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_RENDER_TEXT_FIELD_H 18 19 #include <functional> 20 21 #include "base/geometry/dimension.h" 22 #include "base/geometry/offset.h" 23 #include "base/geometry/rect.h" 24 #include "base/geometry/size.h" 25 #include "base/memory/referenced.h" 26 #include "base/utils/system_properties.h" 27 #include "core/common/clipboard/clipboard.h" 28 #include "core/common/ime/text_edit_controller.h" 29 #include "core/common/ime/text_input_client.h" 30 #include "core/common/ime/text_input_connection.h" 31 #include "core/common/ime/text_input_formatter.h" 32 #include "core/common/ime/text_input_type.h" 33 #include "core/common/ime/text_selection.h" 34 #include "core/components/common/properties/color.h" 35 #include "core/components/common/properties/decoration.h" 36 #include "core/components/common/properties/text_style.h" 37 #include "core/components/image/render_image.h" 38 #include "core/components/panel/render_sliding_panel.h" 39 #include "core/components/text_field/text_field_component.h" 40 #include "core/components/text_overlay/text_overlay_manager.h" 41 #include "core/gestures/click_recognizer.h" 42 #include "core/gestures/long_press_recognizer.h" 43 #include "core/gestures/raw_recognizer.h" 44 #include "core/pipeline/base/overlay_show_option.h" 45 #include "core/pipeline/base/render_node.h" 46 47 #if defined(ENABLE_STANDARD_INPUT) 48 #include "commonlibrary/c_utils/base/include/refbase.h" 49 50 namespace OHOS::MiscServices { 51 class OnTextChangedListener; 52 } 53 #endif 54 55 namespace OHOS::Ace { 56 57 class ClickRecognizer; 58 class ClickInfo; 59 class TextOverlayComponent; 60 struct TextEditingValue; 61 62 // Currently only CHARACTER. 63 enum class CursorMoveSkip { 64 CHARACTER, // Smallest code unit. 65 WORDS, 66 SIBLING_SPACE, 67 PARAGRAPH, 68 }; 69 70 enum class InputAction { UNKNOWN, INSERT, DELETE_FORWARD, DELETE_BACKWARD }; 71 72 enum { 73 ACTION_SELECT_ALL, // Smallest code unit. 74 ACTION_UNDO, 75 ACTION_REDO, 76 ACTION_CUT, 77 ACTION_COPY, 78 ACTION_PASTE, 79 ACTION_SHARE, 80 ACTION_PASTE_AS_PLAIN_TEXT, 81 ACTION_REPLACE, 82 ACTION_ASSIST, 83 ACTION_AUTOFILL, 84 }; 85 86 const wchar_t UPPER_CASE_A = L'A'; 87 const std::wstring NUM_SYMBOLS = L")!@#$%^&*("; 88 const std::unordered_map<KeyCode, wchar_t> KEYBOARD_SYMBOLS = { 89 { KeyCode::KEY_GRAVE, L'`' }, 90 { KeyCode::KEY_MINUS, L'-' }, 91 { KeyCode::KEY_EQUALS, L'=' }, 92 { KeyCode::KEY_LEFT_BRACKET, L'[' }, 93 { KeyCode::KEY_RIGHT_BRACKET, L']' }, 94 { KeyCode::KEY_BACKSLASH, L'\\' }, 95 { KeyCode::KEY_SEMICOLON, L';' }, 96 { KeyCode::KEY_APOSTROPHE, L'\'' }, 97 { KeyCode::KEY_COMMA, L',' }, 98 { KeyCode::KEY_PERIOD, L'.' }, 99 { KeyCode::KEY_SLASH, L'/' }, 100 { KeyCode::KEY_SPACE, L' ' }, 101 { KeyCode::KEY_NUMPAD_DIVIDE, L'/' }, 102 { KeyCode::KEY_NUMPAD_MULTIPLY, L'*' }, 103 { KeyCode::KEY_NUMPAD_SUBTRACT, L'-' }, 104 { KeyCode::KEY_NUMPAD_ADD, L'+' }, 105 { KeyCode::KEY_NUMPAD_DOT, L'.' }, 106 { KeyCode::KEY_NUMPAD_COMMA, L',' }, 107 { KeyCode::KEY_NUMPAD_EQUALS, L'=' }, 108 }; 109 110 static const std::unordered_map<KeyCode, wchar_t> SHIFT_KEYBOARD_SYMBOLS = { 111 { KeyCode::KEY_GRAVE, L'~' }, 112 { KeyCode::KEY_MINUS, L'_' }, 113 { KeyCode::KEY_EQUALS, L'+' }, 114 { KeyCode::KEY_LEFT_BRACKET, L'{' }, 115 { KeyCode::KEY_RIGHT_BRACKET, L'}' }, 116 { KeyCode::KEY_BACKSLASH, L'|' }, 117 { KeyCode::KEY_SEMICOLON, L':' }, 118 { KeyCode::KEY_APOSTROPHE, L'\"' }, 119 { KeyCode::KEY_COMMA, L'<' }, 120 { KeyCode::KEY_PERIOD, L'>' }, 121 { KeyCode::KEY_SLASH, L'?' }, 122 }; 123 124 class RenderTextField : public RenderNode, public TextInputClient, public ValueChangeObserver { 125 DECLARE_ACE_TYPE(RenderTextField, RenderNode, TextInputClient, ValueChangeObserver); 126 127 public: 128 ~RenderTextField() override; 129 130 static RefPtr<RenderNode> Create(); 131 132 using TapCallback = std::function<bool(bool)>; 133 134 void Update(const RefPtr<Component>& component) override; 135 void PerformLayout() override; 136 void UpdateInsertText(std::string insertValue); 137 // Override TextInputClient 138 void UpdateEditingValue(const std::shared_ptr<TextEditingValue>& value, bool needFireChangeEvent = true) override; 139 void PerformDefaultAction(); 140 void PerformAction(TextInputAction action, bool forceCloseKeyboard = false) override; 141 void OnStatusChanged(RenderStatus renderStatus) override; 142 void OnValueChanged(bool needFireChangeEvent = true, bool needFireSelectChangeEvent = true) override; 143 void OnPaintFinish() override; 144 void Dump() override; 145 146 bool OnKeyEvent(const KeyEvent& event); 147 bool RequestKeyboard(bool isFocusViewChanged, bool needStartTwinkling = false, bool needShowSoftKeyboard = true); 148 bool CloseKeyboard(bool forceClose = false); 149 void ShowError(const std::string& errorText, bool resetToStart = true); 150 void ShowTextOverlay(const Offset& showOffset, bool isSingleHandle, bool isUsingMouse = false); 151 void SetOnValueChange(const std::function<void()>& onValueChange); 152 const std::function<void()>& GetOnValueChange() const; 153 void SetOnKeyboardClose(const std::function<void(bool)>& onKeyboardClose); 154 void SetOnClipRectChanged(const std::function<void(const Rect&)>& onClipRectChanged); 155 void SetUpdateHandlePosition(const std::function<void(const OverlayShowOption&)>& updateHandlePosition); 156 void SetUpdateHandleDiameter(const std::function<void(const double&)>& updateHandleDiameter); 157 void SetUpdateHandleDiameterInner(const std::function<void(const double&)>& updateHandleDiameterInner); 158 void SetIsOverlayShowed(bool isOverlayShowed, bool needStartTwinkling = true); 159 void UpdateFocusAnimation(); 160 const TextEditingValue& GetEditingValue() const; 161 const TextEditingValue& GetPreEditingValue() const; 162 double GetEditingBoxY() const override; 163 double GetEditingBoxTopY() const override; 164 bool GetEditingBoxModel() const override; 165 void Delete(int32_t start, int32_t end); 166 void CursorMoveLeft(CursorMoveSkip skip = CursorMoveSkip::CHARACTER); 167 void CursorMoveRight(CursorMoveSkip skip = CursorMoveSkip::CHARACTER); 168 void CursorMoveUp(); 169 void CursorMoveDown(); 170 void Insert(const std::string& text); 171 void StartTwinkling(); 172 void StopTwinkling(); 173 void EditingValueFilter(TextEditingValue& result); 174 void PopTextOverlay(); 175 RefPtr<RenderSlidingPanel> GetSlidingPanelAncest(); 176 void ResetOnFocusForTextFieldManager(); 177 void ResetSlidingPanelParentHeight(); 178 void HandleSetSelection(int32_t start, int32_t end); 179 void HandleExtendAction(int32_t action); 180 void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip); 181 std::u16string GetLeftTextOfCursor(int32_t number); 182 std::u16string GetRightTextOfCursor(int32_t number); 183 int32_t GetTextIndexAtCursor(); 184 bool IsSelected() const; 185 void DeleteLeft(); 186 void DeleteRight(); 187 bool HandleShiftPressedEvent(const KeyEvent& event); 188 void InsertValueDone(const std::string& appendElement); 189 SetInputFilter(const std::string & inputFilter)190 void SetInputFilter(const std::string& inputFilter) 191 { 192 inputFilter_ = inputFilter; 193 } 194 SetTextStyle(const TextStyle & style)195 void SetTextStyle(const TextStyle& style) 196 { 197 style_ = style; 198 } 199 SetOnOverlayFocusChange(const std::function<void (bool)> & onOverlayFocusChange)200 void SetOnOverlayFocusChange(const std::function<void(bool)>& onOverlayFocusChange) 201 { 202 onOverlayFocusChange_ = onOverlayFocusChange; 203 } 204 205 // Whether textOverlay is showed. IsOverlayShowed()206 bool IsOverlayShowed() const 207 { 208 return isOverlayShowed_; 209 } 210 SetOverlayColor(const Color & overlayColor)211 void SetOverlayColor(const Color& overlayColor) 212 { 213 overlayColor_ = overlayColor; 214 } 215 RegisterTapCallback(const TapCallback & callback)216 void RegisterTapCallback(const TapCallback& callback) 217 { 218 tapCallback_ = callback; 219 } 220 SetNextFocusEvent(const std::function<void ()> & event)221 void SetNextFocusEvent(const std::function<void()>& event) 222 { 223 moveNextFocusEvent_ = event; 224 } 225 SetSubmitEvent(std::function<void (const std::string &)> && event)226 void SetSubmitEvent(std::function<void(const std::string&)>&& event) 227 { 228 onSubmitEvent_ = event; 229 } 230 GetColor()231 const Color& GetColor() const 232 { 233 if (decoration_) { 234 return decoration_->GetBackgroundColor(); 235 } 236 return Color::TRANSPARENT; 237 } 238 SetColor(const Color & color)239 void SetColor(const Color& color) 240 { 241 if (decoration_) { 242 decoration_->SetBackgroundColor(color); 243 MarkNeedRender(); 244 } 245 } 246 GetHeight()247 double GetHeight() const 248 { 249 return height_.Value(); 250 } 251 GetDimensionHeight()252 const Dimension& GetDimensionHeight() const 253 { 254 return height_; 255 } 256 SetHeight(double height)257 void SetHeight(double height) 258 { 259 if (GreatOrEqual(height, 0.0) && !NearEqual(height_.Value(), height)) { 260 height_.SetValue(height); 261 MarkNeedLayout(); 262 } 263 } 264 GetStackElement()265 const WeakPtr<StackElement> GetStackElement() const 266 { 267 return stackElement_; 268 } 269 SetWidthReservedForSearch(double widthReservedForSearch)270 void SetWidthReservedForSearch(double widthReservedForSearch) 271 { 272 widthReservedForSearch_ = widthReservedForSearch; 273 } 274 SetPaddingHorizonForSearch(double paddingHorizon)275 void SetPaddingHorizonForSearch(double paddingHorizon) 276 { 277 paddingHorizonForSearch_ = paddingHorizon; 278 } 279 HasTextOverlayPushed()280 bool HasTextOverlayPushed() const 281 { 282 return hasTextOverlayPushed_; 283 } 284 GetIsInEditStatus()285 bool GetIsInEditStatus() const 286 { 287 return isInEditStatus_; 288 } 289 SetIsInEditStatus(bool isInEditStatus)290 void SetIsInEditStatus(bool isInEditStatus) 291 { 292 isInEditStatus_ = isInEditStatus; 293 } 294 GetPlaceholder()295 const std::string GetPlaceholder() const 296 { 297 return placeholder_; 298 } 299 GetValue()300 const std::string GetValue() const 301 { 302 return text_; 303 } 304 GetAction()305 TextInputAction GetAction() const 306 { 307 return action_; 308 } 309 GetKeyboard()310 TextInputType GetKeyboard() const 311 { 312 return keyboard_; 313 } 314 GetInactivePlaceholderColor()315 Color GetInactivePlaceholderColor() const 316 { 317 return inactivePlaceholderColor_; 318 } 319 GetPlaceHoldStyle()320 TextStyle GetPlaceHoldStyle() 321 { 322 return placeHoldStyle_; 323 } 324 GetTextAlign()325 TextAlign GetTextAlign() 326 { 327 return textAlign_; 328 } 329 GetCursorColor()330 Color GetCursorColor() const 331 { 332 return cursorColor_; 333 } 334 GetEditingStyle()335 TextStyle GetEditingStyle() 336 { 337 return editingStyle_; 338 } 339 GetMaxLength()340 int32_t GetMaxLength() 341 { 342 return maxLength_; 343 } 344 GetTextInputFilter()345 const std::string GetTextInputFilter() const 346 { 347 return inputFilter_; 348 } 349 SetTextOverlayPushed(bool hasTextOverlayPushed)350 void SetTextOverlayPushed(bool hasTextOverlayPushed) 351 { 352 hasTextOverlayPushed_ = hasTextOverlayPushed; 353 } 354 IsSingleHandle()355 bool IsSingleHandle() const 356 { 357 return isSingleHandle_; 358 } 359 GetInitIndex()360 int32_t GetInitIndex() const 361 { 362 return initIndex_; 363 } 364 SetInitIndex(int32_t initIndex)365 void SetInitIndex(int32_t initIndex) 366 { 367 initIndex_ = initIndex; 368 } 369 SetOnTextChangeEvent(const std::function<void (const std::string &)> & onTextChangeEvent)370 void SetOnTextChangeEvent(const std::function<void(const std::string&)>& onTextChangeEvent) 371 { 372 onTextChangeEvent_ = onTextChangeEvent; 373 } 374 SetOnValueChangeEvent(const std::function<void (const std::string &)> & onTextChangeEvent)375 void SetOnValueChangeEvent(const std::function<void(const std::string&)>& onTextChangeEvent) 376 { 377 onValueChangeEvent_ = onTextChangeEvent; 378 } 379 SetNeedNotifyChangeEvent(bool needNotifyChangeEvent)380 void SetNeedNotifyChangeEvent(bool needNotifyChangeEvent) 381 { 382 needNotifyChangeEvent_ = needNotifyChangeEvent; 383 } 384 385 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) SetInputMethodStatus(bool imeAttached)386 void SetInputMethodStatus(bool imeAttached) 387 { 388 imeAttached_ = imeAttached; 389 } 390 #endif 391 392 // distribute 393 std::string ProvideRestoreInfo() override; 394 395 int32_t instanceId_ = -1; 396 397 bool hasFocus_ = false; 398 void SetEditingValue(TextEditingValue&& newValue, bool needFireChangeEvent = true, bool isClearRecords = true); 399 void SetEditingValue(const std::string& text); 400 GetOnEditChanged()401 const std::function<void(bool)>& GetOnEditChanged() const 402 { 403 return onEditChanged_; 404 } 405 406 void OnEditChange(bool isInEditStatus); 407 void GetFieldAndOverlayTouchRect(std::vector<Rect>& resRectList); 408 SetInputStyle(InputStyle style)409 void SetInputStyle(InputStyle style) 410 { 411 inputStyle_ = style; 412 } 413 GetInputStyle()414 InputStyle GetInputStyle() 415 { 416 return inputStyle_; 417 } 418 419 void HandleOnBlur(); 420 SetCanPaintSelection(bool flag)421 void SetCanPaintSelection(bool flag) 422 { 423 canPaintSelection_ = flag; 424 } 425 GetCanPaintSelection()426 bool GetCanPaintSelection() const 427 { 428 return canPaintSelection_; 429 } 430 SetLastInputAction(InputAction action)431 void SetLastInputAction(InputAction action) 432 { 433 lastInputAction_ = action; 434 } 435 GetLastInputAction()436 InputAction GetLastInputAction() 437 { 438 return lastInputAction_; 439 } 440 441 bool NeedToFilter(); 442 HasSurfaceChangedCallback()443 bool HasSurfaceChangedCallback() 444 { 445 return surfaceChangedCallbackId_.has_value(); 446 } UpdateSurfaceChangedCallbackId(int32_t id)447 void UpdateSurfaceChangedCallbackId(int32_t id) 448 { 449 surfaceChangedCallbackId_ = id; 450 } 451 HasSurfacePositionChangedCallback()452 bool HasSurfacePositionChangedCallback() 453 { 454 return surfacePositionChangedCallbackId_.has_value(); 455 } UpdateSurfacePositionChangedCallbackId(int32_t id)456 void UpdateSurfacePositionChangedCallbackId(int32_t id) 457 { 458 surfacePositionChangedCallbackId_ = id; 459 } 460 461 protected: 462 // Describe where caret is and how tall visually. 463 struct CaretMetrics { ResetCaretMetrics464 void Reset() 465 { 466 offset.Reset(); 467 height = 0.0; 468 } 469 470 Offset offset; 471 // When caret is close to different glyphs, the height will be different. 472 double height = 0.0; ToStringCaretMetrics473 std::string ToString() const 474 { 475 std::string result = "Offset: "; 476 result += offset.ToString(); 477 result += ", height: "; 478 result += std::to_string(height); 479 return result; 480 } 481 }; 482 483 RenderTextField(); 484 virtual Size Measure(); 485 virtual int32_t GetCursorPositionForMoveUp() = 0; 486 virtual int32_t GetCursorPositionForMoveDown() = 0; 487 virtual int32_t GetCursorPositionForClick(const Offset& offset) = 0; 488 virtual Offset GetHandleOffset(int32_t extend) = 0; 489 virtual double PreferredLineHeight() = 0; 490 virtual int32_t AdjustCursorAndSelection(int32_t currentCursorPosition) = 0; 491 virtual DirectionStatus GetDirectionStatusOfPosition(int32_t position) const = 0; 492 virtual Size ComputeDeflateSizeOfErrorAndCountText() const = 0; ResetStatus()493 virtual void ResetStatus() {}; 494 495 void OnTouchTestHit( 496 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override; 497 void OnHiddenChanged(bool hidden) override; 498 void OnAppHide() override; 499 void OnClick(const ClickInfo& clickInfo); 500 void OnDoubleClick(const ClickInfo& clickInfo); 501 void OnLongPress(const LongPressInfo& longPressInfo); 502 bool HandleMouseEvent(const MouseEvent& event) override; 503 void HandleMouseHoverEvent(MouseState mouseState) override; 504 void AnimateMouseHoverEnter() override; 505 void AnimateMouseHoverExit() override; 506 void HandleValueFilter(TextEditingValue& valueBeforeUpdate, TextEditingValue& valueNeedToUpdate); 507 bool FilterWithRegex(std::string& valueToUpdate, const std::string& filter, bool needToEscape = false); 508 void KeyboardEditingValueFilter(TextEditingValue& valueToUpdate); 509 510 std::u16string GetTextForDisplay(const std::string& text) const; 511 512 void UpdateStartSelection(int32_t end, const Offset& pos, bool isSingleHandle, bool isLongPress); 513 void UpdateEndSelection(int32_t start, const Offset& pos); 514 void UpdateSelection(int32_t both); 515 void UpdateSelection(int32_t start, int32_t end); 516 void UpdateDirectionStatus(); 517 void UpdateCaretInfoToController(); 518 Offset GetPositionForExtend(int32_t extend, bool isSingleHandle); 519 /** 520 * Get grapheme cluster length before or after extend. 521 * For example, here is a sentence: 123 522 * The emoji character contains 2 code unit. Assumpt that the cursor is at the end (that will be 3+2+2 = 7). 523 * When calling GetGraphemeClusterLength(3, false), '' is not in Utf16Bmp, so result is 2. 524 * When calling GetGraphemeClusterLength(3, true), '3' is in Utf16Bmp, so result is 1. 525 */ 526 int32_t GetGraphemeClusterLength(int32_t extend, bool isPrefix) const; 527 HasConnection()528 bool HasConnection() const 529 { 530 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) 531 return imeAttached_; 532 #else 533 return connection_; 534 #endif 535 } 536 537 bool ShowCounter() const; 538 IsSelectiveDevice()539 static bool IsSelectiveDevice() 540 { 541 return (SystemProperties::GetDeviceType() != DeviceType::TV && 542 SystemProperties::GetDeviceType() != DeviceType::WATCH); 543 } 544 545 void AddOutOfRectCallbackToContext(); 546 547 // Used for compare to the current value and decide whether to UpdateRemoteEditing(). 548 std::shared_ptr<TextEditingValue> lastKnownRemoteEditingValue_; 549 550 // An outline for caret. It is used by default when the actual size cannot be retrieved. 551 Rect caretProto_; 552 Rect innerRect_; 553 // Click region of password icon. 554 Rect passwordIconRect_; 555 556 std::string placeholder_; 557 std::string inputFilter_; 558 Color placeholderColor_; 559 // Colors when not focused. 560 Color inactivePlaceholderColor_; 561 Color inactiveBgColor_; 562 Color inactiveTextColor_; 563 // Colors when focused. 564 Color focusPlaceholderColor_; 565 Color focusBgColor_; 566 Color focusTextColor_; 567 // Color when selected. 568 Color selectedColor_; 569 Color cursorColor_; 570 // Overlay color for hover and press. 571 Color overlayColor_ = Color::TRANSPARENT; 572 // The interval for caret twinkling, in ms. 573 uint32_t twinklingInterval = 0; 574 bool showPlaceholder_ = false; 575 bool cursorVisibility_ = false; 576 bool showCursor_ = true; 577 bool cursorColorIsSet_ = false; 578 Dimension cursorRadius_; 579 Dimension fontSize_; 580 // Used under case of [obscure_ == true]. 581 // When a character input, it will be displayed naked for a while. 582 // The remaining time to naked display the recent character is indicated by [obscureTickPendings_], 583 // multiply by [twinklingInterval]. For example, 3 * 500ms = 1500ms. 584 int32_t obscureTickPendings_ = 0; 585 // What the keyboard should appears. 586 TextInputType keyboard_ = TextInputType::UNSPECIFIED; 587 // Action when "enter" pressed. 588 TextInputAction action_ = TextInputAction::UNSPECIFIED; 589 std::string actionLabel_; 590 uint32_t maxLength_ = std::numeric_limits<uint32_t>::max(); 591 // Default to the start of text (according to RTL/LTR). 592 TextAlign textAlign_ = TextAlign::START; 593 // RTL/LTR is inherit from parent. 594 TextDirection textDirection_ = TextDirection::INHERIT; 595 TextDirection realTextDirection_ = TextDirection::INHERIT; 596 TextAffinity textAffinity_ = TextAffinity::DOWNSTREAM; 597 598 TextStyle countTextStyle_; 599 TextStyle overCountStyle_; 600 TextStyle countTextStyleOuter_; 601 TextStyle overCountStyleOuter_; 602 TextStyle style_; 603 TextStyle placeHoldStyle_; 604 TextStyle editingStyle_; 605 std::string text_; 606 std::string insertValue_; 607 bool insertTextUpdated_ = false; 608 std::string errorText_; 609 TextStyle errorTextStyle_; 610 double errorSpacing_ = 0.0; 611 Dimension errorSpacingInDimension_; 612 // Place error text in or under input. In input when device is TV, under input when device is phone. 613 bool errorIsInner_ = false; 614 Dimension errorBorderWidth_; 615 Color errorBorderColor_; 616 617 RefPtr<Decoration> decoration_; 618 Border originBorder_; 619 620 // One line TextField is more common in usual cases. 621 uint32_t maxLines_ = 1; 622 size_t textLines_ = 0; 623 size_t textLinesLast_ = 0; 624 int32_t cursorPositionForShow_ = 0; 625 CursorPositionType cursorPositionType_ = CursorPositionType::NORMAL; 626 DirectionStatus directionStatus_ = DirectionStatus::LEFT_LEFT; 627 CopyOptions copyOption_ = CopyOptions::Distributed; 628 629 bool showPasswordIcon_ = true; // Whether show password icon, effect only type is password. 630 bool showCounter_ = false; // Whether show counter, 10/100 means maxlength is 100 and 10 has been inputted. 631 bool overCount_ = false; // Whether count of text is over limit. 632 bool obscure_ = false; // Obscure the text, for example, password. 633 bool passwordRecord_ = true; // Record the status of password display or non-display. 634 bool enabled_ = true; // Whether input is disable of enable. 635 bool needFade_ = false; // Fade in/out text when overflow. 636 bool blockRightShade_ = false; 637 bool isValueFromFront_ = false; // Is value from developer-set. 638 bool isValueFromRemote_ = false; // Remote value coming form typing, other is from clopboard. 639 bool existStrongDirectionLetter_ = false; // Whether exist strong direction letter in text. 640 bool isVisible_ = true; 641 bool needNotifyChangeEvent_ = false; 642 bool resetToStart_ = true; // When finish inputting text, whether show header of text. 643 bool showEllipsis_ = false; // When text is overflow, whether show ellipsis. 644 bool extend_ = false; // Whether input support extend, this attribute is worked in textarea. 645 bool isCallbackCalled_ = false; // Whether custom font is loaded. 646 bool isOverlayShowed_ = false; // Whether overlay has showed. 647 bool isLongPressStatus_ = false; 648 double textHeight_ = 0.0; // Height of text. 649 double textHeightLast_ = 0.0; 650 double iconSize_ = 0.0; 651 double iconHotZoneSize_ = 0.0; 652 double extendHeight_ = 0.0; 653 double widthReservedForSearch_ = 0.0; // Width reserved for delete icon of search. 654 double paddingHorizonForSearch_ = 0.0; // Width reserved for search button of search. 655 double selectHeight_ = 0.0; 656 bool canPaintSelection_ = false; 657 Dimension height_; 658 Dimension iconSizeInDimension_; 659 Dimension iconHotZoneSizeInDimension_; 660 Dimension widthReserved_; 661 std::optional<LayoutParam> lastLayoutParam_; 662 std::optional<Color> imageFill_; 663 std::string iconSrc_; 664 std::string showIconSrc_; 665 std::string hideIconSrc_; 666 RefPtr<RenderImage> iconImage_; 667 RefPtr<RenderImage> renderShowIcon_; 668 RefPtr<RenderImage> renderHideIcon_; 669 670 Offset clickOffset_; 671 // For ensuring caret is visible on screen, we take a strategy that move the whole text painting area. 672 // It maybe seems rough, and doesn't support scrolling smoothly. 673 Offset textOffsetForShowCaret_; 674 InputStyle inputStyle_; 675 Rect caretRect_; 676 677 private: 678 void SetCallback(const RefPtr<TextFieldComponent>& textField); 679 void StartPressAnimation(bool isPressDown); 680 void StartHoverAnimation(bool isHovered); 681 void ScheduleCursorTwinkling(); 682 void OnCursorTwinkling(); 683 void CursorMoveOnClick(const Offset& offset); 684 void UpdateRemoteEditing(bool needFireChangeEvent = true); 685 void UpdateObscure(const RefPtr<TextFieldComponent>& textField); 686 void UpdateFormatters(); 687 void UpdateFocusStyles(); 688 void UpdateIcon(const RefPtr<TextFieldComponent>& textField); 689 void UpdatePasswordIcon(const RefPtr<TextFieldComponent>& textField); 690 void UpdateOverlay(); 691 void RegisterFontCallbacks(); 692 void HandleOnSelect(KeyCode keyCode, CursorMoveSkip skip = CursorMoveSkip::CHARACTER); 693 void HandleOnRevoke(); 694 void HandleOnInverseRevoke(); 695 void HandleOnCut(); 696 void HandleOnCopy(); 697 void HandleOnPaste(); 698 void HandleOnCopyAll(const std::function<void(const Offset&, const Offset&)>& callback); 699 void HandleOnStartHandleMove(int32_t end, const Offset& startHandleOffset, 700 const std::function<void(const Offset&)>& startCallback, bool isSingleHandle = false); 701 void HandleOnEndHandleMove( 702 int32_t start, const Offset& endHandleOffset, const std::function<void(const Offset&)>& endCallback); 703 RefPtr<StackElement> GetLastStack() const; 704 void InitAnimation(); 705 void RegisterCallbacksToOverlay(); 706 void PushTextOverlayToStack(); 707 void UpdateAccessibilityAttr(); 708 void InitAccessibilityEventListener(); 709 bool HandleKeyEvent(const KeyEvent& event); 710 void ClearEditingValue(); 711 virtual bool GetCaretRect(int32_t extent, Rect& caretRect, double caretHeightOffset = 0.0) const = 0; 712 bool SearchAction(const Offset& globalPosition, const Offset& globalOffset); 713 void ChangeCounterStyle(const TextEditingValue& value); 714 void ChangeBorderToErrorStyle(); 715 void HandleDeviceOrientationChange(); 716 void OnOverlayFocusChange(bool isFocus, bool needCloseKeyboard); 717 void FireSelectChangeIfNeeded(const TextEditingValue& newValue, bool needFireSelectChangeEvent) const; 718 int32_t GetInstanceId() const; 719 void ApplyAspectRatio(); // If aspect ratio is setted, height will follow box parent. 720 721 /** 722 * @brief Update remote editing value only if text or selection is changed. 723 */ 724 void UpdateRemoteEditingIfNeeded(bool needFireChangeEvent = true); 725 726 void AttachIme(); 727 728 // distribute 729 void ApplyRestoreInfo(); 730 void OnTapCallback(); 731 void HandleSurfacePositionChanged(int32_t posX, int32_t posY); 732 void HandleSurfaceChanged(int32_t newWidth, int32_t newHeight, int32_t prevWidth, int32_t prevHeight); 733 734 int32_t initIndex_ = 0; 735 bool isOverlayFocus_ = false; 736 bool isShiftDown_ = false; 737 bool isCtrlDown_ = false; 738 double fontScale_ = 1.0; 739 bool isSingleHandle_ = false; 740 bool hasTextOverlayPushed_ = false; 741 bool softKeyboardEnabled_ = true; 742 bool isInEditStatus_ = false; 743 bool isFocusOnTouch_ = true; 744 bool onTapCallbackResult_ = false; 745 InputAction lastInputAction_ = InputAction::UNKNOWN; 746 Color pressColor_; 747 Color hoverColor_; 748 TextSelection selection_; // Selection from custom. 749 DeviceOrientation deviceOrientation_ = DeviceOrientation::PORTRAIT; 750 std::function<void()> onValueChange_; 751 std::function<void(bool)> onKeyboardClose_; 752 std::function<void(const Rect&)> onClipRectChanged_; 753 std::function<void(const OverlayShowOption&)> updateHandlePosition_; 754 std::function<void(const double&)> updateHandleDiameter_; 755 std::function<void(const double&)> updateHandleDiameterInner_; 756 std::function<void(const std::string&)> onTextChangeEvent_; 757 std::function<void(std::string)> onChange_; 758 std::function<void(const ClickInfo& clickInfo)> onClick_; 759 std::function<void(const std::string&)> onError_; 760 std::function<void(bool)> onEditChanged_; 761 std::function<void(int32_t)> onSubmit_; 762 std::function<void(const std::string&)> onValueChangeEvent_; 763 std::function<void(const std::string&)> onSelectChangeEvent_; 764 std::function<void(const std::string&)> onFinishInputEvent_; 765 std::function<void(const std::string&)> onSubmitEvent_; 766 std::function<void()> onTapEvent_; 767 std::function<void()> onLongPressEvent_; 768 std::function<void()> moveNextFocusEvent_; 769 std::function<void(bool)> onOverlayFocusChange_; 770 std::function<void(std::string)> onCopy_; 771 std::function<void(std::string)> onCut_; 772 std::function<void(std::string)> onPaste_; 773 EventMarker onOptionsClick_; 774 EventMarker onTranslate_; 775 EventMarker onShare_; 776 EventMarker onSearch_; 777 bool catchMode_ = true; 778 TapCallback tapCallback_; 779 CancelableCallback<void()> cursorTwinklingTask_; 780 781 std::optional<int32_t> surfaceChangedCallbackId_; 782 std::optional<int32_t> surfacePositionChangedCallbackId_; 783 std::vector<InputOption> inputOptions_; 784 std::list<std::unique_ptr<TextInputFormatter>> textInputFormatters_; 785 RefPtr<TextEditController> controller_; 786 RefPtr<TextFieldController> textFieldController_; 787 RefPtr<TextInputConnection> connection_; 788 RefPtr<Clipboard> clipboard_; 789 RefPtr<TextOverlayComponent> textOverlay_; 790 WeakPtr<StackElement> stackElement_; 791 RefPtr<ClickRecognizer> clickRecognizer_; 792 RefPtr<ClickRecognizer> doubleClickRecognizer_; 793 RefPtr<LongPressRecognizer> longPressRecognizer_; 794 RefPtr<RawRecognizer> rawRecognizer_; 795 RefPtr<Animator> pressController_; 796 RefPtr<Animator> hoverController_; 797 RefPtr<Animator> animator_; 798 std::vector<TextEditingValue> operationRecords_; 799 std::vector<TextEditingValue> inverseOperationRecords_; 800 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) 801 bool imeAttached_ = false; 802 #endif 803 804 #if defined(ENABLE_STANDARD_INPUT) 805 sptr<OHOS::MiscServices::OnTextChangedListener> textChangeListener_; 806 #endif 807 }; 808 809 } // namespace OHOS::Ace 810 811 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_RENDER_TEXT_FIELD_H 812