• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_keyboard_controller_engine.h"
17 
18 #include "input_method_ability.h"
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
24 thread_local napi_ref JsKeyboardControllerEngine::KCERef_ = nullptr;
25 const std::string JsKeyboardControllerEngine::KCE_CLASS_NAME = "KeyboardController";
Init(napi_env env,napi_value info)26 napi_value JsKeyboardControllerEngine::Init(napi_env env, napi_value info)
27 {
28     napi_property_descriptor properties[] = {
29         DECLARE_NAPI_FUNCTION("hideKeyboard", HideKeyboard),
30         DECLARE_NAPI_FUNCTION("hide", Hide),
31     };
32     napi_value cons = nullptr;
33     NAPI_CALL(env, napi_define_class(env, KCE_CLASS_NAME.c_str(), KCE_CLASS_NAME.size(), JsConstructor, nullptr,
34                        sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
35     NAPI_CALL(env, napi_create_reference(env, cons, 1, &KCERef_));
36     NAPI_CALL(env, napi_set_named_property(env, info, KCE_CLASS_NAME.c_str(), cons));
37 
38     return info;
39 }
40 
JsConstructor(napi_env env,napi_callback_info info)41 napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback_info info)
42 {
43     napi_value thisVar = nullptr;
44     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
45 
46     JsKeyboardControllerEngine *controllerObject = new (std::nothrow) JsKeyboardControllerEngine();
47     if (controllerObject == nullptr) {
48         IMSA_HILOGE("controllerObject is nullptr");
49         napi_value result = nullptr;
50         napi_get_null(env, &result);
51         return result;
52     }
53     auto finalize = [](napi_env env, void *data, void *hint) {
54         IMSA_HILOGE("JsKeyboardControllerEngine finalize");
55         auto *objInfo = reinterpret_cast<JsKeyboardControllerEngine *>(data);
56         if (objInfo != nullptr) {
57             delete objInfo;
58         }
59     };
60     napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr);
61     if (status != napi_ok) {
62         IMSA_HILOGE("JsKeyboardControllerEngine napi_wrap failed: %{public}d", status);
63         delete controllerObject;
64         return nullptr;
65     }
66 
67     return thisVar;
68 }
69 
GetKeyboardControllerInstance(napi_env env)70 napi_value JsKeyboardControllerEngine::GetKeyboardControllerInstance(napi_env env)
71 {
72     napi_value instance = nullptr;
73     napi_value cons = nullptr;
74     if (napi_get_reference_value(env, KCERef_, &cons) != napi_ok) {
75         IMSA_HILOGE("GetKeyboardController::napi_get_reference_value not ok");
76         return nullptr;
77     }
78     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
79         IMSA_HILOGE("GetKeyboardController::napi_new_instance not ok");
80         return nullptr;
81     }
82     IMSA_HILOGE("New the JsKeyboardControllerEngine instance complete");
83     return instance;
84 }
85 
Hide(napi_env env,napi_callback_info info)86 napi_value JsKeyboardControllerEngine::Hide(napi_env env, napi_callback_info info)
87 {
88     auto ctxt = std::make_shared<HideContext>();
89     auto input = [ctxt](
90                      napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { return napi_ok; };
91     auto exec = [ctxt](AsyncCall::Context *ctx) {
92         int32_t code = InputMethodAbility::GetInstance()->HideKeyboardSelf();
93         if (code == ErrorCode::NO_ERROR) {
94             ctxt->status = napi_ok;
95             ctxt->SetState(ctxt->status);
96         } else {
97             ctxt->SetErrorCode(code);
98         }
99     };
100     ctxt->SetAction(std::move(input));
101     // 1 means JsAPI:hide has 1 params at most.
102     AsyncCall asyncCall(env, info, ctxt, 1);
103     return asyncCall.Call(env, exec, "keyboard.hide");
104 }
105 
HideKeyboard(napi_env env,napi_callback_info info)106 napi_value JsKeyboardControllerEngine::HideKeyboard(napi_env env, napi_callback_info info)
107 {
108     auto ctxt = std::make_shared<HideKeyboardContext>();
109     auto input = [ctxt](
110                      napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { return napi_ok; };
111     auto exec = [ctxt](AsyncCall::Context *ctx) {
112         InputMethodAbility::GetInstance()->HideKeyboardSelf();
113         ctxt->status = napi_ok;
114     };
115     ctxt->SetAction(std::move(input));
116     // 1 means JsAPI:hideKeyboard has 1 params at most.
117     AsyncCall asyncCall(env, info, ctxt, 1);
118     return asyncCall.Call(env, exec, "hideKeyboard");
119 }
120 } // namespace MiscServices
121 } // namespace OHOS