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 
EnumClassConstructor(napi_env env,napi_callback_info info)42 napi_value JsGestureEvent::EnumClassConstructor(napi_env env, napi_callback_info info)
43 {
44     CALL_DEBUG_ENTER;
45     size_t argc = 0;
46     napi_value args[1] = { 0 };
47     napi_value ret = nullptr;
48     void *data = nullptr;
49     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
50     return ret;
51 }
52 
Export(napi_env env,napi_value exports)53 napi_value JsGestureEvent::Export(napi_env env, napi_value exports)
54 {
55     CALL_INFO_TRACE;
56     napi_property_descriptor actionArr[] = {
57         DECLARE_NAPI_STATIC_PROPERTY("CANCEL", GetNapiInt32(env, static_cast<int32_t>(ActionType::CANCEL))),
58         DECLARE_NAPI_STATIC_PROPERTY("BEGIN", GetNapiInt32(env, static_cast<int32_t>(ActionType::BEGIN))),
59         DECLARE_NAPI_STATIC_PROPERTY("UPDATE", GetNapiInt32(env, static_cast<int32_t>(ActionType::UPDATE))),
60         DECLARE_NAPI_STATIC_PROPERTY("END", GetNapiInt32(env, static_cast<int32_t>(ActionType::END))),
61     };
62     napi_value actionType = nullptr;
63     CHKRP(napi_define_class(env, "ActionType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
64         sizeof(actionArr) / sizeof(*actionArr), actionArr, &actionType), DEFINE_CLASS);
65     CHKRP(napi_set_named_property(env, exports, "ActionType", actionType), SET_NAMED_PROPERTY);
66     return exports;
67 }
68 } // namespace MMI
69 } // namespace OHOS