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 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 SetInputFilter(const std::string & inputFilter)176 void SetInputFilter(const std::string& inputFilter) 177 { 178 inputFilter_ = inputFilter; 179 } 180 SetTextStyle(const TextStyle & style)181 void SetTextStyle(const TextStyle& style) 182 { 183 style_ = style; 184 } 185 SetOnOverlayFocusChange(const std::function<void (bool)> & onOverlayFocusChange)186 void SetOnOverlayFocusChange(const std::function<void(bool)>& onOverlayFocusChange) 187 { 188 onOverlayFocusChange_ = onOverlayFocusChange; 189 } 190 191 // Whether textOverlay is showed. IsOverlayShowed()192 bool IsOverlayShowed() const 193 { 194 return isOverlayShowed_; 195 } 196 SetOverlayColor(const Color & overlayColor)197 void SetOverlayColor(const Color& overlayColor) 198 { 199 overlayColor_ = overlayColor; 200 } 201 RegisterTapCallback(const TapCallback & callback)202 void RegisterTapCallback(const TapCallback& callback) 203 { 204 tapCallback_ = callback; 205 } 206 SetNextFocusEvent(const std::function<void ()> & event)207 void SetNextFocusEvent(const std::function<void()>& event) 208 { 209 moveNextFocusEvent_ = event; 210 } 211 SetSubmitEvent(std::function<void (const std::string &)> && event)212 void SetSubmitEvent(std::function<void(const std::string&)>&& event) 213 { 214 onSubmitEvent_ = event; 215 } 216 GetColor()217 const Color& GetColor() const 218 { 219 if (decoration_) { 220 return decoration_->GetBackgroundColor(); 221 } 222 return Color::TRANSPARENT; 223 } 224 SetColor(const Color & color)225 void SetColor(const Color& color) 226 { 227 if (decoration_) { 228 decoration_->SetBackgroundColor(color); 229 MarkNeedRender(); 230 } 231 } 232 GetHeight()233 double GetHeight() const 234 { 235 return height_.Value(); 236 } 237 GetDimensionHeight()238 const Dimension& GetDimensionHeight() const 239 { 240 return height_; 241 } 242 SetHeight(double height)243 void SetHeight(double height) 244 { 245 if (GreatOrEqual(height, 0.0) && !NearEqual(height_.Value(), height)) { 246 height_.SetValue(height); 247 MarkNeedLayout(); 248 } 249 } 250 GetStackElement()251 const WeakPtr<StackElement> GetStackElement() const 252 { 253 return stackElement_; 254 } 255 SetWidthReservedForSearch(double widthReservedForSearch)256 void SetWidthReservedForSearch(double widthReservedForSearch) 257 { 258 widthReservedForSearch_ = widthReservedForSearch; 259 } 260 SetPaddingHorizonForSearch(double paddingHorizon)261 void SetPaddingHorizonForSearch(double paddingHorizon) 262 { 263 paddingHorizonForSearch_ = paddingHorizon; 264 } 265 HasTextOverlayPushed()266 bool HasTextOverlayPushed() const 267 { 268 return hasTextOverlayPushed_; 269 } 270 GetIsInEditStatus()271 bool GetIsInEditStatus() const 272 { 273 return isInEditStatus_; 274 } 275 SetIsInEditStatus(bool isInEditStatus)276 void SetIsInEditStatus(bool isInEditStatus) 277 { 278 isInEditStatus_ = isInEditStatus; 279 } 280 GetPlaceholder()281 const std::string GetPlaceholder() const 282 { 283 return placeholder_; 284 } 285 GetValue()286 const std::string GetValue() const 287 { 288 return text_; 289 } 290 GetAction()291 TextInputAction GetAction() const 292 { 293 return action_; 294 } 295 GetKeyboard()296 TextInputType GetKeyboard() const 297 { 298 return keyboard_; 299 } 300 GetInactivePlaceholderColor()301 Color GetInactivePlaceholderColor() const 302 { 303 return inactivePlaceholderColor_; 304 } 305 GetPlaceHoldStyle()306 TextStyle GetPlaceHoldStyle() 307 { 308 return placeHoldStyle_; 309 } 310 GetTextAlign()311 TextAlign GetTextAlign() 312 { 313 return textAlign_; 314 } 315 GetCursorColor()316 Color GetCursorColor() const 317 { 318 return cursorColor_; 319 } 320 GetEditingStyle()321 TextStyle GetEditingStyle() 322 { 323 return editingStyle_; 324 } 325 GetMaxLength()326 int32_t GetMaxLength() 327 { 328 return maxLength_; 329 } 330 GetTextInputFilter()331 const std::string GetTextInputFilter() const 332 { 333 return inputFilter_; 334 } 335 SetTextOverlayPushed(bool hasTextOverlayPushed)336 void SetTextOverlayPushed(bool hasTextOverlayPushed) 337 { 338 hasTextOverlayPushed_ = hasTextOverlayPushed; 339 } 340 IsSingleHandle()341 bool IsSingleHandle() const 342 { 343 return isSingleHandle_; 344 } 345 GetInitIndex()346 int32_t GetInitIndex() const 347 { 348 return initIndex_; 349 } 350 SetInitIndex(int32_t initIndex)351 void SetInitIndex(int32_t initIndex) 352 { 353 initIndex_ = initIndex; 354 } 355 SetOnTextChangeEvent(const std::function<void (const std::string &)> & onTextChangeEvent)356 void SetOnTextChangeEvent(const std::function<void(const std::string&)>& onTextChangeEvent) 357 { 358 onTextChangeEvent_ = onTextChangeEvent; 359 } 360 SetOnValueChangeEvent(const std::function<void (const std::string &)> & onTextChangeEvent)361 void SetOnValueChangeEvent(const std::function<void(const std::string&)>& onTextChangeEvent) 362 { 363 onValueChangeEvent_ = onTextChangeEvent; 364 } 365 SetNeedNotifyChangeEvent(bool needNotifyChangeEvent)366 void SetNeedNotifyChangeEvent(bool needNotifyChangeEvent) 367 { 368 needNotifyChangeEvent_ = needNotifyChangeEvent; 369 } 370 371 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) SetInputMethodStatus(bool imeAttached)372 void SetInputMethodStatus(bool imeAttached) override 373 { 374 imeAttached_ = imeAttached; 375 } 376 #endif 377 378 // distribute 379 std::string ProvideRestoreInfo() override; 380 381 int32_t scrollBarOpacity_ = 0; 382 383 bool hasFocus_ = false; 384 void SetEditingValue(TextEditingValue&& newValue, bool needFireChangeEvent = true, bool isClearRecords = true); 385 void SetEditingValue(const std::string& text); 386 GetOnEditChanged()387 const std::function<void(bool)>& GetOnEditChanged() const 388 { 389 return onEditChanged_; 390 } 391 392 void OnEditChange(bool isInEditStatus); 393 void GetFieldAndOverlayTouchRect(std::vector<Rect>& resRectList); 394 SetInputStyle(InputStyle style)395 void SetInputStyle(InputStyle style) 396 { 397 inputStyle_ = style; 398 } 399 GetInputStyle()400 InputStyle GetInputStyle() 401 { 402 return inputStyle_; 403 } 404 405 void HandleOnBlur(); 406 SetCanPaintSelection(bool flag)407 void SetCanPaintSelection(bool flag) 408 { 409 canPaintSelection_ = flag; 410 } 411 GetCanPaintSelection()412 bool GetCanPaintSelection() const 413 { 414 return canPaintSelection_; 415 } 416 SetLastInputAction(InputAction action)417 void SetLastInputAction(InputAction action) 418 { 419 lastInputAction_ = action; 420 } 421 GetLastInputAction()422 InputAction GetLastInputAction() 423 { 424 return lastInputAction_; 425 } 426 427 bool NeedToFilter(); 428 HasSurfaceChangedCallback()429 bool HasSurfaceChangedCallback() 430 { 431 return surfaceChangedCallbackId_.has_value(); 432 } UpdateSurfaceChangedCallbackId(int32_t id)433 void UpdateSurfaceChangedCallbackId(int32_t id) 434 { 435 surfaceChangedCallbackId_ = id; 436 } 437 HasSurfacePositionChangedCallback()438 bool HasSurfacePositionChangedCallback() 439 { 440 return surfacePositionChangedCallbackId_.has_value(); 441 } UpdateSurfacePositionChangedCallbackId(int32_t id)442 void UpdateSurfacePositionChangedCallbackId(int32_t id) 443 { 444 surfacePositionChangedCallbackId_ = id; 445 } 446 447 int32_t GetInstanceId() const override; 448 449 protected: 450 // Describe where caret is and how tall visually. 451 struct CaretMetrics { ResetCaretMetrics452 void Reset() 453 { 454 offset.Reset(); 455 height = 0.0; 456 } 457 458 Offset offset; 459 // When caret is close to different glyphs, the height will be different. 460 double height = 0.0; ToStringCaretMetrics461 std::string ToString() const 462 { 463 std::string result = "Offset: "; 464 result += offset.ToString(); 465 result += ", height: "; 466 result += std::to_string(height); 467 return result; 468 } 469 }; 470 471 RenderTextField(); 472 virtual Size Measure(); 473 virtual int32_t GetCursorPositionForMoveUp() = 0; 474 virtual int32_t GetCursorPositionForMoveDown() = 0; 475 virtual int32_t GetCursorPositionForClick(const Offset& offset) = 0; 476 virtual Offset GetHandleOffset(int32_t extend) = 0; 477 virtual double PreferredLineHeight() = 0; 478 virtual int32_t AdjustCursorAndSelection(int32_t currentCursorPosition) = 0; 479 virtual DirectionStatus GetDirectionStatusOfPosition(int32_t position) const = 0; 480 virtual Size ComputeDeflateSizeOfErrorAndCountText() const = 0; ResetStatus()481 virtual void ResetStatus() {}; GetLongestLine()482 virtual double GetLongestLine() const 483 { 484 return 0; 485 } 486 487 void OnTouchTestHit( 488 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override; 489 void OnHiddenChanged(bool hidden) override; 490 void OnAppHide() override; 491 void OnClick(const ClickInfo& clickInfo); 492 void OnDoubleClick(const ClickInfo& clickInfo); 493 void OnLongPress(const LongPressInfo& longPressInfo); 494 bool HandleMouseEvent(const MouseEvent& event) override; 495 void HandleMouseHoverEvent(MouseState mouseState) override; 496 void AnimateMouseHoverEnter() override; 497 void AnimateMouseHoverExit() override; 498 void HandleValueFilter(TextEditingValue& valueBeforeUpdate, TextEditingValue& valueNeedToUpdate); 499 bool FilterWithRegex(std::string& valueToUpdate, const std::string& filter, bool needToEscape = false); 500 void KeyboardEditingValueFilter(TextEditingValue& valueToUpdate); 501 502 std::u16string GetTextForDisplay(const std::string& text) const; 503 504 void UpdateStartSelection(int32_t end, const Offset& pos, bool isSingleHandle, bool isLongPress); 505 void UpdateEndSelection(int32_t start, const Offset& pos); 506 void UpdateSelection(int32_t both); 507 void UpdateSelection(int32_t start, int32_t end); 508 void UpdateDirectionStatus(); 509 void UpdateCaretInfoToController(); 510 Offset GetPositionForExtend(int32_t extend, bool isSingleHandle); 511 Offset GetLastOffset() const; 512 /** 513 * Get grapheme cluster length before or after extend. 514 * For example, here is a sentence: 123 515 * The emoji character contains 2 code unit. Assumpt that the cursor is at the end (that will be 3+2+2 = 7). 516 * When calling GetGraphemeClusterLength(3, false), '' is not in Utf16Bmp, so result is 2. 517 * When calling GetGraphemeClusterLength(3, true), '3' is in Utf16Bmp, so result is 1. 518 */ 519 int32_t GetGraphemeClusterLength(int32_t extend, bool isPrefix) const; IsOverflowX()520 bool IsOverflowX() const 521 { 522 return overflowX_ == TextFieldOverflowX::AUTO || overflowX_ == TextFieldOverflowX::SCROLL; 523 } 524 HasConnection()525 bool HasConnection() const 526 { 527 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) 528 return imeAttached_; 529 #else 530 return connection_; 531 #endif 532 } 533 534 bool ShowCounter() const; 535 IsSelectiveDevice()536 static bool IsSelectiveDevice() 537 { 538 return (SystemProperties::GetDeviceType() != DeviceType::TV && 539 SystemProperties::GetDeviceType() != DeviceType::WATCH); 540 } 541 542 void CalculateMainScrollExtent(); 543 void AddOutOfRectCallbackToContext(); 544 545 // Used for compare to the current value and decide whether to UpdateRemoteEditing(). 546 std::shared_ptr<TextEditingValue> lastKnownRemoteEditingValue_; 547 548 // An outline for caret. It is used by default when the actual size cannot be retrieved. 549 Rect caretProto_; 550 Rect innerRect_; 551 // Click region of password icon. 552 Rect passwordIconRect_; 553 554 std::string placeholder_; 555 std::string inputFilter_; 556 Color placeholderColor_; 557 // Colors when not focused. 558 Color inactivePlaceholderColor_; 559 Color inactiveBgColor_; 560 Color inactiveTextColor_; 561 // Colors when focused. 562 Color focusPlaceholderColor_; 563 Color focusBgColor_; 564 Color focusTextColor_; 565 // Color when selected. 566 Color selectedColor_; 567 Color cursorColor_; 568 // Overlay color for hover and press. 569 Color overlayColor_ = Color::TRANSPARENT; 570 // The interval for caret twinkling, in ms. 571 uint32_t twinklingInterval = 0; 572 bool showPlaceholder_ = false; 573 bool cursorVisibility_ = false; 574 bool showCursor_ = true; 575 bool cursorColorIsSet_ = false; 576 Dimension cursorRadius_; 577 Dimension fontSize_; 578 // Used under case of [obscure_ == true]. 579 // When a character input, it will be displayed naked for a while. 580 // The remaining time to naked display the recent character is indicated by [obscureTickPendings_], 581 // multiply by [twinklingInterval]. For example, 3 * 500ms = 1500ms. 582 int32_t obscureTickPendings_ = 0; 583 // What the keyboard should appears. 584 TextInputType keyboard_ = TextInputType::UNSPECIFIED; 585 // Action when "enter" pressed. 586 TextInputAction action_ = TextInputAction::UNSPECIFIED; 587 std::string actionLabel_; 588 uint32_t maxLength_ = std::numeric_limits<uint32_t>::max(); 589 // Default to the start of text (according to RTL/LTR). 590 TextAlign textAlign_ = TextAlign::START; 591 // RTL/LTR is inherit from parent. 592 TextDirection textDirection_ = TextDirection::INHERIT; 593 TextDirection realTextDirection_ = TextDirection::INHERIT; 594 TextAffinity textAffinity_ = TextAffinity::DOWNSTREAM; 595 596 TextStyle countTextStyle_; 597 TextStyle overCountStyle_; 598 TextStyle countTextStyleOuter_; 599 TextStyle overCountStyleOuter_; 600 TextStyle style_; 601 TextStyle placeHoldStyle_; 602 TextStyle editingStyle_; 603 std::string text_; 604 std::string insertValue_; 605 bool insertTextUpdated_ = false; 606 std::string errorText_; 607 TextStyle errorTextStyle_; 608 double errorSpacing_ = 0.0; 609 Dimension errorSpacingInDimension_; 610 // Place error text in or under input. In input when device is TV, under input when device is phone. 611 bool errorIsInner_ = false; 612 Dimension errorBorderWidth_; 613 Color errorBorderColor_; 614 615 RefPtr<Decoration> decoration_; 616 Border originBorder_; 617 618 // One line TextField is more common in usual cases. 619 uint32_t maxLines_ = 1; 620 size_t textLines_ = 0; 621 size_t textLinesLast_ = 0; 622 int32_t cursorPositionForShow_ = 0; 623 CursorPositionType cursorPositionType_ = CursorPositionType::NORMAL; 624 DirectionStatus directionStatus_ = DirectionStatus::LEFT_LEFT; 625 double estimateHeight_ = 0.0; 626 CopyOptions copyOption_ = CopyOptions::Distributed; 627 TextFieldOverflowX overflowX_ = TextFieldOverflowX::HIDDEN; 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 RefPtr<ScrollBar> scrollBar_; // system defined scroll bar 671 double lastOffset_ = 0.0; 672 double currentOffset_ = 0.0; 673 Offset clickOffset_; 674 // For ensuring caret is visible on screen, we take a strategy that move the whole text painting area. 675 // It maybe seems rough, and doesn't support scrolling smoothly. 676 Offset textOffsetForShowCaret_; 677 InputStyle inputStyle_; 678 Rect caretRect_; 679 680 private: 681 void SetCallback(const RefPtr<TextFieldComponent>& textField); 682 void StartPressAnimation(bool isPressDown); 683 void StartHoverAnimation(bool isHovered); 684 void ScheduleCursorTwinkling(); 685 void OnCursorTwinkling(); 686 void CursorMoveOnClick(const Offset& offset); 687 void UpdateRemoteEditing(bool needFireChangeEvent = true); 688 void UpdateObscure(const RefPtr<TextFieldComponent>& textField); 689 void UpdateFormatters(); 690 void UpdateFocusStyles(); 691 void UpdateIcon(const RefPtr<TextFieldComponent>& textField); 692 void UpdatePasswordIcon(const RefPtr<TextFieldComponent>& textField); 693 void UpdateOverlay(); 694 void RegisterFontCallbacks(); 695 void HandleOnRevoke(); 696 void HandleOnInverseRevoke(); 697 void HandleOnCut() override; 698 void HandleOnCopy(bool isUsingExternalKeyboard = false) override; 699 void HandleOnPaste() override; 700 void HandleOnCopyAll(const std::function<void(const Offset&, const Offset&)>& callback); 701 void HandleOnStartHandleMove(int32_t end, const Offset& startHandleOffset, 702 const std::function<void(const Offset&)>& startCallback, bool isSingleHandle = false); 703 void HandleOnEndHandleMove( 704 int32_t start, const Offset& endHandleOffset, const std::function<void(const Offset&)>& endCallback); 705 RefPtr<StackElement> GetLastStack() const; 706 void InitAnimation(); 707 void RegisterCallbacksToOverlay(); 708 void PushTextOverlayToStack(); 709 void UpdateAccessibilityAttr(); 710 void InitAccessibilityEventListener(); 711 bool HandleKeyEvent(const KeyEvent& event); 712 void ClearEditingValue(); 713 virtual bool GetCaretRect(int32_t extent, Rect& caretRect, double caretHeightOffset = 0.0) const = 0; 714 bool SearchAction(const Offset& globalPosition, const Offset& globalOffset); 715 void ChangeCounterStyle(const TextEditingValue& value); 716 void ChangeBorderToErrorStyle(); 717 void HandleDeviceOrientationChange(); 718 void OnOverlayFocusChange(bool isFocus, bool needCloseKeyboard); 719 void FireSelectChangeIfNeeded(const TextEditingValue& newValue, bool needFireSelectChangeEvent) const; 720 void ApplyAspectRatio(); // If aspect ratio is setted, height will follow box parent. 721 722 /** 723 * @brief Update remote editing value only if text or selection is changed. 724 */ 725 void UpdateRemoteEditingIfNeeded(bool needFireChangeEvent = true); 726 727 void AttachIme(); 728 729 // distribute 730 void ApplyRestoreInfo(); 731 void OnTapCallback(); GetRealTextWidth()732 virtual double GetRealTextWidth() const 733 { 734 return 0; 735 } 736 void HandleSurfacePositionChanged(int32_t posX, int32_t posY); 737 void HandleSurfaceChanged(int32_t newWidth, int32_t newHeight, int32_t prevWidth, int32_t prevHeight); 738 739 int32_t initIndex_ = 0; 740 bool isOverlayFocus_ = false; 741 bool isShiftDown_ = false; 742 bool isCtrlDown_ = false; 743 double fontScale_ = 1.0; 744 bool isSingleHandle_ = false; 745 bool hasTextOverlayPushed_ = false; 746 bool softKeyboardEnabled_ = true; 747 bool isInEditStatus_ = false; 748 bool isFocusOnTouch_ = true; 749 bool onTapCallbackResult_ = false; 750 InputAction lastInputAction_ = InputAction::UNKNOWN; 751 Color pressColor_; 752 Color hoverColor_; 753 TextSelection selection_; // Selection from custom. 754 DeviceOrientation deviceOrientation_ = DeviceOrientation::PORTRAIT; 755 std::function<void()> onValueChange_; 756 std::function<void(bool)> onKeyboardClose_; 757 std::function<void(const Rect&)> onClipRectChanged_; 758 std::function<void(const OverlayShowOption&)> updateHandlePosition_; 759 std::function<void(const double&)> updateHandleDiameter_; 760 std::function<void(const double&)> updateHandleDiameterInner_; 761 std::function<void(const std::string&)> onTextChangeEvent_; 762 std::function<void(std::string)> onChange_; 763 std::function<void(const ClickInfo& clickInfo)> onClick_; 764 std::function<void(const std::string&)> onError_; 765 std::function<void(bool)> onEditChanged_; 766 std::function<void(int32_t)> onSubmit_; 767 std::function<void(const std::string&)> onValueChangeEvent_; 768 std::function<void(const std::string&)> onSelectChangeEvent_; 769 std::function<void(const std::string&)> onFinishInputEvent_; 770 std::function<void(const std::string&)> onSubmitEvent_; 771 std::function<void()> onTapEvent_; 772 std::function<void()> onLongPressEvent_; 773 std::function<void()> moveNextFocusEvent_; 774 std::function<void(bool)> onOverlayFocusChange_; 775 std::function<void(std::string)> onCopy_; 776 std::function<void(std::string)> onCut_; 777 std::function<void(std::string)> onPaste_; 778 EventMarker onOptionsClick_; 779 EventMarker onTranslate_; 780 EventMarker onShare_; 781 EventMarker onSearch_; 782 bool catchMode_ = true; 783 TapCallback tapCallback_; 784 CancelableCallback<void()> cursorTwinklingTask_; 785 786 std::optional<int32_t> surfaceChangedCallbackId_; 787 std::optional<int32_t> surfacePositionChangedCallbackId_; 788 std::vector<InputOption> inputOptions_; 789 std::list<std::unique_ptr<TextInputFormatter>> textInputFormatters_; 790 RefPtr<TextEditController> controller_; 791 RefPtr<TextFieldController> textFieldController_; 792 RefPtr<TextInputConnection> connection_; 793 RefPtr<Clipboard> clipboard_; 794 RefPtr<TextOverlayComponent> textOverlay_; 795 WeakPtr<StackElement> stackElement_; 796 RefPtr<ClickRecognizer> clickRecognizer_; 797 RefPtr<ClickRecognizer> doubleClickRecognizer_; 798 RefPtr<LongPressRecognizer> longPressRecognizer_; 799 RefPtr<RawRecognizer> rawRecognizer_; 800 RefPtr<Animator> pressController_; 801 RefPtr<Animator> hoverController_; 802 RefPtr<Animator> animator_; 803 std::vector<TextEditingValue> operationRecords_; 804 std::vector<TextEditingValue> inverseOperationRecords_; 805 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) 806 bool imeAttached_ = false; 807 #endif 808 809 #if defined(ENABLE_STANDARD_INPUT) 810 sptr<OHOS::MiscServices::OnTextChangedListener> textChangeListener_; 811 #endif 812 }; 813 814 } // namespace OHOS::Ace 815 816 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_RENDER_TEXT_FIELD_H 817