• 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         DECLARE_NAPI_FUNCTION("exitCurrentInputType", ExitCurrentInputType),
32     };
33     napi_value cons = nullptr;
34     NAPI_CALL(env, napi_define_class(env, KCE_CLASS_NAME.c_str(), KCE_CLASS_NAME.size(), JsConstructor, nullptr,
35                        sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
36     NAPI_CALL(env, napi_create_reference(env, cons, 1, &KCERef_));
37     NAPI_CALL(env, napi_set_named_property(env, info, KCE_CLASS_NAME.c_str(), cons));
38 
39     return info;
40 }
41 
JsConstructor(napi_env env,napi_callback_info info)42 napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback_info info)
43 {
44     napi_value thisVar = nullptr;
45     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
46 
47     JsKeyboardControllerEngine *controllerObject = new (std::nothrow) JsKeyboardControllerEngine();
48     if (controllerObject == nullptr) {
49         IMSA_HILOGE("controllerObject is nullptr");
50         napi_value result = nullptr;
51         napi_get_null(env, &result);
52         return result;
53     }
54     auto finalize = [](napi_env env, void *data, void *hint) {
55         IMSA_HILOGD("JsKBCEngine finalize");
56         auto *objInfo = reinterpret_cast<JsKeyboardControllerEngine *>(data);
57         if (objInfo != nullptr) {
58             delete objInfo;
59         }
60     };
61     napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr);
62     if (status != napi_ok) {
63         IMSA_HILOGE("JsKeyboardControllerEngine napi_wrap failed: %{public}d", status);
64         delete controllerObject;
65         return nullptr;
66     }
67 
68     return thisVar;
69 }
70 
GetKeyboardControllerInstance(napi_env env)71 napi_value JsKeyboardControllerEngine::GetKeyboardControllerInstance(napi_env env)
72 {
73     napi_value instance = nullptr;
74     napi_value cons = nullptr;
75     if (napi_get_reference_value(env, KCERef_, &cons) != napi_ok) {
76         IMSA_HILOGE("GetKeyboardController::napi_get_reference_value not ok");
77         return nullptr;
78     }
79     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
80         IMSA_HILOGE("GetKeyboardController::napi_new_instance not ok");
81         return nullptr;
82     }
83     IMSA_HILOGD("success");
84     return instance;
85 }
86 
Hide(napi_env env,napi_callback_info info)87 napi_value JsKeyboardControllerEngine::Hide(napi_env env, napi_callback_info info)
88 {
89     auto ctxt = std::make_shared<HideContext>();
90     auto input = [ctxt](
91                      napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { return napi_ok; };
92     auto exec = [ctxt](AsyncCall::Context *ctx) {
93         int32_t code = InputMethodAbility::GetInstance()->HideKeyboardSelf();
94         if (code == ErrorCode::NO_ERROR) {
95             ctxt->status = napi_ok;
96             ctxt->SetState(ctxt->status);
97         } else {
98             ctxt->SetErrorCode(code);
99         }
100     };
101     ctxt->SetAction(std::move(input));
102     // 1 means JsAPI:hide has 1 params at most.
103     AsyncCall asyncCall(env, info, ctxt, 1);
104     return asyncCall.Call(env, exec, "keyboard.hide");
105 }
106 
HideKeyboard(napi_env env,napi_callback_info info)107 napi_value JsKeyboardControllerEngine::HideKeyboard(napi_env env, napi_callback_info info)
108 {
109     auto ctxt = std::make_shared<HideKeyboardContext>();
110     auto input = [ctxt](
111                      napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { return napi_ok; };
112     auto exec = [ctxt](AsyncCall::Context *ctx) {
113         InputMethodAbility::GetInstance()->HideKeyboardSelf();
114         ctxt->status = napi_ok;
115     };
116     ctxt->SetAction(std::move(input));
117     // 1 means JsAPI:hideKeyboard has 1 params at most.
118     AsyncCall asyncCall(env, info, ctxt, 1);
119     return asyncCall.Call(env, exec, "hideKeyboard");
120 }
121 
ExitCurrentInputType(napi_env env,napi_callback_info info)122 napi_value JsKeyboardControllerEngine::ExitCurrentInputType(napi_env env, napi_callback_info info)
123 {
124     auto ctxt = std::make_shared<ExitContext>();
125     auto input = [ctxt](
126                      napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { return napi_ok; };
127     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { return napi_ok; };
128     auto exec = [ctxt](AsyncCall::Context *ctx) {
129         int32_t errorCode = InputMethodAbility::GetInstance()->ExitCurrentInputType();
130         if (errorCode == ErrorCode::NO_ERROR) {
131             ctxt->status = napi_ok;
132             ctxt->SetState(napi_ok);
133         } else {
134             ctxt->SetErrorCode(errorCode);
135         }
136     };
137     ctxt->SetAction(std::move(input), std::move(output));
138     AsyncCall asyncCall(env, info, ctxt, 1);
139     return asyncCall.Call(env, exec, "exitCurrentInputType");
140 }
141 } // namespace MiscServices
142 } // namespace OHOS