• 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 "event_util.h"
17 #include "ace_log.h"
18 #include "async_task_manager.h"
19 
20 namespace OHOS {
21 namespace ACELite {
22 constexpr char ATTR_TYPE[] = "type";
23 constexpr char ATTR_TARGET[] = "target";
24 constexpr char ATTR_CURRENT_TARGET[] = "currentTarget";
25 constexpr char ATTR_TIMESTAMP[] = "timestamp";
26 constexpr char ATTR_GLOBAL_X[] = "globalX";
27 constexpr char ATTR_GLOBAL_Y[] = "globalY";
28 constexpr char ATTR_DIRECTION[] = "direction";
29 constexpr char ATTR_DIRECTION_RIGHT[] = "right";
30 constexpr char ATTR_DIRECTION_LEFT[] = "left";
31 constexpr char ATTR_DIRECTION_UP[] = "up";
32 constexpr char ATTR_DIRECTION_DOWN[] = "down";
33 const char *EventUtil::EVENT_CLICK = "click";
34 const char *EventUtil::EVENT_LONGPRESS = "longpress";
35 const char *EventUtil::EVENT_SWIPE = "swipe";
36 const char *EventUtil::EVENT_TOUCH = "touch";
37 
CallbackExecutor(void * data)38 void CallbackExecutor(void *data)
39 {
40     if (data == nullptr) {
41         HILOG_ERROR(HILOG_MODULE_ACE, "failed to execute the callback function of event.");
42         return;
43     }
44 
45     auto *params = static_cast<CallbackParams *>(data);
46     if (!JSFunction::Is(params->fn)) {
47         HILOG_ERROR(HILOG_MODULE_ACE, "failed to execute the callback function of event.");
48         delete params;
49         params = nullptr;
50         return;
51     }
52     constexpr uint8_t argsLength = 1;
53     JSValue args[argsLength] = {params->arg};
54     JSRelease(JSFunction::Call(params->fn, params->vm, args, argsLength));
55     JSRelease(params->arg);
56     delete params;
57     params = nullptr;
58 }
59 
CreateEvent(const char * type,UIView & view,const Event & event)60 JSValue EventUtil::CreateEvent(const char *type, UIView &view, const Event &event)
61 {
62     // create a JAVASCRIPT plain object that is used as the input parameter of
63     // the callback function for click or longpress event.
64     JSValue arg = JSObject::Create();
65     // set the 'type' attribute value
66     JSObject::SetString(arg, ATTR_TYPE, type);
67     // set the 'target' attribute value
68     UIView *target = nullptr;
69     UIView *currentTarget = nullptr;
70     Point point = event.GetCurrentPos();
71     view.GetTargetView(point, &currentTarget, &target);
72     JSObject::Set(arg, ATTR_TARGET, GetElementByUIView(target));
73     // set the 'currentTarget' attribute value
74     JSObject::Set(arg, ATTR_CURRENT_TARGET, GetElementByUIView(currentTarget));
75     // set the 'timestamp' attribute value
76     JSObject::SetNumber(arg, ATTR_TIMESTAMP, event.GetTimeStamp());
77     // set the 'globalX' attribute
78     JSObject::SetNumber(arg, ATTR_GLOBAL_X, point.x);
79     // set the 'globalY' attribute
80     JSObject::SetNumber(arg, ATTR_GLOBAL_Y, point.y);
81     return arg;
82 }
CreateSwipeEvent(UIView & view,const DragEvent & event)83 JSValue EventUtil::CreateSwipeEvent(UIView &view, const DragEvent &event)
84 {
85     // create a JAVASCRIPT plain object that is used as the input parameter of
86     // the event callback function.
87     JSValue arg = EventUtil::CreateEvent(EVENT_SWIPE, view, event);
88     // set the 'direction' attribute for the input parameter of the swipe event callback function.
89     switch (event.GetDragDirection()) {
90         case DragEvent::DIRECTION_LEFT_TO_RIGHT: {
91             JSObject::SetString(arg, ATTR_DIRECTION, ATTR_DIRECTION_RIGHT);
92             break;
93         }
94         case DragEvent::DIRECTION_RIGHT_TO_LEFT: {
95             JSObject::SetString(arg, ATTR_DIRECTION, ATTR_DIRECTION_LEFT);
96             break;
97         }
98         case DragEvent::DIRECTION_TOP_TO_BOTTOM: {
99             JSObject::SetString(arg, ATTR_DIRECTION, ATTR_DIRECTION_DOWN);
100             break;
101         }
102         case DragEvent::DIRECTION_BOTTOM_TO_TOP: {
103             JSObject::SetString(arg, ATTR_DIRECTION, ATTR_DIRECTION_UP);
104             break;
105         }
106         default: {
107             HILOG_ERROR(HILOG_MODULE_ACE, "failed to set direction attribute for event argument.");
108             break;
109         }
110     }
111     return arg;
112 }
CreateTouchEvent(UIView & view,const DragEvent & event)113 JSValue EventUtil::CreateTouchEvent(UIView &view, const DragEvent &event)
114 {
115     // create a JAVASCRIPT plain object that is used as the input parameter of
116     // the event callback function.
117     JSValue arg = EventUtil::CreateEvent(EVENT_TOUCH, view, event);
118 
119     return arg;
120 }
InvokeCallback(JSValue vm,JSValue callback,JSValue event,const void * context)121 void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event, const void *context)
122 {
123     auto *params = new CallbackParams();
124     if (params == nullptr) {
125         HILOG_ERROR(HILOG_MODULE_ACE, "fail to invoke event callback.");
126         return;
127     }
128     params->vm = vm;
129     params->fn = callback;
130     params->arg = event;
131     // The views may be destroyed or recreated in conditional or list rendering.
132     // If we directly call the event callback function, the program will crash.
133     if (DISPATCH_FAILURE ==
134         AsyncTaskManager::GetInstance().Dispatch(CallbackExecutor, static_cast<void *>(params), context)) {
135         HILOG_ERROR(HILOG_MODULE_ACE, "EventUtil::InvokeCallback failed: Async task dispatch failure.");
136         delete params;
137         params = nullptr;
138         JSRelease(event);
139     }
140 }
GetElementByUIView(UIView * view)141 JSValue EventUtil::GetElementByUIView(UIView *view)
142 {
143     if (view == nullptr) {
144         HILOG_ERROR(HILOG_MODULE_ACE, "fail to get element by UI view.");
145         return JSUndefined::Create();
146     }
147     UIView::ViewExtraMsg *extraMsg = view->GetExtraMsg();
148     if (extraMsg == nullptr) {
149         HILOG_ERROR(HILOG_MODULE_ACE, "fail to get element by UI view.");
150         return JSUndefined::Create();
151     }
152     JSValue *elementPtr = reinterpret_cast<JSValue *>(extraMsg->elementPtr);
153     if (elementPtr == nullptr) {
154         HILOG_ERROR(HILOG_MODULE_ACE, "fail to get element by UI view.");
155         return JSUndefined::Create();
156     }
157     return *elementPtr;
158 }
159 } // namespace ACELite
160 } // namespace OHOS
161