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 #include "js_panel.h"
17
18 #include "event_checker.h"
19 #include "input_method_ability.h"
20 #include "inputmethod_trace.h"
21 #include "js_text_input_client_engine.h"
22 #include "js_util.h"
23 #include "js_utils.h"
24 #include "napi/native_common.h"
25 #include "panel_listener_impl.h"
26
27 namespace OHOS {
28 namespace MiscServices {
29 using namespace std::chrono;
30 using WMError = OHOS::Rosen::WMError;
31 const std::string JsPanel::CLASS_NAME = "Panel";
32 thread_local napi_ref JsPanel::panelConstructorRef_ = nullptr;
33 std::mutex JsPanel::panelConstructorMutex_;
34 constexpr int32_t MAX_WAIT_TIME = 10;
35 FFRTBlockQueue<JsEventInfo> JsPanel::jsQueue_{ MAX_WAIT_TIME };
36
Init(napi_env env)37 napi_value JsPanel::Init(napi_env env)
38 {
39 IMSA_HILOGI("JsPanel start.");
40 napi_value constructor = nullptr;
41 std::lock_guard<std::mutex> lock(panelConstructorMutex_);
42 if (panelConstructorRef_ != nullptr) {
43 napi_status status = napi_get_reference_value(env, panelConstructorRef_, &constructor);
44 CHECK_RETURN(status == napi_ok, "failed to get jsPanel constructor.", nullptr);
45 return constructor;
46 }
47 const napi_property_descriptor properties[] = {
48 DECLARE_NAPI_FUNCTION("setUiContent", SetUiContent),
49 DECLARE_NAPI_FUNCTION("resize", Resize),
50 DECLARE_NAPI_FUNCTION("moveTo", MoveTo),
51 DECLARE_NAPI_FUNCTION("show", Show),
52 DECLARE_NAPI_FUNCTION("hide", Hide),
53 DECLARE_NAPI_FUNCTION("changeFlag", ChangeFlag),
54 DECLARE_NAPI_FUNCTION("setPrivacyMode", SetPrivacyMode),
55 DECLARE_NAPI_FUNCTION("on", Subscribe),
56 DECLARE_NAPI_FUNCTION("off", UnSubscribe),
57 DECLARE_NAPI_FUNCTION("adjustPanelRect", AdjustPanelRect),
58 };
59 NAPI_CALL(env, napi_define_class(env, CLASS_NAME.c_str(), CLASS_NAME.size(), JsNew, nullptr,
60 sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor));
61 CHECK_RETURN(constructor != nullptr, "failed to define class!", nullptr);
62 NAPI_CALL(env, napi_create_reference(env, constructor, 1, &panelConstructorRef_));
63 return constructor;
64 }
65
JsNew(napi_env env,napi_callback_info info)66 napi_value JsPanel::JsNew(napi_env env, napi_callback_info info)
67 {
68 IMSA_HILOGD("create panel instance start.");
69 std::shared_ptr<PanelListenerImpl> panelImpl = PanelListenerImpl::GetInstance();
70 if (panelImpl != nullptr) {
71 IMSA_HILOGD("set eventHandler.");
72 panelImpl->SetEventHandler(AppExecFwk::EventHandler::Current());
73 }
74 JsPanel *panel = new (std::nothrow) JsPanel();
75 CHECK_RETURN(panel != nullptr, "no memory for JsPanel!", nullptr);
76 auto finalize = [](napi_env env, void *data, void *hint) {
77 IMSA_HILOGD("jsPanel finalize.");
78 auto *jsPanel = reinterpret_cast<JsPanel *>(data);
79 CHECK_RETURN_VOID(jsPanel != nullptr, "finalize nullptr!");
80 jsPanel->GetNative() = nullptr;
81 delete jsPanel;
82 };
83 napi_value thisVar = nullptr;
84 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
85 if (status != napi_ok) {
86 IMSA_HILOGE("failed to get cb info: %{public}d!", status);
87 delete panel;
88 return nullptr;
89 }
90 status = napi_wrap(env, thisVar, panel, finalize, nullptr, nullptr);
91 if (status != napi_ok) {
92 IMSA_HILOGE("failed to wrap: %{public}d!", status);
93 delete panel;
94 return nullptr;
95 }
96 return thisVar;
97 }
98
~JsPanel()99 JsPanel::~JsPanel()
100 {
101 inputMethodPanel_ = nullptr;
102 }
103
SetNative(const std::shared_ptr<InputMethodPanel> & panel)104 void JsPanel::SetNative(const std::shared_ptr<InputMethodPanel> &panel)
105 {
106 inputMethodPanel_ = panel;
107 }
108
GetNative()109 std::shared_ptr<InputMethodPanel> JsPanel::GetNative()
110 {
111 return inputMethodPanel_;
112 }
113
SetUiContent(napi_env env,napi_callback_info info)114 napi_value JsPanel::SetUiContent(napi_env env, napi_callback_info info)
115 {
116 IMSA_HILOGI("JsPanel start.");
117 auto ctxt = std::make_shared<PanelContentContext>(env, info);
118 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
119 napi_status status = napi_generic_failure;
120 PARAM_CHECK_RETURN(env, argc >= 1, "at least one parameter is required!", TYPE_NONE, status);
121 // 0 means the first param path<std::string>
122 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[0], ctxt->path) == napi_ok,
123 "js param path covert failed, must be string!", TYPE_NONE, status);
124 // if type of argv[1] is object, we will get value of 'storage' from it.
125 if (argc >= 2) {
126 napi_valuetype valueType = napi_undefined;
127 status = napi_typeof(env, argv[1], &valueType);
128 CHECK_RETURN(status == napi_ok, "get valueType failed!", status);
129 if (valueType == napi_object) {
130 napi_ref storage = nullptr;
131 napi_create_reference(env, argv[1], 1, &storage);
132 auto contentStorage = (storage == nullptr) ? nullptr
133 : std::shared_ptr<NativeReference>(
134 reinterpret_cast<NativeReference *>(storage));
135 ctxt->contentStorage = contentStorage;
136 }
137 }
138 return napi_ok;
139 };
140
141 auto exec = [ctxt](AsyncCall::Context *ctx) {
142 ctxt->SetState(napi_ok);
143 };
144 auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
145 CHECK_RETURN(ctxt->inputMethodPanel != nullptr, "inputMethodPanel is nullptr!", napi_generic_failure);
146 auto code = ctxt->inputMethodPanel->SetUiContent(ctxt->path, env, ctxt->contentStorage);
147 if (code == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
148 ctxt->SetErrorCode(code);
149 ctxt->SetErrorMessage("path should be a path to specific page.");
150 return napi_generic_failure;
151 }
152 return napi_ok;
153 };
154 ctxt->SetAction(std::move(input), std::move(output));
155 // 3 means JsAPI:setUiContent has 3 params at most.
156 AsyncCall asyncCall(env, info, ctxt, 3);
157 return asyncCall.Call(env, exec, "setUiContent");
158 }
159
Resize(napi_env env,napi_callback_info info)160 napi_value JsPanel::Resize(napi_env env, napi_callback_info info)
161 {
162 auto ctxt = std::make_shared<PanelContentContext>(env, info);
163 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
164 napi_status status = napi_generic_failure;
165 PARAM_CHECK_RETURN(env, argc > 1, "at least two parameters is required", TYPE_NONE, status);
166 // 0 means the first param width<uint32_t>
167 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[0], ctxt->width) == napi_ok,
168 "width type must be number!", TYPE_NONE, status);
169 // 1 means the second param height<uint32_t>
170 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[1], ctxt->height) == napi_ok,
171 "height type must be number!", TYPE_NONE, status);
172 ctxt->info = { std::chrono::system_clock::now(), JsEvent::RESIZE };
173 jsQueue_.Push(ctxt->info);
174 return napi_ok;
175 };
176
177 auto exec = [ctxt](AsyncCall::Context *ctx) {
178 jsQueue_.Wait(ctxt->info);
179 if (ctxt->inputMethodPanel == nullptr) {
180 IMSA_HILOGE("inputMethodPanel_ is nullptr.");
181 jsQueue_.Pop();
182 return;
183 }
184 SysPanelStatus sysPanelStatus = { false, ctxt->inputMethodPanel->GetPanelFlag(), ctxt->width, ctxt->height };
185 InputMethodAbility::GetInstance()->NotifyPanelStatus(ctxt->inputMethodPanel, sysPanelStatus);
186 auto code = ctxt->inputMethodPanel->Resize(ctxt->width, ctxt->height);
187 jsQueue_.Pop();
188 if (code == ErrorCode::NO_ERROR) {
189 ctxt->SetState(napi_ok);
190 return;
191 }
192 ctxt->SetErrorCode(code);
193 };
194 ctxt->SetAction(std::move(input));
195 // 3 means JsAPI:resize has 3 params at most.
196 AsyncCall asyncCall(env, info, ctxt, 3);
197 return asyncCall.Call(env, exec, "resize");
198 }
199
MoveTo(napi_env env,napi_callback_info info)200 napi_value JsPanel::MoveTo(napi_env env, napi_callback_info info)
201 {
202 auto ctxt = std::make_shared<PanelContentContext>(env, info);
203 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
204 napi_status status = napi_generic_failure;
205 PARAM_CHECK_RETURN(env, argc > 1, "at least two parameters is required ", TYPE_NONE, status);
206 // 0 means the first param x<int32_t>
207 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[0], ctxt->x) == napi_ok, "x type must be number",
208 TYPE_NONE, status);
209 // 1 means the second param y<int32_t>
210 PARAM_CHECK_RETURN(env, JsUtils::GetValue(env, argv[1], ctxt->y) == napi_ok, "y type must be number",
211 TYPE_NONE, status);
212 ctxt->info = { std::chrono::system_clock::now(), JsEvent::MOVE_TO };
213 jsQueue_.Push(ctxt->info);
214 return napi_ok;
215 };
216
217 auto exec = [ctxt](AsyncCall::Context *ctx) {
218 int64_t start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
219 jsQueue_.Wait(ctxt->info);
220 PrintEditorQueueInfoIfTimeout(start, ctxt->info);
221 if (ctxt->inputMethodPanel == nullptr) {
222 IMSA_HILOGE("inputMethodPanel_ is nullptr.");
223 jsQueue_.Pop();
224 return;
225 }
226 auto code = ctxt->inputMethodPanel->MoveTo(ctxt->x, ctxt->y);
227 jsQueue_.Pop();
228 if (code == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
229 ctxt->SetErrorCode(code);
230 return;
231 }
232 ctxt->SetState(napi_ok);
233 };
234 ctxt->SetAction(std::move(input));
235 // 3 means JsAPI:moveTo has 3 params at most.
236 AsyncCall asyncCall(env, info, ctxt, 3);
237 return asyncCall.Call(env, exec, "moveTo");
238 }
239
PrintEditorQueueInfoIfTimeout(int64_t start,const JsEventInfo & currentInfo)240 void JsPanel::PrintEditorQueueInfoIfTimeout(int64_t start, const JsEventInfo ¤tInfo)
241 {
242 int64_t end = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
243 if (end - start >= MAX_WAIT_TIME) {
244 JsEventInfo frontInfo;
245 auto ret = jsQueue_.GetFront(frontInfo);
246 int64_t frontTime = duration_cast<microseconds>(frontInfo.timestamp.time_since_epoch()).count();
247 int64_t currentTime = duration_cast<microseconds>(currentInfo.timestamp.time_since_epoch()).count();
248 IMSA_HILOGI("ret:%{public}d,front[%{public}" PRId64 ",%{public}d],current[%{public}" PRId64 ",%{public}d]", ret,
249 frontTime, static_cast<int32_t>(frontInfo.event), currentTime, static_cast<int32_t>(currentInfo.event));
250 }
251 }
252
Show(napi_env env,napi_callback_info info)253 napi_value JsPanel::Show(napi_env env, napi_callback_info info)
254 {
255 InputMethodSyncTrace tracer("JsPanel_Show");
256 auto ctxt = std::make_shared<PanelContentContext>(env, info);
257 auto exec = [ctxt](AsyncCall::Context *ctx) {
258 CHECK_RETURN_VOID(ctxt->inputMethodPanel != nullptr, "inputMethodPanel is nullptr!");
259 auto code = InputMethodAbility::GetInstance()->ShowPanel(ctxt->inputMethodPanel);
260 if (code == ErrorCode::NO_ERROR) {
261 ctxt->SetState(napi_ok);
262 return;
263 }
264 ctxt->SetErrorCode(code);
265 };
266 // 1 means JsAPI:show has 1 param at most.
267 AsyncCall asyncCall(env, info, ctxt, 1);
268 return asyncCall.Call(env, exec, "show");
269 }
270
Hide(napi_env env,napi_callback_info info)271 napi_value JsPanel::Hide(napi_env env, napi_callback_info info)
272 {
273 InputMethodSyncTrace tracer("JsPanel_Hide");
274 auto ctxt = std::make_shared<PanelContentContext>(env, info);
275 auto exec = [ctxt](AsyncCall::Context *ctx) {
276 CHECK_RETURN_VOID(ctxt->inputMethodPanel != nullptr, "inputMethodPanel is nullptr!");
277 auto code = InputMethodAbility::GetInstance()->HidePanel(ctxt->inputMethodPanel);
278 if (code == ErrorCode::NO_ERROR) {
279 ctxt->SetState(napi_ok);
280 return;
281 }
282 ctxt->SetErrorCode(code);
283 };
284 // 1 means JsAPI:hide has 1 param at most.
285 AsyncCall asyncCall(env, info, ctxt, 1);
286 return asyncCall.Call(env, exec, "panel.hide");
287 }
288
ChangeFlag(napi_env env,napi_callback_info info)289 napi_value JsPanel::ChangeFlag(napi_env env, napi_callback_info info)
290 {
291 size_t argc = ARGC_MAX;
292 napi_value argv[ARGC_MAX] = { nullptr };
293 napi_value thisVar = nullptr;
294 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
295 PARAM_CHECK_RETURN(env, argc > 0, "at least one parameter is required!", TYPE_NONE, nullptr);
296 int32_t panelFlag = 0;
297 // 0 means the first param flag<PanelFlag>
298 napi_status status = JsUtils::GetValue(env, argv[0], panelFlag);
299 PARAM_CHECK_RETURN(env, status == napi_ok, "flag type must be PanelFlag!", TYPE_NONE, nullptr);
300 auto inputMethodPanel = UnwrapPanel(env, thisVar);
301 if (inputMethodPanel == nullptr) {
302 IMSA_HILOGE("inputMethodPanel is nullptr");
303 return nullptr;
304 }
305 PARAM_CHECK_RETURN(env,
306 (panelFlag == PanelFlag::FLG_FIXED || panelFlag == PanelFlag::FLG_FLOATING ||
307 panelFlag == PanelFlag::FLG_CANDIDATE_COLUMN),
308 "flag type must be one of PanelFlag!", TYPE_NONE, nullptr);
309 JsEventInfo eventInfo = { std::chrono::system_clock::now(), JsEvent::CHANGE_FLAG };
310 jsQueue_.Push(eventInfo);
311 jsQueue_.Wait(eventInfo);
312 auto ret = inputMethodPanel->ChangePanelFlag(PanelFlag(panelFlag));
313 jsQueue_.Pop();
314 CHECK_RETURN(ret == ErrorCode::NO_ERROR, "failed to ChangePanelFlag!", nullptr);
315 return nullptr;
316 }
317
SetPrivacyMode(napi_env env,napi_callback_info info)318 napi_value JsPanel::SetPrivacyMode(napi_env env, napi_callback_info info)
319 {
320 size_t argc = ARGC_MAX;
321 napi_value argv[ARGC_MAX] = { nullptr };
322 napi_value thisVar = nullptr;
323 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
324 PARAM_CHECK_RETURN(env, argc > 0, "at least one parameter is required!", TYPE_NONE, nullptr);
325 bool isPrivacyMode = false;
326 // 0 means the first param isPrivacyMode<boolean>
327 napi_status status = JsUtils::GetValue(env, argv[0], isPrivacyMode);
328 PARAM_CHECK_RETURN(env, status == napi_ok, "isPrivacyMode type must be boolean!", TYPE_NONE, nullptr);
329 CHECK_RETURN(status == napi_ok, "failed to get isPrivacyMode!", nullptr);
330 auto inputMethodPanel = UnwrapPanel(env, thisVar);
331 if (inputMethodPanel == nullptr) {
332 IMSA_HILOGE("inputMethodPanel is nullptr");
333 return nullptr;
334 }
335 auto ret = inputMethodPanel->SetPrivacyMode(isPrivacyMode);
336 if (ret == static_cast<int32_t>(WMError::WM_ERROR_INVALID_PERMISSION)) {
337 JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_STATUS_PERMISSION_DENIED),
338 " ohos.permission.PRIVACY_WINDOW permission denied", TYPE_NONE);
339 }
340 CHECK_RETURN(ret == ErrorCode::NO_ERROR, "failed to SetPrivacyMode!", nullptr);
341 return nullptr;
342 }
343
Subscribe(napi_env env,napi_callback_info info)344 napi_value JsPanel::Subscribe(napi_env env, napi_callback_info info)
345 {
346 IMSA_HILOGD("JsPanel start.");
347 size_t argc = ARGC_MAX;
348 napi_value argv[ARGC_MAX] = { nullptr };
349 napi_value thisVar = nullptr;
350 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
351 std::string type;
352 // 2 means least param num.
353 if (argc < 2 || !JsUtil::GetValue(env, argv[0], type) ||
354 !EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type) ||
355 JsUtil::GetType(env, argv[1]) != napi_function) {
356 IMSA_HILOGE("subscribe failed, type: %{public}s!", type.c_str());
357 return nullptr;
358 }
359 IMSA_HILOGD("subscribe type: %{public}s.", type.c_str());
360 std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
361 auto inputMethodPanel = UnwrapPanel(env, thisVar);
362 // 1 means the second param callback.
363 std::shared_ptr<JSCallbackObject> cbObject =
364 std::make_shared<JSCallbackObject>(env, argv[1], std::this_thread::get_id());
365 observer->Subscribe(inputMethodPanel->windowId_, type, cbObject);
366 bool ret = inputMethodPanel->SetPanelStatusListener(observer, type);
367 if (!ret) {
368 IMSA_HILOGE("failed to subscribe %{public}s!", type.c_str());
369 observer->RemoveInfo(type, inputMethodPanel->windowId_);
370 }
371 napi_value result = nullptr;
372 napi_get_undefined(env, &result);
373 return result;
374 }
375
UnSubscribe(napi_env env,napi_callback_info info)376 napi_value JsPanel::UnSubscribe(napi_env env, napi_callback_info info)
377 {
378 size_t argc = ARGC_MAX;
379 napi_value argv[ARGC_MAX] = { nullptr };
380 napi_value thisVar = nullptr;
381 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
382 std::string type;
383 // 1 means least param num.
384 PARAM_CHECK_RETURN(env, argc >= 1, "at least one parameter is required!", TYPE_NONE, nullptr);
385 PARAM_CHECK_RETURN(env, JsUtil::GetValue(env, argv[0], type), "type must be string!", TYPE_NONE, nullptr);
386 PARAM_CHECK_RETURN(env, EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type),
387 "type should be show/hide/sizeChange!", TYPE_NONE, nullptr);
388 // if the second param is not napi_function/napi_null/napi_undefined, return
389 auto paramType = JsUtil::GetType(env, argv[1]);
390 PARAM_CHECK_RETURN(env, (paramType == napi_function || paramType == napi_null || paramType == napi_undefined),
391 "callback should be function or null or undefined!", TYPE_NONE, nullptr);
392 // if the second param is napi_function, delete it, else delete all
393 argv[1] = paramType == napi_function ? argv[1] : nullptr;
394
395 IMSA_HILOGD("unsubscribe type: %{public}s.", type.c_str());
396 std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
397 auto inputMethodPanel = UnwrapPanel(env, thisVar);
398 if (inputMethodPanel == nullptr) {
399 IMSA_HILOGE("inputMethodPanel is nullptr");
400 return nullptr;
401 }
402 observer->RemoveInfo(type, inputMethodPanel->windowId_);
403 inputMethodPanel->ClearPanelListener(type);
404 napi_value result = nullptr;
405 napi_get_null(env, &result);
406 return result;
407 }
408
CheckParam(napi_env env,size_t argc,napi_value * argv,std::shared_ptr<PanelContentContext> ctxt)409 napi_status JsPanel::CheckParam(napi_env env, size_t argc, napi_value *argv,
410 std::shared_ptr<PanelContentContext> ctxt)
411 {
412 napi_status status = napi_generic_failure;
413 PARAM_CHECK_RETURN(env, argc > 1, "at least two paramsters is required", TYPE_NONE, status);
414 // 0 means the first param flag
415 PARAM_CHECK_RETURN(env, JsUtil::GetType(env, argv[0]) == napi_number, "flag",
416 TYPE_NUMBER, napi_generic_failure);
417 int32_t panelFlag = 0;
418 CHECK_RETURN(JsUtils::GetValue(env, argv[0], panelFlag) == napi_ok,
419 "js param flag covert failed", napi_generic_failure);
420 ctxt->panelFlag = PanelFlag(panelFlag);
421 if (InputMethodAbility::GetInstance()->IsDefaultIme()) {
422 PARAM_CHECK_RETURN(env, ctxt->panelFlag == 0 || ctxt->panelFlag == 1 ||
423 ctxt->panelFlag == FLG_CANDIDATE_COLUMN,
424 "param flag type shoule be FLG_FIXED or FLG_FLOATING or FLG_CANDIDATE_COLUMN", TYPE_NONE,
425 napi_generic_failure);
426 } else {
427 PARAM_CHECK_RETURN(env, ctxt->panelFlag == 0 || ctxt->panelFlag == 1,
428 "param flag type shoule be FLG_FIXED or FLG_FLOATING ", TYPE_NONE, napi_generic_failure);
429 }
430 // 1 means the second param rect
431 PARAM_CHECK_RETURN(env, JsUtil::GetType(env, argv[1]) == napi_object, "param rect type must be PanelRect",
432 TYPE_NONE, napi_generic_failure);
433 PARAM_CHECK_RETURN(env, JsPanelRect::Read(env, argv[1], ctxt->layoutParams), "js param rect covert failed",
434 TYPE_NONE, napi_generic_failure);
435 return napi_ok;
436 }
437
AdjustPanelRect(napi_env env,napi_callback_info info)438 napi_value JsPanel::AdjustPanelRect(napi_env env, napi_callback_info info)
439 {
440 auto ctxt = std::make_shared<PanelContentContext>(env, info);
441 auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
442 if (CheckParam(env, argc, argv, ctxt) != napi_ok) {
443 return napi_generic_failure;
444 }
445 ctxt->info = { std::chrono::system_clock::now(), JsEvent::ADJUST_PANEL_RECT };
446 jsQueue_.Push(ctxt->info);
447 return napi_ok;
448 };
449
450 auto exec = [ctxt](AsyncCall::Context *ctx) {
451 jsQueue_.Wait(ctxt->info);
452 if (ctxt->inputMethodPanel == nullptr) {
453 IMSA_HILOGE("inputMethodPanel_ is nullptr.");
454 jsQueue_.Pop();
455 return;
456 }
457 SysPanelStatus sysPanelStatus;
458 if (ctxt->inputMethodPanel->IsDisplayPortrait()) {
459 sysPanelStatus = { false, ctxt->panelFlag, ctxt->layoutParams.portraitRect.width_,
460 ctxt->layoutParams.portraitRect.height_ };
461 } else {
462 sysPanelStatus = { false, ctxt->panelFlag, ctxt->layoutParams.landscapeRect.width_,
463 ctxt->layoutParams.landscapeRect.height_ };
464 }
465 InputMethodAbility::GetInstance()->NotifyPanelStatus(ctxt->inputMethodPanel, sysPanelStatus);
466 auto code = ctxt->inputMethodPanel->AdjustPanelRect(ctxt->panelFlag, ctxt->layoutParams);
467 jsQueue_.Pop();
468 if (code == ErrorCode::NO_ERROR) {
469 ctxt->SetState(napi_ok);
470 return;
471 } else if (code == ErrorCode::ERROR_PARAMETER_CHECK_FAILED) {
472 ctxt->SetErrorMessage("width limit:[0, displayWidth], height limit:[0, 70 percent of displayHeight]!");
473 }
474 ctxt->SetErrorCode(code);
475 };
476 ctxt->SetAction(std::move(input));
477 // 2 means JsAPI:adjustPanelRect has 2 params at most
478 AsyncCall asyncCall(env, info, ctxt, 2);
479 return asyncCall.Call(env, exec, "adjustPanelRect");
480 }
481
UnwrapPanel(napi_env env,napi_value thisVar)482 std::shared_ptr<InputMethodPanel> JsPanel::UnwrapPanel(napi_env env, napi_value thisVar)
483 {
484 void *native = nullptr;
485 napi_status status = napi_unwrap(env, thisVar, &native);
486 CHECK_RETURN((status == napi_ok && native != nullptr), "failed to unwrap!", nullptr);
487 auto jsPanel = reinterpret_cast<JsPanel *>(native);
488 if (jsPanel == nullptr) {
489 return nullptr;
490 }
491 auto inputMethodPanel = jsPanel->GetNative();
492 CHECK_RETURN(inputMethodPanel != nullptr, "inputMethodPanel is nullptr", nullptr);
493 return inputMethodPanel;
494 }
495
Write(napi_env env,const LayoutParams & layoutParams)496 napi_value JsPanelRect::Write(napi_env env, const LayoutParams &layoutParams)
497 {
498 napi_value jsObject = nullptr;
499 napi_create_object(env, &jsObject);
500 bool ret =
501 JsUtil::Object::WriteProperty(env, jsObject, "landscapeRect", JsRect::Write(env, layoutParams.landscapeRect));
502 ret = ret &&
503 JsUtil::Object::WriteProperty(env, jsObject, "portraitRect", JsRect::Write(env, layoutParams.portraitRect));
504 return ret ? jsObject : JsUtil::Const::Null(env);
505 }
Read(napi_env env,napi_value object,LayoutParams & layoutParams)506 bool JsPanelRect::Read(napi_env env, napi_value object, LayoutParams &layoutParams)
507 {
508 napi_value rectObject = nullptr;
509 napi_get_named_property(env, object, "landscapeRect", &rectObject);
510 bool ret = JsRect::Read(env, rectObject, layoutParams.landscapeRect);
511 napi_get_named_property(env, object, "portraitRect", &rectObject);
512 ret = ret && JsRect::Read(env, rectObject, layoutParams.portraitRect);
513 return ret;
514 }
515 } // namespace MiscServices
516 } // namespace OHOS