1 /* 2 * Copyright (c) 2021-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_GESTURES_RECOGNIZERS_CLICK_RECOGNIZER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_CLICK_RECOGNIZER_H 18 19 #include <functional> 20 21 #include "base/thread/cancelable_callback.h" 22 #include "core/accessibility/accessibility_utils.h" 23 #include "core/components_ng/gestures/gesture_info.h" 24 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h" 25 26 namespace OHOS::Ace::NG { 27 using OnAccessibilityEventFunc = std::function<void(AccessibilityEventType)>; 28 29 class ClickRecognizer : public MultiFingersRecognizer { 30 DECLARE_ACE_TYPE(ClickRecognizer, MultiFingersRecognizer); 31 32 public: 33 ClickRecognizer() = default; 34 ClickRecognizer(int32_t fingers, int32_t count); 35 36 ~ClickRecognizer() override = default; 37 38 void OnAccepted() override; 39 void OnRejected() override; 40 SetOnClick(const ClickCallback & onClick)41 void SetOnClick(const ClickCallback& onClick) 42 { 43 onClick_ = onClick; 44 } 45 SetRemoteMessage(const ClickCallback & remoteMessage)46 void SetRemoteMessage(const ClickCallback& remoteMessage) 47 { 48 remoteMessage_ = remoteMessage; 49 } 50 SetUseCatchMode(bool useCatchMode)51 void SetUseCatchMode(bool useCatchMode) 52 { 53 useCatchMode_ = useCatchMode; 54 } 55 SetOnAccessibility(OnAccessibilityEventFunc onAccessibilityEvent)56 void SetOnAccessibility(OnAccessibilityEventFunc onAccessibilityEvent) 57 { 58 onAccessibilityEventFunc_ = std::move(onAccessibilityEvent); 59 } 60 GetCount()61 int GetCount() 62 { 63 return count_; 64 } 65 GetTapActionFunc()66 GestureEventFunc GetTapActionFunc() 67 { 68 auto callback = [weak = WeakClaim(this)](GestureEvent& info) { 69 auto clickRecognizer = weak.Upgrade(); 70 CHECK_NULL_VOID(clickRecognizer); 71 if (clickRecognizer->onAction_) { 72 (*(clickRecognizer->onAction_))(info); 73 } 74 }; 75 return callback; 76 } 77 78 private: 79 void HandleTouchDownEvent(const TouchEvent& event) override; 80 void HandleTouchUpEvent(const TouchEvent& event) override; 81 void HandleTouchMoveEvent(const TouchEvent& event) override; 82 void HandleTouchCancelEvent(const TouchEvent& event) override; 83 bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override; 84 OnResetStatus()85 void OnResetStatus() override 86 { 87 MultiFingersRecognizer::OnResetStatus(); 88 tappedCount_ = 0; 89 equalsToFingers_ = false; 90 focusPoint_ = {}; 91 fingerDeadlineTimer_.Cancel(); 92 tapDeadlineTimer_.Cancel(); 93 currentTouchPointsNum_ = 0; 94 } 95 96 void HandleOverdueDeadline(); 97 void DeadlineTimer(CancelableCallback<void()>& deadlineTimer, int32_t time); 98 Offset ComputeFocusPoint(); 99 100 void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback); 101 bool ExceedSlop(); 102 void InitGlobalValue(SourceType deviceId); 103 104 bool CheckNeedReceiveEvent(); 105 106 int32_t count_ = 1; 107 108 // number of tap action. 109 int32_t tappedCount_ = 0; 110 111 // Check whether the touch point num has reached the configured value 112 bool equalsToFingers_ = false; 113 // the time when gesture recognition is successful 114 TimeStamp time_; 115 Offset focusPoint_; 116 117 ClickCallback onClick_; 118 ClickCallback remoteMessage_; 119 bool useCatchMode_ = true; 120 CancelableCallback<void()> fingerDeadlineTimer_; 121 CancelableCallback<void()> tapDeadlineTimer_; 122 123 int32_t currentTouchPointsNum_ = 0; 124 125 OnAccessibilityEventFunc onAccessibilityEventFunc_ = nullptr; 126 }; 127 128 } // namespace OHOS::Ace::NG 129 130 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_CLICK_RECOGNIZER_H 131