1 /*
2 * Copyright (c) 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 #include "core/components_ng/event/long_press_event.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/event/gesture_event_hub.h"
21 #include "core/components_ng/gestures/recognizers/long_press_recognizer.h"
22 #include "core/pipeline_ng/pipeline_context.h"
23
24 namespace OHOS::Ace::NG {
25
LongPressEventActuator(const WeakPtr<GestureEventHub> & gestureEventHub)26 LongPressEventActuator::LongPressEventActuator(const WeakPtr<GestureEventHub>& gestureEventHub)
27 : gestureEventHub_(gestureEventHub)
28 {}
29
OnCollectTouchTarget(const OffsetF & coordinateOffset,const TouchRestrict & touchRestrict,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result,ResponseLinkResult & responseLinkResult)30 void LongPressEventActuator::OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict,
31 const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result, ResponseLinkResult& responseLinkResult)
32 {
33 CHECK_NULL_VOID(longPressEvent_);
34 auto gestureHub = gestureEventHub_.Upgrade();
35 CHECK_NULL_VOID(gestureHub);
36 auto frameNode = gestureHub->GetFrameNode();
37 CHECK_NULL_VOID(frameNode);
38
39 if (!longPressRecognizer_) {
40 longPressRecognizer_ = MakeRefPtr<LongPressRecognizer>(isForDrag_, isDisableMouseLeft_);
41 }
42
43 longPressRecognizer_->SetIsSystemGesture(true);
44 longPressRecognizer_->SetRecognizerType(GestureTypeName::LONG_PRESS_GESTURE);
45 longPressRecognizer_->SetOnAction(GetGestureEventFunc());
46 longPressRecognizer_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
47 longPressRecognizer_->SetGetEventTargetImpl(getEventTargetImpl);
48 result.emplace_back(longPressRecognizer_);
49 responseLinkResult.emplace_back(longPressRecognizer_);
50 }
51
GetGestureEventFunc()52 GestureEventFunc LongPressEventActuator::GetGestureEventFunc()
53 {
54 auto callback = [weak = WeakClaim(this)](GestureEvent& info) {
55 auto actuator = weak.Upgrade();
56 CHECK_NULL_VOID(actuator);
57 if (actuator->longPressEvent_) {
58 (*actuator->longPressEvent_)(info);
59 }
60 if (actuator->onAccessibilityEventFunc_) {
61 actuator->onAccessibilityEventFunc_(AccessibilityEventType::LONG_PRESS);
62 }
63 };
64 return callback;
65 }
66
67 } // namespace OHOS::Ace::NG
68