1 /*
2  * Copyright (c) 2021 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 "frameworks/bridge/declarative_frontend/jsview/js_touch_handler.h"
17 
18 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
19 
20 namespace OHOS::Ace::Framework {
21 
CreateComponent(const JSCallbackInfo & args)22 RefPtr<OHOS::Ace::SingleChild> JSTouchHandler::CreateComponent(const JSCallbackInfo& args)
23 {
24         auto touchComponent = ViewStackProcessor::GetInstance()->GetTouchListenerComponent();
25 
26     if (jsOnDownFunc_) {
27         auto touchDownId = EventMarker(
28             [execCtx = args.GetExecutionContext(), func = std::move(jsOnDownFunc_)](BaseEventInfo* info) {
29                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
30                 TouchEventInfo* touchInfo = static_cast<TouchEventInfo*>(info);
31                 if (!touchInfo) {
32                     LOGE("Error processing event. Not an instance of TouchEventInfo");
33                     return;
34                 }
35                 func->Execute(*touchInfo);
36             },
37             "touchDown", 0);
38         touchComponent->SetOnTouchDownId(touchDownId);
39     }
40 
41     if (jsOnUpFunc_) {
42         auto touchUpId = EventMarker(
43             [execCtx = args.GetExecutionContext(), func = std::move(jsOnUpFunc_)](BaseEventInfo* info) {
44                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
45                 TouchEventInfo* touchInfo = static_cast<TouchEventInfo*>(info);
46                 if (!touchInfo) {
47                     LOGE("Error processing event. Not an instance of TouchEventInfo");
48                     return;
49                 }
50                 func->Execute(*touchInfo);
51             },
52             "touchUp", 0);
53         touchComponent->SetOnTouchUpId(touchUpId);
54     }
55 
56     if (jsOnMoveFunc_) {
57         auto touchMoveId = EventMarker(
58             [execCtx = args.GetExecutionContext(), func = std::move(jsOnMoveFunc_)](BaseEventInfo* info) {
59                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
60                 TouchEventInfo* touchInfo = static_cast<TouchEventInfo*>(info);
61                 if (!touchInfo) {
62                     LOGE("Error processing event. Not an instance of TouchEventInfo");
63                     return;
64                 }
65                 func->Execute(*touchInfo);
66             },
67             "touchMove", 0);
68         touchComponent->SetOnTouchMoveId(touchMoveId);
69     }
70 
71     if (jsOnCancelFunc_) {
72         auto touchCancelId = EventMarker(
73             [execCtx = args.GetExecutionContext(), func = std::move(jsOnCancelFunc_)](BaseEventInfo* info) {
74                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
75                 TouchEventInfo* touchInfo = static_cast<TouchEventInfo*>(info);
76                 if (!touchInfo) {
77                     LOGE("Error processing event. Not an instance of TouchEventInfo");
78                     return;
79                 }
80                 func->Execute(*touchInfo);
81             },
82             "touchCancel", 0);
83         touchComponent->SetOnTouchCancel(touchCancelId);
84     }
85 
86     return touchComponent;
87 }
88 
JsHandlerOnTouch(TouchEvent action,const JSCallbackInfo & args)89 void JSTouchHandler::JsHandlerOnTouch(TouchEvent action, const JSCallbackInfo& args)
90 {
91     if (args[0]->IsFunction()) {
92         JSRef<JSFunc> jsFunction = JSRef<JSFunc>::Cast(args[0]);
93         RefPtr<JsTouchFunction> handlerFunc = AceType::MakeRefPtr<JsTouchFunction>(jsFunction);
94         switch (action) {
95             case TouchEvent::DOWN:
96                 jsOnDownFunc_ = handlerFunc;
97                 break;
98             case TouchEvent::UP:
99                 jsOnUpFunc_ = handlerFunc;
100                 break;
101             case TouchEvent::MOVE:
102                 jsOnMoveFunc_ = handlerFunc;
103                 break;
104             case TouchEvent::CANCEL:
105                 jsOnCancelFunc_ = handlerFunc;
106                 break;
107             default:
108                 break;
109         }
110     }
111     args.ReturnSelf();
112 }
113 
JsHandlerOnDown(const JSCallbackInfo & args)114 void JSTouchHandler::JsHandlerOnDown(const JSCallbackInfo& args)
115 {
116     JSTouchHandler::JsHandlerOnTouch(TouchEvent::DOWN, args);
117 }
118 
JsHandlerOnUp(const JSCallbackInfo & args)119 void JSTouchHandler::JsHandlerOnUp(const JSCallbackInfo& args)
120 {
121     JSTouchHandler::JsHandlerOnTouch(TouchEvent::UP, args);
122 }
123 
JsHandlerOnMove(const JSCallbackInfo & args)124 void JSTouchHandler::JsHandlerOnMove(const JSCallbackInfo& args)
125 {
126     JSTouchHandler::JsHandlerOnTouch(TouchEvent::MOVE, args);
127 }
128 
JsHandlerOnCancel(const JSCallbackInfo & args)129 void JSTouchHandler::JsHandlerOnCancel(const JSCallbackInfo& args)
130 {
131     JSTouchHandler::JsHandlerOnTouch(TouchEvent::CANCEL, args);
132 }
133 
JSBind(BindingTarget globalObj)134 void JSTouchHandler::JSBind(BindingTarget globalObj)
135 {
136     JSClass<JSTouchHandler>::Declare("TouchHandler");
137     JSClass<JSTouchHandler>::CustomMethod("onDown", &JSTouchHandler::JsHandlerOnDown);
138     JSClass<JSTouchHandler>::CustomMethod("onUp", &JSTouchHandler::JsHandlerOnUp);
139     JSClass<JSTouchHandler>::CustomMethod("onMove", &JSTouchHandler::JsHandlerOnMove);
140     JSClass<JSTouchHandler>::CustomMethod("onCancel", &JSTouchHandler::JsHandlerOnCancel);
141     JSClass<JSTouchHandler>::Bind<>(globalObj);
142 }
143 }; // namespace OHOS::Ace::Framework
144