1 /*
2  * Copyright (c) 2021-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_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 #ifdef WEB_SUPPORTED
46 class RenderWeb;
47 #endif
48 class RenderTextOverlay : public RenderNode {
49     DECLARE_ACE_TYPE(RenderTextOverlay, RenderNode)
50 
51 public:
52     RenderTextOverlay();
53     ~RenderTextOverlay() override;
54 
55     static RefPtr<RenderNode> Create();
56     void Update(const RefPtr<Component>& component) override;
57     void PerformLayout() override;
58     bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint, const TouchRestrict& touchRestrict,
59         TouchTestResult& result) override;
60 
61     void PopOverlay();
62     void OnFocusChange(RenderStatus renderStatus);
63     void SetOnRebuild(const std::function<void(bool, bool, bool, bool, bool)>& onRebuild);
64     void SetStartAnimationCallback(const StartAnimationCallback& value);
65 
66     void SetIsAnimationStarted(bool isAnimationStart);
67     bool IsAnimationStarted() const;
68     void SetIsAnimationStopped(bool isAnimationStopped);
69     double GetHorizonOffsetForAnimation() const;
70     bool HandleMouseEvent(const MouseEvent& event) override;
71 
72 protected:
73     void OnPaintFinish() override;
74     void OnTouchTestHit(
75         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
76 
77     void RestoreMoreButtonStyle();
78     void InitRenderChild(const RefPtr<RenderNode>& render);
79     void ResetRenderChild();
80     bool HasToolBarOnly() const; // Has tool bar and has no menu.
81 
82     bool hasMenu_ = false;
83     bool hasPoped_ = false;
84     bool reverse_ = false;
85     bool showMagnifier_ = false;
86     bool showOverlay_ = true;
87     bool isDragging_ = false;
88     bool isSingleHandle_ = false;
89     bool isStartDrag_ = false;
90     bool isEndDrag_ = false;
91     bool isTouchStartDrag_ = false;
92     bool isTouchEndDrag_ = false;
93     bool isAnimationStarted_ = true;
94     bool isAnimationStopped_ = true;
95     float rotateDegree_ = 0.0f;
96     double clipWidth_ = 0.0;
97     double lineHeight_ = 0.0;
98     std::optional<double> startHandleHeight_;
99     std::optional<double> endHandleHeight_;
100 
101     // Mark startHandle and endHandle index
102     int32_t startIndex_ = 0;
103     int32_t endIndex_ = 0;
104 
105     OverlayShowOption showOption_;
106 
107     Dimension handleRadius_;
108     Dimension handleRadiusInner_;
109     Dimension handleDiameter_;
110     Dimension handleDiameterInner_;
111     Dimension menuSpacingWithText_;
112     Dimension menuSpacingWithHandle_;
113     Dimension strokeWidth_ = STROKE_MAX_WIDTH;
114 
115     Color handleColor_;
116     Color handleColorInner_;
117 
118     Offset startHandleOffset_;
119     Offset endHandleOffset_;
120     Offset startHandleCenter_;
121     Offset endHandleCenter_;
122     Offset mouseOffset_;
123 
124     // dot offset align to arrow endings
125     Offset dot1StartOffset_;
126     Offset dot2StartOffset_;
127     Offset dot3StartOffset_;
128     Offset dot4StartOffset_;
129 
130     // line ending offset compare to dot position in animation
131     Offset dot2Offset_;
132     Offset dot3Offset_;
133     Offset dot4Offset_;
134 
135     Offset moreButtonPosition_;
136 
137     Rect clipRect_;
138     TouchRegion startHandleRegion_;
139     TouchRegion endHandleRegion_;
140 
141     TextDirection textDirection_ = TextDirection::LTR;
142     TextDirection realTextDirection_ = TextDirection::LTR;
143 
144     RefPtr<RenderBox> renderBox_;
145     RefPtr<RenderClip> renderClip_;
146     RefPtr<RenderSelectPopup> renderMenu_;
147     RefPtr<ClickRecognizer> clickDetector_;
148     RefPtr<DragRecognizer> dragDetector_;
149     RefPtr<RawRecognizer> touchDetector_;
150     WeakPtr<RenderTextField> weakTextField_;
151     WeakPtr<RenderText> weakText_;
152     WeakPtr<RenderImage> weakImage_;
153 #ifdef WEB_SUPPORTED
154     WeakPtr<RenderWeb> weakWeb_;
155 #endif
156     bool isUsedByWeb_ = false;
157     RefPtr<Animator> controller_;
158     WeakPtr<TextOverlayComponent> overlayComponent_;
159 
160 private:
161     void UpdateWeakTextField(const RefPtr<TextOverlayComponent>& overlay);
162     void UpdateWeakText(const RefPtr<TextOverlayComponent>& overlay);
163     void UpdateWeakImage(const RefPtr<TextOverlayComponent>& overlay);
164 #ifdef WEB_SUPPORTED
165     void UpdateWeakWeb(const RefPtr<TextOverlayComponent>& overlay);
166 #endif
167     void BindBackendEvent(const RefPtr<TextOverlayComponent>& overlay);
168     void BindBackendEventV2(const RefPtr<TextOverlayComponent>& overlay);
169     void RemoveBackendEvent(const RefPtr<TextOverlayComponent>& overlay);
170 
171     void HandleClick(const Offset& clickOffset);
172     void HandleDragStart(const Offset& startOffset);
173     void HandleDragUpdateAndEnd(const Offset& offset);
174 
175     void HandleCut();
176     void HandleCopy();
177     void HandlePaste();
178     void HandleCopyAll();
179     void HandleMoreButtonClick();
180 
181     void StartMoreAnimation(bool reverse);
182     void BuildAndStartMoreButtonAnimation();
183 
184     void BuildStrokeWidthAnimation(const RefPtr<KeyframeAnimation<Dimension>>& widthAnimation, const Dimension& from,
185             const Dimension& to, bool reverse);
186 
187     void ProcessFrictionAnimation(double value);
188     void BuildFrictionAnimation(const RefPtr<KeyframeAnimation<double>>& animation, double from, double to);
189 
190     void ProcessEndPointAnimation(double value);
191     void BuildEndPointOffsetAnimation(
192             const RefPtr<KeyframeAnimation<double>>& offsetAnimation, double from, double to, bool reverse);
193 
194     Offset ComputeChildPosition(const RefPtr<RenderNode>& child);
195 
196     void InitAnimation();
197 
198     bool needStartTwinkling_ = true;
199     bool needCloseKeyboard_ = true;
200     bool isAnimationInited_ = false;
201     bool animateUntilPaint_ = false;
202     double horizonOffsetForAnimation_ = 0.0;
203     double childRightBoundary_ = 0.0;
204 
205     TweenOption tweenOptionIn_;
206     TweenOption tweenOptionOut_;
207     TweenOption innerTweenOptionIn_;
208     TweenOption innerTweenOptionOut_;
209 
210     CommonCallback onCut_;
211     CommonCallback onCopy_;
212     CommonCallback onPaste_;
213     CopyAllCallback onCopyAll_;
214     StartHandleMoveCallback onStartHandleMove_;
215     EndHandleMoveCallback onEndHandleMove_;
216     StartAnimationCallback startAnimation_;
217 
218     std::function<void(bool, bool)> onFocusChange_;
219     std::function<void(bool, bool, bool, bool, bool)> onRebuild_;
220 
221     bool isUsingMouse_ = false;
222     bool needClipRect_ = true;
223 };
224 
225 } // namespace OHOS::Ace
226 
227 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_OVERLAY_RENDER_TEXT_OVERLAY_H
228