• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CLICK_EVENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_CLICK_EVENT_H
18 
19 #include <list>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "base/utils/noncopyable.h"
24 #include "core/components_ng/event/gesture_event_actuator.h"
25 #include "core/components_ng/gestures/recognizers/click_recognizer.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class GestureEventHub;
30 
31 class ClickEvent : public AceType {
DECLARE_ACE_TYPE(ClickEvent,AceType)32     DECLARE_ACE_TYPE(ClickEvent, AceType)
33 public:
34     explicit ClickEvent(GestureEventFunc&& callback) : callback_(std::move(callback)) {}
35     ~ClickEvent() override = default;
36 
GetGestureEventFunc()37     const GestureEventFunc& GetGestureEventFunc() const
38     {
39         return callback_;
40     }
41 
operator()42     void operator()(GestureEvent& info) const
43     {
44         if (callback_) {
45             callback_(info);
46         }
47     }
48 
SetSysJudge(const GestureJudgeFunc & sysJudge)49     void SetSysJudge(const GestureJudgeFunc& sysJudge)
50     {
51         sysJudge_ = sysJudge;
52     }
53 
HasSysGestureJudge()54     bool HasSysGestureJudge() const
55     {
56         return sysJudge_.has_value();
57     }
58 
GetSysJudge()59     std::optional<GestureJudgeFunc> GetSysJudge() const
60     {
61         if (sysJudge_.has_value()) {
62             return sysJudge_.value();
63         }
64         return nullptr;
65     }
66 
67 private:
68     GestureEventFunc callback_;
69 
70     std::optional<GestureJudgeFunc> sysJudge_ = nullptr;
71 
72     ACE_DISALLOW_COPY_AND_MOVE(ClickEvent);
73 };
74 
75 class ACE_EXPORT ClickEventActuator : public GestureEventActuator {
76     DECLARE_ACE_TYPE(ClickEventActuator, GestureEventActuator)
77 public:
78     explicit ClickEventActuator(const WeakPtr<GestureEventHub>& gestureEventHub);
79     ~ClickEventActuator() override = default;
80 
SetUserCallback(GestureEventFunc && callback)81     void SetUserCallback(GestureEventFunc&& callback)
82     {
83         userCallback_ = MakeRefPtr<ClickEvent>(std::move(callback));
84     }
85 
ClearUserCallback()86     void ClearUserCallback()
87     {
88         // When the event param is undefined, it will clear the callback.
89         if (userCallback_) {
90             userCallback_.Reset();
91         }
92     }
93 
AddClickEvent(const RefPtr<ClickEvent> & clickEvent)94     void AddClickEvent(const RefPtr<ClickEvent>& clickEvent)
95     {
96         if (clickEvents_.empty()) {
97             clickEvents_.emplace_back(clickEvent);
98             return;
99         }
100         if (std::find(clickEvents_.begin(), clickEvents_.end(), clickEvent) == clickEvents_.end()) {
101             clickEvents_.emplace_back(clickEvent);
102         }
103     }
104 
RemoveClickEvent(const RefPtr<ClickEvent> & clickEvent)105     void RemoveClickEvent(const RefPtr<ClickEvent>& clickEvent)
106     {
107         clickEvents_.remove(clickEvent);
108     }
109 
IsClickEventsEmpty()110     bool IsClickEventsEmpty() const
111     {
112         return clickEvents_.empty();
113     }
114 
115     void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict,
116         const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result) override;
117 
118     GestureEventFunc GetClickEvent();
119     std::optional<GestureJudgeFunc> GetSysJudgeFunc() const;
120 
GetClickRecognizer()121     const RefPtr<ClickRecognizer>& GetClickRecognizer()
122     {
123         if (!clickRecognizer_) {
124             clickRecognizer_ = MakeRefPtr<ClickRecognizer>();
125         }
126         return clickRecognizer_;
127     }
128 
CopyClickEvent(const RefPtr<ClickEventActuator> & clickEventActuator)129     void CopyClickEvent(const RefPtr<ClickEventActuator>& clickEventActuator)
130     {
131         clickEvents_ = clickEventActuator->clickEvents_;
132         userCallback_ = clickEventActuator->userCallback_;
133         if (clickEventActuator->clickRecognizer_) {
134             clickRecognizer_ = MakeRefPtr<ClickRecognizer>();
135         }
136     }
137 
138 private:
139     WeakPtr<GestureEventHub> gestureEventHub_;
140     std::list<RefPtr<ClickEvent>> clickEvents_;
141     RefPtr<ClickEvent> userCallback_;
142     RefPtr<ClickRecognizer> clickRecognizer_;
143 
144     ACE_DISALLOW_COPY_AND_MOVE(ClickEventActuator);
145 };
146 
147 } // namespace OHOS::Ace::NG
148 
149 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_CLICK_EVENT_H
150