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