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 #ifndef INTERFACE_KITS_JS_KEYBOARD_CONTROLLER_H
16 #define INTERFACE_KITS_JS_KEYBOARD_CONTROLLER_H
17 
18 #include "async_call.h"
19 #include "global.h"
20 
21 namespace OHOS {
22 namespace MiscServices {
23 struct HideKeyboardContext : public AsyncCall::Context {
24     napi_status status = napi_generic_failure;
HideKeyboardContextHideKeyboardContext25     HideKeyboardContext() : Context(nullptr, nullptr){};
HideKeyboardContextHideKeyboardContext26     HideKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
27 
operatorHideKeyboardContext28     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
29     {
30         CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg);
31         return Context::operator()(env, argc, argv, self);
32     }
operatorHideKeyboardContext33     napi_status operator()(napi_env env, napi_value *result) override
34     {
35         if (status != napi_ok) {
36             output_ = nullptr;
37             return status;
38         }
39         return Context::operator()(env, result);
40     }
41 };
42 
43 struct HideContext : public AsyncCall::Context {
44     napi_status status = napi_generic_failure;
HideContextHideContext45     HideContext() : Context(nullptr, nullptr){};
HideContextHideContext46     HideContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
47 
operatorHideContext48     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
49     {
50         CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg);
51         return Context::operator()(env, argc, argv, self);
52     }
operatorHideContext53     napi_status operator()(napi_env env, napi_value *result) override
54     {
55         if (status != napi_ok) {
56             output_ = nullptr;
57             return status;
58         }
59         return Context::operator()(env, result);
60     }
61 };
62 
63 struct ExitContext : public AsyncCall::Context {
64     napi_status status = napi_generic_failure;
ExitContextExitContext65     ExitContext() : Context(nullptr, nullptr){};
66 
operatorExitContext67     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
68     {
69         CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg);
70         return Context::operator()(env, argc, argv, self);
71     }
operatorExitContext72     napi_status operator()(napi_env env, napi_value *result) override
73     {
74         if (status != napi_ok) {
75             output_ = nullptr;
76             return status;
77         }
78         return Context::operator()(env, result);
79     }
80 };
81 
82 class JsKeyboardControllerEngine {
83 public:
84     JsKeyboardControllerEngine() = default;
85     ~JsKeyboardControllerEngine() = default;
86     static napi_value Init(napi_env env, napi_value info);
87     static napi_value GetKeyboardControllerInstance(napi_env env);
88     static napi_value HideKeyboard(napi_env env, napi_callback_info info);
89     static napi_value Hide(napi_env env, napi_callback_info info);
90     static napi_value ExitCurrentInputType(napi_env env, napi_callback_info info);
91 
92 private:
93     static napi_value JsConstructor(napi_env env, napi_callback_info info);
94     static const std::string KCE_CLASS_NAME;
95     static thread_local napi_ref KCERef_;
96 };
97 } // namespace MiscServices
98 } // namespace OHOS
99 #endif // INTERFACE_KITS_JS_KEYBOARD_CONTROLLER_H