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_GESTURES_LONG_PRESS_RECOGNIZER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_LONG_PRESS_RECOGNIZER_H 18 19 #include "base/thread/cancelable_callback.h" 20 #include "core/gestures/multi_fingers_recognizer.h" 21 #include "core/pipeline/pipeline_context.h" 22 23 namespace OHOS::Ace { 24 25 class LongPressInfo : public BaseEventInfo, public TouchLocationInfo { 26 DECLARE_RELATIONSHIP_OF_CLASSES(LongPressInfo, BaseEventInfo, TouchLocationInfo); 27 28 public: LongPressInfo(int32_t fingerId)29 explicit LongPressInfo(int32_t fingerId) : BaseEventInfo("onLongPress"), TouchLocationInfo(fingerId) {} 30 ~LongPressInfo() override = default; 31 }; 32 33 using OnLongPress = std::function<void(const LongPressInfo&)>; 34 using LongPressCallback = std::function<void(const GestureEvent&)>; 35 using LongPressNoParamCallback = std::function<void()>; 36 37 class LongPressRecognizer : public MultiFingersRecognizer { 38 DECLARE_ACE_TYPE(LongPressRecognizer, MultiFingersRecognizer); 39 40 public: LongPressRecognizer(const WeakPtr<PipelineContext> & context)41 explicit LongPressRecognizer(const WeakPtr<PipelineContext>& context) : context_(context) {} LongPressRecognizer(const WeakPtr<PipelineContext> & context,int32_t duration,int32_t fingers,bool repeat)42 LongPressRecognizer(const WeakPtr<PipelineContext>& context, int32_t duration, int32_t fingers, bool repeat) 43 : context_(context), duration_(duration), repeat_(repeat) 44 { 45 fingers_ = fingers; 46 } 47 ~LongPressRecognizer() override = default; 48 49 void OnAccepted() override; 50 void OnRejected() override; 51 SetOnLongPress(const OnLongPress & onLongPress)52 void SetOnLongPress(const OnLongPress& onLongPress) 53 { 54 onLongPress_ = onLongPress; 55 } 56 57 private: 58 void HandleTouchDownEvent(const TouchEvent& event) override; 59 void HandleTouchUpEvent(const TouchEvent& event) override; 60 void HandleTouchMoveEvent(const TouchEvent& event) override; 61 void HandleTouchCancelEvent(const TouchEvent& event) override; 62 bool ReconcileFrom(const RefPtr<GestureRecognizer>& recognizer) override; 63 void HandleOverdueDeadline(); 64 void DeadlineTimer(int32_t time); 65 void DoRepeat(); 66 void StartRepeatTimer(); 67 void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback, bool isRepeat); 68 void Reset(); 69 double ConvertPxToVp(double offset) const; 70 71 WeakPtr<PipelineContext> context_; 72 OnLongPress onLongPress_; 73 CancelableCallback<void()> deadlineTimer_; 74 CancelableCallback<void()> timer_; 75 int32_t duration_ = 500; 76 int32_t fingers_ = 1; 77 bool repeat_ = false; 78 std::map<int32_t, TouchEvent> touchMap_; 79 int32_t pointsCount_ = 0; 80 TimeStamp time_; 81 bool pendingEnd_ = false; 82 bool pendingCancel_ = false; 83 }; 84 85 } // namespace OHOS::Ace 86 87 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_LONG_PRESS_RECOGNIZER_H 88