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_BAR_CONTROLLER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_BAR_CONTROLLER_H 18 19 #include <functional> 20 21 #include "core/animation/animator.h" 22 #include "core/animation/friction_motion.h" 23 #include "core/animation/scroll_motion.h" 24 #include "core/common/vibrator/vibrator.h" 25 #include "core/event/touch_event.h" 26 #include "core/gestures/drag_recognizer.h" 27 #include "core/gestures/raw_recognizer.h" 28 #include "core/pipeline/base/render_node.h" 29 30 namespace OHOS::Ace { 31 32 using ScrollBarPositionCallback = std::function<bool(double, int32_t source)>; 33 using ScrollBarEndCallback = std::function<void(int32_t)>; 34 using ScrollBarEventCallback = std::function<void()>; 35 using ScrollBarTouchEventCallback = std::function<void(double)>; 36 37 class ScrollBarController : public TouchEventTarget { 38 DECLARE_ACE_TYPE(ScrollBarController, TouchEventTarget); 39 40 public: 41 ScrollBarController() = default; 42 ~ScrollBarController() override = default; 43 44 void Initialize(const WeakPtr<PipelineContext>& context); 45 void HandleScrollBarEnd(); 46 void HandleTouchDown(); 47 void HandleTouchUp(); 48 void HandleDragUpdate(const DragUpdateInfo& info); 49 void HandleDragEnd(const DragEndInfo& info); 50 virtual void MarkScrollRender(); 51 void Reset(); 52 DispatchEvent(const TouchEvent & point)53 bool DispatchEvent(const TouchEvent& point) override 54 { 55 return true; 56 } HandleEvent(const TouchEvent & event)57 bool HandleEvent(const TouchEvent& event) override 58 { 59 if (dragRecognizer_) { 60 dragRecognizer_->HandleEvent(event); 61 } 62 if (rawRecognizer_) { 63 return rawRecognizer_->HandleEvent(event); 64 } 65 return true; 66 } 67 SetCallback(const ScrollBarPositionCallback & callback)68 void SetCallback(const ScrollBarPositionCallback& callback) 69 { 70 callback_ = callback; 71 } 72 SetBarEndCallback(const ScrollBarEndCallback & barEndCallback)73 void SetBarEndCallback(const ScrollBarEndCallback& barEndCallback) 74 { 75 barEndCallback_ = barEndCallback; 76 } 77 SetCoordinateOffset(const Offset & offset)78 void SetCoordinateOffset(const Offset& offset) const 79 { 80 if (dragRecognizer_) { 81 dragRecognizer_->SetCoordinateOffset(offset); 82 } 83 if (rawRecognizer_) { 84 rawRecognizer_->SetCoordinateOffset(offset); 85 } 86 } 87 SetScrollEndCallback(const ScrollBarEventCallback & scrollEndCallback)88 void SetScrollEndCallback(const ScrollBarEventCallback& scrollEndCallback) 89 { 90 scrollEndCallback_ = scrollEndCallback; 91 } 92 SetTouchUpCallback(const ScrollBarTouchEventCallback & touchUpCallback)93 void SetTouchUpCallback(const ScrollBarTouchEventCallback& touchUpCallback) 94 { 95 touchUpCallback_ = touchUpCallback; 96 } 97 SetTouchDownCallback(const ScrollBarTouchEventCallback & touchDownCallback)98 void SetTouchDownCallback(const ScrollBarTouchEventCallback& touchDownCallback) 99 { 100 touchDownCallback_ = touchDownCallback; 101 } 102 SetScrollNode(const WeakPtr<RenderNode> & scroll)103 void SetScrollNode(const WeakPtr<RenderNode>& scroll) 104 { 105 scroll_ = scroll; 106 } 107 IsActive()108 bool IsActive() 109 { 110 return isActive_; 111 } 112 SetActive(bool isActive)113 void SetActive(bool isActive) 114 { 115 isActive_ = isActive; 116 } 117 SetInactiveWidth(const Dimension & inactiveWidth)118 void SetInactiveWidth(const Dimension& inactiveWidth) 119 { 120 inactiveWidth_ = inactiveWidth; 121 } 122 SetActiveWidth(const Dimension & activeWidth)123 void SetActiveWidth(const Dimension& activeWidth) 124 { 125 activeWidth_ = activeWidth; 126 } 127 GetInactiveWidth()128 const Dimension& GetInactiveWidth() const 129 { 130 return inactiveWidth_; 131 } 132 GetActiveWidth()133 const Dimension& GetActiveWidth() const 134 { 135 return activeWidth_; 136 } 137 138 protected: 139 virtual bool UpdateScrollPosition(double offset, int32_t source); 140 141 virtual void ProcessScrollMotion(double position); 142 143 virtual bool CheckScroll(); 144 145 WeakPtr<RenderNode> scroll_; 146 ScrollBarPositionCallback callback_; 147 ScrollBarEventCallback scrollEndCallback_; 148 ScrollBarEndCallback barEndCallback_; 149 ScrollBarTouchEventCallback touchUpCallback_; 150 ScrollBarTouchEventCallback touchDownCallback_; 151 152 RefPtr<DragRecognizer> dragRecognizer_; 153 RefPtr<RawRecognizer> rawRecognizer_; 154 155 RefPtr<Animator> scrollEndAnimator_; 156 RefPtr<Animator> touchAnimator_; 157 RefPtr<Animator> dragEndAnimator_; 158 RefPtr<FrictionMotion> dragEndMotion_; 159 RefPtr<Vibrator> vibrator_; 160 WeakPtr<PipelineContext> context_; 161 162 bool isActive_ = false; 163 double currentPos_ = 0.0; 164 Dimension activeWidth_; 165 Dimension inactiveWidth_; 166 }; 167 168 } // namespace OHOS::Ace 169 170 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_BAR_CONTROLLER_H 171