• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * appear without animation.
39      */
40     APPEAR_WITHOUT_ANIMATION,
41 };
42 
43 enum class HoverAnimationType {
44     /*
45      * do not run hover animation.
46      */
47     NONE = 0,
48     /*
49      *  run grow animation.
50      */
51     GROW,
52     /*
53      *  run shrink animation.
54      */
55     SHRINK,
56 };
57 
58 class ScrollBarOverlayModifier : public OverlayModifier {
59     DECLARE_ACE_TYPE(ScrollBarOverlayModifier, OverlayModifier);
60 
61 public:
62     explicit ScrollBarOverlayModifier(const OffsetF& barOffset = OffsetF(), const SizeF& barSize = SizeF());
63 
64     ~ScrollBarOverlayModifier() override = default;
65 
66     void onDraw(DrawingContext& drawingContext) override;
67 
68     void SetOffset(const OffsetF& barOffset);
69 
70     void SetSize(const SizeF& barSize);
71 
72     void SetRect(const Rect& barRect);
73 
GetHoverAnimatingType()74     HoverAnimationType GetHoverAnimatingType() const
75     {
76         return hoverAnimatingType_;
77     }
78 
SetHoverAnimatingType(HoverAnimationType hoverAnimatingType)79     void SetHoverAnimatingType(HoverAnimationType hoverAnimatingType)
80     {
81         hoverAnimatingType_ = hoverAnimatingType;
82     }
83 
GetOpacityAnimatingType()84     OpacityAnimationType GetOpacityAnimatingType() const
85     {
86         return opacityAnimatingType_;
87     }
88 
SetOpacityAnimatingType(OpacityAnimationType opacityAnimatingType)89     void SetOpacityAnimatingType(OpacityAnimationType opacityAnimatingType)
90     {
91         opacityAnimatingType_ = opacityAnimatingType;
92     }
93 
SetOpacity(uint8_t opacity)94     void SetOpacity(uint8_t opacity)
95     {
96         AnimationUtils::ExecuteWithoutAnimation([weak = AceType::WeakClaim(this), opacity]() {
97             auto modifier = weak.Upgrade();
98             CHECK_NULL_VOID(modifier);
99             auto modifierOpacity = modifier->opacity_;
100             CHECK_NULL_VOID(modifierOpacity);
101             modifierOpacity->Set(opacity);
102         });
103     }
104 
GetOpacity()105     uint8_t GetOpacity() const
106     {
107         CHECK_NULL_RETURN(opacity_, 0);
108         return opacity_->Get();
109     }
110 
111     void StartBarAnimation(HoverAnimationType hoverAnimationType, OpacityAnimationType opacityAnimationType,
112         bool needAdaptAnimation, const Rect& barRect);
113 
114     void StartOpacityAnimation(OpacityAnimationType opacityAnimationType);
115 
116     void StartHoverAnimation(const Rect& barRect, HoverAnimationType hoverAnimationType);
117 
118     void StartAdaptAnimation(const Rect& barRect, bool needAdaptAnimation);
119 
120     void StopOpacityAnimation();
121 
122     void StopHoverAnimation();
123 
124     void StopAdaptAnimation();
125 
126     void SetMainModeSize(const Size& size);
127 
128     void SetCrossModeSize(const Size& size);
129 
130     void SetMainModeOffset(const Offset& offset);
131 
132     void SetCrossModeOffset(const Offset& offset);
133 
134     void SetBarColor(Color barColor);
135 
GetBarColor()136     RefPtr<PropertyColor> GetBarColor()
137     {
138         return barColor_;
139     }
SetPositionMode(const PositionMode & positionMode)140     void SetPositionMode(const PositionMode& positionMode)
141     {
142         positionMode_ = positionMode;
143     }
144 
SetScrollable(bool isScrollable)145     void SetScrollable(bool isScrollable)
146     {
147         isScrollable_ = isScrollable;
148     }
149 
GetScrollable()150     bool GetScrollable() const
151     {
152         return isScrollable_;
153     }
154 
SetNavDestinationShow(bool isNavDestinationShow)155     void SetNavDestinationShow(bool isNavDestinationShow)
156     {
157         isNavDestinationShow_ = isNavDestinationShow;
158     }
159 
160 protected:
161     std::shared_ptr<AnimationUtils::Animation> hoverAnimation_;
162 
163 private:
164     Offset GetHoverOffset(const Size& size) const;
165     void CheckMainModeNearEqual();
166     // Animatable
167     RefPtr<AnimatablePropertyUint8> opacity_;
168     RefPtr<AnimatablePropertyFloat> barWidth_;
169     RefPtr<AnimatablePropertyFloat> barHeight_;
170     RefPtr<AnimatablePropertyFloat> barX_;
171     RefPtr<AnimatablePropertyFloat> barY_;
172 
173     // no Animatable
174     RefPtr<PropertyColor> barColor_;
175     bool isAdaptAnimationStop_ = true;
176     float lastMainModeHeight_ = 0.f;
177     float lastMainModeOffset_ = 0.f;
178     ACE_DISALLOW_COPY_AND_MOVE(ScrollBarOverlayModifier);
179 
180     std::shared_ptr<AnimationUtils::Animation> opacityAnimation_;
181     std::shared_ptr<AnimationUtils::Animation> adaptAnimation_;
182     HoverAnimationType hoverAnimatingType_ = HoverAnimationType::NONE;
183     OpacityAnimationType opacityAnimatingType_ = OpacityAnimationType::NONE;
184     PositionMode positionMode_ = PositionMode::RIGHT;
185 
186     bool isScrollable_ = true;
187     bool isNavDestinationShow_ = true;
188 };
189 } // namespace OHOS::Ace::NG
190 
191 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_OVERLAY_MODIFIER_H
192