• 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 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("failed to get reference value!");
77         return nullptr;
78     }
79     if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
80         IMSA_HILOGE("failed to new instance value!");
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](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
91         return napi_ok;
92     };
93     auto exec = [ctxt](AsyncCall::Context *ctx) {
94         int32_t code = InputMethodAbility::GetInstance().HideKeyboardSelf();
95         if (code == ErrorCode::NO_ERROR) {
96             ctxt->status = napi_ok;
97             ctxt->SetState(ctxt->status);
98         } else {
99             ctxt->SetErrorCode(code);
100         }
101     };
102     ctxt->SetAction(std::move(input));
103     // 1 means JsAPI:hide has 1 params at most.
104     AsyncCall asyncCall(env, info, ctxt, 1);
105     return asyncCall.Call(env, exec, "keyboard.hide");
106 }
107 
HideKeyboard(napi_env env,napi_callback_info info)108 napi_value JsKeyboardControllerEngine::HideKeyboard(napi_env env, napi_callback_info info)
109 {
110     auto ctxt = std::make_shared<HideKeyboardContext>();
111     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
112         return napi_ok;
113     };
114     auto exec = [ctxt](AsyncCall::Context *ctx) {
115         InputMethodAbility::GetInstance().HideKeyboardSelf();
116         ctxt->status = napi_ok;
117     };
118     ctxt->SetAction(std::move(input));
119     // 1 means JsAPI:hideKeyboard has 1 params at most.
120     AsyncCall asyncCall(env, info, ctxt, 1);
121     return asyncCall.Call(env, exec, "hideKeyboard");
122 }
123 
ExitCurrentInputType(napi_env env,napi_callback_info info)124 napi_value JsKeyboardControllerEngine::ExitCurrentInputType(napi_env env, napi_callback_info info)
125 {
126     auto ctxt = std::make_shared<ExitContext>();
127     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
128         return napi_ok;
129     };
130     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { return napi_ok; };
131     auto exec = [ctxt](AsyncCall::Context *ctx) {
132         int32_t errorCode = InputMethodAbility::GetInstance().ExitCurrentInputType();
133         if (errorCode == ErrorCode::NO_ERROR) {
134             ctxt->status = napi_ok;
135             ctxt->SetState(napi_ok);
136         } else {
137             ctxt->SetErrorCode(errorCode);
138         }
139     };
140     ctxt->SetAction(std::move(input), std::move(output));
141     AsyncCall asyncCall(env, info, ctxt, 1);
142     return asyncCall.Call(env, exec, "exitCurrentInputType");
143 }
144 } // namespace MiscServices
145 } // namespace OHOS