• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.h"
17 
18 #include "base/geometry/ng/size_t.h"
19 #include "base/i18n/localization.h"
20 #include "base/log/log.h"
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "base/utils/system_properties.h"
24 #include "base/utils/utils.h"
25 #include "bridge/card_frontend/card_frontend_declarative.h"
26 #include "bridge/card_frontend/form_frontend_declarative.h"
27 #include "bridge/common/utils/engine_helper.h"
28 #include "bridge/declarative_frontend/declarative_frontend.h"
29 #include "bridge/declarative_frontend/engine/functions/js_drag_function.h"
30 #include "bridge/declarative_frontend/engine/js_object_template.h"
31 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_api_bridge.h"
32 #include "bridge/declarative_frontend/frontend_delegate_declarative.h"
33 #include "bridge/declarative_frontend/interfaces/profiler/js_profiler.h"
34 #include "bridge/declarative_frontend/jsview/js_canvas_image_data.h"
35 #include "bridge/js_frontend/engine/jsi/ark_js_runtime.h"
36 #include "core/common/card_scope.h"
37 #include "core/common/container.h"
38 #include "core/components_ng/base/inspector.h"
39 #include "core/components_ng/pattern/stage/page_pattern.h"
40 #include "core/components_v2/inspector/inspector.h"
41 
42 namespace OHOS::Ace::Framework {
43 
44 constexpr int FUNC_SET_CREATE_ARG_LEN = 2;
45 
CreateJsObjectFromJsonValue(const EcmaVM * vm,const std::unique_ptr<JsonValue> & jsonValue)46 JSRef<JSVal> CreateJsObjectFromJsonValue(const EcmaVM* vm, const std::unique_ptr<JsonValue>& jsonValue)
47 {
48     if (jsonValue->IsBool()) {
49         return JSRef<JSVal>::Make(JsiValueConvertor::toJsiValueWithVM(vm, jsonValue->GetBool()));
50     } else if (jsonValue->IsNumber()) {
51         return JSRef<JSVal>::Make(JsiValueConvertor::toJsiValueWithVM(vm, jsonValue->GetDouble()));
52     } else if (jsonValue->IsString()) {
53         return JSRef<JSVal>::Make(JsiValueConvertor::toJsiValueWithVM(vm, jsonValue->GetString()));
54     } else if (jsonValue->IsArray()) {
55         JSRef<JSArray> array = JSRef<JSArray>::New();
56         int32_t size = jsonValue->GetArraySize();
57         for (int32_t i = 0; i < size; ++i) {
58             std::unique_ptr<JsonValue> item = jsonValue->GetArrayItem(i);
59             array->SetValueAt(i, CreateJsObjectFromJsonValue(vm, item));
60         }
61         return array;
62     } else if (jsonValue->IsObject()) {
63         JSRef<JSObject> object = JSRef<JSObject>::New();
64         std::unique_ptr<JsonValue> child = jsonValue->GetChild();
65         while (child && child->IsValid()) {
66             const std::string& key = child->GetKey();
67             object->SetPropertyObject(key.c_str(), CreateJsObjectFromJsonValue(vm, child));
68             child = child->GetNext();
69         }
70         return object;
71     } else if (jsonValue->IsNull()) {
72         return JSRef<JSVal>::Make(panda::JSValueRef::Null(vm));
73     } else {
74         return JSRef<JSVal>::Make(panda::JSValueRef::Undefined(vm));
75     }
76 }
77 
RegisterCardUpdateCallback(int64_t cardId,const panda::Local<panda::ObjectRef> & obj)78 void RegisterCardUpdateCallback(int64_t cardId, const panda::Local<panda::ObjectRef>& obj)
79 {
80     JSRef<JSObject> object = JSRef<JSObject>::Make(obj);
81     JSRef<JSVal> storageValue = object->GetProperty("localStorage_");
82     if (!storageValue->IsObject()) {
83         return;
84     }
85 
86     JSRef<JSObject> storage = JSRef<JSObject>::Cast(storageValue);
87     JSRef<JSVal> setOrCreateVal = storage->GetProperty("setOrCreate");
88     if (!setOrCreateVal->IsFunction()) {
89         return;
90     }
91 
92     JSRef<JSFunc> setOrCreate = JSRef<JSFunc>::Cast(setOrCreateVal);
93     auto id = ContainerScope::CurrentId();
94     auto callback = [storage, setOrCreate, id](const std::string& data) {
95         ContainerScope scope(id);
96         const EcmaVM* vm = storage->GetEcmaVM();
97         CHECK_NULL_VOID(vm);
98         std::unique_ptr<JsonValue> jsonRoot = JsonUtil::ParseJsonString(data);
99         CHECK_NULL_VOID(jsonRoot);
100         auto child = jsonRoot->GetChild();
101         if (!child || !child->IsValid()) {
102             return;
103         }
104 
105         while (child && child->IsValid()) {
106             const std::string& key = child->GetKey();
107             JSRef<JSVal> args[] = {
108                 JSRef<JSVal>::Make(JsiValueConvertor::toJsiValueWithVM(vm, key)),
109                 CreateJsObjectFromJsonValue(vm, child),
110             };
111             setOrCreate->Call(storage, FUNC_SET_CREATE_ARG_LEN, args);
112             child = child->GetNext();
113         }
114     };
115 
116     auto container = Container::Current();
117     if (container->IsFRSCardContainer() || container->IsDynamicRender()) {
118         auto frontEnd = AceType::DynamicCast<FormFrontendDeclarative>(container->GetCardFrontend(cardId).Upgrade());
119         CHECK_NULL_VOID(frontEnd);
120         auto delegate = frontEnd->GetDelegate();
121         CHECK_NULL_VOID(delegate);
122         delegate->SetUpdateCardDataCallback(callback);
123         delegate->UpdatePageDataImmediately();
124     } else {
125         auto frontEnd = AceType::DynamicCast<CardFrontendDeclarative>(container->GetCardFrontend(cardId).Upgrade());
126         CHECK_NULL_VOID(frontEnd);
127         auto delegate = frontEnd->GetDelegate();
128         CHECK_NULL_VOID(delegate);
129         delegate->SetUpdateCardDataCallback(callback);
130         delegate->UpdatePageDataImmediately();
131     }
132 }
133 
SetFormCallbacks(RefPtr<Container> container,JSView * view)134 void SetFormCallbacks(RefPtr<Container> container, JSView* view)
135 {
136     auto pipeline = container->GetPipelineContext();
137     if (pipeline != nullptr) {
138         pipeline->SetOnFormRecycleCallback([weak = Referenced::WeakClaim(view)]() {
139             auto view = weak.Upgrade();
140             std::string statusData;
141             if (view) {
142                 statusData = view->FireOnFormRecycle();
143             } else {
144                 LOGE("view is null");
145             }
146             return statusData;
147         });
148 
149         pipeline->SetOnFormRecoverCallback([weak = Referenced::WeakClaim(view)](const std::string& statusData) {
150             auto view = weak.Upgrade();
151             if (view) {
152                 view->FireOnFormRecover(statusData);
153             } else {
154                 LOGE("view is null");
155             }
156         });
157     } else {
158         LOGE("execute SetOnFormRecycleCallback and SetOnFormRecoverCallback failed due to pipeline is null");
159     }
160 }
161 
UpdatePageLifeCycleFunctions(RefPtr<NG::FrameNode> pageNode,JSView * view)162 void UpdatePageLifeCycleFunctions(RefPtr<NG::FrameNode> pageNode, JSView* view)
163 {
164     auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
165     CHECK_NULL_VOID(pagePattern);
166     pagePattern->SetOnPageShow([weak = Referenced::WeakClaim(view)]() {
167         auto view = weak.Upgrade();
168         if (view) {
169             view->FireOnShow();
170         }
171     });
172     pagePattern->SetOnPageHide([weak = Referenced::WeakClaim(view)]() {
173         auto view = weak.Upgrade();
174         if (view) {
175             view->FireOnHide();
176         }
177     });
178     pagePattern->SetOnBackPressed([weak = Referenced::WeakClaim(view)]() {
179         auto view = weak.Upgrade();
180         if (view) {
181             return view->FireOnBackPress();
182         }
183         return false;
184     });
185 }
186 
UpdateCardRootComponent(const panda::Local<panda::ObjectRef> & obj)187 void UpdateCardRootComponent(const panda::Local<panda::ObjectRef>& obj)
188 {
189     auto* view = static_cast<JSView*>(obj->GetNativePointerField(0));
190     if (!view && !static_cast<JSViewPartialUpdate*>(view) && !static_cast<JSViewFullUpdate*>(view)) {
191         return;
192     }
193 
194     auto container = Container::Current();
195     if (container && container->IsUseNewPipeline()) {
196         // Set Partial Update
197         Container::SetCurrentUsePartialUpdate(!view->isFullUpdate());
198 
199         auto cardId = CardScope::CurrentId();
200         view->SetCardId(cardId);
201 
202         RegisterCardUpdateCallback(cardId, obj);
203 
204         RefPtr<NG::PageRouterManager> pageRouterManager;
205 
206         if (container->IsFRSCardContainer() || container->IsDynamicRender()) {
207             auto frontEnd = AceType::DynamicCast<FormFrontendDeclarative>(container->GetCardFrontend(cardId).Upgrade());
208             CHECK_NULL_VOID(frontEnd);
209             auto delegate = frontEnd->GetDelegate();
210             CHECK_NULL_VOID(delegate);
211             pageRouterManager = delegate->GetPageRouterManager();
212         } else {
213             auto frontEnd = AceType::DynamicCast<CardFrontendDeclarative>(container->GetCardFrontend(cardId).Upgrade());
214             CHECK_NULL_VOID(frontEnd);
215             auto delegate = frontEnd->GetDelegate();
216             CHECK_NULL_VOID(delegate);
217             pageRouterManager = delegate->GetPageRouterManager();
218         }
219         CHECK_NULL_VOID(pageRouterManager);
220         auto pageNode = pageRouterManager->GetCurrentPageNode();
221         CHECK_NULL_VOID(pageNode);
222 
223         auto pageRootNode = AceType::DynamicCast<NG::UINode>(view->CreateViewNode());
224         CHECK_NULL_VOID(pageRootNode);
225         pageRootNode->MountToParent(pageNode);
226 
227         SetFormCallbacks(container, view);
228         UpdatePageLifeCycleFunctions(pageNode, view);
229     }
230 }
231 
JsLoadDocument(panda::JsiRuntimeCallInfo * runtimeCallInfo)232 panda::Local<panda::JSValueRef> JsLoadDocument(panda::JsiRuntimeCallInfo* runtimeCallInfo)
233 {
234     EcmaVM* vm = runtimeCallInfo->GetVM();
235     int32_t argc = runtimeCallInfo->GetArgsNumber();
236     if (argc != 1) {
237         return panda::JSValueRef::Undefined(vm);
238     }
239     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
240     if (!firstArg->IsObject()) {
241         return panda::JSValueRef::Undefined(vm);
242     }
243 
244     auto container = Container::Current();
245     if (container && container->IsDynamicRender()) {
246         LOGD("load dynamic component card");
247         panda::Local<panda::ObjectRef> obj = firstArg->ToObject(vm);
248         UpdateCardRootComponent(obj);
249         return panda::JSValueRef::Undefined(vm);
250     }
251 
252     panda::Global<panda::ObjectRef> obj(vm, Local<panda::ObjectRef>(firstArg));
253     JsiDeclarativeEngine::SetEntryObject(obj);
254 #if defined(PREVIEW)
255     panda::Global<panda::ObjectRef> rootView(vm, obj->ToObject(vm));
256     auto runtime = JsiDeclarativeEngineInstance::GetCurrentRuntime();
257     shared_ptr<ArkJSRuntime> arkRuntime = std::static_pointer_cast<ArkJSRuntime>(runtime);
258     arkRuntime->AddRootView(rootView);
259 #endif
260 
261     return panda::JSValueRef::Undefined(vm);
262 }
263 
JsLoadCustomTitleBar(panda::JsiRuntimeCallInfo * runtimeCallInfo)264 panda::Local<panda::JSValueRef> JsLoadCustomTitleBar(panda::JsiRuntimeCallInfo* runtimeCallInfo)
265 {
266     EcmaVM* vm = runtimeCallInfo->GetVM();
267     int32_t argc = static_cast<int32_t>(runtimeCallInfo->GetArgsNumber());
268     if (argc != 1) {
269         return panda::JSValueRef::Undefined(vm);
270     }
271     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
272     if (!firstArg->IsObject()) {
273         return panda::JSValueRef::Undefined(vm);
274     }
275 
276     panda::Local<panda::ObjectRef> obj = firstArg->ToObject(vm);
277     AddCustomTitleBarComponent(obj);
278 
279     return panda::JSValueRef::Undefined(vm);
280 }
281 
JsRegisterNamedRoute(panda::JsiRuntimeCallInfo * runtimeCallInfo)282 panda::Local<panda::JSValueRef> JsRegisterNamedRoute(panda::JsiRuntimeCallInfo* runtimeCallInfo)
283 {
284     EcmaVM* vm = runtimeCallInfo->GetVM();
285     int32_t argc = runtimeCallInfo->GetArgsNumber();
286     // will need three arguments
287     if (argc != 3) {
288         return panda::JSValueRef::Undefined(vm);
289     }
290     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
291     if (!firstArg->IsFunction()) {
292         return panda::JSValueRef::Undefined(vm);
293     }
294 #ifdef DYNAMIC_COMPONENT_SUPPORT
295     auto container = Container::Current();
296     if (container && container->IsDynamicRender()) {
297         LOGD("load dynamic component card through named route");
298         panda::Local<panda::FunctionRef> objSupplier = firstArg;
299         std::vector<Local<JSValueRef>> argv;
300         auto obj = objSupplier->Call(vm, JSNApi::GetGlobalObject(vm), argv.data(), 0);
301         UpdateCardRootComponent(obj->ToObject(vm));
302         return panda::JSValueRef::Undefined(vm);
303     }
304 #endif
305     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
306     if (!secondArg->IsString()) {
307         return panda::JSValueRef::Undefined(vm);
308     }
309     Local<JSValueRef> thirdArg = runtimeCallInfo->GetCallArgRef(2);
310     if (!thirdArg->IsObject()) {
311         return panda::JSValueRef::Undefined(vm);
312     }
313 
314     JsiDeclarativeEngine::AddToNamedRouterMap(vm,
315         panda::Global<panda::FunctionRef>(vm, Local<panda::FunctionRef>(firstArg)), secondArg->ToString(vm)->ToString(),
316         thirdArg->ToObject(vm));
317 
318     return panda::JSValueRef::Undefined(vm);
319 }
320 
JSPostCardAction(panda::JsiRuntimeCallInfo * runtimeCallInfo)321 panda::Local<panda::JSValueRef> JSPostCardAction(panda::JsiRuntimeCallInfo* runtimeCallInfo)
322 {
323     EcmaVM* vm = runtimeCallInfo->GetVM();
324 #if defined(PREVIEW)
325     LOGW("[Engine Log] The postCardAction interface in the Previewer is a mocked implementation and"
326 "may behave differently than an real device.");
327     return panda::JSValueRef::Undefined(vm);
328 #endif
329     int32_t argc = runtimeCallInfo->GetArgsNumber();
330     if (argc > 2) {
331         return panda::JSValueRef::Undefined(vm);
332     }
333     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
334     if (!firstArg->IsObject()) {
335         return panda::JSValueRef::Undefined(vm);
336     }
337 
338     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
339     if (!secondArg->IsObject()) {
340         return panda::JSValueRef::Undefined(vm);
341     }
342 
343     panda::Local<panda::ObjectRef> obj = firstArg->ToObject(vm);
344     auto* view = static_cast<JSView*>(obj->GetNativePointerField(0));
345     if (!view && !static_cast<JSViewPartialUpdate*>(view) && !static_cast<JSViewFullUpdate*>(view)) {
346         return panda::JSValueRef::Undefined(vm);
347     }
348 
349     auto value = panda::JSON::Stringify(vm, secondArg);
350     if (!value->IsString()) {
351         return panda::JSValueRef::Undefined(vm);
352     }
353     auto valueStr = panda::Local<panda::StringRef>(value);
354     auto action = valueStr->ToString();
355 
356 #if !defined(NG_BUILD)
357     int64_t cardId = view->GetCardId();
358     auto container = Container::Current();
359     if (container && container->IsUseNewPipeline()) {
360         if (container->IsFRSCardContainer()) {
361             auto frontEnd = AceType::DynamicCast<FormFrontendDeclarative>(container->GetCardFrontend(cardId).Upgrade());
362             CHECK_NULL_RETURN(frontEnd, panda::JSValueRef::Undefined(vm));
363             auto delegate = frontEnd->GetDelegate();
364             CHECK_NULL_RETURN(delegate, panda::JSValueRef::Undefined(vm));
365             delegate->FireCardAction(action);
366         } else {
367             auto frontEnd = AceType::DynamicCast<CardFrontendDeclarative>(container->GetCardFrontend(cardId).Upgrade());
368             CHECK_NULL_RETURN(frontEnd, panda::JSValueRef::Undefined(vm));
369             auto delegate = frontEnd->GetDelegate();
370             CHECK_NULL_RETURN(delegate, panda::JSValueRef::Undefined(vm));
371             delegate->FireCardAction(action);
372         }
373     }
374 #endif
375     return panda::JSValueRef::Undefined(vm);
376 }
377 
JsLoadEtsCard(panda::JsiRuntimeCallInfo * runtimeCallInfo)378 panda::Local<panda::JSValueRef> JsLoadEtsCard(panda::JsiRuntimeCallInfo* runtimeCallInfo)
379 {
380     EcmaVM* vm = runtimeCallInfo->GetVM();
381     int32_t argc = runtimeCallInfo->GetArgsNumber();
382     if (argc > 2) {
383         return panda::JSValueRef::Undefined(vm);
384     }
385     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
386     if (!firstArg->IsObject()) {
387         return panda::JSValueRef::Undefined(vm);
388     }
389 
390     panda::Local<panda::ObjectRef> obj = firstArg->ToObject(vm);
391     UpdateCardRootComponent(obj);
392 
393     return panda::JSValueRef::Undefined(vm);
394 }
395 
396 #if defined(PREVIEW)
JsPreviewerComponent(panda::JsiRuntimeCallInfo * runtimeCallInfo)397 panda::Local<panda::JSValueRef> JsPreviewerComponent(panda::JsiRuntimeCallInfo* runtimeCallInfo)
398 {
399     EcmaVM* vm = runtimeCallInfo->GetVM();
400 
401     auto runtime = JsiDeclarativeEngineInstance::GetCurrentRuntime();
402     shared_ptr<ArkJSRuntime> arkRuntime = std::static_pointer_cast<ArkJSRuntime>(runtime);
403     std::string requiredComponentName = arkRuntime->GetRequiredComponent();
404     panda::Global<panda::ObjectRef> obj = arkRuntime->GetPreviewComponent(vm, requiredComponentName);
405     if (obj->IsUndefined()) {
406         return panda::JSValueRef::Undefined(vm);
407     }
408     UpdateRootComponent(obj.ToLocal());
409 
410     return panda::JSValueRef::Undefined(vm);
411 }
412 
JsGetPreviewComponentFlag(panda::JsiRuntimeCallInfo * runtimeCallInfo)413 panda::Local<panda::JSValueRef> JsGetPreviewComponentFlag(panda::JsiRuntimeCallInfo* runtimeCallInfo)
414 {
415     EcmaVM* vm = runtimeCallInfo->GetVM();
416 
417     auto runtime = JsiDeclarativeEngineInstance::GetCurrentRuntime();
418     shared_ptr<ArkJSRuntime> arkRuntime = std::static_pointer_cast<ArkJSRuntime>(runtime);
419     bool isComponentPreview = arkRuntime->GetPreviewFlag();
420     if (!isComponentPreview) {
421         return panda::JSValueRef::False(vm);
422     }
423     return panda::JSValueRef::True(vm);
424 }
425 
JsStorePreviewComponents(panda::JsiRuntimeCallInfo * runtimeCallInfo)426 panda::Local<panda::JSValueRef> JsStorePreviewComponents(panda::JsiRuntimeCallInfo* runtimeCallInfo)
427 {
428     EcmaVM* vm = runtimeCallInfo->GetVM();
429 
430     auto runtime = JsiDeclarativeEngineInstance::GetCurrentRuntime();
431     shared_ptr<ArkJSRuntime> arkRuntime = std::static_pointer_cast<ArkJSRuntime>(runtime);
432     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
433     if (!firstArg->IsNumber()) {
434         return panda::JSValueRef::Undefined(vm);
435     }
436     panda::Local<NumberRef> componentNum = firstArg->ToNumber(vm);
437     uint32_t numOfComponent = componentNum->Value();
438     for (uint32_t index = 1; index <= numOfComponent * 2; index++) { // 2: each component pass two args, name and itself
439         Local<JSValueRef> componentName = runtimeCallInfo->GetCallArgRef(index);
440         if (!componentName->IsString()) {
441             return panda::JSValueRef::Undefined(vm);
442         }
443         std::string name = componentName->ToString(vm)->ToString();
444         Local<JSValueRef> componentObj = runtimeCallInfo->GetCallArgRef(++index);
445         if (componentObj->IsUndefined()) {
446             return panda::JSValueRef::Undefined(vm);
447         }
448         panda::Global<panda::ObjectRef> obj(vm, componentObj->ToObject(vm));
449         arkRuntime->AddPreviewComponent(name, obj);
450     }
451 
452     return panda::JSValueRef::Undefined(vm);
453 }
454 
JsGetRootView(panda::JsiRuntimeCallInfo * runtimeCallInfo)455 panda::Local<panda::JSValueRef> JsGetRootView(panda::JsiRuntimeCallInfo* runtimeCallInfo)
456 {
457     auto runtime = JsiDeclarativeEngineInstance::GetCurrentRuntime();
458     shared_ptr<ArkJSRuntime> arkRuntime = std::static_pointer_cast<ArkJSRuntime>(runtime);
459     return arkRuntime->GetRootView().ToLocal();
460 }
461 #endif
462 
JsDumpMemoryStats(panda::JsiRuntimeCallInfo * runtimeCallInfo)463 panda::Local<panda::JSValueRef> JsDumpMemoryStats(panda::JsiRuntimeCallInfo* runtimeCallInfo)
464 {
465     EcmaVM* vm = runtimeCallInfo->GetVM();
466     return panda::JSValueRef::Undefined(vm);
467 }
468 
JsGetI18nResource(panda::JsiRuntimeCallInfo * runtimeCallInfo)469 panda::Local<panda::JSValueRef> JsGetI18nResource(panda::JsiRuntimeCallInfo* runtimeCallInfo)
470 {
471     EcmaVM* vm = runtimeCallInfo->GetVM();
472     int32_t argc = runtimeCallInfo->GetArgsNumber();
473     if (vm == nullptr) {
474         return panda::JSValueRef::Undefined(vm);
475     }
476     if (argc != 2 && argc != 1) {
477         return panda::JSValueRef::Undefined(vm);
478     }
479     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
480     if (!firstArg->IsString()) {
481         return panda::JSValueRef::Undefined(vm);
482     }
483 
484     std::vector<std::string> splitStr;
485     std::string str = firstArg->ToString(vm)->ToString();
486     StringUtils::SplitStr(str, ".", splitStr);
487     if (splitStr.size() != 2) {
488         return panda::JSValueRef::Undefined(vm);
489     }
490 
491     auto targetStringKey = splitStr[0];
492     auto targetStringKeyValue = splitStr[1];
493     auto resultStrJson = JsiDeclarativeEngineInstance::GetI18nStringResource(targetStringKey, targetStringKeyValue);
494     auto resultStr = resultStrJson->GetString();
495     if (argc == 2) {
496         panda::LocalScope scope(vm);
497         Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
498         if (secondArg->IsArray(vm)) {
499             auto arrayVal = panda::Local<panda::ArrayRef>(secondArg);
500             auto len = arrayVal->Length(vm);
501             std::vector<std::string> arrayResult;
502             for (auto i = 0U; i < len; i++) {
503                 auto subItemVal = panda::ArrayRef::GetValueAt(vm, arrayVal, i);
504                 if (!subItemVal->IsString()) {
505                     arrayResult.emplace_back(std::string());
506                     continue;
507                 }
508                 auto itemVal = panda::Local<panda::StringRef>(subItemVal);
509                 arrayResult.emplace_back(itemVal->ToString());
510             }
511             ReplacePlaceHolderArray(resultStr, arrayResult);
512         } else if (secondArg->IsObject()) {
513             auto value = panda::JSON::Stringify(vm, secondArg);
514             if (value->IsString()) {
515                 auto valueStr = panda::Local<panda::StringRef>(value);
516                 std::unique_ptr<JsonValue> argsPtr = JsonUtil::ParseJsonString(valueStr->ToString());
517                 ReplacePlaceHolder(resultStr, argsPtr);
518             }
519         } else if (secondArg->IsNumber()) {
520             double count = secondArg->ToNumber(vm)->Value();
521             auto pluralChoice = Localization::GetInstance()->PluralRulesFormat(count);
522             if (!pluralChoice.empty()) {
523                 resultStr = ParserPluralResource(resultStrJson, pluralChoice, str);
524             }
525         }
526     }
527 
528     return panda::StringRef::NewFromUtf8(vm, resultStr.c_str());
529 }
530 
JsGetMediaResource(panda::JsiRuntimeCallInfo * runtimeCallInfo)531 panda::Local<panda::JSValueRef> JsGetMediaResource(panda::JsiRuntimeCallInfo* runtimeCallInfo)
532 {
533     EcmaVM* vm = runtimeCallInfo->GetVM();
534     int32_t argc = runtimeCallInfo->GetArgsNumber();
535     if (argc != 1) {
536         return panda::JSValueRef::Undefined(vm);
537     }
538     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
539     if (!firstArg->IsString()) {
540         return panda::JSValueRef::Undefined(vm);
541     }
542 
543     std::string targetMediaFileName = firstArg->ToString(vm)->ToString();
544     std::string filePath = JsiDeclarativeEngineInstance::GetMediaResource(targetMediaFileName);
545     return panda::StringRef::NewFromUtf8(vm, filePath.c_str());
546 }
547 
JsGetFrontendDelegate()548 RefPtr<FrontendDelegate> JsGetFrontendDelegate()
549 {
550     auto engine = EngineHelper::GetEngine(Container::CurrentId());
551     auto jsiEngine = AceType::DynamicCast<JsiDeclarativeEngine>(engine);
552     if (!jsiEngine) {
553         return nullptr;
554     }
555     auto engineInstance = jsiEngine->GetEngineInstance();
556     if (engineInstance == nullptr) {
557         return nullptr;
558     }
559     return engineInstance->GetDelegate();
560 }
561 
JsGetInspectorNodes(panda::JsiRuntimeCallInfo * runtimeCallInfo)562 panda::Local<panda::JSValueRef> JsGetInspectorNodes(panda::JsiRuntimeCallInfo* runtimeCallInfo)
563 {
564     ContainerScope scope{Container::CurrentIdSafely()};
565     EcmaVM* vm = runtimeCallInfo->GetVM();
566     if (vm == nullptr) {
567         return panda::JSValueRef::Undefined(vm);
568     }
569     auto declarativeDelegate = AceType::DynamicCast<FrontendDelegateDeclarative>(JsGetFrontendDelegate());
570     if (!declarativeDelegate) {
571         return panda::JSValueRef::Undefined(vm);
572     }
573     auto accessibilityManager = declarativeDelegate->GetJSAccessibilityManager();
574     auto nodeInfos = accessibilityManager->DumpComposedElementsToJson();
575     return panda::JSON::Parse(vm, panda::StringRef::NewFromUtf8(vm, nodeInfos->ToString().c_str()));
576 }
577 
JsGetInspectorNodeById(panda::JsiRuntimeCallInfo * runtimeCallInfo)578 panda::Local<panda::JSValueRef> JsGetInspectorNodeById(panda::JsiRuntimeCallInfo* runtimeCallInfo)
579 {
580     ContainerScope scope{Container::CurrentIdSafely()};
581     EcmaVM* vm = runtimeCallInfo->GetVM();
582     int32_t argc = runtimeCallInfo->GetArgsNumber();
583     if (vm == nullptr) {
584         return panda::JSValueRef::Undefined(vm);
585     }
586     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
587     if (argc < 1 || !firstArg->IsNumber()) {
588         return panda::JSValueRef::Undefined(vm);
589     }
590     auto declarativeDelegate = OHOS::Ace::AceType::DynamicCast<FrontendDelegateDeclarative>(JsGetFrontendDelegate());
591     if (!declarativeDelegate) {
592         return panda::JSValueRef::Undefined(vm);
593     }
594     auto accessibilityManager = declarativeDelegate->GetJSAccessibilityManager();
595     if (!accessibilityManager) {
596         return panda::JSValueRef::Undefined(vm);
597     }
598     int32_t intValue = firstArg->Int32Value(vm);
599     auto nodeInfo = accessibilityManager->DumpComposedElementToJson(intValue);
600     return panda::JSON::Parse(vm, panda::StringRef::NewFromUtf8(vm, nodeInfo->ToString().c_str()));
601 }
602 
JsGetInspectorTree(panda::JsiRuntimeCallInfo * runtimeCallInfo)603 panda::Local<panda::JSValueRef> JsGetInspectorTree(panda::JsiRuntimeCallInfo* runtimeCallInfo)
604 {
605     ContainerScope scope{Container::CurrentIdSafely()};
606     EcmaVM* vm = runtimeCallInfo->GetVM();
607     if (vm == nullptr) {
608         return panda::JSValueRef::Undefined(vm);
609     }
610     auto container = Container::Current();
611     if (!container) {
612         return panda::JSValueRef::Undefined(vm);
613     }
614 
615     if (container->IsUseNewPipeline()) {
616         auto nodeInfos = NG::Inspector::GetInspector();
617         return panda::JSON::Parse(vm, panda::StringRef::NewFromUtf8(vm, nodeInfos.c_str()));
618     }
619 #if !defined(NG_BUILD)
620     auto pipelineContext = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
621     if (pipelineContext == nullptr) {
622         return panda::JSValueRef::Undefined(vm);
623     }
624     auto nodeInfos = V2::Inspector::GetInspectorTree(pipelineContext);
625     return panda::JSON::Parse(vm, panda::StringRef::NewFromUtf8(vm, nodeInfos.c_str()));
626 #else
627     return panda::JSValueRef::Undefined(vm);
628 #endif
629 }
630 
JsGetInspectorByKey(panda::JsiRuntimeCallInfo * runtimeCallInfo)631 panda::Local<panda::JSValueRef> JsGetInspectorByKey(panda::JsiRuntimeCallInfo* runtimeCallInfo)
632 {
633     ContainerScope scope{Container::CurrentIdSafely()};
634     EcmaVM* vm = runtimeCallInfo->GetVM();
635     auto argc = runtimeCallInfo->GetArgsNumber();
636     if (vm == nullptr) {
637         return panda::JSValueRef::Undefined(vm);
638     }
639     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
640     if (argc < 1 || !firstArg->IsString()) {
641         return panda::JSValueRef::Undefined(vm);
642     }
643     auto container = Container::Current();
644     if (!container) {
645         return panda::JSValueRef::Undefined(vm);
646     }
647     std::string key = firstArg->ToString(vm)->ToString();
648     if (container->IsUseNewPipeline()) {
649         auto resultStr = NG::Inspector::GetInspectorNodeByKey(key);
650         return panda::StringRef::NewFromUtf8(vm, resultStr.c_str());
651     }
652 #if !defined(NG_BUILD)
653     auto pipelineContext = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
654     if (pipelineContext == nullptr) {
655         return panda::JSValueRef::Undefined(vm);
656     }
657     auto resultStr = V2::Inspector::GetInspectorNodeByKey(pipelineContext, key);
658     return panda::StringRef::NewFromUtf8(vm, resultStr.c_str());
659 #else
660     return panda::JSValueRef::Undefined(vm);
661 #endif
662 }
663 
JsSendEventByKey(panda::JsiRuntimeCallInfo * runtimeCallInfo)664 panda::Local<panda::JSValueRef> JsSendEventByKey(panda::JsiRuntimeCallInfo* runtimeCallInfo)
665 {
666     ContainerScope scope{Container::CurrentIdSafely()};
667     EcmaVM* vm = runtimeCallInfo->GetVM();
668     auto argc = runtimeCallInfo->GetArgsNumber();
669     if (vm == nullptr) {
670         return panda::JSValueRef::Undefined(vm);
671     }
672     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
673     if (argc < 3 || !firstArg->IsString()) { // 3: arg numbers
674         return panda::JSValueRef::Undefined(vm);
675     }
676     auto container = Container::Current();
677     if (!container) {
678         return panda::JSValueRef::Undefined(vm);
679     }
680 
681     std::string key = firstArg->ToString(vm)->ToString();
682     auto action = runtimeCallInfo->GetCallArgRef(1)->Int32Value(vm);
683     auto params = runtimeCallInfo->GetCallArgRef(2)->ToString(vm)->ToString();
684     if (container->IsUseNewPipeline()) {
685         auto result = NG::Inspector::SendEventByKey(key, action, params);
686         return panda::BooleanRef::New(vm, result);
687     }
688 #if !defined(NG_BUILD)
689     auto pipelineContext = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
690     if (pipelineContext == nullptr) {
691         return panda::JSValueRef::Undefined(vm);
692     }
693     auto result = V2::Inspector::SendEventByKey(pipelineContext, key, action, params);
694     return panda::BooleanRef::New(vm, result);
695 #else
696     return panda::JSValueRef::Undefined(vm);
697 #endif
698 }
699 
GetTouchPointFromJS(const JsiObject & value)700 static TouchEvent GetTouchPointFromJS(const JsiObject& value)
701 {
702     TouchEvent touchPoint;
703 
704     auto type = value->GetProperty("type");
705     touchPoint.type = static_cast<TouchType>(type->ToNumber<int32_t>());
706 
707     auto id = value->GetProperty("id");
708     touchPoint.id = id->ToNumber<int32_t>();
709 
710     auto x = value->GetProperty("x");
711     touchPoint.x = x->ToNumber<float>();
712 
713     auto y = value->GetProperty("y");
714     touchPoint.y = y->ToNumber<float>();
715 
716     touchPoint.time = std::chrono::high_resolution_clock::now();
717 
718     return touchPoint;
719 }
720 
JsSendTouchEvent(panda::JsiRuntimeCallInfo * runtimeCallInfo)721 panda::Local<panda::JSValueRef> JsSendTouchEvent(panda::JsiRuntimeCallInfo* runtimeCallInfo)
722 {
723     ContainerScope scope{Container::CurrentIdSafely()};
724     EcmaVM* vm = runtimeCallInfo->GetVM();
725     auto argc = runtimeCallInfo->GetArgsNumber();
726     if (vm == nullptr) {
727         return panda::JSValueRef::Undefined(vm);
728     }
729     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
730     if (argc < 1 || !firstArg->IsObject()) {
731         return panda::JSValueRef::Undefined(vm);
732     }
733 
734     auto container = Container::Current();
735     if (!container) {
736         return panda::JSValueRef::Undefined(vm);
737     }
738     auto pipelineContext = container->GetPipelineContext();
739     if (pipelineContext == nullptr) {
740         return panda::JSValueRef::Undefined(vm);
741     }
742     JsiObject obj(firstArg);
743     TouchEvent touchPoint = GetTouchPointFromJS(obj);
744     auto result = pipelineContext->GetTaskExecutor()->PostTask(
745         [pipelineContext, touchPoint]() { pipelineContext->OnTouchEvent(touchPoint); }, TaskExecutor::TaskType::UI);
746     return panda::BooleanRef::New(vm, result);
747 }
748 
GetKeyEventFromJS(const JsiObject & value)749 static KeyEvent GetKeyEventFromJS(const JsiObject& value)
750 {
751     auto type = value->GetProperty("type");
752     auto action = static_cast<KeyAction>(type->ToNumber<int32_t>());
753 
754     auto jsKeyCode = value->GetProperty("keyCode");
755     auto code = static_cast<KeyCode>(jsKeyCode->ToNumber<int32_t>());
756 
757     KeyEvent keyEvent(code, action);
758 
759     auto jsKeySource = value->GetProperty("keySource");
760     keyEvent.sourceType = static_cast<SourceType>(jsKeySource->ToNumber<int32_t>());
761 
762     auto jsDeviceId = value->GetProperty("deviceId");
763     keyEvent.deviceId = jsDeviceId->ToNumber<int32_t>();
764 
765     auto jsMetaKey = value->GetProperty("metaKey");
766     keyEvent.metaKey = jsMetaKey->ToNumber<int32_t>();
767 
768     auto jsTimestamp = value->GetProperty("timestamp");
769     auto timeStamp = jsTimestamp->ToNumber<int64_t>();
770     keyEvent.SetTimeStamp(timeStamp);
771 
772     return keyEvent;
773 }
774 
JsSendKeyEvent(panda::JsiRuntimeCallInfo * runtimeCallInfo)775 panda::Local<panda::JSValueRef> JsSendKeyEvent(panda::JsiRuntimeCallInfo* runtimeCallInfo)
776 {
777     ContainerScope scope{Container::CurrentIdSafely()};
778     EcmaVM* vm = runtimeCallInfo->GetVM();
779     auto argc = runtimeCallInfo->GetArgsNumber();
780     if (vm == nullptr) {
781         return panda::JSValueRef::Undefined(vm);
782     }
783     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
784     if (argc < 1 || !firstArg->IsObject()) {
785         return panda::JSValueRef::Undefined(vm);
786     }
787 
788     auto container = Container::Current();
789     if (!container) {
790         return panda::JSValueRef::Undefined(vm);
791     }
792     auto pipelineContext = container->GetPipelineContext();
793     if (pipelineContext == nullptr) {
794         return panda::JSValueRef::Undefined(vm);
795     }
796     JsiObject obj(firstArg);
797     KeyEvent keyEvent = GetKeyEventFromJS(obj);
798     auto result = pipelineContext->GetTaskExecutor()->PostTask(
799         [pipelineContext, keyEvent]() { pipelineContext->OnKeyEvent(keyEvent); }, TaskExecutor::TaskType::UI);
800     return panda::BooleanRef::New(vm, result);
801 }
802 
GetMouseEventFromJS(const JsiObject & value)803 static MouseEvent GetMouseEventFromJS(const JsiObject& value)
804 {
805     MouseEvent mouseEvent;
806 
807     auto action = value->GetProperty("action");
808     mouseEvent.action = static_cast<MouseAction>(action->ToNumber<int32_t>());
809 
810     auto button = value->GetProperty("button");
811     mouseEvent.button = static_cast<MouseButton>(button->ToNumber<int32_t>());
812 
813     auto x = value->GetProperty("x");
814     mouseEvent.x = x->ToNumber<float>();
815     mouseEvent.deltaX = mouseEvent.x;
816 
817     auto y = value->GetProperty("y");
818     mouseEvent.y = y->ToNumber<float>();
819     mouseEvent.deltaY = mouseEvent.y;
820 
821     mouseEvent.time = std::chrono::high_resolution_clock::now();
822     mouseEvent.sourceType = SourceType::MOUSE;
823     return mouseEvent;
824 }
825 
JsSendMouseEvent(panda::JsiRuntimeCallInfo * runtimeCallInfo)826 panda::Local<panda::JSValueRef> JsSendMouseEvent(panda::JsiRuntimeCallInfo* runtimeCallInfo)
827 {
828     ContainerScope scope{Container::CurrentIdSafely()};
829     EcmaVM* vm = runtimeCallInfo->GetVM();
830     auto argc = runtimeCallInfo->GetArgsNumber();
831     if (vm == nullptr) {
832         return panda::JSValueRef::Undefined(vm);
833     }
834     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
835     if (argc < 1 || !firstArg->IsObject()) {
836         return panda::JSValueRef::Undefined(vm);
837     }
838 
839     auto container = Container::Current();
840     if (!container) {
841         return panda::JSValueRef::Undefined(vm);
842     }
843     auto pipelineContext = container->GetPipelineContext();
844     if (pipelineContext == nullptr) {
845         return panda::JSValueRef::Undefined(vm);
846     }
847     JsiObject obj(firstArg);
848     MouseEvent mouseEvent = GetMouseEventFromJS(obj);
849     auto result = pipelineContext->GetTaskExecutor()->PostTask(
850         [pipelineContext, mouseEvent]() { pipelineContext->OnMouseEvent(mouseEvent); }, TaskExecutor::TaskType::UI);
851     return panda::BooleanRef::New(vm, result);
852 }
853 
Vp2Px(panda::JsiRuntimeCallInfo * runtimeCallInfo)854 panda::Local<panda::JSValueRef> Vp2Px(panda::JsiRuntimeCallInfo* runtimeCallInfo)
855 {
856     EcmaVM* vm = runtimeCallInfo->GetVM();
857     int32_t argc = runtimeCallInfo->GetArgsNumber();
858     if (argc != 1) {
859         return panda::JSValueRef::Undefined(vm);
860     }
861     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
862     if (!firstArg->IsNumber()) {
863         return panda::JSValueRef::Undefined(vm);
864     }
865 
866     double vpValue = firstArg->ToNumber(vm)->Value();
867     double density = PipelineBase::GetCurrentDensity();
868     double pxValue = vpValue * density;
869     return panda::NumberRef::New(vm, pxValue);
870 }
871 
Px2Vp(panda::JsiRuntimeCallInfo * runtimeCallInfo)872 panda::Local<panda::JSValueRef> Px2Vp(panda::JsiRuntimeCallInfo* runtimeCallInfo)
873 {
874     ContainerScope scope(Container::CurrentIdSafely());
875     EcmaVM* vm = runtimeCallInfo->GetVM();
876     int32_t argc = runtimeCallInfo->GetArgsNumber();
877     if (argc != 1) {
878         return panda::JSValueRef::Undefined(vm);
879     }
880     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
881     if (!firstArg->IsNumber()) {
882         return panda::JSValueRef::Undefined(vm);
883     }
884     double density = PipelineBase::GetCurrentDensity();
885     if (NearZero(density)) {
886         return panda::JSValueRef::Undefined(vm);
887     }
888 
889     double pxValue = firstArg->ToNumber(vm)->Value();
890     double vpValue = pxValue / density;
891 
892     return panda::NumberRef::New(vm, vpValue);
893 }
894 
Fp2Px(panda::JsiRuntimeCallInfo * runtimeCallInfo)895 panda::Local<panda::JSValueRef> Fp2Px(panda::JsiRuntimeCallInfo* runtimeCallInfo)
896 {
897     ContainerScope scope(Container::CurrentIdSafely());
898     EcmaVM* vm = runtimeCallInfo->GetVM();
899     int32_t argc = runtimeCallInfo->GetArgsNumber();
900     if (argc != 1) {
901         return panda::JSValueRef::Undefined(vm);
902     }
903     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
904     if (!firstArg->IsNumber()) {
905         return panda::JSValueRef::Undefined(vm);
906     }
907 
908     double density = PipelineBase::GetCurrentDensity();
909     double fpValue = firstArg->ToNumber(vm)->Value();
910 
911     auto container = Container::Current();
912     if (!container) {
913         return panda::JSValueRef::Undefined(vm);
914     }
915     auto pipelineContext = container->GetPipelineContext();
916     double fontScale = 1.0;
917     if (pipelineContext) {
918         fontScale = pipelineContext->GetFontScale();
919     }
920     double pxValue = fpValue * density * fontScale;
921     return panda::NumberRef::New(vm, pxValue);
922 }
923 
Px2Fp(panda::JsiRuntimeCallInfo * runtimeCallInfo)924 panda::Local<panda::JSValueRef> Px2Fp(panda::JsiRuntimeCallInfo* runtimeCallInfo)
925 {
926     ContainerScope scope(Container::CurrentIdSafely());
927     EcmaVM* vm = runtimeCallInfo->GetVM();
928     int32_t argc = runtimeCallInfo->GetArgsNumber();
929     if (argc != 1) {
930         return panda::JSValueRef::Undefined(vm);
931     }
932     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
933     if (!firstArg->IsNumber()) {
934         return panda::JSValueRef::Undefined(vm);
935     }
936     double density = PipelineBase::GetCurrentDensity();
937     if (NearZero(density)) {
938         return panda::JSValueRef::Undefined(vm);
939     }
940 
941     double pxValue = firstArg->ToNumber(vm)->Value();
942     auto container = Container::Current();
943     if (!container) {
944         return panda::JSValueRef::Undefined(vm);
945     }
946     auto pipelineContext = container->GetPipelineContext();
947     double fontScale = 1.0;
948     if (pipelineContext) {
949         fontScale = pipelineContext->GetFontScale();
950     }
951     double ratio = density * fontScale;
952     double fpValue = pxValue / ratio;
953     return panda::NumberRef::New(vm, fpValue);
954 }
955 
Lpx2Px(panda::JsiRuntimeCallInfo * runtimeCallInfo)956 panda::Local<panda::JSValueRef> Lpx2Px(panda::JsiRuntimeCallInfo* runtimeCallInfo)
957 {
958     ContainerScope scope(Container::CurrentIdSafely());
959     EcmaVM* vm = runtimeCallInfo->GetVM();
960     int32_t argc = runtimeCallInfo->GetArgsNumber();
961     if (argc != 1) {
962         return panda::JSValueRef::Undefined(vm);
963     }
964     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
965     if (!firstArg->IsNumber()) {
966         return panda::JSValueRef::Undefined(vm);
967     }
968     auto container = Container::Current();
969     CHECK_NULL_RETURN(container, panda::JSValueRef::Undefined(vm));
970     auto window = container->GetWindow();
971     CHECK_NULL_RETURN(window, panda::JSValueRef::Undefined(vm));
972     auto width = window->GetCurrentWindowRect().Width();
973 
974     auto frontend = container->GetFrontend();
975     CHECK_NULL_RETURN(frontend, panda::JSValueRef::Undefined(vm));
976     auto windowConfig = frontend->GetWindowConfig();
977     windowConfig.UpdateDesignWidthScale(width);
978 
979     double lpxValue = firstArg->ToNumber(vm)->Value();
980     double pxValue = lpxValue * windowConfig.designWidthScale;
981     return panda::NumberRef::New(vm, pxValue);
982 }
983 
Px2Lpx(panda::JsiRuntimeCallInfo * runtimeCallInfo)984 panda::Local<panda::JSValueRef> Px2Lpx(panda::JsiRuntimeCallInfo* runtimeCallInfo)
985 {
986     ContainerScope scope(Container::CurrentIdSafely());
987     EcmaVM* vm = runtimeCallInfo->GetVM();
988     int32_t argc = runtimeCallInfo->GetArgsNumber();
989     if (argc != 1) {
990         return panda::JSValueRef::Undefined(vm);
991     }
992     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
993     if (!firstArg->IsNumber()) {
994         return panda::JSValueRef::Undefined(vm);
995     }
996     auto container = Container::Current();
997     CHECK_NULL_RETURN(container, panda::JSValueRef::Undefined(vm));
998     auto window = container->GetWindow();
999     CHECK_NULL_RETURN(window, panda::JSValueRef::Undefined(vm));
1000     auto width = window->GetCurrentWindowRect().Width();
1001 
1002     auto frontend = container->GetFrontend();
1003     CHECK_NULL_RETURN(frontend, panda::JSValueRef::Undefined(vm));
1004     auto windowConfig = frontend->GetWindowConfig();
1005     windowConfig.UpdateDesignWidthScale(width);
1006 
1007     double pxValue = firstArg->ToNumber(vm)->Value();
1008     double lpxValue = pxValue / windowConfig.designWidthScale;
1009     return panda::NumberRef::New(vm, lpxValue);
1010 }
1011 
SetAppBackgroundColor(panda::JsiRuntimeCallInfo * runtimeCallInfo)1012 panda::Local<panda::JSValueRef> SetAppBackgroundColor(panda::JsiRuntimeCallInfo* runtimeCallInfo)
1013 {
1014     ContainerScope scope(Container::CurrentIdSafely());
1015     EcmaVM* vm = runtimeCallInfo->GetVM();
1016     int32_t argc = runtimeCallInfo->GetArgsNumber();
1017     if (argc != 1) {
1018         return panda::JSValueRef::Undefined(vm);
1019     }
1020     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
1021     if (!firstArg->IsString()) {
1022         return panda::JSValueRef::Undefined(vm);
1023     }
1024     std::string backgroundColorStr = firstArg->ToString(vm)->ToString();
1025     auto container = Container::Current();
1026     if (!container) {
1027         return panda::JSValueRef::Undefined(vm);
1028     }
1029     auto pipelineContext = container->GetPipelineContext();
1030     if (pipelineContext) {
1031         pipelineContext->SetAppBgColor(Color::FromString(backgroundColorStr));
1032     }
1033     return panda::JSValueRef::Undefined(vm);
1034 }
1035 
RequestFocus(panda::JsiRuntimeCallInfo * runtimeCallInfo)1036 panda::Local<panda::JSValueRef> RequestFocus(panda::JsiRuntimeCallInfo* runtimeCallInfo)
1037 {
1038     ContainerScope scope(Container::CurrentIdSafely());
1039     EcmaVM* vm = runtimeCallInfo->GetVM();
1040     int32_t argc = runtimeCallInfo->GetArgsNumber();
1041     if (vm == nullptr) {
1042         return panda::JSValueRef::Undefined(vm);
1043     }
1044     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
1045     if (argc < 1 || !firstArg->IsString()) {
1046         return panda::JSValueRef::Undefined(vm);
1047     }
1048     std::string inspectorKey = firstArg->ToString(vm)->ToString();
1049 
1050     bool result = false;
1051     auto pipelineContext = PipelineContext::GetCurrentContext();
1052     CHECK_NULL_RETURN(pipelineContext, panda::BooleanRef::New(vm, result));
1053     if (!pipelineContext->GetTaskExecutor()) {
1054         return panda::BooleanRef::New(vm, result);
1055     }
1056     pipelineContext->GetTaskExecutor()->PostSyncTask(
1057         [pipelineContext, inspectorKey, &result]() { result = pipelineContext->RequestFocus(inspectorKey); },
1058         TaskExecutor::TaskType::UI);
1059     return panda::BooleanRef::New(vm, result);
1060 }
1061 
SetCursor(panda::JsiRuntimeCallInfo * runtimeCallInfo)1062 panda::Local<panda::JSValueRef> SetCursor(panda::JsiRuntimeCallInfo* runtimeCallInfo)
1063 {
1064     ContainerScope scope(Container::CurrentIdSafely());
1065     EcmaVM* vm = runtimeCallInfo->GetVM();
1066     int32_t argc = static_cast<int32_t>(runtimeCallInfo->GetArgsNumber());
1067     if (vm == nullptr) {
1068         return panda::JSValueRef::Undefined(vm);
1069     }
1070     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
1071     if (argc < 1 || !firstArg->IsNumber()) {
1072         return panda::JSValueRef::Undefined(vm);
1073     }
1074     int32_t intValue = firstArg->Int32Value(vm);
1075 
1076     auto pipelineContext = PipelineContext::GetCurrentContext();
1077     CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
1078     if (!pipelineContext->GetTaskExecutor()) {
1079         return panda::JSValueRef::Undefined(vm);
1080     }
1081     pipelineContext->GetTaskExecutor()->PostSyncTask(
1082         [pipelineContext, intValue]() { pipelineContext->SetCursor(intValue); },
1083         TaskExecutor::TaskType::UI);
1084     return panda::JSValueRef::Undefined(vm);
1085 }
1086 
RestoreDefault(panda::JsiRuntimeCallInfo * runtimeCallInfo)1087 panda::Local<panda::JSValueRef> RestoreDefault(panda::JsiRuntimeCallInfo* runtimeCallInfo)
1088 {
1089     ContainerScope scope(Container::CurrentIdSafely());
1090     EcmaVM* vm = runtimeCallInfo->GetVM();
1091     if (vm == nullptr) {
1092         return panda::JSValueRef::Undefined(vm);
1093     }
1094 
1095     auto pipelineContext = PipelineContext::GetCurrentContext();
1096     CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
1097     if (!pipelineContext->GetTaskExecutor()) {
1098         return panda::JSValueRef::Undefined(vm);
1099     }
1100     pipelineContext->GetTaskExecutor()->PostSyncTask(
1101         [pipelineContext]() { pipelineContext->RestoreDefault(); },
1102         TaskExecutor::TaskType::UI);
1103     return panda::JSValueRef::Undefined(vm);
1104 }
1105 
1106 #ifdef FORM_SUPPORTED
JsRegisterFormViews(BindingTarget globalObj,const std::unordered_set<std::string> & formModuleList,bool isReload,void * nativeEngine)1107 void JsRegisterFormViews(
1108     BindingTarget globalObj, const std::unordered_set<std::string>& formModuleList, bool isReload, void* nativeEngine)
1109 {
1110     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
1111     if (!runtime) {
1112         return;
1113     }
1114     if (isReload) {
1115         JsBindFormViews(globalObj, formModuleList, nativeEngine, isReload);
1116         return;
1117     }
1118     auto vm = runtime->GetEcmaVm();
1119     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "loadEtsCard"),
1120         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsLoadEtsCard));
1121     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "postCardAction"),
1122         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSPostCardAction));
1123 #if defined(PREVIEW)
1124     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "previewComponent"),
1125         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsPreviewerComponent));
1126     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getPreviewComponentFlag"),
1127         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetPreviewComponentFlag));
1128     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "storePreviewComponents"),
1129         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsStorePreviewComponents));
1130     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "GetRootView"),
1131         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetRootView));
1132 #endif
1133     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "dumpMemoryStats"),
1134         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsDumpMemoryStats));
1135     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "$s"),
1136         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetI18nResource));
1137     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "$m"),
1138         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetMediaResource));
1139     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorNodes"),
1140         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorNodes));
1141     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorNodeById"),
1142         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorNodeById));
1143     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorTree"),
1144         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorTree));
1145     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorByKey"),
1146         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorByKey));
1147     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendEventByKey"),
1148         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendEventByKey));
1149     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendTouchEvent"),
1150         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendTouchEvent));
1151     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendKeyEvent"),
1152         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendKeyEvent));
1153     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendMouseEvent"),
1154         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendMouseEvent));
1155     globalObj->Set(
1156         vm, panda::StringRef::NewFromUtf8(vm, "vp2px"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Vp2Px));
1157     globalObj->Set(
1158         vm, panda::StringRef::NewFromUtf8(vm, "px2vp"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Vp));
1159     globalObj->Set(
1160         vm, panda::StringRef::NewFromUtf8(vm, "fp2px"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Fp2Px));
1161     globalObj->Set(
1162         vm, panda::StringRef::NewFromUtf8(vm, "px2fp"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Fp));
1163     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "lpx2px"),
1164         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Lpx2Px));
1165     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "px2lpx"),
1166         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Lpx));
1167     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "setAppBgColor"),
1168         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), SetAppBackgroundColor));
1169 
1170     JsBindFormViews(globalObj, formModuleList, nativeEngine);
1171 
1172     JSObjectTemplate toggleType;
1173     toggleType.Constant("Checkbox", 0);
1174     toggleType.Constant("Switch", 1);
1175     toggleType.Constant("Button", 2); // 2 means index of constant
1176 
1177     JSObjectTemplate refreshStatus;
1178     refreshStatus.Constant("Inactive", 0);
1179     refreshStatus.Constant("Drag", 1);
1180     refreshStatus.Constant("OverDrag", 2);
1181     refreshStatus.Constant("Refresh", 3); // 3 means index of constant
1182     refreshStatus.Constant("Done", 4);    // 4 means index of constant
1183 
1184     JSObjectTemplate mainAxisAlign;
1185     mainAxisAlign.Constant("Start", 1);
1186     mainAxisAlign.Constant("Center", 2);       // 2 means index of constant
1187     mainAxisAlign.Constant("End", 3);          // 3 means index of constant
1188     mainAxisAlign.Constant("SpaceBetween", 6); // 6 means index of constant
1189     mainAxisAlign.Constant("SpaceAround", 7);  // 7 means index of constant
1190 
1191     JSObjectTemplate crossAxisAlign;
1192     crossAxisAlign.Constant("Start", 1);
1193 
1194     crossAxisAlign.Constant("Center", 2);  // 2 means index of constant
1195     crossAxisAlign.Constant("End", 3);     // 3 means index of constant
1196     crossAxisAlign.Constant("Stretch", 4); // 4 means index of constant
1197 
1198     JSObjectTemplate direction;
1199     direction.Constant("Horizontal", 0);
1200     direction.Constant("Vertical", 1);
1201 
1202     JSObjectTemplate loadingProgressStyle;
1203     loadingProgressStyle.Constant("Default", 1);
1204     loadingProgressStyle.Constant("Circular", 2); // 2 means index of constant
1205     loadingProgressStyle.Constant("Orbital", 3);  // 3 means index of constant
1206 
1207     JSObjectTemplate progressStyle;
1208     progressStyle.Constant("Linear", 0);
1209     progressStyle.Constant("Ring", 1);      // 1 means index of constant
1210     progressStyle.Constant("Eclipse", 2);   // 2 means index of constant
1211     progressStyle.Constant("ScaleRing", 3); // 3 means index of constant
1212     progressStyle.Constant("Capsule", 4);   // 4 means index of constant
1213 
1214     JSObjectTemplate stackFit;
1215     stackFit.Constant("Keep", 0);
1216     stackFit.Constant("Stretch", 1);
1217     stackFit.Constant("Inherit", 2);    // 2 means index of constant
1218     stackFit.Constant("FirstChild", 3); // 3 means index of constant
1219 
1220     JSObjectTemplate overflow;
1221     overflow.Constant("Clip", 0);
1222     overflow.Constant("Observable", 1);
1223 
1224     JSObjectTemplate alignment;
1225     alignment.Constant("TopLeft", 0);
1226     alignment.Constant("TopCenter", 1);
1227     alignment.Constant("TopRight", 2);     // 2 means index of constant
1228     alignment.Constant("CenterLeft", 3);   // 3 means index of constant
1229     alignment.Constant("Center", 4);       // 4 means index of constant
1230     alignment.Constant("CenterRight", 5);  // 5 means index of constant
1231     alignment.Constant("BottomLeft", 6);   // 6 means index of constant
1232     alignment.Constant("BottomCenter", 7); // 7 means index of constant
1233     alignment.Constant("BottomRight", 8);  // 8 means index of constant
1234 
1235     JSObjectTemplate sliderStyle;
1236     sliderStyle.Constant("OutSet", 0);
1237     sliderStyle.Constant("InSet", 1);
1238 
1239     JSObjectTemplate sliderChangeMode;
1240     sliderChangeMode.Constant("Begin", 0);
1241     sliderChangeMode.Constant("Moving", 1);
1242     sliderChangeMode.Constant("End", 2); // 2 means index of constant
1243 
1244     JSObjectTemplate pickerStyle;
1245     pickerStyle.Constant("Inline", 0);
1246     pickerStyle.Constant("Block", 1);
1247     pickerStyle.Constant("Fade", 2); // 2 means index of constant
1248 
1249     JSObjectTemplate buttonType;
1250     buttonType.Constant("Normal", (int)ButtonType::NORMAL);
1251     buttonType.Constant("Capsule", (int)ButtonType::CAPSULE);
1252     buttonType.Constant("Circle", (int)ButtonType::CIRCLE);
1253     buttonType.Constant("Arc", (int)ButtonType::ARC);
1254 
1255     JSObjectTemplate iconPosition;
1256     iconPosition.Constant("Start", 0);
1257     iconPosition.Constant("End", 1);
1258 
1259     JSObjectTemplate badgePosition;
1260     badgePosition.Constant("RightTop", 0);
1261     badgePosition.Constant("Right", 1);
1262     badgePosition.Constant("Left", 2); // 2 means index of constant
1263 
1264     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "MainAxisAlign"), *mainAxisAlign);
1265     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "CrossAxisAlign"), *crossAxisAlign);
1266     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Direction"), *direction);
1267     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "StackFit"), *stackFit);
1268     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Align"), *alignment);
1269     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Overflow"), *overflow);
1270     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ButtonType"), *buttonType);
1271     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "LoadingProgressStyle"), *loadingProgressStyle);
1272     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ProgressStyle"), *progressStyle);
1273     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ToggleType"), *toggleType);
1274     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "RefreshStatus"), *refreshStatus);
1275     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "SliderStyle"), *sliderStyle);
1276     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "SliderChangeMode"), *sliderChangeMode);
1277     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "IconPosition"), *iconPosition);
1278     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "PickerStyle"), *pickerStyle);
1279     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "BadgePosition"), *badgePosition);
1280 }
1281 #endif
1282 
JsRegisterViews(BindingTarget globalObj,void * nativeEngine)1283 void JsRegisterViews(BindingTarget globalObj, void* nativeEngine)
1284 {
1285     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
1286     if (!runtime) {
1287         return;
1288     }
1289     auto vm = runtime->GetEcmaVm();
1290     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "loadDocument"),
1291         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsLoadDocument));
1292     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "loadEtsCard"),
1293         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsLoadEtsCard));
1294     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "postCardAction"),
1295         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSPostCardAction));
1296 #if defined(PREVIEW)
1297     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "previewComponent"),
1298         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsPreviewerComponent));
1299     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getPreviewComponentFlag"),
1300         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetPreviewComponentFlag));
1301     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "storePreviewComponents"),
1302         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsStorePreviewComponents));
1303     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "GetRootView"),
1304         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetRootView));
1305 #endif
1306     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "dumpMemoryStats"),
1307         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsDumpMemoryStats));
1308     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "$s"),
1309         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetI18nResource));
1310     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "$m"),
1311         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetMediaResource));
1312     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorNodes"),
1313         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorNodes));
1314     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorNodeById"),
1315         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorNodeById));
1316     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorTree"),
1317         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorTree));
1318     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getInspectorByKey"),
1319         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsGetInspectorByKey));
1320     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendEventByKey"),
1321         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendEventByKey));
1322     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendTouchEvent"),
1323         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendTouchEvent));
1324     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendKeyEvent"),
1325         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendKeyEvent));
1326     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "sendMouseEvent"),
1327         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsSendMouseEvent));
1328     globalObj->Set(
1329         vm, panda::StringRef::NewFromUtf8(vm, "vp2px"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Vp2Px));
1330     globalObj->Set(
1331         vm, panda::StringRef::NewFromUtf8(vm, "px2vp"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Vp));
1332     globalObj->Set(
1333         vm, panda::StringRef::NewFromUtf8(vm, "fp2px"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Fp2Px));
1334     globalObj->Set(
1335         vm, panda::StringRef::NewFromUtf8(vm, "px2fp"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Fp));
1336     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "lpx2px"),
1337         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Lpx2Px));
1338     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "px2lpx"),
1339         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Lpx));
1340     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "setAppBgColor"),
1341         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), SetAppBackgroundColor));
1342 
1343     BindingTarget focusControlObj = panda::ObjectRef::New(const_cast<panda::EcmaVM*>(vm));
1344     focusControlObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "requestFocus"),
1345         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), RequestFocus));
1346     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "focusControl"), focusControlObj);
1347     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "registerNamedRoute"),
1348         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsRegisterNamedRoute));
1349     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "getArkUINativeModule"),
1350         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), NG::ArkUINativeModule::GetArkUINativeModule));
1351     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "loadCustomTitleBar"),
1352         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JsLoadCustomTitleBar));
1353 
1354     BindingTarget cursorControlObj = panda::ObjectRef::New(const_cast<panda::EcmaVM*>(vm));
1355     cursorControlObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "setCursor"),
1356         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), SetCursor));
1357     cursorControlObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "restoreDefault"),
1358         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), RestoreDefault));
1359     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "cursorControl"), cursorControlObj);
1360     JsBindViews(globalObj, nativeEngine);
1361 
1362     JSObjectTemplate toggleType;
1363     toggleType.Constant("Checkbox", 0);
1364     toggleType.Constant("Switch", 1);
1365     toggleType.Constant("Button", 2); // 2 means index of constant
1366 
1367     JSObjectTemplate refreshStatus;
1368     refreshStatus.Constant("Inactive", 0);
1369     refreshStatus.Constant("Drag", 1);
1370     refreshStatus.Constant("OverDrag", 2);
1371     refreshStatus.Constant("Refresh", 3); // 3 means index of constant
1372     refreshStatus.Constant("Done", 4);    // 4 means index of constant
1373 
1374     JSObjectTemplate mainAxisAlign;
1375     mainAxisAlign.Constant("Start", 1);
1376     mainAxisAlign.Constant("Center", 2);       // 2 means index of constant
1377     mainAxisAlign.Constant("End", 3);          // 3 means index of constant
1378     mainAxisAlign.Constant("SpaceBetween", 6); // 6 means index of constant
1379     mainAxisAlign.Constant("SpaceAround", 7);  // 7 means index of constant
1380 
1381     JSObjectTemplate crossAxisAlign;
1382     crossAxisAlign.Constant("Start", 1);
1383 
1384     crossAxisAlign.Constant("Center", 2);  // 2 means index of constant
1385     crossAxisAlign.Constant("End", 3);     // 3 means index of constant
1386     crossAxisAlign.Constant("Stretch", 4); // 4 means index of constant
1387 
1388     JSObjectTemplate direction;
1389     direction.Constant("Horizontal", 0);
1390     direction.Constant("Vertical", 1);
1391 
1392     JSObjectTemplate loadingProgressStyle;
1393     loadingProgressStyle.Constant("Default", 1);
1394     loadingProgressStyle.Constant("Circular", 2); // 2 means index of constant
1395     loadingProgressStyle.Constant("Orbital", 3);  // 3 means index of constant
1396 
1397     JSObjectTemplate progressStyle;
1398     progressStyle.Constant("Linear", 0);
1399     progressStyle.Constant("Ring", 1);      // 1 means index of constant
1400     progressStyle.Constant("Eclipse", 2);   // 2 means index of constant
1401     progressStyle.Constant("ScaleRing", 3); // 3 means index of constant
1402     progressStyle.Constant("Capsule", 4);   // 4 means index of constant
1403 
1404     JSObjectTemplate stackFit;
1405     stackFit.Constant("Keep", 0);
1406     stackFit.Constant("Stretch", 1);
1407     stackFit.Constant("Inherit", 2);    // 2 means index of constant
1408     stackFit.Constant("FirstChild", 3); // 3 means index of constant
1409 
1410     JSObjectTemplate overflow;
1411     overflow.Constant("Clip", 0);
1412     overflow.Constant("Observable", 1);
1413 
1414     JSObjectTemplate alignment;
1415     alignment.Constant("TopLeft", 0);
1416     alignment.Constant("TopCenter", 1);
1417     alignment.Constant("TopRight", 2);     // 2 means index of constant
1418     alignment.Constant("CenterLeft", 3);   // 3 means index of constant
1419     alignment.Constant("Center", 4);       // 4 means index of constant
1420     alignment.Constant("CenterRight", 5);  // 5 means index of constant
1421     alignment.Constant("BottomLeft", 6);   // 6 means index of constant
1422     alignment.Constant("BottomCenter", 7); // 7 means index of constant
1423     alignment.Constant("BottomRight", 8);  // 8 means index of constant
1424 
1425     JSObjectTemplate sliderStyle;
1426     sliderStyle.Constant("OutSet", 0);
1427     sliderStyle.Constant("InSet", 1);
1428 
1429     JSObjectTemplate sliderChangeMode;
1430     sliderChangeMode.Constant("Begin", 0);
1431     sliderChangeMode.Constant("Moving", 1);
1432     sliderChangeMode.Constant("End", 2);   // 2 means index of constant
1433     sliderChangeMode.Constant("Click", 3); // 3 means index of constant
1434 
1435     JSObjectTemplate pickerStyle;
1436     pickerStyle.Constant("Inline", 0);
1437     pickerStyle.Constant("Block", 1);
1438     pickerStyle.Constant("Fade", 2); // 2 means index of constant
1439 
1440     JSObjectTemplate buttonType;
1441     buttonType.Constant("Normal", (int)ButtonType::NORMAL);
1442     buttonType.Constant("Capsule", (int)ButtonType::CAPSULE);
1443     buttonType.Constant("Circle", (int)ButtonType::CIRCLE);
1444     buttonType.Constant("Arc", (int)ButtonType::ARC);
1445 
1446     JSObjectTemplate iconPosition;
1447     iconPosition.Constant("Start", 0);
1448     iconPosition.Constant("End", 1);
1449 
1450     JSObjectTemplate badgePosition;
1451     badgePosition.Constant("RightTop", 0);
1452     badgePosition.Constant("Right", 1);
1453     badgePosition.Constant("Left", 2); // 2 means index of constant
1454 
1455     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "MainAxisAlign"), *mainAxisAlign);
1456     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "CrossAxisAlign"), *crossAxisAlign);
1457     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Direction"), *direction);
1458     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "StackFit"), *stackFit);
1459     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Align"), *alignment);
1460     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Overflow"), *overflow);
1461     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ButtonType"), *buttonType);
1462     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "LoadingProgressStyle"), *loadingProgressStyle);
1463     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ProgressStyle"), *progressStyle);
1464     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ToggleType"), *toggleType);
1465     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "RefreshStatus"), *refreshStatus);
1466     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "SliderStyle"), *sliderStyle);
1467     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "SliderChangeMode"), *sliderChangeMode);
1468     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "IconPosition"), *iconPosition);
1469     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "PickerStyle"), *pickerStyle);
1470     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "BadgePosition"), *badgePosition);
1471 }
1472 
JsRegisterWorkerViews(BindingTarget globalObj,void * nativeEngine)1473 void JsRegisterWorkerViews(BindingTarget globalObj, void* nativeEngine)
1474 {
1475     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
1476     if (!runtime) {
1477         return;
1478     }
1479     auto vm = runtime->GetEcmaVm();
1480     globalObj->Set(
1481         vm, panda::StringRef::NewFromUtf8(vm, "vp2px"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Vp2Px));
1482     globalObj->Set(
1483         vm, panda::StringRef::NewFromUtf8(vm, "px2vp"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Vp));
1484     globalObj->Set(
1485         vm, panda::StringRef::NewFromUtf8(vm, "fp2px"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Fp2Px));
1486     globalObj->Set(
1487         vm, panda::StringRef::NewFromUtf8(vm, "px2fp"), panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Fp));
1488     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "lpx2px"),
1489         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Lpx2Px));
1490     globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "px2lpx"),
1491         panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), Px2Lpx));
1492     JsBindWorkerViews(globalObj, nativeEngine);
1493 }
1494 
1495 } // namespace OHOS::Ace::Framework
1496