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 ADJUST_PANEL_RECT, 39 UPDATE_REGION, 40 SHOW, 41 HIDE, 42 SET_UI_CONTENT, 43 GET_DISPLAYID, 44 SET_IMMERSIVE_MODE, 45 GET_IMMERSIVE_MODE, 46 SET_IMMERSIVE_EFFECT, 47 EVENT_END, 48 }; 49 50 struct JsEventInfo { 51 std::chrono::system_clock::time_point timestamp{}; 52 JsEvent event{ JsEvent::EVENT_END }; 53 bool operator==(const JsEventInfo &info) const 54 { 55 return (timestamp == info.timestamp && event == info.event); 56 } 57 }; 58 59 struct JsPanelRect { 60 static napi_value Write(napi_env env, const LayoutParams &layoutParams); 61 static bool Read(napi_env env, napi_value object, LayoutParams &layoutParams); 62 }; 63 64 struct JsEnhancedPanelRect { 65 static bool Read(napi_env env, napi_value object, EnhancedLayoutParams &layoutParams); 66 }; 67 68 struct JsHotArea { 69 static bool Read(napi_env env, napi_value object, std::vector<Rosen::Rect> &hotAreas); 70 }; 71 72 struct JsImmersiveEffect { 73 static bool Read(napi_env env, napi_value object, ImmersiveEffect &effect); 74 }; 75 76 class JsPanel { 77 public: 78 JsPanel() = default; 79 ~JsPanel(); 80 static napi_value Init(napi_env env); 81 static napi_value SetUiContent(napi_env env, napi_callback_info info); 82 static napi_value Resize(napi_env env, napi_callback_info info); 83 static napi_value MoveTo(napi_env env, napi_callback_info info); 84 static napi_value Show(napi_env env, napi_callback_info info); 85 static napi_value Hide(napi_env env, napi_callback_info info); 86 static napi_value ChangeFlag(napi_env env, napi_callback_info info); 87 static napi_value SetPrivacyMode(napi_env env, napi_callback_info info); 88 static napi_value Subscribe(napi_env env, napi_callback_info info); 89 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 90 static napi_value AdjustPanelRect(napi_env env, napi_callback_info info); 91 static napi_value UpdateRegion(napi_env env, napi_callback_info info); 92 static napi_value StartMoving(napi_env env, napi_callback_info info); 93 static napi_value GetDisplayId(napi_env env, napi_callback_info info); 94 void SetNative(const std::shared_ptr<InputMethodPanel> &panel); 95 std::shared_ptr<InputMethodPanel> GetNative(); 96 static napi_value SetImmersiveMode(napi_env env, napi_callback_info info); 97 static napi_value GetImmersiveMode(napi_env env, napi_callback_info info); 98 static napi_value SetImmersiveEffect(napi_env env, napi_callback_info info); 99 static napi_value SetKeepScreenOn(napi_env env, napi_callback_info info); 100 101 private: 102 struct PanelContentContext : public AsyncCall::Context { 103 LayoutParams layoutParams = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }; 104 EnhancedLayoutParams enhancedLayoutParams; 105 HotAreas hotAreas; 106 std::vector<Rosen::Rect> hotArea; 107 bool isEnhancedCall{ false }; 108 PanelFlag panelFlag = PanelFlag::FLG_FIXED; 109 std::string path = ""; 110 uint32_t width = 0; 111 uint32_t height = 0; 112 int32_t x = 0; 113 int32_t y = 0; 114 uint64_t displayId = 0; 115 bool isKeepScreenOn = false; 116 std::shared_ptr<InputMethodPanel> inputMethodPanel = nullptr; 117 std::shared_ptr<NativeReference> contentStorage = nullptr; 118 JsEventInfo info; PanelContentContextPanelContentContext119 PanelContentContext(napi_env env, napi_callback_info info) : Context(nullptr, nullptr) 120 { 121 napi_value self = nullptr; 122 napi_status status = napi_get_cb_info(env, info, 0, nullptr, &self, nullptr); 123 CHECK_RETURN_VOID((status == napi_ok) && (self != nullptr), "get callback info failed."); 124 void *native = nullptr; 125 status = napi_unwrap(env, self, &native); 126 CHECK_RETURN_VOID((status == napi_ok) && (native != nullptr), "get jsPanel failed."); 127 inputMethodPanel = reinterpret_cast<JsPanel *>(native)->GetNative(); 128 }; PanelContentContextPanelContentContext129 PanelContentContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; operatorPanelContentContext130 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 131 { 132 CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg); 133 return Context::operator()(env, argc, argv, self); 134 } operatorPanelContentContext135 napi_status operator()(napi_env env, napi_value *result) override 136 { 137 if (status_ != napi_ok) { 138 output_ = nullptr; 139 return status_; 140 } 141 return Context::operator()(env, result); 142 } 143 }; 144 static napi_value JsNew(napi_env env, napi_callback_info info); 145 static std::shared_ptr<InputMethodPanel> UnwrapPanel(napi_env env, napi_value thisVar); 146 static void PrintEditorQueueInfoIfTimeout(int64_t start, const JsEventInfo ¤tInfo); 147 148 static bool IsEnhancedAdjust(napi_env env, napi_value *argv); 149 static bool IsPanelFlagValid(napi_env env, PanelFlag panelFlag, bool isEnhancedCalled); 150 static napi_status ParsePanelFlag(napi_env env, napi_value *argv, PanelFlag &panelFlag, bool isEnhancedCalled); 151 static napi_status CheckParam( 152 napi_env env, size_t argc, napi_value *argv, std::shared_ptr<PanelContentContext> ctxt); 153 static napi_status CheckEnhancedParam( 154 napi_env env, size_t argc, napi_value *argv, std::shared_ptr<PanelContentContext> ctxt); 155 static void AdjustLayoutParam(std::shared_ptr<PanelContentContext> ctxt); 156 static void AdjustEnhancedLayoutParam(std::shared_ptr<PanelContentContext> ctxt); 157 158 static const std::string CLASS_NAME; 159 static constexpr size_t ARGC_MAX = 6; 160 std::shared_ptr<InputMethodPanel> inputMethodPanel_ = nullptr; 161 162 static std::mutex panelConstructorMutex_; 163 static thread_local napi_ref panelConstructorRef_; 164 165 static FFRTBlockQueue<JsEventInfo> jsQueue_; 166 }; 167 } // namespace MiscServices 168 } // namespace OHOS 169 170 #endif //INPUTMETHOD_IMF_JSPANEL_H 171