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 #include <limits>
21 
22 #include "base/geometry/ng/rect_t.h"
23 #include "base/geometry/ng/point_t.h"
24 #include "base/thread/cancelable_callback.h"
25 #include "core/accessibility/accessibility_utils.h"
26 #include "core/components_ng/gestures/tap_gesture.h"
27 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h"
28 #include "core/gestures/click_info.h"
29 
30 namespace OHOS::Ace::NG {
31 using OnAccessibilityEventFunc = std::function<void(AccessibilityEventType)>;
32 
33 class ClickRecognizer : public MultiFingersRecognizer {
34     DECLARE_ACE_TYPE(ClickRecognizer, MultiFingersRecognizer);
35 
36 public:
37     ClickRecognizer() = default;
38     ClickRecognizer(int32_t fingers, int32_t count, double distanceThreshold = std::numeric_limits<double>::infinity());
39 
40     ~ClickRecognizer() override = default;
41 
42     void OnAccepted() override;
43     void OnRejected() override;
44 
SetOnClick(const ClickCallback & onClick)45     void SetOnClick(const ClickCallback& onClick)
46     {
47         onClick_ = onClick;
48     }
49 
SetRemoteMessage(const ClickCallback & remoteMessage)50     void SetRemoteMessage(const ClickCallback& remoteMessage)
51     {
52         remoteMessage_ = remoteMessage;
53     }
54 
SetUseCatchMode(bool useCatchMode)55     void SetUseCatchMode(bool useCatchMode)
56     {
57         useCatchMode_ = useCatchMode;
58     }
59 
SetOnAccessibility(OnAccessibilityEventFunc onAccessibilityEvent)60     void SetOnAccessibility(OnAccessibilityEventFunc onAccessibilityEvent)
61     {
62         onAccessibilityEventFunc_ = std::move(onAccessibilityEvent);
63     }
64 
SetDistanceThreshold(double distanceThreshold)65     void SetDistanceThreshold(double distanceThreshold)
66     {
67         distanceThreshold_ = distanceThreshold;
68         if (distanceThreshold_ <= 0) {
69             distanceThreshold_ = std::numeric_limits<double>::infinity();
70         }
71     }
72 
GetCount()73     int GetCount()
74     {
75         return count_;
76     }
77 
GetTapActionFunc()78     GestureEventFunc GetTapActionFunc()
79     {
80         auto callback = [weak = WeakClaim(this)](GestureEvent& info) {
81             auto clickRecognizer = weak.Upgrade();
82             CHECK_NULL_VOID(clickRecognizer);
83             if (clickRecognizer->onAction_) {
84                 (*(clickRecognizer->onAction_))(info);
85             }
86         };
87         return callback;
88     }
89 
90     virtual RefPtr<GestureSnapshot> Dump() const override;
91     RefPtr<Gesture> CreateGestureFromRecognizer() const override;
92     void ForceCleanRecognizer() override;
93     void CleanRecognizerState() override;
94     GestureEvent GetGestureEventInfo();
95     ClickInfo GetClickInfo();
96 
97 private:
98     // Recognize whether MOVE/UP event is in response region.
99     bool IsPointInRegion(const TouchEvent& event);
100     void HandleTouchDownEvent(const TouchEvent& event) override;
101     void HandleTouchUpEvent(const TouchEvent& event) override;
102     void HandleTouchMoveEvent(const TouchEvent& event) override;
103     void HandleTouchCancelEvent(const TouchEvent& event) override;
104     bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override;
105 
OnResetStatus()106     void OnResetStatus() override
107     {
108         MultiFingersRecognizer::OnResetStatus();
109         tappedCount_ = 0;
110         equalsToFingers_ = false;
111         focusPoint_ = {};
112         fingerDeadlineTimer_.Cancel();
113         tapDeadlineTimer_.Cancel();
114         currentTouchPointsNum_ = 0;
115         responseRegionBuffer_.clear();
116     }
117 
118     void HandleOverdueDeadline();
119     void DeadlineTimer(CancelableCallback<void()>& deadlineTimer, int32_t time);
120     Offset ComputeFocusPoint();
121 
122     void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback);
123     GestureJudgeResult TriggerGestureJudgeCallback();
124     bool ExceedSlop();
125     void InitGlobalValue(SourceType deviceId);
126 
127     bool CheckNeedReceiveEvent();
128 
129     bool IsFormRenderClickRejected(const TouchEvent& event);
130     void TriggerClickAccepted(const TouchEvent& event);
131 
132     int32_t count_ = 1;
133     double distanceThreshold_ = std::numeric_limits<double>::infinity();
134 
135     // number of tap action.
136     int32_t tappedCount_ = 0;
137 
138     // Check whether the touch point num has reached the configured value
139     bool equalsToFingers_ = false;
140     // the time when gesture recognition is successful
141     TimeStamp time_;
142     Offset focusPoint_;
143     TimeStamp touchDownTime_;
144 
145     ClickCallback onClick_;
146     ClickCallback remoteMessage_;
147     bool useCatchMode_ = true;
148     CancelableCallback<void()> fingerDeadlineTimer_;
149     CancelableCallback<void()> tapDeadlineTimer_;
150     std::vector<RectF> responseRegionBuffer_;
151 
152     int32_t currentTouchPointsNum_ = 0;
153 
154     OnAccessibilityEventFunc onAccessibilityEventFunc_ = nullptr;
155 };
156 
157 } // namespace OHOS::Ace::NG
158 
159 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_CLICK_RECOGNIZER_H
160