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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_OVERLAY_MODIFIER_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_OVERLAY_MODIFIER_H
17 
18 #include "base/memory/ace_type.h"
19 #include "core/components/common/properties/color.h"
20 #include "core/components_ng/base/modifier.h"
21 #include "core/components_ng/render/node_paint_method.h"
22 
23 namespace OHOS::Ace::NG {
24 enum class OpacityAnimationType {
25     /*
26      * do not run opacity animation.
27      */
28     NONE = 0,
29     /*
30      *  run disappear animation.
31      */
32     DISAPPEAR,
33     /*
34      * run appear animation.
35      */
36     APPEAR,
37 };
38 
39 enum class HoverAnimationType {
40     /*
41      * do not run hover animation.
42      */
43     NONE = 0,
44     /*
45      *  run grow animation.
46      */
47     GROW,
48     /*
49      *  run shrink animation.
50      */
51     SHRINK,
52 };
53 
54 class ScrollBarOverlayModifier : public OverlayModifier {
55     DECLARE_ACE_TYPE(ScrollBarOverlayModifier, OverlayModifier)
56 
57 public:
58     ScrollBarOverlayModifier(const OffsetF& barOffset = OffsetF(), const SizeF& barSize = SizeF());
59 
60     ~ScrollBarOverlayModifier() override = default;
61 
62     void onDraw(DrawingContext& drawingContext) override;
63 
64     void SetOffset(const OffsetF& barOffset);
65 
66     void SetSize(const SizeF& barSize);
67 
68     void SetRect(const Rect& barRect);
69 
GetHoverAnimatingType()70     HoverAnimationType GetHoverAnimatingType() const
71     {
72         return hoverAnimatingType_;
73     }
74 
SetHoverAnimatingType(HoverAnimationType hoverAnimatingType)75     void SetHoverAnimatingType(HoverAnimationType hoverAnimatingType)
76     {
77         hoverAnimatingType_ = hoverAnimatingType;
78     }
79 
GetOpacityAnimatingType()80     OpacityAnimationType GetOpacityAnimatingType() const
81     {
82         return opacityAnimatingType_;
83     }
84 
SetOpacityAnimatingType(OpacityAnimationType opacityAnimatingType)85     void SetOpacityAnimatingType(OpacityAnimationType opacityAnimatingType)
86     {
87         opacityAnimatingType_ = opacityAnimatingType;
88     }
89 
SetOpacity(uint8_t opacity)90     void SetOpacity(uint8_t opacity)
91     {
92         CHECK_NULL_VOID(opacity_);
93         opacity_->Set(opacity);
94     }
95 
GetOpacity()96     uint8_t GetOpacity() const
97     {
98         CHECK_NULL_RETURN(opacity_, 0);
99         return opacity_->Get();
100     }
101 
102     void StartBarAnimation(HoverAnimationType hoverAnimationType, OpacityAnimationType opacityAnimationType,
103         bool needAdaptAnimation, const Rect& barRect);
104 
105     void StartOpacityAnimation(OpacityAnimationType opacityAnimationType);
106 
107     void StartHoverAnimation(const Rect& barRect, HoverAnimationType hoverAnimationType);
108 
109     void StartAdaptAnimation(const Rect& barRect, bool needAdaptAnimation);
110 
111     void StopOpacityAnimation();
112 
113     void StopHoverAnimation();
114 
115     void StopAdaptAnimation();
116 
117     void SetMainModeSize(const Size& size);
118 
119     void SetCrossModeSize(const Size& size);
120 
121     void SetMainModeOffset(const Offset& offset);
122 
123     void SetCrossModeOffset(const Offset& offset);
124 
125     void SetBarColor(Color barColor);
126 
SetPositionMode(const PositionMode & positionMode)127     void SetPositionMode(const PositionMode& positionMode)
128     {
129         positionMode_ = positionMode;
130     }
131 
SetScrollable(bool isScrollable)132     void SetScrollable(bool isScrollable)
133     {
134         isScrollable_ = isScrollable;
135     }
136 
137 private:
138     Offset GetHoverOffset(const Size& size) const;
139     void CheckMainModeNearEqual();
140     // Animatable
141     RefPtr<AnimatablePropertyUint8> opacity_;
142     RefPtr<AnimatablePropertyFloat> barWidth_;
143     RefPtr<AnimatablePropertyFloat> barHeight_;
144     RefPtr<AnimatablePropertyFloat> barX_;
145     RefPtr<AnimatablePropertyFloat> barY_;
146 
147     // no Animatable
148     RefPtr<PropertyColor> barColor_;
149     bool isAdaptAnimationStop_ = true;
150     float lastMainModeHeight_ = 0.f;
151     float lastMainModeOffset_ = 0.f;
152     ACE_DISALLOW_COPY_AND_MOVE(ScrollBarOverlayModifier);
153 
154     std::shared_ptr<AnimationUtils::Animation> hoverAnimation_;
155     std::shared_ptr<AnimationUtils::Animation> opacityAnimation_;
156     std::shared_ptr<AnimationUtils::Animation> adaptAnimation_;
157     HoverAnimationType hoverAnimatingType_ = HoverAnimationType::NONE;
158     OpacityAnimationType opacityAnimatingType_ = OpacityAnimationType::NONE;
159     PositionMode positionMode_ = PositionMode::RIGHT;
160 
161     bool isScrollable_ = true;
162 };
163 } // namespace OHOS::Ace::NG
164 
165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_OVERLAY_MODIFIER_H
166