1 /*
2 * Copyright (c) 2022-2023 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/click_event.h"
17
18 #include "base/utils/utils.h"
19 #include "core/accessibility/accessibility_utils.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components_ng/base/frame_node.h"
22 #include "core/components_ng/event/gesture_event_hub.h"
23 #include "core/components_ng/event/gesture_info.h"
24 #include "core/components_ng/gestures/recognizers/click_recognizer.h"
25
26 namespace OHOS::Ace::NG {
27
ClickEventActuator(const WeakPtr<GestureEventHub> & gestureEventHub)28 ClickEventActuator::ClickEventActuator(const WeakPtr<GestureEventHub>& gestureEventHub)
29 : gestureEventHub_(gestureEventHub)
30 {}
31
OnCollectTouchTarget(const OffsetF & coordinateOffset,const TouchRestrict & touchRestrict,const GetEventTargetImpl & getEventTargetImpl,TouchTestResult & result)32 void ClickEventActuator::OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict,
33 const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result)
34 {
35 if (clickEvents_.empty() && !userCallback_) {
36 return;
37 }
38 auto gestureHub = gestureEventHub_.Upgrade();
39 CHECK_NULL_VOID(gestureHub);
40 auto frameNode = gestureHub->GetFrameNode();
41 CHECK_NULL_VOID(frameNode);
42
43 if (!clickRecognizer_) {
44 clickRecognizer_ = MakeRefPtr<ClickRecognizer>();
45 }
46 clickRecognizer_->SetGestureInfo(MakeRefPtr<GestureInfo>(GestureTypeName::CLICK, true));
47 clickRecognizer_->SetOnAction(GetClickEvent());
48 clickRecognizer_->SetCoordinateOffset(Offset(coordinateOffset.GetX(), coordinateOffset.GetY()));
49 clickRecognizer_->SetGetEventTargetImpl(getEventTargetImpl);
50 auto sysJudgeFunc = GetSysJudgeFunc();
51 if (sysJudgeFunc.has_value()) {
52 clickRecognizer_->SetSysGestureJudge(sysJudgeFunc.value());
53 }
54 result.emplace_back(clickRecognizer_);
55 }
56
GetSysJudgeFunc() const57 std::optional<GestureJudgeFunc> ClickEventActuator::GetSysJudgeFunc() const
58 {
59 for (const auto& callback : clickEvents_) {
60 if (callback->HasSysGestureJudge()) {
61 return callback->GetSysJudge();
62 }
63 }
64 return nullptr;
65 }
66
GetClickEvent()67 GestureEventFunc ClickEventActuator::GetClickEvent()
68 {
69 auto callback = [weak = WeakClaim(this)](GestureEvent& info) {
70 auto actuator = weak.Upgrade();
71 CHECK_NULL_VOID(actuator);
72 auto clickEvents = actuator->clickEvents_;
73 for (const auto& callback : clickEvents) {
74 if (callback) {
75 (*callback)(info);
76 }
77 }
78 if (actuator->userCallback_) {
79 // actuator->userCallback_ may be overwritten in its invoke so we copy it first
80 auto userCallback = actuator->userCallback_;
81 (*userCallback)(info);
82 }
83 if (actuator->onAccessibilityEventFunc_) {
84 actuator->onAccessibilityEventFunc_(AccessibilityEventType::CLICK);
85 }
86 };
87 return callback;
88 }
89
90 } // namespace OHOS::Ace::NG
91