1 /*
2  * Copyright (c) 2021-2024 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/jsview/js_xcomponent.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "base/memory/referenced.h"
20 #include "base/utils/utils.h"
21 #include "bridge/common/utils/engine_helper.h"
22 #include "bridge/declarative_frontend/engine/js_converter.h"
23 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
25 #include "bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
26 #include "bridge/declarative_frontend/jsview/models/xcomponent_model_impl.h"
27 #include "core/components/common/layout/constants.h"
28 #include "core/components_ng/base/view_stack_processor.h"
29 #include "core/components_ng/pattern/xcomponent/xcomponent_model.h"
30 #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h"
31 #include "frameworks/core/components_ng/base/view_abstract_model.h"
32 #include "frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h"
33 
34 namespace OHOS::Ace {
35 namespace {
ConvertToXComponentType(const std::string & type)36 XComponentType ConvertToXComponentType(const std::string& type)
37 {
38     if (type == "surface") {
39         return XComponentType::SURFACE;
40     }
41     if (type == "component") {
42         return XComponentType::COMPONENT;
43     }
44     if (type == "node") {
45         return XComponentType::NODE;
46     }
47     return XComponentType::SURFACE;
48 }
49 } // namespace
50 
51 std::unique_ptr<XComponentModel> XComponentModel::instance_ = nullptr;
52 std::mutex XComponentModel::mutex_;
53 
GetInstance()54 XComponentModel* XComponentModel::GetInstance()
55 {
56     if (!instance_) {
57         std::lock_guard<std::mutex> lock(mutex_);
58         if (!instance_) {
59 #ifdef NG_BUILD
60             instance_.reset(new NG::XComponentModelNG());
61 #else
62             if (Container::IsCurrentUseNewPipeline()) {
63                 instance_.reset(new NG::XComponentModelNG());
64             } else {
65                 instance_.reset(new Framework::XComponentModelImpl());
66             }
67 #endif
68         }
69     }
70     return instance_.get();
71 }
72 
73 } // namespace OHOS::Ace
74 
75 namespace OHOS::Ace::Framework {
SetControllerOnCreated(const WeakPtr<NG::FrameNode> & targetNode,const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)76 void SetControllerOnCreated(
77     const WeakPtr<NG::FrameNode>& targetNode, const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
78 {
79     auto jsCreatedFunc = object->GetProperty("onSurfaceCreated");
80     if (jsCreatedFunc->IsFunction()) {
81         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsCreatedFunc));
82         auto onSurfaceCreated = [execCtx, func = std::move(jsFunc), node = targetNode](
83                                     const std::string& surfaceId, const std::string& xcomponentId) {
84             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
85             ACE_SCORING_EVENT("XComponentController.onSurfaceCreated");
86             PipelineContext::SetCallBackNode(node);
87             auto jsVal = JSRef<JSVal>::Make(ToJSValue(surfaceId));
88             func->ExecuteJS(1, &jsVal);
89             TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] ControllerOnCreated surfaceId:%{public}s",
90                 xcomponentId.c_str(), surfaceId.c_str());
91         };
92         XComponentModel::GetInstance()->SetControllerOnCreated(std::move(onSurfaceCreated));
93     }
94 }
95 
SetControllerOnChanged(const WeakPtr<NG::FrameNode> & targetNode,const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)96 void SetControllerOnChanged(
97     const WeakPtr<NG::FrameNode>& targetNode, const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
98 {
99     auto jsChangedFunc = object->GetProperty("onSurfaceChanged");
100     if (jsChangedFunc->IsFunction()) {
101         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsChangedFunc));
102         auto onSurfaceChanged = [execCtx, func = std::move(jsFunc), node = targetNode](
103                                     const std::string& surfaceId, const NG::RectF& rect) {
104             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
105             ACE_SCORING_EVENT("XComponentController.onSurfaceChanged");
106             PipelineContext::SetCallBackNode(node);
107             JSRef<JSObject> rectObj = JSRef<JSObject>::New();
108             rectObj->SetProperty("offsetX", rect.Left());
109             rectObj->SetProperty("offsetY", rect.Top());
110             rectObj->SetProperty("surfaceWidth", rect.Width());
111             rectObj->SetProperty("surfaceHeight", rect.Height());
112             auto jsSurfaceId = JSRef<JSVal>::Make(ToJSValue(surfaceId));
113             JSRef<JSVal> params[2] = { jsSurfaceId, rectObj };
114             func->ExecuteJS(2, params);
115         };
116         XComponentModel::GetInstance()->SetControllerOnChanged(std::move(onSurfaceChanged));
117     }
118 }
119 
SetControllerOnDestroyed(const WeakPtr<NG::FrameNode> & targetNode,const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)120 void SetControllerOnDestroyed(
121     const WeakPtr<NG::FrameNode>& targetNode, const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
122 {
123     auto jsDestroyedFunc = object->GetProperty("onSurfaceDestroyed");
124     if (jsDestroyedFunc->IsFunction()) {
125         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsDestroyedFunc));
126         auto onSurfaceDestroyed = [execCtx, func = std::move(jsFunc), node = targetNode](
127                                       const std::string& surfaceId, const std::string& xcomponentId) {
128             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
129             ACE_SCORING_EVENT("XComponentController.onSurfaceDestroyed");
130             PipelineContext::SetCallBackNode(node);
131             auto jsVal = JSRef<JSVal>::Make(ToJSValue(surfaceId));
132             func->ExecuteJS(1, &jsVal);
133             TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] ControllerOnDestroyed surfaceId:%{public}s",
134                 xcomponentId.c_str(), surfaceId.c_str());
135         };
136         XComponentModel::GetInstance()->SetControllerOnDestroyed(std::move(onSurfaceDestroyed));
137     }
138 }
139 
SetControllerCallback(const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)140 void SetControllerCallback(const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
141 {
142     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
143     SetControllerOnCreated(targetNode, object, execCtx);
144     SetControllerOnChanged(targetNode, object, execCtx);
145     SetControllerOnDestroyed(targetNode, object, execCtx);
146 }
147 
GetXComponentController(const JSRef<JSObject> & controller,std::optional<std::string> & id,const JsiExecutionContext & execCtx)148 std::shared_ptr<InnerXComponentController> GetXComponentController(
149     const JSRef<JSObject>& controller, std::optional<std::string>& id, const JsiExecutionContext& execCtx)
150 {
151     std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
152     auto* jsXComponentController = controller->Unwrap<JSXComponentController>();
153     if (jsXComponentController) {
154         jsXComponentController->SetInstanceId(Container::CurrentId());
155         if (id.has_value()) {
156             XComponentClient::GetInstance().AddControllerToJSXComponentControllersMap(
157                 id.value(), jsXComponentController);
158         }
159         xcomponentController = jsXComponentController->GetController();
160     }
161     return xcomponentController;
162 }
163 
JSBind(BindingTarget globalObj)164 void JSXComponent::JSBind(BindingTarget globalObj)
165 {
166     JSClass<JSXComponent>::Declare("XComponent");
167     JSClass<JSXComponent>::StaticMethod("create", &JSXComponent::Create);
168     JSClass<JSXComponent>::StaticMethod("onLoad", &JSXComponent::JsOnLoad);
169     JSClass<JSXComponent>::StaticMethod("onDestroy", &JSXComponent::JsOnDestroy);
170     JSClass<JSXComponent>::StaticMethod("onAppear", &JSXComponent::JsOnAppear);
171     JSClass<JSXComponent>::StaticMethod("onDisAppear", &JSXComponent::JsOnDisAppear);
172     JSClass<JSXComponent>::StaticMethod("onAttach", &JSXComponent::JsOnAttach);
173     JSClass<JSXComponent>::StaticMethod("onDetach", &JSXComponent::JsOnDetach);
174 
175     JSClass<JSXComponent>::StaticMethod("onTouch", &JSXComponent::JsOnTouch);
176     JSClass<JSXComponent>::StaticMethod("onClick", &JSXComponent::JsOnClick);
177     JSClass<JSXComponent>::StaticMethod("onKeyEvent", &JSXComponent::JsOnKeyEvent);
178     JSClass<JSXComponent>::StaticMethod("onMouse", &JSXComponent::JsOnMouse);
179     JSClass<JSXComponent>::StaticMethod("onHover", &JSXComponent::JsOnHover);
180     JSClass<JSXComponent>::StaticMethod("onFocus", &JSXComponent::JsOnFocus);
181     JSClass<JSXComponent>::StaticMethod("onBlur", &JSXComponent::JsOnBlur);
182 
183     JSClass<JSXComponent>::StaticMethod("backgroundColor", &JSXComponent::JsBackgroundColor);
184     JSClass<JSXComponent>::StaticMethod("backgroundImage", &JSXComponent::JsBackgroundImage);
185     JSClass<JSXComponent>::StaticMethod("backgroundImageSize", &JSXComponent::JsBackgroundImageSize);
186     JSClass<JSXComponent>::StaticMethod("backgroundImagePosition", &JSXComponent::JsBackgroundImagePosition);
187     JSClass<JSXComponent>::StaticMethod("opacity", &JSXComponent::JsOpacity);
188     JSClass<JSXComponent>::StaticMethod("blur", &JSXComponent::JsBlur);
189     JSClass<JSXComponent>::StaticMethod("backdropBlur", &JSXComponent::JsBackdropBlur);
190     JSClass<JSXComponent>::StaticMethod("grayscale", &JSXComponent::JsGrayscale);
191     JSClass<JSXComponent>::StaticMethod("brightness", &JSXComponent::JsBrightness);
192     JSClass<JSXComponent>::StaticMethod("saturate", &JSXComponent::JsSaturate);
193     JSClass<JSXComponent>::StaticMethod("contrast", &JSXComponent::JsContrast);
194     JSClass<JSXComponent>::StaticMethod("invert", &JSXComponent::JsInvert);
195     JSClass<JSXComponent>::StaticMethod("sepia", &JSXComponent::JsSepia);
196     JSClass<JSXComponent>::StaticMethod("hueRotate", &JSXComponent::JsHueRotate);
197     JSClass<JSXComponent>::StaticMethod("colorBlend", &JSXComponent::JsColorBlend);
198     JSClass<JSXComponent>::StaticMethod("sphericalEffect", &JSXComponent::JsSphericalEffect);
199     JSClass<JSXComponent>::StaticMethod("lightUpEffect", &JSXComponent::JsLightUpEffect);
200     JSClass<JSXComponent>::StaticMethod("pixelStretchEffect", &JSXComponent::JsPixelStretchEffect);
201     JSClass<JSXComponent>::StaticMethod("linearGradientBlur", &JSXComponent::JsLinearGradientBlur);
202     JSClass<JSXComponent>::StaticMethod("enableAnalyzer", &JSXComponent::JsEnableAnalyzer);
203     JSClass<JSXComponent>::StaticMethod("renderFit", &JSXComponent::JsRenderFit);
204     JSClass<JSXComponent>::StaticMethod("enableSecure", &JSXComponent::JsEnableSecure);
205 
206     JSClass<JSXComponent>::InheritAndBind<JSContainerBase>(globalObj);
207 }
208 
Create(const JSCallbackInfo & info)209 void JSXComponent::Create(const JSCallbackInfo& info)
210 {
211     if (info.Length() < 1 || !info[0]->IsObject()) {
212         return;
213     }
214     auto paramObject = JSRef<JSObject>::Cast(info[0]);
215     auto id = paramObject->GetProperty("id");
216 
217     auto type = paramObject->GetProperty("type");
218     auto libraryNameValue = paramObject->GetProperty("libraryname");
219     std::optional<std::string> idOpt = std::nullopt;
220     std::optional<std::string> libraryNameOpt = std::nullopt;
221     if (id->IsString()) {
222         idOpt = id->ToString();
223     }
224     if (libraryNameValue->IsString()) {
225         libraryNameOpt = libraryNameValue->ToString();
226     }
227     auto controller = paramObject->GetProperty("controller");
228     auto aiOptions = paramObject->GetProperty("imageAIOptions");
229     std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
230     JSRef<JSObject> controllerObj;
231     if (controller->IsObject()) {
232         controllerObj = JSRef<JSObject>::Cast(controller);
233         xcomponentController = GetXComponentController(controllerObj, idOpt, info.GetExecutionContext());
234     }
235     XComponentType xcomponentType = XComponentType::SURFACE;
236     if (type->IsString()) {
237         xcomponentType = ConvertToXComponentType(type->ToString());
238     } else if (type->IsNumber()) {
239         xcomponentType = static_cast<XComponentType>(type->ToNumber<int32_t>());
240     }
241     XComponentModel::GetInstance()->Create(idOpt, xcomponentType, libraryNameOpt, xcomponentController);
242     if (!libraryNameOpt.has_value() && xcomponentController && !controllerObj->IsUndefined()) {
243         SetControllerCallback(controllerObj, info.GetExecutionContext());
244     }
245 
246     auto detachCallback = [](const std::string& xcomponentId) {
247         XComponentClient::GetInstance().DeleteControllerFromJSXComponentControllersMap(xcomponentId);
248         XComponentClient::GetInstance().DeleteFromJsValMapById(xcomponentId);
249     };
250     XComponentModel::GetInstance()->SetDetachCallback(std::move(detachCallback));
251 
252     if (info.Length() > 1 && info[1]->IsString()) {
253         auto soPath = info[1]->ToString();
254         XComponentModel::GetInstance()->SetSoPath(soPath);
255     }
256     ParseImageAIOptions(aiOptions);
257 }
258 
Create(const XComponentParams & params)259 void* JSXComponent::Create(const XComponentParams& params)
260 {
261     std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
262     if (params.controller) {
263         xcomponentController = params.controller->GetController();
264     }
265     auto frameNode = AceType::DynamicCast<NG::FrameNode>(XComponentModel::GetInstance()->Create(params.elmtId,
266         static_cast<float>(params.width), static_cast<float>(params.height), params.xcomponentId,
267         static_cast<XComponentType>(params.xcomponentType), params.libraryName, xcomponentController));
268     frameNode->SetIsArkTsFrameNode(true);
269     auto pattern = frameNode->GetPattern<NG::XComponentPattern>();
270     CHECK_NULL_RETURN(pattern, nullptr);
271     pattern->SetRenderType(static_cast<NodeRenderType>(params.renderType));
272     pattern->SetExportTextureSurfaceId(params.surfaceId);
273 
274     auto container = Container::Current();
275     CHECK_NULL_RETURN(container, nullptr);
276     auto pipelineContext = AceType::DynamicCast<NG::PipelineContext>(container->GetPipelineContext());
277     CHECK_NULL_RETURN(pipelineContext, nullptr);
278     auto taskExecutor = pipelineContext->GetTaskExecutor();
279     CHECK_NULL_RETURN(taskExecutor, nullptr);
280     auto* jsXComponent = new JSXComponent();
281     jsXComponent->SetFrameNode(frameNode);
282     taskExecutor->PostTask(
283         [weak = AceType::WeakClaim(AceType::RawPtr(frameNode))]() {
284             auto frameNode = weak.Upgrade();
285             CHECK_NULL_VOID(frameNode);
286             auto xcPattern = frameNode->GetPattern<NG::XComponentPattern>();
287             CHECK_NULL_VOID(xcPattern);
288             xcPattern->XComponentSizeInit();
289             xcPattern->SetXcomponentInit(true);
290         },
291         TaskExecutor::TaskType::JS, "ArkUIXComponentCreate");
292 
293     return jsXComponent;
294 }
295 
ParseImageAIOptions(const JSRef<JSVal> & jsValue)296 void JSXComponent::ParseImageAIOptions(const JSRef<JSVal>& jsValue)
297 {
298     if (!jsValue->IsObject()) {
299         return;
300     }
301     auto engine = EngineHelper::GetCurrentEngine();
302     CHECK_NULL_VOID(engine);
303     NativeEngine* nativeEngine = engine->GetNativeEngine();
304     CHECK_NULL_VOID(nativeEngine);
305     panda::Local<JsiValue> value = jsValue.Get().GetLocalHandle();
306     JSValueWrapper valueWrapper = value;
307     ScopeRAII scope(reinterpret_cast<napi_env>(nativeEngine));
308     napi_value optionsValue = nativeEngine->ValueToNapiValue(valueWrapper);
309     XComponentModel::GetInstance()->SetImageAIOptions(optionsValue);
310 }
311 
ChangeRenderType(int32_t renderType)312 bool JSXComponent::ChangeRenderType(int32_t renderType)
313 {
314     auto xcFrameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
315     CHECK_NULL_RETURN(xcFrameNode, false);
316     auto pattern = xcFrameNode->GetPattern<NG::XComponentPattern>();
317     CHECK_NULL_RETURN(pattern, false);
318     return pattern->ChangeRenderType(static_cast<NodeRenderType>(renderType));
319 }
320 
JsOnLoad(const JSCallbackInfo & args)321 void JSXComponent::JsOnLoad(const JSCallbackInfo& args)
322 {
323     if (args.Length() < 1 || !args[0]->IsFunction()) {
324         return;
325     }
326     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
327     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
328     auto onLoad = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
329                       const std::string& xcomponentId) {
330         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
331         ACE_SCORING_EVENT("XComponent.onLoad");
332         PipelineContext::SetCallBackNode(node);
333         std::vector<std::string> keys = { "load", xcomponentId };
334         func->ExecuteNew(keys, "");
335         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] onLoad triggers", xcomponentId.c_str());
336     };
337     XComponentModel::GetInstance()->SetOnLoad(std::move(onLoad));
338 }
339 
RegisterOnCreate(const JsiExecutionContext & execCtx,const Local<JSValueRef> & func)340 void JSXComponent::RegisterOnCreate(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)
341 {
342     auto frameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
343     CHECK_NULL_VOID(frameNode);
344 
345     if (!func->IsFunction(execCtx.vm_)) {
346         return;
347     }
348 
349     auto jsFunc = panda::Global<panda::FunctionRef>(execCtx.vm_, Local<panda::FunctionRef>(func));
350     auto onLoad = [execCtx, funcRef = std::move(jsFunc), node = AceType::WeakClaim(AceType::RawPtr(frameNode))](
351                       const std::string& xcomponentId) {
352         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
353         ACE_SCORING_EVENT("XComponentNode.onCreate");
354         PipelineContext::SetCallBackNode(node);
355         std::vector<Local<JSValueRef>> argv;
356         JSRef<JSVal> jsVal;
357         if (XComponentClient::GetInstance().GetJSVal(xcomponentId, jsVal)) {
358             argv.emplace_back(jsVal->GetLocalHandle());
359         }
360         funcRef->Call(execCtx.vm_, JSNApi::GetGlobalObject(execCtx.vm_), argv.data(), argv.size());
361     };
362     XComponentModel::GetInstance()->RegisterOnCreate(frameNode, std::move(onLoad));
363 }
364 
RegisterOnDestroy(const JsiExecutionContext & execCtx,const Local<JSValueRef> & func)365 void JSXComponent::RegisterOnDestroy(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)
366 {
367     auto frameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
368     CHECK_NULL_VOID(frameNode);
369 
370     if (!func->IsFunction(execCtx.vm_)) {
371         return;
372     }
373 
374     auto jsFunc = panda::Global<panda::FunctionRef>(execCtx.vm_, Local<panda::FunctionRef>(func));
375     auto onDestroy = [execCtx, funcRef = std::move(jsFunc), node = AceType::WeakClaim(AceType::RawPtr(frameNode))](
376                          const std::string& xcomponentId) {
377         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
378         ACE_SCORING_EVENT("XComponentNode.onDestroy");
379         PipelineContext::SetCallBackNode(node);
380         funcRef->Call(execCtx.vm_, JSNApi::GetGlobalObject(execCtx.vm_), nullptr, 0);
381     };
382     XComponentModel::GetInstance()->RegisterOnDestroy(frameNode, std::move(onDestroy));
383 }
384 
JsOnDestroy(const JSCallbackInfo & args)385 void JSXComponent::JsOnDestroy(const JSCallbackInfo& args)
386 {
387     if (args.Length() < 1 || !args[0]->IsFunction()) {
388         return;
389     }
390     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
391     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
392     auto onDestroy = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
393                          const std::string& xcomponentId) {
394         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
395         ACE_SCORING_EVENT("XComponent.onDestroy");
396         PipelineContext::SetCallBackNode(node);
397         std::vector<std::string> keys = { "destroy" };
398         func->Execute(keys, "");
399         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] onDestroy", xcomponentId.c_str());
400     };
401     XComponentModel::GetInstance()->SetOnDestroy(std::move(onDestroy));
402 }
403 
JsOnAppear(const JSCallbackInfo & args)404 void JSXComponent::JsOnAppear(const JSCallbackInfo& args)
405 {
406     auto type = XComponentModel::GetInstance()->GetType();
407     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
408     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
409         return;
410     }
411     JSInteractableView::JsOnAppear(args);
412 }
413 
JsOnDisAppear(const JSCallbackInfo & args)414 void JSXComponent::JsOnDisAppear(const JSCallbackInfo& args)
415 {
416     auto type = XComponentModel::GetInstance()->GetType();
417     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
418     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
419         return;
420     }
421     JSInteractableView::JsOnDisAppear(args);
422 }
423 
JsOnAttach(const JSCallbackInfo & args)424 void JSXComponent::JsOnAttach(const JSCallbackInfo& args)
425 {
426     auto type = XComponentModel::GetInstance()->GetType();
427     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
428     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
429         return;
430     }
431     JSInteractableView::JsOnAttach(args);
432 }
433 
JsOnDetach(const JSCallbackInfo & args)434 void JSXComponent::JsOnDetach(const JSCallbackInfo& args)
435 {
436     auto type = XComponentModel::GetInstance()->GetType();
437     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
438     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
439         return;
440     }
441     JSInteractableView::JsOnDetach(args);
442 }
443 
JsOnTouch(const JSCallbackInfo & args)444 void JSXComponent::JsOnTouch(const JSCallbackInfo& args)
445 {
446     auto type = XComponentModel::GetInstance()->GetType();
447     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
448     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
449         return;
450     }
451     JSInteractableView::JsOnTouch(args);
452 }
453 
JsOnClick(const JSCallbackInfo & args)454 void JSXComponent::JsOnClick(const JSCallbackInfo& args)
455 {
456     auto type = XComponentModel::GetInstance()->GetType();
457     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
458     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
459         return;
460     }
461     JSViewAbstract::JsOnClick(args);
462 }
463 
JsOnKeyEvent(const JSCallbackInfo & args)464 void JSXComponent::JsOnKeyEvent(const JSCallbackInfo& args)
465 {
466     auto type = XComponentModel::GetInstance()->GetType();
467     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
468     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
469         return;
470     }
471     JSViewAbstract::JsOnKeyEvent(args);
472 }
473 
JsOnMouse(const JSCallbackInfo & args)474 void JSXComponent::JsOnMouse(const JSCallbackInfo& args)
475 {
476     auto type = XComponentModel::GetInstance()->GetType();
477     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
478     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
479         return;
480     }
481     JSViewAbstract::JsOnMouse(args);
482 }
483 
JsOnHover(const JSCallbackInfo & args)484 void JSXComponent::JsOnHover(const JSCallbackInfo& args)
485 {
486     auto type = XComponentModel::GetInstance()->GetType();
487     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
488     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
489         return;
490     }
491     JSViewAbstract::JsOnHover(args);
492 }
493 
494 
JsOnFocus(const JSCallbackInfo & args)495 void JSXComponent::JsOnFocus(const JSCallbackInfo& args)
496 {
497     auto type = XComponentModel::GetInstance()->GetType();
498     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
499     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
500         return;
501     }
502     JSViewAbstract::JsOnFocus(args);
503 }
504 
JsOnBlur(const JSCallbackInfo & args)505 void JSXComponent::JsOnBlur(const JSCallbackInfo& args)
506 {
507     auto type = XComponentModel::GetInstance()->GetType();
508     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
509     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
510         return;
511     }
512     JSViewAbstract::JsOnBlur(args);
513 }
514 
JsBackgroundColor(const JSCallbackInfo & args)515 void JSXComponent::JsBackgroundColor(const JSCallbackInfo& args)
516 {
517     auto type = XComponentModel::GetInstance()->GetType();
518     if (!XComponentModel::IsBackGroundColorAvailable(type)) {
519         return;
520     }
521     if (args.Length() < 1) {
522         return;
523     }
524     Color backgroundColor;
525     if (!ParseJsColor(args[0], backgroundColor)) {
526         backgroundColor = (type == XComponentType::SURFACE) ? Color::BLACK : Color::TRANSPARENT;
527     }
528     ViewAbstractModel::GetInstance()->SetBackgroundColor(backgroundColor);
529 }
530 
JsBackgroundImage(const JSCallbackInfo & args)531 void JSXComponent::JsBackgroundImage(const JSCallbackInfo& args)
532 {
533     auto type = XComponentModel::GetInstance()->GetType();
534     if (type != XComponentType::NODE) {
535         return;
536     }
537     JSViewAbstract::JsBackgroundImage(args);
538 }
539 
JsBackgroundImageSize(const JSCallbackInfo & args)540 void JSXComponent::JsBackgroundImageSize(const JSCallbackInfo& args)
541 {
542     auto type = XComponentModel::GetInstance()->GetType();
543     if (type != XComponentType::NODE) {
544         return;
545     }
546     JSViewAbstract::JsBackgroundImageSize(args);
547 }
548 
JsBackgroundImagePosition(const JSCallbackInfo & args)549 void JSXComponent::JsBackgroundImagePosition(const JSCallbackInfo& args)
550 {
551     auto type = XComponentModel::GetInstance()->GetType();
552     if (type != XComponentType::NODE) {
553         return;
554     }
555     JSViewAbstract::JsBackgroundImagePosition(args);
556 }
557 
558 
JsOpacity(const JSCallbackInfo & args)559 void JSXComponent::JsOpacity(const JSCallbackInfo& args)
560 {
561     auto type = XComponentModel::GetInstance()->GetType();
562     if (type == XComponentType::SURFACE || type == XComponentType::COMPONENT) {
563         return;
564     }
565     JSViewAbstract::JsOpacity(args);
566 }
567 
JsBlur(const JSCallbackInfo & args)568 void JSXComponent::JsBlur(const JSCallbackInfo& args)
569 {
570     auto type = XComponentModel::GetInstance()->GetType();
571     if (type != XComponentType::NODE) {
572         return;
573     }
574     JSViewAbstract::JsBlur(args);
575 }
576 
JsBackdropBlur(const JSCallbackInfo & args)577 void JSXComponent::JsBackdropBlur(const JSCallbackInfo& args)
578 {
579     auto type = XComponentModel::GetInstance()->GetType();
580     if (type != XComponentType::NODE) {
581         return;
582     }
583     JSViewAbstract::JsBackdropBlur(args);
584 }
585 
JsGrayscale(const JSCallbackInfo & args)586 void JSXComponent::JsGrayscale(const JSCallbackInfo& args)
587 {
588     auto type = XComponentModel::GetInstance()->GetType();
589     if (type != XComponentType::NODE) {
590         return;
591     }
592    // JSViewAbstract::JsGrayscale(args);
593 }
594 
JsBrightness(const JSCallbackInfo & args)595 void JSXComponent::JsBrightness(const JSCallbackInfo& args)
596 {
597     auto type = XComponentModel::GetInstance()->GetType();
598     if (type != XComponentType::NODE) {
599         return;
600     }
601     JSViewAbstract::JsBrightness(args);
602 }
603 
JsSaturate(const JSCallbackInfo & args)604 void JSXComponent::JsSaturate(const JSCallbackInfo& args)
605 {
606     auto type = XComponentModel::GetInstance()->GetType();
607     if (type != XComponentType::NODE) {
608         return;
609     }
610     JSViewAbstract::JsSaturate(args);
611 }
612 
JsContrast(const JSCallbackInfo & args)613 void JSXComponent::JsContrast(const JSCallbackInfo& args)
614 {
615     auto type = XComponentModel::GetInstance()->GetType();
616     if (type != XComponentType::NODE) {
617         return;
618     }
619     JSViewAbstract::JsContrast(args);
620 }
621 
JsInvert(const JSCallbackInfo & args)622 void JSXComponent::JsInvert(const JSCallbackInfo& args)
623 {
624     auto type = XComponentModel::GetInstance()->GetType();
625     if (type != XComponentType::NODE) {
626         return;
627     }
628     JSViewAbstract::JsInvert(args);
629 }
630 
JsSepia(const JSCallbackInfo & args)631 void JSXComponent::JsSepia(const JSCallbackInfo& args)
632 {
633     auto type = XComponentModel::GetInstance()->GetType();
634     if (type != XComponentType::NODE) {
635         return;
636     }
637     JSViewAbstract::JsSepia(args);
638 }
639 
JsHueRotate(const JSCallbackInfo & args)640 void JSXComponent::JsHueRotate(const JSCallbackInfo& args)
641 {
642     auto type = XComponentModel::GetInstance()->GetType();
643     if (type != XComponentType::NODE) {
644         return;
645     }
646     JSViewAbstract::JsHueRotate(args);
647 }
648 
JsColorBlend(const JSCallbackInfo & args)649 void JSXComponent::JsColorBlend(const JSCallbackInfo& args)
650 {
651     auto type = XComponentModel::GetInstance()->GetType();
652     if (type != XComponentType::NODE) {
653         return;
654     }
655     JSViewAbstract::JsColorBlend(args);
656 }
657 
JsSphericalEffect(const JSCallbackInfo & args)658 void JSXComponent::JsSphericalEffect(const JSCallbackInfo& args)
659 {
660     auto type = XComponentModel::GetInstance()->GetType();
661     if (type != XComponentType::NODE) {
662         return;
663     }
664     JSViewAbstract::JsSphericalEffect(args);
665 }
666 
JsLightUpEffect(const JSCallbackInfo & args)667 void JSXComponent::JsLightUpEffect(const JSCallbackInfo& args)
668 {
669     auto type = XComponentModel::GetInstance()->GetType();
670     if (type != XComponentType::NODE) {
671         return;
672     }
673     JSViewAbstract::JsLightUpEffect(args);
674 }
675 
JsPixelStretchEffect(const JSCallbackInfo & args)676 void JSXComponent::JsPixelStretchEffect(const JSCallbackInfo& args)
677 {
678     auto type = XComponentModel::GetInstance()->GetType();
679     if (type != XComponentType::NODE) {
680         return;
681     }
682     JSViewAbstract::JsPixelStretchEffect(args);
683 }
684 
JsLinearGradientBlur(const JSCallbackInfo & args)685 void JSXComponent::JsLinearGradientBlur(const JSCallbackInfo& args)
686 {
687     auto type = XComponentModel::GetInstance()->GetType();
688     if (type != XComponentType::NODE) {
689         return;
690     }
691     JSViewAbstract::JsLinearGradientBlur(args);
692 }
693 
JsEnableAnalyzer(bool enable)694 void JSXComponent::JsEnableAnalyzer(bool enable)
695 {
696     auto type = XComponentModel::GetInstance()->GetType();
697     if (type == XComponentType::COMPONENT || type == XComponentType::NODE) {
698         return;
699     }
700     XComponentModel::GetInstance()->EnableAnalyzer(enable);
701 }
702 
JsRenderFit(const JSCallbackInfo & args)703 void JSXComponent::JsRenderFit(const JSCallbackInfo& args)
704 {
705     auto type = XComponentModel::GetInstance()->GetType();
706     if (type == XComponentType::COMPONENT || type == XComponentType::NODE || args.Length() != 1) {
707         return;
708     }
709     if (type == XComponentType::TEXTURE) {
710         JSViewAbstract::JSRenderFit(args);
711         return;
712     }
713 
714     // set RenderFit on SurfaceNode when type is SURFACE
715     RenderFit renderFit = RenderFit::RESIZE_FILL;
716     if (args[0]->IsNumber()) {
717         int32_t fitNumber = args[0]->ToNumber<int32_t>();
718         if (fitNumber >= static_cast<int32_t>(RenderFit::CENTER) &&
719             fitNumber <= static_cast<int32_t>(RenderFit::RESIZE_COVER_BOTTOM_RIGHT)) {
720             renderFit = static_cast<RenderFit>(fitNumber);
721         }
722     }
723     XComponentModel::GetInstance()->SetRenderFit(renderFit);
724 }
725 
JsEnableSecure(const JSCallbackInfo & args)726 void JSXComponent::JsEnableSecure(const JSCallbackInfo& args)
727 {
728     auto type = XComponentModel::GetInstance()->GetType();
729     if (type != XComponentType::SURFACE || args.Length() != 1) {
730         return;
731     }
732     // set isSecure on SurfaceNode when type is SURFACE
733     if (args[0]->IsBoolean()) {
734         bool isSecure = args[0]->ToBoolean();
735         XComponentModel::GetInstance()->EnableSecure(isSecure);
736     }
737 }
738 } // namespace OHOS::Ace::Framework
739