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_SCROLL_SCROLL_EDGE_EFFECT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_EDGE_EFFECT_H 18 19 #include <functional> 20 21 #include "base/memory/ace_type.h" 22 #include "core/components/common/layout/constants.h" 23 #include "core/components/common/properties/color.h" 24 #include "core/components/scroll/scrollable.h" 25 26 namespace OHOS::Ace { 27 28 using EdgeCallback = std::function<double()>; 29 30 class ScrollEdgeEffect : public AceType { 31 DECLARE_ACE_TYPE(ScrollEdgeEffect, AceType); 32 33 public: ScrollEdgeEffect(EdgeEffect edgeEffect)34 explicit ScrollEdgeEffect(EdgeEffect edgeEffect) 35 { 36 edgeEffect_ = edgeEffect; 37 } 38 39 ~ScrollEdgeEffect() override = default; 40 GetEdgeEffect()41 EdgeEffect GetEdgeEffect() const 42 { 43 return edgeEffect_; 44 } 45 46 // support layout out of boundary fill with blank IsRestrictBoundary()47 bool IsRestrictBoundary() const 48 { 49 return edgeEffect_ != EdgeEffect::SPRING; 50 } 51 IsFadeEffect()52 bool IsFadeEffect() const 53 { 54 return edgeEffect_ == EdgeEffect::FADE; 55 } 56 IsSpringEffect()57 bool IsSpringEffect() const 58 { 59 return edgeEffect_ == EdgeEffect::SPRING; 60 } 61 SetScrollable(const RefPtr<Scrollable> & scrollable)62 void SetScrollable(const RefPtr<Scrollable>& scrollable) 63 { 64 scrollable_ = scrollable; 65 } 66 SetScrollNode(const WeakPtr<RenderNode> & scroll)67 void SetScrollNode(const WeakPtr<RenderNode>& scroll) 68 { 69 scroll_ = scroll; 70 } 71 SetCurrentPositionCallback(const EdgeCallback & currentPositionCallback)72 void SetCurrentPositionCallback(const EdgeCallback& currentPositionCallback) 73 { 74 currentPositionCallback_ = currentPositionCallback; 75 } 76 SetLeadingCallback(const EdgeCallback & leadingCallback)77 void SetLeadingCallback(const EdgeCallback& leadingCallback) 78 { 79 leadingCallback_ = leadingCallback; 80 } 81 SetTrailingCallback(const EdgeCallback & trainingCallback)82 void SetTrailingCallback(const EdgeCallback& trainingCallback) 83 { 84 trailingCallback_ = trainingCallback; 85 } 86 SetInitLeadingCallback(const EdgeCallback & initLeadingCallback)87 void SetInitLeadingCallback(const EdgeCallback& initLeadingCallback) 88 { 89 initLeadingCallback_ = initLeadingCallback; 90 } 91 SetInitTrailingCallback(const EdgeCallback & initTrainingCallback)92 void SetInitTrailingCallback(const EdgeCallback& initTrainingCallback) 93 { 94 initTrailingCallback_ = initTrainingCallback; 95 } 96 97 // common use InitialEdgeEffect()98 virtual void InitialEdgeEffect() {} 99 100 // fade effect use CalculateOverScroll(double oldPosition,bool isReachMax)101 virtual double CalculateOverScroll(double oldPosition, bool isReachMax) 102 { 103 return 0.0; 104 } 105 HandleOverScroll(Axis axis,double overScroll,const Size & viewPort)106 virtual void HandleOverScroll(Axis axis, double overScroll, const Size& viewPort) {} 107 Paint(RenderContext & context,const Size & viewPort,const Offset & offset)108 virtual void Paint(RenderContext& context, const Size& viewPort, const Offset& offset) {} 109 110 // spring effect use RegisterSpringCallback()111 virtual void RegisterSpringCallback() {} 112 ProcessScrollOver(double velocity)113 virtual void ProcessScrollOver(double velocity) {} 114 115 protected: 116 RefPtr<Scrollable> scrollable_; 117 WeakPtr<RenderNode> scroll_; 118 EdgeCallback currentPositionCallback_; // get current position 119 EdgeCallback leadingCallback_; // get stable min extent 120 EdgeCallback trailingCallback_; // get stable max extent 121 EdgeCallback initLeadingCallback_; // get current min extent 122 EdgeCallback initTrailingCallback_; // get current max extent 123 124 private: 125 EdgeEffect edgeEffect_ = EdgeEffect::SPRING; 126 }; 127 128 } // namespace OHOS::Ace 129 130 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_EDGE_EFFECT_H 131