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 "ffrt_block_queue.h" 24 #include "input_method_panel.h" 25 #include "js_callback_object.h" 26 #include "napi/native_api.h" 27 #include "napi/native_common.h" 28 #include "napi/native_node_api.h" 29 #include "native_engine/native_engine.h" 30 #include "native_engine/native_value.h" 31 32 namespace OHOS { 33 namespace MiscServices { 34 35 enum class JsEvent : uint32_t { 36 RESIZE = 0, 37 MOVE_TO, 38 CHANGE_FLAG, 39 ADJUST_PANEL_RECT, 40 EVENT_END, 41 }; 42 43 struct JsEventInfo { 44 std::chrono::system_clock::time_point timestamp{}; 45 JsEvent event{ JsEvent::EVENT_END }; 46 bool operator==(const JsEventInfo &info) const 47 { 48 return (timestamp == info.timestamp && event == info.event); 49 } 50 }; 51 52 struct JsPanelRect { 53 static napi_value Write(napi_env env, const LayoutParams &layoutParams); 54 static bool Read(napi_env env, napi_value object, LayoutParams &layoutParams); 55 }; 56 57 class JsPanel { 58 public: 59 JsPanel() = default; 60 ~JsPanel(); 61 static napi_value Init(napi_env env); 62 static napi_value SetUiContent(napi_env env, napi_callback_info info); 63 static napi_value Resize(napi_env env, napi_callback_info info); 64 static napi_value MoveTo(napi_env env, napi_callback_info info); 65 static napi_value Show(napi_env env, napi_callback_info info); 66 static napi_value Hide(napi_env env, napi_callback_info info); 67 static napi_value ChangeFlag(napi_env env, napi_callback_info info); 68 static napi_value SetPrivacyMode(napi_env env, napi_callback_info info); 69 static napi_value Subscribe(napi_env env, napi_callback_info info); 70 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 71 static napi_value AdjustPanelRect(napi_env env, napi_callback_info info); 72 void SetNative(const std::shared_ptr<InputMethodPanel> &panel); 73 std::shared_ptr<InputMethodPanel> GetNative(); 74 75 private: 76 77 struct PanelContentContext : public AsyncCall::Context { 78 LayoutParams layoutParams = { 79 {0, 0, 0, 0}, 80 {0, 0, 0, 0} 81 }; 82 PanelFlag panelFlag = PanelFlag::FLG_FIXED; 83 std::string path = ""; 84 uint32_t width = 0; 85 uint32_t height = 0; 86 int32_t x = 0; 87 int32_t y = 0; 88 std::shared_ptr<InputMethodPanel> inputMethodPanel = nullptr; 89 std::shared_ptr<NativeReference> contentStorage = nullptr; 90 JsEventInfo info; PanelContentContextPanelContentContext91 PanelContentContext(napi_env env, napi_callback_info info) : Context(nullptr, nullptr) 92 { 93 napi_value self = nullptr; 94 napi_status status = napi_get_cb_info(env, info, 0, nullptr, &self, nullptr); 95 CHECK_RETURN_VOID((status == napi_ok) && (self != nullptr), "get callback info failed."); 96 void *native = nullptr; 97 status = napi_unwrap(env, self, &native); 98 CHECK_RETURN_VOID((status == napi_ok) && (native != nullptr), "get jsPanel failed."); 99 inputMethodPanel = reinterpret_cast<JsPanel *>(native)->GetNative(); 100 }; PanelContentContextPanelContentContext101 PanelContentContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; operatorPanelContentContext102 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 103 { 104 CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg); 105 return Context::operator()(env, argc, argv, self); 106 } operatorPanelContentContext107 napi_status operator()(napi_env env, napi_value *result) override 108 { 109 if (status_ != napi_ok) { 110 output_ = nullptr; 111 return status_; 112 } 113 return Context::operator()(env, result); 114 } 115 }; 116 static napi_value JsNew(napi_env env, napi_callback_info info); 117 static std::shared_ptr<InputMethodPanel> UnwrapPanel(napi_env env, napi_value thisVar); 118 static void PrintEditorQueueInfoIfTimeout(int64_t start, const JsEventInfo ¤tInfo); 119 static napi_status CheckParam(napi_env env, size_t argc, napi_value *argv, 120 std::shared_ptr<PanelContentContext> ctxt); 121 static const std::string CLASS_NAME; 122 static constexpr size_t ARGC_MAX = 6; 123 std::shared_ptr<InputMethodPanel> inputMethodPanel_ = nullptr; 124 125 static std::mutex panelConstructorMutex_; 126 static thread_local napi_ref panelConstructorRef_; 127 128 static FFRTBlockQueue<JsEventInfo> jsQueue_; 129 }; 130 } // namespace MiscServices 131 } // namespace OHOS 132 133 #endif //INPUTMETHOD_IMF_JSPANEL_H 134