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