1 /* 2 * Copyright (c) 2021 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_INPUT_METHOD_H 16 #define INTERFACE_KITS_JS_INPUT_METHOD_H 17 18 #include "async_call.h" 19 #include "global.h" 20 #include "input_method_controller.h" 21 #include "native_engine/native_engine.h" 22 #include "native_engine/native_value.h" 23 24 namespace OHOS { 25 namespace MiscServices { 26 struct SwitchInputMethodContext : public AsyncCall::Context { 27 bool isSwitchInput = false; 28 std::string packageName; // in InputMethodProperty 29 std::string methodId; // in InputMethodProperty 30 std::string name; // in InputMethodSubtype 31 std::string id; // in InputMethodSubtype 32 napi_status status = napi_generic_failure; SwitchInputMethodContextSwitchInputMethodContext33 SwitchInputMethodContext() : Context(nullptr, nullptr){}; SwitchInputMethodContextSwitchInputMethodContext34 SwitchInputMethodContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 35 operatorSwitchInputMethodContext36 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 37 { 38 CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg); 39 return Context::operator()(env, argc, argv, self); 40 } operatorSwitchInputMethodContext41 napi_status operator()(napi_env env, napi_value *result) override 42 { 43 if (status != napi_ok) { 44 output_ = nullptr; 45 return status; 46 } 47 return Context::operator()(env, result); 48 } 49 }; 50 51 class JsInputMethod { 52 public: 53 JsInputMethod() = default; 54 ~JsInputMethod() = default; 55 static napi_value Init(napi_env env, napi_value exports); 56 static napi_value SwitchInputMethod(napi_env env, napi_callback_info info); 57 static napi_value SwitchCurrentInputMethodSubtype(napi_env env, napi_callback_info info); 58 static napi_value SwitchCurrentInputMethodAndSubtype(napi_env env, napi_callback_info info); 59 static napi_value GetCurrentInputMethodSubtype(napi_env env, napi_callback_info info); 60 static napi_value GetCurrentInputMethod(napi_env env, napi_callback_info info); 61 static napi_value GetJsInputMethodProperty(napi_env env, const Property &property); 62 static napi_value GetJSInputMethodSubProperties(napi_env env, const std::vector<SubProperty> &subProperties); 63 static napi_value GetJSInputMethodProperties(napi_env env, const std::vector<Property> &properties); 64 static napi_value GetJsInputMethodSubProperty(napi_env env, const SubProperty &subProperty); 65 66 private: 67 static napi_status GetInputMethodProperty( 68 napi_env env, napi_value argv, std::shared_ptr<SwitchInputMethodContext> ctxt); 69 static napi_status GetInputMethodSubProperty( 70 napi_env env, napi_value argv, std::shared_ptr<SwitchInputMethodContext> ctxt); 71 static constexpr std::int32_t MAX_VALUE_LEN = 4096; 72 static constexpr size_t PARAM_POS_TWO = 2; 73 static constexpr size_t PARAM_POS_ONE = 1; 74 }; 75 } // namespace MiscServices 76 } // namespace OHOS 77 #endif // INTERFACE_KITS_JS_INPUT_METHOD_H 78