1 /* 2 * Copyright (c) 2021-2022 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_PRESS_RECOGNIZER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_PRESS_RECOGNIZER_H 18 19 #include "base/thread/cancelable_callback.h" 20 #include "core/gestures/gesture_recognizer.h" 21 #include "core/pipeline/pipeline_context.h" 22 23 namespace OHOS::Ace { 24 25 class PressInfo : public TouchLocationInfo { 26 DECLARE_RELATIONSHIP_OF_CLASSES(PressInfo, TouchLocationInfo); 27 28 public: PressInfo(int32_t fingerId)29 explicit PressInfo(int32_t fingerId) : TouchLocationInfo("onPress", fingerId) {} 30 ~PressInfo() override = default; 31 }; 32 33 using OnPress = std::function<void(const PressInfo&)>; 34 using OnPressCancel = std::function<void()>; 35 36 class PressRecognizer : public GestureRecognizer { 37 DECLARE_ACE_TYPE(PressRecognizer, GestureRecognizer); 38 39 public: PressRecognizer(const WeakPtr<PipelineContext> & context)40 explicit PressRecognizer(const WeakPtr<PipelineContext>& context) : context_(context) {} 41 ~PressRecognizer() override = default; 42 43 void OnAccepted(size_t touchId) override; 44 void OnRejected(size_t touchId) override; 45 SetOnPress(const OnPress & onPress)46 void SetOnPress(const OnPress& onPress) 47 { 48 onPress_ = onPress; 49 } 50 SetOnPressCancel(const OnPressCancel & onPressCancel)51 void SetOnPressCancel(const OnPressCancel& onPressCancel) 52 { 53 onPressCancel_ = onPressCancel; 54 } 55 56 private: 57 void HandleTouchDownEvent(const TouchEvent& event) override; 58 void HandleTouchUpEvent(const TouchEvent& event) override; 59 void HandleTouchMoveEvent(const TouchEvent& event) override; 60 void HandleTouchCancelEvent(const TouchEvent& event) override; 61 void HandleOverdueDeadline(); 62 63 WeakPtr<PipelineContext> context_; 64 TouchEvent trackPoint_; 65 OnPress onPress_; 66 CancelableCallback<void()> deadlineTimer_; 67 OnPressCancel onPressCancel_; 68 }; 69 70 } // namespace OHOS::Ace 71 72 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_PRESS_RECOGNIZER_H 73