1 /* 2 * Copyright (c) 2023 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 #ifndef INPUTMETHOD_IMF_JSPANEL_H 17 #define INPUTMETHOD_IMF_JSPANEL_H 18 19 #include <mutex> 20 #include <uv.h> 21 22 #include "async_call.h" 23 #include "input_method_panel.h" 24 #include "js_callback_object.h" 25 #include "napi/native_api.h" 26 #include "napi/native_common.h" 27 #include "napi/native_node_api.h" 28 #include "native_engine/native_engine.h" 29 #include "native_engine/native_value.h" 30 31 namespace OHOS { 32 namespace MiscServices { 33 class JsPanel { 34 public: 35 JsPanel() = default; 36 ~JsPanel(); 37 static napi_value Init(napi_env env); 38 static napi_value SetUiContent(napi_env env, napi_callback_info info); 39 static napi_value Resize(napi_env env, napi_callback_info info); 40 static napi_value MoveTo(napi_env env, napi_callback_info info); 41 static napi_value Show(napi_env env, napi_callback_info info); 42 static napi_value Hide(napi_env env, napi_callback_info info); 43 static napi_value ChangeFlag(napi_env env, napi_callback_info info); 44 static napi_value Subscribe(napi_env env, napi_callback_info info); 45 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 46 void SetNative(const std::shared_ptr<InputMethodPanel> &panel); 47 std::shared_ptr<InputMethodPanel> GetNative(); 48 49 private: 50 struct PanelContentContext : public AsyncCall::Context { 51 std::string path = ""; 52 uint32_t width = 0; 53 uint32_t height = 0; 54 int32_t x = 0; 55 int32_t y = 0; 56 std::shared_ptr<InputMethodPanel> inputMethodPanel = nullptr; 57 std::shared_ptr<NativeReference> contentStorage = nullptr; PanelContentContextPanelContentContext58 PanelContentContext(napi_env env, napi_callback_info info) : Context(nullptr, nullptr) 59 { 60 napi_value self = nullptr; 61 napi_status status = napi_get_cb_info(env, info, 0, nullptr, &self, nullptr); 62 CHECK_RETURN_VOID((status == napi_ok) && (self != nullptr), "get callback info failed."); 63 void *native = nullptr; 64 status = napi_unwrap(env, self, &native); 65 CHECK_RETURN_VOID((status == napi_ok) && (native != nullptr), "get jsPanel failed."); 66 inputMethodPanel = reinterpret_cast<JsPanel *>(native)->GetNative(); 67 }; PanelContentContextPanelContentContext68 PanelContentContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; operatorPanelContentContext69 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 70 { 71 CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg); 72 return Context::operator()(env, argc, argv, self); 73 } operatorPanelContentContext74 napi_status operator()(napi_env env, napi_value *result) override 75 { 76 if (status_ != napi_ok) { 77 output_ = nullptr; 78 return status_; 79 } 80 return Context::operator()(env, result); 81 } 82 }; 83 static napi_value JsNew(napi_env env, napi_callback_info info); 84 static std::shared_ptr<InputMethodPanel> UnwrapPanel(napi_env env, napi_value thisVar); 85 static const std::string CLASS_NAME; 86 static constexpr size_t ARGC_MAX = 6; 87 std::shared_ptr<InputMethodPanel> inputMethodPanel_ = nullptr; 88 89 static std::mutex panelConstructorMutex_; 90 static thread_local napi_ref panelConstructorRef_; 91 }; 92 } // namespace MiscServices 93 } // namespace OHOS 94 95 #endif //INPUTMETHOD_IMF_JSPANEL_H 96