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_LONG_PRESS_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_LONG_PRESS_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/event/drag_event.h"
24 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
25 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h"
26 
27 namespace OHOS::Ace::NG {
28 using OnAccessibilityEventFunc = std::function<void(AccessibilityEventType)>;
29 
30 class GestureEventHub;
31 class LongPressInfo : public TouchLocationInfo {
32     DECLARE_RELATIONSHIP_OF_CLASSES(LongPressInfo, TouchLocationInfo);
33 
34 public:
LongPressInfo(int32_t fingerId)35     explicit LongPressInfo(int32_t fingerId) : TouchLocationInfo("onLongPress", fingerId) {}
36     ~LongPressInfo() override = default;
37 };
38 
39 using OnLongPress = std::function<void(const LongPressInfo&)>;
40 using LongPressCallback = std::function<void(const GestureEvent&)>;
41 using LongPressNoParamCallback = std::function<void()>;
42 
43 class ACE_FORCE_EXPORT LongPressRecognizer : public MultiFingersRecognizer {
44     DECLARE_ACE_TYPE(LongPressRecognizer, MultiFingersRecognizer);
45 
46 public:
47     explicit LongPressRecognizer() = default;
48     LongPressRecognizer(int32_t duration, int32_t fingers, bool repeat,
49         bool isForDrag = false, bool isDisableMouseLeft = false);
50 
51     LongPressRecognizer(bool isForDrag = false, bool isDisableMouseLeft = false)
52         : isForDrag_(isForDrag), isDisableMouseLeft_(isDisableMouseLeft)
53     {}
54     ~LongPressRecognizer() override = default;
55 
56     void OnAccepted() override;
57     void OnRejected() override;
58 
SetOnLongPress(const OnLongPress & onLongPress)59     void SetOnLongPress(const OnLongPress& onLongPress)
60     {
61         onLongPress_ = onLongPress;
62     }
63 
HasAction()64     bool HasAction() const
65     {
66         if (onAction_ && *onAction_) {
67             return true;
68         }
69         return false;
70     }
71 
SetOnLongPressRecorder(const GestureEventFunc & recorder)72     void SetOnLongPressRecorder(const GestureEventFunc& recorder)
73     {
74         longPressRecorder_ = std::make_unique<GestureEventFunc>(recorder);
75     }
76 
SetUseCatchMode(bool useCatchMode)77     void SetUseCatchMode(bool useCatchMode)
78     {
79         useCatchMode_ = useCatchMode;
80     }
81 
SetDuration(int32_t duration)82     void SetDuration(int32_t duration)
83     {
84         duration_ = duration;
85     }
86 
GetDuration()87     int32_t GetDuration() const
88     {
89         return duration_;
90     }
91 
SetGestureHub(WeakPtr<GestureEventHub> gestureHub)92     void SetGestureHub(WeakPtr<GestureEventHub> gestureHub)
93     {
94         gestureHub_ = gestureHub;
95     }
96 
SetThumbnailDeadline(int32_t deadlineTime)97     void SetThumbnailDeadline(int32_t deadlineTime)
98     {
99         thumbnailDeadline = deadlineTime;
100     }
101 
SetThumbnailCallback(std::function<void (Offset)> && callback)102     void SetThumbnailCallback(std::function<void(Offset)>&& callback)
103     {
104         callback_ = std::move(callback);
105     }
106 
HasThumbnailCallback()107     bool HasThumbnailCallback()
108     {
109         return static_cast<bool>(callback_);
110     }
111 
SetOnAccessibility(OnAccessibilityEventFunc onAccessibilityEvent)112     void SetOnAccessibility(OnAccessibilityEventFunc onAccessibilityEvent)
113     {
114         onAccessibilityEventFunc_ = std::move(onAccessibilityEvent);
115     }
116 
117     GestureEventFunc GetLongPressActionFunc();
118 
119     virtual RefPtr<GestureSnapshot> Dump() const override;
120 
121     void PrintCurrentFingersInfo() const;
122 
123 private:
124     void HandleTouchDownEvent(const TouchEvent& event) override;
125     void HandleTouchUpEvent(const TouchEvent& event) override;
126     void HandleTouchMoveEvent(const TouchEvent& event) override;
127     void HandleTouchCancelEvent(const TouchEvent& event) override;
128     bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override;
129     void HandleOverdueDeadline(bool isCatchMode);
130     void DeadlineTimer(int32_t time, bool isCatchMode);
131     void DoRepeat();
132     void StartRepeatTimer();
133     void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback, bool isRepeat, bool isOnAction = false);
134     GestureJudgeResult TriggerGestureJudgeCallback();
135     void OnResetStatus() override;
136     double ConvertPxToVp(double offset) const;
137     void ThumbnailTimer(int32_t time);
138     RefPtr<DragEventActuator> GetDragEventActuator();
139 
140     TouchEvent lastTouchEvent_;
141     WeakPtr<GestureEventHub> gestureHub_;
142     CancelableCallback<void()> thumbnailTimer_;
143     int32_t thumbnailDeadline = 150;
144     OnLongPress onLongPress_;
145     CancelableCallback<void()> deadlineTimer_;
146     CancelableCallback<void()> timer_;
147     std::function<void(Offset)> callback_;
148     int32_t duration_ = 500;
149     bool repeat_ = false;
150     TimeStamp time_;
151     bool useCatchMode_ = true;
152     bool isForDrag_ = false;
153     bool isDisableMouseLeft_ = false;
154     Point globalPoint_;
155     DelayedTask task_;
156     OnAccessibilityEventFunc onAccessibilityEventFunc_ = nullptr;
157     std::unique_ptr<GestureEventFunc> longPressRecorder_;
158 };
159 
160 } // namespace OHOS::Ace::NG
161 
162 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_LONG_PRESS_RECOGNIZER_H
163