1 /*
2  * Copyright (c) 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 "js_key_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 "JsKeyEvent"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 enum class Action : int32_t {
29     CANCEL = 0,
30     DOWN = 1,
31     UP = 2,
32 };
33 } // namespace
34 
GetNapiInt32(napi_env env,int32_t code)35 napi_value JsKeyEvent::GetNapiInt32(napi_env env, int32_t code)
36 {
37     CALL_DEBUG_ENTER;
38     napi_value ret = nullptr;
39     CHKRP(napi_create_int32(env, code, &ret), CREATE_INT32);
40     return ret;
41 }
42 
EnumClassConstructor(napi_env env,napi_callback_info info)43 napi_value JsKeyEvent::EnumClassConstructor(napi_env env, napi_callback_info info)
44 {
45     CALL_DEBUG_ENTER;
46     size_t argc = 0;
47     napi_value args[1] = { 0 };
48     napi_value ret = nullptr;
49     void *data = nullptr;
50     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
51     return ret;
52 }
53 
Export(napi_env env,napi_value exports)54 napi_value JsKeyEvent::Export(napi_env env, napi_value exports)
55 {
56     CALL_DEBUG_ENTER;
57     napi_property_descriptor desc[] = {
58         DECLARE_NAPI_STATIC_PROPERTY("CANCEL", GetNapiInt32(env, static_cast<int32_t>(Action::CANCEL))),
59         DECLARE_NAPI_STATIC_PROPERTY("DOWN", GetNapiInt32(env, static_cast<int32_t>(Action::DOWN))),
60         DECLARE_NAPI_STATIC_PROPERTY("UP", GetNapiInt32(env, static_cast<int32_t>(Action::UP))),
61     };
62 
63     napi_value action = nullptr;
64     CHKRP(napi_define_class(env, "Action", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
65         sizeof(desc) / sizeof(*desc), desc, &action), DEFINE_CLASS);
66     CHKRP(napi_set_named_property(env, exports, "Action", action), SET_NAMED_PROPERTY);
67     return exports;
68 }
69 } // namespace MMI
70 } // namespace OHOS