• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
23 namespace MMI {
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsKeyEvent" };
26 
27 enum class Action : int32_t {
28     CANCEL = 0,
29     DOWN = 1,
30     UP = 2,
31 };
32 } // namespace
33 
GetNapiInt32(napi_env env,int32_t code)34 napi_value JsKeyEvent::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 JsKeyEvent::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 JsKeyEvent::Export(napi_env env, napi_value exports)
54 {
55     CALL_DEBUG_ENTER;
56     napi_property_descriptor desc[] = {
57         DECLARE_NAPI_STATIC_PROPERTY("CANCEL", GetNapiInt32(env, static_cast<int32_t>(Action::CANCEL))),
58         DECLARE_NAPI_STATIC_PROPERTY("DOWN", GetNapiInt32(env, static_cast<int32_t>(Action::DOWN))),
59         DECLARE_NAPI_STATIC_PROPERTY("UP", GetNapiInt32(env, static_cast<int32_t>(Action::UP))),
60     };
61 
62     napi_value action = nullptr;
63     CHKRP(napi_define_class(env, "Action", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
64         sizeof(desc) / sizeof(*desc), desc, &action), DEFINE_CLASS);
65     CHKRP(napi_set_named_property(env, exports, "Action", action), SET_NAMED_PROPERTY);
66     return exports;
67 }
68 } // namespace MMI
69 } // namespace OHOS