1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_OVERLAY_RENDER_TEXT_OVERLAY_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_OVERLAY_RENDER_TEXT_OVERLAY_H 18 19 #include <functional> 20 21 #include "core/components/box/render_box.h" 22 #include "core/components/clip/render_clip.h" 23 #include "core/components/select_popup/render_select_popup.h" 24 #include "core/components/slider/render_slider.h" 25 #include "core/components/text_field/render_text_field.h" 26 #include "core/components/text_overlay/text_overlay_component.h" 27 #include "core/gestures/click_recognizer.h" 28 #include "core/gestures/drag_recognizer.h" 29 #include "core/gestures/raw_recognizer.h" 30 #include "core/pipeline/base/overlay_show_option.h" 31 #include "core/pipeline/base/render_node.h" 32 #include "frameworks/base/utils/system_properties.h" 33 34 namespace OHOS::Ace { 35 36 const Offset DOT1_POSITION = Offset(6.25, 6.75); 37 const Offset DOT2_POSITION = Offset(17.75, 6.75); 38 const Offset DOT3_POSITION = Offset(6.25, 17.25); 39 const Offset DOT4_POSITION = Offset(17.75, 17.25); 40 const Dimension STROKE_MAX_WIDTH = 4.0_vp; 41 const Dimension STROKE_MIN_WIDTH = 1.5_vp; 42 43 using StartAnimationCallback = std::function<void(const TweenOption&, const TweenOption&, bool, bool)>; 44 45 class RenderTextOverlay : public RenderNode { 46 DECLARE_ACE_TYPE(RenderTextOverlay, RenderNode) 47 48 public: 49 RenderTextOverlay(); 50 ~RenderTextOverlay() override; 51 52 static RefPtr<RenderNode> Create(); 53 void Update(const RefPtr<Component>& component) override; 54 void PerformLayout() override; 55 bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint, const TouchRestrict& touchRestrict, 56 TouchTestResult& result) override; 57 58 void PopOverlay(); 59 void OnFocusChange(RenderStatus renderStatus); 60 void SetOnRebuild(const std::function<void(bool, bool, bool, bool, bool)>& onRebuild); 61 void SetStartAnimationCallback(const StartAnimationCallback& value); 62 63 void SetIsAnimationStarted(bool isAnimationStart); 64 bool IsAnimationStarted() const; 65 void SetIsAnimationStopped(bool isAnimationStopped); 66 double GetHorizonOffsetForAnimation() const; 67 bool HandleMouseEvent(const MouseEvent& event) override; 68 69 protected: 70 void OnPaintFinish() override; 71 void OnTouchTestHit( 72 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override; 73 74 void RestoreMoreButtonStyle(); 75 void InitRenderChild(const RefPtr<RenderNode>& render); 76 void ResetRenderChild(); 77 bool HasToolBarOnly() const; // Has tool bar and has no menu. 78 79 bool hasMenu_ = false; 80 bool hasPoped_ = false; 81 bool reverse_ = false; 82 bool showMagnifier_ = false; 83 bool showOverlay_ = true; 84 bool isDragging_ = false; 85 bool isSingleHandle_ = false; 86 bool isStartDrag_ = false; 87 bool isEndDrag_ = false; 88 bool isTouchStartDrag_ = false; 89 bool isTouchEndDrag_ = false; 90 bool isAnimationStarted_ = true; 91 bool isAnimationStopped_ = true; 92 float rotateDegree_ = 0.0f; 93 double clipWidth_ = 0.0; 94 double lineHeight_ = 0.0; 95 96 // Mark startHandle and endHandle index 97 int32_t startIndex_ = 0; 98 int32_t endIndex_ = 0; 99 100 OverlayShowOption showOption_; 101 102 Dimension handleRadius_; 103 Dimension handleRadiusInner_; 104 Dimension handleDiameter_; 105 Dimension handleDiameterInner_; 106 Dimension menuSpacingWithText_; 107 Dimension menuSpacingWithHandle_; 108 Dimension strokeWidth_ = STROKE_MAX_WIDTH; 109 110 Color handleColor_; 111 Color handleColorInner_; 112 113 Offset startHandleOffset_; 114 Offset endHandleOffset_; 115 Offset startHandleCenter_; 116 Offset endHandleCenter_; 117 118 // dot offset align to arrow endings 119 Offset dot1StartOffset_; 120 Offset dot2StartOffset_; 121 Offset dot3StartOffset_; 122 Offset dot4StartOffset_; 123 124 // line ending offset compare to dot position in animation 125 Offset dot2Offset_; 126 Offset dot3Offset_; 127 Offset dot4Offset_; 128 129 Offset moreButtonPosition_; 130 131 Rect clipRect_; 132 TouchRegion startHandleRegion_; 133 TouchRegion endHandleRegion_; 134 135 TextDirection textDirection_ = TextDirection::LTR; 136 TextDirection realTextDirection_ = TextDirection::LTR; 137 138 RefPtr<RenderBox> renderBox_; 139 RefPtr<RenderClip> renderClip_; 140 RefPtr<RenderSelectPopup> renderMenu_; 141 RefPtr<ClickRecognizer> clickDetector_; 142 RefPtr<DragRecognizer> dragDetector_; 143 RefPtr<RawRecognizer> touchDetector_; 144 WeakPtr<RenderTextField> weakTextField_; 145 RefPtr<Animator> controller_; 146 WeakPtr<TextOverlayComponent> overlayComponent_; 147 148 private: 149 void UpdateWeakTextField(const RefPtr<TextOverlayComponent>& overlay); 150 void BindBackendEvent(const RefPtr<TextOverlayComponent>& overlay); 151 void BindBackendEventV2(const RefPtr<TextOverlayComponent>& overlay); 152 void RemoveBackendEvent(const RefPtr<TextOverlayComponent>& overlay); 153 154 void HandleClick(const Offset& clickOffset); 155 void HandleDragStart(const Offset& startOffset); 156 void HandleDragUpdateAndEnd(const Offset& offset); 157 158 void HandleCut(); 159 void HandleCopy(); 160 void HandlePaste(); 161 void HandleCopyAll(); 162 void HandleMoreButtonClick(); 163 164 void StartMoreAnimation(bool reverse); 165 void BuildAndStartMoreButtonAnimation(); 166 167 void BuildStrokeWidthAnimation(const RefPtr<KeyframeAnimation<Dimension>>& widthAnimation, const Dimension& from, 168 const Dimension& to, bool reverse); 169 170 void ProcessFrictionAnimation(double value); 171 void BuildFrictionAnimation(const RefPtr<KeyframeAnimation<double>>& animation, double from, double to); 172 173 void ProcessEndPointAnimation(double value); 174 void BuildEndPointOffsetAnimation( 175 const RefPtr<KeyframeAnimation<double>>& offsetAnimation, double from, double to, bool reverse); 176 177 Offset ComputeChildPosition(const RefPtr<RenderNode>& child); 178 179 void InitAnimation(); 180 181 bool needStartTwinkling_ = true; 182 bool needCloseKeyboard_ = true; 183 bool isAnimationInited_ = false; 184 bool animateUntilPaint_ = false; 185 double horizonOffsetForAnimation_ = 0.0; 186 double childRightBoundary_ = 0.0; 187 188 TweenOption tweenOptionIn_; 189 TweenOption tweenOptionOut_; 190 TweenOption innerTweenOptionIn_; 191 TweenOption innerTweenOptionOut_; 192 193 CommonCallback onCut_; 194 CommonCallback onCopy_; 195 CommonCallback onPaste_; 196 CopyAllCallback onCopyAll_; 197 StartHandleMoveCallback onStartHandleMove_; 198 EndHandleMoveCallback onEndHandleMove_; 199 StartAnimationCallback startAnimation_; 200 201 std::function<void(bool, bool)> onFocusChange_; 202 std::function<void(bool, bool, bool, bool, bool)> onRebuild_; 203 }; 204 205 } // namespace OHOS::Ace 206 207 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_OVERLAY_RENDER_TEXT_OVERLAY_H 208