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