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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_LONG_PRESS_EVENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_LONG_PRESS_EVENT_H
18 
19 #include <list>
20 
21 #include "base/memory/referenced.h"
22 #include "base/utils/noncopyable.h"
23 #include "core/components_ng/event/gesture_event_actuator.h"
24 #include "core/components_ng/gestures/recognizers/long_press_recognizer.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 class GestureEventHub;
29 
30 class LongPressEvent : public virtual AceType {
DECLARE_ACE_TYPE(LongPressEvent,AceType)31     DECLARE_ACE_TYPE(LongPressEvent, AceType)
32 public:
33     explicit LongPressEvent(GestureEventFunc&& callback) : callback_(std::move(callback)) {}
34     ~LongPressEvent() override = default;
35 
GetGestureEventFunc()36     const GestureEventFunc& GetGestureEventFunc() const
37     {
38         return callback_;
39     }
40 
operator()41     void operator()(GestureEvent& info) const
42     {
43         if (callback_) {
44             callback_(info);
45         }
46     }
47 
48 private:
49     GestureEventFunc callback_;
50 
51     ACE_DISALLOW_COPY_AND_MOVE(LongPressEvent);
52 };
53 
54 class ACE_FORCE_EXPORT LongPressEventActuator : public GestureEventActuator {
55     DECLARE_ACE_TYPE(LongPressEventActuator, GestureEventActuator)
56 public:
57     explicit LongPressEventActuator(const WeakPtr<GestureEventHub>& gestureEventHub);
58     ~LongPressEventActuator() override = default;
59 
60     void SetLongPressEvent(const RefPtr<LongPressEvent>& event, bool isForDrag = false, bool isDisableMouseLeft = false)
61     {
62         longPressEvent_ = event;
63         isForDrag_ = isForDrag;
64         isDisableMouseLeft_ = isDisableMouseLeft;
65     }
66 
GetLongPressRecognizer()67     RefPtr<LongPressRecognizer> GetLongPressRecognizer() const
68     {
69         return longPressRecognizer_;
70     }
71 
SetDuration(int32_t duration)72     void SetDuration(int32_t duration)
73     {
74         if (!longPressRecognizer_) {
75             longPressRecognizer_ = MakeRefPtr<LongPressRecognizer>(isForDrag_, isDisableMouseLeft_);
76         }
77         longPressRecognizer_->SetDuration(duration);
78     }
79 
80     void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict,
81         const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result,
82         ResponseLinkResult& responseLinkResult) override;
83 
84     GestureEventFunc GetGestureEventFunc();
85 
CopyLongPressEvent(const RefPtr<LongPressEventActuator> & longPressEventActuator)86     void CopyLongPressEvent(const RefPtr<LongPressEventActuator>& longPressEventActuator)
87     {
88         isForDrag_ = longPressEventActuator->isForDrag_;
89         isDisableMouseLeft_ = longPressEventActuator->isDisableMouseLeft_;
90         auto originalLongPressRecognizer = longPressEventActuator->longPressRecognizer_;
91         if (originalLongPressRecognizer) {
92             if (!longPressRecognizer_) {
93                 longPressRecognizer_ = MakeRefPtr<LongPressRecognizer>(isForDrag_, isDisableMouseLeft_);
94             }
95             longPressRecognizer_->SetDuration(originalLongPressRecognizer->GetDuration());
96         }
97     }
98 private:
99     bool isForDrag_ = false;
100     bool isDisableMouseLeft_ = false;
101     WeakPtr<GestureEventHub> gestureEventHub_;
102     RefPtr<LongPressRecognizer> longPressRecognizer_;
103     RefPtr<LongPressEvent> longPressEvent_;
104 
105     ACE_DISALLOW_COPY_AND_MOVE(LongPressEventActuator);
106 };
107 
108 } // namespace OHOS::Ace::NG
109 
110 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_LONG_PRESS_EVENT_H
111