1 /*
2  * Copyright (c) 2023 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_NG_PATTERNS_TEXT_DRAG_TEXT_DRAG_OVERLAY_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_DRAG_TEXT_DRAG_OVERLAY_MODIFIER_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components_ng/base/modifier.h"
22 #include "core/components_ng/pattern/image/image_pattern.h"
23 #include "core/components_ng/pattern/pattern.h"
24 
25 namespace OHOS::Ace::NG {
26 constexpr int32_t TEXT_ANIMATION_DURATION = 300;
27 constexpr Dimension TEXT_DRAG_DEFAULT_OFFSET = 8.0_vp;
28 
29 class TextDragPattern;
30 enum class DragAnimType { FLOATING, FLOATING_CANCEL, DEFAULT };
31 
32 class TextDragOverlayModifier : public OverlayModifier {
33     DECLARE_ACE_TYPE(TextDragOverlayModifier, OverlayModifier);
34 
35 public:
36     explicit TextDragOverlayModifier(const WeakPtr<OHOS::Ace::NG::Pattern>& pattern);
37     ~TextDragOverlayModifier() override = default;
38 
StartFloatingAnimate()39     virtual void StartFloatingAnimate()
40     {
41         isAnimating_ = true;
42         type_ = DragAnimType::FLOATING;
43         backgroundOffset_->Set(0);
44         AnimationOption option;
45         option.SetDuration(TEXT_ANIMATION_DURATION);
46         option.SetCurve(Curves::EASE_OUT);
47         option.SetDelay(0);
48         auto finishFuc = [weakModifier = WeakClaim(this)]() {
49         auto modifier = weakModifier.Upgrade();
50         CHECK_NULL_VOID(modifier);
51         modifier->SetAnimateFlag(false);
52         };
53         SetShadowOpacity(0.0);
54         option.SetOnFinishEvent(finishFuc);
55         auto propertyCallback = [weakModifier = WeakClaim(this)]() {
56             auto modifier = weakModifier.Upgrade();
57             CHECK_NULL_VOID(modifier);
58             modifier->SetBackgroundOffset(TEXT_DRAG_DEFAULT_OFFSET.ConvertToPx());
59             modifier->SetShadowOpacity(1.0);
60         };
61         AnimationUtils::Animate(option, propertyCallback, option.GetOnFinishEvent());
62     }
63 
IsHandlesShow()64     bool IsHandlesShow()
65     {
66         return isHandlesShow_;
67     }
68 
UpdateHandlesShowFlag(bool isHandlesShow)69     void UpdateHandlesShowFlag(bool isHandlesShow)
70     {
71         isHandlesShow_ = isHandlesShow;
72     }
73 
SetAnimateFlag(bool isAnimate)74     void SetAnimateFlag(bool isAnimate)
75     {
76         isAnimating_ = isAnimate;
77     }
78 
SetShadowOpacity(float opacity)79     void SetShadowOpacity(float opacity)
80     {
81         CHECK_NULL_VOID(shadowOpacity_);
82         shadowOpacity_->Set(opacity);
83     }
84 
85     void PaintBackground(const RSPath& path, RSCanvas& canvas, RefPtr<TextDragPattern> textDragPattern);
86     void PaintShadow(const RSPath& path, const Shadow& shadow, RSCanvas& canvas);
87     void onDraw(DrawingContext& context) override;
88     void SetBackgroundOffset(float offset);
89     void SetSelectedBackgroundOpacity(float opacity);
90 
91 protected:
92     WeakPtr<Pattern> pattern_;
93     bool isAnimating_ = false;
94     bool isHandlesShow_ = false;
95     RefPtr<AnimatablePropertyFloat> backgroundOffset_;
96     RefPtr<AnimatablePropertyFloat> selectedBackgroundOpacity_;
97 private:
98     DragAnimType type_ = DragAnimType::DEFAULT;
99     RefPtr<AnimatablePropertyFloat> shadowOpacity_;
100 
101     ACE_DISALLOW_COPY_AND_MOVE(TextDragOverlayModifier);
102 };
103 } // namespace OHOS::Ace::NG
104 
105 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_DRAG_TEXT_DRAG_OVERLAY_MODIFIER_H
106