1 /*
2 * Copyright (c) 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 "js_gesture_event.h"
17
18 #include "mmi_log.h"
19 #include "napi_constants.h"
20 #include "util_napi.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "JsGestureEvent"
24 namespace OHOS {
25 namespace MMI {
26
27 enum class ActionType : int32_t {
28 CANCEL = 0,
29 BEGIN = 1,
30 UPDATE = 2,
31 END = 3,
32 };
33
GetNapiInt32(napi_env env,int32_t code)34 napi_value JsGestureEvent::GetNapiInt32(napi_env env, int32_t code)
35 {
36 CALL_DEBUG_ENTER;
37 napi_value ret = nullptr;
38 CHKRP(napi_create_int32(env, code, &ret), CREATE_INT32);
39 return ret;
40 }
41
GetNapiString(napi_env env,std::string str)42 napi_value JsGestureEvent::GetNapiString(napi_env env, std::string str)
43 {
44 CALL_DEBUG_ENTER;
45 napi_value ret = nullptr;
46 CHKRP(napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &ret), CREATE_STRING_UTF8);
47 return ret;
48 }
49
EnumClassConstructor(napi_env env,napi_callback_info info)50 napi_value JsGestureEvent::EnumClassConstructor(napi_env env, napi_callback_info info)
51 {
52 CALL_DEBUG_ENTER;
53 size_t argc = 0;
54 napi_value args[1] = { 0 };
55 napi_value ret = nullptr;
56 void *data = nullptr;
57 CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
58 return ret;
59 }
60
Export(napi_env env,napi_value exports)61 napi_value JsGestureEvent::Export(napi_env env, napi_value exports)
62 {
63 CALL_INFO_TRACE;
64 napi_property_descriptor actionArr[] = {
65 DECLARE_NAPI_STATIC_PROPERTY("CANCEL", GetNapiInt32(env, static_cast<int32_t>(ActionType::CANCEL))),
66 DECLARE_NAPI_STATIC_PROPERTY("BEGIN", GetNapiInt32(env, static_cast<int32_t>(ActionType::BEGIN))),
67 DECLARE_NAPI_STATIC_PROPERTY("UPDATE", GetNapiInt32(env, static_cast<int32_t>(ActionType::UPDATE))),
68 DECLARE_NAPI_STATIC_PROPERTY("END", GetNapiInt32(env, static_cast<int32_t>(ActionType::END))),
69 };
70 napi_value actionType = nullptr;
71 CHKRP(napi_define_class(env, "ActionType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
72 sizeof(actionArr) / sizeof(*actionArr), actionArr, &actionType), DEFINE_CLASS);
73 CHKRP(napi_set_named_property(env, exports, "ActionType", actionType), SET_NAMED_PROPERTY);
74
75 napi_property_descriptor gestureActionArr[] = {
76 DECLARE_NAPI_STATIC_PROPERTY("SWIPE_DOWN",
77 GetNapiInt32(env, static_cast<int32_t>(TouchGesturAction::SWIPE_DOWN))),
78 DECLARE_NAPI_STATIC_PROPERTY("SWIPE_UP",
79 GetNapiInt32(env, static_cast<int32_t>(TouchGesturAction::SWIPE_UP))),
80 DECLARE_NAPI_STATIC_PROPERTY("SWIPE_LEFT",
81 GetNapiInt32(env, static_cast<int32_t>(TouchGesturAction::SWIPE_LEFT))),
82 DECLARE_NAPI_STATIC_PROPERTY("SWIPE_RIGHT",
83 GetNapiInt32(env, static_cast<int32_t>(TouchGesturAction::SWIPE_RIGHT))),
84 DECLARE_NAPI_STATIC_PROPERTY("PINCH_CLOSED",
85 GetNapiInt32(env, static_cast<int32_t>(TouchGesturAction::PINCH_CLOSED))),
86 DECLARE_NAPI_STATIC_PROPERTY("PINCH_OPENED",
87 GetNapiInt32(env, static_cast<int32_t>(TouchGesturAction::PINCH_OPENED))),
88 DECLARE_NAPI_STATIC_PROPERTY("GESTURE_END",
89 GetNapiInt32(env, static_cast<int32_t>(TouchGesturAction::GESTURE_END))),
90 };
91 napi_value gestureActionType = nullptr;
92 CHKRP(napi_define_class(env, "TouchGestureAction", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
93 sizeof(gestureActionArr) / sizeof(*gestureActionArr), gestureActionArr, &gestureActionType), DEFINE_CLASS);
94 CHKRP(napi_set_named_property(env, exports, "TouchGestureAction", gestureActionType), SET_NAMED_PROPERTY);
95 return exports;
96 }
97 } // namespace MMI
98 } // namespace OHOS
99