• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "frameworks/bridge/declarative_frontend/jsview/js_pan_handler.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
20 
21 namespace OHOS::Ace::Framework {
22 
CreateComponent(const JSCallbackInfo & args)23 RefPtr<OHOS::Ace::SingleChild> JSPanHandler::CreateComponent(const JSCallbackInfo& args)
24 {
25     LOGD("JSPanHandler wrapComponent");
26     auto gestureComponent = ViewStackProcessor::GetInstance()->GetPanGestureListenerComponent();
27 
28     if (jsOnStartFunc_) {
29         auto dragStartId = EventMarker(
30             [execCtx = args.GetExecutionContext(), func = std::move(jsOnStartFunc_)](const BaseEventInfo* info) {
31                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
32                 const DragStartInfo* dragStartInfo = static_cast<const DragStartInfo*>(info);
33 
34                 if (!dragStartInfo) {
35                     LOGE("Error processing event. Not an instance of DragStartInfo");
36                     return;
37                 }
38                 ACE_SCORING_EVENT("PanHandler.onDragStart");
39                 func->Execute(*dragStartInfo);
40             },
41             "dragStart", 0);
42         gestureComponent->SetOnVerticalDragStartId(dragStartId);
43     }
44 
45     if (jsOnUpdateFunc_) {
46         auto dragUpdateId = EventMarker(
47             [execCtx = args.GetExecutionContext(), func = std::move(jsOnUpdateFunc_)](const BaseEventInfo* info) {
48                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
49                 const DragUpdateInfo* dragUpdateInfo = static_cast<const DragUpdateInfo*>(info);
50 
51                 if (!dragUpdateInfo) {
52                     LOGE("Error processing event. Not an instance of DragUpdateInfo");
53                     return;
54                 }
55                 ACE_SCORING_EVENT("PanHandler.onDragUpdate");
56                 func->Execute(*dragUpdateInfo);
57             },
58             "dragUpdate", 0);
59         gestureComponent->SetOnVerticalDragUpdateId(dragUpdateId);
60     }
61 
62     if (jsOnEndFunc_) {
63         auto dragEndId = EventMarker(
64             [execCtx = args.GetExecutionContext(), func = std::move(jsOnEndFunc_)](const BaseEventInfo* info) {
65                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
66                 const DragEndInfo* dragEndInfo = static_cast<const DragEndInfo*>(info);
67 
68                 if (!dragEndInfo) {
69                     LOGE("Error processing event. Not an instance of DragEndInfo");
70                     return;
71                 }
72                 ACE_SCORING_EVENT("PanHandler.onDragEnd");
73                 func->Execute(*dragEndInfo);
74             },
75             "dragEnd", 0);
76         gestureComponent->SetOnVerticalDragEndId(dragEndId);
77     }
78 
79     if (jsOnCancelFunc_) {
80         auto dragCancelId = EventMarker(
81             [execCtx = args.GetExecutionContext(), func = std::move(jsOnCancelFunc_)](const BaseEventInfo* info) {
82                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
83                 ACE_SCORING_EVENT("PanHandler.onDragCancel");
84                 func->Execute();
85             }, "dragCancel", 0);
86         gestureComponent->SetOnVerticalDragCancelId(dragCancelId);
87     }
88 
89     return gestureComponent;
90 }
91 
JsHandlerOnPan(PanEvent action,const JSCallbackInfo & args)92 void JSPanHandler::JsHandlerOnPan(PanEvent action, const JSCallbackInfo& args)
93 {
94     LOGD("JSPanHandler JsHandlerOnPan");
95     if (args[0]->IsFunction()) {
96         JSRef<JSFunc> jsFunction = JSRef<JSFunc>::Cast(args[0]);
97         RefPtr<JsPanFunction> handlerFunc = AceType::MakeRefPtr<JsPanFunction>(jsFunction);
98         switch (action) {
99             case PanEvent::START:
100                 jsOnStartFunc_ = handlerFunc;
101                 break;
102             case PanEvent::UPDATE:
103                 jsOnUpdateFunc_ = handlerFunc;
104                 break;
105             case PanEvent::END:
106                 jsOnEndFunc_ = handlerFunc;
107                 break;
108             case PanEvent::CANCEL:
109                 jsOnCancelFunc_ = handlerFunc;
110                 break;
111             default:
112                 break;
113         }
114     }
115     args.ReturnSelf();
116 }
117 
JsHandlerOnStart(const JSCallbackInfo & args)118 void JSPanHandler::JsHandlerOnStart(const JSCallbackInfo& args)
119 {
120     JSPanHandler::JsHandlerOnPan(PanEvent::START, args);
121 }
122 
JsHandlerOnUpdate(const JSCallbackInfo & args)123 void JSPanHandler::JsHandlerOnUpdate(const JSCallbackInfo& args)
124 {
125     JSPanHandler::JsHandlerOnPan(PanEvent::UPDATE, args);
126 }
127 
JsHandlerOnEnd(const JSCallbackInfo & args)128 void JSPanHandler::JsHandlerOnEnd(const JSCallbackInfo& args)
129 {
130     JSPanHandler::JsHandlerOnPan(PanEvent::END, args);
131 }
132 
JsHandlerOnCancel(const JSCallbackInfo & args)133 void JSPanHandler::JsHandlerOnCancel(const JSCallbackInfo& args)
134 {
135     JSPanHandler::JsHandlerOnPan(PanEvent::CANCEL, args);
136 }
137 
JSBind(BindingTarget globalObj)138 void JSPanHandler::JSBind(BindingTarget globalObj)
139 {
140     JSClass<JSPanHandler>::Declare("PanHandler");
141     JSClass<JSPanHandler>::CustomMethod("onStart", &JSPanHandler::JsHandlerOnStart);
142     JSClass<JSPanHandler>::CustomMethod("onUpdate", &JSPanHandler::JsHandlerOnUpdate);
143     JSClass<JSPanHandler>::CustomMethod("onEnd", &JSPanHandler::JsHandlerOnEnd);
144     JSClass<JSPanHandler>::CustomMethod("onCancel", &JSPanHandler::JsHandlerOnCancel);
145     JSClass<JSPanHandler>::Bind<>(globalObj);
146 }
147 }; // namespace OHOS::Ace::Framework
148