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